def get_bundle(bundle, to_file=False): """ Attempts to grab the bundle.yaml Arguments: bundle: name of bundle or absolute path to local bundle to_file: store to a temporary file Returns: Dictionary of bundle's yaml unless to_file is True, then returns the path to the downloaded bundle """ if path.isfile(bundle): if to_file: with NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as tempf: shutil.copyfile(bundle, tempf.name) return tempf.name else: with open(bundle) as f: return yaml.safe_load(f.read()) bundle = path.join(cs, bundle, 'archive/bundle.yaml') req = requests.get(bundle) if not req.ok: raise Exception("Problem getting bundle: {}".format(req)) if to_file: with NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as tempf: spew(tempf.name, req.text) return tempf.name else: return yaml.safe_load(req.text)
async def do_deploy(msg_cb): if app.metadata.needs_juju: await events.ModelConnected.wait() for step in app.steps: await step.before_deploy(msg_cb=msg_cb) events.PreDeployComplete.set() msg = 'Deploying Applications.' app.log.info(msg) msg_cb(msg) if app.metadata.needs_juju: datetimestr = datetime.now().strftime("%Y%m%d.%H%m") fn = os.path.join( app.env['CONJURE_UP_CACHEDIR'], '{}-deployed-{}.yaml'.format(app.env['CONJURE_UP_SPELL'], datetimestr)) utils.spew(fn, app.current_bundle.to_yaml()) for attempt in range(3): try: await app.juju.client.deploy(fn) break # success except websockets.ConnectionClosed: if attempt == 2: raise await asyncio.sleep(1) events.DeploymentComplete.set()
def set_default_profile(self): """ Sets the default profile with the correct parent network bridges """ profile = textwrap.dedent(""" config: boot.autostart: "true" description: Default LXD profile devices: eth0: name: eth0 nictype: bridged parent: conjureup1 type: nic eth1: name: eth1 nictype: bridged parent: conjureup0 type: nic root: path: / pool: default type: disk name: default """) with NamedTemporaryFile(mode='w', encoding='utf-8', delete=False) as tempf: utils.spew(tempf.name, profile) out = utils.run_script( 'cat {} |conjure-up.lxc profile edit default'.format( tempf.name)) if out.returncode != 0: raise Exception( "Problem setting default profile: {}".format(out))
async def do_deploy(msg_cb): for step in app.steps: await step.before_deploy(msg_cb=msg_cb) events.PreDeployComplete.set() msg = 'Deploying Applications.' app.log.info(msg) msg_cb(msg) datetimestr = datetime.now().strftime("%Y%m%d.%H%m") fn = os.path.join( app.env['CONJURE_UP_CACHEDIR'], '{}-deployed-{}.yaml'.format(app.env['CONJURE_UP_SPELL'], datetimestr)) utils.spew(fn, app.current_bundle.to_yaml()) try: for _app in app.current_bundle.applications: cmd = [ 'sudo', 'snap', 'install', _app.snap, '--channel', _app.channel ] if hasattr(_app, 'confinement') and _app.confinement: cmd += ['--{}'.format(_app.confinement)] ret, out, err = await utils.arun(cmd, cb_stdout=msg_cb) if ret > 0: raise errors.DeploymentFailure(err) except CalledProcessError as e: app.log.error("Could not snap install application: {}".format(e)) raise events.DeploymentComplete.set()
def save(template, opts): """ Saves template to temporary file Arguments: template: loaded jinja template opts: dictionary of items to be passed into template """ modified = template.render(**opts) with NamedTemporaryFile(mode='w', encoding='utf-8', delete=False) as tempf: spew(tempf.name, modified) return tempf.name
def add_cloud(name, config): """ Adds a cloud Arguments: name: name of cloud to add config: cloud configuration """ _config = {'clouds': {name: config}} app.log.debug(_config) with NamedTemporaryFile(mode='w', encoding='utf-8', delete=False) as tempf: output = yaml.safe_dump(_config, default_flow_style=False) spew(tempf.name, output) sh = run('juju add-cloud {} {}'.format(name, tempf.name), shell=True, stdout=PIPE, stderr=PIPE) if sh.returncode > 0: raise Exception("Unable to add cloud: {}".format( sh.stderr.decode('utf8')))
async def do_deploy(msg_cb): await events.ModelConnected.wait() for step in app.steps: await step.before_deploy(msg_cb=msg_cb) events.PreDeployComplete.set() msg = 'Deploying Applications.' app.log.info(msg) msg_cb(msg) datetimestr = datetime.now().strftime("%Y%m%d.%H%m") fn = os.path.join(app.env['CONJURE_UP_CACHEDIR'], '{}-deployed-{}.yaml'.format(app.env['CONJURE_UP_SPELL'], datetimestr)) utils.spew(fn, app.current_bundle.to_yaml()) await app.juju.client.deploy(fn) events.DeploymentComplete.set()
def finish(self, needs_lxd_setup=False, lxdnetwork=None, back=False): """ Processes the new LXD setup and loads the controller to finish bootstrapping the model. Arguments: back: if true loads previous controller needs_lxd_setup: if true prompt user to run lxd init """ if back: return controllers.use('clouds').render() if needs_lxd_setup: EventLoop.remove_alarms() EventLoop.exit(1) if lxdnetwork is None: return app.ui.show_exception_message( Exception("Unable to configure LXD network bridge.")) formatted_network = self.__format_input(lxdnetwork) app.log.debug("LXD Config {}".format(formatted_network)) out = self.__format_conf(formatted_network) with NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as tempf: app.log.debug("Saving LXD config to {}".format(tempf.name)) utils.spew(tempf.name, out) sh = run('sudo mv {} /etc/default/lxd-bridge'.format( tempf.name), shell=True) if sh.returncode > 0: return app.ui.show_exception_message( Exception("Problem saving config: {}".format( sh.stderr.decode('utf8')))) app.log.debug("Restarting lxd-bridge") run("sudo systemctl restart lxd-bridge.service", shell=True) utils.pollinate(app.session_id, 'L002') controllers.use('newcloud').render( cloud='localhost', bootstrap=True)
def finish(self, needs_lxd_setup=False, lxdnetwork=None, back=False): """ Processes the new LXD setup and loads the controller to finish bootstrapping the model. Arguments: back: if true loads previous controller needs_lxd_setup: if true prompt user to run lxd init """ if back: return controllers.use('clouds').render() if needs_lxd_setup: EventLoop.remove_alarms() EventLoop.exit(1) if lxdnetwork is None: return app.ui.show_exception_message( Exception("Unable to configure LXD network bridge.")) formatted_network = self.__format_input(lxdnetwork) app.log.debug("LXD Config {}".format(formatted_network)) out = self.__format_conf(formatted_network) with NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as tempf: app.log.debug("Saving LXD config to {}".format(tempf.name)) utils.spew(tempf.name, out) sh = utils.run('sudo mv {} /etc/default/lxd-bridge'.format( tempf.name), shell=True) if sh.returncode > 0: return app.ui.show_exception_message( Exception("Problem saving config: {}".format( sh.stderr.decode('utf8')))) app.log.debug("Restarting lxd-bridge") utils.run("sudo systemctl restart lxd-bridge.service", shell=True) utils.pollinate(app.session_id, 'L002') controllers.use('newcloud').render( cloud='localhost', bootstrap=True)