Exemple #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
}
Exemple #2
0
func LaunchClusterApplicationNoScript(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
	}

	serviceJsonMap := make(map[string]interface{})
	err = json.Unmarshal([]byte(cluster.ServiceJson), &serviceJsonMap)
	if err != nil {
		log.Error("Unmarshal service 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] = environmentJsonMap
								}

								// 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
							}
						}
					}
				}
			}
		}
	}

	// Change size
	specJsonMap, ok := replicationControllerJsonMap["spec"].(map[string]interface{})
	if ok {
		specJsonMap["replicas"] = size
	}

	err = control.CreateReplicationControllerWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerJsonMap)
	if err != nil {
		log.Error("CreateReplicationControllerWithJson error %s", err)
		return err
	}
	err = control.CreateServiceWithJson(kubeApiServerEndPoint, kubeApiServerToken, namespace, serviceJsonMap)
	if err != nil {
		log.Error("CreateReplicationControllerWithJson error %s", err)
		return err
	}

	return nil
}
func CreateReplicationController(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, replicationController ReplicationController) (returnedError error) {
	defer func() {
		if err := recover(); err != nil {
			log.Error("CreateReplicationController Error: %s", err)
			log.Error(logger.GetStackTrace(4096, false))
			returnedError = err.(error)
		}
	}()

	containerJsonMapSlice := make([]interface{}, 0)
	for _, replicationControllerContainer := range replicationController.ContainerSlice {
		containerJsonMap := make(map[string]interface{})
		containerJsonMap["name"] = replicationControllerContainer.Name
		containerJsonMap["image"] = replicationControllerContainer.Image
		containerJsonMap["resources"] = replicationControllerContainer.ResourceMap

		portJsonMapSlice := make([]interface{}, 0)
		for _, replicationControllerContainerPort := range replicationControllerContainer.PortSlice {
			portJsonMap := make(map[string]interface{})
			portJsonMap["name"] = replicationControllerContainerPort.Name
			portJsonMap["containerPort"] = replicationControllerContainerPort.ContainerPort
			portJsonMapSlice = append(portJsonMapSlice, portJsonMap)
		}
		containerJsonMap["ports"] = portJsonMapSlice

		environmentJsonMapSlice := make([]interface{}, 0)
		for _, environment := range replicationControllerContainer.EnvironmentSlice {
			environmentJsonMap := make(map[string]interface{})
			environmentJsonMap["name"] = environment.Name
			environmentJsonMap["value"] = environment.Value
			environmentJsonMapSlice = append(environmentJsonMapSlice, environmentJsonMap)
		}
		containerJsonMap["env"] = environmentJsonMapSlice

		// FIXME temporarily to use nested docker
		volumeMountJsonMap := make(map[string]interface{})
		volumeMountJsonMap["name"] = "docker"
		volumeMountJsonMap["readOnly"] = true
		volumeMountJsonMap["mountPath"] = "/var/run/docker.sock"
		volumeMountJsonMapSlice := make([]interface{}, 0)
		volumeMountJsonMapSlice = append(volumeMountJsonMapSlice, volumeMountJsonMap)
		containerJsonMap["volumeMounts"] = volumeMountJsonMapSlice

		containerJsonMapSlice = append(containerJsonMapSlice, containerJsonMap)
	}

	bodyJsonMap := make(map[string]interface{})
	bodyJsonMap["kind"] = "ReplicationController"
	bodyJsonMap["apiVersion"] = "v1"
	bodyJsonMap["metadata"] = make(map[string]interface{})
	bodyJsonMap["metadata"].(map[string]interface{})["name"] = replicationController.Name
	bodyJsonMap["metadata"].(map[string]interface{})["labels"] = make(map[string]interface{})
	bodyJsonMap["metadata"].(map[string]interface{})["labels"].(map[string]interface{})["name"] = replicationController.Label.Name
	bodyJsonMap["spec"] = make(map[string]interface{})
	bodyJsonMap["spec"].(map[string]interface{})["replicas"] = replicationController.ReplicaAmount
	bodyJsonMap["spec"].(map[string]interface{})["selector"] = make(map[string]interface{})
	bodyJsonMap["spec"].(map[string]interface{})["selector"].(map[string]interface{})["name"] = replicationController.Selector.Name
	bodyJsonMap["spec"].(map[string]interface{})["selector"].(map[string]interface{})["version"] = replicationController.Selector.Version
	bodyJsonMap["spec"].(map[string]interface{})["template"] = make(map[string]interface{})
	bodyJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["metadata"] = make(map[string]interface{})
	bodyJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["metadata"].(map[string]interface{})["labels"] = make(map[string]interface{})
	bodyJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["metadata"].(map[string]interface{})["labels"].(map[string]interface{})["name"] = replicationController.Selector.Name
	bodyJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["metadata"].(map[string]interface{})["labels"].(map[string]interface{})["version"] = replicationController.Selector.Version
	bodyJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"] = make(map[string]interface{})
	bodyJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["containers"] = containerJsonMapSlice

	// FIXME temporarily to use nested docker
	volumeJsonMap := make(map[string]interface{})
	volumeJsonMap["name"] = "docker"
	volumeJsonMap["hostPath"] = make(map[string]interface{})
	volumeJsonMap["hostPath"].(map[string]interface{})["path"] = "/var/run/docker.sock"
	volumeJsonMapSlice := make([]interface{}, 0)
	volumeJsonMapSlice = append(volumeJsonMapSlice, volumeJsonMap)
	bodyJsonMap["spec"].(map[string]interface{})["template"].(map[string]interface{})["spec"].(map[string]interface{})["volumes"] = volumeJsonMapSlice

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

	headerMap := make(map[string]string)
	headerMap["Authorization"] = kubeApiServerToken

	url := kubeApiServerEndPoint + "/api/v1/namespaces/" + namespace + "/replicationcontrollers/"
	_, err := restclient.RequestPost(url, bodyJsonMap, headerMap, true)

	if err != nil {
		return err
	} else {
		return nil
	}
}