Example #1
0
// newSystemd creates a systemd object that implements systemdInterface.
func newSystemd() (*systemd, error) {
	dbusConn, err := dbus.New()
	if err != nil {
		return nil, err
	}
	return &systemd{dbusConn}, nil
}
Example #2
0
func NewSystemdUnitManager(uDir string, systemdUser bool) (*systemdUnitManager, error) {
	var systemd *dbus.Conn
	var err error
	if systemdUser {
		systemd, err = dbus.NewUserConnection()
	} else {
		systemd, err = dbus.New()
	}
	if err != nil {
		return nil, err
	}

	if err := os.MkdirAll(uDir, os.FileMode(0755)); err != nil {
		return nil, err
	}

	hashes, err := hashUnitFiles(uDir)
	if err != nil {
		return nil, err
	}

	mgr := systemdUnitManager{
		systemd:  systemd,
		unitsDir: uDir,
		hashes:   hashes,
		mutex:    sync.RWMutex{},
	}
	return &mgr, nil
}
Example #3
0
func (s *systemd) RunUnitCommand(u Unit, c string) (string, error) {
	conn, err := dbus.New()
	if err != nil {
		return "", err
	}

	var fn func(string, string) (string, error)
	switch c {
	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", c)
	}

	return fn(u.Name, "replace")
}
Example #4
0
// Restart behaves as `systemctl restart <unit>`
func (sdc *SystemdClient) Restart(unit string) error {
	sdc.Logger.Debugf("restarting %s", unit)

	conn, err := dbus.New()
	if err != nil {
		return maskAny(err)
	}

	responseChan := make(chan string, 1)
	if _, err := conn.RestartUnit(unit, "replace", responseChan); err != nil {
		sdc.Logger.Errorf("restarting %s failed: %#v", unit, err)
		return maskAny(err)
	}

	select {
	case res := <-responseChan:
		switch res {
		case "done":
			return nil
		case "failed":
			// We need a start considered to be failed, when the unit is already running.
			return nil
		case "canceled", "timeout", "dependency", "skipped":
			return maskAny(errgo.WithCausef(nil, SystemdError, res))
		default:
			// that should never happen
			sdc.Logger.Errorf("unexpected systemd response: '%s'", res)
			return maskAny(errgo.WithCausef(nil, SystemdError, res))
		}
	case <-time.After(jobTimeout):
		return maskAny(errgo.WithCausef(nil, SystemdError, "job timeout"))
	}

	return nil
}
Example #5
0
func UseSystemd() bool {
	if !systemd.SdBooted() {
		return false
	}

	connLock.Lock()
	defer connLock.Unlock()

	if theConn == nil {
		var err error
		theConn, err = systemd1.New()
		if err != nil {
			return false
		}

		// Assume we have StartTransientUnit
		hasStartTransientUnit = true

		// But if we get UnknownMethod error we don't
		if _, err := theConn.StartTransientUnit("test.scope", "invalid"); err != nil {
			if dbusError, ok := err.(dbus.Error); ok {
				if dbusError.Name == "org.freedesktop.DBus.Error.UnknownMethod" {
					hasStartTransientUnit = false
				}
			}
		}
	}
	return hasStartTransientUnit
}
Example #6
0
func New(machine *machine.Machine) *Target {
	name := "coreinit-" + machine.BootId
	systemd := systemdDbus.New()
	target := &Target{name, systemd, machine}
	target.createSystemdTarget()
	return target
}
Example #7
0
func (nc *nspawnCluster) systemd(unitName, exec string) error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}
	defer conn.Close()

	props := []dbus.Property{
		dbus.PropExecStart(strings.Split(exec, " "), false),
	}

	log.Printf("Creating transient systemd unit %s", unitName)

	res1 := make(chan string)
	if _, err = conn.StartTransientUnit(unitName, "replace", props, res1); err != nil {
		log.Printf("Failed creating transient unit %s: %v", unitName, err)
		return err
	}
	<-res1

	res2 := make(chan string)
	_, err = conn.StartUnit(unitName, "replace", res2)
	if err != nil {
		log.Printf("Failed starting transient unit %s: %v", unitName, err)
		return err
	}

	<-res2
	return nil
}
Example #8
0
func (s *systemd) DaemonReload() error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	return conn.Reload()
}
Example #9
0
File: nspawn.go Project: pulcy/j2
func (nc *nspawnCluster) systemdReload() error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}
	conn.Reload()
	return nil
}
Example #10
0
// New creates the rkt container runtime which implements the container runtime interface.
// It will test if the rkt binary is in the $PATH, and whether we can get the
// version of it. If so, creates the rkt container runtime, otherwise returns an error.
func New(config *Config,
	generator kubecontainer.RunContainerOptionsGenerator,
	recorder record.EventRecorder,
	containerRefManager *kubecontainer.RefManager,
	readinessManager *kubecontainer.ReadinessManager,
	volumeGetter volumeGetter) (kubecontainer.Runtime, error) {

	systemdVersion, err := getSystemdVersion()
	if err != nil {
		return nil, err
	}
	result, err := systemdVersion.Compare(systemdMinimumVersion)
	if err != nil {
		return nil, err
	}
	if result < 0 {
		return nil, fmt.Errorf("rkt: systemd version is too old, requires at least %v", systemdMinimumVersion)
	}

	systemd, err := dbus.New()
	if err != nil {
		return nil, fmt.Errorf("cannot connect to dbus: %v", err)
	}

	// Test if rkt binary is in $PATH.
	// TODO(yifan): Use a kubelet flag to read the path.
	rktBinAbsPath, err := exec.LookPath("rkt")
	if err != nil {
		return nil, fmt.Errorf("cannot find rkt binary: %v", err)
	}

	rkt := &runtime{
		systemd:             systemd,
		rktBinAbsPath:       rktBinAbsPath,
		config:              config,
		dockerKeyring:       credentialprovider.NewDockerKeyring(),
		containerRefManager: containerRefManager,
		generator:           generator,
		recorder:            recorder,
		readinessManager:    readinessManager,
		volumeGetter:        volumeGetter,
	}
	rkt.prober = prober.New(rkt, readinessManager, containerRefManager, recorder)

	// Test the rkt version.
	version, err := rkt.Version()
	if err != nil {
		return nil, err
	}
	result, err = version.Compare(rktMinimumVersion)
	if err != nil {
		return nil, err
	}
	if result < 0 {
		return nil, fmt.Errorf("rkt: Version is too old, requires at least %v", rktMinimumVersion)
	}
	return rkt, nil
}
Example #11
0
// daemonReload instructs systemd to reload it's configuration
func (s *Service) daemonReload() error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}
	defer conn.Close()

	return conn.Reload()
}
func (c *systemdCollector) listUnits() ([]dbus.UnitStatus, error) {
	conn, err := dbus.New()
	if err != nil {
		return nil, fmt.Errorf("couldn't get dbus connection: %s", err)
	}
	units, err := conn.ListUnits()
	conn.Close()
	return units, err
}
func (c *systemdCollector) getSystemState() (state string, err error) {
	conn, err := dbus.New()
	if err != nil {
		return "", fmt.Errorf("couldn't get dbus connection: %s", err)
	}
	state, err = conn.GetManagerProperty("SystemState")
	conn.Close()
	return state, err
}
Example #14
0
func StartUnit(name string) error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	_, err = conn.StartUnit(name, "replace")
	return err
}
Example #15
0
func (s *systemd) EnableUnitFile(u Unit) error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	units := []string{u.Name}
	_, _, err = conn.EnableUnitFiles(units, u.Runtime, true)
	return err
}
Example #16
0
// unitProperty retrieves the requested property for the service unit
func (s *Service) unitProperty(name string) (*dbus.Property, error) {
	conn, err := dbus.New()
	if err != nil {
		return nil, err
	}
	defer conn.Close()

	property, err := conn.GetUnitProperty(s.unit, name)

	return property, err
}
Example #17
0
func NewSystemdManager(machine *machine.Machine) *SystemdManager {
	systemd := systemdDbus.New()

	name := "coreinit-" + machine.BootId + ".target"
	target := NewSystemdTarget(name)

	mgr := &SystemdManager{systemd, target, machine, defaultSystemdRuntimePath, defaultSystemdDbusPath}

	mgr.writeUnit(target.Name(), "")

	return mgr
}
func main() {
	var port int
	flag.IntVar(&port, "port", 3001, "server port")
	flag.Parse()
	fmt.Println("Port: ", port)
	var connection, err = dbus.New()
	if err != nil {
		panic(err)
	}
	var handler = createHandler(connection)
	http.HandleFunc("/", handler)
	http.ListenAndServe(":"+strconv.Itoa(port), nil)
}
Example #19
0
func killService(name string, signal int32) error {
	conn, err := dbus.New()
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "dbus.New",
		}).Error("error creating new dbus connection")
		return err
	}

	conn.KillUnit(name, signal)
	return nil
}
Example #20
0
func NewRegistry(ttl string) (registry *Registry) {
	etcdC := etcd.NewClient(nil)
	mach := machine.NewMachine("")
	systemd := systemdDbus.New()

	if ttl == "" {
		ttl = DefaultServiceTTL
	}

	registry = &Registry{etcdC, systemd, mach, ttl}

	return registry
}
Example #21
0
/*
Stop a Systemd unit and wait for it to return, this method may block.
*/
func (service *Service) stopSystemdUnit(name string) error {
	conn, err := dbus.New()
	defer conn.Close()
	if err != nil {
		return err
	}
	c := make(chan string)
	conn.StopUnit(name, "replace", c)
	done := <-c
	if done != "finished" {
		return fmt.Errorf("Problem starting systemd unit, dbus responded: %s", done)
	}
	return nil
}
Example #22
0
func NewSystemdManager(machine *machine.Machine, unitPrefix string) *SystemdManager {
	//TODO(bcwaldon): Handle error in call to New()
	systemd, _ := systemdDbus.New()
	systemd.Subscribe()

	name := "coreinit-" + machine.BootId + ".target"
	target := NewSystemdTarget(name)

	mgr := &SystemdManager{systemd, target, machine, unitPrefix, defaultSystemdRuntimePath}

	mgr.writeUnit(target.Name(), "")

	return mgr
}
Example #23
0
// Reload instructs systemd to scan for and reload unit files.
// Equivalent to a 'systemctl daemon-reload'.
// See https://godoc.org/github.com/coreos/go-systemd/dbus#Conn.Reload.
func (sdc *SystemdClient) Reload() error {
	vLogger("  call SystemdClient.Reload()")

	conn, err := systemdPkg.New()
	if err != nil {
		return maskAny(err)
	}

	if err := conn.Reload(); err != nil {
		return maskAny(err)
	}

	return nil
}
Example #24
0
// Enable behaves as `systemctl enable <unit>`
func (sdc *SystemdClient) Enable(unit string) error {
	sdc.Logger.Debugf("enabling %s", unit)

	conn, err := dbus.New()
	if err != nil {
		return maskAny(err)
	}

	if _, _, err := conn.EnableUnitFiles([]string{unit}, false, false); err != nil {
		sdc.Logger.Errorf("enabling %s failed: %#v", unit, err)
		return maskAny(err)
	}

	return nil
}
Example #25
0
// Reload behaves as `systemctl daemon-reload`
func (sdc *SystemdClient) Reload() error {
	sdc.Logger.Debug("reloading daemon")

	conn, err := dbus.New()
	if err != nil {
		return maskAny(err)
	}

	if err := conn.Reload(); err != nil {
		sdc.Logger.Errorf("reloading daemon failed: %#v", err)
		return maskAny(err)
	}

	return nil
}
Example #26
0
func (s *System) detectService() {
	switch {
	case util.IsRunningSystemd():
		s.NewService = NewServiceDbus
		dbus, err := dbus.New()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		s.Dbus = dbus
	case isUbuntu():
		s.NewService = NewServiceUpstart
	default:
		s.NewService = NewServiceInit
	}
}
Example #27
0
/*
Check if the specified unit name is running with Systemd
*/
func (service *Service) systemdUnitRunning(name string) (bool, error) {
	conn, err := dbus.New()
	defer conn.Close()
	if err != nil {
		return false, err
	}
	units, err := conn.ListUnits()
	if err != nil {
		return false, err
	}
	for _, unit := range units {
		if unit.Name == name+".service" {
			return unit.ActiveState == "active", nil
		}
	}
	return true, nil
}
Example #28
0
func StartUnit(target string) error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	reschan := make(chan string)
	_, err = conn.StartUnit(target, "replace", reschan)
	if err != nil {
		return err
	}

	job := <-reschan
	if job != "done" {
		return err
	}

	return nil
}
Example #29
0
func ExecuteScript(scriptPath string) (string, error) {
	props := []dbus.Property{
		dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"),
		dbus.PropExecStart([]string{"/bin/bash", scriptPath}, false),
	}

	base := path.Base(scriptPath)
	name := fmt.Sprintf("coreos-cloudinit-%s.service", base)

	log.Printf("Creating transient systemd unit '%s'", name)

	conn, err := dbus.New()
	if err != nil {
		return "", err
	}

	_, err = conn.StartTransientUnit(name, "replace", props...)
	return name, err
}
Example #30
0
File: flex.go Project: coreos/torus
func connectSystemd() systemd {
	var sysd systemd
	conn, err := dbus.New()
	if err != nil {
		onErr(err)
	}
	err = conn.Subscribe()
	if err != nil {
		onErr(err)
	}

	err = conn.Unsubscribe()
	if err != nil {
		onErr(err)
	}
	sysd.Conn = conn
	sysd.evChan, sysd.errChan = conn.SubscribeUnits(time.Second)
	return sysd
}