cloudinit/system/systemd.go

197 lines
4.5 KiB
Go
Raw Normal View History

2014-03-18 09:00:41 -07:00
package system
2014-03-04 16:36:05 -08:00
import (
"fmt"
"io/ioutil"
2014-03-04 16:36:05 -08:00
"log"
2014-03-12 15:43:51 -07:00
"os"
"os/exec"
2014-03-04 16:36:05 -08:00
"path"
2014-03-12 15:43:51 -07:00
"path/filepath"
"strings"
2014-03-04 16:36:05 -08:00
2014-03-12 19:36:31 -07:00
"github.com/coreos/coreos-cloudinit/third_party/github.com/coreos/go-systemd/dbus"
2014-03-04 16:36:05 -08:00
)
// fakeMachineID is placed on non-usr CoreOS images and should
// never be used as a true MachineID
const fakeMachineID = "42000000000000000000000000000042"
// Name for drop-in service configuration files created by cloudconfig
const cloudConfigDropIn = "20-cloudinit.conf"
2014-03-12 15:43:51 -07:00
type Unit struct {
Name string
Mask bool
Enable bool
2014-03-12 15:43:51 -07:00
Runtime bool
Content string
Command string
// For drop-in units, a cloudinit.conf is generated.
// This is currently unbound in YAML (and hence unsettable in cloud-config files)
// until the correct behaviour for multiple drop-in units is determined.
DropIn bool `yaml:"-"`
2014-03-12 15:43:51 -07:00
}
func (u *Unit) Type() string {
ext := filepath.Ext(u.Name)
return strings.TrimLeft(ext, ".")
}
func (u *Unit) Group() (group string) {
t := u.Type()
if t == "network" || t == "netdev" || t == "link" {
group = "network"
} else {
group = "system"
}
return
}
2014-03-04 16:36:05 -08:00
type Script []byte
// UnitDestination builds the appropriate absolute file path for
// the given Unit. The root argument indicates the effective base
// directory of the system (similar to a chroot).
func UnitDestination(u *Unit, root string) string {
2014-03-12 15:43:51 -07:00
dir := "etc"
if u.Runtime {
dir = "run"
}
if u.DropIn {
return path.Join(root, dir, "systemd", u.Group(), fmt.Sprintf("%s.d", u.Name), cloudConfigDropIn)
} else {
return path.Join(root, dir, "systemd", u.Group(), u.Name)
}
}
// PlaceUnit writes a unit file at the provided destination, creating
// parent directories as necessary.
func PlaceUnit(u *Unit, dst string) error {
dir := filepath.Dir(dst)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, os.FileMode(0755)); err != nil {
return err
2014-03-12 15:43:51 -07:00
}
}
2014-03-18 09:00:41 -07:00
file := File{
Path: dst,
Content: u.Content,
2014-03-18 09:00:41 -07:00
RawFilePermissions: "0644",
}
err := WriteFile(&file)
2014-03-12 15:43:51 -07:00
if err != nil {
return err
2014-03-12 15:43:51 -07:00
}
return nil
2014-03-12 15:43:51 -07:00
}
func EnableUnitFile(unit string, runtime bool) error {
2014-03-12 15:43:51 -07:00
conn, err := dbus.New()
if err != nil {
return err
}
units := []string{unit}
_, _, err = conn.EnableUnitFiles(units, runtime, true)
2014-03-12 15:43:51 -07:00
return err
}
func RunUnitCommand(command, unit string) (string, error) {
conn, err := dbus.New()
if err != nil {
return "", err
2014-03-12 15:43:51 -07:00
}
var fn func(string, string) (string, error)
switch command {
case "start":
fn = conn.StartUnit
case "stop":
fn = conn.StopUnit
case "restart":
fn = conn.RestartUnit
case "reload":
fn = conn.ReloadUnit
case "try-restart":
fn = conn.TryRestartUnit
case "reload-or-restart":
fn = conn.ReloadOrRestartUnit
case "reload-or-try-restart":
fn = conn.ReloadOrTryRestartUnit
default:
return "", fmt.Errorf("Unsupported systemd command %q", command)
2014-03-12 15:43:51 -07:00
}
return fn(unit, "replace")
2014-03-12 15:43:51 -07:00
}
func DaemonReload() error {
conn, err := dbus.New()
if err != nil {
return err
}
return conn.Reload()
2014-03-12 15:43:51 -07:00
}
func ExecuteScript(scriptPath string) (string, error) {
2014-03-04 16:36:05 -08:00
props := []dbus.Property{
dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"),
2014-03-04 16:36:05 -08:00
dbus.PropExecStart([]string{"/bin/bash", scriptPath}, false),
}
base := path.Base(scriptPath)
name := fmt.Sprintf("coreos-cloudinit-%s.service", base)
2014-03-04 16:36:05 -08:00
log.Printf("Creating transient systemd unit '%s'", name)
conn, err := dbus.New()
if err != nil {
return "", err
2014-03-04 16:36:05 -08:00
}
_, err = conn.StartTransientUnit(name, "replace", props...)
return name, err
2014-03-04 16:36:05 -08:00
}
func SetHostname(hostname string) error {
return exec.Command("hostnamectl", "set-hostname", hostname).Run()
}
func Hostname() (string, error) {
2014-03-18 11:04:33 -07:00
return os.Hostname()
}
func MachineID(root string) string {
contents, _ := ioutil.ReadFile(path.Join(root, "etc", "machine-id"))
id := strings.TrimSpace(string(contents))
if id == fakeMachineID {
id = ""
}
return id
}
2014-05-06 16:18:36 -07:00
// MaskUnit masks a Unit by the given name by symlinking its unit file (in
// /etc/systemd/system) to /dev/null, analogous to `systemctl mask`
// N.B.: Unlike `systemctl mask`, this function will *remove any existing unit
// file* in /etc/systemd/system, to ensure that the mask will succeed.
2014-05-06 16:18:36 -07:00
func MaskUnit(unit string, root string) error {
masked := path.Join(root, "etc", "systemd", "system", unit)
if _, err := os.Stat(masked); os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(masked), os.FileMode(0755)); err != nil {
return err
}
} else if err := os.Remove(masked); err != nil {
2014-05-06 16:18:36 -07:00
return err
}
return os.Symlink("/dev/null", masked)
}