func GetDefaultTestHost() (*host.Host, error) { hostOptions := &host.HostOptions{ EngineOptions: &engine.EngineOptions{}, SwarmOptions: &swarm.SwarmOptions{}, AuthOptions: &auth.AuthOptions{ CaCertPath: HostTestCaCert, CaPrivateKeyPath: HostTestPrivateKey, }, } driver := none.NewDriver(DefaultHostName, "/tmp/artifacts") host := &host.Host{ ConfigVersion: version.ConfigVersion, Name: DefaultHostName, Driver: driver, DriverName: "none", HostOptions: hostOptions, } flags := GetTestDriverFlags() if err := host.Driver.SetConfigFromFlags(flags); err != nil { return nil, err } return host, nil }
func runDriver(driverName string) { switch driverName { case "amazonec2": plugin.RegisterDriver(amazonec2.NewDriver("", "")) case "digitalocean": plugin.RegisterDriver(digitalocean.NewDriver("", "")) case "exoscale": plugin.RegisterDriver(exoscale.NewDriver("", "")) case "generic": plugin.RegisterDriver(generic.NewDriver("", "")) case "google": plugin.RegisterDriver(google.NewDriver("", "")) case "hyperv": plugin.RegisterDriver(hyperv.NewDriver("", "")) case "none": plugin.RegisterDriver(none.NewDriver("", "")) case "openstack": plugin.RegisterDriver(openstack.NewDriver("", "")) case "rackspace": plugin.RegisterDriver(rackspace.NewDriver("", "")) case "softlayer": plugin.RegisterDriver(softlayer.NewDriver("", "")) case "virtualbox": plugin.RegisterDriver(virtualbox.NewDriver("", "")) case "vmwarefusion": plugin.RegisterDriver(vmwarefusion.NewDriver("", "")) case "vmwarevcloudair": plugin.RegisterDriver(vmwarevcloudair.NewDriver("", "")) case "vmwarevsphere": plugin.RegisterDriver(vmwarevsphere.NewDriver("", "")) default: fmt.Fprintf(os.Stderr, "Unsupported driver: %s\n", driverName) os.Exit(1) } }
func TestStoreLoad(t *testing.T) { defer cleanup() expectedURL := "unix:///foo/baz" flags := hosttest.GetTestDriverFlags() flags.Data["url"] = expectedURL store := getTestStore() h, err := hosttest.GetDefaultTestHost() if err != nil { t.Fatal(err) } if err := h.Driver.SetConfigFromFlags(flags); err != nil { t.Fatal(err) } if err := store.Save(h); err != nil { t.Fatal(err) } h, err = store.Load(h.Name) if err != nil { t.Fatal(err) } rawDataDriver, ok := h.Driver.(*host.RawDataDriver) if !ok { t.Fatal("Expected driver loaded from store to be of type *host.RawDataDriver and it was not") } realDriver := none.NewDriver(h.Name, store.Path) if err := json.Unmarshal(rawDataDriver.Data, &realDriver); err != nil { t.Fatalf("Error unmarshaling rawDataDriver data into concrete 'none' driver: %s", err) } h.Driver = realDriver actualURL, err := h.URL() if err != nil { t.Fatal(err) } if actualURL != expectedURL { t.Fatalf("GetURL is not %q, got %q", expectedURL, actualURL) } }
func NewDriver(driverName, hostName, storePath string) (drivers.Driver, error) { var ( driver drivers.Driver ) switch driverName { case "virtualbox": driver = virtualbox.NewDriver(hostName, storePath) case "digitalocean": driver = digitalocean.NewDriver(hostName, storePath) case "amazonec2": driver = amazonec2.NewDriver(hostName, storePath) case "azure": driver = azure.NewDriver(hostName, storePath) case "exoscale": driver = exoscale.NewDriver(hostName, storePath) case "generic": driver = generic.NewDriver(hostName, storePath) case "google": driver = google.NewDriver(hostName, storePath) case "hyperv": driver = hyperv.NewDriver(hostName, storePath) case "openstack": driver = openstack.NewDriver(hostName, storePath) case "rackspace": driver = rackspace.NewDriver(hostName, storePath) case "softlayer": driver = softlayer.NewDriver(hostName, storePath) case "vmwarefusion": driver = vmwarefusion.NewDriver(hostName, storePath) case "vmwarevcloudair": driver = vmwarevcloudair.NewDriver(hostName, storePath) case "vmwarevsphere": driver = vmwarevsphere.NewDriver(hostName, storePath) case "none": driver = none.NewDriver(hostName, storePath) case "aiyara": driver = aiyara.NewDriver(hostName, storePath) default: return nil, fmt.Errorf("Driver %q not recognized", driverName) } return driver, nil }
func RunDriver(driverName string) error { switch driverName { case "amazonec2": plugin.RegisterDriver(amazonec2.NewDriver("", "")) case "azure": plugin.RegisterDriver(azure.NewDriver("", "")) case "digitalocean": plugin.RegisterDriver(digitalocean.NewDriver("", "")) case "exoscale": plugin.RegisterDriver(exoscale.NewDriver("", "")) case "generic": plugin.RegisterDriver(generic.NewDriver("", "")) case "google": plugin.RegisterDriver(google.NewDriver("", "")) case "hyperv": plugin.RegisterDriver(hyperv.NewDriver("", "")) case "none": plugin.RegisterDriver(none.NewDriver("", "")) case "openstack": plugin.RegisterDriver(openstack.NewDriver("", "")) case "rackspace": plugin.RegisterDriver(rackspace.NewDriver("", "")) case "softlayer": plugin.RegisterDriver(softlayer.NewDriver("", "")) case "virtualbox": plugin.RegisterDriver(virtualbox.NewDriver("", "")) case "vmwarefusion": plugin.RegisterDriver(vmwarefusion.NewDriver("", "")) case "vmwarevcloudair": plugin.RegisterDriver(vmwarevcloudair.NewDriver("", "")) case "vmwarevsphere": plugin.RegisterDriver(vmwarevsphere.NewDriver("", "")) case "cloudstack": plugin.RegisterDriver(cloudstack.NewDriver("", "")) default: return errors.Errorf("Unsupported driver: %s\n", driverName) } return nil }
func MigrateHost(h *Host, data []byte) (*Host, bool, error) { var ( migrationNeeded = false migrationPerformed = false hostV1 *V1 hostV2 *V2 ) migratedHostMetadata, err := getMigratedHostMetadata(data) if err != nil { return nil, false, err } globalStorePath := filepath.Dir(filepath.Dir(migratedHostMetadata.HostOptions.AuthOptions.StorePath)) driver := &RawDataDriver{none.NewDriver(h.Name, globalStorePath), nil} if migratedHostMetadata.ConfigVersion > version.ConfigVersion { return nil, false, errConfigFromFuture } if migratedHostMetadata.ConfigVersion == version.ConfigVersion { h.Driver = driver if err := json.Unmarshal(data, &h); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling most recent host version: %s", err) } } else { migrationNeeded = true } if migrationNeeded { migrationPerformed = true for h.ConfigVersion = migratedHostMetadata.ConfigVersion; h.ConfigVersion < version.ConfigVersion; h.ConfigVersion++ { log.Debugf("Migrating to config v%d", h.ConfigVersion) switch h.ConfigVersion { case 0: hostV0 := &V0{ Driver: driver, } if err := json.Unmarshal(data, &hostV0); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 0: %s", err) } hostV1 = MigrateHostV0ToHostV1(hostV0) case 1: if hostV1 == nil { hostV1 = &V1{ Driver: driver, } if err := json.Unmarshal(data, &hostV1); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 1: %s", err) } } hostV2 = MigrateHostV1ToHostV2(hostV1) case 2: if hostV2 == nil { hostV2 = &V2{ Driver: driver, } if err := json.Unmarshal(data, &hostV2); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 2: %s", err) } } h = MigrateHostV2ToHostV3(hostV2, data, globalStorePath) driver.Data = h.RawDriver h.Driver = driver case 3: } } } h.RawDriver = driver.Data return h, migrationPerformed, nil }
func MigrateHost(h *Host, data []byte) (*Host, bool, error) { var ( migrationNeeded = false migrationPerformed = false hostV1 *V1 hostV2 *V2 ) migratedHostMetadata, err := getMigratedHostMetadata(data) if err != nil { return nil, false, err } globalStorePath := filepath.Dir(filepath.Dir(migratedHostMetadata.HostOptions.AuthOptions.StorePath)) driver := none.NewDriver(h.Name, globalStorePath) if migratedHostMetadata.ConfigVersion > version.ConfigVersion { return nil, false, errors.New("Config version is from the future, please upgrade your Docker Machine client.") } if migratedHostMetadata.ConfigVersion == version.ConfigVersion { h.Driver = driver if err := json.Unmarshal(data, &h); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling most recent host version: %s", err) } // We are config version 3, so we definitely should have a // RawDriver field. However, it's possible some might use // older clients after already migrating, so check if it exists // and create one if not. The following code is an (admittedly // fragile) attempt to account for the fact that the above code // to forbid loading from future clients was not introduced // sooner. if h.RawDriver == nil { log.Warn("It looks like you have used an older Docker Machine binary to interact with hosts after using a 0.5.0 binary.") log.Warn("Please be advised that doing so can result in erratic behavior due to migrated configuration settings.") log.Warn("Machine will attempt to re-migrate the configuration settings, but safety is not guaranteed.") migrationNeeded = true // Treat the data as config version 1, even though it // says "latest". migratedHostMetadata.ConfigVersion = 1 } } else { migrationNeeded = true } if migrationNeeded { migrationPerformed = true for h.ConfigVersion = migratedHostMetadata.ConfigVersion; h.ConfigVersion < version.ConfigVersion; h.ConfigVersion++ { log.Debugf("Migrating to config v%d", h.ConfigVersion) switch h.ConfigVersion { case 0: hostV0 := &V0{ Driver: driver, } if err := json.Unmarshal(data, &hostV0); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 0: %s", err) } hostV1 = MigrateHostV0ToHostV1(hostV0) case 1: if hostV1 == nil { hostV1 = &V1{ Driver: driver, } if err := json.Unmarshal(data, &hostV1); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 1: %s", err) } } hostV2 = MigrateHostV1ToHostV2(hostV1) case 2: if hostV2 == nil { hostV2 = &V2{ Driver: driver, } if err := json.Unmarshal(data, &hostV2); err != nil { return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 2: %s", err) } } h = MigrateHostV2ToHostV3(hostV2, data, globalStorePath) h.Driver = RawDataDriver{driver, nil} case 3: } } } return h, migrationPerformed, nil }
func TestMigrateHost(t *testing.T) { testCases := []struct { description string hostBefore *Host rawData []byte expectedHostAfter *Host expectedMigrationPerformed bool expectedMigrationError error }{ { // Point of this test is largely that no matter what was in RawDriver // before, it shoud load into the Host struct based on what is actually // in the Driver field. // // Note that we don't check for the presence of RawDriver's literal "on // disk" here. It's intentional. description: "Config version 3 load with existing RawDriver on disk", hostBefore: &Host{ Name: "default", }, rawData: []byte(`{ "ConfigVersion": 3, "Driver": {"MachineName": "default"}, "DriverName": "virtualbox", "HostOptions": { "Driver": "", "Memory": 0, "Disk": 0, "AuthOptions": { "StorePath": "/Users/nathanleclaire/.docker/machine/machines/default" } }, "Name": "default", "RawDriver": "eyJWQm94TWFuYWdlciI6e30sIklQQWRkcmVzcyI6IjE5Mi4xNjguOTkuMTAwIiwiTWFjaGluZU5hbWUiOiJkZWZhdWx0IiwiU1NIVXNlciI6ImRvY2tlciIsIlNTSFBvcnQiOjU4MTQ1LCJTU0hLZXlQYXRoIjoiL1VzZXJzL25hdGhhbmxlY2xhaXJlLy5kb2NrZXIvbWFjaGluZS9tYWNoaW5lcy9kZWZhdWx0L2lkX3JzYSIsIlN0b3JlUGF0aCI6Ii9Vc2Vycy9uYXRoYW5sZWNsYWlyZS8uZG9ja2VyL21hY2hpbmUiLCJTd2FybU1hc3RlciI6ZmFsc2UsIlN3YXJtSG9zdCI6InRjcDovLzAuMC4wLjA6MzM3NiIsIlN3YXJtRGlzY292ZXJ5IjoiIiwiQ1BVIjoxLCJNZW1vcnkiOjEwMjQsIkRpc2tTaXplIjoyMDAwMCwiQm9vdDJEb2NrZXJVUkwiOiIiLCJCb290MkRvY2tlckltcG9ydFZNIjoiIiwiSG9zdE9ubHlDSURSIjoiMTkyLjE2OC45OS4xLzI0IiwiSG9zdE9ubHlOaWNUeXBlIjoiODI1NDBFTSIsIkhvc3RPbmx5UHJvbWlzY01vZGUiOiJkZW55IiwiTm9TaGFyZSI6ZmFsc2V9" }`), expectedHostAfter: &Host{ ConfigVersion: 3, HostOptions: &Options{ AuthOptions: &auth.Options{ StorePath: "/Users/nathanleclaire/.docker/machine/machines/default", }, }, Name: "default", DriverName: "virtualbox", RawDriver: []byte(`{"MachineName": "default"}`), Driver: &RawDataDriver{ Data: []byte(`{"MachineName": "default"}`), // TODO (nathanleclaire): The "." argument here is a already existing // bug (or at least likely to cause them in the future) and most // likely should be "/Users/nathanleclaire/.docker/machine" // // These default StorePath settings get over-written when we // instantiate the plugin driver, but this seems entirely incidental. Driver: none.NewDriver("default", "."), }, }, expectedMigrationPerformed: false, expectedMigrationError: nil, }, { description: "Config version 4 (from the FUTURE) on disk", hostBefore: &Host{ Name: "default", }, rawData: []byte(`{ "ConfigVersion": 4, "Driver": {"MachineName": "default"}, "DriverName": "virtualbox", "HostOptions": { "Driver": "", "Memory": 0, "Disk": 0, "AuthOptions": { "StorePath": "/Users/nathanleclaire/.docker/machine/machines/default" } }, "Name": "default" }`), expectedHostAfter: nil, expectedMigrationPerformed: false, expectedMigrationError: errConfigFromFuture, }, { description: "Config version 3 load WITHOUT any existing RawDriver field on disk", hostBefore: &Host{ Name: "default", }, rawData: []byte(`{ "ConfigVersion": 3, "Driver": {"MachineName": "default"}, "DriverName": "virtualbox", "HostOptions": { "Driver": "", "Memory": 0, "Disk": 0, "AuthOptions": { "StorePath": "/Users/nathanleclaire/.docker/machine/machines/default" } }, "Name": "default" }`), expectedHostAfter: &Host{ ConfigVersion: 3, HostOptions: &Options{ AuthOptions: &auth.Options{ StorePath: "/Users/nathanleclaire/.docker/machine/machines/default", }, }, Name: "default", DriverName: "virtualbox", RawDriver: []byte(`{"MachineName": "default"}`), Driver: &RawDataDriver{ Data: []byte(`{"MachineName": "default"}`), // TODO: See note above. Driver: none.NewDriver("default", "."), }, }, expectedMigrationPerformed: false, expectedMigrationError: nil, }, { description: "Config version 2 load and migrate. Ensure StorePath gets set properly.", hostBefore: &Host{ Name: "default", }, rawData: []byte(`{ "ConfigVersion": 2, "Driver": {"MachineName": "default"}, "DriverName": "virtualbox", "HostOptions": { "Driver": "", "Memory": 0, "Disk": 0, "AuthOptions": { "StorePath": "/Users/nathanleclaire/.docker/machine/machines/default" } }, "StorePath": "/Users/nathanleclaire/.docker/machine/machines/default", "Name": "default" }`), expectedHostAfter: &Host{ ConfigVersion: 3, HostOptions: &Options{ AuthOptions: &auth.Options{ StorePath: "/Users/nathanleclaire/.docker/machine/machines/default", }, }, Name: "default", DriverName: "virtualbox", RawDriver: []byte(`{"MachineName":"default","StorePath":"/Users/nathanleclaire/.docker/machine"}`), Driver: &RawDataDriver{ Data: []byte(`{"MachineName":"default","StorePath":"/Users/nathanleclaire/.docker/machine"}`), Driver: none.NewDriver("default", "/Users/nathanleclaire/.docker/machine"), }, }, expectedMigrationPerformed: true, expectedMigrationError: nil, }, } for _, tc := range testCases { actualHostAfter, actualMigrationPerformed, actualMigrationError := MigrateHost(tc.hostBefore, tc.rawData) assert.Equal(t, tc.expectedHostAfter, actualHostAfter) assert.Equal(t, tc.expectedMigrationPerformed, actualMigrationPerformed) assert.Equal(t, tc.expectedMigrationError, actualMigrationError) } }
func main() { plugin.RegisterDriver(none.NewDriver("", "")) }