def run_command(self, command_c, slice_name=None, rspec_path=None): """ runs given command for slice name and returns output. command_c should be an array """ # Required args command = [self.java_path, '-jar', self.jfed_cli_path] command += command_c # Optional args # All optional args need to be initialized as array if rspec_path: command += ['--rspec', rspec_path] if slice_name: slice_name = 'urn:publicid:IDN+wall2.ilabt.iminds.be:' + self.project_name + '+slice+' + slice_name command += ['-s', slice_name] if self.call_log: command += ['--call-log', '/var/log/jfed_call_log'] if self.key_path: command += ['-p', self.key_path] if self.password: command += ['-P', self.password] elif self.properties: command += ['--context-file', self.properties] else: fail("either keyfile or context file must be specified") if self.s4cert: command += ['--speaks-for', self.s4cert] debug("command is: `{}`".format(' '.join(command))) iserror = False try: output = subprocess.check_output(command, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as ex: output = ex.output iserror = True print "DEBUG: output = {}".format(output) return parse_output(output, iserror)
def destroy(self): if self.locked: fail('Cannot destroy locked model') else: print('Destroying jfed slice and slivers...') jfed = self.init_jfed() jfed.delete()
def get_provider(config=global_conf): provider = config.get('provider', 'jfed') if provider == "ssh": return ssh_provider.SSHProvider(global_conf) elif provider == "jfed": return jfed_provider.JfedProvider(global_conf) else: fail("No provider of type {} found".format(provider))
def reload(self): if self.locked: fail('Cannot reload locked model') else: print('Reloading jfed slivers...') jfed = self.init_jfed() jfed.reload() print('renewed reloaded succesfully')
def delete(self): """ Deletes jfed slice """ if self.locked: fail("Environment is locked, cannot delete it.") else: url = self.slice_url response = requests.delete(url, headers=self.headers) if response.status_code == 200: return raise Exception( "Call to {2} failed with code {0} and message:\n{1}".format( response.status_code, response.text, url))
def get_provider(config=GLOBAL_CONF): """ Gets the provider. The idea is that every environment can have a different provider, but we still want a default provider. This means that provider will be saved in both env_conf(different for each environment) and global_conf (the default provider for new environments)""" provider = config.get('provider', 'rest2jfed') if provider == "ssh": return ssh_provider.SSHProvider(GLOBAL_CONF) elif provider == "rest2jfed": return jfed_provider.JfedProvider(GLOBAL_CONF) elif provider == "juju-powered": return juju_powered_provider.JujuPoweredProvider(GLOBAL_CONF) else: fail("No provider of type {} found".format(provider))
def create(name, juju_config, machines, init_bundle): """Creates Juju environment and deploy the init bundle.""" if JujuEnvironment.env_exists(name): fail( "Juju environment already exists. Remove it first with 'tengu destroy-model {}'" .format(name)) JujuEnvironment._create_env(name, juju_config) # Wait 5 seconds before adding machines because python # is too fast for juju sleep(20) environment = JujuEnvironment(name) environment.add_machines(machines) environment.deploy_init_bundle(init_bundle) return environment
def destroy_juju_environment(name): """ Destroy Juju environment and destroy Juju environment config files """ env_conf = init_environment_config(name) if env_conf['locked']: fail('Cannot destroy locked environment') else: okwhite("removing juju environment from juju config files") with open(expanduser("~/.juju/environments.yaml"), 'r') as config_file: config = yaml.load(config_file) if config['environments'] is not None: config['environments'].pop(name, None) with open(expanduser("~/.juju/environments.yaml"), 'w') as config_file: config_file.write(yaml.dump(config, default_flow_style=False)) okwhite("removing juju environment from juju environment folder") if os.path.isfile(expanduser("~/.juju/environments/%s.jenv" % name)): os.remove(expanduser("~/.juju/environments/%s.jenv" % name))
def create_juju(env_conf, provider_env): if JujuEnvironment.env_exists(env_conf['env-name']): fail( "Juju environment already exists. Remove it first with 'tengu destroy {}'" .format(env_conf['env-name'])) try: machines = provider_env.machines except Exception as ex: # pylint: disable=W0703 fail("Could not get machines from manifest", ex) # Create Juju environment env_conf['juju-env-conf']['bootstrap-host'] = machines.pop(0) env_conf.save() wait_for_init(env_conf) return JujuEnvironment.create(env_conf['env-name'], env_conf['juju-env-conf']['bootstrap-host'], env_conf['juju-env-conf'], machines)
def create_juju(env_conf, provider_env): if JujuEnvironment.env_exists(env_conf['env-name']): fail("Juju environment already exists. Remove it first with 'tengu destroy {}'".format(env_conf['env-name'])) try: machines = provider_env.machines except Exception as ex: # pylint: disable=W0703 fail("Could not get machines from manifest", ex) # Create Juju environment env_conf['juju-env-conf']['bootstrap-host'] = machines.pop(0) env_conf.save() wait_for_init(env_conf) return JujuEnvironment.create( env_conf['env-name'], env_conf['juju-env-conf']['bootstrap-host'], env_conf['juju-env-conf'], machines )
def __init__(self, config_path, default_path=None): """ Open config from config_path. If config does not exist, create new one from default_path if config_path == None, then saving config will not do anything.""" self.config_path = config_path self.default_path = default_path if not os.path.isdir(tengu_dir()): os.makedirs(tengu_dir()) if config_path is not None and os.path.isfile(self.config_path): src = self.config_path else: src = default_path try: with open(src, "r") as src_file: super(Config, self).__init__(yaml.load(src_file)) except Exception: fail("failed opening config. Is there a config file at %s ?" % src) raise self.save()
def c_reset_model(modelname): """ Destroys the model's services and containers except for lxc-networking, network-agent and openvpn. if whitelist is provided, only services in whitelist will be destroyed. NAME: name of model WHITELIST: names of charms to destroy """ if click.confirm( 'Warning! This will remove all services and containers of the model "{}". Are you sure you want to continue?' .format(modelname)): env_conf = init_environment_config(modelname) if env_conf['locked']: fail('Cannot reset locked environment') else: jujuenv = JujuEnvironment(modelname) for servicename in jujuenv.status['services'].keys(): if servicename not in [ 'lxc-networking', 'network-agent', 'openvpn' ]: # We should get these services from the init bundle... Service(servicename, jujuenv).destroy(force=True)
def destroy(self): # pylint:disable=W0613,R0201 if self.locked: fail('Cannot destroy locked model') else: print('Destroying Juju powered environment')
def reload(self): # pylint:disable=W0613,R0201 if self.locked: fail('Cannot reload locked model') else: self.destroy()