Example #1
0
func downloadFileWithChecksum(url string, destPath string) {
	fileName := filepath.Base(destPath)
	tmpPath := filepath.Join(dvmDir, ".tmp", fileName)
	downloadFile(url, tmpPath)

	checksumURL := url + ".sha256"
	checksumPath := filepath.Join(dvmDir, ".tmp", (fileName + ".sh256"))
	downloadFile(checksumURL, checksumPath)

	checksum.CompareChecksum(tmpPath, checksumPath)
	isValid, err := checksum.CompareChecksum(tmpPath, checksumPath)
	if err != nil {
		die("Unable to calculate checksum of %s.", err, retCodeRuntimeError, tmpPath)
	}
	if !isValid {
		die("The checksum of %s failed to match %s.", nil, retCodeRuntimeError, tmpPath, checksumPath)
	}

	// Copy to final location, if different
	if destPath != tmpPath {
		ensureParentDirectoryExists(destPath)
		err = os.Rename(tmpPath, destPath)
		if err != nil {
			die("Unable to copy %s to %s.", err, retCodeRuntimeError, tmpPath, destPath)
		}
	}

	// Cleanup temp files
	if err = os.Remove(checksumPath); err != nil {
		writeWarning("Unable to remove temporary file: %s.", checksumPath)
	}
}
Example #2
0
func install(version string) {
	if version == "" {
		version = getDockerVersionVar()
	}

	if version == "" {
		die("The install command requires that a version is specified or the DOCKER_VERSION environment variable is set.", nil, retCodeInvalidArgument)
	}

	if !versionExists(version) {
		die("Version %s not found - try `dvm ls-remote` to browse available versions.", nil, retCodeInvalidOperation, version)
	}

	versionDir := getVersionDir(version)

	if version == "experimental" && pathExists(versionDir) {
		// Always install latest of experimental build
		err := os.RemoveAll(versionDir)
		if err != nil {
			die("Unable to remove experimental version at %s.", err, retCodeRuntimeError, versionDir)
		}
	}

	if _, err := os.Stat(versionDir); err == nil {
		writeWarning("%s is already installed", version)
		use(version)
		return
	}

	writeInfo("Installing %s...", version)

	// Download to a temporary location
	url := buildDownloadURL(version)
	tmpPath := filepath.Join(getDvmDir(), ".tmp/docker", version, getBinaryName())
	downloadFile(url, tmpPath)

	// Download Checksum to verify the binary
	checksumURL := url + ".sha256"
	checksumPath := tmpPath + ".sha256"
	downloadFile(checksumURL, checksumPath)

	// Verify checksum
	isValid, err := checksum.CompareChecksum(tmpPath, checksumPath)
	if err != nil {
		die("Unable to calculate checksum of %s.", err, retCodeRuntimeError, tmpPath)
	}
	if !isValid {
		die("The checksum of %s failed to match %s.", nil, retCodeRuntimeError, tmpPath, checksumPath)
	}

	// Copy to final location
	binaryPath := filepath.Join(getDvmDir(), "bin/docker", version, getBinaryName())
	ensureParentDirectoryExists(binaryPath)
	err = os.Rename(tmpPath, binaryPath)
	if err != nil {
		die("Unable to copy %s to %s.", err, retCodeRuntimeError, tmpPath, binaryPath)
	}

	// Cleanup temp files
	tmpDir := filepath.Dir(tmpPath)
	writeDebug("Cleaning up temporary installation files in %s.", tmpDir)
	if err = os.RemoveAll(tmpDir); err != nil {
		writeWarning("Unable to remove temporary files in %s.", tmpDir)
	}

	writeDebug("Installed Docker %s to %s.", version, binaryPath)
	use(version)
}