1 Star 0 Fork 0

litian/machine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
arch.go 5.72 KB
一键复制 编辑 原始数据 按行查看 历史
Olivier Gambier 提交于 2015-11-04 17:28 . Lint
package provision
import (
"bytes"
"fmt"
"text/template"
"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{
GenericProvisioner{
DockerOptionsDir: "/etc/docker",
DaemonOptionsFile: "/etc/systemd/system/docker.service",
OsReleaseID: "arch",
Packages: []string{},
Driver: d,
},
}
}
type ArchProvisioner struct {
GenericProvisioner
}
func (provisioner *ArchProvisioner) CompatibleWithHost() bool {
return provisioner.OsReleaseInfo.ID == provisioner.OsReleaseID || provisioner.OsReleaseInfo.IDLike == provisioner.OsReleaseID
}
func (provisioner *ArchProvisioner) Service(name string, action serviceaction.ServiceAction) error {
// daemon-reload to catch config updates; systemd -- ugh
if _, err := provisioner.SSHCommand("sudo systemctl daemon-reload"); err != nil {
return err
}
command := fmt.Sprintf("sudo systemctl %s %s", action.String(), name)
if _, err := provisioner.SSHCommand(command); err != nil {
return err
}
return nil
}
func (provisioner *ArchProvisioner) Package(name string, action pkgaction.PackageAction) error {
var packageAction string
updateMetadata := true
switch action {
case pkgaction.Install:
packageAction = "S"
case pkgaction.Remove:
packageAction = "R"
updateMetadata = false
case pkgaction.Upgrade:
packageAction = "U"
}
switch name {
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 {
if _, err := provisioner.SSHCommand("sudo docker version"); err != nil {
log.Warnf("Error getting SSH command to check if the daemon is up: %s", err)
return false
}
// The daemon is up if the command worked. Carry on.
return true
}
func (provisioner *ArchProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error {
provisioner.SwarmOptions = swarmOptions
provisioner.AuthOptions = authOptions
provisioner.EngineOptions = engineOptions
swarmOptions.Env = engineOptions.Env
if provisioner.EngineOptions.StorageDriver == "" {
provisioner.EngineOptions.StorageDriver = "overlay"
}
// 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")
if err := provisioner.Service("docker", serviceaction.Enable); err != nil {
return err
}
return nil
}
func (provisioner *ArchProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
var (
engineCfg bytes.Buffer
)
driverNameLabel := fmt.Sprintf("provider=%s", provisioner.Driver.DriverName())
provisioner.EngineOptions.Labels = append(provisioner.EngineOptions.Labels, driverNameLabel)
engineConfigTmpl := `[Service]
ExecStart=/usr/bin/docker -d -H tcp://0.0.0.0:{{.DockerPort}} -H unix:///var/run/docker.sock --storage-driver {{.EngineOptions.StorageDriver}} --tlsverify --tlscacert {{.AuthOptions.CaCertRemotePath}} --tlscert {{.AuthOptions.ServerCertRemotePath}} --tlskey {{.AuthOptions.ServerKeyRemotePath}} {{ range .EngineOptions.Labels }}--label {{.}} {{ end }}{{ range .EngineOptions.InsecureRegistry }}--insecure-registry {{.}} {{ end }}{{ range .EngineOptions.RegistryMirror }}--registry-mirror {{.}} {{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}} {{ end }}
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
`
t, err := template.New("engineConfig").Parse(engineConfigTmpl)
if err != nil {
return nil, err
}
engineConfigContext := EngineConfigContext{
DockerPort: dockerPort,
AuthOptions: provisioner.AuthOptions,
EngineOptions: provisioner.EngineOptions,
}
t.Execute(&engineCfg, engineConfigContext)
return &DockerOptions{
EngineOptions: engineCfg.String(),
EngineOptionsPath: provisioner.DaemonOptionsFile,
}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/litian33/machine.git
git@gitee.com:litian33/machine.git
litian33
machine
machine
v0.5.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385