func (h *Host) Restart() error { if drivers.MachineInState(h.Driver, state.Running)() { if err := h.Stop(); err != nil { return err } if err := utils.WaitFor(drivers.MachineInState(h.Driver, state.Stopped)); err != nil { return err } } if err := h.Start(); err != nil { return err } if err := utils.WaitFor(drivers.MachineInState(h.Driver, state.Running)); err != nil { return err } if err := h.SaveConfig(); err != nil { return err } return nil }
func (provisioner *Boot2DockerProvisioner) upgradeIso() error { log.Info("Stopping machine to do the upgrade...") if err := provisioner.Driver.Stop(); err != nil { return err } if err := utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Stopped)); err != nil { return err } machineName := provisioner.GetDriver().GetMachineName() log.Infof("Upgrading machine %s...", machineName) isoFilename := "" switch provisioner.GetDriver().DriverName() { case "virtualbox": isoFilename = "boot2docker-virtualbox.iso" case "vmwarefusion", "vmwarevsphere", "vmwareworkstation": isoFilename = "boot2docker-vmware.iso" case "hyper-v": isoFilename = "boot2docker-hyperv.iso" default: return ErrUnknownDriver } b2dutils := utils.NewB2dUtils("", "", isoFilename) // Usually we call this implicitly, but call it here explicitly to get // the latest boot2docker ISO. if err := b2dutils.DownloadLatestBoot2Docker(); err != nil { return err } // Copy the latest version of boot2docker ISO to the machine's directory if err := b2dutils.CopyIsoToMachineDir("", machineName); err != nil { return err } if err := provisioner.Driver.Start(); err != nil { return err } return utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Running)) }
func (h *Host) Create(name string) error { // create the instance if err := h.Driver.Create(); err != nil { return err } // save to store if err := h.SaveConfig(); err != nil { return err } // TODO: Not really a fan of just checking "none" here. if h.Driver.DriverName() != "none" { if err := utils.WaitFor(drivers.MachineInState(h.Driver, state.Running)); err != nil { return err } if err := WaitForSSH(h); err != nil { return err } provisioner, err := provision.DetectProvisioner(h.Driver) if err != nil { return err } if err := provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions); err != nil { return err } } return nil }
func (provisioner *UbuntuProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions) error { if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil { return err } for _, pkg := range provisioner.packages { if err := provisioner.Package(pkg, pkgaction.Install); err != nil { return err } } if err := installDockerGeneric(provisioner); err != nil { return err } if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil { return err } if err := ConfigureAuth(provisioner, authOptions); err != nil { return err } if err := configureSwarm(provisioner, swarmOptions); err != nil { return err } return nil }
func (d *Driver) waitForInstance() error { if err := utils.WaitFor(d.instanceIsRunning); err != nil { return err } return nil }
func (provisioner *SUSEProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error { provisioner.SwarmOptions = swarmOptions provisioner.AuthOptions = authOptions provisioner.EngineOptions = engineOptions if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil { return err } for _, pkg := range provisioner.Packages { if err := provisioner.Package(pkg, pkgaction.Install); err != nil { return err } } // update OS -- this is needed for libdevicemapper and the docker install if _, err := provisioner.SSHCommand("sudo zypper ref"); err != nil { return err } if _, err := provisioner.SSHCommand("sudo zypper -n update"); err != nil { return err } if err := installDockerGeneric(provisioner, engineOptions.InstallURL); err != nil { return err } if _, err := provisioner.SSHCommand("sudo systemctl start docker"); err != nil { return err } if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil { return err } if _, err := provisioner.SSHCommand("sudo systemctl stop docker"); err != nil { return err } // open firewall port required by docker if _, err := provisioner.SSHCommand("sudo /sbin/yast2 firewall services add ipprotocol=tcp tcpport=2376 zone=EXT"); err != nil { return err } if err := makeDockerOptionsDir(provisioner); err != nil { return err } provisioner.AuthOptions = setRemoteAuthOptions(provisioner) if err := ConfigureAuth(provisioner); err != nil { return err } if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil { return err } return nil }
func (provisioner *DebianProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error { provisioner.SwarmOptions = swarmOptions provisioner.AuthOptions = authOptions provisioner.EngineOptions = engineOptions if provisioner.EngineOptions.StorageDriver == "" { provisioner.EngineOptions.StorageDriver = "aufs" } // HACK: since debian does not come with sudo by default we install log.Debug("installing sudo") if _, err := provisioner.SSHCommand("if ! type sudo; then apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y 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 := installDockerGeneric(provisioner, engineOptions.InstallURL); err != nil { return err } log.Debug("waiting for docker daemon") if err := utils.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 (h *Host) Kill() error { if err := h.Driver.Stop(); err != nil { return err } if err := h.SaveConfig(); err != nil { return err } return utils.WaitFor(drivers.MachineInState(h.Driver, state.Stopped)) }
func (h *Host) Start() error { if err := h.Driver.Start(); err != nil { return err } if err := h.SaveConfig(); err != nil { return err } return utils.WaitFor(drivers.MachineInState(h.Driver, state.Running)) }
func (provisioner *Boot2DockerProvisioner) upgradeIso() error { log.Infof("Stopping machine to do the upgrade...") switch provisioner.Driver.DriverName() { case "vmwarefusion", "vmwarevsphere": return errors.New("Upgrade functionality is currently not supported for these providers, as they use a custom ISO.") } if err := provisioner.Driver.Stop(); err != nil { return err } if err := utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Stopped)); err != nil { return err } machineName := provisioner.GetDriver().GetMachineName() log.Infof("Upgrading machine %s...", machineName) b2dutils := utils.NewB2dUtils("", "") // Usually we call this implicitly, but call it here explicitly to get // the latest boot2docker ISO. if err := b2dutils.DownloadLatestBoot2Docker(); err != nil { return err } // Copy the latest version of boot2docker ISO to the machine's directory if err := b2dutils.CopyIsoToMachineDir("", machineName); err != nil { return err } log.Infof("Starting machine back up...") if err := provisioner.Driver.Start(); err != nil { return err } return utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Running)) }
func (client NativeClient) session(command string) (*ssh.Session, error) { if err := utils.WaitFor(client.dialSuccess); err != nil { return nil, fmt.Errorf("Error attempting SSH client dial: %s", err) } conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Hostname, client.Port), &client.Config) if err != nil { return nil, fmt.Errorf("Mysterious error dialing TCP for SSH (we already succeeded at least once) : %s", err) } return conn.NewSession() }
func (provisioner *RancherProvisioner) upgradeIso() error { // Largely copied from Boot2Docker provisioner, we should find a way to share this code log.Info("Stopping machine to do the upgrade...") if err := provisioner.Driver.Stop(); err != nil { return err } if err := utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Stopped)); err != nil { return err } machineName := provisioner.GetDriver().GetMachineName() log.Infof("Upgrading machine %s...", machineName) b2dutils := utils.NewB2dUtils("", "", isoFilename) url, err := provisioner.getLatestISOURL() if err != nil { return err } if err := b2dutils.DownloadISOFromURL(url); err != nil { return err } // Copy the latest version of boot2docker ISO to the machine's directory if err := b2dutils.CopyIsoToMachineDir("", machineName); err != nil { return err } log.Infof("Starting machine back up...") if err := provisioner.Driver.Start(); err != nil { return err } return utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Running)) }
func (provisioner *RedHatProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error { provisioner.SwarmOptions = swarmOptions provisioner.AuthOptions = authOptions provisioner.EngineOptions = engineOptions // set default storage driver for redhat if provisioner.EngineOptions.StorageDriver == "" { provisioner.EngineOptions.StorageDriver = "devicemapper" } if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil { return err } for _, pkg := range provisioner.Packages { log.Debugf("installing base package: name=%s", pkg) if err := provisioner.Package(pkg, pkgaction.Install); err != nil { return err } } // update OS -- this is needed for libdevicemapper and the docker install if _, err := provisioner.SSHCommand("sudo yum -y update"); err != nil { return err } // install docker if err := installDocker(provisioner); err != nil { return err } if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil { return err } if err := makeDockerOptionsDir(provisioner); err != nil { return err } provisioner.AuthOptions = setRemoteAuthOptions(provisioner) if err := ConfigureAuth(provisioner); err != nil { return err } if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil { return err } return nil }
func (provisioner *Boot2DockerProvisioner) upgradeIso() error { log.Info("Stopping machine to do the upgrade...") if err := provisioner.Driver.Stop(); err != nil { return err } if err := utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Stopped)); err != nil { return err } machineName := provisioner.GetDriver().GetMachineName() log.Infof("Upgrading machine %s...", machineName) b2dutils := utils.NewB2dUtils("", "") // Usually we call this implicitly, but call it here explicitly to get // the latest boot2docker ISO. if err := b2dutils.DownloadLatestBoot2Docker(); err != nil { return err } // Copy the latest version of boot2docker ISO to the machine's directory if err := b2dutils.CopyIsoToMachineDir("", machineName); err != nil { return err } log.Infof("Starting machine back up...") if err := provisioner.Driver.Start(); err != nil { return err } return utils.WaitFor(drivers.MachineInState(provisioner.Driver, state.Running)) }
func (h *Host) runActionForState(action func() error, desiredState state.State) error { if drivers.MachineInState(h.Driver, desiredState)() { log.Debug("Machine already in state %s, returning", desiredState) return nil } if err := action(); err != nil { return err } if err := h.SaveConfig(); err != nil { return err } return utils.WaitFor(drivers.MachineInState(h.Driver, desiredState)) }
func (d *Driver) configureSecurityGroup(groupName string) error { log.Debugf("configuring security group in %s", d.VpcId) var securityGroup *amz.SecurityGroup groups, err := d.getClient().GetSecurityGroups() if err != nil { return err } for _, grp := range groups { if grp.GroupName == groupName { log.Debugf("found existing security group (%s) in %s", groupName, d.VpcId) securityGroup = &grp break } } // if not found, create if securityGroup == nil { log.Debugf("creating security group (%s) in %s", groupName, d.VpcId) group, err := d.getClient().CreateSecurityGroup(groupName, "Docker Machine", d.VpcId) if err != nil { return err } securityGroup = group // wait until created (dat eventual consistency) log.Debugf("waiting for group (%s) to become available", group.GroupId) if err := utils.WaitFor(d.securityGroupAvailableFunc(group.GroupId)); err != nil { return err } } d.SecurityGroupId = securityGroup.GroupId perms := d.configureSecurityGroupPermissions(securityGroup) if len(perms) != 0 { log.Debugf("authorizing group %s with permissions: %v", securityGroup.GroupName, perms) if err := d.getClient().AuthorizeSecurityGroup(d.SecurityGroupId, perms); err != nil { return err } } return nil }
func (provisioner *UbuntuProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error { provisioner.SwarmOptions = swarmOptions provisioner.AuthOptions = authOptions provisioner.EngineOptions = engineOptions if provisioner.EngineOptions.StorageDriver == "" { provisioner.EngineOptions.StorageDriver = "aufs" } if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil { return err } for _, pkg := range provisioner.Packages { if err := provisioner.Package(pkg, pkgaction.Install); err != nil { return err } } if err := installDockerGeneric(provisioner, engineOptions.InstallURL); err != nil { return err } if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil { return err } if err := makeDockerOptionsDir(provisioner); err != nil { return err } provisioner.AuthOptions = setRemoteAuthOptions(provisioner) if err := ConfigureAuth(provisioner); err != nil { return err } if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil { return err } return nil }
func (provisioner *AiyaraProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions) error { log.Debug("Entering Provision") if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil { return err } if err := provisioner.installPublicKey(); err != nil { return err } if d0, ok := provisioner.Driver.(interface { ClearSSHPasswd() }); ok { d0.ClearSSHPasswd() } if err := provisioner.installCustomDocker(); err != nil { return err } if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil { return err } if err := ConfigureAuth(provisioner, authOptions); err != nil { return err } if err := configureSwarm(provisioner, swarmOptions); err != nil { return err } return nil }
func (d *Driver) Create() error { if err := d.checkPrereqs(); err != nil { return err } log.Infof("Launching instance...") if err := d.createKeyPair(); err != nil { return fmt.Errorf("unable to create key pair: %s", err) } if err := d.configureSecurityGroup(d.SecurityGroupName); err != nil { return err } bdm := &amz.BlockDeviceMapping{ DeviceName: "/dev/sda1", VolumeSize: d.RootSize, DeleteOnTermination: true, VolumeType: "gp2", } log.Debugf("launching instance in subnet %s", d.SubnetId) instance, err := d.getClient().RunInstance(d.AMI, d.InstanceType, d.Zone, 1, 1, d.SecurityGroupId, d.KeyName, d.SubnetId, bdm, d.IamInstanceProfile) if err != nil { return fmt.Errorf("Error launching instance: %s", err) } d.InstanceId = instance.InstanceId log.Debug("waiting for ip address to become available") if err := utils.WaitFor(d.instanceIpAvailable); err != nil { return err } if len(instance.NetworkInterfaceSet) > 0 { d.PrivateIPAddress = instance.NetworkInterfaceSet[0].PrivateIpAddress } d.waitForInstance() log.Debugf("created instance ID %s, IP address %s, Private IP address %s", d.InstanceId, d.IPAddress, d.PrivateIPAddress, ) log.Infof("Waiting for SSH on %s:%d", d.IPAddress, 22) if err := ssh.WaitForTCP(fmt.Sprintf("%s:%d", d.IPAddress, 22)); err != nil { return err } log.Debug("Settings tags for instance") tags := map[string]string{ "Name": d.MachineName, } if err = d.getClient().CreateTags(d.InstanceId, tags); err != nil { return err } return nil }
func (d *Driver) Create() error { // Create SSH key log.Debugf("Creating ssh key --- \n") if err := d.createSSHKey(); err != nil { return err } return nil // Create instance log.Info("Creating ECS instance...") createResp, err := d.getClient().Instance.CreateInstance(map[string]string{ "RegionId": d.RegionId, "SecurityGroupId": d.SecurityGroupId, "ImageId": d.ImageId, "InstanceType": d.InstanceTypeId, "InstanceName": d.MachineName, "InternetMaxBandwidthOut": d.BandwidthOut, "Password": d.SSHPass, }) if err != nil { return err } d.InstanceId = createResp.InstanceId // Allocate public ip address log.Info("Allocating public IP address...") ipResp, err := d.getClient().Network.AllocatePublicIpAddress(map[string]string{ "InstanceId": d.InstanceId, }) if err != nil { return nil } d.IPAddress = ipResp.IpAddress // Start instance log.Info("Starting instance, this may take several minutes...") _, err = d.getClient().Instance.StartInstance(map[string]string{ "InstanceId": d.InstanceId, }) if err != nil { return err } // Get private IP address statusResp, err := d.getClient().Instance.DescribeInstanceAttribute(map[string]string{ "InstanceId": d.InstanceId, }) if err != nil { return err } if ips := statusResp.InnerIpAddress.AllIpAddress; len(ips) > 0 { d.PrivateIPAddress = ips[0] } // Wait for instance to start if err := utils.WaitFor(d.waitToStartInstance); err != nil { return err } //TODO 暂时处理:等待20秒,主机真正启动 time.Sleep(20 * time.Second) // Wait for ssh available port, e := d.GetSSHPort() if e != nil { return e } ip, e := d.GetIP() if e != nil { return e } if err := ssh.WaitForTCP(fmt.Sprintf("%s:%d", ip, port)); err != nil { return err } // Upload SSH key to host log.Info("Upload SSH key to machine...") if err := d.uploadKeyPair(); err != nil { return err } log.Infof("Created Instance ID %s, Public IP address %s, Private IP address %s", d.InstanceId, d.IPAddress, d.PrivateIPAddress, ) return nil }
func (d *Driver) Create() error { if err := d.checkPrereqs(); err != nil { return err } log.Infof("Launching instance...") if err := d.createKeyPair(); err != nil { return fmt.Errorf("unable to create key pair: %s", err) } if err := d.configureSecurityGroup(d.SecurityGroupName); err != nil { return err } bdm := &amz.BlockDeviceMapping{ DeviceName: "/dev/sda1", VolumeSize: d.RootSize, DeleteOnTermination: true, VolumeType: "gp2", } log.Debugf("launching instance in subnet %s", d.SubnetId) var instance amz.EC2Instance if d.RequestSpotInstance { spotInstanceRequestId, err := d.getClient().RequestSpotInstances(d.AMI, d.InstanceType, d.Zone, 1, d.SecurityGroupId, d.KeyName, d.SubnetId, bdm, d.IamInstanceProfile, d.SpotPrice, d.Monitoring) if err != nil { return fmt.Errorf("Error request spot instance: %s", err) } var instanceId string var spotInstanceRequestStatus string log.Info("Waiting for spot instance...") // check until fulfilled for instanceId == "" { time.Sleep(time.Second * 5) spotInstanceRequestStatus, instanceId, err = d.getClient().DescribeSpotInstanceRequests(spotInstanceRequestId) if err != nil { return fmt.Errorf("Error describe spot instance request: %s", err) } log.Debugf("spot instance request status: %s", spotInstanceRequestStatus) } instance, err = d.getClient().GetInstance(instanceId) if err != nil { return fmt.Errorf("Error get instance: %s", err) } } else { inst, err := d.getClient().RunInstance(d.AMI, d.InstanceType, d.Zone, 1, 1, d.SecurityGroupId, d.KeyName, d.SubnetId, bdm, d.IamInstanceProfile, d.PrivateIPOnly, d.Monitoring) if err != nil { return fmt.Errorf("Error launching instance: %s", err) } instance = inst } d.InstanceId = instance.InstanceId log.Debug("waiting for ip address to become available") if err := utils.WaitFor(d.instanceIpAvailable); err != nil { return err } if len(instance.NetworkInterfaceSet) > 0 { d.PrivateIPAddress = instance.NetworkInterfaceSet[0].PrivateIpAddress } d.waitForInstance() log.Debugf("created instance ID %s, IP address %s, Private IP address %s", d.InstanceId, d.IPAddress, d.PrivateIPAddress, ) log.Debug("Settings tags for instance") tags := map[string]string{ "Name": d.MachineName, } if err := d.getClient().CreateTags(d.InstanceId, tags); err != nil { return err } return nil }
func WaitForSSH(h *Host) error { if err := utils.WaitFor(sshAvailableFunc(h)); err != nil { return fmt.Errorf("Too many retries. Last error: %s", err) } return nil }
func (d *Driver) configureSecurityGroup(groupName string) error { log.Debugf("configuring security group in %s", d.VpcId) var securityGroup *ecs.DescribeSecurityGroupAttributeResponse args := ecs.DescribeSecurityGroupsArgs{ RegionId: d.Region, VpcId: d.VpcId, } //TODO handle pagination groups, _, err := d.getClient().DescribeSecurityGroups(&args) if err != nil { return err } //log.Debugf("DescribeSecurityGroups: %++v\n", groups) for _, grp := range groups { if grp.SecurityGroupName == groupName && grp.VpcId == d.VpcId { log.Debugf("found existing security group (%s) in %s", groupName, d.VpcId) securityGroup, _ = d.getSecurityGroup(grp.SecurityGroupId) break } } // if not found, create if securityGroup == nil { log.Debugf("creating security group (%s) in %s", groupName, d.VpcId) creationArgs := ecs.CreateSecurityGroupArgs{ RegionId: d.Region, SecurityGroupName: groupName, Description: "Docker Machine", VpcId: d.VpcId, ClientToken: d.getClient().GenerateClientToken(), } groupId, err := d.getClient().CreateSecurityGroup(&creationArgs) if err != nil { return err } // wait until created (dat eventual consistency) log.Debugf("waiting for group (%s) to become available", groupId) if err := utils.WaitFor(d.securityGroupAvailableFunc(groupId)); err != nil { return err } securityGroup, err = d.getSecurityGroup(groupId) if err != nil { return err } } d.SecurityGroupId = securityGroup.SecurityGroupId perms := d.configureSecurityGroupPermissions(securityGroup) for _, permission := range perms { log.Debugf("authorizing group %s with permission: %v", securityGroup.SecurityGroupName, permission) args := permission.createAuthorizeSecurityGroupArgs(d.Region, d.SecurityGroupId) if err := d.getClient().AuthorizeSecurityGroup(args); err != nil { return err } } return nil }