def get_diagnostics_bundle(item: pytest.Item): rc, _, _ = sdk_cmd.run_raw_cli('node diagnostics create all') if rc: log.error('Diagnostics bundle creation failed.') return @retrying.retry(wait_fixed=5000, stop_max_delay=10 * 60 * 1000, retry_on_result=lambda result: result is None) def wait_for_bundle_file(): rc, stdout, stderr = sdk_cmd.run_raw_cli( 'node diagnostics --status --json') if rc: return None # e.g. { "some-ip": { stuff we want } } status = next(iter(json.loads(stdout).values())) if status['job_progress_percentage'] != 100: return None # e.g. "/var/lib/dcos/dcos-diagnostics/diag-bundles/bundle-2018-01-11-1515698691.zip" return os.path.basename(status['last_bundle_dir']) bundle_filename = wait_for_bundle_file() if bundle_filename: sdk_cmd.run_cli('node diagnostics download {} --location={}'.format( bundle_filename, setup_artifact_path(item, bundle_filename))) else: log.error('Diagnostics bundle didnt finish in time, giving up.')
def install_app_from_file(app_name: str, app_def_path: str) -> (bool, str): """ Installs a marathon app using the path to an app definition. Args: app_def_path: Path to app definition Returns: (bool, str) tuple: Boolean indicates success of install attempt. String indicates error message if install attempt failed. """ cmd = "marathon app add {}".format(app_def_path) log.info("Running %s", cmd) rc, stdout, stderr = sdk_cmd.run_raw_cli(cmd) if rc or stderr: log.error("returncode=%s stdout=%s stderr=%s", rc, stdout, stderr) return False, stderr if "Created deployment" not in stdout: stderr = "'Created deployment' not in STDOUT" log.error(stderr) return False, stderr log.info("Waiting for app %s to be running...", app_name) shakedown.wait_for_task("marathon", app_name) return True, ""
def install_enterprise_cli(force=False): """ Install the enterprise CLI if required """ log.info("Installing DC/OS enterprise CLI") if not force: cmd = "security --version" _, stdout, _ = sdk_cmd.run_raw_cli(cmd, print_output=False) if stdout: log.info("DC/OS enterprise version %s CLI already installed", stdout.strip()) return cmd = "package install --yes --cli dcos-enterprise-cli" @retrying.retry(stop_max_attempt_number=3, wait_fixed=2000, retry_on_result=lambda result: result) def _install_impl(): rc, stdout, stderr = sdk_cmd.run_raw_cli(cmd) if rc: log.error("rc=%s stdout=%s stderr=%s", rc, stdout, stderr) return rc try: _install_impl() except Exception as e: raise RuntimeError( "Failed to install the dcos-enterprise-cli: {}".format(repr(e)))
def wait_for_bundle_file(): rc, stdout, stderr = sdk_cmd.run_raw_cli( 'node diagnostics --status --json') if rc: return None # e.g. { "some-ip": { stuff we want } } status = next(iter(json.loads(stdout).values())) if status['job_progress_percentage'] != 100: return None # e.g. "/var/lib/dcos/dcos-diagnostics/diag-bundles/bundle-2018-01-11-1515698691.zip" return os.path.basename(status['last_bundle_dir'])
def get_task_log_for_id(task_id: str, task_file: str = 'stdout', lines: int = 1000000) -> str: log.info('Fetching {} from {}'.format(task_file, task_id)) rc, stdout, stderr = sdk_cmd.run_raw_cli( 'task log {} --all --lines {} {}'.format(task_id, lines, task_file), print_output=False) if rc != 0: if not stderr.startswith('No files exist. Exiting.'): raise ConnectionError( 'Failed to get {} task log for task_id={}: {}'.format( task_file, task_id, stderr)) return '' return stdout
def remove_universe_repos(stub_urls): log.info('Removing universe repos') # clear out the added universe repositores at testing end for name, url in stub_urls.items(): log.info('Removing stub URL: {}'.format(url)) rc, stdout, stderr = sdk_cmd.run_raw_cli( 'package repo remove {}'.format(name)) if rc != 0 or stderr: if stderr.endswith('is not present in the list'): # tried to remove something that wasn't there, move on. pass else: raise Exception( 'Failed to remove stub repo: stdout=[{}], stderr=[{}]'. format(stdout, stderr)) log.info('Finished removing universe repos')
def _get_pkg_version(package_name): cmd = 'package describe {}'.format(package_name) # Only log stdout/stderr if there's actually an error. rc, stdout, stderr = sdk_cmd.run_raw_cli(cmd, print_output=False) if rc != 0: log.warning('Failed to run "{}":\nSTDOUT:\n{}\nSTDERR:\n{}'.format(cmd, stdout, stderr)) return None try: describe = json.loads(stdout) # New location (either 1.10+ or 1.11+): version = describe.get('package', {}).get('version', None) if version is None: # Old location (until 1.9 or until 1.10): version = describe['version'] return version except: log.warning('Failed to extract package version from "{}":\nSTDOUT:\n{}\nSTDERR:\n{}'.format(cmd, stdout, stderr)) log.warning(traceback.format_exc()) return None
def __create_and_upload_secret(self, keytab_path: str): """ This method base64 encodes the keytab file and creates a secret with this encoded content so the tasks can fetch it. """ log.info("Creating and uploading the keytab file %s to the secret store", keytab_path) try: base64_encoded_keytab_path = "{}.base64".format(keytab_path) with open(keytab_path, "rb") as f: keytab = f.read() base64_encoding = base64.b64encode(keytab).decode("utf-8") with open(base64_encoded_keytab_path, "w") as f: f.write(base64_encoding) log.info("Finished base64-encoding secret content (%d bytes): %s", len(base64_encoding), base64_encoding) except Exception as e: raise Exception("Failed to base64-encode the keytab file: {}".format(repr(e))) self.keytab_secret_path = "{}_keytab".format(DCOS_BASE64_PREFIX) sdk_security.install_enterprise_cli() # try to delete any preexisting secret data: sdk_security.delete_secret(self.keytab_secret_path) # create new secret: create_secret_cmd = "security secrets create {keytab_secret_path} --value-file {encoded_keytab_path}".format( keytab_secret_path=self.keytab_secret_path, encoded_keytab_path=base64_encoded_keytab_path) log.info("Creating secret named %s from file %s: %s", self.keytab_secret_path, base64_encoded_keytab_path, create_secret_cmd) rc, stdout, stderr = sdk_cmd.run_raw_cli(create_secret_cmd) if rc != 0: raise RuntimeError("Failed ({}) to create secret: {}\nstdout: {}\nstderr: {}".format(rc, create_secret_cmd, stdout, stderr)) log.info("Successfully uploaded a base64-encoded keytab file to the secret store")
def _install_impl(): rc, stdout, stderr = sdk_cmd.run_raw_cli(cmd) if rc: log.error("rc=%s stdout=%s stderr=%s", rc, stdout, stderr) return rc