-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds the ability to run popper check in multiple environments. Docker and the host are supported as of now but this can be extended. fixes #167
- Loading branch information
Showing
4 changed files
with
171 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
sh "github.com/codeskyblue/go-sh" | ||
"github.com/spf13/cobra" | ||
"github.com/theherk/viper" | ||
) | ||
|
||
var environment []string | ||
var volumes []string | ||
var volume []string | ||
var skip string | ||
var timeout string | ||
|
||
func runOnHost(checkFlags []string) { | ||
s := make([]interface{}, len(checkFlags)) | ||
for i, v := range checkFlags { | ||
s[i] = v | ||
} | ||
if err := sh.Command(popperFolder+"/popper/_check/check.py", s...).Run(); err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
func runInDocker(checkFlags []string, checkEnv string) { | ||
dockerFlags := "" | ||
if len(environment) > 0 { | ||
dockerFlags += " -e " + strings.Join(environment, " -e ") | ||
} | ||
if len(volume) > 0 { | ||
dockerFlags += " -v " + strings.Join(volume, " -v ") | ||
} | ||
cmd_args := []string{"run", "--rm", "-i"} | ||
cmd_args = append(cmd_args, strings.Fields(dockerFlags)...) | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cmd_args = append(cmd_args, "--volume", dir+":"+dir, "--workdir", dir, "--volume", "/var/run/docker.sock:/var/run/docker.sock", "ivotron/poppercheck:"+checkEnv) | ||
|
||
s := make([]interface{}, len(cmd_args)+len(checkFlags)) | ||
for i, v := range cmd_args { | ||
s[i] = v | ||
} | ||
for i, v := range checkFlags { | ||
s[i+len(cmd_args)] = v | ||
} | ||
if err := sh.Command("docker", s...).Run(); err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
func runCheck() { | ||
expName, err := getExperimentName() | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
err = readPopperConfig() | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
checkEnv := "" | ||
if !viper.IsSet("envs." + expName) { | ||
fmt.Println("No environment in .popper.yml, using default (alpine-3.4)") | ||
checkEnv = "alpine-3.4" | ||
} else { | ||
checkEnv = viper.GetString("envs." + expName) | ||
} | ||
|
||
checkFlags := []string{"--timeout=" + timeout} | ||
if len(skip) > 0 { | ||
checkFlags = append(checkFlags, "--skip="+skip) | ||
} | ||
|
||
if checkEnv == "host" { | ||
runOnHost(checkFlags) | ||
} else { | ||
runInDocker(checkFlags, checkEnv) | ||
} | ||
} | ||
|
||
var checkCmd = &cobra.Command{ | ||
Use: "check", | ||
Short: "Run experiment and report on its status", | ||
Long: `Executes an experiment in its own isolated environment (docker container). | ||
Environment variables and folders can be made available inside the experiment's environment | ||
using -e and -v flags respectively. These are analogous to Docker's flags and are passed | ||
down to the 'docker run' command. The experiment folder is always passed to the experiment | ||
environment.`, | ||
Long: `Executes an experiment in its corresponding environment (host or docker). If using docker, | ||
environment variables and folders can be made available inside the container by using -e | ||
and -v flags respectively. These flags are passed down to the 'docker run' command. The | ||
experiment folder is bind-mounted. If the environment is 'host', the -v and -e flags are | ||
ignored.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
env := "" | ||
if len(environment) > 0 { | ||
env += " -e " + strings.Join(environment, " -e ") | ||
} | ||
if len(volumes) > 0 { | ||
env += " -v " + strings.Join(volumes, " -v ") | ||
} | ||
cmd_args := []string{"run", "--rm", "-i"} | ||
cmd_args = append(cmd_args, strings.Fields(env)...) | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cmd_args = append(cmd_args, "--volume", dir+":"+dir, "--workdir", dir, "--volume", "/var/run/docker.sock:/var/run/docker.sock", "ivotron/poppercheck", "--timeout", timeout) | ||
if len(skip) > 0 { | ||
cmd_args = append(cmd_args, "--skip="+skip) | ||
} | ||
|
||
s := make([]interface{}, len(cmd_args)) | ||
for i, v := range cmd_args { | ||
s[i] = v | ||
} | ||
if err := sh.Command("docker", s...).Run(); err != nil { | ||
log.Fatalln(err) | ||
if len(args) != 0 { | ||
log.Fatalln("This command doesn't take arguments.") | ||
} | ||
runCheck() | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(checkCmd) | ||
|
||
checkCmd.Flags().StringSliceVarP(&environment, "environment", "e", []string{}, "Environment variables available to the experiment.") | ||
checkCmd.Flags().StringSliceVarP(&volumes, "volume", "v", []string{}, "Volumes available to the experiment.") | ||
checkCmd.Flags().StringSliceVarP(&environment, "environment", "e", []string{}, `Environment variable available to the experiment. Can be | ||
given multiple times. This flag is ignored when the environment | ||
is 'host'.`) | ||
checkCmd.Flags().StringSliceVarP(&volume, "volume", "v", []string{}, `Volume available to the experiment. Can be given multiple times | ||
This flag is ignored when the environment is 'host'.`) | ||
checkCmd.Flags().StringVarP(&skip, "skip", "s", "", "Comma-separated list of stages to skip.") | ||
checkCmd.Flags().StringVarP(&timeout, "timeout", "t", "36000", "Timeout limit for experiment in seconds.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters