From e9feefa03529728db84c974b50e970f35209f421 Mon Sep 17 00:00:00 2001 From: Max Fowler Date: Thu, 12 Nov 2020 16:46:28 +0100 Subject: [PATCH] Basic working script --- .gitignore | 3 +- scripts/setup_debian_repo.py | 29 ++++++++++--------- scripts/setup_vps.py | 56 ------------------------------------ scripts/vars.py | 11 ------- 4 files changed, 18 insertions(+), 81 deletions(-) delete mode 100644 scripts/setup_vps.py delete mode 100644 scripts/vars.py diff --git a/.gitignore b/.gitignore index 5dacb78..fbe14c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ deploy.sh secret* secret_files* -ssh.sh \ No newline at end of file +ssh.sh +notes.txt \ No newline at end of file diff --git a/scripts/setup_debian_repo.py b/scripts/setup_debian_repo.py index 71740b1..a29b341 100644 --- a/scripts/setup_debian_repo.py +++ b/scripts/setup_debian_repo.py @@ -3,17 +3,17 @@ from utils import render_template import subprocess import os - INITIALIZE_DEBIAN_REPO = True -MICROSERVICES_SRC_DIR = "/srv/peachcloud/src" +MICROSERVICES_SRC_DIR = "/srv/peachcloud/automation/microservices" WEB_DIR = "/var/www/" APT_DIR = "/var/www/repos/apt" DEBIAN_REPO_DIR = "/var/www/repos/apt/debian" DEBIAN_REPO_CONF_DIR = "/var/www/repos/apt/debian/conf" # before running this script run `gpg --gen-key` on the server, and put the key id here -GPG_KEY_ID = "E62CD13A85763FCEC3EDBA8EA98440817F1A3CE5", +# `gpg --list-keys` +GPG_KEY_ID = "4ACEF251EA3E091167E8F03EBF69A52BE3565476" SERVICES = [ {"name": "peach-oled", "repo_url": "https://github.com/peachcloud/peach-oled.git"}, @@ -38,10 +38,12 @@ if INITIALIZE_DEBIAN_REPO: first_command.wait() print("[ INSTALLING CARGO-DEB ]") - subprocess.call(['cargo', 'install', 'cargo-deb']) + if not os.path.exists("/root/.cargo/bin/cargo-deb"): + subprocess.call(["/root/.cargo/bin/cargo", "install", "cargo-deb"]) print("[ INSTALL TOOLCHAIN FOR CROSS-COMPILATION ]") - subprocess.call(['rustup', 'toolchain', 'install', 'nightly-aarch64-unknown-linux-gnu']) + subprocess.call(["/root/.cargo/bin/rustup", "target", "add", "aarch64-unknown-linux-gnu"]) + subprocess.call(["/root/.cargo/bin/rustup", "toolchain", "install", "nightly-aarch64-unknown-linux-gnu"]) print("[ PULLING MICROSERVICES CODE FROM GITHUB ]") for service in SERVICES: @@ -76,7 +78,8 @@ if INITIALIZE_DEBIAN_REPO: print("[ EXPORTING PUBLIC GPG KEY ]") output_path = "{}/peach_pub.gpg".format(APT_DIR) - subprocess.call(["gpg", "--armor", "--output", output_path, "--export", GPG_KEY_ID]) + if not os.path.exists(output_path): + subprocess.call(["gpg", "--armor", "--output", output_path, "--export", GPG_KEY_ID]) print("[ COPYING NGINX CONFIG ]") render_template( @@ -91,15 +94,15 @@ if INITIALIZE_DEBIAN_REPO: # below is code for updating the microservices, building the microservices, # and adding them to the debian repo for service in SERVICES: - service_name = service['name'] + service_name = service["name"] service_path = os.path.join(MICROSERVICES_SRC_DIR, service_name) print("[ BUILIDING SERVICE {} ]".format(service_name)) - subprocess.call("cd {} && git pull;".format(service_path)) - subprocess.call("cd {} && cargo deb --target aarch64-unknown-linux-gnu;".format(service_path)) - deb_path = '?' - subprocess.call("cd {debian_dir} && reprepro includedeb buster {deb_path}".format( + subprocess.call(["git", "pull"], cwd=service_path) + debian_package_path = str(subprocess.check_output(["/root/.cargo/bin/cargo", "deb", "--target", "aarch64-unknown-linux-gnu"], cwd=service_path)) + print('OUTPUT: {}'.format(debian_package_path)) + subprocess.call("reprepro includedeb buster {deb_path}".format( debian_dir=DEBIAN_REPO_DIR, - deb_path=deb_path - )) + deb_path=debian_package_path + ), cwd=DEBIAN_REPO_DIR) diff --git a/scripts/setup_vps.py b/scripts/setup_vps.py deleted file mode 100644 index 24aa5e8..0000000 --- a/scripts/setup_vps.py +++ /dev/null @@ -1,56 +0,0 @@ -from utils import render_template, cargo_install -from vars import VARS - -import subprocess -import os -import pwd -import grp - - -print("[ UPDATING OPERATING SYSTEM ]") -subprocess.check_call(["apt-get", "update", "-y"]) -subprocess.check_call(["apt-get", "upgrade", "-y"]) - -print("[ INSTALLING SYSTEM REQUIREMENTS ]") -subprocess.check_call(["apt-get", "install", "git", "nginx", "curl", "build-essential", "mosh"]) - -print("[ CREATING SYSTEM GROUPS ]") -group = 'peach' -try: - grp.getgrnam(group) - # if group exists -except KeyError: - # if group doesn't eixst - subprocess.check_call(["/usr/sbin/groupadd", "peach"]) - -print("[ ADDING SYSTEM USER ]") -users = ["notplants", "glyph"] -for user in users: - try: - # if user exists - pwd.getpwnam(user) - except: - # if user does not exist - subprocess.check_call(["/usr/sbin/adduser", user]) - subprocess.check_call(["usermod", "-aG", "sudo", user]) - subprocess.check_call(["/usr/sbin/usermod", "-a", "-G", user, "peach"]) - -print("[ CREATING DIRECTORIES ]") -folders = [VARS["src_dir"], VARS["web_dir"], VARS["debian_rep_dir"]] -for folder in folders: - if not os.path.exists(folder): - os.makedirs(folder) - -print("[ INSTALLING RUST ]") -if not os.path.exists('/root/.cargo/bin/rustc'): - first_command = subprocess.Popen(["curl", "https://sh.rustup.rs", "-sSf"], stdout=subprocess.PIPE) - output = subprocess.check_output(["sh", "-s", "--", "-y"], stdin=first_command.stdout) - first_command.wait() - -print("[ INSTALLING CARGO PACKAGES ]") -cargo_install("cargo-deb") - -print("[ COPYING NGINX CONFIG ]") -render_template(src='nginx/nginx.conf', dest='/etc/nginx/nginx.conf') - - diff --git a/scripts/vars.py b/scripts/vars.py deleted file mode 100644 index 8b66352..0000000 --- a/scripts/vars.py +++ /dev/null @@ -1,11 +0,0 @@ -VARS = { - 'log_dir': '/srv/log', - 'src_dir': '/srv/src', - 'web_dir': 'srv/www', - 'debian_rep_dir': '/srv/www/repos/apt/debian', - 'gpg_key_id': 'E62CD13A85763FCEC3EDBA8EA98440817F1A3CE5', - 'services': [ - {'name': 'peach-oled', 'repo_url': 'https://github.com/peachcloud/peach-oled.git'}, - {'name': 'peach-oled', 'repo_url': 'https://github.com/peachcloud/peach-oled.git'} - ] -} \ No newline at end of file