func postClusterApplication(request *restful.Request, response *restful.Response) { cluster := &application.Cluster{} err := request.ReadEntity(&cluster) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Read body failure" jsonMap["ErrorMessage"] = err.Error() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } err = application.GetStorage().SaveClusterApplication(cluster) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Save cluster application failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["cluster"] = cluster errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } }
func getAllStatelessApplication(request *restful.Request, response *restful.Response) { statelessSlice, err := application.GetStorage().LoadAllStatelessApplication() if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get all stateless application failure" jsonMap["ErrorMessage"] = err.Error() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } response.WriteJson(statelessSlice, "[]Stateless") }
func deleteStatelessApplication(request *restful.Request, response *restful.Response) { name := request.PathParameter("statelessapplication") err := application.GetStorage().DeleteStatelessApplication(name) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Delete stateless application failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["name"] = name errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } }
func getClusterApplication(request *restful.Request, response *restful.Response) { name := request.PathParameter("clusterapplication") cluster, err := application.GetStorage().LoadClusterApplication(name) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get cluster application failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["cluster"] = cluster errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } response.WriteJson(cluster, "Cluster") }
func deleteClusterApplication(request *restful.Request, response *restful.Response) { name := request.PathParameter("clusterapplication") deployClusterApplicationSlice, err := deploy.GetAllDeployClusterApplication() if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get all cluster application deployment failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["name"] = name errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } for _, deployClusterApplication := range deployClusterApplicationSlice { if deployClusterApplication.Name == name { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Used template" jsonMap["ErrorMessage"] = "There is at least one deployment using this template" jsonMap["name"] = name errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(403, string(errorMessageByteSlice)) return } } err = application.GetStorage().DeleteClusterApplication(name) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Delete cluster application failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["name"] = name errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } }
func DeleteDeployClusterApplication(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, name string) error { deployClusterApplication, err := GetDeployClusterApplication(namespace, name) if err != nil { log.Error("Get deploy cluster application error %s", err) return err } // Remove deploy cluster application err = GetStorage().DeleteDeployClusterApplication(namespace, name) if err != nil { log.Error("Delete the deploy cluster application error %s", err) return err } cluster, err := application.GetStorage().LoadClusterApplication(name) if err != nil { log.Error("Load cluster application error %s", err) return err } // Generate random work space workingDirectory := "/tmp/tmp_" + random.UUID() scriptFileName := "script" // Check working space if _, err := os.Stat(workingDirectory); os.IsNotExist(err) { err := os.MkdirAll(workingDirectory, os.ModePerm) if err != nil { log.Error("Create non-existing directory %s error: %s", workingDirectory, err) return err } } err = ioutil.WriteFile(workingDirectory+string(os.PathSeparator)+scriptFileName, []byte(cluster.ScriptContent), os.ModePerm) if err != nil { log.Error("Write script file for cluster application error %s", err) return err } switch cluster.ScriptType { case "none": for _, replicationControllerName := range deployClusterApplication.ReplicationControllerNameSlice { err := control.DeleteReplicationControllerAndRelatedPod(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName) if err != nil { log.Error("Delete replication controller %s error %s", replicationControllerName, err) return err } } err = control.DeleteService(kubeApiServerEndPoint, kubeApiServerToken, namespace, deployClusterApplication.ServiceName) if err != nil { log.Error("Delete service %s error %s", deployClusterApplication.ServiceName, err) return err } case "python": command := exec.Command("python", scriptFileName, "--application_name="+name, "--kube_apiserver_endpoint="+kubeApiServerEndPoint, "--kube_apiserver_token="+kubeApiServerToken, "--namespace="+namespace, "--timeout_in_second=120", "--action=delete") command.Dir = workingDirectory out, err := command.CombinedOutput() log.Debug(string(out)) if err != nil { log.Error("Run python script file for cluster application error %s", err) return errors.New("Error: " + err.Error() + " Output: " + string(out)) } default: log.Error("No such script type: %s", cluster.ScriptType) return errors.New("No such script type: " + cluster.ScriptType) } // Remove working space err = os.RemoveAll(workingDirectory) if err != nil { log.Error("Remove the working directory for cluster application error %s", err) return err } return nil }
func ResizeDeployClusterApplication(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, name string, environmentSlice []interface{}, size int) error { cluster, err := application.GetStorage().LoadClusterApplication(name) if err != nil { log.Error("Load cluster application error %s", err) return err } replicationControllerJsonMap := make(map[string]interface{}) err = json.Unmarshal([]byte(cluster.ReplicationControllerJson), &replicationControllerJsonMap) if err != nil { log.Error("Unmarshal replication controller json for cluster application error %s", err) return err } // Add environment variable if environmentSlice != nil { if replicationControllerJsonMap["spec"] != nil { containerSlice := replicationControllerJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["containers"].([]interface{}) for i := 0; i < len(containerSlice); i++ { _, ok := containerSlice[i].(map[string]interface{})["env"].([]interface{}) if ok { for _, environment := range environmentSlice { containerSlice[i].(map[string]interface{})["env"] = append(containerSlice[i].(map[string]interface{})["env"].([]interface{}), environment) } } else { containerSlice[i].(map[string]interface{})["env"] = environmentSlice } } } } replicationControllerByteSlice, err := json.Marshal(replicationControllerJsonMap) if err != nil { log.Error("Marshal replication controller json for cluster application error %s", err) return err } environmentByteSlice, err := json.Marshal(environmentSlice) if err != nil { log.Error("Marshal environment json for cluster application error %s", err) return err } // Generate random work space workingDirectory := "/tmp/tmp_" + random.UUID() replicationControllerFileName := "replication-controller.json" scriptFileName := "script" environmentFileName := "environment.json" // Check working space if _, err := os.Stat(workingDirectory); os.IsNotExist(err) { err := os.MkdirAll(workingDirectory, os.ModePerm) if err != nil { log.Error("Create non-existing directory %s error: %s", workingDirectory, err) return err } } err = ioutil.WriteFile(workingDirectory+string(os.PathSeparator)+environmentFileName, environmentByteSlice, os.ModePerm) if err != nil { log.Error("Write environment json file for cluster application error %s", err) return err } err = ioutil.WriteFile(workingDirectory+string(os.PathSeparator)+replicationControllerFileName, replicationControllerByteSlice, os.ModePerm) if err != nil { log.Error("Write replication controller json file for cluster application error %s", err) return err } err = ioutil.WriteFile(workingDirectory+string(os.PathSeparator)+scriptFileName, []byte(cluster.ScriptContent), os.ModePerm) if err != nil { log.Error("Write script file for cluster application error %s", err) return err } switch cluster.ScriptType { case "none": deployClusterApplication, err := GetDeployClusterApplication(namespace, name) if err != nil { log.Error("Get deploy cluster application error %s", err) return err } for _, replicationControllerName := range deployClusterApplication.ReplicationControllerNameSlice { err := control.UpdateReplicationControllerSize(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName, size) if err != nil { log.Error("Resize replication controller %s error %s", replicationControllerName, err) return err } } case "python": command := exec.Command("python", scriptFileName, "--application_name="+name, "--kube_apiserver_endpoint="+kubeApiServerEndPoint, "--kube_apiserver_token="+kubeApiServerToken, "--namespace="+namespace, "--size="+strconv.Itoa(size), "--replication_controller_file_name="+replicationControllerFileName, "--environment_file_name="+environmentFileName, "--timeout_in_second=120", "--action=resize") command.Dir = workingDirectory out, err := command.CombinedOutput() log.Debug(string(out)) if err != nil { log.Error("Run python script file for cluster application error %s", err) return errors.New("Error: " + err.Error() + " Output: " + string(out)) } default: log.Error("No such script type: %s", cluster.ScriptType) return errors.New("No such script type: " + cluster.ScriptType) } // Remove working space err = os.RemoveAll(workingDirectory) if err != nil { log.Error("Remove the working directory for cluster application error %s", err) return err } // Update deploy data deployClusterApplication, err := GetStorage().LoadDeployClusterApplication(namespace, name) if err != nil { log.Error("Update the deploy cluster application with error %s", err) return err } _, serviceName, replicationControllerNameSlice, err := getServiceNameAndReplicationControllerNameSlice(kubeApiServerEndPoint, kubeApiServerToken, namespace, name) if err != nil { log.Error(err) return err } deployClusterApplication.Size = size deployClusterApplication.EnvironmentSlice = environmentSlice deployClusterApplication.ServiceName = serviceName deployClusterApplication.ReplicationControllerNameSlice = replicationControllerNameSlice err = GetStorage().SaveDeployClusterApplication(deployClusterApplication) if err != nil { log.Error("Save the deploy cluster application error %s", err) return err } return nil }