arch.go 3.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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("Arch", &RegisteredProvisioner{
		New: NewArchProvisioner,
	})
}

func NewArchProvisioner(d drivers.Driver) Provisioner {
	return &ArchProvisioner{
24
		NewSystemdProvisioner("arch", d),
25
26
27
28
	}
}

type ArchProvisioner struct {
29
	SystemdProvisioner
30
31
}

David Gageot's avatar
David Gageot committed
32
33
34
35
func (provisioner *ArchProvisioner) String() string {
	return "arch"
}

36
func (provisioner *ArchProvisioner) CompatibleWithHost() bool {
Olivier Gambier's avatar
Lint    
Olivier Gambier committed
37
	return provisioner.OsReleaseInfo.ID == provisioner.OsReleaseID || provisioner.OsReleaseInfo.IDLike == provisioner.OsReleaseID
38
39
40
41
42
43
44
45
}

func (provisioner *ArchProvisioner) Package(name string, action pkgaction.PackageAction) error {
	var packageAction string

	updateMetadata := true

	switch action {
46
	case pkgaction.Install, pkgaction.Upgrade:
47
48
49
50
51
52
53
		packageAction = "S"
	case pkgaction.Remove:
		packageAction = "R"
		updateMetadata = false
	}

	switch name {
54
55
	case "docker-engine":
		name = "docker"
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	case "docker":
		name = "docker"
	}

	pacmanOpts := "-" + packageAction
	if updateMetadata {
		pacmanOpts = pacmanOpts + "y"
	}

	pacmanOpts = pacmanOpts + " --noconfirm --noprogressbar"

	command := fmt.Sprintf("sudo -E pacman %s %s", pacmanOpts, name)

	log.Debugf("package: action=%s name=%s", action.String(), name)

	if _, err := provisioner.SSHCommand(command); err != nil {
		return err
	}

	return nil
}

func (provisioner *ArchProvisioner) dockerDaemonResponding() bool {
79
80
81
	log.Debug("checking docker daemon")

	if out, err := provisioner.SSHCommand("sudo docker version"); err != nil {
82
		log.Warnf("Error getting SSH command to check if the daemon is up: %s", err)
83
		log.Debugf("'sudo docker version' output:\n%s", out)
84
85
86
87
88
89
90
		return false
	}

	// The daemon is up if the command worked.  Carry on.
	return true
}

Olivier Gambier's avatar
Lint    
Olivier Gambier committed
91
func (provisioner *ArchProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error {
92
93
94
	provisioner.SwarmOptions = swarmOptions
	provisioner.AuthOptions = authOptions
	provisioner.EngineOptions = engineOptions
95
	swarmOptions.Env = engineOptions.Env
96

97
	storageDriver, err := decideStorageDriver(provisioner, "overlay2", engineOptions.StorageDriver)
98
99
	if err != nil {
		return err
100
	}
101
	provisioner.EngineOptions.StorageDriver = storageDriver
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

	// HACK: since Arch does not come with sudo by default we install
	log.Debug("Installing sudo")
	if _, err := provisioner.SSHCommand("if ! type sudo; then pacman -Sy --noconfirm --noprogressbar 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 systemd 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 in systemd
	log.Debug("Enabling docker in systemd")
Joffrey F's avatar
Joffrey F committed
150
151
	err = provisioner.Service("docker", serviceaction.Enable)
	return err
152
}