Something went wrong on our end
-
Stéphane Diemer authoredStéphane Diemer authored
0_setup.py 3.16 KiB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import subprocess
import utils
def setup(interactive=True):
# Generate SSH key if not already done
if not os.path.exists('/root/.ssh/id_rsa'):
code, out = utils.exec_cmd('ssh-keygen -b 4096 -t rsa -f /root/.ssh/id_rsa -P ""')
if code != 0:
raise Exception('Failed to create SSH key: ssh-keygen returned code %s:\n%s' % (code, out))
with open('/root/.ssh/id_rsa.pub', 'r') as fo:
public_key = fo.read()
# Get requests module
if subprocess.run(['dpkg', '-s', 'python3-requests'], stdout=subprocess.DEVNULL).returncode != 0:
cmds = [
'apt-get update',
'apt-get install -y python3-requests',
]
utils.run_commands(cmds)
import requests
# Check skyreach url
verify = utils.get_conf('SKYREACH_SSL_VERIFY') != '0'
sk_url = utils.get_conf('SKYREACH_HOST')
if not sk_url:
raise Exception('No URL defined to contact Panel / Skyreach.')
sk_url = 'https://' + sk_url
req = requests.head(sk_url, verify=verify, timeout=20)
if req.status_code != 301:
raise Exception('Unexpected response from "%s": code %s, should have been 301.' % (sk_url, req.status_code))
# Get conf using API key if already set or using an activation key
req = None
api_key = utils.get_conf('SKYREACH_API_KEY')
if api_key:
req = requests.post(sk_url + '/erp/credentials/envsetup-conf.sh', params=dict(api_key=api_key), data=dict(public_key=public_key), verify=verify, timeout=20)
else:
act_key = utils.get_conf('SKYREACH_ACTIVATION_KEY')
if act_key:
req = requests.post(sk_url + '/erp/credentials/envsetup-conf.sh', data=dict(key=act_key, public_key=public_key), verify=verify, timeout=20)
if req is None:
utils.log('\033[1;33m No activation key nor API key are set, skipping configuration download. \033[0m')
return
# Write conf
if req.status_code != 200:
if len(req.text) > 300:
with open('/tmp/envsetup-conf-dl.txt', 'w') as fo:
fo.write(req.text)
raise Exception('Request on "%s" failed with status %s. Full response content available in "/tmp/envsetup-conf-dl.txt".' % (req.url, req.status_code))
else:
raise Exception('Request on "%s" failed with status %s. Response: "%s".' % (req.url, req.status_code, req.text))
path = os.path.join(os.path.dirname(os.path.dirname(utils.get_dir(__file__))), 'auto-generated-conf.sh')
utils.log('Configuration path: %s' % path)
with open(path, 'w') as fo:
fo.write(req.text)
utils.log('Configuration written.')
utils.log('Comment activation key in conf.sh')
path = os.path.join(os.path.dirname(os.path.dirname(utils.get_dir(__file__))), 'conf.sh')
with open(path, 'r') as fo:
content = fo.read()
content = content.replace('SKYREACH_ACTIVATION_KEY', '#SKYREACH_ACTIVATION_KEY').replace('##SKYREACH_ACTIVATION_KEY', '#SKYREACH_ACTIVATION_KEY')
with open(path, 'w') as fo:
fo.write(content)
utils.log('Autogenerate empty conf.')
cmds = [
'bash fill_empty_conf.sh',
]
utils.run_commands(cmds)