cloudinit/system/systemd.go

160 lines
3.0 KiB
Go
Raw Normal View History

2014-03-18 20:00:41 +04:00
package system
2014-03-05 04:36:05 +04:00
import (
"fmt"
"io/ioutil"
2014-03-05 04:36:05 +04:00
"log"
2014-03-13 02:43:51 +04:00
"os"
"os/exec"
2014-03-05 04:36:05 +04:00
"path"
2014-03-13 02:43:51 +04:00
"path/filepath"
"strings"
2014-03-05 04:36:05 +04:00
2014-03-13 06:36:31 +04:00
"github.com/coreos/coreos-cloudinit/third_party/github.com/coreos/go-systemd/dbus"
2014-03-05 04:36:05 +04:00
)
// fakeMachineID is placed on non-usr CoreOS images and should
// never be used as a true MachineID
const fakeMachineID = "42000000000000000000000000000042"
2014-03-13 02:43:51 +04:00
type Unit struct {
Name string
Runtime bool
Content string
Command string
2014-03-13 02:43:51 +04: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-05 04:36:05 +04:00
type Script []byte
2014-03-18 20:00:41 +04:00
func PlaceUnit(u *Unit, root string) (string, error) {
2014-03-13 02:43:51 +04:00
dir := "etc"
if u.Runtime {
dir = "run"
}
dst := path.Join(root, dir, "systemd", u.Group())
if _, err := os.Stat(dst); os.IsNotExist(err) {
if err := os.MkdirAll(dst, os.FileMode(0755)); err != nil {
return "", err
}
}
dst = path.Join(dst, u.Name)
2014-03-18 20:00:41 +04:00
file := File{
Path: dst,
Content: u.Content,
RawFilePermissions: "0644",
}
err := WriteFile(&file)
2014-03-13 02:43:51 +04:00
if err != nil {
return "", err
}
return dst, nil
}
func EnableUnitFile(file string, runtime bool) error {
conn, err := dbus.New()
if err != nil {
return err
}
files := []string{file}
_, _, err = conn.EnableUnitFiles(files, runtime, true)
return err
}
func RunUnitCommand(command, unit string) (string, error) {
conn, err := dbus.New()
if err != nil {
return "", err
2014-03-13 02:43:51 +04: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-13 02:43:51 +04:00
}
return fn(unit, "replace")
2014-03-13 02:43:51 +04:00
}
func DaemonReload() error {
conn, err := dbus.New()
if err != nil {
return err
}
return conn.Reload()
2014-03-13 02:43:51 +04:00
}
func ExecuteScript(scriptPath string) (string, error) {
2014-03-05 04:36:05 +04:00
props := []dbus.Property{
dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"),
2014-03-05 04:36:05 +04:00
dbus.PropExecStart([]string{"/bin/bash", scriptPath}, false),
}
base := path.Base(scriptPath)
name := fmt.Sprintf("coreos-cloudinit-%s.service", base)
2014-03-05 04:36:05 +04:00
log.Printf("Creating transient systemd unit '%s'", name)
conn, err := dbus.New()
if err != nil {
return "", err
2014-03-05 04:36:05 +04:00
}
_, err = conn.StartTransientUnit(name, "replace", props...)
return name, err
2014-03-05 04:36:05 +04:00
}
func SetHostname(hostname string) error {
return exec.Command("hostnamectl", "set-hostname", hostname).Run()
}
func Hostname() (string, error) {
2014-03-18 22:04:33 +04: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
}