diff --git a/cli/server/server.go b/cli/server/server.go index 477adee..6b6aed6 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -95,7 +95,7 @@ func (c *serverCommand) run(*kingpin.ParseContext) error { if loadedConfig.Server.SkipPrepareServer { logrus.Infoln("skipping prepare server eg install docker / git") } else { - setup.PrepareSystem() + setup.PrepareSystem(&loadedConfig) } // starts the http server. err = serverInstance.Start(ctx) diff --git a/config/config.go b/config/config.go index 915bee6..019cfe1 100644 --- a/config/config.go +++ b/config/config.go @@ -19,6 +19,11 @@ type Config struct { NetworkDriver string `envconfig:"NETWORK_DRIVER"` } + Docker struct { + Binary string `envconfig:"DOCKER_BIN" default:""` + Socket string `envconfig:"DOCKER_SOCKET" default:""` + } + Server struct { Bind string `envconfig:"HTTPS_BIND" default:":3000"` CertFile string `envconfig:"SERVER_CERT_FILE" default:"/tmp/certs/server-cert.pem"` // Server certificate PEM file diff --git a/handler/handler.go b/handler/handler.go index 8e304f2..60b70e2 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -52,7 +52,7 @@ func Handler(config *config.Config, engine *engine.Engine, stepExecutor *runtime // Health check r.Mount("/healthz", func() http.Handler { sr := chi.NewRouter() - sr.Get("/", HandleHealth()) + sr.Get("/", HandleHealth(config)) return sr }()) diff --git a/handler/health.go b/handler/health.go index d72d466..1fcffbd 100644 --- a/handler/health.go +++ b/handler/health.go @@ -5,6 +5,7 @@ package handler import ( + "github.com/harness/harness-docker-runner/config" "net/http" "github.com/harness/harness-docker-runner/api" @@ -13,11 +14,11 @@ import ( "github.com/sirupsen/logrus" ) -func HandleHealth() http.HandlerFunc { +func HandleHealth(config *config.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { logrus.Infoln("handler: HandleHealth()") instanceInfo := setup.GetInstanceInfo() - dockerOK := setup.DockerInstalled(instanceInfo) + dockerOK := setup.DockerInstalled(instanceInfo, config) gitOK := setup.GitInstalled(instanceInfo) version := version.Version response := api.HealthResponse{ diff --git a/handler/setup.go b/handler/setup.go index 0e51d75..b3207fc 100644 --- a/handler/setup.go +++ b/handler/setup.go @@ -91,7 +91,7 @@ func HandleSetup(config *config.Config) http.HandlerFunc { logger.FromRequest(r).Traceln("starting the setup process") if s.MountDockerSocket == nil || *s.MountDockerSocket { // required to support m1 where docker isn't installed. - s.Volumes = append(s.Volumes, getDockerSockVolume()) + s.Volumes = append(s.Volumes, getDockerSockVolume(config)) } // fmt.Printf("setup request config: %+v\n", s.SetupRequestConfig) @@ -189,10 +189,13 @@ func sanitize(r string) string { return strings.ReplaceAll(r, "[-_]", "") } -func getDockerSockVolume() *spec.Volume { - path := engine.DockerSockUnixPath - if runtime.GOOS == "windows" { - path = engine.DockerSockWinPath +func getDockerSockVolume(config *config.Config) *spec.Volume { + path := config.Docker.Socket + if path == "" { + path = engine.DockerSockUnixPath + if runtime.GOOS == "windows" { + path = engine.DockerSockWinPath + } } return &spec.Volume{ HostPath: &spec.VolumeHostPath{ diff --git a/handler/step.go b/handler/step.go index 7d8ddec..441bb97 100644 --- a/handler/step.go +++ b/handler/step.go @@ -39,7 +39,7 @@ func HandleStartStep(config *config.Config) http.HandlerFunc { } if s.MountDockerSocket == nil || *s.MountDockerSocket { // required to support m1 where docker isn't installed. - s.Volumes = append(s.Volumes, getDockerSockVolumeMount()) + s.Volumes = append(s.Volumes, getDockerSockVolumeMount(config)) } ex := executor.GetExecutor() stageData, err := ex.Get(s.StageRuntimeID) @@ -205,10 +205,13 @@ func updateDelegateCapacity(s *api.StartStepRequestConfig) { } } -func getDockerSockVolumeMount() *spec.VolumeMount { - path := engine.DockerSockUnixPath - if runtime.GOOS == "windows" { - path = engine.DockerSockWinPath +func getDockerSockVolumeMount(config *config.Config) *spec.VolumeMount { + path := config.Docker.Socket + if path == "" { + path = engine.DockerSockUnixPath + if runtime.GOOS == "windows" { + path = engine.DockerSockWinPath + } } return &spec.VolumeMount{ Name: engine.DockerSockVolName, diff --git a/setup/setup.go b/setup/setup.go index 536e88c..7e4713f 100644 --- a/setup/setup.go +++ b/setup/setup.go @@ -5,6 +5,7 @@ package setup import ( + "github.com/harness/harness-docker-runner/config" "os" "os/exec" "runtime" @@ -21,12 +22,12 @@ func GetInstanceInfo() InstanceInfo { return InstanceInfo{osType: osType} } -func PrepareSystem() { +func PrepareSystem(config *config.Config) { instanceInfo := GetInstanceInfo() if !GitInstalled(instanceInfo) { installGit(instanceInfo) } - if !DockerInstalled(instanceInfo) { + if !DockerInstalled(instanceInfo, config) { installDocker(instanceInfo) } } @@ -48,13 +49,17 @@ func GitInstalled(instanceInfo InstanceInfo) (installed bool) { return true } -func DockerInstalled(instanceInfo InstanceInfo) (installed bool) { +func DockerInstalled(instanceInfo InstanceInfo, config *config.Config) (installed bool) { logrus.Infoln("checking docker is installed") switch instanceInfo.osType { case windowsString: logrus.Infoln("windows: we should check docker installation here") case osxString: - cmd := exec.Command("/usr/local/bin/docker", "ps") + binPath := config.Docker.Binary + if binPath == "" { + binPath = "/usr/local/bin/docker" + } + cmd := exec.Command(binPath, "ps") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err := cmd.Run()