dotfiles/gist-read-write.ipynb
2022-07-26 17:25:29 +02:00

210 lines
6 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "f2ec458c-c7a2-42ee-8816-b5ea8f8c0759",
"metadata": {},
"outputs": [],
"source": [
"# each computer should "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2075b6f7-9c53-454b-a6af-818f223f24b4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"python version: 3.9\n"
]
},
{
"data": {
"text/plain": [
"'id_rsa_mynetwork.pub'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# %pip install python-dotenv\n",
"# make a new token on:\n",
"# https://github.com/settings/tokens/new\n",
"# scope should only include gist\n",
"'''\n",
"# template .env file\n",
"token='ghp_<your gist token>'\n",
"gist_id='abc...123'\n",
"filename='<ssh-public-key-filename>'\n",
"'''\n",
"\n",
"from dotenv import load_dotenv\n",
"import requests\n",
"import json\n",
"import os\n",
"from pathlib import Path\n",
"import sys\n",
"\n",
"# check python version\n",
"version = sys.version_info\n",
"required_version = (3, 6)\n",
"if not version >= required_version:\n",
" raise EnvironmentError(\n",
" f'detected python version {version} - must be at least 3.6!'\n",
" )\n",
"print(f'python version: {version.major}.{version.minor}')\n",
"\n",
"### write current machiens public key to gist file\n",
"\n",
"# read env vars\n",
"# S/O https://stackoverflow.com/a/61029741\n",
"load_dotenv()\n",
"token = os.getenv('token')\n",
"gist_id = os.getenv('gist_id')\n",
"ssh_filename = os.getenv('filename')\n",
"ssh_filename"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0e534b35-8b71-4cb1-8dde-0eef9eb6dd36",
"metadata": {},
"outputs": [],
"source": [
"# get id_rsa_mynetwork public key\n",
"gist_filename = 'authorized-keys'\n",
"\n",
"\n",
"def append_gist(gist_id, data, append=False):\n",
" # request data\n",
" headers = {'Authorization': f'token {token}'}\n",
" request_url = f'https://api.github.com/gists/{gist_id}' \n",
"\n",
" # get gist contents (all-files)\n",
" request_data = json.dumps({'files':gist_filename})\n",
" r = requests.get(\n",
" url=request_url,\n",
" data=request_data,\n",
" headers=headers)\n",
" gist_content = r.json()\n",
" \n",
" # if gist file exists, get its data\n",
" if gist_filename in gist_content['files'].keys():\n",
" gist_text = gist_content['files'][gist_filename]['content']\n",
" # ... if not, set its data to empty-string\n",
" else:\n",
" gist_text = ''\n",
"\n",
" # if gist already contains data, exit\n",
" if data in gist_text:\n",
" return\n",
"\n",
" # append gist new contents to existing and patch\n",
" content = gist_text + '\\n'+ data\n",
" request_data = json.dumps({'files': {gist_filename: {\"content\": content}}})\n",
" r = requests.patch(\n",
" url=request_url,\n",
" data=request_data,\n",
" headers=headers)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "93ae1879-f245-4478-98fe-0a62e72c00f6",
"metadata": {},
"outputs": [],
"source": [
"# get ssh public key\n",
"ssh_pub = Path.home().joinpath(f'.ssh/{ssh_filename}').read_text()\n",
"\n",
"# write ssh key to gist\n",
"append_gist(\n",
" gist_id=gist_id,\n",
" data=ssh_pub)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fcda799c-3feb-4591-9582-f6187994fe36",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDm6wZlW45N+K24nw6uspMDofP6X+2T11XI8o1Z10gfieX1miJMCaDzlOvATYOKe/bzdkCR7vd8qNyKeJn/cSS95tvOCFi+LjryPzPp1bwuSqZSvtUIb9RD11exlXcXSawVEDmpOOElOiJs7lK7gulB4McIc/Y+ZxrSgo86/mFDOchaE2fWScC/QFVbNRRyuHa2/jlHtQW4ROODJIJXsu9OVMfrTrEdkXuwkNQ6d1KKKC5/IxbvmUtFC0vWEY7vuRVTBgGWv32n9RIhYePbpQeW/l5PwvrE+C3LmCgcQhi7y5NoZNvAoN3wAknQPAGIn0gZ5WukOUGlUkAdRwyJ007kXgiyfMVFV57HioO441rsVFCrOhYcQOMBhmO2a0V/y4aRG1hd0DJY/dBCzh8vVxYMq02h2ta+Sg89uxlcn4DAl5z7KqEkbPFJnLA67xUHvnvOLzKY5PXI7/3m8mbvOleeNGOOiBGRitLT+PbbVRxUg6yxVyL4T9ewOA9uMo0e3tSmaSEMBS2c8DJ4p1GVoFWKm0WqMF3GRWNUyzGEUZqcWblYjssjmx0G9L7lwI8JXHLXrxkhqmZyCE2atag7oWfrUirnQlcPaUX7BgpJbmKPTFK39jiOgij62y7DcbbQwKhSV6Bx8mRjPOaDVJuDKxuW0wIXdW2GY033V8gxPhbdRQ== dth@dth-MacBookAir\\n'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"### fetch gist file content\n",
"\n",
"headers = {'Authorization': f'token {token}'}\n",
"request_url = f'https://api.github.com/gists/{gist_id}' \n",
"request_data = json.dumps({'files':gist_filename})\n",
"r = requests.get(\n",
" url=request_url,\n",
" data=request_data,\n",
" headers=headers)\n",
"gist_content = r.json()['files'][gist_filename]['content']\n",
"gist_content"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "07a9cbfe-8ab1-42d0-9810-cf2b68c97b5d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'94.147.46.129'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gist_content"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}