#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os 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 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 an activation key act_key = utils.get_conf('SKYREACH_ACTIVATION_KEY') if not act_key: utils.log('\033[1;33m No activation key is set, skipping configuration download. \033[0m') return req = requests.post(sk_url + '/erp/credentials/envsetup-conf.sh', data=dict(key=act_key, public_key=public_key), verify=verify, timeout=20) # 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.')