def _bundle_download(bundle, location): """ Download diagnostics bundle. :param bundle: bundle file name. :type bundle: string :param location: location on a local filesystem. :type location: string :return: status code :rtype: int """ # make sure the requested bundle exists bundle_size = 0 for available_bundle in _get_bundle_list(): # _get_bundle_list must return a list of tuples # where first element is file name and second is its size. if len(available_bundle) != 2: raise DCOSException( 'Request to get a list of diagnostic bundles returned an ' 'unexpected response: {}'.format(available_bundle)) # available_bundle[0] is a file name # available_bundle[1] is a file size if available_bundle[0] == bundle: bundle_size = available_bundle[1] url = urllib.parse.urljoin(DIAGNOSTICS_BASE_URL, 'serve/' + bundle) bundle_location = os.path.join(os.getcwd(), bundle) if location: if os.path.isdir(location): bundle_location = os.path.join(location, bundle) else: bundle_location = location if bundle_size > BUNDLE_WARN_SIZE: msg = ('Diagnostics bundle size is {}, ' 'are you sure you want to download it?') if not confirm(msg.format(sizeof_fmt(bundle_size)), False): return 1 r = _do_request(url, 'GET', stream=True) try: with open(bundle_location, 'wb') as f: for chunk in r.iter_content(1024): f.write(chunk) except Exception as e: raise DCOSException(e) emitter.publish('Diagnostics bundle downloaded to ' + bundle_location) return 0
def _user_cert_validation(cert_str): """Prompt user for validation of certification from cluster :param cert_str: cluster certificate bundle :type cert_str: str :returns whether or not user validated cert :rtype: bool """ cert = x509.load_pem_x509_certificate( cert_str.encode('utf-8'), default_backend()) fingerprint = cert.fingerprint(hashes.SHA256()) pp_fingerprint = ":".join("{:02x}".format(c) for c in fingerprint).upper() msg = "SHA256 fingerprint of cluster certificate bundle:\n{}".format( pp_fingerprint) return confirm(msg, False)
def _install(package_name, package_version, options_path, app_id, cli, global_, app, yes): """Install the specified package. :param package_name: the package to install :type package_name: str :param package_version: package version to install :type package_version: str :param options_path: path to file containing option values :type options_path: str :param app_id: app ID for installation of this package :type app_id: str :param cli: indicates if the cli should be installed :type cli: bool :param global_: indicates that the cli should be installed globally :type global_: bool :param app: indicate if the application should be installed :type app: bool :param yes: automatically assume yes to all prompts :type yes: bool :returns: process status :rtype: int """ if cli is False and app is False: # Install both if neither flag is specified cli = app = True # Fail early if options file isn't valid user_options = util.read_file_json(options_path) package_manager = get_package_manager() pkg = package_manager.get_package_version(package_name, package_version) pkg_json = pkg.package_json() selected = pkg_json.get('selected') if selected: link = ('https://mesosphere.com/' 'catalog-terms-conditions/#certified-services') else: link = ('https://mesosphere.com/' 'catalog-terms-conditions/#community-services') emitter.publish(('By Deploying, you agree to ' 'the Terms and Conditions ' + link)) pre_install_notes = pkg_json.get('preInstallNotes') if app and pre_install_notes: emitter.publish(pre_install_notes) if not confirm('Continue installing?', yes): emitter.publish('Exiting installation.') return 0 if app and pkg.marathon_template(): # Even though package installation will check for template rendering # errors, we want to fail early, before trying to install. pkg.options(user_options) # Install in Marathon msg = 'Installing Marathon app for package [{}] version [{}]'.format( pkg.name(), pkg.version()) if app_id is not None: msg += ' with app id [{}]'.format(app_id) emitter.publish(msg) if app_id is not None: msg = "Usage of --app-id is deprecated. Use --options instead " \ "and specify a file that contains [service.name] property" emitter.publish(msg) package_manager.install_app(pkg, user_options, app_id) if cli and pkg.cli_definition(): # Install subcommand msg = 'Installing CLI subcommand for package [{}] version [{}]'.format( pkg.name(), pkg.version()) emitter.publish(msg) subcommand.install(pkg, global_) subcommand_paths = subcommand.get_package_commands(package_name) new_commands = [ os.path.basename(p).replace('-', ' ', 1) for p in subcommand_paths ] if new_commands: commands = ', '.join(new_commands) plural = "s" if len(new_commands) > 1 else "" emitter.publish("New command{} available: {}".format( plural, commands)) post_install_notes = pkg_json.get('postInstallNotes') if app and post_install_notes: emitter.publish(post_install_notes) return 0