Skip to content

Commit

Permalink
Merge pull request #189 from carolynvs/use-dvm-tmp
Browse files Browse the repository at this point in the history
Use dvm tmp for docker downloads
  • Loading branch information
rgbkrk authored Jan 2, 2019
2 parents eea28e4 + 6c4e644 commit d137c0c
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 75 deletions.
18 changes: 9 additions & 9 deletions dvm-helper/dockerversion/dockerversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package dockerversion

import (
"fmt"
"log"
"net/http"
"path/filepath"
"sort"
"strings"

"github.com/Masterminds/semver"
"github.com/howtowhale/dvm/dvm-helper/internal/config"
"github.com/howtowhale/dvm/dvm-helper/internal/downloader"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -102,26 +102,26 @@ func (version Version) buildDownloadURL(mirror string, forcePrerelease bool) (ur
// version - the desired version.
// mirrorURL - optional alternate download location.
// binaryPath - full path to where the Docker client binary should be saved.
func (version Version) Download(mirrorURL string, binaryPath string, l *log.Logger) error {
err := version.download(false, mirrorURL, binaryPath, l)
func (version Version) Download(opts config.DvmOptions, binaryPath string) error {
err := version.download(false, opts, binaryPath)
if err != nil && !version.IsPrerelease() && version.shouldBeInDockerStore() {
// Docker initially publishes non-rc version versions to the test location
// and then later republishes to the stable location
// Retry stable versions against test to find "unstable" stable versions. :-)
l.Printf("Could not find a stable release for %s, checking for a test release\n", version)
retryErr := version.download(true, mirrorURL, binaryPath, l)
opts.Logger.Printf("Could not find a stable release for %s, checking for a test release\n", version)
retryErr := version.download(true, opts, binaryPath)
return errors.Wrapf(retryErr, "Attempted to fallback to downloading from the prerelease location after downloading from the stable location failed: %s", err.Error())
}
return err
}

func (version Version) download(forcePrerelease bool, mirrorURL string, binaryPath string, l *log.Logger) error {
url, archived, checksumed, err := version.buildDownloadURL(mirrorURL, forcePrerelease)
func (version Version) download(forcePrerelease bool, opts config.DvmOptions, binaryPath string) error {
url, archived, checksumed, err := version.buildDownloadURL(opts.MirrorURL, forcePrerelease)
if err != nil {
return errors.Wrapf(err, "Unable to determine the download URL for %s", version)
}

l.Printf("Checking if %s can be found at %s", version, url)
opts.Logger.Printf("Checking if %s can be found at %s", version, url)
head, err := http.Head(url)
if err != nil {
return errors.Wrapf(err, "Unable to determine if %s is a valid version", version)
Expand All @@ -130,7 +130,7 @@ func (version Version) download(forcePrerelease bool, mirrorURL string, binaryPa
return errors.Errorf("Version %s not found (%v) - try `dvm ls-remote` to browse available versions", version, head.StatusCode)
}

d := downloader.New(l)
d := downloader.New(opts)
binaryName := filepath.Base(binaryPath)

if archived {
Expand Down
10 changes: 5 additions & 5 deletions dvm-helper/dockerversion/dockerversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"path/filepath"
"testing"

"log"

"github.com/howtowhale/dvm/dvm-helper/internal/config"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -212,10 +211,11 @@ func TestVersion_BuildDownloadURL(t *testing.T) {
func TestVersion_DownloadEdgeRelease(t *testing.T) {
version := Parse("edge")
tempDir, _ := ioutil.TempDir("", "dvmtest")
destPath := filepath.Join(tempDir, "docker")
opts := config.NewDvmOptions()
opts.DvmDir = filepath.Join(tempDir, ".dvm")
destPath := filepath.Join(opts.DvmDir, "docker")

l := log.New(ioutil.Discard, "", log.LstdFlags)
err := version.Download("", destPath, l)
err := version.Download(opts, destPath)
if err != nil {
t.Fatalf("%#v", err)
}
Expand Down
68 changes: 32 additions & 36 deletions dvm-helper/dvm-helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,24 @@ import (
"github.com/fatih/color"
"github.com/google/go-github/github"
"github.com/howtowhale/dvm/dvm-helper/dockerversion"
"github.com/howtowhale/dvm/dvm-helper/internal/config"
"github.com/howtowhale/dvm/dvm-helper/url"
"github.com/pkg/errors"
"github.com/ryanuber/go-glob"
"golang.org/x/oauth2"
)

// These are global command line variables
var shell string
var dvmDir string
var mirrorURL string
var githubUrlOverride string
var debug bool
var silent bool
var opts = config.NewDvmOptions()

// This is a nasty global state flag that we flip. Bad Carolyn.
var useAfterInstall bool
var token string
var includePrereleases bool

// These are set during the build
var dvmVersion string
var dvmCommit string
var upgradeDisabled string // Allow package managers like homebrew to disable in-place upgrades
var githubUrlOverride string

const (
retCodeInvalidArgument = 127
Expand Down Expand Up @@ -274,20 +271,26 @@ func makeCliApp() *cli.App {
func setGlobalVars(c *cli.Context) {
useAfterInstall = true

debug = c.GlobalBool("debug")
token = c.GlobalString("github-token")
shell = c.GlobalString("shell")
opts.Debug = c.GlobalBool("debug")
if opts.Debug {
opts.Logger = log.New(color.Output, "", log.LstdFlags)
} else {
opts.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
}

opts.Token = c.GlobalString("github-token")
opts.Shell = c.GlobalString("shell")
validateShellFlag()

silent = c.GlobalBool("silent")
mirrorURL = c.String("mirror-url")
includePrereleases = c.Bool("pre")
opts.Silent = c.GlobalBool("silent")
opts.MirrorURL = c.String("mirror-url")
opts.IncludePrereleases = c.Bool("pre")

dvmDir = c.GlobalString("dvm-dir")
if dvmDir == "" {
dvmDir = filepath.Join(getUserHomeDir(), ".dvm")
opts.DvmDir = c.GlobalString("dvm-dir")
if opts.DvmDir == "" {
opts.DvmDir = filepath.Join(getUserHomeDir(), ".dvm")
}
writeDebug("The dvm home directory is: %s", dvmDir)
writeDebug("The dvm home directory is: %s", opts.DvmDir)
}

func detect() {
Expand Down Expand Up @@ -429,21 +432,14 @@ func install(version dockerversion.Version) {

func downloadRelease(version dockerversion.Version) {
destPath := filepath.Join(getVersionDir(version), getBinaryName())
err := version.Download(mirrorURL, destPath, getDebugLogger())
err := version.Download(opts, destPath)
if err != nil {
die("", err, retCodeRuntimeError)
}

writeDebug("Downloaded Docker %s to %s", version, destPath)
}

func getDebugLogger() *log.Logger {
if debug {
return log.New(color.Output, "", log.LstdFlags)
}
return log.New(ioutil.Discard, "", log.LstdFlags)
}

func uninstall(version dockerversion.Version) {
current, _ := getCurrentDockerVersion()
if current.Equals(version) {
Expand Down Expand Up @@ -561,7 +557,7 @@ func getAliases() map[string]string {
}

func getAliasPath(alias string) string {
return filepath.Join(dvmDir, "alias", alias)
return filepath.Join(opts.DvmDir, "alias", alias)
}

func getBinaryName() string {
Expand All @@ -587,14 +583,14 @@ func writeEnvironmentVariableScript(name string) {

func buildDvmOutputScriptPath() string {
var fileExtension string
if shell == "powershell" {
if opts.Shell == "powershell" {
fileExtension = "ps1"
} else if shell == "cmd" {
} else if opts.Shell == "cmd" {
fileExtension = "cmd"
} else { // default to bash
fileExtension = "sh"
}
return filepath.Join(dvmDir, ".tmp", ("dvm-output." + fileExtension))
return filepath.Join(opts.DvmDir, ".tmp", "dvm-output."+fileExtension)
}

func removePreviousDockerVersionFromPath() {
Expand Down Expand Up @@ -712,7 +708,7 @@ func getDockerVersion(dockerPath string, includeBuild bool) (dockerversion.Versi
}

func listRemote(prefix string) {
versions := getAvailableVersions(prefix, includePrereleases)
versions := getAvailableVersions(prefix, opts.IncludePrereleases)
for _, version := range versions {
writeInfo(version.String())
}
Expand Down Expand Up @@ -768,7 +764,7 @@ func getAvailableVersions(pattern string, includePrereleases bool) []dockerversi
}

writeDebug("Retrieving Docker releases")
stableVersions, err := dockerversion.ListVersions(mirrorURL, dockerversion.Stable)
stableVersions, err := dockerversion.ListVersions(opts.MirrorURL, dockerversion.Stable)
if err != nil {
die("", err, retCodeRuntimeError)
}
Expand All @@ -780,7 +776,7 @@ func getAvailableVersions(pattern string, includePrereleases bool) []dockerversi

if includePrereleases {
writeDebug("Retrieving Docker pre-releases")
prereleaseVersions, err := dockerversion.ListVersions(mirrorURL, dockerversion.Test)
prereleaseVersions, err := dockerversion.ListVersions(opts.MirrorURL, dockerversion.Test)
if err != nil {
die("", err, retCodeRuntimeError)
}
Expand Down Expand Up @@ -867,7 +863,7 @@ func isUpgradeAvailable() (bool, string) {
}

func getVersionsDir() string {
return filepath.Join(dvmDir, "bin", "docker")
return filepath.Join(opts.DvmDir, "bin", "docker")
}

func getVersionDir(version dockerversion.Version) string {
Expand All @@ -883,8 +879,8 @@ func getDockerVersionVar() string {
}

func buildGithubClient() *github.Client {
if token != "" {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
if opts.Token != "" {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: opts.Token})
httpClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
return github.NewClient(httpClient)
}
Expand Down
6 changes: 3 additions & 3 deletions dvm-helper/dvm-helper.nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
const binaryFileExt string = ""

func upgradeSelf(version string) {
d := downloader.New(getDebugLogger())
d := downloader.New(opts)

binaryURL := buildDvmReleaseURL(version, dvmOS, dvmArch, "dvm-helper")
binaryPath := filepath.Join(dvmDir, "dvm-helper", "dvm-helper")
binaryPath := filepath.Join(opts.DvmDir, "dvm-helper", "dvm-helper")
err := d.DownloadFileWithChecksum(binaryURL, binaryPath)
if err != nil {
die("", err, retCodeRuntimeError)
}

scriptURL := buildDvmReleaseURL(version, "dvm.sh")
scriptPath := filepath.Join(dvmDir, "dvm.sh")
scriptPath := filepath.Join(opts.DvmDir, "dvm.sh")
err = d.DownloadFile(scriptURL, scriptPath)
if err != nil {
die("", err, retCodeRuntimeError)
Expand Down
16 changes: 8 additions & 8 deletions dvm-helper/dvm-helper.windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ const dvmOS string = "Windows"
const binaryFileExt string = ".exe"

func upgradeSelf(version string) {
d := downloader.New(getDebugLogger())
d := downloader.New(opts)

binaryURL := buildDvmReleaseURL(version, dvmOS, dvmArch, "dvm-helper.exe")
binaryPath := filepath.Join(dvmDir, ".tmp", "dvm-helper.exe")
binaryPath := filepath.Join(opts.DvmDir, ".tmp", "dvm-helper.exe")
err := d.DownloadFileWithChecksum(binaryURL, binaryPath)
if err != nil {
die("", err, retCodeRuntimeError)
}

psScriptURL := buildDvmReleaseURL(version, "dvm.ps1")
psScriptPath := filepath.Join(dvmDir, "dvm.ps1")
psScriptPath := filepath.Join(opts.DvmDir, "dvm.ps1")
err = d.DownloadFile(psScriptURL, psScriptPath)
if err != nil {
die("", err, retCodeRuntimeError)
}

cmdScriptURL := buildDvmReleaseURL(version, "dvm.cmd")
cmdScriptPath := filepath.Join(dvmDir, "dvm.cmd")
cmdScriptPath := filepath.Join(opts.DvmDir, "dvm.cmd")
err = d.DownloadFile(cmdScriptURL, cmdScriptPath)
if err != nil {
die("", err, retCodeRuntimeError)
Expand All @@ -43,11 +43,11 @@ func upgradeSelf(version string) {

func writeUpgradeScript() {
scriptPath := buildDvmOutputScriptPath()
tmpBinaryPath := filepath.Join(dvmDir, ".tmp", "dvm-helper.exe")
binaryPath := filepath.Join(dvmDir, "dvm-helper", "dvm-helper.exe")
tmpBinaryPath := filepath.Join(opts.DvmDir, ".tmp", "dvm-helper.exe")
binaryPath := filepath.Join(opts.DvmDir, "dvm-helper", "dvm-helper.exe")

var contents string
if shell == "powershell" {
if opts.Shell == "powershell" {
contents = fmt.Sprintf("cp -force '%s' '%s'", tmpBinaryPath, binaryPath)
} else { // cmd
contents = fmt.Sprintf("cp /Y '%s' '%s'", tmpBinaryPath, binaryPath)
Expand All @@ -63,7 +63,7 @@ func getCleanPathRegex() string {
}

func validateShellFlag() {
if shell != "powershell" && shell != "cmd" {
if opts.Shell != "powershell" && opts.Shell != "cmd" {
die("The --shell flag or SHELL environment variable must be set when running on Windows. Available values are powershell and cmd.", nil, retCodeInvalidArgument)
}
}
Expand Down
23 changes: 23 additions & 0 deletions dvm-helper/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import (
"io/ioutil"
"log"
)

type DvmOptions struct {
DvmDir string
MirrorURL string
Token string
Shell string
Debug bool
Silent bool
IncludePrereleases bool
Logger *log.Logger
}

func NewDvmOptions() DvmOptions {
return DvmOptions{
Logger: log.New(ioutil.Discard, "", log.LstdFlags),
}
}
15 changes: 6 additions & 9 deletions dvm-helper/internal/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package downloader

import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -11,6 +10,7 @@ import (
"strings"

"github.com/howtowhale/dvm/dvm-helper/checksum"
"github.com/howtowhale/dvm/dvm-helper/internal/config"
"github.com/pivotal-golang/archiver/extractor"
"github.com/pkg/errors"
)
Expand All @@ -23,14 +23,11 @@ type Client struct {

// New creates a downloader client.
// l - optional logger for debug output
func New(l *log.Logger) Client {
if l == nil {
l = log.New(ioutil.Discard, "", log.LstdFlags)
}
tmpDir, _ := ioutil.TempDir("", "dvm")

func New(opts config.DvmOptions) Client {
return Client{
log: l, tmp: tmpDir}
log: opts.Logger,
tmp: filepath.Join(opts.DvmDir, ".tmp"),
}
}

func (d Client) ensureParentDirectoryExists(path string) error {
Expand All @@ -52,7 +49,7 @@ func (d Client) DownloadFile(url string, destPath string) error {
defer destFile.Close()
os.Chmod(destPath, 0755)

d.log.Printf("Downloading %s\n", url)
d.log.Printf("Downloading %s to %s\n", url, destPath)

response, err := http.Get(url)
if err != nil {
Expand Down
Loading

0 comments on commit d137c0c

Please sign in to comment.