2014-05-07 04:44:18 +04:00
|
|
|
package initialize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
|
|
|
)
|
|
|
|
|
|
|
|
const locksmithUnit = "locksmithd.service"
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
type UpdateConfig map[string]string
|
2014-05-07 04:44:18 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
func (uc UpdateConfig) strategy() string {
|
|
|
|
s, _ := uc["reboot-strategy"]
|
|
|
|
return s
|
|
|
|
}
|
2014-05-14 21:46:30 +04:00
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
// File creates an `/etc/coreos/update.conf` file with the requested
|
|
|
|
// strategy, by either rewriting the existing file on disk, or starting
|
|
|
|
// from `/usr/share/coreos/update.conf`
|
|
|
|
func (uc UpdateConfig) File(root string) (*system.File, error) {
|
|
|
|
|
|
|
|
// If no reboot-strategy is set, we don't need to generate a new config
|
|
|
|
if _, ok := uc["reboot-strategy"]; !ok {
|
|
|
|
return nil, nil
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
var out string
|
|
|
|
|
|
|
|
etcUpdate := path.Join(root, "etc", "coreos", "update.conf")
|
|
|
|
usrUpdate := path.Join(root, "usr", "share", "coreos", "update.conf")
|
|
|
|
|
2014-05-07 04:44:18 +04:00
|
|
|
conf, err := os.Open(etcUpdate)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
conf, err = os.Open(usrUpdate)
|
2014-05-10 07:33:34 +04:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(conf)
|
|
|
|
|
|
|
|
sawStrat := false
|
2014-05-10 07:33:34 +04:00
|
|
|
stratLine := "REBOOT_STRATEGY=" + uc.strategy()
|
2014-05-07 04:44:18 +04:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2014-05-07 05:11:28 +04:00
|
|
|
if strings.HasPrefix(line, "REBOOT_STRATEGY=") {
|
2014-05-07 04:44:18 +04:00
|
|
|
line = stratLine
|
|
|
|
sawStrat = true
|
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
out += line
|
|
|
|
out += "\n"
|
2014-05-07 04:44:18 +04:00
|
|
|
if err := scanner.Err(); err != nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
return nil, err
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sawStrat {
|
2014-05-10 07:33:34 +04:00
|
|
|
out += stratLine
|
|
|
|
out += "\n"
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
return &system.File{
|
|
|
|
Path: path.Join("etc", "coreos", "update.conf"),
|
|
|
|
RawFilePermissions: "0644",
|
|
|
|
Content: out,
|
|
|
|
}, nil
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
// Unit generates a locksmith system.Unit for the cloud-init initializer to
|
|
|
|
// act on appropriately
|
|
|
|
func (uc UpdateConfig) Unit(root string) (*system.Unit, error) {
|
|
|
|
u := &system.Unit{
|
|
|
|
Name: locksmithUnit,
|
|
|
|
Enable: true,
|
|
|
|
Command: "restart",
|
|
|
|
Mask: false,
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
|
|
|
|
if uc.strategy() == "off" {
|
|
|
|
u.Enable = false
|
|
|
|
u.Command = "stop"
|
|
|
|
u.Mask = true
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
|
|
|
|
return u, nil
|
2014-05-07 04:44:18 +04:00
|
|
|
}
|