From 9ae0c66f7336803c0bdfc9b35bcc4e68b3b1f013 Mon Sep 17 00:00:00 2001 From: Max Fowler Date: Tue, 10 Nov 2020 16:03:45 +0100 Subject: [PATCH] Python script version --- conf/templates/debian/distributions | 8 +++++ conf/templates/debian/options | 3 ++ conf/templates/debian/override.buster | 4 +++ conf/templates/devdocs/git-post-receive | 13 +++++++++ conf/templates/nginx/nginx.conf | 31 ++++++++++++++++++++ conf/templates/nginx/nginx_devdocs.conf | 10 +++++++ peach_vps_scripts/__init__.py | 0 peach_vps_scripts/setup_vps.py | 39 +++++++++++++++++++++++++ peach_vps_scripts/utils.py | 32 ++++++++++++++++++++ peach_vps_scripts/vars.py | 11 +++++++ requirements.txt | 1 + 11 files changed, 152 insertions(+) create mode 100644 conf/templates/debian/distributions create mode 100644 conf/templates/debian/options create mode 100644 conf/templates/debian/override.buster create mode 100644 conf/templates/devdocs/git-post-receive create mode 100644 conf/templates/nginx/nginx.conf create mode 100644 conf/templates/nginx/nginx_devdocs.conf create mode 100644 peach_vps_scripts/__init__.py create mode 100644 peach_vps_scripts/setup_vps.py create mode 100644 peach_vps_scripts/utils.py create mode 100644 peach_vps_scripts/vars.py create mode 100644 requirements.txt diff --git a/conf/templates/debian/distributions b/conf/templates/debian/distributions new file mode 100644 index 0000000..f6fed0f --- /dev/null +++ b/conf/templates/debian/distributions @@ -0,0 +1,8 @@ +Origin: PeachCloud +Label: PeachCloud +Codename: buster +Architectures: amd64 +Components: main +Description: Apt repository for PeachCloud debian packages +SignWith: {{gpg_key_id}} +DebOverride: override.buster diff --git a/conf/templates/debian/options b/conf/templates/debian/options new file mode 100644 index 0000000..5dd7345 --- /dev/null +++ b/conf/templates/debian/options @@ -0,0 +1,3 @@ +verbose +basedir {{debian_rep_dir}} +ask-passphrase diff --git a/conf/templates/debian/override.buster b/conf/templates/debian/override.buster new file mode 100644 index 0000000..ec9fb2d --- /dev/null +++ b/conf/templates/debian/override.buster @@ -0,0 +1,4 @@ +{% for service in services %} +{{service}} Priority optional +{{service}} Section net +{% endfor %} \ No newline at end of file diff --git a/conf/templates/devdocs/git-post-receive b/conf/templates/devdocs/git-post-receive new file mode 100644 index 0000000..36b37bd --- /dev/null +++ b/conf/templates/devdocs/git-post-receive @@ -0,0 +1,13 @@ +#!/bin/bash +while read oldrev newrev ref +do + if [[ $ref =~ .*/master$ ]]; + then + echo "Master ref received. Deploying master branch to build directory..." + git --work-tree={{src_dir}}/devdocs_build --git-dir={{src_dir}}/devdocs_bare checkout -f + echo "Building docs and deploying to production..." + /root/.cargo/bin/mdbook build {{src_dir}}/devdocs_build --dest-dir {{web_dir}}/docs:peachcloud:org/html + else + echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server." + fi +done \ No newline at end of file diff --git a/conf/templates/nginx/nginx.conf b/conf/templates/nginx/nginx.conf new file mode 100644 index 0000000..668b54c --- /dev/null +++ b/conf/templates/nginx/nginx.conf @@ -0,0 +1,31 @@ +user www-data; +worker_processes 1; +worker_rlimit_nofile 8192; + +events { + worker_connections 3000; +} + +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + #gzip on; + + include /etc/nginx/conf.d/*.conf; + include /etc/nginx/sites-enabled/*; +} \ No newline at end of file diff --git a/conf/templates/nginx/nginx_devdocs.conf b/conf/templates/nginx/nginx_devdocs.conf new file mode 100644 index 0000000..c97291c --- /dev/null +++ b/conf/templates/nginx/nginx_devdocs.conf @@ -0,0 +1,10 @@ +server { + listen 80; + server_name 159.89.5.141; + + location / { + root {{web_dir}}/docs:peachcloud:org/html; + index index.html; + try_files $uri $uri/ /index.html; + } +} \ No newline at end of file diff --git a/peach_vps_scripts/__init__.py b/peach_vps_scripts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/peach_vps_scripts/setup_vps.py b/peach_vps_scripts/setup_vps.py new file mode 100644 index 0000000..4001f1d --- /dev/null +++ b/peach_vps_scripts/setup_vps.py @@ -0,0 +1,39 @@ +from peach_vps_scripts.utils import render_template, cargo_install +from peach_vps_scripts.vars import VARS + +import subprocess +import os + + +print("[ UPDATING OPERATING SYSTEM ]") +subprocess.call(["apt-get", "update", "-y"]) +subprocess.call(["apt-get", "upgrade", "-y"]) + +print("[ INSTALLING SYSTEM REQUIREMENTS ]") +subprocess.call(["apt-get", "install", "git", "nginx", "curl", "build-essential", "mosh"]) + +print("[ CREATING SYSTEM GROUPS ]") +subprocess.call(["/usr/sbin/groupadd", "peach"]) + +print("[ ADDING SYSTEM USER ]") +users = ["notplants", "glyph"] +for user in users: + subprocess.call(["/usr/sbin/adduser", user]) + subprocess.call(["usermod", "-aG", "sudo", user]) + subprocess.call(["/usr/sbin/usermod", "-a", "-G", user, "peach"]) + +print("[ CREATING DIRECTORIES ]") +folders = [VARS["src_dir"], VARS["www_dir"], VARS["debian_repo_dir"]] +for folder in folders: + if not os.path.exists(folder): + os.makedirs(folder) + +print("[ INSTALLING RUST ]") +subprocess.call(["curl", "https://sh.rustup.rs", "-sSf", "|", "sh", "-s", "--", "-y"]) + +print("[ INSTALLING CARGO PACKAGES ]") +cargo_install("cargo-deb") + +render_template(src='nginx/nginx.conf', dest='/etc/nginx/nginx.conf') + + diff --git a/peach_vps_scripts/utils.py b/peach_vps_scripts/utils.py new file mode 100644 index 0000000..8533937 --- /dev/null +++ b/peach_vps_scripts/utils.py @@ -0,0 +1,32 @@ +from peach_vps_scripts.vars import VARS + +import os +import jinja2 +import subprocess + +PROJECT_PATH = os.path.abspath(os.path.dirname(__file__)) + +template_path = os.path.join(PROJECT_PATH, 'templates') +template_loader = jinja2.FileSystemLoader(searchpath=template_path) +template_env = jinja2.Environment(loader=template_loader) + + +def render_template(src, dest, template_vars=None): + """ + :param src: relative string path to jinja template file + :param dest: absolute string path of output destination file + :param template_vars: variables to render template with + :return: None + """ + template = template_env.get_template(src) + template_vars.update(VARS) + output_text = template.render(template_vars=template_vars) + if os.path.exists(dest): + os.remove(dest) + with open(dest, 'w') as f: + f.write(output_text) + + +def cargo_install(package): + subprocess.call(['/root/.cargo/bin/cargo', 'install', package]) + diff --git a/peach_vps_scripts/vars.py b/peach_vps_scripts/vars.py new file mode 100644 index 0000000..8b66352 --- /dev/null +++ b/peach_vps_scripts/vars.py @@ -0,0 +1,11 @@ +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 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..758129b --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Jinja2==2.11.2