def update_monitoring(request): """ Enable/disable monitoring for this machine using the hosted mist.io service. """ core_uri = request.registry.settings['core_uri'] try: email = request.json_body['email'] password = request.json_body['pass'] timestamp = request.json_body['timestamp'] hash = request.json_body['hash'] except: email = request.registry.settings.get('email','') password = request.registry.settings.get('password','') timestamp = datetime.utcnow().strftime("%s") hash = sha256("%s:%s:%s" % (email, timestamp, password)).hexdigest() action = request.json_body['action'] or 'enable' payload = {'email': email, 'timestamp': timestamp, 'hash': hash, 'action': action, } #TODO: make ssl verification configurable globally, set to true by default ret = requests.post(core_uri+request.path, params=payload, verify=False) if ret.status_code != 200: return Response('Service unavailable', 503) request.registry.settings['email'] = email request.registry.settings['password'] = password save_settings(request) return ret.json()
def update_monitoring(request): """ Enable/disable monitoring for this machine using the hosted mist.io service. """ core_uri = request.registry.settings['core_uri'] try: email = request.json_body['email'] password = request.json_body['pass'] timestamp = request.json_body['timestamp'] hash = request.json_body['hash'] except: email = request.registry.settings.get('email', '') password = request.registry.settings.get('password', '') timestamp = datetime.utcnow().strftime("%s") hash = sha256("%s:%s:%s" % (email, timestamp, password)).hexdigest() action = request.json_body['action'] or 'enable' payload = { 'email': email, 'timestamp': timestamp, 'hash': hash, 'action': action, } #TODO: make ssl verification configurable globally, set to true by default ret = requests.post(core_uri + request.path, params=payload, verify=False) if ret.status_code != 200: return Response('Service unavailable', 503) request.registry.settings['email'] = email request.registry.settings['password'] = password save_settings(request) return ret.json()
def add_backend(request, renderer='json'): try: backends = request.environ['beaker.session']['backends'] except: backends = request.registry.settings['backends'] params = request.json_body provider = params.get('provider', '0')['provider'] apikey = params.get('apikey', '') apisecret = params.get('apisecret', '') apiurl = params.get('apiurl', '') tenant_name = params.get('tenant_name', '') if apisecret == 'getsecretfromdb': for backend_id in backends: backend = backends[backend_id] if backend.get('apikey', None) == apikey: apisecret = backend.get('apisecret', None) region = '' if not provider.__class__ is int and ':' in provider: region = provider.split(':')[1] provider = provider.split(':')[0] if not provider or not apikey or not apisecret: return Response('Invalid backend data', 400) backend_id = generate_backend_id(provider, region, apikey) if backend_id in backends: return Response('Backend exists', 409) backend = {'title': params.get('provider', '0')['title'], 'provider': provider, 'apikey': apikey, 'apisecret': apisecret, 'apiurl': apiurl, 'tenant_name': tenant_name, 'region': region, 'poll_interval': request.registry.settings['default_poll_interval'], 'enabled': 1, } request.registry.settings['backends'][backend_id] = backend save_settings(request) ret = {'id' : backend_id, 'apikey' : backend['apikey'], 'apiurl' : backend['apiurl'], 'tenant_name' : backend['tenant_name'], 'title' : backend['title'], 'provider' : backend['provider'], 'poll_interval': backend['poll_interval'], 'region' : backend['region'], 'status' : 'off', 'enabled' : 1, } return ret
def delete_backend(request, renderer="json"): """Deletes a backend. .. note:: It assumes the user may re-add it later so it does not remove any key associations. """ request.registry.settings["backends"].pop(request.matchdict["backend"]) save_settings(request) return Response("OK", 200)
def update_monitoring(request): """Enable/disable monitoring for this machine using the hosted mist.io service. """ core_uri = request.registry.settings['core_uri'] try: email = request.json_body['email'] password = request.json_body['pass'] timestamp = request.json_body['timestamp'] hash = request.json_body['hash'] except: email = request.registry.settings.get('email','') password = request.registry.settings.get('password','') timestamp = datetime.utcnow().strftime("%s") hash = sha256("%s:%s:%s" % (email, timestamp, password)).hexdigest() name = request.json_body.get('name','') public_ips = request.json_body.get('public_ips', []) dns_name = request.json_body.get('dns_name', '') action = request.json_body['action'] or 'enable' payload = {'email': email, 'timestamp': timestamp, 'hash': hash, 'action': action, 'name': name, 'public_ips': public_ips, 'dns_name': dns_name, } if action == 'enable': backend = request.registry.settings['backends'][request.matchdict['backend']] payload['backend_title'] = backend['title'] payload['backend_provider'] = backend['provider'] payload['backend_region'] = backend['region'] payload['backend_apikey'] = backend['apikey'] payload['backend_apisecret'] = backend['apisecret'] #TODO: make ssl verification configurable globally, set to true by default ret = requests.post(core_uri+request.path, params=payload, verify=False) if ret.status_code == 402: return Response(ret.text, 402) elif ret.status_code != 200: return Response('Service unavailable', 503) request.registry.settings['email'] = email request.registry.settings['password'] = password request.registry.settings['auth'] = 1 save_settings(request) return ret.json()
def delete_key(request): """Deletes a keypair. When a key gets deleted it takes its asociations with it so just need to remove form the server too. If the default key gets deleted, it sets the next one as default, provided that at least another key exists. It returns the list of all keys after the deletion, excluding the private keys (check also list_keys). """ params = request.json_body try: key_id = params["key_id"] except KeyError: return Response("Key name not provided", 400) keypairs = request.registry.settings["keypairs"] key = keypairs.pop(key_id) try: # TODO: alert user for key undeployment # for machine in key.get('machines', []): # undeploy_key(request, machine[0], machine[1], key) ret_code = 200 except: ret_code = 206 if key.get("default", None): try: new_default_key = keypairs.keys()[0] keypairs[new_default_key]["default"] = True except IndexError: pass save_settings(request) ret = [ { "name": key, "machines": keypairs[key].get("machines", False), "pub": keypairs[key]["public"], "default_key": keypairs[key].get("default", False), } for key in keypairs.keys() ] return ret
def set_default_key(request): params = request.json_body id = params.get('name', '') keypairs = request.registry.settings['keypairs'] for key in keypairs: if keypairs[key].get('default', False): keypairs[key]['default'] = False keypairs[id]['default'] = True save_settings(request) return {}
def delete_key(request): params = request.json_body id = params.get('name', '') key = request.registry.settings['keypairs'].pop(id) if key.get('default', None): #if we delete the default key, make the next one as default, provided #that it exists try: first_key_id = request.registry.settings['keypairs'].keys()[0] request.registry.settings['keypairs'][first_key_id]['default'] = True except KeyError: pass save_settings(request) return {}
def delete_key(request): params = request.json_body id = params.get('name', '') key = request.registry.settings['keypairs'].pop(id) if key.get('default', None): #if we delete the default key, make the next one as default, provided #that it exists try: first_key_id = request.registry.settings['keypairs'].keys()[0] request.registry.settings['keypairs'][first_key_id][ 'default'] = True except KeyError: pass save_settings(request) return {}
def add_key(request): params = request.json_body id = params.get('name', '') key = {'public' : params.get('pub', ''), 'private' : params.get('priv', '')} if not len(request.registry.settings['keypairs']): key['default'] = True request.registry.settings['keypairs'][id] = key save_settings(request) ret = {'name': id, 'pub': key['public'], 'priv': key['private'], 'default_key': key.get('default', False), 'machines': []} return ret
def update_monitoring(request): """Enable/disable monitoring for this machine using the hosted mist.io service. """ core_uri = request.registry.settings["core_uri"] try: email = request.json_body["email"] password = request.json_body["pass"] timestamp = request.json_body["timestamp"] hash = request.json_body["hash"] except: email = request.registry.settings.get("email", "") password = request.registry.settings.get("password", "") timestamp = datetime.utcnow().strftime("%s") hash = sha256("%s:%s:%s" % (email, timestamp, password)).hexdigest() action = request.json_body["action"] or "enable" payload = {"email": email, "timestamp": timestamp, "hash": hash, "action": action} if action == "enable": backend = request.registry.settings["backends"][request.matchdict["backend"]] payload["backend_title"] = backend["title"] payload["backend_provider"] = backend["provider"] payload["backend_region"] = backend["region"] payload["backend_apikey"] = backend["apikey"] payload["backend_apisecret"] = backend["apisecret"] # TODO: make ssl verification configurable globally, set to true by default ret = requests.post(core_uri + request.path, params=payload, verify=False) if ret.status_code == 402: return Response(ret.text, 402) elif ret.status_code != 200: return Response("Service unavailable", 503) request.registry.settings["email"] = email request.registry.settings["password"] = password request.registry.settings["auth"] = 1 save_settings(request) return ret.json()
def add_key(request): params = request.json_body id = params.get('name', '') key = {'public': params.get('pub', ''), 'private': params.get('priv', '')} if not len(request.registry.settings['keypairs']): key['default'] = True request.registry.settings['keypairs'][id] = key save_settings(request) ret = { 'name': id, 'pub': key['public'], 'priv': key['private'], 'default_key': key.get('default', False), 'machines': [] } return ret
def add_key(request): """Creates a new keypair.""" params = request.json_body key_id = params.get("name", "") key = {"public": params.get("pub", ""), "private": params.get("priv", "")} if not len(request.registry.settings["keypairs"]): key["default"] = True request.registry.settings["keypairs"][key_id] = key save_settings(request) ret = { "name": key_id, "pub": key["public"], "priv": key["private"], "default_key": key.get("default", False), "machines": [], } return ret
def add_backend(request, renderer="json"): params = request.json_body provider = params.get("provider", "0")["provider"] apikey = params.get("apikey", "") apisecret = params.get("apisecret", "") region = "" if not provider.__class__ is int and ":" in provider: region = provider.split(":")[1] provider = provider.split(":")[0] if not provider or not apikey or not apisecret: return Response("Invalid backend data", 400) backend_id = generate_backend_id(provider, region, apikey) backend = { "title": params.get("provider", "0")["title"], "provider": provider, "apikey": apikey, "apisecret": apisecret, "region": region, "poll_interval": request.registry.settings["default_poll_interval"], "enabled": 1, } request.registry.settings["backends"][backend_id] = backend save_settings(request) ret = { "id": backend_id, "apikey": backend["apikey"], "title": backend["title"], "provider": backend["provider"], "poll_interval": backend["poll_interval"], "region": backend["region"], "status": "off", "enabled": 1, } return ret
def add_backend(request, renderer='json'): params = request.json_body provider = params.get('provider', '0')['provider'] apikey = params.get('apikey', '') apisecret = params.get('apisecret', '') region = '' if not provider.__class__ is int and ':' in provider: region = provider.split(':')[1] provider = provider.split(':')[0] if not provider or not apikey or not apisecret: return Response('Invalid backend data', 400) backend_id = sha256('%s%s%s' % (provider, region, apikey)).hexdigest() backend = { 'title': params.get('provider', '0')['title'], 'provider': provider, 'apikey': apikey, 'apisecret': apisecret, 'region': region, 'poll_interval': request.registry.settings['default_poll_interval'], 'enabled': 1, } request.registry.settings['backends'][backend_id] = backend save_settings(request) ret = { 'id': backend_id, 'apikey': backend['apikey'], 'title': backend['title'], 'provider': backend['provider'], 'poll_interval': backend['poll_interval'], 'region': backend['region'], 'status': 'off', 'enabled': 1, } return ret
def add_backend(request, renderer='json'): params = request.json_body provider = params.get('provider', '0')['provider'] apikey = params.get('apikey', '') apisecret = params.get('apisecret', '') region = '' if not provider.__class__ is int and ':' in provider: region = provider.split(':')[1] provider = provider.split(':')[0] if not provider or not apikey or not apisecret: return Response('Invalid backend data', 400) backend_id = sha256('%s%s%s' % (provider, region, apikey)).hexdigest() backend = {'title': params.get('provider', '0')['title'], 'provider': provider, 'apikey': apikey, 'apisecret': apisecret, 'region': region, 'poll_interval': request.registry.settings['default_poll_interval'], 'enabled': 1, } request.registry.settings['backends'][backend_id] = backend save_settings(request) ret = {'id' : backend_id, 'apikey' : backend['apikey'], 'title' : backend['title'], 'provider' : backend['provider'], 'poll_interval': backend['poll_interval'], 'region' : backend['region'], 'status' : 'off', 'enabled' : 1, } return ret
def add_key(request): """Creates a new keypair.""" params = request.json_body key_id = params.get('name', '') if key_id in request.registry.settings['keypairs']: return Response('Key "%s" already exists' % key_id, 409) key = {'public' : params.get('pub', ''), 'private' : params.get('priv', '')} if not len(request.registry.settings['keypairs']): key['default'] = True request.registry.settings['keypairs'][key_id] = key save_settings(request) ret = {'name': key_id, 'pub': key['public'], 'priv': key['private'], 'default_key': key.get('default', False), 'machines': []} return ret
def delete_backend(request, renderer='json'): request.registry.settings['backends'].pop(request.matchdict['backend']) save_settings(request) return Response('OK', 200)