func NewCluster(gopt *options.Options) (*Cluster, error) { rootPath := gopt.String("storage-path") os.Setenv("MACHINE_STORAGE_PATH", rootPath) if gopt.Bool("native-ssh") { ssh.SetDefaultClient(ssh.Native) } auth := getTLSAuthOptions(gopt) store := libmachine.NewFilestore(rootPath, auth.CaCertPath, auth.CaKeyPath) provider, _ := libmachine.New(store) hosts, err := provider.List() if err != nil { return nil, err } machines := make(map[string]*Machine, len(hosts)) for _, h := range hosts { machines[h.Name] = &Machine{ Host: h, } } c := &Cluster{ provider: provider, machines: machines, authOptions: auth, } c.LoadStates() return c, nil }
func (d *Driver) Create() error { if err := d.checkPrereqs(); err != nil { return err } log.Infof("Creating key pair for instances...") if err := d.createKeyPair(); err != nil { return fmt.Errorf("unable to create key pair: %s", err) } log.Infof("Configuring security groups...") if err := d.configureSecurityGroup(d.SecurityGroupName); err != nil { return err } // TODO Support data disk if d.SSHPassword == "" { d.SSHPassword = randomPassword() log.Info("Launching instance with generated password, please update password in console or log in with ssh key.") } imageID := d.GetImageID(d.ImageID) log.Infof("Launching instance with image %s ...", imageID) args := ecs.CreateInstanceArgs{ RegionId: d.Region, ImageId: imageID, InstanceType: d.InstanceType, SecurityGroupId: d.SecurityGroupId, Password: d.SSHPassword, VSwitchId: d.VSwitchId, ClientToken: d.getClient().GenerateClientToken(), } // Set InternetMaxBandwidthOut only for classic network if d.VSwitchId == "" { args.InternetMaxBandwidthOut = d.InternetMaxBandwidthOut } //log.Debugf("CreateInstanceArgs: %++v", args) // Create instance instanceId, err := d.getClient().CreateInstance(&args) if err != nil { err = fmt.Errorf("Error create instance: %s", err) log.Error(err) return err } d.InstanceId = instanceId // Wait for creation successfully err = d.getClient().WaitForInstance(instanceId, ecs.Stopped, 300) if err != nil { err = fmt.Errorf("Error wait instance to Stopped: %s", err) log.Error(err) } // Assign public IP if not private IP only if err == nil && !d.PrivateIPOnly { if d.VSwitchId == "" { // Allocate public IP address for classic network _, err = d.getClient().AllocatePublicIpAddress(instanceId) if err != nil { err = fmt.Errorf("Error allocate public IP address for instance %s: %v", instanceId, err) } } else { err = d.configNetwork(instanceId) } } if err == nil { // Start instance err = d.getClient().StartInstance(instanceId) if err == nil { // Wait for running err = d.getClient().WaitForInstance(instanceId, ecs.Running, 300) if err == nil { instance, err := d.getInstance() if err == nil { if len(instance.InnerIpAddress.IpAddress) > 0 { d.PrivateIPAddress = instance.InnerIpAddress.IpAddress[0] } d.IPAddress = d.getIP(instance) ssh.SetDefaultClient(ssh.Native) d.uploadKeyPair() log.Debugf("created instance ID %s, IP address %s, Private IP address %s", d.InstanceId, d.IPAddress, d.PrivateIPAddress, ) } } else { err = fmt.Errorf("Failed to wait instance to running state: %s", err) } } else { err = fmt.Errorf("Failed to start instance %s: %v", instanceId, err) } } if err != nil { log.Warn(err) d.Remove() } return err }
func main() { for _, f := range os.Args { if f == "-D" || f == "--debug" || f == "-debug" { os.Setenv("DEBUG", "1") } } cli.AppHelpTemplate = AppHelpTemplate cli.CommandHelpTemplate = CommandHelpTemplate app := cli.NewApp() app.Name = path.Base(os.Args[0]) app.Author = "Docker Machine Contributors" app.Email = "https://github.com/docker/machine" app.Before = func(c *cli.Context) error { os.Setenv("MACHINE_STORAGE_PATH", c.GlobalString("storage-path")) if c.GlobalBool("native-ssh") { ssh.SetDefaultClient(ssh.Native) } return nil } app.Commands = commands.Commands app.CommandNotFound = cmdNotFound app.Usage = "Create and manage machines running Docker." app.Version = version.VERSION + " (" + version.GITCOMMIT + ")" app.Flags = []cli.Flag{ cli.BoolFlag{ Name: "debug, D", Usage: "Enable debug mode", }, cli.StringFlag{ EnvVar: "MACHINE_STORAGE_PATH", Name: "s, storage-path", Value: utils.GetBaseDir(), Usage: "Configures storage path", }, cli.StringFlag{ EnvVar: "MACHINE_TLS_CA_CERT", Name: "tls-ca-cert", Usage: "CA to verify remotes against", Value: "", }, cli.StringFlag{ EnvVar: "MACHINE_TLS_CA_KEY", Name: "tls-ca-key", Usage: "Private key to generate certificates", Value: "", }, cli.StringFlag{ EnvVar: "MACHINE_TLS_CLIENT_CERT", Name: "tls-client-cert", Usage: "Client cert to use for TLS", Value: "", }, cli.StringFlag{ EnvVar: "MACHINE_TLS_CLIENT_KEY", Name: "tls-client-key", Usage: "Private key used in client TLS auth", Value: "", }, cli.BoolFlag{ EnvVar: "MACHINE_NATIVE_SSH", Name: "native-ssh", Usage: "Use the native (Go-based) SSH implementation.", }, } app.Run(os.Args) }