Skip to content

Commit

Permalink
Merge pull request #5 from openhie/PLAT-232-logging
Browse files Browse the repository at this point in the history
PLAT-232 Added support for a log path bind mount
  • Loading branch information
tumbledwyer authored Jun 30, 2022
2 parents cbc4ae1 + ba92507 commit 9b6468f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 49 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ typings/
/docs/.docusaurus
/docs/.cache-loader

# vscode config
.vscode

# test binaries
/cli/features/test-platform-linux
/cli/features/test-platform-macos
Expand Down
5 changes: 4 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"recommendations": ["esbenp.prettier-vscode"]
"recommendations": [
"esbenp.prettier-vscode",
"golang.go"
]
}
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
},
"files.eol": "\n",
"files.insertFinalNewline": true,
"editor.formatOnSave": true
"editor.formatOnSave": true,
"[go]": {
"editor.defaultFormatter": "golang.go"
}
}
9 changes: 5 additions & 4 deletions cli/buildreleases.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
# This script builds Linux, Windows and MacOS binaries into the cli/bin directory
mkdir -p bin
GOOS=darwin GOARCH=amd64 go build && mv ./cli ./bin/gocli-macos \
&& GOOS=linux GOARCH=amd64 go build && mv ./cli ./bin/gocli-linux \
&& GOOS=windows GOARCH=amd64 go build && mv ./cli.exe ./bin/gocli.exe\
&& go clean
GOOS=darwin GOARCH=amd64 go build && mv ./cli ./bin/gocli-macos &&
GOOS=linux GOARCH=amd64 go build && mv ./cli ./bin/gocli-linux &&
GOOS=windows GOARCH=amd64 go build && mv ./cli.exe ./bin/gocli.exe && go clean
82 changes: 50 additions & 32 deletions cli/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ var (
MountCustomPackage = mountCustomPackage
)

type commandsOptions struct {
environmentVariables []string
deployCommand string
otherFlags []string
packages []string
customPackagePaths []string
instantVersion string
targetLauncher string
}

func debugDocker() error {
fmt.Printf("...checking your Docker setup")

Expand Down Expand Up @@ -99,57 +109,59 @@ func sliceContains(slice []string, element string) bool {
return false
}

func extractCommands(startupCommands []string) (environmentVariables []string, deployCommand string, otherFlags []string, packages []string, customPackagePaths []string, instantVersion string, targetLauncher string) {
instantVersion = "latest"
func extractCommands(startupCommands []string) commandsOptions {
commandOptions := commandsOptions{
instantVersion: "latest",
}

for _, option := range startupCommands {
switch {
case sliceContains([]string{"init", "up", "down", "destroy"}, option):
deployCommand = option
commandOptions.deployCommand = option
case strings.HasPrefix(option, "-c=") || strings.HasPrefix(option, "--custom-package="):
customPackagePaths = append(customPackagePaths, option)
commandOptions.customPackagePaths = append(commandOptions.customPackagePaths, option)
case strings.HasPrefix(option, "-e=") || strings.HasPrefix(option, "--env-file="):
environmentVariables = append(environmentVariables, option)
commandOptions.environmentVariables = append(commandOptions.environmentVariables, option)
case strings.HasPrefix(option, "--instant-version="):
instantVersion = strings.Split(option, "--instant-version=")[1]
commandOptions.instantVersion = strings.Split(option, "--instant-version=")[1]
case strings.HasPrefix(option, "-t="):
targetLauncher = strings.Split(option, "-t=")[1]
commandOptions.targetLauncher = strings.Split(option, "-t=")[1]
case strings.HasPrefix(option, "-") || strings.HasPrefix(option, "--"):
otherFlags = append(otherFlags, option)
commandOptions.otherFlags = append(commandOptions.otherFlags, option)
default:
packages = append(packages, option)
commandOptions.packages = append(commandOptions.packages, option)
}
}

if len(customPackagePaths) > 0 {
customPackagePaths = getPackagePaths(customPackagePaths, []string{"-c=", "--custom-package="})
if len(commandOptions.customPackagePaths) > 0 {
commandOptions.customPackagePaths = getPackagePaths(commandOptions.customPackagePaths, []string{"-c=", "--custom-package="})
}

if len(environmentVariables) > 0 {
environmentVariables = getEnvironmentVariables(environmentVariables, []string{"-e=", "--env-file="})
if len(commandOptions.environmentVariables) > 0 {
commandOptions.environmentVariables = getEnvironmentVariables(commandOptions.environmentVariables, []string{"-e=", "--env-file="})
}

if targetLauncher == "" {
targetLauncher = customOptions.targetLauncher
if commandOptions.targetLauncher == "" {
commandOptions.targetLauncher = customOptions.targetLauncher
}

return
return commandOptions
}

func RunDeployCommand(startupCommands []string) error {
fmt.Println("Note: Initial setup takes 1-5 minutes.\nWait for the DONE message.\n--------------------------")

environmentVariables, deployCommand, otherFlags, packages, customPackagePaths, instantVersion, targetLauncher := extractCommands(startupCommands)
commandOptions := extractCommands(startupCommands)

fmt.Println("Action:", deployCommand)
fmt.Println("Package IDs:", packages)
fmt.Println("Custom package paths:", customPackagePaths)
fmt.Println("Environment Variables:", environmentVariables)
fmt.Println("Other Flags:", otherFlags)
fmt.Println("InstantVersion:", instantVersion)
fmt.Println("Target Launcher:", targetLauncher)
fmt.Println("Action:", commandOptions.deployCommand)
fmt.Println("Package IDs:", commandOptions.packages)
fmt.Println("Custom package paths:", commandOptions.customPackagePaths)
fmt.Println("Environment Variables:", commandOptions.environmentVariables)
fmt.Println("Other Flags:", commandOptions.otherFlags)
fmt.Println("InstantVersion:", commandOptions.instantVersion)
fmt.Println("Target Launcher:", commandOptions.targetLauncher)

instantImage := cfg.Image + ":" + instantVersion
instantImage := cfg.Image + ":" + commandOptions.instantVersion

fmt.Println("Creating fresh instant container with volumes...")
commandSlice := []string{
Expand All @@ -160,19 +172,25 @@ func RunDeployCommand(startupCommands []string) error {
"-v", "/var/run/docker.sock:/var/run/docker.sock",
"--network", "host",
}
commandSlice = append(commandSlice, environmentVariables...)
commandSlice = append(commandSlice, []string{instantImage, deployCommand}...)
commandSlice = append(commandSlice, otherFlags...)
commandSlice = append(commandSlice, []string{"-t", targetLauncher}...)
commandSlice = append(commandSlice, packages...)

if cfg.LogPath != "" {
commandSlice = append(commandSlice, fmt.Sprintf("--mount=type=bind,src=%s,dst=/tmp/logs", cfg.LogPath))
}

commandSlice = append(commandSlice, commandOptions.environmentVariables...)
commandSlice = append(commandSlice, []string{instantImage, commandOptions.deployCommand}...)
commandSlice = append(commandSlice, commandOptions.otherFlags...)
commandSlice = append(commandSlice, []string{"-t", commandOptions.targetLauncher}...)
commandSlice = append(commandSlice, commandOptions.packages...)

_, err := RunCommand("docker", nil, commandSlice...)
if err != nil {
return err
}

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

for _, c := range customPackagePaths {
for _, c := range commandOptions.customPackagePaths {
fmt.Print("- " + c)
err = MountCustomPackage(c)
if err != nil {
Expand All @@ -189,7 +207,7 @@ func RunDeployCommand(startupCommands []string) error {
return nil
}

if deployCommand == "destroy" {
if commandOptions.deployCommand == "destroy" {
fmt.Println("Delete instant volume...")
commandSlice := []string{"volume", "rm", "instant"}
_, err = RunCommand("docker", nil, commandSlice...)
Expand Down
17 changes: 9 additions & 8 deletions cli/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func Test_extractCommands(t *testing.T) {
packages []string
customPackagePaths []string
instantVersion string
logPath string
}

testCases := []struct {
Expand Down Expand Up @@ -208,27 +209,27 @@ func Test_extractCommands(t *testing.T) {

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
environmentVariables, deployCommand, otherFlags, packages, customPackagePaths, instantVersion, targetLauncher := extractCommands(tt.startupCommands)
commandOptions := extractCommands(tt.startupCommands)

if !assert.Equal(t, tt.expectedResults.environmentVariables, environmentVariables) {
if !assert.Equal(t, tt.expectedResults.environmentVariables, commandOptions.environmentVariables) {
t.Fatal("ExtractCommands should return the correct environment variables")
}
if !assert.Equal(t, tt.expectedResults.deployCommand, deployCommand) {
if !assert.Equal(t, tt.expectedResults.deployCommand, commandOptions.deployCommand) {
t.Fatal("ExtractCommands should return the correct deploy command")
}
if !assert.Equal(t, tt.expectedResults.otherFlags, otherFlags) {
if !assert.Equal(t, tt.expectedResults.otherFlags, commandOptions.otherFlags) {
t.Fatal("ExtractCommands should return the correct 'otherFlags'")
}
if !assert.Equal(t, tt.expectedResults.targetLauncher, targetLauncher) {
if !assert.Equal(t, tt.expectedResults.targetLauncher, commandOptions.targetLauncher) {
t.Fatal("ExtractCommands should return the correct targetLauncher")
}
if !assert.Equal(t, tt.expectedResults.packages, packages) {
if !assert.Equal(t, tt.expectedResults.packages, commandOptions.packages) {
t.Fatal("ExtractCommands should return the correct packages")
}
if !assert.Equal(t, tt.expectedResults.customPackagePaths, customPackagePaths) {
if !assert.Equal(t, tt.expectedResults.customPackagePaths, commandOptions.customPackagePaths) {
t.Fatal("ExtractCommands should return the correct custom package paths")
}
if !assert.Equal(t, tt.expectedResults.instantVersion, instantVersion) {
if !assert.Equal(t, tt.expectedResults.instantVersion, commandOptions.instantVersion) {
t.Fatal("ExtractCommands should return the correct instant version")
}
t.Log(tt.name + " passed!")
Expand Down
1 change: 1 addition & 0 deletions cli/goinstant.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
DisableKubernetes bool `yaml:"disableKubernetes"`
DisableIG bool `yaml:"disableIG"`
DisableCustomTargetSelection bool `yaml:"disableCustomTargetSelection"`
LogPath string `yaml:"logPath"`
}

type customOption struct {
Expand Down

0 comments on commit 9b6468f

Please sign in to comment.