func (d *Driver) GetState() (state.State, error) { log.WithField("MachineId", d.MachineId).Debug("Get status for OpenStack instance...") if err := d.initCompute(); err != nil { return state.None, err } s, err := d.client.GetInstanceState(d) if err != nil { return state.None, err } log.WithFields(log.Fields{ "MachineId": d.MachineId, "State": s, }).Debug("State for OpenStack instance") switch s { case "ACTIVE": return state.Running, nil case "PAUSED": return state.Paused, nil case "SUSPENDED": return state.Saved, nil case "SHUTOFF": return state.Stopped, nil case "BUILDING": return state.Starting, nil case "ERROR": return state.Error, nil } return state.None, nil }
// Authenticate creates a Rackspace-specific Gophercloud client. func (c *Client) Authenticate(d *openstack.Driver) error { if c.Provider != nil { return nil } log.WithFields(log.Fields{ "Username": d.Username, }).Debug("Authenticating to Rackspace.") apiKey := c.driver.APIKey opts := gophercloud.AuthOptions{ Username: d.Username, APIKey: apiKey, } provider, err := rackspace.NewClient(rackspace.RackspaceUSIdentity) if err != nil { return err } provider.UserAgent.Prepend(fmt.Sprintf("docker-machine/v%s", version.Version)) err = rackspace.Authenticate(provider, opts) if err != nil { return err } c.Provider = provider return nil }
func (d *Driver) assignFloatingIp() error { if err := d.initNetwork(); err != nil { return err } portId, err := d.client.GetInstancePortId(d) if err != nil { return err } ips, err := d.client.GetFloatingIPs(d) if err != nil { return err } var floatingIp *FloatingIp log.WithFields(log.Fields{ "MachineId": d.MachineId, "Pool": d.FloatingIpPool, }).Debugf("Looking for an available floating IP") for _, ip := range ips { if ip.PortId == "" { log.WithFields(log.Fields{ "MachineId": d.MachineId, "IP": ip.Ip, }).Debugf("Available floating IP found") floatingIp = &ip break } } if floatingIp == nil { floatingIp = &FloatingIp{} log.WithField("MachineId", d.MachineId).Debugf("No available floating IP found. Allocating a new one...") } else { log.WithField("MachineId", d.MachineId).Debugf("Assigning floating IP to the instance") } if err := d.client.AssignFloatingIP(d, floatingIp, portId); err != nil { return err } d.IPAddress = floatingIp.Ip return nil }
func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { log.WithFields(log.Fields{ "machineName": machineName, "storePath": storePath, "caCert": caCert, "privateKey": privateKey, }).Debug("Instantiating OpenStack driver...") return NewDerivedDriver(machineName, storePath, &GenericClient{}, caCert, privateKey) }
func (d *Driver) lookForIpAddress() error { ip, err := d.GetIP() if err != nil { return err } d.IPAddress = ip log.WithFields(log.Fields{ "IP": ip, "MachineId": d.MachineId, }).Debug("IP address found") return nil }
func (c *GenericClient) Authenticate(d *Driver) error { if c.Provider != nil { return nil } log.WithFields(log.Fields{ "AuthUrl": d.AuthUrl, "Insecure": d.Insecure, "DomainID": d.DomainID, "DomainName": d.DomainName, "Username": d.Username, "TenantName": d.TenantName, "TenantID": d.TenantId, }).Debug("Authenticating...") opts := gophercloud.AuthOptions{ IdentityEndpoint: d.AuthUrl, DomainID: d.DomainID, DomainName: d.DomainName, Username: d.Username, Password: d.Password, TenantName: d.TenantName, TenantID: d.TenantId, AllowReauth: true, } provider, err := openstack.NewClient(opts.IdentityEndpoint) if err != nil { return err } provider.UserAgent.Prepend(fmt.Sprintf("docker-machine/v%s", version.VERSION)) if d.Insecure { // Configure custom TLS settings. config := &tls.Config{InsecureSkipVerify: true} transport := &http.Transport{TLSClientConfig: config} provider.HTTPClient.Transport = transport } err = openstack.Authenticate(provider, opts) if err != nil { return err } c.Provider = provider return nil }
func (d *Driver) createMachine() error { log.WithFields(log.Fields{ "FlavorId": d.FlavorId, "ImageId": d.ImageId, }).Debug("Creating OpenStack instance...") if err := d.initCompute(); err != nil { return err } instanceId, err := d.client.CreateInstance(d) if err != nil { return err } d.MachineId = instanceId return nil }
// NewDriver instantiates a Rackspace driver. func NewDriver(machineName string, storePath string, caCert string, privateKey string) (drivers.Driver, error) { log.WithFields(log.Fields{ "machineName": machineName, "storePath": storePath, "caCert": caCert, "privateKey": privateKey, }).Debug("Instantiating Rackspace driver.") client := &Client{} inner, err := openstack.NewDerivedDriver(machineName, storePath, client, caCert, privateKey) if err != nil { return nil, err } driver := &Driver{Driver: inner} client.driver = driver return driver, nil }
func (d *Driver) resolveIds() error { if d.NetworkName != "" { if err := d.initNetwork(); err != nil { return err } networkId, err := d.client.GetNetworkId(d) if err != nil { return err } if networkId == "" { return fmt.Errorf(errorUnknownNetworkName, d.NetworkName) } d.NetworkId = networkId log.WithFields(log.Fields{ "Name": d.NetworkName, "ID": d.NetworkId, }).Debug("Found network id using its name") } if d.FlavorName != "" { if err := d.initCompute(); err != nil { return err } flavorId, err := d.client.GetFlavorId(d) if err != nil { return err } if flavorId == "" { return fmt.Errorf(errorUnknownFlavorName, d.FlavorName) } d.FlavorId = flavorId log.WithFields(log.Fields{ "Name": d.FlavorName, "ID": d.FlavorId, }).Debug("Found flavor id using its name") } if d.ImageName != "" { if err := d.initCompute(); err != nil { return err } imageId, err := d.client.GetImageId(d) if err != nil { return err } if imageId == "" { return fmt.Errorf(errorUnknownImageName, d.ImageName) } d.ImageId = imageId log.WithFields(log.Fields{ "Name": d.ImageName, "ID": d.ImageId, }).Debug("Found image id using its name") } if d.FloatingIpPool != "" { if err := d.initNetwork(); err != nil { return err } f, err := d.client.GetFloatingIpPoolId(d) if err != nil { return err } if f == "" { return fmt.Errorf(errorUnknownNetworkName, d.FloatingIpPool) } d.FloatingIpPoolId = f log.WithFields(log.Fields{ "Name": d.FloatingIpPool, "ID": d.FloatingIpPoolId, }).Debug("Found floating IP pool id using its name") } return nil }