Skip to content

Commit

Permalink
resove conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
amrita-shrestha committed Jan 7, 2025
1 parent 78ebcae commit 5f509bd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@env-config @backup-consistency
@env-config
Feature: backup consistency
As a user
I want to check my data for inconsistencies
Expand Down
109 changes: 45 additions & 64 deletions tests/ociswrapper/ocis/ocis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"fmt"
"io"
"net/http"
"ociswrapper/common"
"ociswrapper/log"
"ociswrapper/ocis/config"
"os"
"os/exec"
"strconv"
Expand All @@ -18,48 +15,56 @@ import (
"syscall"
"time"

"ociswrapper/common"
"ociswrapper/log"
"ociswrapper/ocis/config"

"github.com/creack/pty"
)

var cmd *exec.Cmd
var retryCount = 0
var stopSignal = false
var EnvConfigs = []string{}
var runningCommands = make(map[string]int) // Maps unique IDs to PIDs
var runningServices = make(map[string]int)

func Start(envMap []string) {
StartService("", envMap)
StartService("", envMap)
}

func Stop() (bool, string) {
log.Println(fmt.Sprintf("Stop ocis check cmd %s\n", cmd))
log.Println("Stopping oCIS server...")
stopSignal = true

for listservice, pid := range runningCommands {
log.Println(fmt.Sprintf("Services running before terminating: %s with process and id: %v\n", listservice, pid))
process, err := os.FindProcess(pid)
err = process.Signal(syscall.SIGINT)
if err != nil {
if !strings.HasSuffix(err.Error(), "process already finished") {
log.Fatalln(err)
} else {
return true, "oCIS server is already stopped"
}
}
process.Wait()
for listservice, pid := range runningServices {
process, err := os.FindProcess(pid)
if err != nil {
log.Println(fmt.Sprintf("Error finding process for service %s: %v", listservice, err))
continue
}
pKillError := process.Signal(syscall.SIGINT)
if pKillError != nil {
if !strings.HasSuffix(pKillError.Error(), "process already finished") {
log.Fatalln(pKillError)
} else {
return true, "oCIS server is already stopped"
}
}
_, waitErr := process.Wait()
if waitErr != nil {
log.Println(fmt.Sprintf("Error waiting for process to exit: %v\n", waitErr))
}
delete(runningServices, "ocis")
}

cmd = nil
success, message := waitUntilCompleteShutdown()

cmd = nil
return success, message
}

func Restart(envMap []string) (bool, string) {
log.Println(fmt.Sprintf("Restarting ocis check cmd %s\n", cmd))
log.Println(fmt.Sprintf("Restaring ocis with rollback os environ %s\n", envMap))
log.Println(fmt.Sprintf("OS environment: %s\n", os.Environ()))
go Stop()
Stop()

log.Println("Restarting oCIS server...")
common.Wg.Add(1)
Expand All @@ -69,8 +74,8 @@ func Restart(envMap []string) (bool, string) {
}

func IsOcisRunning() bool {
if runningCommands["ocis"] > 0 {
return true
if runningServices["ocis"] > 0 {
return true
}
return false
}
Expand Down Expand Up @@ -136,7 +141,6 @@ func WaitForConnection() (bool, string) {
}

func waitUntilCompleteShutdown() (bool, string) {
log.Println("Process found. Waiting... waitUntilCompleteShutdown")
timeout := 30 * time.Second
startTime := time.Now()

Expand Down Expand Up @@ -207,11 +211,9 @@ func StartService(service string, envMap []string) {
cmdArgs := []string{"server"} // Default command args

if service != "" {
// Directly append service if provided
cmdArgs = append([]string{service}, cmdArgs...)
}

// wait for the log scanner to finish
var wg sync.WaitGroup
wg.Add(2)

Expand All @@ -220,18 +222,12 @@ func StartService(service string, envMap []string) {
defer common.Wg.Done()
}

// Initialize the command
cmd = exec.Command(config.Get("bin"), cmdArgs...)

// Use the provided envMap if not empty, otherwise use EnvConfigs
if len(envMap) == 0 {
cmd.Env = append(os.Environ(), EnvConfigs...)
log.Println(fmt.Sprintf("OS environment variables while running ocis service: %s\n", cmd.Env))
log.Println(fmt.Sprintf("OS environment: %s\n", os.Environ()))
} else {
cmd.Env = append(os.Environ(), envMap...)
log.Println(fmt.Sprintf("OS environment variables while running ocis service: %s\n", cmd.Env))
log.Println(fmt.Sprintf("OS environment: %s\n", os.Environ()))
}

logs, err := cmd.StderrPipe()
Expand All @@ -243,9 +239,6 @@ func StartService(service string, envMap []string) {
log.Panic(err)
}

// log.Println(fmt.Sprintf("command env used to start service %s\n", cmd.Env))
// log.Println(fmt.Sprintf("command used to start service %s\n", cmd))

err = cmd.Start()

if err != nil {
Expand All @@ -256,17 +249,14 @@ func StartService(service string, envMap []string) {
outputScanner := bufio.NewScanner(output)
outChan := make(chan string)

// If service is an empty string, set the PID for "ocis"
if service == "" {
runningCommands["ocis"] = cmd.Process.Pid
runningServices["ocis"] = cmd.Process.Pid
} else {
runningCommands[service] = cmd.Process.Pid
runningServices[service] = cmd.Process.Pid
}

log.Println("Started oCIS processes:")
for listservice, pid := range runningCommands {
for listservice, pid := range runningServices {
log.Println(fmt.Sprintf("Service started: %s with process and id: %v\n", listservice, pid))

}

// Read the logs when the 'ocis server' command is running
Expand Down Expand Up @@ -316,45 +306,36 @@ func StartService(service string, envMap []string) {
}
}
}

log.Println(fmt.Sprintf(" ---- ocis start service ending line---- %s\n", cmd))
wg.Wait()
close(outChan)
}

// Stop oCIS service or a specific service by its unique identifier
func StopService(service string) (bool, string) {
for listservice, pid := range runningCommands {
log.Println(fmt.Sprintf("Services running before terminating: %s with process and id: %v\n", listservice, pid))
}

pid, exists := runningCommands[service]
log.Println(fmt.Sprintf("Services process id to terminate: %s\n", pid))
pid, exists := runningServices[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)
log.Println(fmt.Sprintf("Found process to terminate in os: %s\n", 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)
}

err = process.Signal(syscall.SIGINT)
log.Println("Process terminated using signal")
if err != nil {
log.Println(fmt.Sprintf("Failed to send signal: %v", err))
pterr := process.Signal(syscall.SIGINT)
if pterr != nil {
return false, fmt.Sprintf("Failed to stop service with PID %d", pid)
}

time.Sleep(30 * time.Second)
process.Wait()
log.Println("Process terminating process.wait")
delete(runningCommands, service)
for listservice, pid := range runningCommands {
log.Println(fmt.Sprintf("Service list after deleteing %s service. list contain service: %s with process and id: %v\n", service, listservice, pid))
}
_, waitErr := process.Wait()
if waitErr != nil {
log.Println(fmt.Sprintf("Error waiting for process to exit: %v\n", waitErr))
}

log.Println(fmt.Sprintf("Service %s process %v terminated", service, pid))
delete(runningServices, service)

return true, fmt.Sprintf("Service %s stopped successfully", service)
}
2 changes: 1 addition & 1 deletion tests/ociswrapper/wrapper/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func RollbackHandler(res http.ResponseWriter, req *http.Request) {

var message string
ocis.EnvConfigs = []string{}
log.Printf(fmt.Sprintf("os Environ when rollback %s", os.Environ))

success, _ := ocis.Restart(ocis.EnvConfigs)
if success {
message = "oCIS configuration rolled back successfully"
Expand Down

0 comments on commit 5f509bd

Please sign in to comment.