func putCredential(request *restful.Request, response *restful.Response) { ip := request.PathParameter("ip") credential := host.Credential{} err := request.ReadEntity(&credential) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Read body failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["ip"] = ip errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } if ip != credential.IP { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Path parameter ip is different from ip in the body" jsonMap["path"] = ip jsonMap["body"] = credential.IP errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(400, string(errorMessageByteSlice)) return } oldCredential, _ := host.GetStorage().LoadCredential(credential.IP) if oldCredential == nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "The credential to update doesn't exist" jsonMap["ip"] = credential.IP errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(404, string(errorMessageByteSlice)) return } err = host.GetStorage().SaveCredential(&credential) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Save credential failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["credential"] = credential errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } }
func RemoveImageFromAllHost(imageRecordSlcie []ImageRecord) error { credentialSlice, err := host.GetStorage().LoadAllCredential() if err != nil { log.Error(err) return err } amount := len(imageRecordSlcie) commandSlice := make([]string, 0) // Delete all stopped instance so image could be removed commandSlice = append(commandSlice, "sudo docker rm $(sudo docker ps -aqf status=exited | xargs)\n") for _, imageRecord := range imageRecordSlcie { commandSlice = append(commandSlice, "sudo docker rmi "+imageRecord.Path+"\n") } hasError := false buffer := bytes.Buffer{} for _, credential := range credentialSlice { if credential.Disabled == false { interactiveMap := make(map[string]string) interactiveMap["[sudo]"] = credential.SSH.Password + "\n" resultSlice, err := sshclient.InteractiveSSH( 2*time.Second, time.Duration(amount)*time.Minute, credential.IP, credential.SSH.Port, credential.SSH.User, credential.SSH.Password, commandSlice, interactiveMap) log.Info("Issue command via ssh with result:\n %v", resultSlice) if err != nil { hasError = true errorMessage := fmt.Sprintf("Error message: %v Result Output: %v .", err, resultSlice) log.Error(errorMessage) buffer.WriteString(errorMessage) } } } if hasError { return errors.New(buffer.String()) } else { return nil } }
func getAllCredential(request *restful.Request, response *restful.Response) { credentialClusterSlice, err := host.GetStorage().LoadAllCredential() if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Get all credential failure" jsonMap["ErrorMessage"] = err.Error() errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } response.WriteJson(credentialClusterSlice, "[]Credential") }
func postCredential(request *restful.Request, response *restful.Response) { credential := host.Credential{} err := request.ReadEntity(&credential) 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 } oldCredential, _ := host.GetStorage().LoadCredential(credential.IP) if oldCredential != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "The credential to create already exists" jsonMap["ip"] = credential.IP errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(409, string(errorMessageByteSlice)) return } err = host.GetStorage().SaveCredential(&credential) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Save credential failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["credential"] = credential errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } }
func deleteCredential(request *restful.Request, response *restful.Response) { ip := request.PathParameter("ip") err := host.GetStorage().DeleteCredential(ip) if err != nil { jsonMap := make(map[string]interface{}) jsonMap["Error"] = "Delete credential failure" jsonMap["ErrorMessage"] = err.Error() jsonMap["ip"] = ip errorMessageByteSlice, _ := json.Marshal(jsonMap) log.Error(jsonMap) response.WriteErrorString(422, string(errorMessageByteSlice)) return } }