2
0
mirror of https://github.com/frappe/frappe_docker.git synced 2024-12-25 19:21:09 +00:00
frappe_docker/build/frappe-worker/commands/utils.py

209 lines
5.4 KiB
Python
Raw Normal View History

2020-07-10 17:45:36 +00:00
import json
import os
import subprocess
2021-12-10 08:53:00 +00:00
2020-07-10 17:45:36 +00:00
import boto3
import git
2021-12-10 08:52:40 +00:00
from constants import APP_VERSIONS_JSON_FILE, APPS_TXT_FILE, COMMON_SITE_CONFIG_FILE
2021-12-10 08:53:00 +00:00
from frappe.installer import update_site_config
2021-12-10 08:52:40 +00:00
2020-07-10 10:48:25 +00:00
def run_command(command, stdout=None, stdin=None, stderr=None):
stdout = stdout or subprocess.PIPE
stderr = stderr or subprocess.PIPE
2020-07-10 13:51:59 +00:00
stdin = stdin or subprocess.PIPE
2020-07-10 10:48:25 +00:00
process = subprocess.Popen(command, stdout=stdout, stdin=stdin, stderr=stderr)
out, error = process.communicate()
if process.returncode:
print("Something went wrong:")
print(f"return code: {process.returncode}")
print(f"stdout:\n{out}")
print(f"\nstderr:\n{error}")
exit(process.returncode)
2020-07-10 17:45:36 +00:00
def save_version_file(versions):
2021-12-10 08:52:40 +00:00
with open(APP_VERSIONS_JSON_FILE, "w") as f:
2020-07-10 17:45:36 +00:00
return json.dump(versions, f, indent=1, sort_keys=True)
def get_apps():
apps = []
try:
with open(APPS_TXT_FILE) as apps_file:
for app in apps_file.readlines():
if app.strip():
apps.append(app.strip())
except FileNotFoundError as exception:
print(exception)
exit(1)
except Exception:
print(APPS_TXT_FILE + " is not valid")
exit(1)
return apps
def get_container_versions(apps):
versions = {}
for app in apps:
try:
version = __import__(app).__version__
versions.update({app: version})
except Exception:
pass
try:
2021-12-10 08:52:40 +00:00
path = os.path.join("..", "apps", app)
2020-07-10 17:45:36 +00:00
repo = git.Repo(path)
commit_hash = repo.head.object.hexsha
2021-12-10 08:52:40 +00:00
versions.update({app + "_git_hash": commit_hash})
2020-07-10 17:45:36 +00:00
except Exception:
pass
return versions
def get_version_file():
versions = None
try:
with open(APP_VERSIONS_JSON_FILE) as versions_file:
versions = json.load(versions_file)
except Exception:
pass
return versions
def get_config():
config = None
try:
with open(COMMON_SITE_CONFIG_FILE) as config_file:
config = json.load(config_file)
except FileNotFoundError as exception:
print(exception)
exit(1)
except Exception:
print(COMMON_SITE_CONFIG_FILE + " is not valid")
exit(1)
return config
def get_site_config(site_name):
site_config = None
2021-12-10 08:52:40 +00:00
with open(f"{site_name}/site_config.json") as site_config_file:
2020-07-10 17:45:36 +00:00
site_config = json.load(site_config_file)
return site_config
def save_config(config):
2021-12-10 08:52:40 +00:00
with open(COMMON_SITE_CONFIG_FILE, "w") as f:
2020-07-10 17:45:36 +00:00
return json.dump(config, f, indent=1, sort_keys=True)
def get_password(env_var, default=None):
2021-12-10 08:52:40 +00:00
return (
os.environ.get(env_var)
or get_password_from_secret(f"{env_var}_FILE")
or default
)
2020-07-10 17:45:36 +00:00
def get_password_from_secret(env_var):
"""Fetches the secret value from the docker secret file
usually located inside /run/secrets/
Arguments:
env_var {str} -- Name of the environment variable
containing the path to the secret file.
Returns:
[str] -- Secret value
"""
passwd = None
secret_file_path = os.environ.get(env_var)
if secret_file_path:
with open(secret_file_path) as secret_file:
passwd = secret_file.read().strip()
return passwd
def get_s3_config():
check_s3_environment_variables()
2021-12-10 08:52:40 +00:00
bucket = os.environ.get("BUCKET_NAME")
2020-07-10 17:45:36 +00:00
conn = boto3.client(
2021-12-10 08:52:40 +00:00
"s3",
region_name=os.environ.get("REGION"),
aws_access_key_id=os.environ.get("ACCESS_KEY_ID"),
aws_secret_access_key=os.environ.get("SECRET_ACCESS_KEY"),
endpoint_url=os.environ.get("ENDPOINT_URL"),
2020-07-10 17:45:36 +00:00
)
return conn, bucket
def upload_file_to_s3(filename, folder, conn, bucket):
destpath = os.path.join(folder, os.path.basename(filename))
try:
print("Uploading file:", filename)
conn.upload_file(filename, bucket, destpath)
except Exception as e:
print("Error uploading: %s" % (e))
exit(1)
def list_directories(path):
directories = []
for name in os.listdir(path):
if os.path.isdir(os.path.join(path, name)):
directories.append(name)
return directories
def get_site_config_from_path(site_config_path):
site_config = dict()
if os.path.exists(site_config_path):
2021-12-10 08:51:47 +00:00
with open(site_config_path) as sc:
2020-07-10 17:45:36 +00:00
site_config = json.load(sc)
return site_config
def set_key_in_site_config(key, site, site_config_path):
site_config = get_site_config_from_path(site_config_path)
value = site_config.get(key)
if value:
2021-12-10 08:52:40 +00:00
print(f"Set {key} in site config for site: {site}")
update_site_config(
key,
value,
site_config_path=os.path.join(os.getcwd(), site, "site_config.json"),
)
2020-07-10 17:45:36 +00:00
def check_s3_environment_variables():
2021-12-10 08:52:40 +00:00
if "BUCKET_NAME" not in os.environ:
print("Variable BUCKET_NAME not set")
2020-07-10 17:45:36 +00:00
exit(1)
2021-12-10 08:52:40 +00:00
if "ACCESS_KEY_ID" not in os.environ:
print("Variable ACCESS_KEY_ID not set")
2020-07-10 17:45:36 +00:00
exit(1)
2021-12-10 08:52:40 +00:00
if "SECRET_ACCESS_KEY" not in os.environ:
print("Variable SECRET_ACCESS_KEY not set")
2020-07-10 17:45:36 +00:00
exit(1)
2021-12-10 08:52:40 +00:00
if "ENDPOINT_URL" not in os.environ:
print("Variable ENDPOINT_URL not set")
2020-07-10 17:45:36 +00:00
exit(1)
2021-12-10 08:52:40 +00:00
if "BUCKET_DIR" not in os.environ:
print("Variable BUCKET_DIR not set")
2020-07-10 17:45:36 +00:00
exit(1)
2021-12-10 08:52:40 +00:00
if "REGION" not in os.environ:
print("Variable REGION not set")
2020-07-10 17:45:36 +00:00
exit(1)