Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(worker): Add support for configuring docker host settings #575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion worker/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func init() {
rootCmd.PersistentFlags().Int("logger-max-size", 500, "maximum log file size (in MB)")
rootCmd.PersistentFlags().Int("logger-max-backups", 3, "maximum log file backups")
rootCmd.PersistentFlags().Int("logger-max-age", 3, "maximum log age")
rootCmd.PersistentFlags().StringSlice("docker-mounts", []string{}, "Global mount points, colon separated")
rootCmd.PersistentFlags().StringSlice("docker-devices", []string{}, "Device redirection, colon separated")
rootCmd.PersistentFlags().Bool("docker-privileged", false, "Run build container in privileged mode")
rootCmd.PersistentFlags().StringSlice("docker-addcaps", []string{}, "Add Linux capabilities")
}

func initDefaults() {
Expand All @@ -118,6 +122,10 @@ func initDefaults() {
viper.BindPFlag("logger.maxsize", rootCmd.PersistentFlags().Lookup("logger-max-size"))
viper.BindPFlag("logger.maxbackups", rootCmd.PersistentFlags().Lookup("logger-max-backups"))
viper.BindPFlag("logger.maxage", rootCmd.PersistentFlags().Lookup("logger-max-age"))
viper.BindPFlag("docker.mounts", rootCmd.PersistentFlags().Lookup("docker-mounts"))
viper.BindPFlag("docker.devices", rootCmd.PersistentFlags().Lookup("docker-devices"))
viper.BindPFlag("docker.privileged", rootCmd.PersistentFlags().Lookup("docker-privileged"))
viper.BindPFlag("docker.addcaps", rootCmd.PersistentFlags().Lookup("docker-addcaps"))
}

func newConfig() *config.Config {
Expand Down Expand Up @@ -185,7 +193,7 @@ func newConfig() *config.Config {
fatal(err)
}

docker.Init(cfg.Registry)
docker.Init(cfg)

return cfg
}
Expand Down
9 changes: 9 additions & 0 deletions worker/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type (
Auth *Auth `json:"auth"`
Registry *Registry `json:"registry"`
Logger *Logger `json:"logger"`
Docker *Docker `json:"docker"`
}

// Server configuration.
Expand Down Expand Up @@ -55,4 +56,12 @@ type (
Level string `json:"level"`
Stdout bool `json:"stdout"`
}

// Docker Additional host configuration.
Docker struct {
Mounts []string `json:"mounts"`
Devices []string `json:"devices"`
Privileged bool `json:"privileged"`
AddCaps []string `json:"addcaps"`
}
)
23 changes: 20 additions & 3 deletions worker/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,22 @@ func createContainer(cli *client.Client, name, image, dir string, cmd []string,
mounts := []mount.Mount{
{Type: mount.TypeBind, Source: path.Join(dir), Target: "/build"},
}
for i := range mountdir {
m := strings.Split(mountdir[i], ":")
var devices []container.DeviceMapping

for _, elem := range cfg.host.Devices {
m := strings.Split(elem, ":")
if len(m) != 2 || !fs.Exists(m[0]) {
continue
}
devices = append(devices, container.DeviceMapping{
PathOnHost: m[0],
PathInContainer: m[1],
CgroupPermissions: "rwm",
})
}

for _, elem := range append(mountdir, cfg.host.Mounts...) {
m := strings.Split(elem, ":")
if len(m) != 2 || !fs.Exists(m[0]) {
continue
}
Expand All @@ -273,7 +287,10 @@ func createContainer(cli *client.Client, name, image, dir string, cmd []string,
Env: env,
WorkingDir: "/build",
}, &container.HostConfig{
Mounts: mounts,
Mounts: mounts,
CapAdd: cfg.host.AddCaps,
Resources: container.Resources{Devices: devices},
Privileged: cfg.host.Privileged,
}, nil, nil, name)
}

Expand Down
10 changes: 5 additions & 5 deletions worker/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func PushImage(tag string) (io.ReadCloser, error) {
}
tag = prependTag(tag)

authConfig := types.AuthConfig{Username: cfg.Username, Password: cfg.Password}
authConfig := types.AuthConfig{Username: cfg.registry.Username, Password: cfg.registry.Password}
authJSON, _ := json.Marshal(authConfig)
auth := base64.URLEncoding.EncodeToString(authJSON)

Expand All @@ -93,8 +93,8 @@ func PullImage(image string, config *config.Registry) error {

opts := types.ImagePullOptions{}

if cfg.Username != "" && cfg.Password != "" {
authConfig := types.AuthConfig{Username: cfg.Username, Password: cfg.Password}
if cfg.registry.Username != "" && cfg.registry.Password != "" {
authConfig := types.AuthConfig{Username: cfg.registry.Username, Password: cfg.registry.Password}
authJSON, _ := json.Marshal(authConfig)
opts.RegistryAuth = base64.URLEncoding.EncodeToString(authJSON)
}
Expand Down Expand Up @@ -148,8 +148,8 @@ func configureTags(tags []string) []string {
}

func prependTag(tag string) string {
if !strings.HasPrefix(tag, cfg.Addr) {
tag = path.Clean(path.Join(cfg.Addr, tag))
if !strings.HasPrefix(tag, cfg.registry.Addr) {
tag = path.Clean(path.Join(cfg.registry.Addr, tag))
}
return tag
}
10 changes: 7 additions & 3 deletions worker/docker/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package docker
import "github.com/bleenco/abstruse/worker/config"

var (
cfg *config.Registry
cfg struct {
registry *config.Registry
host *config.Docker
}
)

// Init initializes global variables
func Init(config *config.Registry) {
cfg = config
func Init(config *config.Config) {
cfg.registry = config.Registry
cfg.host = config.Docker
}