Skip to content

Commit

Permalink
works good but polishing remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
amrita-shrestha committed Dec 18, 2024
1 parent 3225e7f commit 74de42a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
43 changes: 32 additions & 11 deletions tests/ociswrapper/ocis/ocis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var cmd *exec.Cmd
var retryCount = 0
var stopSignal = false
var EnvConfigs = []string{}
var runningCommands = make(map[string]int) // Maps unique IDs to PIDs

func Start(envMap []string) {
// wait for the log scanner to finish
Expand Down Expand Up @@ -272,7 +273,7 @@ func RunCommand(command string, inputs []string) (int, string) {
return c.ProcessState.ExitCode(), cmdOutput
}

func runOcisService(service string) {
func RunOcisService(service string) {
// wait for the log scanner to finish
var wg sync.WaitGroup
wg.Add(2)
Expand All @@ -282,13 +283,7 @@ func runOcisService(service string) {
defer common.Wg.Done()
}

cmd = exec.Command(config.Get("bin"), service , "server")
// output, err := cmd.CombinedOutput()
// if err != nil {
// fmt.Printf("Error running service %s: %v\nOutput: %s\n", service, err, output)
// return
// }
// fmt.Printf("Service %s started successfully: %s\n", service, output)
cmd = exec.Command(config.Get("bin"), service, "server")

logs, err := cmd.StderrPipe()
if err != nil {
Expand All @@ -307,7 +302,7 @@ func runOcisService(service string) {
logScanner := bufio.NewScanner(logs)
outputScanner := bufio.NewScanner(output)
outChan := make(chan string)

runningCommands[service] = cmd.Process.Pid
// Read the logs when the 'ocis server' command is running
go func() {
defer wg.Done()
Expand Down Expand Up @@ -349,12 +344,38 @@ func runOcisService(service string) {
log.Println(fmt.Sprintf("Retry starting oCIS server... (retry %v)", retryCount))
// wait 500 milliseconds before retrying
time.Sleep(500 * time.Millisecond)
runOcisService(service)
return
RunOcisService(service)
}
}
}
}
wg.Wait()
close(outChan)
}

// Stop oCIS service or a specific service by its unique identifier
func StopService(service string) (bool, string) {
pid, exists := runningCommands[service]
if !exists {
return false, fmt.Sprintf("Service %s is not running", service)
}

// Find the process by PID and send SIGINT to stop it
process, err := os.FindProcess(pid)
if err != nil {
log.Println(fmt.Sprintf("Failed to find process: %v", err))
return false, fmt.Sprintf("Failed to find process with ID %d", pid)
}

// Send SIGINT to stop the command
err = process.Signal(syscall.SIGINT)
if err != nil {
log.Println(fmt.Sprintf("Failed to send signal: %v", err))
return false, fmt.Sprintf("Failed to stop service with ID %s", service)
}

// Wait for the service to stop
process.Wait()
delete(runningCommands, service)
return true, fmt.Sprintf("Service %s stopped successfully", service)
}
27 changes: 18 additions & 9 deletions tests/ociswrapper/wrapper/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"ociswrapper/common"
"ociswrapper/ocis"
"os"
"strings"
)

type BasicResponse struct {
Expand Down Expand Up @@ -209,25 +210,33 @@ func OcisServiceHandler(res http.ResponseWriter, req *http.Request) {
return
}

envBody, err := parseJsonBody(req.Body)
if err != nil {
sendResponse(res, http.StatusMethodNotAllowed, "Invalid json body")
return
}
serviceName := strings.TrimPrefix(req.URL.Path, "/services/")

var envMap []string
for key, value := range envBody {
envMap = append(envMap, fmt.Sprintf("%s=%v", key, value))
if serviceName == "" {
sendResponse(res, http.StatusUnprocessableEntity, "Service name is required")
return
}
ocis.EnvConfigs = append(ocis.EnvConfigs, envMap...)
envVar := fmt.Sprintf("OCIS_EXCLUDE_RUN_SERVICES=%s", serviceName)
ocis.EnvConfigs = append(ocis.EnvConfigs, envVar)

if req.Method == http.MethodPost {
success, _ := ocis.Restart(ocis.EnvConfigs)
if success {
sendResponse(res, http.StatusOK, "oCIS configured successfully")
ocis.RunOcisService(serviceName)
return
}
sendResponse(res, http.StatusInternalServerError, "Failed to restart oCIS with new configuration")
}

if req.Method == http.MethodDelete {
// Attempt to stop the service
success, message := ocis.StopService(serviceName)
if success {
sendResponse(res, http.StatusOK, fmt.Sprintf("Service %s stopped successfully", serviceName))
} else {
sendResponse(res, http.StatusInternalServerError, message)
}
}

}
2 changes: 1 addition & 1 deletion tests/ociswrapper/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Start(port string) {
mux.HandleFunc("/command", handlers.CommandHandler)
mux.HandleFunc("/stop", handlers.StopOcisHandler)
mux.HandleFunc("/start", handlers.StartOcisHandler)
mux.HandleFunc("/services", handlers.OcisServiceHandler)
mux.HandleFunc("/services/", handlers.OcisServiceHandler)

httpServer.Handler = mux

Expand Down

0 comments on commit 74de42a

Please sign in to comment.