Ejemplo n.º 1
0
    def start(self, name, worker_api, mapped_ports, log_type, log_level,
              log_server, config):
        try:
            cluster, cluster_name, kube_config, ports_index, nfsServer_ip, \
                consensus = self._get_cluster_info(name, config)

            operation = K8sClusterOperation(kube_config)
            containers = operation.start_cluster(cluster_name, ports_index,
                                                 nfsServer_ip, consensus)

            if not containers:
                logger.warning(
                    "failed to start cluster={}, stop it again.".format(
                        cluster_name))
                operation.stop_cluster(cluster_name, ports_index, nfsServer_ip,
                                       consensus)
                return None

            service_urls = self.get_services_urls(name)
            # Update the service port table in db
            for k, v in service_urls.items():
                service_port = ServicePort(name=k,
                                           ip=v.split(":")[0],
                                           port=int(v.split(":")[1]),
                                           cluster=cluster)
                service_port.save()
            for k, v in containers.items():
                container = Container(id=v, name=k, cluster=cluster)
                container.save()

        except Exception as e:
            logger.error("Failed to start Kubernetes Cluster: {}".format(e))
            return None
        return containers
Ejemplo n.º 2
0
    def delete(self, cid, worker_api, config):
        try:
            cluster, cluster_name, kube_config, ports_index, nfsServer_ip,\
                consensus = self._get_cluster_info(cid, config)

            operation = K8sClusterOperation(kube_config)
            cluster_name = self.trim_cluster_name(cluster_name)
            operation.delete_cluster(cluster_name, ports_index, nfsServer_ip,
                                     consensus, config.get('network_type'))

            # delete ports for clusters
            cluster_ports = \
                ServicePort.Query.filter(cluster=cluster.as_pointer)
            if cluster_ports:
                for ports in cluster_ports:
                    ports.delete()
            cluster_containers = Container.Query.\
                filter(cluster=cluster.as_pointer)
            if cluster_containers:
                for container in cluster_containers:
                    container.delete()

        except Exception as e:
            logger.error("Failed to delete Kubernetes Cluster: {}".format(e))
            return False
        return True
Ejemplo n.º 3
0
    def delete(self, cid, worker_api, config):
        try:
            cluster, cluster_name, kube_config, ports_index, nfsServer_ip,\
                consensus = self._get_cluster_info(cid, config)

            operation = K8sClusterOperation(kube_config)
            operation.delete_cluster(cluster_name,
                                     ports_index,
                                     nfsServer_ip,
                                     consensus)

            # delete ports for clusters
            cluster_ports = ServicePort.objects(cluster=cluster)
            if cluster_ports:
                for ports in cluster_ports:
                    ports.delete()
            cluster_containers = Container.objects(cluster=cluster)
            if cluster_containers:
                for container in cluster_containers:
                    container.delete()

        except Exception as e:
            logger.error("Failed to delete Kubernetes Cluster: {}".format(e))
            return False
        return True
Ejemplo n.º 4
0
 def stop(self, kube_config, cluster_name, ports_index, nfsServer_ip):
     try:
         operation = K8sClusterOperation(kube_config)
         operation.stop_cluster(cluster_name, ports_index, nfsServer_ip)
     except Exception as e:
         logger.error("Failed to stop Kubernetes Cluster: {}".format(e))
         return False
     return True
Ejemplo n.º 5
0
 def start(self, kube_config, cluster_name, ports_index, nfsServer_ip):
     try:
         operation = K8sClusterOperation(kube_config)
         containers = operation.start_cluster(cluster_name, ports_index,
                                              nfsServer_ip)
     except Exception as e:
         logger.error("Failed to start Kubernetes Cluster: {}".format(e))
         return None
     return containers
Ejemplo n.º 6
0
 def get_services_urls(self, kube_config, cluster_name):
     try:
         operation = K8sClusterOperation(kube_config)
         services_urls = operation.get_services_urls(cluster_name)
     except Exception as e:
         logger.error(
             "Failed to get Kubernetes services's urls: {}".format(e))
         return None
     return services_urls
Ejemplo n.º 7
0
    def create(self, cid, mapped_ports, host, config, user_id):
        try:
            cluster, cluster_name, kube_config, ports_index, nfsServer_ip, \
                consensus = self._get_cluster_info(cid, config)

            operation = K8sClusterOperation(kube_config)
            containers = operation.deploy_cluster(cluster_name, ports_index,
                                                  nfsServer_ip, consensus)
        except Exception as e:
            logger.error("Failed to create Kubernetes Cluster: {}".format(e))
            return None
        return containers
Ejemplo n.º 8
0
    def get_services_urls(self, cid):
        try:
            cluster = ClusterModel.objects.get(id=cid)

            cluster_name = cluster.name
            kube_config = KubernetesOperation()._get_from_params(
                cluster.host.k8s_param)

            operation = K8sClusterOperation(kube_config)
            services_urls = operation.get_services_urls(cluster_name)
        except Exception as e:
            logger.error(
                "Failed to get Kubernetes services's urls: {}".format(e))
            return None
        return services_urls
Ejemplo n.º 9
0
    def stop(self, name, worker_api, mapped_ports, log_type, log_level,
             log_server, config):
        try:
            cluster, cluster_name, kube_config, ports_index, nfsServer_ip, \
                consensus = self._get_cluster_info(name, config)

            operation = K8sClusterOperation(kube_config)
            operation.stop_cluster(cluster_name, ports_index, nfsServer_ip,
                                   consensus)

            cluster_ports = ServicePort.objects(cluster=cluster)
            for ports in cluster_ports:
                ports.delete()
            cluster_containers = Container.objects(cluster=cluster)
            for container in cluster_containers:
                container.delete()

        except Exception as e:
            logger.error("Failed to stop Kubernetes Cluster: {}".format(e))
            return False
        return True