Skip to content

Commit

Permalink
Re-enabled docker pull on kdk ssh if image not present
Browse files Browse the repository at this point in the history
  • Loading branch information
dcwangmit01 authored and Joshua Dotson committed Sep 26, 2018
1 parent 736e55c commit 807db7e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DOCKER_REGISTRY ?=
IMAGE_PREFIX ?= ciscosso
SHORT_NAME ?= kdk
TARGETS ?= darwin/amd64 linux/amd64 windows/amd64
VERSION := $(shell ./scripts/cicd.sh version)
VERSION ?= $(shell ./scripts/cicd.sh version)
BASE_IMAGE ?= $(IMAGE_PREFIX)/$(SHORT_NAME)
NEW_IMAGE_TAG ?= $(BASE_IMAGE):$(VERSION)

Expand Down
2 changes: 1 addition & 1 deletion cmd/kdk/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var pullCmd = &cobra.Command{
Long: `Pull the latest/configured KDK docker image`,
Run: func(cmd *cobra.Command, args []string) {
log.Info("Pulling KDK image. This may take a moment...")
if err := kdk.Pull(&CurrentKdkEnvConfig); err != nil {
if err := kdk.Pull(&CurrentKdkEnvConfig, true); err != nil {
log.WithField("error", err).Fatal("Failed to pull KDK image")
}
log.Info("Successfully pulled KDK image.")
Expand Down
15 changes: 13 additions & 2 deletions pkg/kdk/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ import (
"github.com/docker/docker/api/types"
)

func Pull(cfg *KdkEnvConfig) error {
return pullImage(cfg, cfg.ImageCoordinates())
func Pull(cfg *KdkEnvConfig, force bool) error {
tag := cfg.ConfigFile.AppConfig.ImageTag
if hasKdkImageWithTag(cfg, tag) {
if force {
log.WithField("tag", tag).Info("Re-pulling existing KDK Image")
return pullImage(cfg, cfg.ImageCoordinates())
}
} else {
log.WithField("tag", tag).Info("Pulling missing KDK Image")
return pullImage(cfg, cfg.ImageCoordinates())
}
log.WithField("tag", tag).Debug("Not pulling already present KDK Image")
return nil
}

func pullImage(cfg *KdkEnvConfig, imageCoordinates string) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/kdk/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Ssh(cfg KdkEnvConfig) {
// if KDK container is not running, start it and provision KDK user
if !kdkRunning {
log.Info("KDK is not currently running. Starting...")
Pull(&cfg, false)
Up(cfg)
Provision(cfg)
}
Expand Down
51 changes: 30 additions & 21 deletions pkg/kdk/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -49,9 +50,11 @@ func WarnIfUpdateAvailable(cfg *KdkEnvConfig) {
if needsUpdateBin() || needsUpdateImage(cfg) || needsUpdateConfig(cfg) {
log.Warn("Upgrade Available\n" + strings.Join([]string{
"***************************************",
"The installed KDK version is out of date",
" Current: " + Version,
" Latest : " + latestReleaseVersion,
"Some KDK components are out of date",
" Latest Version: " + latestReleaseVersion,
" Binary Version: " + Version,
" Config Version: " + cfg.ConfigFile.AppConfig.ImageTag,
" Container Present at Config Version: " + strconv.FormatBool(!needsUpdateImage(cfg)),
"",
"Please upgrade the KDK with the commands:",
" " + sudo + "kdk update",
Expand All @@ -69,19 +72,7 @@ func needsUpdateBin() bool {

// check if kdk image needs to be updated
func needsUpdateImage(cfg *KdkEnvConfig) bool {
kdkImages := getKdkImages(cfg)

for _, image := range kdkImages {
var tags []string
for _, tag := range image.RepoTags {
imageTag := strings.Split(tag, ":")[1]
tags = append(tags, imageTag)
}
if utils.Contains(tags, latestReleaseVersion) {
return false
}
}
return true
return !hasKdkImageWithTag(cfg, latestReleaseVersion)
}

// check if kdk config needs to be updated
Expand All @@ -104,7 +95,6 @@ func Update(cfg *KdkEnvConfig) {
log.Warn("Upgrade Unavailable. Already at latest versions")
return
}
log.Info("Upgrading KDK...\n")

if needsUpdateBin() {
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") && os.Geteuid() != 0 {
Expand All @@ -121,13 +111,13 @@ func Update(cfg *KdkEnvConfig) {
}

if needsUpdateImage(cfg) {
log.Info("Updating KDK docker image")
log.Info("Updating KDK container image")
err := pullImage(cfg, cfg.ConfigFile.AppConfig.ImageRepository+":"+latestReleaseVersion)
if err != nil {
log.WithField("error", err).Fatal("Failed to update KDK image")
}
} else {
log.Info("Updating KDK docker image skipped: Already at latest version")
log.Info("Updating KDK container image skipped: Already at latest version")
}

if needsUpdateConfig(cfg) {
Expand Down Expand Up @@ -193,25 +183,28 @@ func updateBin() error {
log.WithField("error", err).WithField("file", kdkBinFile+".new").Fatal("Failed to chmod file")
return err
}
log.WithField("file", kdkBinFile+".new").Info("Successfuly chmod'd file")
log.WithField("file", kdkBinFile+".new").Info("Successfully chmod'd file")

// remove the original bin file
err = os.Remove(kdkBinFile)
if err != nil {
log.WithField("error", err).WithField("file", kdkBinFile).Fatal("Failed to delete file")
}
log.WithField("file", kdkBinFile).Info("Successfully deleted file")

// rename the new file to be the the executable file
err = os.Rename(kdkBinFile+".new", kdkBinFile)
if err != nil {
log.WithField("error", err).WithField("fileSrc", kdkBinFile+".new").WithField("fileDst", kdkBinFile).Fatal("Failed to rename file")
}
log.WithField("fileSrc", kdkBinFile+".new").WithField("fileDst", kdkBinFile).Info("Successfully renamed file")
} else if runtime.GOOS == "windows" {
// rename the bin file to a trash location out of the way
err = os.Rename(kdkBinFile, kdkBinFileTrash)
if err != nil {
log.WithField("error", err).WithField("fileSrc", kdkBinFile).WithField("fileDst", kdkBinFileTrash).Fatal("Failed to rename file")
}
log.WithField("fileSrc", kdkBinFile).WithField("fileDst", kdkBinFileTrash).Info("Successfully renamed file")

// copy the new file next to the org binary, so it is on the same partition/filesystem
err = copyFile(kdkBinFileUnpacked, kdkBinFile)
Expand All @@ -237,7 +230,7 @@ func updateBin() error {

// update kdk image
func updateImage(cfg *KdkEnvConfig) error {
Pull(cfg)
Pull(cfg, true)
return nil
}

Expand Down Expand Up @@ -307,6 +300,22 @@ func getKdkImages(cfg *KdkEnvConfig) (out []types.ImageSummary) {
return kdkImages
}

func hasKdkImageWithTag(cfg *KdkEnvConfig, tagSearch string) bool {
kdkImages := getKdkImages(cfg)

for _, image := range kdkImages {
var tags []string
for _, tag := range image.RepoTags {
imageTag := strings.Split(tag, ":")[1]
tags = append(tags, imageTag)
}
if utils.Contains(tags, tagSearch) {
return true
}
}
return false
}

func copyFile(src, dst string) error {
// Copy the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
Expand Down

0 comments on commit 807db7e

Please sign in to comment.