From f69fea4256ea8067201211ccca819a4632651b6b Mon Sep 17 00:00:00 2001 From: Max Fowler Date: Tue, 10 Nov 2020 17:14:21 +0100 Subject: [PATCH] Working on python version --- .gitignore | 4 ++ README.md | 26 +++++++++- peach_vps_scripts/setup_vps.py | 39 --------------- {peach_vps_scripts => scripts}/__init__.py | 0 scripts/setup_vps.py | 55 ++++++++++++++++++++++ {peach_vps_scripts => scripts}/utils.py | 12 +++-- {peach_vps_scripts => scripts}/vars.py | 0 7 files changed, 91 insertions(+), 45 deletions(-) create mode 100644 .gitignore delete mode 100644 peach_vps_scripts/setup_vps.py rename {peach_vps_scripts => scripts}/__init__.py (100%) create mode 100644 scripts/setup_vps.py rename {peach_vps_scripts => scripts}/utils.py (71%) rename {peach_vps_scripts => scripts}/vars.py (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5dacb78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +deploy.sh +secret* +secret_files* +ssh.sh \ No newline at end of file diff --git a/README.md b/README.md index 7828353..8e91bd1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,24 @@ -# peach-vps -# simple-ansible-template +# peach-vps config + +Code for configuring the peachcloud vps for various hosting and automation +- debian repository of microservices +- mdbook builder for devdocs + + +# setup +``` +apt update +apt install git python python3-pip rsync +git clone https://github.com/peachcloud/peach-vps.git +cd peach-vps +pip3 install -r requirements.txt +python peach_vps_scripts/setup_vps.py +``` + + +# update +(for more frequent updates that don't involve the whole initial setup) +``` +cd peach-vps +python peach_vps_scripts/update_vps.py +``` \ No newline at end of file diff --git a/peach_vps_scripts/setup_vps.py b/peach_vps_scripts/setup_vps.py deleted file mode 100644 index 4001f1d..0000000 --- a/peach_vps_scripts/setup_vps.py +++ /dev/null @@ -1,39 +0,0 @@ -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/__init__.py b/scripts/__init__.py similarity index 100% rename from peach_vps_scripts/__init__.py rename to scripts/__init__.py diff --git a/scripts/setup_vps.py b/scripts/setup_vps.py new file mode 100644 index 0000000..3f85fe8 --- /dev/null +++ b/scripts/setup_vps.py @@ -0,0 +1,55 @@ +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 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") + +render_template(src='nginx/nginx.conf', dest='/etc/nginx/nginx.conf') + + diff --git a/peach_vps_scripts/utils.py b/scripts/utils.py similarity index 71% rename from peach_vps_scripts/utils.py rename to scripts/utils.py index 8533937..a9508cd 100644 --- a/peach_vps_scripts/utils.py +++ b/scripts/utils.py @@ -1,12 +1,13 @@ -from peach_vps_scripts.vars import VARS +from vars import VARS import os import jinja2 import subprocess -PROJECT_PATH = os.path.abspath(os.path.dirname(__file__)) +PROJECT_PATH = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) +print('PROJECT_PATH: {}'.format(PROJECT_PATH)) -template_path = os.path.join(PROJECT_PATH, 'templates') +template_path = os.path.join(PROJECT_PATH, 'conf/templates') template_loader = jinja2.FileSystemLoader(searchpath=template_path) template_env = jinja2.Environment(loader=template_loader) @@ -19,7 +20,10 @@ def render_template(src, dest, template_vars=None): :return: None """ template = template_env.get_template(src) - template_vars.update(VARS) + if template_vars: + template_vars.update(VARS) + else: + template_vars = VARS output_text = template.render(template_vars=template_vars) if os.path.exists(dest): os.remove(dest) diff --git a/peach_vps_scripts/vars.py b/scripts/vars.py similarity index 100% rename from peach_vps_scripts/vars.py rename to scripts/vars.py