Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #166 from openhie/DIO-141-stop-instant-with-CLI
Browse files Browse the repository at this point in the history
Dio 141 stop instant with cli
  • Loading branch information
bradsawadye authored Dec 17, 2021
2 parents be4c2c1 + 4634e6c commit 87fd0cd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 25 deletions.
2 changes: 1 addition & 1 deletion goinstant/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func CLI() {
switch startupCommands[0] {
case "docker":
if len(startupCommands) < 3 {
panic("Incorrect arguments list passed to CLI. Requires at least 3 arguments when in non-interactive mode.")
gracefulPanic(nil, "Incorrect arguments list passed to CLI. Requires at least 3 arguments when in non-interactive mode.")
}

RunDirectDockerCommand(startupCommands)
Expand Down
55 changes: 32 additions & 23 deletions goinstant/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ func debugDocker() {
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Can't get current working directory... this is not a great error.")
// panic(err)
// gracefulPanic(err,"")
} else {
fmt.Println(cwd)
}

cli, err := client.NewClientWithOpts()
if err != nil {
panic(err)
gracefulPanic(err, "")
}

info, err := cli.Info(context.Background())
if err != nil {
fmt.Println("Unable to get Docker context. Please ensure that Docker is downloaded and running")
panic(err)
gracefulPanic(err, "")
} else {
// Docker default is 2GB, which may need to be revisited if Instant grows.
str1 := "bytes memory is allocated\n"
Expand Down Expand Up @@ -135,7 +135,7 @@ func RunDirectDockerCommand(startupCommands []string) {
if deployCommand == "init" {
fmt.Println("\n\nDelete a pre-existing instant volume...")
commandSlice := []string{"volume", "rm", "instant"}
runCommand(deployEnvironment, commandSlice...)
runCommand(deployEnvironment, nil, commandSlice...)
}

fmt.Println("Creating fresh instant container with volumes...")
Expand All @@ -152,7 +152,7 @@ func RunDirectDockerCommand(startupCommands []string) {
commandSlice = append(commandSlice, otherFlags...)
commandSlice = append(commandSlice, []string{"-t", deployEnvironment}...)
commandSlice = append(commandSlice, packages...)
runCommand(deployEnvironment, commandSlice...)
runCommand(deployEnvironment, nil, commandSlice...)

fmt.Println("Adding 3rd party packages to instant volume:")

Expand All @@ -163,22 +163,25 @@ func RunDirectDockerCommand(startupCommands []string) {

fmt.Println("\nRun Instant OpenHIE Installer Container")
commandSlice = []string{"start", "-a", "instant-openhie"}
runCommand(deployEnvironment, commandSlice...)
runCommand(deployEnvironment, nil, commandSlice...)

if deployCommand == "destroy" {
fmt.Println("Delete instant volume...")
commandSlice := []string{"volume", "rm", "instant"}
runCommand(deployEnvironment, commandSlice...)
runCommand(deployEnvironment, nil, commandSlice...)
}
}

func runCommand(commandName string, commandSlice ...string) (pathToPackage string) {
func runCommand(commandName string, suppressErrors []string, commandSlice ...string) (pathToPackage string) {
cmd := exec.Command(commandName, commandSlice...)
cmdReader, err := cmd.StdoutPipe()
var stderr bytes.Buffer
cmd.Stderr = &stderr

if err != nil {
if suppressErrors != nil && sliceContains(suppressErrors, strings.TrimSpace(stderr.String())) {
return
}
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
return
}
Expand All @@ -191,11 +194,17 @@ func runCommand(commandName string, commandSlice ...string) (pathToPackage strin
}()

if err := cmd.Start(); err != nil {
if suppressErrors != nil && sliceContains(suppressErrors, strings.TrimSpace(stderr.String())) {
return
}
fmt.Fprintln(os.Stderr, "Error starting Cmd.", stderr.String(), err)
return
}

if err := cmd.Wait(); err != nil {
if suppressErrors != nil && sliceContains(suppressErrors, strings.TrimSpace(stderr.String())) {
return
}
fmt.Fprintln(os.Stderr, "Error waiting for Cmd.", stderr.String(), err)
return
}
Expand Down Expand Up @@ -223,17 +232,17 @@ func mountCustomPackage(deployEnvironment string, pathToPackage string) {
tarRegex := regexp.MustCompile(`\.tar`)

if gitRegex.MatchString(pathToPackage) {
pathToPackage = runCommand("git", []string{"clone", pathToPackage}...)
pathToPackage = runCommand("git", nil, []string{"clone", pathToPackage}...)
} else if httpRegex.MatchString(pathToPackage) {
resp, err := http.Get(pathToPackage)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in dowloading custom package", err)
panic(err)
gracefulPanic(err, "")
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
panic("Error in dowloading custom package - HTTP status code: " + strconv.Itoa(resp.StatusCode))
gracefulPanic(nil, "Error in dowloading custom package - HTTP status code: "+strconv.Itoa(resp.StatusCode))
}

if zipRegex.MatchString(pathToPackage) {
Expand All @@ -244,21 +253,21 @@ func mountCustomPackage(deployEnvironment string, pathToPackage string) {
}

commandSlice := []string{"cp", pathToPackage, "instant-openhie:instant/"}
runCommand(deployEnvironment, commandSlice...)
runCommand(deployEnvironment, nil, commandSlice...)
}

func createZipFile(file string, content io.Reader) {
output, err := os.Create(file)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in creating zip file:")
panic(err)
gracefulPanic(err, "")
}
defer output.Close()

_, err = io.Copy(output, content)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in copying zip file content:")
panic(err)
gracefulPanic(err, "")
}
}

Expand All @@ -270,7 +279,7 @@ func unzipPackage(zipContent io.ReadCloser) (pathToPackage string) {
archive, err := zip.OpenReader(tempZipFile)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in unzipping file:")
panic(err)
gracefulPanic(err, "")
}

packageName := ""
Expand All @@ -288,18 +297,18 @@ func unzipPackage(zipContent io.ReadCloser) (pathToPackage string) {
content, err := file.Open()
if err != nil {
fmt.Fprintln(os.Stderr, "Error in unzipping file:")
panic(err)
gracefulPanic(err, "")
}

dest, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
fmt.Fprintln(os.Stderr, "Error in unzipping file:")
panic(err)
gracefulPanic(err, "")
}
_, err = io.Copy(dest, content)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in copying unzipped files:")
panic(err)
gracefulPanic(err, "")
}
content.Close()
}
Expand All @@ -310,7 +319,7 @@ func unzipPackage(zipContent io.ReadCloser) (pathToPackage string) {
err = os.Remove(tempFilePath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in deleting temp.zip file:")
panic(err)
gracefulPanic(err, "")
}

pathToPackage = filepath.Join(".", packageName)
Expand All @@ -322,7 +331,7 @@ func untarPackage(tarContent io.ReadCloser) (pathToPackage string) {
gzipReader, err := gzip.NewReader(tarContent)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in extracting tar file:")
panic(err)
gracefulPanic(err, "")
}
defer gzipReader.Close()

Expand All @@ -338,7 +347,7 @@ func untarPackage(tarContent io.ReadCloser) (pathToPackage string) {
}
if err != nil {
fmt.Fprintln(os.Stderr, "Error in extracting tar file:")
panic(err)
gracefulPanic(err, "")
}

filePath := filepath.Join(".", file.Name)
Expand All @@ -353,11 +362,11 @@ func untarPackage(tarContent io.ReadCloser) (pathToPackage string) {
dest, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
fmt.Fprintln(os.Stderr, "Error in untaring file:")
panic(err)
gracefulPanic(err, "")
}
if _, err := io.Copy(dest, tarReader); err != nil {
fmt.Fprintln(os.Stderr, "Error in extracting tar file:")
panic(err)
gracefulPanic(err, "")
}
}
pathToPackage = filepath.Join(".", packageName)
Expand Down
15 changes: 15 additions & 0 deletions goinstant/goinstant.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ var customOptions = customOption{
instantVersion: "latest",
}

func stopContainer() {
commandSlice := []string{"stop", "instant-openhie"}
suppressErrors := []string{"Error response from daemon: No such container: instant-openhie"}
runCommand("docker", suppressErrors, commandSlice...)
}

//Gracefully shut down the instant container and then kill the go cli with the panic error or message passed.
func gracefulPanic(err error, message string) {
stopContainer()
if message != "" {
panic(message)
}
panic(err)
}

func main() {
data, _ := f.ReadFile("banner.txt")
color.Green(string(data))
Expand Down
2 changes: 1 addition & 1 deletion goinstant/ig.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func loadIGpackage(url_entry string, fhir_server string, params *Params) {
var msg IndexJSON
err := json.Unmarshal(bs, &msg)
if err != nil {
panic(err)
gracefulPanic(err, "")
}

// Order mostly from: https://github.com/nmdp-bioinformatics/igloader/blob/main/igloader/igloader.py#L33
Expand Down
1 change: 1 addition & 0 deletions goinstant/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func quit() {
stopContainer()
os.Exit(0)
}

Expand Down

0 comments on commit 87fd0cd

Please sign in to comment.