a
This commit is contained in:
parent
e000ba6dd8
commit
a9f8d245b9
10 changed files with 127 additions and 83 deletions
36
app/api.py
36
app/api.py
|
|
@ -11,6 +11,7 @@ from app.helper import (
|
|||
PipenvOperation,
|
||||
FileOperation,
|
||||
PackageManagementOperation,
|
||||
VirtualenvOperation
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -32,15 +33,24 @@ def api():
|
|||
default="https://otp.ungleich.ch/ungleichotp/",
|
||||
help="URL of ungleich OTP server",
|
||||
)
|
||||
def setup(path, auth_name, auth_seed, auth_realm,
|
||||
realm_allowed, otp_server, etcd_url,
|
||||
etcd_password):
|
||||
def setup(
|
||||
path,
|
||||
auth_name,
|
||||
auth_seed,
|
||||
auth_realm,
|
||||
realm_allowed,
|
||||
otp_server,
|
||||
etcd_url,
|
||||
etcd_password,
|
||||
):
|
||||
os.chdir(path)
|
||||
|
||||
|
||||
repo_name = "ucloud-api"
|
||||
|
||||
|
||||
# Git Operation Depends on success of previous operation i.e PackageManagementOperation
|
||||
op = GitOperation.clone(url=f"https://code.ungleich.ch/ungleich-public/{repo_name}.git")
|
||||
op = GitOperation.clone(
|
||||
url=f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
|
||||
)
|
||||
|
||||
content = (
|
||||
f"AUTH_NAME={auth_name}\n"
|
||||
|
|
@ -53,9 +63,7 @@ def setup(path, auth_name, auth_seed, auth_realm,
|
|||
)
|
||||
|
||||
# FileOperation depends on success of previos operation i.e GitOperation.clone
|
||||
op.add(
|
||||
FileOperation.write, path=os.path.join(repo_name, ".env"), content=content
|
||||
)
|
||||
op.add(FileOperation.write, path=os.path.join(repo_name, ".env"), content=content)
|
||||
|
||||
op.add(
|
||||
GitOperation.clone,
|
||||
|
|
@ -66,8 +74,10 @@ def setup(path, auth_name, auth_seed, auth_realm,
|
|||
op.add(
|
||||
GitOperation.clone,
|
||||
path=repo_name,
|
||||
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper"
|
||||
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
|
||||
)
|
||||
shutil.copytree(src=os.path.join(repo_name, "etcd3_wrapper"),
|
||||
dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"))
|
||||
op.add(PipenvOperation.install, path=repo_name)
|
||||
shutil.copytree(
|
||||
src=os.path.join(repo_name, "etcd3_wrapper"),
|
||||
dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"),
|
||||
)
|
||||
op.add(VirtualenvOperation.install, path=repo_name)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import click
|
|||
import os
|
||||
import subprocess
|
||||
|
||||
|
||||
@click.group()
|
||||
def ceph():
|
||||
pass
|
||||
|
|
@ -21,7 +22,7 @@ def setup(ceph_url, ssh_username):
|
|||
keys = "\n".join(keys)
|
||||
with open(os.path.expanduser("~/.ssh/known_hosts"), "a") as known_hosts:
|
||||
known_hosts.write(keys)
|
||||
|
||||
|
||||
os.makedirs("/etc/ceph", exist_ok=True)
|
||||
command = f"sftp -b ./ceph_batch_cmd {ssh_username}@{ceph_url}:/etc/ceph"
|
||||
subprocess.check_output(command.split())
|
||||
subprocess.check_output(command.split())
|
||||
|
|
|
|||
14
app/file.py
14
app/file.py
|
|
@ -32,10 +32,12 @@ def setup(path, base_dir, file_prefix, etcd_url, etcd_password):
|
|||
f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
|
||||
)
|
||||
|
||||
content = f"BASE_DIR={base_dir}\n" \
|
||||
f"FILE_PREFIX={file_prefix}\n" \
|
||||
f"ETCD_URL={etcd_url}\n" \
|
||||
f"ETCD_PASSWORD={etcd_password}\n"
|
||||
content = (
|
||||
f"BASE_DIR={base_dir}\n"
|
||||
f"FILE_PREFIX={file_prefix}\n"
|
||||
f"ETCD_URL={etcd_url}\n"
|
||||
f"ETCD_PASSWORD={etcd_password}\n"
|
||||
)
|
||||
|
||||
op_result.add(
|
||||
FileOperation.write, path=os.path.join(repo_name, ".env"), content=content
|
||||
|
|
@ -51,4 +53,6 @@ def setup(path, base_dir, file_prefix, etcd_url, etcd_password):
|
|||
|
||||
# Write Crontab entry
|
||||
with open("/etc/crontabs/root", "a") as crontab:
|
||||
crontab.write(f"*/5\t*\t*\t*\t*\tcd {os.path.join(os.getcwd(), repo_name)} && pipenv run python main.py")
|
||||
crontab.write(
|
||||
f"*/5\t*\t*\t*\t*\tcd {os.path.join(os.getcwd(), repo_name)} && pipenv run python main.py"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -68,6 +68,33 @@ class GitOperation(object):
|
|||
return Result(output, ResultType.success)
|
||||
|
||||
|
||||
class VirtualenvOperation(object):
|
||||
@staticmethod
|
||||
def create(path=".", site_packages=False):
|
||||
if site_packages:
|
||||
command = f"virtualenv ."
|
||||
else:
|
||||
command = "virtualenv --system-site-packages ."
|
||||
try:
|
||||
output = subprocess.check_output(command.split(), cwd=path)
|
||||
except subprocess.CalledProcessError as e:
|
||||
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
|
||||
else:
|
||||
return Result(output, ResultType.success)
|
||||
|
||||
@staticmethod
|
||||
def install(path=".", package_name=None):
|
||||
if package_name:
|
||||
command = f"pip install {package_name}"
|
||||
else:
|
||||
command = f"pip install"
|
||||
try:
|
||||
output = subprocess.check_output(command.split(), cwd=path)
|
||||
except subprocess.CalledProcessError as e:
|
||||
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
|
||||
else:
|
||||
return Result(output, ResultType.success)
|
||||
|
||||
class PipenvOperation(object):
|
||||
@staticmethod
|
||||
def create(path=".", site_packages=False):
|
||||
|
|
@ -89,7 +116,7 @@ class PipenvOperation(object):
|
|||
else:
|
||||
command = f"pipenv install"
|
||||
try:
|
||||
output = subprocess.check_output(command.split(), cwd=path, env={**os.environ, "PIP_INSTALL_OPTION": "--jobs=8"})
|
||||
output = subprocess.check_output(command.split(), cwd=path)
|
||||
except subprocess.CalledProcessError as e:
|
||||
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
|
||||
else:
|
||||
|
|
|
|||
10
app/host.py
10
app/host.py
|
|
@ -57,8 +57,10 @@ def setup(path, ssh_username, ssh_key_path, ssh_key_pass, etcd_url, etcd_passwor
|
|||
path=repo_name,
|
||||
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
|
||||
)
|
||||
|
||||
shutil.copytree(src=os.path.join(repo_name, "etcd3_wrapper"),
|
||||
dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"))
|
||||
|
||||
|
||||
shutil.copytree(
|
||||
src=os.path.join(repo_name, "etcd3_wrapper"),
|
||||
dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"),
|
||||
)
|
||||
|
||||
op_result.add(PipenvOperation.install, path=repo_name)
|
||||
|
|
|
|||
15
app/image.py
15
app/image.py
|
|
@ -31,23 +31,26 @@ def setup(path, base_dir, etcd_url, etcd_password):
|
|||
f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
|
||||
)
|
||||
|
||||
content = f"BASE_DIR={base_dir}\n" \
|
||||
f"ETCD_URL={etcd_url}\n" \
|
||||
f"ETCD_PASSWORD={etcd_password}\n"
|
||||
content = (
|
||||
f"BASE_DIR={base_dir}\n"
|
||||
f"ETCD_URL={etcd_url}\n"
|
||||
f"ETCD_PASSWORD={etcd_password}\n"
|
||||
)
|
||||
|
||||
op_result.add(
|
||||
FileOperation.write, path=os.path.join(repo_name, ".env"), content=content
|
||||
)
|
||||
|
||||
|
||||
op_result.add(
|
||||
GitOperation.clone,
|
||||
path=repo_name,
|
||||
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
|
||||
)
|
||||
|
||||
|
||||
op_result.add(PipenvOperation.install, path=repo_name)
|
||||
|
||||
# Write Crontab entry
|
||||
with open("/etc/crontabs/root", "a") as crontab:
|
||||
crontab.write(f"*/5\t*\t*\t*\t*\tcd {os.path.join(os.getcwd(), repo_name)} && pipenv run python main.py")
|
||||
crontab.write(
|
||||
f"*/5\t*\t*\t*\t*\tcd {os.path.join(os.getcwd(), repo_name)} && pipenv run python main.py"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -58,7 +58,9 @@ def setup(path, vm_prefix, host_prefix, request_prefix, etcd_url, etcd_password)
|
|||
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
|
||||
)
|
||||
|
||||
shutil.copytree(src=os.path.join(repo_name, "etcd3_wrapper"),
|
||||
dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"))
|
||||
|
||||
shutil.copytree(
|
||||
src=os.path.join(repo_name, "etcd3_wrapper"),
|
||||
dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"),
|
||||
)
|
||||
|
||||
op_result.add(PipenvOperation.install, path=repo_name)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue