diff --git a/build.yml b/build.yml index 36a889d..fddb235 100644 --- a/build.yml +++ b/build.yml @@ -5,7 +5,7 @@ build: tester: name: tester dockerfile: Dockerfile.tester - command: go test + command: go test . ./configuration crosscompile: name: crosscompile depends_on: diff --git a/build/builder.go b/build/builder.go index cf586e3..51cc89a 100644 --- a/build/builder.go +++ b/build/builder.go @@ -439,14 +439,26 @@ func (b *Builder) BuildStep(step *Step, step_number int) error { permMap := make(map[string]int) if b.Conf.UseStatForPermissions { for _, art := range step.Artifacts { + // Define stat command for debian and override if told to use another OS + statCmd := []string{"stat", "--format='%a'", art.Source} + switch b.Conf.OsType { + case "redhat": + statCmd = []string{"stat", "--format='%a'", art.Source} + case "busybox": + statCmd = []string{"stat", "-c", "'%a'", art.Source} + case "alpine": + statCmd = []string{"stat", "-c", "'%a'", art.Source} + } + execOpts := docker.CreateExecOptions{ Container: container.ID, AttachStdin: false, AttachStdout: true, AttachStderr: true, Tty: false, - Cmd: []string{"stat", "--format='%a'", art.Source}, + Cmd: statCmd, } + b.Conf.Logger.Debugf("Executing inside %s: %s", container.ID, execOpts.Cmd) execObj, err := b.docker.CreateExec(execOpts) if err != nil { return err @@ -512,6 +524,7 @@ func (b *Builder) BuildStep(step *Step, step_number int) error { Tty: true, Cmd: strings.Split(step.Command, " "), } + b.Conf.Logger.Debugf("Executing inside %s: %s", container.ID, execOpts.Cmd) execObj, err := b.docker.CreateExec(execOpts) if err != nil { return err @@ -705,12 +718,23 @@ func (b *Builder) copyToHost(a *Artifact, container string, perms map[string]int } func (b *Builder) createContainer(step *Step) (*docker.Container, error) { + // Define shell for debian and override if another OS is specified + shell := []string{"/bin/bash"} + switch b.Conf.OsType { + case "redhat": + shell = []string{"/bin/bash"} + case "busybox": + shell = []string{"/bin/sh"} + case "alpine": + shell = []string{"/bin/sh"} + } + config := docker.Config{ AttachStdout: true, AttachStdin: false, AttachStderr: true, Image: b.uniqueStepName(step), - Cmd: []string{"/bin/bash"}, + Cmd: shell, Tty: true, } diff --git a/configuration/config.go b/configuration/config.go index ecb0823..4c42e2a 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -14,6 +14,15 @@ type TupleItem struct { type TupleArray []TupleItem +// Recognized OS type for --os switch. +// Needs to agree with builder.go +var OsTypes = []string{ + "debian", + "redhat", + "busybox", + "alpine", +} + // Config stores application configurations type Config struct { Buildfile string @@ -36,6 +45,7 @@ type Config struct { UseTLS bool UseStatForPermissions bool FroceRmImages bool + OsType string ApiPort int ApiBinding string SecretService bool @@ -79,3 +89,12 @@ func (i *TupleArray) Find(key string) string { func CreateConfig() Config { return Config{} } + +func (c *Config) ValidateOsType() bool { + for _, os := range OsTypes { + if c.OsType == os { + return true + } + } + return false +} diff --git a/configuration/config_test.go b/configuration/config_test.go new file mode 100644 index 0000000..9dcf008 --- /dev/null +++ b/configuration/config_test.go @@ -0,0 +1,50 @@ +package configuration_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "testing" + "github.com/cloud66/habitus/configuration" +) + +var _ = Describe("Config", func() { + var c configuration.Config + + BeforeEach(func() { + c = configuration.CreateConfig() + }) + + Describe("--os allowed values", func() { + It("should accept debian", func() { + c.OsType = "debian" + Expect(c.ValidateOsType()).To(Equal(true)) + }) + + It("should accept redhat", func() { + c.OsType = "redhat" + Expect(c.ValidateOsType()).To(Equal(true)) + }) + + It("should accept alpine", func() { + c.OsType = "alpine" + Expect(c.ValidateOsType()).To(Equal(true)) + }) + + It("should accept busybox", func() { + c.OsType = "busybox" + Expect(c.ValidateOsType()).To(Equal(true)) + }) + + It("should not accept msdos", func() { + c.OsType = "msdos" + Expect(c.ValidateOsType()).To(Equal(false)) + }) + }) + +}) + + +func ConfigTest(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Habitus Config Suite") +} diff --git a/main.go b/main.go index 4c4152c..96c653c 100644 --- a/main.go +++ b/main.go @@ -74,6 +74,7 @@ func main() { flag.BoolVar(&config.NoSquash, "no-cleanup", false, "Skip cleanup commands for this run. Used for debugging") flag.BoolVar(&config.FroceRmImages, "force-rmi", false, "Force remove of unwanted images") flag.BoolVar(&config.NoPruneRmImages, "noprune-rmi", false, "No pruning of unwanted images") + flag.StringVar(&config.OsType, "os", "debian", "Specify the OS that the build occurs in") flag.BoolVar(&flagShowHelp, "help", false, "Display the help") flag.BoolVar(&flagShowVersion, "version", false, "Display version information") flag.IntVar(&config.ApiPort, "port", 8080, "Port to server the API") @@ -111,6 +112,13 @@ func main() { return } + // The -os flag allows free form text. Validate what was specified + if !config.ValidateOsType() { + fmt.Println("Invalid OS name") + fmt.Println("Please choose from ", configuration.OsTypes) + return + } + level, err := logging.LogLevel(flagLevel) if err != nil { fmt.Println("Invalid log level value. Falling back to debug")