Example #1
0
    def finish(self, credentials=None, region=None, back=False):
        if region:
            app.current_region = region

        if back:
            return controllers.use('clouds').render()

        if credentials is not None:
            cloud_type = juju.get_cloud_types_by_name()[app.current_cloud]
            if cloud_type == 'maas':
                # Now that we are passed the selection of a cloud we create a
                # new cloud name for the remainder of the deployment and make
                # sure this cloud is saved for future use.
                app.current_cloud = utils.gen_cloud()

                # Save credentials for new cloud
                common.save_creds(app.current_cloud, credentials)

                try:
                    juju.get_cloud(app.current_cloud)
                except LookupError:
                    juju.add_cloud(app.current_cloud,
                                   credentials.cloud_config())
            else:
                common.save_creds(app.current_cloud, credentials)
        credentials_key = common.try_get_creds(app.current_cloud)
        app.loop.create_task(
            common.do_bootstrap(credentials_key,
                                msg_cb=app.ui.set_footer,
                                fail_msg_cb=lambda e: None))
        controllers.use('deploy').render()
Example #2
0
    def save_credential(self, credential):
        cred_path = path.join(utils.juju_path(), 'credentials.yaml')
        cred_name = "conjure-{}-{}".format(app.current_cloud, utils.gen_hash())

        try:
            existing_creds = yaml.safe_load(open(cred_path))
        except:
            existing_creds = {'credentials': {}}

        if app.current_cloud in existing_creds['credentials'].keys():
            c = existing_creds['credentials'][app.current_cloud]
            c[cred_name] = self._format_creds(credential)
        else:
            # Handle the case where path exists but an entry for the cloud
            # has yet to be added.
            existing_creds['credentials'][app.current_cloud] = {
                cred_name: self._format_creds(credential)
            }

        with open(cred_path, 'w') as cred_f:
            cred_f.write(
                yaml.safe_dump(existing_creds, default_flow_style=False))

        # if it's a new MAAS cloud, save it now that we have a credential
        if app.current_cloud_type == 'maas':
            try:
                juju.get_cloud(app.current_cloud)
            except LookupError:
                juju.add_cloud(app.current_cloud, credential.cloud_config())

        # This should return the credential name so juju bootstrap knows
        # which credential to bootstrap with
        self.finish(cred_name)
Example #3
0
def setup_maas():
    maascreds = juju.get_credential(app.provider.cloud)
    if not maascreds:
        raise Exception(
            "Could not find MAAS credentials for cloud: {}".format(
                app.provider.cloud))
    try:
        endpoint = juju.get_cloud(app.provider.cloud).get('endpoint', None)
        app.log.debug(
            "Found endpoint: {} for cloud: {}".format(
                endpoint,
                app.provider.cloud))
    except LookupError as e:
        app.log.debug("Failed to find cloud in list-clouds, "
                      "attempting to read bootstrap-config")
        bc = juju.get_bootstrap_config(app.provider.controller)
        endpoint = bc.get('endpoint', None)

    if endpoint is None:
        raise Exception("Couldn't find endpoint for controller: {}".format(
            app.provider.controller))

    api_key = maascreds['maas-oauth']
    try:
        consumer_key, token_key, token_secret = api_key.split(':')
    except:
        raise Exception("Could not parse MAAS API Key '{}'".format(api_key))

    app.maas.endpoint = endpoint
    app.maas.api_key = api_key
    app.maas.client = MaasClient(server_address=endpoint,
                                 consumer_key=consumer_key,
                                 token_key=token_key,
                                 token_secret=token_secret)
Example #4
0
def setup_maas():
    maascreds = juju.get_credential(app.provider.cloud,
                                    app.provider.credential)
    if not maascreds:
        raise errors.MAASConfigError(
            "Could not find MAAS credentials for cloud: {}".format(
                app.provider.cloud))
    try:
        endpoint = juju.get_cloud(app.provider.cloud).get('endpoint', None)
        app.log.debug("Found endpoint: {} for cloud: {}".format(
            endpoint, app.provider.cloud))
    except LookupError:
        app.log.debug("Failed to find cloud in list-clouds, "
                      "attempting to read bootstrap-config")
        bc = juju.get_bootstrap_config(app.provider.controller)
        endpoint = bc.get('endpoint', None)

    if endpoint is None:
        raise errors.MAASConfigError("Couldn't find endpoint for controller: "
                                     "{}".format(app.provider.controller))

    try:
        api_key = maascreds['maas-oauth']
        consumer_key, token_key, token_secret = api_key.split(':')
    except (KeyError, ValueError):
        raise errors.MAASConfigError("Could not parse MAAS API Key")

    app.maas.endpoint = endpoint
    app.maas.api_key = api_key
Example #5
0
    def render(self):
        cloud = juju.get_cloud(app.current_cloud)
        if cloud['type'] != 'lxd':
            if not common.try_get_creds(app.current_cloud):
                utils.warning("You attempted to do an install against a cloud "
                              "that requires credentials that could not be "
                              "found.  If you wish to supply those "
                              "credentials please run "
                              "`juju add-credential "
                              "{}`.".format(app.current_cloud))
                sys.exit(1)

        if cloud['type'] == 'lxd':
            lxd = common.is_lxd_ready()
            if not lxd['ready']:
                return controllers.use('lxdsetup').render(lxd['msg'])

        utils.info("Bootstrapping Juju controller \"{}\" "
                   "with deployment \"{}\"".format(app.current_controller,
                                                   app.current_model))
        p = juju.bootstrap(controller=app.current_controller,
                           cloud=app.current_cloud,
                           model=app.current_model,
                           credential=common.try_get_creds(app.current_cloud))
        if p.returncode != 0:
            pathbase = os.path.join(app.config['spell-dir'],
                                    '{}-bootstrap').format(
                                        app.current_controller)
            with open(pathbase + ".err") as errf:
                utils.error("Error bootstrapping controller: "
                            "{}".format("".join(errf.readlines())))
            sys.exit(1)

        self.do_post_bootstrap()
        self.finish()
Example #6
0
 def load(self, cloud_name):
     """ Attempts to load a cloud from juju
     """
     try:
         _cloud = get_cloud(cloud_name)
         self.cloud = cloud_name
         self.endpoint = _cloud.get('endpoint', None)
         self.regions = sorted(_cloud.get('regions', {}).keys())
     except LookupError:
         raise errors.SchemaCloudError(cloud_name)
Example #7
0
    def render(self):
        track_screen("Cloud Creation")

        if app.current_controller is None:
            app.current_controller = "conjure-up-{}-{}".format(
                app.current_cloud, utils.gen_hash())

        if app.current_model is None:
            app.current_model = utils.gen_model()

        cloud_type = juju.get_cloud_types_by_name()[app.current_cloud]

        # LXD is a special case as we want to make sure a bridge
        # is configured. If not we'll bring up a new view to allow
        # a user to configure a LXD bridge with suggested network
        # information.
        if cloud_type == 'localhost':
            lxd = common.is_lxd_ready()
            if not lxd['ready']:
                return controllers.use('lxdsetup').render(lxd['msg'])
            # lxd doesn't require user-provided credentials,
            # so never show the editor for localhost
            return self.finish()

        creds = common.try_get_creds(app.current_cloud)
        try:
            endpoint = juju.get_cloud(app.current_cloud).get('endpoint', None)
        except LookupError:
            endpoint = None
        if creds and (endpoint or cloud_type != 'maas'):
            return self.finish()

        # show credentials editor otherwise
        try:
            creds = load_schema(app.current_cloud)
        except Exception as e:
            track_exception("Credentials Error: {}".format(e))
            app.log.exception("Credentials Error: {}".format(e))
            return app.ui.show_exception_message(
                Exception("Unable to find credentials for {}, "
                          "you can double check what credentials you "
                          "do have available by running "
                          "`juju credentials`. Please see `juju help "
                          "add-credential` for more information.".format(
                              app.current_cloud)))

        regions = []
        # No regions for these providers
        if cloud_type not in ['maas', 'vsphere']:
            regions = sorted(juju.get_regions(app.current_cloud).keys())
        view = NewCloudView(creds, regions, self.finish)

        app.ui.set_header(title="New cloud setup", )
        app.ui.set_body(view)
        app.ui.set_footer("")
Example #8
0
    async def _save_credential(self):
        cred_path = path.join(utils.juju_path(), 'credentials.yaml')
        app.provider.credential = "conjure-{}-{}".format(
            app.provider.cloud, utils.gen_hash())

        try:
            existing_creds = yaml.safe_load(open(cred_path))
        except:
            existing_creds = {'credentials': {}}

        if app.provider.cloud in existing_creds['credentials'].keys():
            c = existing_creds['credentials'][app.provider.cloud]
            c[app.provider.credential] = self._format_creds()
        else:
            # Handle the case where path exists but an entry for the cloud
            # has yet to be added.
            existing_creds['credentials'][app.provider.cloud] = {
                app.provider.credential: self._format_creds()
            }

        with open(cred_path, 'w') as cred_f:
            cred_f.write(
                yaml.safe_dump(existing_creds, default_flow_style=False))

        # Persist input fields in current provider, this is so we
        # can login to the provider for things like querying VSphere
        # for datacenters before that custom cloud is known to juju.
        await app.provider.save_form()

        # if it's a new MAAS or VSphere cloud, save it now that
        # we have a credential
        if app.provider.cloud_type in CUSTOM_PROVIDERS:
            try:
                juju.get_cloud(app.provider.cloud)
            except LookupError:
                juju.add_cloud(app.provider.cloud, await
                               app.provider.cloud_config())

        # This should return the credential name so juju bootstrap knows
        # which credential to bootstrap with
        self.finish()
Example #9
0
 def load(self, cloud_name):
     """ Attempts to load a cloud from juju
     """
     try:
         _cloud = get_cloud(cloud_name)
         self.cloud = cloud_name
         self.endpoint = _cloud.get('endpoint', None)
         self.regions = sorted(_cloud.get('regions', {}).keys())
     except LookupError:
         raise SchemaErrorUnknownCloud(
             "Unknown cloud: {}, not updating provider attributes".format(
                 cloud_name))
Example #10
0
    def finish(self, schema=None, region=None, back=False):
        if region:
            app.current_region = region

        if back:
            return controllers.use('clouds').render()

        if schema is not None:
            # Attempt to setup LXD with our selected interface
            if self.cloud_type == 'localhost':
                try:
                    common.lxd_init(schema.network_interface.value)

                    # Store LXD setup completion so we dont reconfigure on
                    # subsequent runs
                    common.get_lxd_setup_path().touch()
                except Exception:
                    raise

            elif self.cloud_type == 'maas':
                # Now that we are passed the selection of a cloud we create a
                # new cloud name for the remainder of the deployment and make
                # sure this cloud is saved for future use.
                app.current_cloud = utils.gen_cloud()

                # Save credentials for new cloud
                common.save_creds(app.current_cloud, schema)

                try:
                    juju.get_cloud(app.current_cloud)
                except LookupError:
                    juju.add_cloud(app.current_cloud,
                                   schema.cloud_config())
            else:
                common.save_creds(app.current_cloud, schema)
        credentials_key = common.try_get_creds(app.current_cloud)
        app.loop.create_task(common.do_bootstrap(credentials_key,
                                                 msg_cb=app.ui.set_footer,
                                                 fail_msg_cb=lambda e: None))
        controllers.use('deploy').render()
Example #11
0
    def render(self):
        track_screen("Cloud Creation")
        self.cloud_type = juju.get_cloud_types_by_name()[app.current_cloud]

        if app.current_controller is None:
            app.current_controller = "conjure-up-{}-{}".format(
                app.current_cloud,
                utils.gen_hash())

        if app.current_model is None:
            app.current_model = utils.gen_model()

        # LXD is a special case as we want to make sure a bridge
        # is configured. If not we'll bring up a new view to allow
        # a user to configure a LXD bridge with suggested network
        # information.
        if self.cloud_type == 'localhost':
            lxd_setup_path = common.get_lxd_setup_path()
            app.log.debug("Determining if embedded LXD is setup and ready.")
            if lxd_setup_path.exists():
                return self.finish()
            else:
                self.render_newcloud()

        # TODO: Prompt user to select credentials and set a region
        creds = common.try_get_creds(app.current_cloud)
        try:
            endpoint = juju.get_cloud(app.current_cloud).get(
                'endpoint', None)
        except LookupError:
            endpoint = None
        if creds and (endpoint or self.cloud_type != 'maas'):
            return self.finish()

        # No existing credentials found, start credential editor
        self.render_newcloud()