From 2ca6cc5116d362e28911a77f4798f3bc1399bba8 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Sun, 28 May 2017 15:58:56 -0400 Subject: [PATCH 1/5] Add Alpine provisioner, re docker/machine#3951. Signed-off-by: Phillip Whelan --- libmachine/provision/alpine.go | 173 +++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 libmachine/provision/alpine.go diff --git a/libmachine/provision/alpine.go b/libmachine/provision/alpine.go new file mode 100644 index 0000000000..f751daf8eb --- /dev/null +++ b/libmachine/provision/alpine.go @@ -0,0 +1,173 @@ +package provision + +import ( + "fmt" + "github.com/docker/machine/libmachine/auth" + "github.com/docker/machine/libmachine/drivers" + "github.com/docker/machine/libmachine/engine" + "github.com/docker/machine/libmachine/log" + "github.com/docker/machine/libmachine/mcnutils" + "github.com/docker/machine/libmachine/provision/pkgaction" + "github.com/docker/machine/libmachine/provision/serviceaction" + "github.com/docker/machine/libmachine/swarm" +) + +func init() { + Register("Alpine", &RegisteredProvisioner{ + New: NewAlpineProvisioner, + }) +} + +func NewAlpineProvisioner(d drivers.Driver) Provisioner { + return &AlpineProvisioner{ + GenericProvisioner{ + SSHCommander: GenericSSHCommander{Driver: d}, + DockerOptionsDir: "/etc/docker", + DaemonOptionsFile: "/etc/conf.d/docker", + OsReleaseID: "alpine", + Packages: []string{ + "curl", + }, + Driver: d, + }, + } +} + +type AlpineProvisioner struct { + GenericProvisioner +} + +func (provisioner *AlpineProvisioner) String() string { + return "alpine" +} + +func (provisioner *AlpineProvisioner) CompatibleWithHost() bool { + return provisioner.OsReleaseInfo.ID == provisioner.OsReleaseID || provisioner.OsReleaseInfo.IDLike == provisioner.OsReleaseID +} + +func (provisioner *AlpineProvisioner) Package(name string, action pkgaction.PackageAction) error { + var packageAction string + + switch action { + case pkgaction.Install, pkgaction.Upgrade: + packageAction = "add" + case pkgaction.Remove: + packageAction = "remove" + } + + switch name { + case "docker-engine": + name = "docker" + case "docker": + name = "docker" + } + + command := fmt.Sprintf("sudo apk %s %s", packageAction, name) + + log.Debugf("package: action=%s name=%s", action.String(), name) + + if _, err := provisioner.SSHCommand(command); err != nil { + return err + } + + return nil +} + +func (provisioner *AlpineProvisioner) dockerDaemonResponding() bool { + log.Debug("checking docker daemon") + + if out, err := provisioner.SSHCommand("sudo docker version"); err != nil { + log.Warnf("Error getting SSH command to check if the daemon is up: %s", err) + log.Debugf("'sudo docker version' output:\n%s", out) + return false + } + + // The daemon is up if the command worked. Carry on. + return true +} + +func (provisioner *AlpineProvisioner) Service(name string, action serviceaction.ServiceAction) error { + var command string + switch action { + case serviceaction.Start, serviceaction.Restart, serviceaction.Stop: + command = fmt.Sprintf("sudo rc-service %s %s", name, action.String()) + case serviceaction.DaemonReload: + command = fmt.Sprintf("sudo rc-service restart %s", name) + case serviceaction.Enable: + command = fmt.Sprintf("sudo rc-update add %s boot", name) + case serviceaction.Disable: + command = fmt.Sprintf("sudo rc-update del %s boot", name) + } + + if _, err := provisioner.SSHCommand(command); err != nil { + return err + } + + return nil +} + +func (provisioner *AlpineProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error { + provisioner.SwarmOptions = swarmOptions + provisioner.AuthOptions = authOptions + provisioner.EngineOptions = engineOptions + swarmOptions.Env = engineOptions.Env + + storageDriver, err := decideStorageDriver(provisioner, "overlay", engineOptions.StorageDriver) + if err != nil { + return err + } + provisioner.EngineOptions.StorageDriver = storageDriver + + // HACK: since Alpine does not come with sudo by default we install + log.Debug("Installing sudo") + if _, err := provisioner.SSHCommand("if ! type sudo; then apk add sudo; fi"); err != nil { + return err + } + + log.Debug("Setting hostname") + if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil { + return err + } + + log.Debug("Installing base packages") + for _, pkg := range provisioner.Packages { + if err := provisioner.Package(pkg, pkgaction.Install); err != nil { + return err + } + } + + log.Debug("Installing docker") + if err := provisioner.Package("docker", pkgaction.Install); err != nil { + return err + } + + log.Debug("Starting docker service") + if err := provisioner.Service("docker", serviceaction.Start); err != nil { + return err + } + + log.Debug("Waiting for docker daemon") + if err := mcnutils.WaitFor(provisioner.dockerDaemonResponding); err != nil { + return err + } + + provisioner.AuthOptions = setRemoteAuthOptions(provisioner) + + log.Debug("Configuring auth") + if err := ConfigureAuth(provisioner); err != nil { + return err + } + + log.Debug("Configuring swarm") + if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil { + return err + } + + // enable service + log.Debug("Enabling docker service") + if err := provisioner.Service("docker", serviceaction.Enable); err != nil { + return err + } + + return nil +} \ No newline at end of file From 09a83ce39b951edb4d08006ef5a9b0828904ac90 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 6 Jul 2018 08:39:35 +1000 Subject: [PATCH 2/5] Install community repo if docker is not found in installed repos Signed-off-by: Sebastian Schmidt --- libmachine/provision/alpine.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libmachine/provision/alpine.go b/libmachine/provision/alpine.go index f751daf8eb..30c8535191 100644 --- a/libmachine/provision/alpine.go +++ b/libmachine/provision/alpine.go @@ -86,7 +86,7 @@ func (provisioner *AlpineProvisioner) dockerDaemonResponding() bool { return true } -func (provisioner *AlpineProvisioner) Service(name string, action serviceaction.ServiceAction) error { +func (provisioner *AlpineProvisioner) Service(name string, action serviceaction.ServiceAction) error { var command string switch action { case serviceaction.Start, serviceaction.Restart, serviceaction.Stop: @@ -98,7 +98,7 @@ func (provisioner *AlpineProvisioner) Service(name string, action serviceaction. case serviceaction.Disable: command = fmt.Sprintf("sudo rc-update del %s boot", name) } - + if _, err := provisioner.SSHCommand(command); err != nil { return err } @@ -136,6 +136,11 @@ func (provisioner *AlpineProvisioner) Provision(swarmOptions swarm.Options, auth } } + log.Debug("Add Community repo") + if _, err := provisioner.SSHCommand("if ! apk info docker >/dev/null; then ver=$(awk '{split($1,a,\".\"); print a[1]\".\"a[2]}' /etc/alpine-release); echo \"http://dl-cdn.alpinelinux.org/alpine/v$ver/community\" >> /etc/apk/repositories; apk update; fi"); err != nil { + return err + } + log.Debug("Installing docker") if err := provisioner.Package("docker", pkgaction.Install); err != nil { return err @@ -170,4 +175,4 @@ func (provisioner *AlpineProvisioner) Provision(swarmOptions swarm.Options, auth } return nil -} \ No newline at end of file +} From ac1a6ff8e00b30e3f298b15574fa890fabb2cac0 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Sun, 8 Jul 2018 19:33:27 +1000 Subject: [PATCH 3/5] Alpine: Fix 'remove' is not an apk command Signed-off-by: Sebastian Schmidt --- libmachine/provision/alpine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libmachine/provision/alpine.go b/libmachine/provision/alpine.go index 30c8535191..a66b01cc98 100644 --- a/libmachine/provision/alpine.go +++ b/libmachine/provision/alpine.go @@ -52,7 +52,7 @@ func (provisioner *AlpineProvisioner) Package(name string, action pkgaction.Pack case pkgaction.Install, pkgaction.Upgrade: packageAction = "add" case pkgaction.Remove: - packageAction = "remove" + packageAction = "del" } switch name { @@ -137,7 +137,7 @@ func (provisioner *AlpineProvisioner) Provision(swarmOptions swarm.Options, auth } log.Debug("Add Community repo") - if _, err := provisioner.SSHCommand("if ! apk info docker >/dev/null; then ver=$(awk '{split($1,a,\".\"); print a[1]\".\"a[2]}' /etc/alpine-release); echo \"http://dl-cdn.alpinelinux.org/alpine/v$ver/community\" >> /etc/apk/repositories; apk update; fi"); err != nil { + if _, err := provisioner.SSHCommand("if ! which docker >/dev/null && ! apk info docker >/dev/null; then ver=$(awk '{split($1,a,\".\"); print a[1]\".\"a[2]}' /etc/alpine-release); echo \"http://dl-cdn.alpinelinux.org/alpine/v$ver/community\" >> /etc/apk/repositories; apk update; fi"); err != nil { return err } From 2f8587befc36d4a5dba5ab264016b5370030b0a2 Mon Sep 17 00:00:00 2001 From: Joe Groocock Date: Sun, 8 Jul 2018 18:48:28 +0100 Subject: [PATCH 4/5] Infer the community repo from the main repo URL Adds a default dl-cdn community repo if main is absent, based on /etc/alpine-release Signed-off-by: Joe Groocock --- libmachine/provision/alpine.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/libmachine/provision/alpine.go b/libmachine/provision/alpine.go index a66b01cc98..a1656788eb 100644 --- a/libmachine/provision/alpine.go +++ b/libmachine/provision/alpine.go @@ -137,7 +137,15 @@ func (provisioner *AlpineProvisioner) Provision(swarmOptions swarm.Options, auth } log.Debug("Add Community repo") - if _, err := provisioner.SSHCommand("if ! which docker >/dev/null && ! apk info docker >/dev/null; then ver=$(awk '{split($1,a,\".\"); print a[1]\".\"a[2]}' /etc/alpine-release); echo \"http://dl-cdn.alpinelinux.org/alpine/v$ver/community\" >> /etc/apk/repositories; apk update; fi"); err != nil { + if _, err := provisioner.SSHCommand(` + if ! grep -q '^[[:blank:]]*[^#].*community$' /etc/apk/repositories; then + if ! grep -q '^[[:blank:]]*[^#].*main$' /etc/apk/repositories; then + echo "http://dl-cdn.alpinelinux.org/alpine/v$(cut -d. -f1-2 /etc/alpine-release)/community" >> /etc/apk/repositories; + else + sed -i '/^\\s*[^#].*main/{p;s/main/community/;}' /etc/apk/repositories; + fi + fi; + apk update`); err != nil { return err } From cdf2165424e67b4edd5823f116e42017d45aea86 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 16 Jul 2018 00:37:51 +1000 Subject: [PATCH 5/5] fix add community repo to /etc/apk/repositories I think with the backtics the extra escaping is not needed. --- libmachine/provision/alpine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libmachine/provision/alpine.go b/libmachine/provision/alpine.go index a1656788eb..f92cee5c6f 100644 --- a/libmachine/provision/alpine.go +++ b/libmachine/provision/alpine.go @@ -142,7 +142,7 @@ func (provisioner *AlpineProvisioner) Provision(swarmOptions swarm.Options, auth if ! grep -q '^[[:blank:]]*[^#].*main$' /etc/apk/repositories; then echo "http://dl-cdn.alpinelinux.org/alpine/v$(cut -d. -f1-2 /etc/alpine-release)/community" >> /etc/apk/repositories; else - sed -i '/^\\s*[^#].*main/{p;s/main/community/;}' /etc/apk/repositories; + sed -i '/^\s*[^#].*main/{p;s/main/community/;}' /etc/apk/repositories; fi fi; apk update`); err != nil {