controllers

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommandDetachedDeleteById = func(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	cmdId := params["cid"]
	if state.GetInstance().GetBackgroundCommandList()[cmdId] == nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.COMMAND_DETACHED_STOP_FAILURE),
			u.GetMessage()[uint32(constants.COMMAND_DETACHED_STOP_FAILURE)],
			errors.New(fmt.Sprintf("Exception: There is no active process for command %s", cmdId)).Error(),
			r.URL.Path))
		return
	}
	u.KillCmdBackgroundProcess(cmdId)

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		u.GetMessage()[uint32(constants.SUCCESS)],
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var CommandDetachedGet = func(w http.ResponseWriter, r *http.Request) {
	jsonFileName := state.GetLastCommand()
	cd := models.NewCommandDescription()

	if !u.DoesFileExists(jsonFileName) {
		err := u.WriteFileJson(jsonFileName, &models.CommandDescription{})
		if err != nil {
			u.ApiResponseError(w, u.ApiMessage(uint32(constants.GET_COMMAND_DETACHED_INFO_FAILURE),
				u.GetMessage()[uint32(constants.GET_COMMAND_DETACHED_INFO_FAILURE)],
				fmt.Sprintf("File %s does not exists", jsonFileName),
				r.URL.Path))
			return
		}
	}

	err := json.Unmarshal(u.ReadFile(jsonFileName), cd)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.GET_COMMAND_DETACHED_INFO_FAILURE),
			u.GetMessage()[uint32(constants.GET_COMMAND_DETACHED_INFO_FAILURE)],
			err.Error(),
			r.URL.Path))
		return
	}
	commands := cd.GetCommands()
	cmdId := cd.GetId()
	processId := cd.GetPid()

	for cmd := range commands {
		cmdDetails := commands[cmd].GetCommandDetails()
		cmdDetails.SetOut(string(u.ReadFile(u.GetBase64HashForTheCommand(cmd, cmdId, ".out"))))
		cmdDetails.SetErr(string(u.ReadFile(u.GetBase64HashForTheCommand(cmd, cmdId, ".err"))))
	}
	var processes = u.GetProcessesForPid(processId)
	cd.SetProcesses(processes)

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		cd,
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var CommandDetachedGetById = func(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	cmdId := params["cid"]
	jsonFileName := fmt.Sprintf(constants.CMD_BACKGROUND_JSON_OUTPUT, cmdId)
	cd := models.NewCommandDescription()

	err := json.Unmarshal(u.ReadFile(jsonFileName), cd)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.GET_COMMAND_DETACHED_INFO_FAILURE),
			u.GetMessage()[uint32(constants.GET_COMMAND_DETACHED_INFO_FAILURE)],
			err.Error(),
			r.URL.Path))
		return
	}
	commands := cd.GetCommands()
	processId := cd.GetPid()
	for cmd := range commands {
		cmdDetails := commands[cmd].GetCommandDetails()
		cmdDetails.SetOut(string(u.ReadFile(u.GetBase64HashForTheCommand(cmd, cmdId, ".out"))))
		cmdDetails.SetErr(string(u.ReadFile(u.GetBase64HashForTheCommand(cmd, cmdId, ".err"))))
	}

	var processes = u.GetProcessesForPid(processId)
	cd.SetProcesses(processes)

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		cd,
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var CommandDetachedPost = func(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	params := mux.Vars(r)
	cmdId := params["cid"]
	commands := u.TrimSpacesAndLineEndings(
		strings.Split(strings.Trim(string(body), " "), "\n"))
	if len(commands) == 0 {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.EMPTY_REQUEST_BODY_PROVIDED),
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			r.URL.Path))
		return
	}
	var cmdBackground []string
	if runtime.GOOS == "windows" {
		cmdBackground = []string{"runcmd",
			"--cid=" + cmdId, "--args=" + strings.Join(commands, ";;"), "--enableStreams=true"}
	} else {
		cmdBackground = []string{"./runcmd --cid=" + cmdId + " --args=\"" + strings.Join(commands, ";;") + "\"" +
			" --enableStreams=true"}
	}
	log.Print(fmt.Sprintf("Starting command '%s' in background", strings.Join(cmdBackground, " ")))
	u.KillCmdBackgroundProcess(cmdId)
	ch := make(chan error)
	go u.StartCommand(cmdId, cmdBackground, ch)
	err := <-ch
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.COMMAND_DETACHED_START_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.COMMAND_DETACHED_START_FAILURE)], cmdId),
			err,
			r.URL.Path))
		return
	}
	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		cmdId,
		r.URL.Path)
	w.Header().Add("Content-Type", "application/json")
	w.WriteHeader(http.StatusAccepted)
	state.SetLastCommand(cmdId)

	u.ApiResponse(w, resp)
}
View Source
var CommandDetachedPostYaml = func(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	params := mux.Vars(r)
	cmdId := params["cid"]
	if len(strings.Trim(string(body), " ")) == 0 {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.EMPTY_REQUEST_BODY_PROVIDED),
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			r.URL.Path))
		return
	}

	var yamlConfig = models.NewYamlConfig()
	var configParser = u.NewYamlConfigParser()
	err := yaml.Unmarshal(body, &yamlConfig)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.INVALID_YAML_CONFIG),
			u.GetMessage()[uint32(constants.INVALID_YAML_CONFIG)],
			err.Error(),
			r.URL.Path))
		return
	}
	err = configParser.CheckConfig(yamlConfig)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.INVALID_YAML_CONFIG),
			u.GetMessage()[uint32(constants.INVALID_YAML_CONFIG)],
			err.Error(),
			r.URL.Path))
		return
	}
	envVars := yamlConfig.GetEnv()
	environment.GetInstance().SetEnvVars(envVars)

	var cmdBackground []string
	if runtime.GOOS == "windows" {
		cmdBackground = []string{"runcmd",
			"--cid=" + cmdId, "--args=" + strings.Join(configParser.GetCommandsList(yamlConfig), ";;"), "--enableStreams=true"}
	} else {
		cmdBackground = []string{"./runcmd --cid=" + cmdId + " --args=\"" + strings.Join(configParser.GetCommandsList(yamlConfig), ";;") + "\"" +
			" --enableStreams=true"}
	}

	log.Print(fmt.Sprintf("Starting command '%s' in background", strings.Join(cmdBackground, " ")))
	u.KillCmdBackgroundProcess(cmdId)
	ch := make(chan error)
	go u.StartCommand(cmdId, cmdBackground, ch)
	err = <-ch
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.COMMAND_DETACHED_START_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.COMMAND_DETACHED_START_FAILURE)], cmdId),
			err.Error(),
			r.URL.Path))
		return
	}
	commandDescription := cmdId
	models.SetDescription(yamlConfig, commandDescription)
	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		models.GetDescription(),
		r.URL.Path)
	w.Header().Add("Content-Type", "application/json")
	w.WriteHeader(http.StatusAccepted)
	state.SetLastCommand(cmdId)

	u.ApiResponse(w, resp)
}
View Source
var CommandParallelPost = func(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	commands := u.TrimSpacesAndLineEndings(strings.Split(string(body), "\n"))
	if len(commands) == 0 {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.EMPTY_REQUEST_BODY_PROVIDED),
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			r.URL.Path))
		return
	}
	cip := command.NewCommandInParallel()
	commandDescription := cip.RunCommands(commands)
	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		commandDescription,
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var CommandPost = func(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	commands := u.TrimSpacesAndLineEndings(strings.Split(string(body), "\n"))

	if len(commands) == 0 {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.EMPTY_REQUEST_BODY_PROVIDED),
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			r.URL.Path))
		return
	}
	cim := command.NewCommandInMemory()
	commandDescription := cim.RunCommands(commands)
	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		commandDescription,
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var CommandPostYaml = func(w http.ResponseWriter, r *http.Request) {
	body, _ := ioutil.ReadAll(r.Body)
	commands := u.TrimSpacesAndLineEndings(strings.Split(string(body), "\n"))

	if len(commands) == 0 {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.EMPTY_REQUEST_BODY_PROVIDED),
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			r.URL.Path))
		return
	}

	var yamlConfig = models.NewYamlConfig()
	var configParser = u.NewYamlConfigParser()
	err := yaml.Unmarshal(body, &yamlConfig)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.INVALID_YAML_CONFIG),
			u.GetMessage()[uint32(constants.INVALID_YAML_CONFIG)],
			err.Error(),
			r.URL.Path))
		return
	}
	err = configParser.CheckConfig(yamlConfig)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.INVALID_YAML_CONFIG),
			u.GetMessage()[uint32(constants.INVALID_YAML_CONFIG)],
			err.Error(),
			r.URL.Path))
		return
	}
	envVars := yamlConfig.GetEnv()
	envVarsSet := environment.GetInstance().SetEnvVars(envVars)

	cim := command.NewCommandInMemory()
	commandDescription := cim.RunCommands(configParser.GetCommandsList(yamlConfig))
	yamlConfig.SetEnv(envVarsSet)
	models.SetDescription(yamlConfig, commandDescription)
	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		models.GetDescription(),
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var DeleteVirtualEnvVars = func(w http.ResponseWriter, r *http.Request) {
	env := environment.GetInstance()

	env.CleanVirtualEnv()

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		env.GetVirtualEnv(),
		r.URL.Path)
	u.ApiResponse(w, resp)
}
View Source
var GetEnvVar = func(w http.ResponseWriter, r *http.Request) {
	env := environment.GetInstance()
	params := mux.Vars(r)
	envVar := env.GetEnvAndVirtualEnv()[params["name"]]

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		envVar,
		r.URL.Path)
	u.ApiResponse(w, resp)
}
View Source
var GetEnvVars = func(w http.ResponseWriter, r *http.Request) {
	env := environment.GetInstance()
	envVars := env.GetEnvAndVirtualEnv()

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		envVars,
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var GetFile = func(w http.ResponseWriter, r *http.Request) {
	fileHeaderName := "File-Path"
	fileName := r.Header.Get(fileHeaderName)
	if fileName == "" {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.HTTP_HEADER_NOT_PROVIDED),
			fmt.Sprintf(u.GetMessage()[uint32(constants.HTTP_HEADER_NOT_PROVIDED)], fileHeaderName),
			fmt.Sprintf(u.GetMessage()[uint32(constants.HTTP_HEADER_NOT_PROVIDED)], fileHeaderName),
			r.URL.Path))
		return
	}

	content, err := ioutil.ReadFile(fileName)

	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.GET_FILE_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.GET_FILE_FAILURE)], fileName),
			err.Error(),
			r.URL.Path))
		return
	}
	w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(fileName))

	u.ApiResponseByteArray(w, content)
}
View Source
var GetFolder = func(w http.ResponseWriter, r *http.Request) {
	folderHeader := "Folder-Path"
	folderName := r.Header.Get(folderHeader)
	if folderName == "" {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.HTTP_HEADER_NOT_PROVIDED),
			fmt.Sprintf(u.GetMessage()[uint32(constants.HTTP_HEADER_NOT_PROVIDED)], folderHeader),
			fmt.Sprintf(u.GetMessage()[uint32(constants.HTTP_HEADER_NOT_PROVIDED)], folderHeader),
			r.URL.Path))
		return
	}
	if !u.IsFolder(folderName) {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.FOLDER_ZIP_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.FOLDER_ZIP_FAILURE)], folderName),
			fmt.Sprintf("Path %s is not a folder", folderName),
			r.URL.Path))
		return
	}
	zipFileName := "response.zip"
	err := zipFolder(folderName, zipFileName)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.FOLDER_ZIP_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.FOLDER_ZIP_FAILURE)], folderName),
			err.Error(),
			r.URL.Path))
		return
	}

	content, err := ioutil.ReadFile(zipFileName)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.GET_FILE_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.GET_FILE_FAILURE)], folderName),
			err.Error(),
			r.URL.Path))
		return
	}

	w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(zipFileName))

	u.ApiResponseZip(w, content)
}
View Source
var GetProcesses = func(w http.ResponseWriter, r *http.Request) {
	processes := u.GetAllProcesses()

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		processes,
		r.URL.Path)

	u.ApiResponse(w, resp)
}
View Source
var GetProcessesByExecName = func(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	processes := u.GetAllProcessesByExecName(params["name"])

	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		processes,
		r.URL.Path)
	u.ApiResponse(w, resp)
}
View Source
var Ping = func(w http.ResponseWriter, r *http.Request) {
	resp := u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		"pong",
		r.URL.Path)
	u.ApiResponse(w, resp)
}
View Source
var PutFile = func(w http.ResponseWriter, r *http.Request) {
	fileHeaderName := "File-Path"
	fileName := r.Header.Get(fileHeaderName)
	if fileName == "" {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.HTTP_HEADER_NOT_PROVIDED),
			fmt.Sprintf(u.GetMessage()[uint32(constants.HTTP_HEADER_NOT_PROVIDED)], fileHeaderName),
			fmt.Sprintf(u.GetMessage()[uint32(constants.HTTP_HEADER_NOT_PROVIDED)], fileHeaderName),
			r.URL.Path))
		return
	}

	body, _ := ioutil.ReadAll(r.Body)
	if len(body) == 0 {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.EMPTY_REQUEST_BODY_PROVIDED),
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			u.GetMessage()[uint32(constants.EMPTY_REQUEST_BODY_PROVIDED)],
			r.URL.Path))
		return
	}

	err := ioutil.WriteFile(fileName, body, 0644)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.UPLOAD_FILE_FAILURE),
			u.GetMessage()[uint32(constants.UPLOAD_FILE_FAILURE)],
			err.Error(),
			r.URL.Path))
		return
	}

	u.ApiResponse(w, u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		u.GetMessage()[uint32(constants.SUCCESS)],
		r.URL.Path))
}
View Source
var SetEnvVars = func(w http.ResponseWriter, r *http.Request) {
	env := environment.GetInstance()
	body, err := ioutil.ReadAll(r.Body)
	attemptedEnvVars := make(map[string]string)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.SET_ENV_VAR_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.SET_ENV_VAR_FAILURE)], string(body)),
			err.Error(),
			r.URL.Path))
		return
	}

	err = json.Unmarshal(body, &attemptedEnvVars)
	if err != nil {
		u.ApiResponseError(w, u.ApiMessage(uint32(constants.SET_ENV_VAR_FAILURE),
			fmt.Sprintf(u.GetMessage()[uint32(constants.SET_ENV_VAR_FAILURE)], string(body)),
			err.Error(),
			r.URL.Path))
		return
	}
	addedEnvVars := env.SetEnvVars(attemptedEnvVars)

	u.ApiResponse(w, u.ApiMessage(uint32(constants.SUCCESS),
		u.GetMessage()[uint32(constants.SUCCESS)],
		addedEnvVars,
		r.URL.Path))
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL