def upgrade(modelname, name): """ Shows the status of this hauchiwa instance """ env = JujuEnvironment(modelname) service = Service(name, env) service.upgrade() return create_response( 200, {"msg": 'Started upgrade of service {}'.format(name)})
def api_hauchiwa_info(instance_id): """ Shows the info of the specified Hauchiwa instance """ if len(instance_id) > 10: return Response("instance_id cannot be longer than 10 characters", status=400, mimetype='text/plain') # get values from request #TODO: Authenticate this action #s4_cert = str(request.headers.get('emulab-s4-cert')) juju = JujuEnvironment(None) info = juju.status['services'].get('h-{}'.format(instance_id)) if info: info = { 'status': info['service-status'], 'public-address': info['units'].values()[0].get('public-address') } resp = Response( json.dumps(info), status=200, mimetype='application/json', ) else: resp = Response( 'Cannot find instance {}'.format(instance_id), status=404, mimetype='text/plain', ) resp.headers['location'] = '/hauchiwa/{}'.format(instance_id) resp.headers['Access-Control-Allow-Origin'] = 'tengu.intec.ugent.be' return resp
def c_export_juju_env(name, filename): """export juju environment to given yaml file """ jujuenv = JujuEnvironment(name) environment = {} environment['environment'] = jujuenv.return_environment() with open(filename, 'w+') as o_file: o_file.write(yaml.dump(environment, default_flow_style=False))
def api_model_update(modelname): """ Deploys new bundle """ bundle = request.data # Write bundle bundle_path = tempfile.mkdtemp() + '/bundle.yaml' with open(bundle_path, 'w+') as bundle_file: bundle_file.write(bundle) # Create environment from bundle if not exist, else deploy bundle if JujuEnvironment.env_exists(modelname): env = JujuEnvironment(modelname) output = env.deploy_bundle(bundle_path, '--skip-unit-wait') else: try: output = subprocess.check_output( ['tengu', 'create-model', '--bundle', bundle_path, modelname], stderr=subprocess.STDOUT) except CalledProcessError as process_error: return create_response( 500, { "msg": "Failed to deploy bundle to environment {}. Output: {}". format(modelname, process_error.output) }) return create_response( 200, { "msg": "Sucessfully deployed bundle to environment {}. Output: {}".format( modelname, output) })
def c_destroy_service(modelname, services): """Destroys given services MODELNAME: name of the model SERVICES: services to destroy""" jujuenv = JujuEnvironment(modelname) for servicename in jujuenv.status['services'].keys(): if servicename in services: Service(servicename, jujuenv).destroy(force=True)
def c_show_config(model, servicename): """Get the config of a service in a format that can be used to set the config of a service. NAME: name of model SERVICENAME: name of the service""" env = JujuEnvironment(model) service = Service(servicename, env) print( yaml.dump({str(servicename): service.get_config()}, default_flow_style=False))
def c_add_machines(name): """Add machines of jfed experiment to Juju environment NAME: name of Tengu """ env_conf = init_environment_config(name) env = PROVIDER.get(env_conf) machines = env.machines machines.pop(0) jujuenv = JujuEnvironment(name) jujuenv.add_machines(machines)
def c_add_machines(model): """Add machines of jfed experiment to Juju environment NAME: name of model """ env_conf = init_environment_config(model) env = get_provider(env_conf).get(env_conf) machines = env.machines machines.pop(0) jujuenv = JujuEnvironment(model) jujuenv.add_machines(machines)
def c_expose_service(model, servicename): """ Expose the service so it is publicly available from the internet. NAME: name of model SERVICENAME: name of the service to expose""" env_conf = init_environment_config(model) provider_env = get_provider(env_conf).get(env_conf) env_conf = init_environment_config(model) env = JujuEnvironment(model) service = Service(servicename, env) provider_env.expose(service)
def c_destroy_model(name): """Destroys model with given name NAME: name of model """ if click.confirm( 'Warning! This will destroy both the Juju environment and the jFed experiment of the model "{}". Are you sure you want to continue?' .format(name)): jujuenv = JujuEnvironment(name) env_conf = init_environment_config(name) jujuenv.destroy(env_conf['locked']) provider_env = get_provider(env_conf).get(env_conf) provider_env.destroy()
def api_service_config_change(modelname, servicename): """ Change config of specified hauchiwa """ config = request.json juju = JujuEnvironment(modelname) service = Service(servicename, juju) try: service.set_config(config) except JujuException as exception: print(exception.message) return create_response(500, { "msg": 'config change failed', "output": exception.message }) return create_response(200, {"msg": 'Config change requested'})
def c_export_model(model, path): """Export the config of the model with given NAME""" jujuenv = JujuEnvironment(model) config = jujuenv.return_environment() files = { "tengu-env-conf": env_conf_path(model), } env_conf = init_environment_config(model) env = get_provider(env_conf).get(env_conf) files.update(env.files) for f_name, f_path in files.iteritems(): with open(f_path, 'r') as f_file: config[f_name] = base64.b64encode(f_file.read()) config['emulab-project-name'] = GLOBAL_CONF['project-name'] export = {str(model): config} with open(path, 'w+') as outfile: outfile.write(yaml.dump(export))
def c_export(name, path): """Export Tengu with given NAME""" jujuenv = JujuEnvironment(name) config = jujuenv.return_environment() files = { "tengu-env-conf": env_conf_path(name), } env_conf = init_environment_config(name) env = PROVIDER.get(env_conf) files.update(env.files) for f_name, f_path in files.iteritems(): with open(f_path, 'r') as f_file: config[f_name] = base64.b64encode(f_file.read()) config['emulab-project-name'] = global_conf['project-name'] export = {str(name): config} with open(path, 'w+') as outfile: outfile.write(yaml.dump(export))
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 api_hauchiwa_create(instance_id): """ Creates new tengu hauchiwa """ if len(instance_id) > 10: return Response("instance_id cannot be longer than 10 characters", status=400, mimetype='text/plain') # get values from request s4_cert = str(request.headers.get('emulab-s4-cert')) body = request.get_json() ssh_keys = body.get('ssh-keys', "") bundle = body.get('bundle', "") # Create config file hauchiwa_name = 'h-{}'.format(instance_id) hauchiwa_cfg = { str(hauchiwa_name): { 'emulab-s4-cert': s4_cert, 'emulab-project-name': "tengu", 'charm-repo-source': "https://github.com/galgalesh/tengu-charms.git", 'ssh-keys': str(','.join([DEFAULT_PUBKEYS, ssh_keys])), 'bundle': bundle, } } hauchiwa_cfg_path = tempfile.mkdtemp() + 'hauchiwa-cfg.yaml' with open(hauchiwa_cfg_path, 'w+') as hauchiwa_cfg_file: hauchiwa_cfg_file.write( yaml.dump(hauchiwa_cfg, default_flow_style=False)) # Deploy Hauchiwa juju = JujuEnvironment(None) juju.deploy('local:hauchiwa', hauchiwa_name, config_path=hauchiwa_cfg_path, to='lxc:1') juju.add_relation(hauchiwa_name, 'rest2jfed') resp = Response( "Created hauchiwa instance {}".format(instance_id), status=201, mimetype='text/plain', ) resp.headers['location'] = '/hauchiwa/{}'.format(instance_id) resp.headers['Access-Control-Allow-Origin'] = 'tengu.intec.ugent.be' return resp
def api_info(): """ Shows the status of this hauchiwa instance """ # get values from request #TODO: Authenticate this action #s4_cert = str(request.headers.get('emulab-s4-cert')) env = JujuEnvironment(None) info = env.status if info: resp = Response( json.dumps(info), status=200, mimetype='application/json', ) else: resp = Response( "Environment {} doesn't exist or isn't bootstrapped".format( env.name), status=404, mimetype='text/plain', ) return resp
def api_hauchiwa(): """ Shows the status of the users Hauchiwa instances """ # get values from request s4_cert = str(request.headers.get('emulab-s4-cert')) juju = JujuEnvironment(None) services = juju.get_services('h-', 'emulab-s4-cert', s4_cert) lst = {} for service in services.keys(): lst[service] = { 'status': services[service]['service-status'], 'public-address': services[service]['units'].values()[0].get('public-address') } resp = Response( json.dumps(lst), status=200, mimetype='application/json', ) resp.headers['Access-Control-Allow-Origin'] = 'tengu.intec.ugent.be' return resp
def c_deploy(bundle, name): """Create a Tengu with given name. Skips slice creation if it already exists. NAME: name of Tengu """ downloadbigfiles(os.environ['JUJU_REPOSITORY']) juju_env = JujuEnvironment(name) juju_env.deploy_bundle(bundle)
def api_service_config(modelname, servicename): """ Shows the info of the specified Hauchiwa instance """ juju = JujuEnvironment(modelname) service = Service(servicename, juju) info = service.config return create_response(200, info)
def api_model_info(modelname): """ Shows the status of this hauchiwa instance """ env = JujuEnvironment(modelname) info = env.status if info: return create_response(200, info)