Created
May 17, 2019 07:50
-
-
Save blakerouse/b41b685a3e5f80f33decec43799f93a0 to your computer and use it in GitHub Desktop.
Transmogrify a manual machine into a MAAS cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import ssl | |
import yaml | |
from subprocess import check_call | |
from pymongo import MongoClient | |
def parser(): | |
parser = argparse.ArgumentParser( | |
description=( | |
'Transmogrify the running manual provider into a ' | |
'MAAS controller.')) | |
parser.add_argument( | |
'--api-key', help='Authentication API key to communicate with MAAS.', | |
required=True) | |
parser.add_argument( | |
'--maas-url', help='URL to communicate with MAAS.', | |
required=True) | |
parser.add_argument( | |
'--system-id', help='MAAS node system-id for this machine.', | |
required=True) | |
parser.add_argument( | |
'--hostname', help='MAAS node hostname for this machine.', | |
required=True) | |
parser.add_argument( | |
'--ip-address', help='MAAS node ip-address for this machine.', | |
required=True) | |
return parser | |
def main(): | |
options = parser().parse_args() | |
with open('/var/lib/juju/agents/machine-0/agent.conf', 'r') as fp: | |
data = fp.read() | |
config = yaml.load(data) | |
check_call(['systemctl', 'stop', 'jujud-machine-0']) | |
url = "mongodb://%s:%s@localhost:37017/juju?authSource=admin&ssl=true&sslValidate=false" | |
db = MongoClient(url % (config['tag'], config['statepassword']), ssl_cert_reqs=ssl.CERT_NONE) | |
cloud = db.juju.clouds.find()[0] | |
cloud['type'] = 'maas' | |
cloud['auth-types'] = ['oauth1'] | |
cloud['endpoint'] = options.maas_url | |
db.juju.clouds.save(cloud) | |
credential = db.juju.cloudCredentials.find()[0] | |
credential['auth-type'] = 'oauth1' | |
credential['attributes'] = { | |
'maas-oauth': options.api_key, | |
} | |
db.juju.cloudCredentials.save(credential) | |
instanceData = db.juju.instanceData.find()[0] | |
instanceData['instanceid'] = options.system_id | |
instanceData['display-name'] = options.hostname | |
db.juju.instanceData.save(instanceData) | |
machine = db.juju.machines.find()[0] | |
machine['addresses'][0] = { | |
'value': options.ip_address, | |
'addresstype': 'ipv4', | |
'networkscope': 'local-cloud', | |
'origin': 'provider', | |
'spacename': 'undefined' | |
} | |
db.juju.machines.save(machine) | |
check_call(['systemctl', 'start', 'jujud-machine-0']) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment