def verify_connectivity(self): # Dummy call to verify if connection and authentication token work response = requests.get(self.base_url + "/mesos_dns/v1/hosts/master.mesos", auth=get_auth(), verify=False) return response.ok
def get_nodes(self): response = requests.get(self.base_url + "/system/health/v1/nodes", auth=get_auth(), verify=False) if not response.ok: raise Exception("Unknown error occured: %s" % response.text) return response.json()["nodes"]
def get_account(self, name): response = requests.get(self.base_url + "/users/" + name, auth=get_auth(), verify=False) if response.status_code != 200: return None return response.json()
def remove_user_from_group(self, user_name, group): response = requests.delete(self.base_url + "/groups/%s/users/%s" % (group, user_name), auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when adding user to group")
def get_groups_for_user(self, name): response = requests.get(self.base_url + "/users/%s/groups" % name, auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when querying groups for user") return [g["group"]["gid"] for g in response.json()["array"]]
def get_deployments(self): response = requests.get(self.marathon_url + "/deployments", auth=get_auth(), verify=False) if not response.ok: print(response.text, flush=True) raise Exception("Failed to get deployments") return response.json()
def get_rids(self): response = requests.get(self.base_url + "/acls", auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when listing permission") return [acl["rid"] for acl in response.json()["array"]]
def remove_permission_from_user(self, user_name, rid, action): rid = self._encode_rid(rid) response = requests.delete(self.base_url + "/acls/%s/users/%s/%s" % (rid, user_name, action), auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when removing permission from user")
def add_permission_to_user(self, user_name, rid, action): rid = self._encode_rid(rid) response = requests.put(self.base_url + r"/acls/%s/users/%s/%s" % (rid, user_name, action), auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when adding permission to user")
def create_account(self, name, description, public_key): data = dict(description=description, public_key=public_key) response = requests.put(self.base_url + "/users/" + name, json=data, auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when creating account")
def get_permissions_for_user(self, name): response = requests.get(self.base_url+"/users/%s/permissions" % name, auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when querying permissions for user") permissions = dict() for permission in response.json()["direct"]: permissions[permission["rid"]] = [a["name"] for a in permission["actions"]] return permissions
def _get_master_state(self): response = requests.get(self.mesos_url + "master/state", auth=get_auth(), verify=False) if response.ok: return response.json() else: raise Exception("Failed to get mesos master state: %s" % response.text)
def _get_plan_status(self, service_name, plan): response = requests.get(get_base_url() + "/service" + service_name + "v1/plans/%s" % plan, auth=get_auth(), verify=False) if response.ok: return response.json()["status"] else: return None
def delete_secret(self, name): response = requests.delete(self.base_url + "secret/default/%s" % name, auth=get_auth(), verify=False) if response.status_code == 404: return if not response.ok: raise Exception("Could not delete secret %s: %s " % (name, response.text))
def create_pool(self, config): """Create a new pool""" response = requests.post(self.base_url + "v2/pools", json=config, auth=get_auth(), verify=False) if not response.ok: raise Exception("Could not create pool %s: %s " % (config["name"], response.text))
def sign_csr(self, csr, hosts=list()): data = {"certificate_request": csr, "hosts": hosts} response = requests.post(self.base_url + "sign", json=data, auth=get_auth(), verify=False) if not response.ok: raise Exception("Failed to sign cert: %s" % response.text) return response.json()["result"]["certificate"]
def update_pool(self, config): """Update an existing pool config""" response = requests.put(self.base_url + "v2/pools/%s" % config["name"], json=config, auth=get_auth(), verify=False) if not response.ok: raise Exception("Could not update pool %s: %s " % (config["name"], response.text))
def get_cluster_info(self): response = requests.get(self.base_url + "/dcos-metadata/dcos-version.json", auth=get_auth(), verify=False) if not response.ok: raise Exception("Unknown error occured: %s" % response.text) info = response.json() return dict(version=info["version"], variant=info["dcos-variant"])
def get_pool(self, name): """Get config for a specific pool""" response = requests.get(self.base_url + "v2/pools/%s" % name, auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Failed to get pool") return response.json()
def delete_pool(self, name): response = requests.delete(self.base_url + "v2/pools/%s" % name, auth=get_auth(), verify=False) if response.ok: return True elif response.status_code == 404: return False else: raise Exception("Unknown error occured: %s" % response.text)
def list_secrets(self): """Retrive a list of secrets names""" response = requests.get(self.base_url + "secret/default/?list=true", auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Failed to list secrets") data = response.json()["array"] return data
def get_jobs(self): response = requests.get(self.metronome_url + "v1/jobs", auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Unknown error occured") data = response.json() for job in data: yield "/" + job["id"].replace(".", "/")
def create_permission(self, rid): rid = self._encode_rid(rid) response = requests.put( self.base_url + "/acls/%s" % rid, json=dict(description="created by dcos-deploy"), auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Error occured when creating permission")
def has_plans_api(self, service_name): if service_name[0] == "/": service_name = service_name[1:] response = requests.get(get_base_url() + "/service/" + service_name + "/v1/plans/", auth=get_auth(), verify=False) if response.ok: return True else: return False
def get_pools(self): """Retrive a list of pool names""" response = requests.get(self.base_url + "v2/pools", auth=get_auth(), verify=False) if not response.ok: print(response.text) raise Exception("Failed to list pools") data = response.json() for pool in data: yield pool["name"]
def restart_app(self, app_id, wait_for_deployment=False): response = requests.post(self.marathon_url + "/apps/%s/restart" % app_id, auth=get_auth(), verify=False) if not response.ok: print(response.text, flush=True) raise Exception("Failed to restart app %s" % app_id) deployment_id = response.json()["deploymentId"] if wait_for_deployment: self.wait_for_specific_deployment(deployment_id)
def delete_account(self, name): response = requests.delete(self.base_url + "/users/" + name, auth=get_auth(), verify=False) if response.ok: return True elif response.status_code == 404: return False else: raise Exception("Error occured when deleting account: %s" % response.text)
def ping(self): response = requests.get(self.base_url + "ping", auth=get_auth(), verify=False) if not response.ok: if response.status_code == 503: return False elif response.status_code == 404: return False else: raise Exception("Could not get ping from edgelb: %s" % response.text) return True
def create_job(self, definition): response = requests.post(self.metronome_url + "v0/scheduled-jobs", json=definition, auth=get_auth(), verify=False) if response.ok: return if response.status_code == 422: print(response.text) raise Exception("Invalid job definition") else: print(response.text) raise Exception("Unknown error occured")
def get_app_state(self, app_id): if not app_id[0] == "/": app_id = "/" + app_id response = requests.get(self.marathon_url + "/apps%s/?embed=app.counts" % app_id, auth=get_auth(), verify=False) if not response.ok: if response.status_code == 404: return None print(response.text, flush=True) raise Exception("Failed to get state for %s" % app_id) return response.json()["app"]