Exemplo n.º 1
0
func LaunchClusterApplicationPython(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, cluster *Cluster, environmentSlice []interface{}, size int, replicationControllerExtraJsonMap map[string]interface{}) error {
	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
	}

	// Configure extra json body
	// It is used for user to input any configuration
	if replicationControllerExtraJsonMap != nil {
		deepcopy.DeepOverwriteJsonMap(replicationControllerExtraJsonMap, replicationControllerJsonMap)
	}

	// Add environment variable
	if environmentSlice != nil {
		specJsonMap, ok := replicationControllerJsonMap["spec"].(map[string]interface{})
		if ok {
			templateJsonMap, ok := specJsonMap["template"].(map[string]interface{})
			if ok {
				specJsonMap, ok := templateJsonMap["spec"].(map[string]interface{})
				if ok {
					containerSlice, ok := specJsonMap["containers"].([]interface{})
					if ok {
						for i := 0; i < len(containerSlice); i++ {
							oldEnvironmentSlice, ok := containerSlice[i].(map[string]interface{})["env"].([]interface{})
							if ok {
								// Track old environment
								environmentMap := make(map[string]interface{})
								for _, oldEnvironment := range oldEnvironmentSlice {
									oldEnvironmentJsonMap, _ := oldEnvironment.(map[string]interface{})
									name, _ := oldEnvironmentJsonMap["name"].(string)
									environmentMap[name] = oldEnvironment
								}

								// Add or overwrite with new value
								for _, environment := range environmentSlice {
									environmentJsonMap, _ := environment.(map[string]interface{})
									name, _ := environmentJsonMap["name"].(string)
									environmentMap[name] = environment
								}

								// Use new slice which the user input overwrite the old data
								newEnvironmentSlice := make([]interface{}, 0)
								for _, value := range environmentMap {
									newEnvironmentSlice = append(newEnvironmentSlice, value)
								}
								containerSlice[i].(map[string]interface{})["env"] = newEnvironmentSlice
							} 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"
	serviceFileName := "service.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)+serviceFileName, []byte(cluster.ServiceJson), os.ModePerm)
	if err != nil {
		log.Error("Write service 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
	}

	command := exec.Command("python", scriptFileName,
		"--application_name="+cluster.Name,
		"--kube_apiserver_endpoint="+kubeApiServerEndPoint,
		"--kube_apiserver_token="+kubeApiServerToken,
		"--namespace="+namespace,
		"--size="+strconv.Itoa(size),
		"--service_file_name="+serviceFileName,
		"--replication_controller_file_name="+replicationControllerFileName,
		"--environment_file_name="+environmentFileName,
		"--timeout_in_second=300",
		"--action=create")
	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))
	}

	// 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 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
}
Exemplo n.º 3
0
func (glusterfsCluster *GlusterfsCluster) CreateVolume(glusterfsVolumeCreateParameter *GlusterfsVolumeCreateParameter) error {
	host, err := glusterfsCluster.getAvailableHost()
	if err != nil {
		return err
	}

	commandBuffer := bytes.Buffer{}
	commandBuffer.WriteString("sudo gluster --mode=script volume create ")
	commandBuffer.WriteString(glusterfsVolumeCreateParameter.VolumeName)

	if glusterfsVolumeCreateParameter.Stripe > 0 {
		commandBuffer.WriteString(" stripe ")
		commandBuffer.WriteString(strconv.Itoa(glusterfsVolumeCreateParameter.Stripe))
	}
	if glusterfsVolumeCreateParameter.Replica > 0 {
		commandBuffer.WriteString(" replica ")
		commandBuffer.WriteString(strconv.Itoa(glusterfsVolumeCreateParameter.Replica))
	}
	if glusterfsVolumeCreateParameter.Arbiter > 0 {
		commandBuffer.WriteString(" arbiter ")
		commandBuffer.WriteString(strconv.Itoa(glusterfsVolumeCreateParameter.Arbiter))
	}
	if glusterfsVolumeCreateParameter.Disperse > 0 {
		commandBuffer.WriteString(" disperse ")
		commandBuffer.WriteString(strconv.Itoa(glusterfsVolumeCreateParameter.Disperse))
	}
	if glusterfsVolumeCreateParameter.DisperseData > 0 {
		commandBuffer.WriteString(" disperse-data ")
		commandBuffer.WriteString(strconv.Itoa(glusterfsVolumeCreateParameter.DisperseData))
	}
	if glusterfsVolumeCreateParameter.Redundancy > 0 {
		commandBuffer.WriteString(" redundancy ")
		commandBuffer.WriteString(strconv.Itoa(glusterfsVolumeCreateParameter.Redundancy))
	}

	// <tcp|rdma|tcp,rdma>
	commandBuffer.WriteString(" transport ")
	commandBuffer.WriteString(glusterfsVolumeCreateParameter.Transport)
	uuid := random.UUID()
	for _, ip := range glusterfsVolumeCreateParameter.HostSlice {
		path := " " + ip + ":" + glusterfsCluster.Path + "/" + glusterfsVolumeCreateParameter.VolumeName + "_" + uuid
		commandBuffer.WriteString(path)
	}
	commandBuffer.WriteString(" force\n")
	commandSlice := make([]string, 0)
	commandSlice = append(commandSlice, commandBuffer.String())

	interactiveMap := make(map[string]string)
	interactiveMap["[sudo]"] = glusterfsCluster.SSHPassword + "\n"

	resultSlice, err := sshclient.InteractiveSSH(
		glusterfsCluster.SSHDialTimeout,
		glusterfsCluster.SSHSessionTimeout,
		*host,
		glusterfsCluster.SSHPort,
		glusterfsCluster.SSHUser,
		glusterfsCluster.SSHPassword,
		commandSlice,
		interactiveMap)

	if err != nil {
		log.Error("Create volume error %s resultSlice %v", err, resultSlice)
		return err
	}

	if strings.Contains(resultSlice[0], "success") == false {
		log.Debug("Issue command: " + commandBuffer.String())
		log.Error("Fail to create volume with error: " + resultSlice[0])
		return errors.New(resultSlice[0])
	}

	err = GetStorage().SaveGlusterfsVolumeCreateParameter(glusterfsVolumeCreateParameter)
	if err != nil {
		log.Error(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
}