def check_cluster(name=None, url=None): """Check whether cluster is already setup. :param url: url of the cluster :return: boolean whether cluster is already setup """ if url is not None: fqdn = urlparse(url).netloc else: fqdn = None attached_cluster = None wanted_cluster = None clusters = subprocess.check_output(['dcos', 'cluster', 'list', '--json'], env=_dcos_path()) for c in json.loads(clusters): if fqdn == urlparse(c['url']).netloc: wanted_cluster = c elif c['name'] == name: wanted_cluster = c if c['attached'] is True: attached_cluster = c display.vvv('wanted:\n{}\nattached:\n{}\n'.format(wanted_cluster, attached_cluster)) if wanted_cluster is None: return False elif wanted_cluster == attached_cluster: return True else: subprocess.check_call( ['dcos', 'cluster', 'attach', wanted_cluster['cluster_id']], env=_dcos_path()) return True
def get_user_state(uid): """Get the current state of a user.""" r = subprocess.check_output([ 'dcos', 'security', 'org', 'users', 'show', '--json' ], env=_dcos_path() ) users = json.loads(r) display.vvv('looking for uid {}'.format(uid)) state = 'absent' for g in users: try: if uid in g: state = 'present' display.vvv('found uid: {}'.format(uid)) except KeyError: continue return state
def get_secret_value(path, store): """Get the current value of a secret.""" display.vvv('looking for secret {} '.format(path)) value = None try: r = subprocess.check_output([ 'dcos', 'security', 'secrets', 'get', '--store-id', store, path ], env=_dcos_path(), stderr=subprocess.STDOUT ) value = r display.vvv('secret {} has value {}'.format(path, value)) except: value = None return value
def app_update(app_id, options): """Update an app via Marathon""" display.vvv("DC/OS: Marathon update app {}".format(app_id)) # create a temporary file for the options json file with tempfile.NamedTemporaryFile('w+') as f: json.dump(options, f) # force write the file to disk to make sure subcommand can read it f.flush() os.fsync(f) cmd = [ 'dcos', 'marathon', 'app', 'update', '--force', app_id ] from subprocess import Popen, PIPE p = Popen(cmd, env=_dcos_path(), stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate(json.dumps(options).encode()) display.vvv("stdout {}".format(stdout)) display.vvv("stderr {}".format(stderr))
def ensure_dcos_edgelb(instance_name): """Check whether the dcos[cli] edgelb extension is installed.""" try: subprocess.check_output([ 'dcos', 'edgelb', '--name=' + instance_name, 'ping' ], env=_dcos_path()).decode() except: display.vvv("dcos edgelb: not installed") install_dcos_edgelb_cli() subprocess.check_output([ 'dcos', 'edgelb', '--name=' + instance_name, 'ping' ], env=_dcos_path()).decode() display.vvv("dcos edgelb: all prerequisites seem to be in order")
def install_dcos_edgelb_cli(): """Install DC/OS edgelb CLI""" display.vvv("dcos edgelb: installing cli") cmd = [ 'dcos', 'package', 'install', 'edgelb', '--cli', '--yes' ] display.vvv(subprocess.check_output(cmd, env=_dcos_path()).decode())
def get_group_state(gid): """Get the current state of a group.""" r = subprocess.check_output( ['dcos', 'security', 'org', 'groups', 'show', '--json'], env=_dcos_path()) groups = json.loads(r) display.vvv('looking for gid {}'.format(gid)) state = 'absent' if gid in groups: state = 'present' display.vvv('found gid: {}'.format(gid)) return state
def get_service_account_state(sid): """Get the current state of a service_account.""" r = subprocess.check_output( ['dcos', 'security', 'org', 'service-accounts', 'show', '--json'], env=_dcos_path()) service_accounts = json.loads(r) display.vvv('looking for sid {}'.format(sid)) state = 'absent' if sid in service_accounts: state = 'present' display.vvv('found sid: {}'.format(sid)) return state
def get_app_state(app_id): """Get the current state of an app.""" r = subprocess.check_output(['dcos', 'marathon', 'app', 'list', '--json' ], env=_dcos_path()) apps = json.loads(r) display.vvv('looking for app_id {}'.format(app_id)) state = 'absent' for a in apps: try: if app_id == a['id']: state = 'present' display.vvv('found app: {}'.format(app_id)) except KeyError: continue return state
def get_pod_state(pod_id): """Get the current state of an pod.""" r = subprocess.check_output(['dcos', 'marathon', 'pod', 'list', '--json' ], env=_dcos_path()) pods = json.loads(r) display.vvv('looking for pod_id {}'.format(pod_id)) state = 'absent' for a in pods: try: if pod_id in a['id']: state = 'present' display.vvv('found pod: {}'.format(pod_id)) except KeyError: continue return state
def get_current_version(package, app_id): """Get the current version of an installed package.""" r = subprocess.check_output( ['dcos', 'package', 'list', '--json', '--app-id=' + app_id], env=_dcos_path()) packages = json.loads(r) display.vvv('looking for package {} app_id {}'.format(package, app_id)) v = None for p in packages: try: if p['name'] == package and '/' + app_id in p['apps']: v = p['version'] except KeyError: continue display.vvv('{} current version: {}'.format(package, v)) return v
def get_repo_state(name): """Get the current state of a repo""" r = subprocess.check_output(['dcos', 'package', 'repo', 'list', '--json'], env=_dcos_path()) repos = json.loads(r)['repositories'] display.vvv('looking for repo {}'.format(name)) state = 'absent' for n in repos: try: if n['name'] == name: state = 'present' display.vvv('found repo name: {}'.format(n['name'])) except KeyError: continue return state
def get_pool_state(pool_id, instance_name): """Get the current state of a pool.""" r = subprocess.check_output( ['dcos', 'edgelb', 'list', '--name=' + instance_name, '--json'], env=_dcos_path()) pools = json.loads(r) display.vvv('looking for pool_id {}'.format(pool_id)) state = 'absent' for p in pools: try: if pool_id in p['name']: state = 'present' display.vvv('found pool: {}'.format(pool_id)) except KeyError: continue return state
def get_quota_state(gid): """Get the current state of a quota.""" r = subprocess.check_output(['dcos', 'quota', 'list', '--json'], env=_dcos_path()) quotas = json.loads(r) display.vvv('looking for gid {}'.format(gid)) state = 'absent' for q in quotas: try: if gid == q['role']: state = 'present' display.vvv('found pool: {}'.format(gid)) except KeyError: continue return state
def update_package(package, app_id, version, options): """Update a Universe package on DC/OS.""" display.vvv("DC/OS: updating package {} version {}".format( package, version)) # create a temporary file for the options json file with tempfile.NamedTemporaryFile('w+') as f: json.dump(options, f) # force write the file to disk to make sure subcommand can read it f.flush() os.fsync(f) display.vvv(subprocess.check_output( ['cat', f.name]).decode()) r = subprocess.check_output([ 'dcos', 'package', 'describe', package, '--options', f.name, '--package-version', version, '--render', '--app', ], env=_dcos_path()) app_update(app_id, json.loads(r)) # workaround: install cli to refresh dcos package list cmd = [ 'dcos', 'package', 'install', package, '--package-version', version, '--yes', '--cli' ] run_command(cmd, 'install cli', stop_on_error=True)
def ensure_dcos_edgelb(instance_name): """Check whether the dcos[cli] edgelb extension is installed.""" tries = 3 for i in range(tries): try: subprocess.check_output( ['dcos', 'edgelb', '--name=' + instance_name, 'ping'], env=_dcos_path()).decode() except: if i < tries - 1: display.vvv("dcos edgelb: ping failed {} times".format(i + 1)) install_dcos_edgelb_cli() time.sleep(10) continue else: raise AnsibleActionFail( 'Edge-LB: Pool cannot be configured because the API server is not reachable.' ) break
def connect_cluster(**kwargs): """Connect to a DC/OS cluster by url""" changed = False url = kwargs.get('url') if not check_cluster(kwargs.get('name'), url): if url is None: raise AnsibleActionFail( 'Not connected: you need to specify the cluster url') display.vvv('DC/OS cluster not setup, setting up') cli_args = parse_connect_options(**kwargs) display.vvv('args: {}'.format(cli_args)) subprocess.check_call(['dcos', 'cluster', 'setup', url] + cli_args, env=_dcos_path()) changed = True # ensure_auth(**kwargs) return changed