ucloud-setup/app/image.py

61 lines
1.9 KiB
Python

import click
import subprocess as sp
import os
from app.helper import clone_etcd_wrapper, install_available
@click.group()
def image():
pass
@image.command("setup")
@click.option("--path", required=True)
@click.option("--base_dir", required=True)
@click.option("--etcd_url", required=True)
@click.option("--without_ceph", default=False, type=bool)
def setup(path, base_dir, etcd_url, without_ceph):
os.chdir(path)
repo_name = "ucloud-image-scanner"
# Clone main repo
sp.check_output(['git', 'clone', '--single-branch', '--branch', 'wip',
f'https://code.ungleich.ch/ungleich-public/{repo_name}.git'])
# Create .env file
with open(os.path.join(repo_name, ".env"), "w") as f:
content = (
f"BASE_DIR={base_dir}\n"
f"ETCD_URL={etcd_url}\n"
f"WITHOUT_CEPH={without_ceph}",
f"VM_PREFIX=/v1/vm\n",
f"HOST_PREFIX=/v1/host\n",
f"IMAGE_PREFIX=/v1/image\n",
f"IMAGE_STORE_PREFIX=/v1/image_store\n",
f"REQUEST_PREFIX=/v1/request\n",
f"FILE_PREFIX=/v1/file\n"
)
f.writelines(content)
# clone_etcd_wrapper(path=repo_name)
install_available(repo_name)
# Create virtualenv with site-packages enabled and install all dependencies
sp.check_output(['pipenv','--site-packages', '--python', '3'], cwd=repo_name)
sp.check_output(['pipenv', 'install'], cwd=repo_name)
# TODO: Devuan/Debian have crontab under /etc/crontab
# while Alpine have it under /etc/crontabs/root
# Detect in the following code where should we write
# our crontab entries
# Write Crontab entry
with open("/etc/crontabs/root", "a") as crontab:
crontab.write(
f"*/1\t*\t*\t*\t*\t$(cd {os.path.join(os.getcwd(), repo_name)} && pipenv run python main.py)\n"
)