diff --git a/cmd/root.go b/cmd/root.go index 83793fb..c7854dd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "os" - "os/exec" "strings" "github.com/golang/glog" @@ -50,8 +49,8 @@ func init() { // podmanRemove kills and removes a container func podmanRemove(cid string) { - exec.Command("podman", "kill", cid).Run() - exec.Command("podman", "rm", "-f", cid).Run() + utils.RunIgnoreErr("podman", "kill", cid) + utils.RunIgnoreErr("podman", "rm", "-f", cid) } // getDefaultDeployment uses rpm-ostree status --json to get the current deployment @@ -71,9 +70,8 @@ func getDefaultDeployment() types.RpmOstreeDeployment { return rosState.Deployments[0] } -// Execute runs the command -func Execute(cmd *cobra.Command, args []string) { - container := args[0] +// Potentially rebases system if not already rebased. +func pullAndRebase(container string) (imgid string, changed bool) { defaultDeployment := getDefaultDeployment() previousPivot := "" @@ -89,14 +87,11 @@ func Execute(cmd *cobra.Command, args []string) { var imagedata types.ImageInspection json.Unmarshal([]byte(output), &imagedata) - imgid := fmt.Sprintf("%s@%s", imagedata.Name, imagedata.Digest) + imgid = fmt.Sprintf("%s@%s", imagedata.Name, imagedata.Digest) glog.Infof("Resolved to: %s", imgid) if previousPivot == imgid { - glog.Info("Already at target pivot; exiting...") - if (exit_77) { - os.Exit(77) - } + changed = false return } @@ -140,13 +135,28 @@ func Execute(cmd *cobra.Command, args []string) { // Kill our dummy container podmanRemove(types.PivotName) + changed = true + return +} + +// Execute runs the command +func Execute(cmd *cobra.Command, args []string) { + container := args[0] + imgid, changed := pullAndRebase(container) + // By default, delete the image. if !keep { - utils.Run("podman", "rmi", imgid) + // Related: https://github.com/containers/libpod/issues/2234 + utils.RunIgnoreErr("podman", "rmi", imgid) } + if !changed { + glog.Info("Already at target pivot; exiting...") + if (exit_77) { + os.Exit(77) + } // Reboot the machine if asked to do so - if reboot { + } else if reboot { utils.Run("systemctl", "reboot") } } diff --git a/utils/run.go b/utils/run.go index b936df2..731a239 100644 --- a/utils/run.go +++ b/utils/run.go @@ -62,6 +62,12 @@ func Run(command string, args ...string) { } } +func RunIgnoreErr(command string, args ...string) { + if _, err := runImpl(false, command, args...); err != nil { + glog.Warningf("(ignored) %s: %s", command, err) + } +} + // Like Run(), but get the output as a string func RunGetOut(command string, args ...string) string { var err error