Example #1
0
 def execute(self):
     """Pause the cluster if it is running."""
     cluster_name = self.params.cluster
     creator = make_creator(self.params.config,
                            storage_path=self.params.storage)
     try:
         cluster = creator.load_cluster(cluster_name)
     except (ClusterNotFound, ConfigurationError) as e:
         log.error("Cannot load cluster `%s`: %s", cluster_name, e)
         return os.EX_NOINPUT
     if not self.params.yes:
         confirm_or_abort(
             "Do you want really want to pause cluster `{cluster_name}`?".
             format(cluster_name=cluster_name),
             msg="Aborting upon user request.")
     print("Pausing cluster `%s` ..." % cluster_name)
     cluster.pause()
Example #2
0
 def execute(self):
     """Pause the cluster if it is running."""
     cluster_name = self.params.cluster
     creator = make_creator(self.params.config,
                            storage_path=self.params.storage)
     try:
         cluster = creator.load_cluster(cluster_name)
     except (ClusterNotFound, ConfigurationError) as e:
         log.error("Cannot load cluster `%s`: %s", cluster_name, e)
         return os.EX_NOINPUT
     if not self.params.yes:
         confirm_or_abort(
             "Do you want really want to pause cluster `{cluster_name}`?"
             .format(cluster_name=cluster_name),
             msg="Aborting upon user request.")
     print("Pausing cluster `%s` ..." % cluster_name)
     cluster.pause()
Example #3
0
    def execute(self):
        """
        Stops the cluster if it's running.
        """
        cluster_name = self.params.cluster
        configurator = get_configurator(self.params.config,
                                        storage_path=self.params.storage)
        try:
            cluster = configurator.load_cluster(cluster_name)
        except (ClusterNotFound, ConfigurationError) as err:
            log.error("Cannot stop cluster `%s`: %s", cluster_name, err)
            return os.EX_NOINPUT

        if not self.params.yes:
            confirm_or_abort(
                "Do you want really want to stop cluster `{cluster_name}`?".
                format(cluster_name=cluster_name),
                msg="Aborting upon user request.")
        print("Destroying cluster `%s` ..." % cluster_name)
        cluster.stop(force=self.params.force, wait=self.params.wait)
    def execute(self):
        creator = make_creator(self.params.config,
                               storage_path=self.params.storage)

        # Get current cluster configuration
        cluster_name = self.params.cluster

        try:
            cluster = creator.load_cluster(cluster_name)
            cluster.update()
        except (ClusterNotFound, ConfigurationError) as ex:
            log.error("Error loading cluster %s: %s\n" %
                      (cluster_name, ex))
            return

        # Find the node to remove.
        try:
            node = cluster.get_node_by_name(self.params.node)
        except NodeNotFound:
            log.error("Node %s not found in cluster %s" % (
                self.params.node, self.params.cluster))
            sys.exit(1)

        # Run
        if not self.params.yes:
            confirm_or_abort("Do you really want to remove node `{}`?"
                             .format(node.name),
                             msg="Aborting upon user request.")

        cluster.remove_node(node, stop=True)
        print("Node %s removed" % node.name)

        if self.params.no_setup:
            print("NOT reconfiguring the cluster as requested.")
        else:
            print("Reconfiguring the cluster.")
            cluster.setup()
Example #5
0
    def execute(self):
        creator = make_creator(self.params.config,
                               storage_path=self.params.storage)

        # Get current cluster configuration
        cluster_name = self.params.cluster

        try:
            cluster = creator.load_cluster(cluster_name)
            cluster.update()
        except (ClusterNotFound, ConfigurationError) as ex:
            log.error("Error loading cluster %s: %s\n" %
                      (cluster_name, ex))
            return

        # Find the node to remove.
        try:
            node = cluster.get_node_by_name(self.params.node)
        except NodeNotFound:
            log.error("Node %s not found in cluster %s" % (
                self.params.node, self.params.cluster))
            sys.exit(1)

        # Run
        if not self.params.yes:
            confirm_or_abort("Do you really want to remove node `{}`?"
                             .format(node.name),
                             msg="Aborting upon user request.")

        cluster.remove_node(node, stop=True)
        print("Node %s removed" % node.name)

        if self.params.no_setup:
            print("NOT reconfiguring the cluster as requested.")
        else:
            print("Reconfiguring the cluster.")
            cluster.setup()
Example #6
0
    def execute(self):
        creator = make_creator(self.params.config,
                               storage_path=self.params.storage)

        try:
            cluster = creator.load_cluster(self.params.cluster)
        except ClusterNotFound:
            log.error("Cluster `%s` not found in storage dir %s." %
                      (self.params.cluster, self.params.storage))
            sys.exit(1)

        if os.path.exists(self.params.zipfile) and not self.params.overwrite:
            log.error("ZIP file `%s` already exists." % self.params.zipfile)
            sys.exit(1)

        with ZipFile(self.params.zipfile, 'w') as zipfile:
            # The root of the zip file will contain:
            # * the storage file
            # * the known_hosts file
            # * ssh public and prived keys, if --save-keys is used
            #
            # it will NOT contain the ansible inventory file, as this
            # is automatically created when needed.
            #
            # Also, if --save-keys is used and there is an host with a
            # different ssh private/public key than the default, they
            # will be saved in:
            #
            #   ./<cluster>/<group>/<nodename>/
            #
            def verbose_add(fname, basedir='', comment=None):
                zipname = basedir + os.path.basename(fname)
                log.info("Adding '%s' as '%s'" % (fname, zipname))
                zipfile.write(fname, zipname)
                if comment:
                    info = zipfile.getinfo(zipname)
                    info.comment = comment

            try:
                verbose_add(cluster.storage_file, comment='cluster-file')
                verbose_add(cluster.known_hosts_file, comment='known_hosts')
                if self.params.save_keys:
                    # that's sensible stuff, let's ask permission.
                    print("""
==========================
WARNING! WARNING! WARNING!
==========================
You are about to add your SSH *private* key to the
ZIP archive. These are sensible data: anyone with
access to the ZIP file will have access to any host
where this private key has been deployed.

                    """)
                    confirm_or_abort(
                        "Are you sure you still want to copy them?",
                        msg="Aborting upon user request.")

                    # Also save all the public and private keys we can find.

                    # Cluster keys
                    verbose_add(cluster.user_key_public)
                    verbose_add(cluster.user_key_private)

                    # Node keys, if found
                    for node in cluster.get_all_nodes():
                        if node.user_key_public != cluster.user_key_public:
                            verbose_add(
                                node.user_key_public, "%s/%s/%s/" %
                                (cluster.name, node.kind, node.name))
                    for node in cluster.get_all_nodes():
                        if node.user_key_private != cluster.user_key_private:
                            verbose_add(
                                node.user_key_private, "%s/%s/%s/" %
                                (cluster.name, node.kind, node.name))
            except OSError as ex:
                # A file is probably missing!
                log.error(
                    "Fatal error: cannot add file %s to zip archive: %s." %
                    (ex.filename, ex))
                sys.exit(1)

        print("Cluster '%s' correctly exported into %s" %
              (cluster.name, self.params.zipfile))
Example #7
0
    def execute(self):
        creator = make_creator(self.params.config,
                               storage_path=self.params.storage)

        # Get current cluster configuration
        cluster_name = self.params.cluster
        template = self.params.template

        try:
            cluster = creator.load_cluster(cluster_name)
            cluster.update()
        except (ClusterNotFound, ConfigurationError) as ex:
            log.error("Listing nodes from cluster %s: %s\n" %
                      (cluster_name, ex))
            return
        for grp in self.params.nodes_to_add:
            print("Adding %d %s node(s) to the cluster"
                  "" % (self.params.nodes_to_add[grp], grp))

            # Currently we can't save which template was used to setup a
            # cluster, therefore we imply the configuration of the new nodes
            # to match already existent nodes in this group. If no node was
            # added to this group yet, it will abort and ask for the
            # `--template` argument.
            # TODO: find a better solution for this problem, it makes things
            #       complicated for the user
            if (not grp in cluster.nodes or not cluster.nodes[grp]) \
                    and not template:
                print "Elasticluster can not infer which template to use for "\
                      "the new node(s). Please provide the template with " \
                      "the `-t` or `--template` option"
                return

            if not template:
                sample_node = cluster.nodes[grp][0]
                for i in range(self.params.nodes_to_add[grp]):
                    cluster.add_node(grp,
                                     sample_node.image_id,
                                     sample_node.image_user,
                                     sample_node.flavor,
                                     sample_node.security_group,
                                     image_userdata=sample_node.image_userdata,
                                     **sample_node.extra)
            else:
                conf = creator.cluster_conf[template]
                conf_kind = conf['nodes'][grp]

                image_user = conf['login']['image_user']
                userdata = conf_kind.get('image_userdata', '')

                extra = conf_kind.copy()
                extra.pop('image_id', None)
                extra.pop('flavor', None)
                extra.pop('security_group', None)
                extra.pop('image_userdata', None)

                for i in range(self.params.nodes_to_add[grp]):
                    cluster.add_node(grp,
                                     conf_kind['image_id'],
                                     image_user,
                                     conf_kind['flavor'],
                                     conf_kind['security_group'],
                                     image_userdata=userdata,
                                     **extra)

        for grp in self.params.nodes_to_remove:
            n_to_rm = self.params.nodes_to_remove[grp]
            print("Removing %d %s node(s) from the cluster."
                  "" % (n_to_rm, grp))
            to_remove = cluster.nodes[grp][-n_to_rm:]
            print("The following nodes will be removed from the cluster.")
            print("    " + str.join("\n    ", [n.name for n in to_remove]))

            if not self.params.yes:
                confirm_or_abort("Do you really want to remove them?",
                                 msg="Aborting upon user request.")

            for node in to_remove:
                cluster.nodes[grp].remove(node)
                node.stop()

        cluster.start()
        if self.params.no_setup:
            print("NOT configuring the cluster as requested.")
        else:
            print("Reconfiguring the cluster.")
            cluster.setup()
        print(cluster_summary(cluster))
    def execute(self):
        creator = make_creator(self.params.config,
                               storage_path=self.params.storage)

        try:
            cluster = creator.load_cluster(self.params.cluster)
        except ClusterNotFound:
            log.error("Cluster `%s` not found in storage dir %s."
                      % (self.params.cluster, self.params.storage))
            sys.exit(1)

        if os.path.exists(self.params.zipfile) and not self.params.overwrite:
            log.error("ZIP file `%s` already exists." % self.params.zipfile)
            sys.exit(1)

        with ZipFile(self.params.zipfile, 'w') as zipfile:
            # The root of the zip file will contain:
            # * the storage file
            # * the known_hosts file
            # * ssh public and prived keys, if --save-keys is used
            #
            # it will NOT contain the ansible inventory file, as this
            # is automatically created when needed.
            #
            # Also, if --save-keys is used and there is an host with a
            # different ssh private/public key than the default, they
            # will be saved in:
            #
            #   ./<cluster>/<group>/<nodename>/
            #
            def verbose_add(fname, basedir='', comment=None):
                zipname = basedir + os.path.basename(fname)
                log.info("Adding '%s' as '%s'" % (fname, zipname))
                zipfile.write(fname, zipname)
                if comment:
                    info = zipfile.getinfo(zipname)
                    info.comment = comment

            try:
                verbose_add(cluster.storage_file, comment='cluster-file')
                verbose_add(cluster.known_hosts_file, comment='known_hosts')
                if self.params.save_keys:
                    # that's sensible stuff, let's ask permission.
                    print("""
==========================
WARNING! WARNING! WARNING!
==========================
You are about to add your SSH *private* key to the
ZIP archive. These are sensible data: anyone with
access to the ZIP file will have access to any host
where this private key has been deployed.

                    """)
                    confirm_or_abort(
                        "Are you sure you still want to copy them?",
                        msg="Aborting upon user request.")

                    # Also save all the public and private keys we can find.

                    # Cluster keys
                    verbose_add(cluster.user_key_public)
                    verbose_add(cluster.user_key_private)

                    # Node keys, if found
                    for node in cluster.get_all_nodes():
                        if node.user_key_public != cluster.user_key_public:
                            verbose_add(node.user_key_public,
                                        "%s/%s/%s/" % (cluster.name,
                                                       node.kind,
                                                       node.name))
                    for node in cluster.get_all_nodes():
                        if node.user_key_private != cluster.user_key_private:
                            verbose_add(node.user_key_private,
                                        "%s/%s/%s/" % (cluster.name,
                                                       node.kind,
                                                       node.name))
            except OSError as ex:
                # A file is probably missing!
                log.error("Fatal error: cannot add file %s to zip archive: %s."
                          % (ex.filename, ex))
                sys.exit(1)

        print("Cluster '%s' correctly exported into %s" %
              (cluster.name, self.params.zipfile))
    def execute(self):
        creator = make_creator(self.params.config,
                               storage_path=self.params.storage)

        # Get current cluster configuration
        cluster_name = self.params.cluster
        template = self.params.template

        try:
            cluster = creator.load_cluster(cluster_name)
            cluster.update()
        except (ClusterNotFound, ConfigurationError) as ex:
            log.error("Listing nodes from cluster %s: %s\n" %
                      (cluster_name, ex))
            return
        for grp in self.params.nodes_to_add:
            print("Adding %d %s node(s) to the cluster"
                  "" % (self.params.nodes_to_add[grp], grp))

            # Currently we can't save which template was used to setup a
            # cluster, therefore we imply the configuration of the new nodes
            # to match already existent nodes in this group. If no node was
            # added to this group yet, it will abort and ask for the
            # `--template` argument.
            # TODO: find a better solution for this problem, it makes things
            #       complicated for the user
            if (not grp in cluster.nodes or not cluster.nodes[grp]) \
                    and not template:
                print "Elasticluster can not infer which template to use for "\
                      "the new node(s). Please provide the template with " \
                      "the `-t` or `--template` option"
                return

            if not template:
                sample_node = cluster.nodes[grp][0]
                for i in range(self.params.nodes_to_add[grp]):
                    cluster.add_node(grp,
                                     sample_node.image_id,
                                     sample_node.image_user,
                                     sample_node.flavor,
                                     sample_node.security_group,
                                     image_userdata=sample_node.image_userdata,
                                     **sample_node.extra)
            else:
                conf = creator.cluster_conf[template]
                conf_kind = conf['nodes'][grp]

                image_user = conf['login']['image_user']
                userdata = conf_kind.get('image_userdata', '')

                extra = conf_kind.copy()
                extra.pop('image_id', None)
                extra.pop('flavor', None)
                extra.pop('security_group', None)
                extra.pop('image_userdata', None)

                for i in range(self.params.nodes_to_add[grp]):
                    cluster.add_node(grp,
                                     conf_kind['image_id'],
                                     image_user,
                                     conf_kind['flavor'],
                                     conf_kind['security_group'],
                                     image_userdata=userdata,
                                     **extra)

        for grp in self.params.nodes_to_remove:
            n_to_rm = self.params.nodes_to_remove[grp]
            print("Removing %d %s node(s) from the cluster."
                  "" % (n_to_rm, grp))
            to_remove = cluster.nodes[grp][-n_to_rm:]
            print("The following nodes will be removed from the cluster.")
            print("    " + str.join("\n    ", [n.name for n in to_remove]))

            if not self.params.yes:
                confirm_or_abort("Do you really want to remove them?",
                                 msg="Aborting upon user request.")

            for node in to_remove:
                cluster.nodes[grp].remove(node)
                node.stop()

        cluster.start()
        if self.params.no_setup:
            print("NOT configuring the cluster as requested.")
        else:
            print("Reconfiguring the cluster.")
            cluster.setup()
        print(cluster_summary(cluster))