~sergiusens/snappy/tools-parted

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2014-2015 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"

	"launchpad.net/snappy/i18n"
	"launchpad.net/snappy/logger"
	"launchpad.net/snappy/progress"
	"launchpad.net/snappy/snappy"
)

type cmdUpdate struct {
	DisableGC  bool `long:"no-gc"`
	AutoReboot bool `long:"automatic-reboot"`
}

func init() {
	arg, err := parser.AddCommand("update",
		i18n.G("Update all installed parts"),
		i18n.G("Ensures system is running with latest parts"),
		&cmdUpdate{})
	if err != nil {
		logger.Panicf("Unable to update: %v", err)
	}
	addOptionDescription(arg, "no-gc", i18n.G("Do not clean up old versions of the package."))
	addOptionDescription(arg, "automatic-reboot", i18n.G("Reboot if necessary to be on the latest running system."))
}

const (
	shutdownCmd     = "/sbin/shutdown"
	shutdownTimeout = "+10"
)

var shutdownMsg = i18n.G("snappy autopilot triggered a reboot to boot into an up to date system -- temprorarily disable the reboot by running 'sudo shutdown -c'")

func (x *cmdUpdate) Execute(args []string) (err error) {
	return withMutex(x.doUpdate)
}

func (x *cmdUpdate) doUpdate() error {
	// FIXME: handle (more?) args
	flags := snappy.DoInstallGC
	if x.DisableGC {
		flags = 0
	}

	updates, err := snappy.Update(flags, progress.MakeProgressBar())
	if err != nil {
		return err
	}

	if len(updates) > 0 {
		showVerboseList(updates, os.Stdout)
	}

	if x.AutoReboot {
		installed, err := snappy.ListInstalled()
		if err != nil {
			return err
		}

		var rebootTriggers []string
		for _, part := range installed {
			if part.NeedsReboot() {
				rebootTriggers = append(rebootTriggers, part.Name())
			}
		}

		if len(rebootTriggers) != 0 {
			// TRANSLATORS: the %s shows a comma separated list
			//              of package names
			fmt.Printf(i18n.G("Rebooting to satisfy updates for %s\n"), strings.Join(rebootTriggers, ", "))
			cmd := exec.Command(shutdownCmd, shutdownTimeout, "-r", shutdownMsg)
			if out, err := cmd.CombinedOutput(); err != nil {
				return fmt.Errorf("failed to auto reboot: %s", out)
			}
		}
	}

	return nil
}