Skip to content

Commit

Permalink
Implement restart command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Luckie authored and rtluckie committed Feb 28, 2019
1 parent 7a752b8 commit e73f166
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
33 changes: 33 additions & 0 deletions cmd/kdk/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright © 2018 Cisco Systems, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/cisco-sso/kdk/pkg/kdk"
"github.com/spf13/cobra"
)

var restartCmd = &cobra.Command{
Use: "restart",
Short: "Restart a running KDK container",
Long: `Restart a running KDK container`,
Run: func(cmd *cobra.Command, args []string) {
kdk.Restart(CurrentKdkEnvConfig)
},
}

func init() {
rootCmd.AddCommand(restartCmd)
}
52 changes: 52 additions & 0 deletions pkg/kdk/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright © 2018 Cisco Systems, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kdk

import (
"github.com/ghodss/yaml"
log "github.com/sirupsen/logrus"
"io/ioutil"
"strings"
)

func Restart(cfg KdkEnvConfig) {

log.Info("Restarting KDK container")

// Create snapshot of running KDK container
snapshotName, err := Snapshot(cfg)
if err != nil {
log.WithField("err", err).Fatal("Failed to create KDK image snapshot")
}

// Destroy running KDK container
Destroy(cfg)

// Save config with snapshot image tag
cfg.ConfigFile.AppConfig.ImageTag = strings.Split(snapshotName, ":")[1]
cfg.ConfigFile.ContainerConfig.Image = snapshotName
y, err := yaml.Marshal(cfg.ConfigFile)
if err != nil {
log.WithField("error", err).Error("Failed to create YAML string of configuration")
}

err = ioutil.WriteFile(cfg.ConfigPath(), y, 0600)
if err != nil {
log.WithField("error", err).Error("Failed to write new config file")
}
// Start KDK container with snapshot image
cfg.Start()
log.Info("KDK container restarted")
}
9 changes: 4 additions & 5 deletions pkg/kdk/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
package kdk

import (
"strconv"
"time"

"github.com/docker/docker/api/types"
log "github.com/sirupsen/logrus"
)

func Snapshot(cfg KdkEnvConfig) error {
snapshotName := cfg.ConfigFile.AppConfig.Name + "-" + strconv.Itoa(int(time.Now().UnixNano()))
func Snapshot(cfg KdkEnvConfig) (string, error) {
snapshotName := "ciscosso/kdk" + ":" + cfg.User() + "-" + cfg.ConfigFile.AppConfig.Name + "-" + time.Now().Format("20060102150405")
_, err := cfg.DockerClient.ContainerCommit(cfg.Ctx, cfg.ConfigFile.AppConfig.Name, types.ContainerCommitOptions{Reference: snapshotName})
if err != nil {
log.WithField("error", err).Fatal("Failed to create snapshot of KDK container")
return err
return "", err
}
log.Info("Successfully created snapshot of KDK container.", snapshotName)
return nil
return snapshotName, nil
}
5 changes: 3 additions & 2 deletions pkg/kdk/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ func WarnIfUpdateAvailable(cfg *KdkEnvConfig) {
if needsUpdateBin() || needsUpdateImage(cfg) || needsUpdateConfig(cfg) {
log.Warn("Upgrade Available\n" + strings.Join([]string{
"***************************************",
"Some KDK components are out of date",
"Some KDK components are out of date.",
"It is safe to ignore updates if you are using a custom image (from snapshot or restart).",
" Latest Version: " + latestReleaseVersion,
" Binary Version: " + Version,
" Config Version: " + cfg.ConfigFile.AppConfig.ImageTag,
" Image Tag: " + cfg.ConfigFile.AppConfig.ImageTag,
" Container Present at Config Version: " + strconv.FormatBool(!needsUpdateImage(cfg)),
"",
"Please upgrade the KDK with the commands:",
Expand Down

0 comments on commit e73f166

Please sign in to comment.