def active_config(self): """Load Active config into ctx return ctx.params['du_url'] ctx.params['du_username'] ctx.params['du_password'] ctx.params['du_tenant'] ctx.params['du_region'] return does not need to be captured if context is available """ config_file = os.path.join(self.ctx.obj['pf9_db_dir'], 'express.conf') if os.path.exists(config_file): try: with open(config_file, 'r') as data: config_file_lines = data.readlines() except Exception as except_err: except_msg = "Failed reading {}: ".format(config_file) logger.exception(except_err, except_msg) raise CLIException(except_msg) try: config = self.config_to_dict(config_file_lines) if config is not None: if "name" in config: self.ctx.params['config_name'] = config["name"] else: self.ctx.params['config_name'] = ( config["os_username"].split( '@', 1)[0]) + '-' + config["du_url"] self.ctx.params['du_url'] = config["du_url"] self.ctx.params['du_username'] = config["os_username"] self.ctx.params['du_password'] = config["os_password"] self.ctx.params['du_tenant'] = config["os_tenant"] self.ctx.params['du_region'] = config["os_region"] # This is needed if we want them to be loaded as python bool if config.get("dev_key") == 'True': self.ctx.params['dev_key'] = True else: self.ctx.params['dev_key'] = False if config.get("disable_analytics") == 'True': self.ctx.params['disable_analytics'] = True else: self.ctx.params['disable_analytics'] = False return self.ctx except Exception as except_err: except_msg = "Failed parsing active config {}: ".format( config_file) logger.exception(except_err, except_msg) raise CLIException(except_msg) except_msg = "No active config. Please define or activate a config." logger.exception(except_msg) raise CLIException(except_msg)
def get_token(ctx, silent): """Returns an Auth Token for the active config from the Platform9 Management Plane""" logger.info(msg=click.get_current_context().info_name) try: # Load Active Config into ctx Get(ctx).active_config() # Get Token token = Get(ctx).get_token() if token is not None: if silent: logger.info(msg="Silent Get Token Success") click.echo(token) sys.exit(0) logger.info(msg="Get Token Success") click.echo("Management Plane: {}".format(ctx.params["du_url"])) click.echo("Username: {}".format(ctx.params["du_username"])) click.echo("Region: {}".format(ctx.params["du_region"])) click.echo("Token: %s" % token) else: msg = "Failed to obtain Authentication from: {}".format( ctx.params["du_url"]) raise CLIException(msg) except (UserAuthFailure, CLIException) as except_err: logger.exception(except_err) click.echo("Failed to obtain Authentication from: {}".format( ctx.params["du_url"])) sys.exit(1) except Exception as except_err: logger.exception("GENERICALLY CAUGHT EXCEPTION! {}".format(except_err)) click.echo("Failed to obtain Authentication from: {}".format( ctx.params["du_url"])) sys.exit(1)
def build_ansible_command(self): """Build the bash command that will be sent to pf9-express""" # Invoke PMK only related playbook. # TODO: rework to allow for PMO/PMK or deauth. In this function or another _inv_file = self.build_express_inventory_file() try: Get(self.ctx).get_token_project() du_fqdn = Get(self.ctx).region_fqdn() if du_fqdn is None: msg = "Failed to obtain region url from: {} \ for region: {}".format(self.ctx.param["du_url"], self.ctx.param["du_region"]) raise CLIException(msg) except (UserAuthFailure, CLIException) as except_err: logger.exception(except_err) raise extra_args = '-e "skip_prereq=1 autoreg={} du_fqdn={} ctrl_ip={} du_username={} du_password={} ' \ 'du_region={} du_tenant={} du_token={}"'.format( "'on'", du_fqdn, Utils.ip_from_dns_name(du_fqdn), self.ctx.params['du_username'], self.ctx.params['du_password'], self.ctx.params['du_region'], self.ctx.params['du_tenant'], self.ctx.params['token']) cmd = '{} -i {} -l pmk {} {}' \ .format( self.ctx.obj['pf9_exec_ansible-playbook'], _inv_file, extra_args, self.ctx.obj['pf9_k8_playbook']) return cmd
def region_fqdn(self): """Calls ostoken.GetRegionURL().get_region_url. return region_fqdn """ try: self.active_config() region_url = GetRegionURL( self.ctx.params["du_url"], self.ctx.params["du_username"], self.ctx.params["du_password"], self.ctx.params["du_tenant"], self.ctx.params["du_region"]).get_region_url() if region_url is None: msg = "Failed to obtain region url from: {} " \ "for region: {}".format(self.ctx.param["du_url"], self.ctx.param["du_region"]) raise CLIException(msg) return region_url except CLIException as except_err: logger.exception(except_err) raise
def test_get_region_url(ctx): """Returns the FQDN of the public service api endpoint on the Platform9 Management Plane for the region specified in the current active config """ logger.info(msg=click.get_current_context().info_name) try: region_url = Get(ctx).region_fqdn() if region_url is None: msg = "Failed to obtain region url from: {} \ for region: {}".format(ctx.param["du_url"], ctx.param["du_region"]) raise CLIException(msg) logger.info(msg="Get Region FQDN Success") click.echo(region_url) except (UserAuthFailure, CLIException) as except_msg: logger.info(msg="Get Region FQDN Failed! {}".format(except_msg)) click.echo("Get Region FQDN Failed!") sys.exit(1) except Exception as except_msg: logger.info(msg="GENERICALLY CAUGHT EXCEPTION! {}".format(except_msg)) click.echo("Get Region FQDN Failed!") sys.exit(1)
def __init__(self, ctx, user, password, ssh_key, ips, node_prep_only, inv_file_template): self.ctx = ctx self.user = user self.password = password self.ssh_key = ssh_key self.ips = ips self.node_prep_only = node_prep_only self.inv_file_template = inv_file_template if self.ctx.params['floating_ip']: floating_ips = ctx.params['floating_ip'] ctx.params['floating_ip'] = ''.join(floating_ips).split( ' ') if all(len(x) == 1 for x in floating_ips) else list(floating_ips) if len(self.ctx.params['floating_ip']) == len(self.ips): self.ips = self.ctx.params['floating_ip'] logger.info( "Configuring node(s) via Floating IP(s): {}".format( self.ips)) else: except_msg = "Number of floating IPs does not match nodes provided" raise CLIException(except_msg)
def get_token_project(self): """Calls ostoken.GetToken().get_token_project() using active config return token, project_id """ try: self.active_config() token_project = GetToken().get_token_project( self.ctx.params["du_url"], self.ctx.params["du_username"], self.ctx.params["du_password"], self.ctx.params["du_tenant"]) if not token_project: except_err = "Failed to obtain an Authentication Token from: {}".format( self.ctx.params["du_url"]) raise CLIException(except_err) # Add token and project_id to ctx and return token_project self.ctx.params['token'], self.ctx.params[ 'project_id'] = token_project return token_project except UserAuthFailure as except_msg: logger.exception(except_msg) raise except CLIException as except_err: logger.exception(except_err) raise except_err