Skip to content

Commit

Permalink
feat: support passing custom build user (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
jithine authored Jan 25, 2022
1 parent 783e2d4 commit 718b304
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Flags:
--sudo Use sudo command for container runtime.
--vol string Mount local volumes into build container. (<src>:<destination>) (default [])

-u, --user string Change default build user. Default value is from container in use.
Global Flags:
-v, --verbose verbose output.
```
Expand Down
9 changes: 9 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func newBuildCmd() *cobra.Command {
var metaFilePath string
var socketPath string
var localVolumes []string
var buildUser string

buildCmd := &cobra.Command{
Use: "build [job name]",
Expand Down Expand Up @@ -256,6 +257,7 @@ func newBuildCmd() *cobra.Command {
SocketPath: socketPath,
FlagVerbose: flagVerbose,
LocalVolumes: localVolumes,
BuildUser: buildUser,
}

launch := launchNew(option)
Expand Down Expand Up @@ -357,5 +359,12 @@ ex) git@github.com:<org>/<repo>.git[#<branch>]
[]string{},
"Volumes to mount into build container.")

buildCmd.Flags().StringVarP(
&buildUser,
"user",
"u",
"",
"Change default build user. Default value is from container in use.")

return buildCmd
}
1 change: 1 addition & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Flags:
ex) git@github.com:<org>/<repo>.git[#<branch>]
https://github.com/<org>/<repo>.git[#<branch>]
--sudo Use sudo command for container runtime.
-u, --user string Change default build user. Default value is from container in use.
--vol strings Volumes to mount into build container.
`, defaultSocketPath)
Expand Down
8 changes: 7 additions & 1 deletion launch/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type docker struct {
interact Interacter
socketPath string
localVolumes []string
buildUser string
}

var _ runner = (*docker)(nil)
Expand All @@ -46,7 +47,7 @@ const (
orgRepo = "sd-local/local-build"
)

func newDocker(setupImage, setupImageVer string, useSudo bool, interactiveMode bool, socketPath string, flagVerbose bool, localVolumes []string) runner {
func newDocker(setupImage, setupImageVer string, useSudo bool, interactiveMode bool, socketPath string, flagVerbose bool, localVolumes []string, buildUser string) runner {
return &docker{
volume: "SD_LAUNCH_BIN",
habVolume: "SD_LAUNCH_HAB",
Expand All @@ -60,6 +61,7 @@ func newDocker(setupImage, setupImageVer string, useSudo bool, interactiveMode b
interact: &Interact{flagVerbose: flagVerbose},
socketPath: socketPath,
localVolumes: localVolumes,
buildUser: buildUser,
}
}

Expand Down Expand Up @@ -148,6 +150,10 @@ func (d *docker) runBuild(buildEntry buildEntry) error {
dockerCommandOptions = append([]string{"--privileged"}, dockerCommandOptions...)
}

if d.buildUser != "" {
dockerCommandOptions = append([]string{fmt.Sprintf("-u%s", d.buildUser)}, dockerCommandOptions...)
}

if d.interactiveMode {
// attach build container for sd-local interact mode
cid, err := d.execDockerCommand(append(dockerCommandArgs, dockerCommandOptions...)...)
Expand Down
3 changes: 2 additions & 1 deletion launch/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ func TestNewDocker(t *testing.T) {
interact: &Interact{},
socketPath: "/auth.sock",
localVolumes: []string{"path:path"},
buildUser: "jithin",
}

d := newDocker("launcher", "latest", false, false, "/auth.sock", false, []string{"path:path"})
d := newDocker("launcher", "latest", false, false, "/auth.sock", false, []string{"path:path"}, "jithin")

assert.Equal(t, expected, d)
})
Expand Down
3 changes: 2 additions & 1 deletion launch/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Option struct {
SocketPath string
FlagVerbose bool
LocalVolumes []string
BuildUser string
}

const (
Expand Down Expand Up @@ -170,7 +171,7 @@ func createBuildEntry(option Option) buildEntry {
func New(option Option) Launcher {
l := new(launch)

l.runner = newDocker(option.Entry.Launcher.Image, option.Entry.Launcher.Version, option.UseSudo, option.InteractiveMode, option.SocketPath, option.FlagVerbose, option.LocalVolumes)
l.runner = newDocker(option.Entry.Launcher.Image, option.Entry.Launcher.Version, option.UseSudo, option.InteractiveMode, option.SocketPath, option.FlagVerbose, option.LocalVolumes, option.BuildUser)
l.buildEntry = createBuildEntry(option)

return l
Expand Down

0 comments on commit 718b304

Please sign in to comment.