ucloud-setup/app/helper.py

75 lines
2.6 KiB
Python
Raw Normal View History

2019-09-07 08:10:28 +00:00
import subprocess as sp
2019-09-11 19:16:34 +00:00
import subprocess as sp
import requests
import json
2019-08-28 11:42:50 +00:00
2019-09-11 19:16:34 +00:00
from decouple import config
2019-08-27 08:31:22 +00:00
2019-09-11 19:16:34 +00:00
def install_available(pipfile):
""" Install Python packages or their dependencies mentioned in
pipfile if they are available in System repos
"""
def is_available_in_system_repos(package):
try:
sp.check_output(['apk', 'info', f'py3-{package}'])
except:
return False
else:
return True
if get_distro_name() == "alpine":
dependencies = []
2019-08-28 11:42:50 +00:00
2019-09-11 19:16:34 +00:00
need_to_be_installed = []
2019-08-28 11:42:50 +00:00
2019-09-11 19:16:34 +00:00
try:
with open(pipfile) as f:
lines = f.readlines()
try:
start_index = lines.index("[packages]\n")
end_index = lines.index("\n", start_index)
for package in lines[start_index: end_index]:
if '= "*"' in package:
package = package[:package.index(" =")]
if is_available_in_system_repos(package):
need_to_be_installed.append(f"py3-{package}")
else:
dependencies.append(package)
except ValueError:
print("Not Found")
except:
return
else:
for dependency in dependencies:
content = requests.get(f"https://libraries.io/api/pypi/{dependency}/latest/dependencies?api_key={config('LIBRARIES_IO_API')}")
content = json.loads(content.content.decode("utf-8"))
for subdependency in content["dependencies"]:
subdependency = subdependency["name"]
if is_available_in_system_repos(subdependency):
need_to_be_installed.append(f"py3-{subdependency}")
for package in need_to_be_installed:
try:
sp.check_output(["apk", "add", package])
except:
print(f"Could not install {package}")
2019-08-27 08:31:22 +00:00
2019-09-07 08:10:28 +00:00
def clone_common(path='.'):
sp.check_output(['git', 'clone',
2019-09-08 16:00:53 +00:00
f'https://code.ungleich.ch/ungleich-public/ucloud_common'], cwd=path)
2019-08-27 08:31:22 +00:00
2019-08-28 11:42:50 +00:00
2019-09-07 08:10:28 +00:00
def clone_etcd_wrapper(path='.'):
sp.check_output(['git', 'clone',
2019-09-08 16:00:53 +00:00
f'https://code.ungleich.ch/ahmedbilal/etcd3_wrapper'], cwd=path)
2019-08-27 08:31:22 +00:00
2019-08-28 11:42:50 +00:00
2019-08-28 07:13:05 +00:00
def get_distro_name():
distro_name = None
with open("/etc/os-release") as f:
content = f.read()
start = content.find("ID=") + 3
end = content.find("\n", start)
distro_name = content[start:end]
2019-09-11 19:16:34 +00:00
return distro_name