import click import subprocess as sp import os from app.helper import clone_etcd_wrapper @click.group() def file_scan(): pass @file_scan.command("setup") @click.option("--path", required=True) @click.option("--base_dir", required=True) @click.option("--file_prefix", required=True) @click.option("--etcd_url", required=True) def setup(path, base_dir, file_prefix, etcd_url): os.chdir(path) repo_name = "ucloud-file-scan" # Clone main repository sp.check_output(['git', 'clone', f'https://code.ungleich.ch/ungleich-public/{repo_name}.git' '-b', 'wip']) # Create .env file with open(os.path.join(repo_name, ".env"), "w") as f: content = ( f"BASE_DIR={base_dir}\n" f"FILE_PREFIX={file_prefix}\n" f"ETCD_URL={etcd_url}\n" ) f.writelines(content) # Clone etcd wrapper clone_etcd_wrapper() # Create virtualenv with site-packages enabled and install all dependencies sp.check_output(['pipenv','--site-packages', '--python', '3'], path=repo_name) sp.check_output(['pipenv', 'install'], path=repo_name) # 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" )