Ejemplo n.º 1
0
// runUpdate
func runUpdate() error {

	fmt.Printf(stylish.Bullet("Updating nanobox..."))

	// get the path of the current executing CLI
	exe, err := osext.Executable()
	if err != nil {
		return err
	}

	//

	prog := filepath.Base(os.Args[0])
	tmpDir := config.Root + "/tmp"
	tmpFile := tmpDir + "/" + prog

	// create a tmp dir to download the new cli to; don't care about the error here
	// because if the tmp dir already exists we'll just use it
	os.Mkdir(tmpDir, 0755)

	// create a tmp cli in tmp dir
	cli, err := os.Create(tmpFile)
	if err != nil {
		return err
	}
	defer cli.Close()

	// download the new cli
	dl := fmt.Sprintf("https://s3.amazonaws.com/tools.nanobox.io/cli/%v/%v/%v", runtime.GOOS, runtime.GOARCH, prog)
	fileutil.Progress(dl, cli)

	// make new CLI executable
	if err := os.Chmod(tmpFile, 0755); err != nil {
		return err
	}

	// move new executable over current CLI's location
	if err = os.Rename(tmpFile, exe); err != nil {
		return err
	}

	// if the new CLI fails to execute, just print a generic message and return
	out, err := exec.Command(exe, "-v").Output()
	if err != nil {
		fmt.Printf(stylish.SubBullet("[√] Update successful"))
		return nil
	}

	fmt.Printf(stylish.SubBullet("[√] Now running %s", string(out)))

	// update the .update file
	return touchUpdate()
}
Ejemplo n.º 2
0
// runUpdate attemtps to update using the updater; if it's not available nanobox
// will download it and then run it.
func runUpdate() error {

	//
	path, err := osext.Executable()
	if err != nil {
		return err
	}

	// get the directory of the current executing cli
	dir := filepath.Dir(path)

	// see if the updater is available on PATH
	if _, err := exec.LookPath("nanobox-update"); err != nil {

		tmpFile := filepath.Join(config.TmpDir, "nanobox-update")

		// create a tmp updater in tmp dir
		f, err := os.Create(tmpFile)
		if err != nil {
			return err
		}
		defer f.Close()

		// the updateder is not available and needs to be downloaded
		dl := fmt.Sprintf("https://s3.amazonaws.com/tools.nanobox.io/updaters/%s/%s/nanobox-update", config.OS, config.ARCH)

		fmt.Printf("'nanobox-update' not found. Downloading from %s\n", dl)

		fileutil.Progress(dl, f)

		// ensure updater download matches the remote md5; if the download fails for any
		// reason this md5 should NOT match.
		md5 := fmt.Sprintf("https://s3.amazonaws.com/tools.nanobox.io/updaters/%s/%s/nanobox-udpate.md5", config.OS, config.ARCH)
		if _, err = util.MD5sMatch(tmpFile, md5); err != nil {
			return err
		}

		// make new updater executable
		if err := os.Chmod(tmpFile, 0755); err != nil {
			return err
		}

		// move updater to the same location as the cli
		if err = os.Rename(tmpFile, filepath.Join(dir, "nanobox-update")); err != nil {
			return err
		}
	}

	cmd := exec.Command(filepath.Join(dir, "nanobox-update"), "-o", filepath.Base(path))

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	// run the updater
	if err := cmd.Run(); err != nil {
		config.Fatal("[commands/update] exec.Command().Run() failed", err.Error())
	}

	// update the .update file
	return touchUpdate()
}