Last active
March 17, 2017 19:14
-
-
Save niedbalski/eeb5630aea40a18d190c to your computer and use it in GitHub Desktop.
Convert a current running juju environment to a juju-deployer bundle.
This file contains hidden or 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 python | |
from __future__ import print_function | |
import yaml | |
import subprocess | |
import argparse | |
__author__ = "Jorge Niedbalski <[email protected]>" | |
def run(cmd): | |
return subprocess.check_output(cmd.split(" "), stderr=subprocess.PIPE) | |
def load_yaml(cmd): | |
return yaml.load(run(cmd)) | |
class Service: | |
def __init__(self, name, environment, | |
skip_relations=["cluster", ]): | |
self.name = name | |
self.skip_relations = skip_relations | |
self.status = environment.get("services")[self.name] | |
def to_dict(self): | |
r = { | |
'num_units': self.units, | |
'charm': self.charm, | |
} | |
if self.constraints: | |
r.update({ | |
'constraints': self.constraints, | |
}) | |
if len(self.options): | |
r.update({ | |
'options': self.options, | |
}) | |
return r | |
@property | |
def constraints(self): | |
try: | |
return run("juju get-constraints %s" % self.name).strip("\n") | |
except: | |
return None | |
@property | |
def options(self): | |
config = load_yaml("juju get %s" % self.name) | |
options = {} | |
for k, v in config.get('settings').items(): | |
if 'value' in v: | |
options[k] = v['value'] | |
return options | |
@property | |
def relations(self): | |
if 'relations' in self.status: | |
for name, items in self.status.get('relations').items(): | |
if name not in self.skip_relations: | |
for item in items: | |
if self.name != item: | |
yield sorted([self.name, item]) | |
@property | |
def units(self): | |
if 'units' in self.status: | |
return len(self.status.get("units")) | |
return 1 | |
@property | |
def charm(self): | |
return self.status.get('charm') | |
@property | |
def placement(self): | |
pass | |
class Environment: | |
def __init__(self, options): | |
self.options = options | |
self.environment = load_yaml("juju status -e %s --format=yaml" % | |
self.options.environment) | |
@property | |
def services(self): | |
services = [] | |
for service in self.environment.get('services').keys(): | |
services.append(Service(service, self.environment)) | |
return services | |
def deployerize(self): | |
output = { | |
self.options.environment: { | |
'services': {}, | |
'relations': [], | |
} | |
} | |
relations = [] | |
for service in self.services: | |
for relation in service.relations: | |
if relation not in relations: | |
relations.append(relation) | |
output[self.options.environment]['services'][ | |
service.name] = service.to_dict() | |
output[self.options.environment]['relations'] = relations | |
with open(self.options.output, 'w+') as f: | |
yaml.dump(output, f, default_flow_style=False) | |
def parse_options(): | |
parser = argparse.ArgumentParser( | |
description='Convert your current juju environment status\ | |
into a YAML suitable for being used on juju-deployer') | |
parser.add_argument("--environment", | |
required=True, | |
help='Juju environment to convert', | |
type=str, | |
metavar='environment') | |
parser.add_argument("--output", | |
default="deployer.yaml", | |
help='File to store the deployer yaml', | |
type=str, | |
metavar='output') | |
args = parser.parse_args() | |
return args | |
def main(): | |
options = parse_options() | |
d = Environment(options) | |
d.deployerize() | |
if __name__ == "__main__": | |
main() |
This file contains hidden or 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
$ ./deployerizer --environment local --output local-bundle.yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment