Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Alpine provisioner #4529

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
178 changes: 178 additions & 0 deletions libmachine/provision/alpine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
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"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ apk remove
ERROR: 'remove' is not an apk command. See 'apk --help'.

This should be:
packageAction = "del"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks

}

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("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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could be better for detecting whether community is enabled. Strictly speaking docker could be installed but community not although I'm not entirely sure in this case that it matters

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer if ! which docker >/dev/null && ! apk info docker >/dev/null; ...?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll think about it. It's probably fine for now

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I've added the check in the last commit do you want me to remove it?

Copy link
Author

@publicarray publicarray Jul 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to summarise what (the updated) script does:
If the docker command is not found and apk can't find docker in any installed repositories via apk info docker we continue to install the community repo. Next we retrieve the major and minor version from /etc/alpine-release. The official mirror (CDN) for the appropriate version is appended to the /etc/apk/repositories file. Finally apk update is used to refresh the local repository database.

I think that this is pretty safe but if you have any ideas to improve this I'm open to improvements.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just spent some time putting this together, tell me what you think:

grep '^[[:blank:]]*[^#].*community' /etc/apk/repositories || sed
 -i '/^\s*[^#].*main/{p;s/main/community/;}' /etc/apk/repositories

This checks for the existence of an enabled community repo. If it's not present, it copies the main repository and s/main/community/.

From what I have tested this works in pretty much every case

Copy link
Author

@publicarray publicarray Jul 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think this is much better. Would you like to make the commit so you get credit for the script?

The only way i can think of where this would not work in the highly unlikely case where when 'main' is not installed. Maybe a user tries to run this script from the alpine.iso directly? from memory the iso is mounted as a cdrom and is the sole repository until setup-alpine is run. But again I don't think that will happen.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, hum. I didn't consider that case. Ideally this would be more complex to account for that too. Maybe I should add the extra check in if the main repo doesn't exist either. I'll make a PR to your fork in a minute

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to commit this later, but here is the revised statement:

if ! grep -q '^[[:blank:]]*[^#].*community$' /etc/apk/repositories; then grep -q '^[[:blank:]]*[^#].*main$' /etc/apk/repositories || echo "http://dl-cdn
.alpinelinux.org/alpine/v$(cut -d. -f1-2 /etc/alpine-release)/main" >> /etc/apk/repositories; sed -i '/^\s*[^#].*main/{p;s/main/community/;}' /etc/apk/repositories; fi; cat /etc/apk/repositories

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
}