2014-03-18 20:00:41 +04:00
|
|
|
package system
|
2014-03-05 04:36:05 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-03-13 02:43:51 +04:00
|
|
|
"os"
|
2014-03-13 09:30:24 +04:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2014-03-13 02:43:51 +04:00
|
|
|
type Unit struct {
|
|
|
|
Name string
|
|
|
|
Runtime bool
|
|
|
|
Content string
|
|
|
|
}
|
|
|
|
|
|
|
|
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 separateNetworkUnits(units []Unit) ([]Unit, []Unit) {
|
|
|
|
networkUnits := make([]Unit, 0)
|
|
|
|
nonNetworkUnits := make([]Unit, 0)
|
|
|
|
for _, unit := range units {
|
|
|
|
if unit.Group() == "network" {
|
|
|
|
networkUnits = append(networkUnits, unit)
|
|
|
|
} else {
|
|
|
|
nonNetworkUnits = append(nonNetworkUnits, unit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return networkUnits, nonNetworkUnits
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartUnits(units []Unit) error {
|
|
|
|
networkUnits, nonNetworkUnits := separateNetworkUnits(units)
|
|
|
|
if len(networkUnits) > 0 {
|
|
|
|
if err := RestartUnitByName("systemd-networkd.service"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, unit := range nonNetworkUnits {
|
|
|
|
if err := RestartUnitByName(unit.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DaemonReload() error {
|
|
|
|
conn, err := dbus.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Reload()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func RestartUnitByName(name string) error {
|
|
|
|
log.Printf("Restarting unit %s", name)
|
|
|
|
conn, err := dbus.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-13 09:04:08 +04:00
|
|
|
output, err := conn.RestartUnit(name, "replace")
|
2014-03-13 08:47:01 +04:00
|
|
|
log.Printf("Restart completed with '%s'", output)
|
|
|
|
|
2014-03-13 02:43:51 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartUnitByName(name string) error {
|
2014-03-05 04:36:05 +04:00
|
|
|
conn, err := dbus.New()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.StartUnit(name, "replace")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-05 09:02:20 +04:00
|
|
|
func ExecuteScript(scriptPath string) (string, error) {
|
2014-03-05 04:36:05 +04:00
|
|
|
props := []dbus.Property{
|
2014-03-05 05:46:35 +04:00
|
|
|
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)
|
2014-03-05 05:46:35 +04:00
|
|
|
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 {
|
2014-03-05 09:02:20 +04:00
|
|
|
return "", err
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.StartTransientUnit(name, "replace", props...)
|
2014-03-05 09:02:20 +04:00
|
|
|
return name, err
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
2014-03-13 09:30:24 +04:00
|
|
|
|
|
|
|
func SetHostname(hostname string) error {
|
|
|
|
return exec.Command("hostnamectl", "set-hostname", hostname).Run()
|
|
|
|
}
|