コード例 #1
0
func MigrateHost(h *Host, data []byte) (*Host, bool, error) {
	var (
		migrationPerformed = true
	)

	migratedHostMetadata, err := getMigratedHostMetadata(data)
	if err != nil {
		return &Host{}, false, err
	}

	// Don't need to specify store path here since it will be read from the data.
	driver, err := drivermaker.NewDriver(migratedHostMetadata.DriverName, h.Name, "")
	if err != nil {
		return &Host{}, false, err
	}

	for h.ConfigVersion = migratedHostMetadata.ConfigVersion; h.ConfigVersion <= version.ConfigVersion; h.ConfigVersion++ {
		switch h.ConfigVersion {
		case 0:
			hostV0 := &HostV0{
				Driver: driver,
			}
			if err := json.Unmarshal(data, &hostV0); err != nil {
				return &Host{}, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 0: %s", err)
			}
			h = MigrateHostV0ToHostV1(hostV0)
		default:
			migrationPerformed = false
		}
	}

	h.Driver = driver
	if err := json.Unmarshal(data, &h); err != nil {
		return &Host{}, migrationPerformed, fmt.Errorf("Error unmarshalling most recent host version: %s", err)
	}

	return h, migrationPerformed, nil
}
コード例 #2
0
func main() {
	if len(os.Args) != 2 {
		bail()
	}

	libmachine.SetDebug(true)

	errR, errW := io.Pipe()
	errTracker := &FancyTracker{
		r: errR,
		w: os.Stderr,
	}

	outR, outW := io.Pipe()
	outTracker := &FancyTracker{
		r: outR,
		w: os.Stdout,
	}

	go errTracker.Track()
	go outTracker.Track()

	dmlog.SetOutWriter(errW)
	dmlog.SetErrWriter(outW)

	store := libmachine.GetDefaultStore()
	store.Path = "./store"

	hostName := "mobydick"

	data, err := ioutil.ReadFile("moby.yml")
	if err != nil {
		log.Fatal(err)
	}

	switch os.Args[1] {
	case "up":
		driver, err := drivermaker.NewDriver("digitalocean", hostName, "./store")
		if err != nil {
			log.Fatal(err)
		}

		h, err := store.NewHost(driver)
		if err != nil {
			log.Fatal(err)
		}

		if err := yaml.Unmarshal(data, h); err != nil {
			log.Fatal(err)
		}

		castedDriver, ok := driver.(*digitalocean.Driver)
		if !ok {
			log.Fatal("Fatal error, shoud be able to cast to driver type \"digitalocean\".")
		}

		driverWrapper := DriverWrapper{castedDriver}

		if err := yaml.Unmarshal(data, &driverWrapper); err != nil {
			log.Fatal(err)
		}

		h.Driver = driverWrapper.DriverOptions
		spew.Dump(h)

		if err := libmachine.Create(store, h); err != nil {
			log.Fatal(err)
		}
	case "apply":
		h, err := store.Get(hostName)
		if err != nil {
			log.Fatal(err)
		}

		spew.Dump(h)

		if err := yaml.Unmarshal(data, h); err != nil {
			log.Fatal(err)
		}

		if err := h.Provision(); err != nil {
			log.Fatal(err)
		}
	default:
		bail()
	}

}