Skip to content

Commit

Permalink
Merge pull request #101 from hickey/feature/busybox
Browse files Browse the repository at this point in the history
Added -busybox switch to handle busybox/alpine containers
  • Loading branch information
khash authored Sep 17, 2018
2 parents 48badad + d516979 commit e509082
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:
tester:
name: tester
dockerfile: Dockerfile.tester
command: go test
command: go test . ./configuration
crosscompile:
name: crosscompile
depends_on:
Expand Down
28 changes: 26 additions & 2 deletions build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}

Expand Down
19 changes: 19 additions & 0 deletions configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,6 +45,7 @@ type Config struct {
UseTLS bool
UseStatForPermissions bool
FroceRmImages bool
OsType string
ApiPort int
ApiBinding string
SecretService bool
Expand Down Expand Up @@ -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
}
50 changes: 50 additions & 0 deletions configuration/config_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit e509082

Please sign in to comment.