Skip to content

Commit

Permalink
Adds support for other container engines (ie. podman) (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngharo committed Sep 9, 2022
1 parent e0cb46d commit 2fe370a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 47 deletions.
4 changes: 2 additions & 2 deletions cmd/cli/assets/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# do not edit --- autogenerated by runiac --- do not edit
# syntax = docker/dockerfile:experimental

ARG RUNIAC_CONTAINER="runiac/deploy:latest-alpine-full"
ARG RUNIAC_CONTAINER="docker.io/runiac/deploy:latest-alpine-full"

# Uncomment the below to pull down terraform modules during build rather than run.
#FROM golang:1.13 as builder
Expand Down Expand Up @@ -37,4 +37,4 @@ COPY entrypoint.sh entrypoint.sh
RUN chmod +x entrypoint.sh


ENTRYPOINT ["bash", "-c", "./entrypoint.sh"]
ENTRYPOINT ["bash", "-c", "./entrypoint.sh"]
91 changes: 53 additions & 38 deletions cmd/cli/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ import (
"github.com/spf13/viper"
)

var AppVersion string
var Environment string
var PrimaryRegions []string
var RegionalRegions []string
var DryRun bool
var SelfDestroy bool
var Account string
var LogLevel string
var Interactive bool
var Container string
var Namespace string
var DeploymentRing string
var Local bool
var Runner string
var PullRequest string
var StepWhitelist []string
var Dockerfile string

// DefaultBaseContainer the base container for runiac
var DefaultBaseContainer = "runiac/deploy:latest-alpine-full"
var DefaultDockerfile = ".runiac/Dockerfile"
var (
AppVersion string
Environment string
PrimaryRegions []string
RegionalRegions []string
DryRun bool
SelfDestroy bool
Account string
LogLevel string
Interactive bool
Container string = "docker.io/runiac/deploy:latest-alpine-full"
Namespace string
DeploymentRing string
Local bool
Runner string
PullRequest string
StepWhitelist []string
Dockerfile string = ".runiac/Dockerfile"
ContainerEngine string = "docker"
Test bool = false
)

func init() {
deployCmd.Flags().StringVarP(&AppVersion, "version", "v", "", "Version of the iac code")
Expand All @@ -51,13 +51,16 @@ func init() {
deployCmd.Flags().BoolVar(&SelfDestroy, "self-destroy", false, "Teardown after running deploy")
deployCmd.Flags().StringVar(&LogLevel, "log-level", "", "Log level")
deployCmd.Flags().BoolVar(&Interactive, "interactive", false, "Run Docker container in interactive mode")
deployCmd.Flags().StringVarP(&Container, "container", "c", "", fmt.Sprintf("The runiac deploy container to execute in, defaults to '%s'", DefaultBaseContainer))
deployCmd.Flags().StringVarP(&Container, "container", "c", Container, "The runiac deploy container to execute in.")
deployCmd.Flags().StringVarP(&DeploymentRing, "deployment-ring", "d", "", "The deployment ring to configure")
deployCmd.Flags().BoolVar(&Local, "local", false, "Pre-configure settings to create an isolated configuration specific to the executing machine")
deployCmd.Flags().StringVarP(&Runner, "runner", "", "terraform", "The deployment tool to use for deploying infrastructure")
deployCmd.Flags().StringSliceVarP(&StepWhitelist, "steps", "s", []string{}, "Only run the specified steps. To specify steps inside a track: -s {trackName}/{stepName}. To run multiple steps, separate with a comma. If empty, it will run all steps. To run no steps, specify a non-existent step.")
deployCmd.Flags().StringVar(&PullRequest, "pull-request", "", "Pre-configure settings to create an isolated configuration specific to a pull request, provide pull request identifier")
deployCmd.Flags().StringVarP(&Dockerfile, "dockerfile", "f", "", fmt.Sprintf("The dockerfile runiac builds to execute the deploy in, defaults to the autogenerated '%s' and must derive from runiac/deploy:{version}-alpine. Runiac official dockerfiles are here: https://github.com/runiac/docker", DefaultDockerfile))
deployCmd.Flags().StringVarP(&Dockerfile, "dockerfile", "f", Dockerfile, "The dockerfile runiac builds to execute the deploy in, defaults to the autogenerated '%s' and must derive from runiac/deploy:{version}-alpine. Runiac official dockerfiles are here: https://github.com/runiac/docker")
deployCmd.Flags().StringVar(&ContainerEngine, "container-engine", ContainerEngine, "Container engine (ie. podman or docker)")
deployCmd.Flags().BoolVar(&Test, "test", Test, "Hidden flag only set during unit testing")
deployCmd.Flags().MarkHidden("test")

rootCmd.AddCommand(deployCmd)
}
Expand All @@ -67,6 +70,18 @@ var deployCmd = &cobra.Command{
Short: "Deploy configurations",
Long: `This will execute the deploy action for each step.`,
Run: func(cmd *cobra.Command, args []string) {
// These options can be set via config file.
// The command line option, if set, always takes precendence.
setStringFlag(cmd, &ContainerEngine, "container-engine", "container_engine")
setStringFlag(cmd, &Container, "container", "container")
setStringFlag(cmd, &Dockerfile, "dockerfile", "dockerfile")

// This condition is only met during unit testing.
// It should come after any setup / option parsing and precendence steps.
if Test {
return
}

checkDockerExists()

ok := checkInitialized()
Expand All @@ -78,7 +93,7 @@ var deployCmd = &cobra.Command{
buildKit := "DOCKER_BUILDKIT=1"
containerTag := viper.GetString("project")

cmdd := exec.Command("docker", "build", "-t", containerTag, "-f", getDockerfileForBuild())
cmdd := exec.Command(ContainerEngine, "build", "-t", containerTag, "-f", Dockerfile)

cmdd.Args = append(cmdd.Args, getBuildArguments()...)

Expand Down Expand Up @@ -112,7 +127,7 @@ var deployCmd = &cobra.Command{

logrus.Info("Completed build, lets run!")

cmd2 := exec.Command("docker", "run", "--rm")
cmd2 := exec.Command(ContainerEngine, "run", "--rm")

cmd2.Env = append(os.Environ(), buildKit)

Expand Down Expand Up @@ -206,6 +221,17 @@ var deployCmd = &cobra.Command{
},
}

// setStringFlag - If flag is changed via command line, do nothing, else check config file for value.
func setStringFlag(cmd *cobra.Command, flag *string, cmdLineOption string, configOption string) {
if cmd.Flags().Changed(cmdLineOption) == false {
configValue := viper.GetString(configOption)

if configValue != "" {
*flag = configValue
}
}
}

func appendEIfSet(slice []string, arg string, val string) []string {
if val != "" {
return appendE(slice, arg, val)
Expand All @@ -218,27 +244,16 @@ func appendE(slice []string, arg string, val string) []string {
}

func checkDockerExists() {
_, err := exec.LookPath("docker")
_, err := exec.LookPath(ContainerEngine)
if err != nil {
fmt.Printf("please add 'docker' to the path\n")
fmt.Printf("please add '%s' to the path\n", ContainerEngine)
}
}

func checkInitialized() bool {
return InitAction()
}

func getDockerfileForBuild() string {
if Dockerfile != "" {
//Dockerfile = Dockerfile
} else if viper.GetString("dockerfile") != "" {
return viper.GetString("dockerfile")
} else {
return DefaultDockerfile
}
return Dockerfile
}

func getBuildArguments() (args []string) {
// check viper configuration if not set
if Container == "" && viper.GetString("container") != "" {
Expand Down
31 changes: 25 additions & 6 deletions cmd/cli/cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,30 @@ func TestGetBuildArguments_ShouldSetBuildArgContainerOnlyWhenValueExists(t *test
}
}

func TestGetDockerfileForBuild(t *testing.T) {
result := getDockerfileForBuild()
require.Equal(t, ".runiac/Dockerfile", result)
func Test_DeployCommand(t *testing.T) {
cmd := rootCmd
cmd.SetArgs([]string{"deploy", "--test"})
cmd.Execute()

// Assert config values are used when command line is not present
cmd.SetArgs([]string{"deploy", "--test"})
viper.Set("container_engine", "mock")
viper.Set("container", "mock")
viper.Set("dockerfile", "mock")
result2 := getDockerfileForBuild()
require.Equal(t, "mock", result2)
viper.Set("dockerfile", "")

cmd.Execute()
require.Equal(t, "mock", Dockerfile)
require.Equal(t, "mock", ContainerEngine)
require.Equal(t, "mock", Container)

// Assert command line precedence
cmd.SetArgs([]string{"deploy", "--test", "--dockerfile=mockofseagulls", "--container-engine=mockofseagulls", "--container=mockofseagulls"})
viper.Set("container_engine", "ignore_me")
viper.Set("container", "ignore_me")
viper.Set("dockerfile", "ignore_me")

cmd.Execute()
require.Equal(t, "mockofseagulls", Dockerfile)
require.Equal(t, "mockofseagulls", ContainerEngine)
require.Equal(t, "mockofseagulls", Container)
}
2 changes: 1 addition & 1 deletion cmd/cli/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const DockerIgnore = `# do not edit --- autogenerated by runiac --- do not edit
const DockerfileTemplate = `# do not edit --- autogenerated by runiac --- do not edit
# syntax = docker/dockerfile:experimental
ARG RUNIAC_CONTAINER="runiac/deploy:latest-alpine-full"
ARG RUNIAC_CONTAINER="docker.io/runiac/deploy:latest-alpine-full"
# Uncomment the below to pull down terraform modules during build rather than run.
#FROM golang:1.13 as builder
Expand Down

0 comments on commit 2fe370a

Please sign in to comment.