예제 #1
0
    def load(self, fp):
        dcluster = yaml.load(fp)
        if not dcluster:
            raise ClusterNotFound('create a cluster')
        from elasticluster import Cluster
        cluster = Cluster(**dcluster)

        cluster.repository = self
        return cluster
예제 #2
0
    def delete(self, cluster):
        """Deletes the cluster from memory.

        :param cluster: cluster to delete
        :type cluster: :py:class:`elasticluster.cluster.Cluster`
        """
        if cluster.name not in self.clusters:
            raise ClusterNotFound(
                "Unable to delete non-existent cluster %s" % cluster.name)
        del self.clusters[cluster.name]
예제 #3
0
    def get(self, name):
        """Retrieves the cluster by the given name.

        :param str name: name of the cluster (identifier)
        :return: instance of :py:class:`elasticluster.cluster.Cluster` that
                 matches the given name
        """
        if name not in self.clusters:
            raise ClusterNotFound("Cluster %s not found." % name)
        return self.clusters.get(name)
예제 #4
0
 def _get_store_by_name(self, name):
     """Return an instance of the correct DiskRepository based on the *first* file that matches the standard syntax for repository files"""
     for cls in self.storage_type_map.values():
         cluster_files = glob.glob(
             '%s/%s.%s' % (self.storage_path, name, cls.file_ending))
         if cluster_files:
             try:
                 return cls(self.storage_path)
             except:
                 continue
     raise ClusterNotFound("No cluster %s was found" % name)
예제 #5
0
 def _get_store_by_name(self, name):
     """Return an instance of the correct DiskRepository based on the *first* file that matches the standard syntax for repository files"""
     for cls in self.storage_type_map.values():
         filename = os.path.join(
             self.storage_path,
             '{name}.{ending}'.format(name=name, ending=cls.file_ending))
         if os.path.exists(filename):
             try:
                 return cls(self.storage_path)
             except:
                 continue
     raise ClusterNotFound(
         "No state file for cluster `{name}` was found in directory `{path}`"
         .format(name=name, path=self.storage_path))
예제 #6
0
    def load_cluster(self, cluster_name):
        """
        Read the storage file, create a cluster and return a `Cluster`
        object.
        """
        db_path = self._get_json_path(cluster_name)

        if not os.path.exists(db_path):
            raise ClusterNotFound("Storage file %s not found" % db_path)
        f = open(db_path, 'r')
        db_json = f.readline()

        information = json.loads(db_json)

        return information
예제 #7
0
    def get(self, name):
        """Retrieves the cluster with the given name.

        :param str name: name of the cluster (identifier)
        :return: :py:class:`elasticluster.cluster.Cluster`
        """
        path = self._get_cluster_storage_path(name)
        if not os.path.exists(path):
            raise ClusterNotFound("Storage file %s not found" % path)

        with open(path, 'r') as storage:
            cluster = pickle.load(storage)
            # Compatibility with previous version of Node
            for node in sum(cluster.nodes.values(), []):
                if not hasattr(node, 'ips'):
                    log.debug("Monkey patching old version of `Node` class: %s", node.name)
                    node.ips = [node.ip_public, node.ip_private]
                    node.preferred_ip = None
            return cluster
예제 #8
0
    def get(self, name):
        """Retrieves the cluster with the given name.

        :param str name: name of the cluster (identifier)
        :return: :py:class:`elasticluster.cluster.Cluster`
        """
        path = self._get_cluster_storage_path(name)

        try:
            with open(path, 'r') as storage:
                cluster = self.load(storage)
                # Compatibility with previous version of Node
                for node in sum(cluster.nodes.values(), []):
                    if not hasattr(node, 'ips'):
                        log.debug("Monkey patching old version of `Node` class: %s", node.name)
                        node.ips = [node.ip_public, node.ip_private]
                        node.preferred_ip = None
                cluster.storage_file = path
                return cluster
        except IOError as ex:
            raise ClusterNotFound("Error accessing storage file %s: %s" % (path, ex))
예제 #9
0
    def execute(self):
        """
        Starts a new cluster.
        """

        cluster_template = self.params.cluster
        if self.params.cluster_name:
            cluster_name = self.params.cluster_name
        else:
            cluster_name = self.params.cluster

        creator = make_creator(self.params.config,
                               storage_path=self.params.storage)

        if cluster_template not in creator.cluster_conf:
            raise ClusterNotFound(
                "No cluster template named `{0}`".format(cluster_template))

        # possibly overwrite node mix from config
        cluster_nodes_conf = creator.cluster_conf[cluster_template]['nodes']
        for kind, num in self.params.nodes_override.items():
            if kind not in cluster_nodes_conf:
                raise ConfigurationError(
                    "No node group `{kind}` defined"
                    " in cluster template `{template}`".format(
                        kind=kind, template=cluster_template))
            cluster_nodes_conf[kind]['num'] = num

        # First, check if the cluster is already created.
        try:
            cluster = creator.load_cluster(cluster_name)
        except ClusterNotFound:
            try:
                cluster = creator.create_cluster(cluster_template,
                                                 cluster_name)
            except ConfigurationError as err:
                log.error("Starting cluster %s: %s", cluster_template, err)
                return

        try:
            print("Starting cluster `{0}` with:".format(cluster.name))
            for cls in cluster.nodes:
                print("* {0:d} {1} nodes.".format(len(cluster.nodes[cls]),
                                                  cls))
            print("(This may take a while...)")
            min_nodes = dict((kind, cluster_nodes_conf[kind]['min_num'])
                             for kind in cluster_nodes_conf)
            cluster.start(min_nodes, self.params.max_concurrent_requests)
            if self.params.no_setup:
                print("NOT configuring the cluster as requested.")
            else:
                print("Configuring the cluster ...")
                print("(this too may take a while)")
                ok = cluster.setup()
                if ok:
                    print("\nYour cluster `{0}` is ready!".format(
                        cluster.name))
                else:
                    print("\nWARNING: YOUR CLUSTER `{0}` IS NOT READY YET!".
                          format(cluster.name))
            print(cluster_summary(cluster))
        except (KeyError, ImageError, SecurityGroupError, ClusterError) as err:
            log.error("Could not start cluster `%s`: %s", cluster.name, err)
            raise