Skip to content

Commit

Permalink
stops ocis and runs ocis with ocis_Exclude_service env
Browse files Browse the repository at this point in the history
  • Loading branch information
amrita-shrestha committed Dec 18, 2024
1 parent 5b646bd commit 3225e7f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 23 deletions.
87 changes: 87 additions & 0 deletions tests/ociswrapper/ocis/ocis.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,90 @@ func RunCommand(command string, inputs []string) (int, string) {

return c.ProcessState.ExitCode(), cmdOutput
}

func runOcisService(service string) {
// wait for the log scanner to finish
var wg sync.WaitGroup
wg.Add(2)

stopSignal = false
if retryCount == 0 {
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)

logs, err := cmd.StderrPipe()
if err != nil {
log.Panic(err)
}
output, err := cmd.StdoutPipe()
if err != nil {
log.Panic(err)
}

err = cmd.Start()
if err != nil {
log.Panic(err)
}

logScanner := bufio.NewScanner(logs)
outputScanner := bufio.NewScanner(output)
outChan := make(chan string)

// Read the logs when the 'ocis server' command is running
go func() {
defer wg.Done()
for logScanner.Scan() {
outChan <- logScanner.Text()
}
}()

go func() {
defer wg.Done()
for outputScanner.Scan() {
outChan <- outputScanner.Text()
}
}()

// Fetch logs from the channel and print them
go func() {
for s := range outChan {
fmt.Println(s)
}
}()

if err := cmd.Wait(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
status := exitErr.Sys().(syscall.WaitStatus)
// retry only if oCIS server exited with code > 0
// -1 exit code means that the process was killed by a signal (syscall.SIGINT)
if status.ExitStatus() > 0 && !stopSignal {
waitUntilCompleteShutdown()

log.Println(fmt.Sprintf("oCIS service server exited with code %v", status.ExitStatus()))

// retry to start oCIS server
retryCount++
maxRetry, _ := strconv.Atoi(config.Get("retry"))
if retryCount <= maxRetry {
wg.Wait()
close(outChan)
log.Println(fmt.Sprintf("Retry starting oCIS server... (retry %v)", retryCount))
// wait 500 milliseconds before retrying
time.Sleep(500 * time.Millisecond)
runOcisService(service)
return
}
}
}
}
wg.Wait()
close(outChan)
}
40 changes: 18 additions & 22 deletions tests/ociswrapper/wrapper/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,34 +204,30 @@ func CommandHandler(res http.ResponseWriter, req *http.Request) {
}

func OcisServiceHandler(res http.ResponseWriter, req *http.Request) {

if req.Method != http.MethodPost || req.Method != http.MethodDelete {
sendResponse(res, http.StatusMethodNotAllowed, "")
return
}

serviceName := strings.TrimPrefix(req.URL.Path, "/services/")

if serviceName == "" {
sendResponse(res, http.StatusUnprocessableEntity, "Service name is required")
if req.Method != http.MethodPost && req.Method != http.MethodDelete {
sendResponse(res, http.StatusMethodNotAllowed, "Method not allowed")
return
}

// add script to check whether ocis runs without that service or not
if !ocis.IsOcisRunning() {
sendResponse(res, http.StatusPreconditionFailed, "oCIS server is not running")
envBody, err := parseJsonBody(req.Body)
if err != nil {
sendResponse(res, http.StatusMethodNotAllowed, "Invalid json body")
return
}
}

common.Wg.Add(1)
// start ocis service with env to set env false
go ocis.Start(nil)
var envMap []string
for key, value := range envBody {
envMap = append(envMap, fmt.Sprintf("%s=%v", key, value))
}
ocis.EnvConfigs = append(ocis.EnvConfigs, envMap...)

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

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 3225e7f

Please sign in to comment.