本文整理汇总了Golang中github.com/coreos/go-systemd/util.IsRunningSystemd函数的典型用法代码示例。如果您正苦于以下问题:Golang IsRunningSystemd函数的具体用法?Golang IsRunningSystemd怎么用?Golang IsRunningSystemd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsRunningSystemd函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: NewService
// NewService creates a new resource for managing services
// using systemd on a GNU/Linux system
func NewService(name string) (Resource, error) {
if !util.IsRunningSystemd() {
return nil, ErrNoSystemd
}
s := &Service{
Base: Base{
Name: name,
Type: "service",
State: "running",
Require: make([]string, 0),
PresentStatesList: []string{"present", "running"},
AbsentStatesList: []string{"absent", "stopped"},
Concurrent: true,
Subscribe: make(TriggerMap),
},
Enable: true,
unit: fmt.Sprintf("%s.service", name),
}
// Set resource properties
s.PropertyList = []Property{
&ResourceProperty{
PropertyName: "enable",
PropertySetFunc: s.setEnable,
PropertyIsSyncedFunc: s.isEnableSynced,
},
}
return s, nil
}
开发者ID:dnaeon,项目名称:gru,代码行数:33,代码来源:service_linux.go
示例2: TestJournalLink
func TestJournalLink(t *testing.T) {
if !sd_util.IsRunningSystemd() {
t.Skip("Systemd is not running on the host.")
}
if _, err := os.Stat(journalDir); os.IsNotExist(err) {
t.Skip("Persistent journaling disabled.")
}
image := getInspectImagePath()
ctx := testutils.NewRktRunCtx()
defer ctx.Cleanup()
rktCmd := fmt.Sprintf("%s prepare --insecure-options=image %s", ctx.Cmd(), image)
uuid := runRktAndGetUUID(t, rktCmd)
rktCmd = fmt.Sprintf("%s run-prepared %s", ctx.Cmd(), uuid)
spawnAndWaitOrFail(t, rktCmd, 0)
machineID := strings.Replace(uuid, "-", "", -1)
journalPath := filepath.Join("/var/log/journal", machineID)
link, err := os.Readlink(journalPath)
if err != nil {
t.Fatalf("failed to read journal link %q", journalPath)
}
podJournal := filepath.Join(ctx.DataDir(), "pods/run", uuid, "stage1/rootfs/var/log/journal/", machineID)
if link != podJournal {
t.Fatalf("unexpected target of journal link: %q. Expected %q", link, podJournal)
}
}
开发者ID:intelsdi-x,项目名称:rkt,代码行数:33,代码来源:rkt_journal_test.go
示例3: Apply
func (obj *ServiceType) Apply() bool {
log.Printf("%v[%v]: Apply", obj.GetType(), obj.GetName())
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
}
conn, err := systemd.NewSystemdConnection() // needs root access
if err != nil {
log.Fatal("Failed to connect to systemd: ", err)
}
defer conn.Close()
var service = fmt.Sprintf("%v.service", obj.Name) // systemd name
var files = []string{service} // the service represented in a list
if obj.Startup == "enabled" {
_, _, err = conn.EnableUnitFiles(files, false, true)
} else if obj.Startup == "disabled" {
_, err = conn.DisableUnitFiles(files, false)
} else {
err = nil
}
if err != nil {
log.Printf("Unable to change startup status: %v", err)
return false
}
result := make(chan string, 1) // catch result information
if obj.State == "running" {
_, err := conn.StartUnit(service, "fail", result)
if err != nil {
log.Fatal("Failed to start unit: ", err)
return false
}
} else if obj.State == "stopped" {
_, err = conn.StopUnit(service, "fail", result)
if err != nil {
log.Fatal("Failed to stop unit: ", err)
return false
}
} else {
log.Fatal("Unknown state: ", obj.State)
}
status := <-result
if &status == nil {
log.Fatal("Result is nil")
return false
}
if status != "done" {
log.Fatal("Unknown return string: ", status)
return false
}
// XXX: also set enabled on boot
return true
}
开发者ID:40a,项目名称:mgmt,代码行数:60,代码来源:service.go
示例4: TestService
func TestService(t *testing.T) {
if !util.IsRunningSystemd() {
return
}
L := newLuaState()
defer L.Close()
const code = `
svc = service.new("nginx")
`
if err := L.DoString(code); err != nil {
t.Fatal(err)
}
svc := luaResource(L, "svc").(*Service)
errorIfNotEqual(t, "service", svc.Type)
errorIfNotEqual(t, "nginx", svc.Name)
errorIfNotEqual(t, "running", svc.State)
errorIfNotEqual(t, []string{}, svc.After)
errorIfNotEqual(t, []string{}, svc.Before)
errorIfNotEqual(t, []string{"present", "running"}, svc.PresentStates)
errorIfNotEqual(t, []string{"absent", "stopped"}, svc.AbsentStates)
errorIfNotEqual(t, true, svc.Enable)
}
开发者ID:ycaille,项目名称:gru,代码行数:26,代码来源:service_test.go
示例5: main
func main() {
flag.Parse()
exists, err := dirExists(*outputDir)
if err != nil {
log.Fatal(err)
}
if !exists {
if err := os.Mkdir(*outputDir, 0755); err != nil {
log.Fatal(err)
}
}
cfg := client.Config{
Endpoints: []string{*endpoint},
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
if err := c.Sync(context.Background()); err != nil {
log.Fatal(err)
}
kapi := client.NewKeysAPI(c)
resp, err := generateConfig(kapi)
if err != nil {
log.Fatal(err)
}
if systemdutil.IsRunningSystemd() {
err := daemon.SdNotify("READY=1")
if err != nil {
log.Printf("failed to notify systemd for readiness: %v", err)
if err == daemon.SdNotifyNoSocket {
log.Printf("forgot to set Type=notify in systemd service file?")
}
}
}
if *watch {
for {
resp, err = generateConfigWatcher(kapi, resp)
if err != nil {
log.Fatal(err)
}
}
}
os.Exit(0)
}
开发者ID:themecloud,项目名称:etcd2envfile,代码行数:55,代码来源:etcd2envfile.go
示例6: StateOK
func (obj *ServiceType) StateOK() bool {
if obj.isStateOK { // cache the state
return true
}
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
}
conn, err := systemd.NewSystemdConnection() // needs root access
if err != nil {
log.Fatal("Failed to connect to systemd: ", err)
}
defer conn.Close()
var service = fmt.Sprintf("%v.service", obj.Name) // systemd name
loadstate, err := conn.GetUnitProperty(service, "LoadState")
if err != nil {
log.Printf("Failed to get load state: %v", err)
return false
}
// NOTE: we have to compare variants with other variants, they are really strings...
var notFound = (loadstate.Value == dbus.MakeVariant("not-found"))
if notFound {
log.Printf("Failed to find service: %v", service)
return false
}
// XXX: check service "enabled at boot" or not status...
//conn.GetUnitProperties(service)
activestate, err := conn.GetUnitProperty(service, "ActiveState")
if err != nil {
log.Fatal("Failed to get active state: ", err)
}
var running = (activestate.Value == dbus.MakeVariant("active"))
if obj.State == "running" {
if !running {
return false // we are in the wrong state
}
} else if obj.State == "stopped" {
if running {
return false
}
} else {
log.Fatal("Unknown state: ", obj.State)
}
return true // all is good, no state change needed
}
开发者ID:40a,项目名称:mgmt,代码行数:54,代码来源:service.go
示例7: detectService
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
}
}
开发者ID:bossjones,项目名称:goss,代码行数:16,代码来源:system.go
示例8: setupSocketActivation
func setupSocketActivation() (net.Listener, error) {
if !util.IsRunningSystemd() {
return nil, nil
}
listenFds := activation.Files(false)
if len(listenFds) > 1 {
return nil, fmt.Errorf("expected only one socket from systemd, got %d", len(listenFds))
}
var listener net.Listener
if len(listenFds) == 1 {
l, err := net.FileListener(listenFds[0])
if err != nil {
return nil, err
}
listener = l
}
return listener, nil
}
开发者ID:CtrlZvi,项目名称:public-ipv6-bridge,代码行数:18,代码来源:unix_listener.go
示例9: NewService
// NewService creates a new resource for managing services
// using systemd on a GNU/Linux system
func NewService(name string) (Resource, error) {
if !util.IsRunningSystemd() {
return nil, ErrNoSystemd
}
s := &Service{
Base: Base{
Name: name,
Type: "service",
State: "running",
After: make([]string, 0),
Before: make([]string, 0),
PresentStates: []string{"present", "running"},
AbsentStates: []string{"absent", "stopped"},
},
Enable: true,
unit: fmt.Sprintf("%s.service", name),
}
return s, nil
}
开发者ID:ycaille,项目名称:gru,代码行数:23,代码来源:service.go
示例10: New
func New(c *cli.Context) *System {
system := &System{
NewFile: NewDefFile,
NewAddr: NewDefAddr,
NewPort: NewDefPort,
NewUser: NewDefUser,
NewGroup: NewDefGroup,
NewCommand: NewDefCommand,
NewDNS: NewDefDNS,
NewProcess: NewDefProcess,
NewGossfile: NewDefGossfile,
}
if util.IsRunningSystemd() {
system.NewService = NewServiceDbus
dbus, err := dbus.New()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
system.Dbus = dbus
} else {
system.NewService = NewServiceInit
}
switch {
case c.GlobalString("package") == "rpm":
system.NewPackage = NewRpmPackage
case c.GlobalString("package") == "deb":
system.NewPackage = NewDebPackage
default:
system.NewPackage = detectPackage()
}
return system
}
开发者ID:timstoop,项目名称:goss,代码行数:36,代码来源:system.go
示例11: linkJournal
func linkJournal(s1Root, machineID string) error {
if !util.IsRunningSystemd() {
return nil
}
absS1Root, err := filepath.Abs(s1Root)
if err != nil {
return err
}
// /var/log/journal doesn't exist on the host, don't do anything
if _, err := os.Stat(journalDir); os.IsNotExist(err) {
return nil
}
machineJournalDir := filepath.Join(journalDir, machineID)
podJournalDir := filepath.Join(absS1Root, machineJournalDir)
hostMachineID, err := util.GetMachineID()
if err != nil {
return err
}
// unlikely, machine ID is random (== pod UUID)
if hostMachineID == machineID {
return fmt.Errorf("host and pod machine IDs are equal (%s)", machineID)
}
fi, err := os.Lstat(machineJournalDir)
switch {
case os.IsNotExist(err):
// good, we'll create the symlink
case err != nil:
return err
// unlikely, machine ID is random (== pod UUID)
default:
if fi.IsDir() {
if err := os.Remove(machineJournalDir); err != nil {
return err
}
}
link, err := os.Readlink(machineJournalDir)
if err != nil {
return err
}
if link == podJournalDir {
return nil
} else {
if err := os.Remove(machineJournalDir); err != nil {
return err
}
}
}
if err := os.Symlink(podJournalDir, machineJournalDir); err != nil {
return err
}
return nil
}
开发者ID:nhlfr,项目名称:rkt,代码行数:62,代码来源:kvm.go
示例12: getContainerSubCgroup
func getContainerSubCgroup(machineID string, canMachinedRegister, unified bool) (string, error) {
var fromUnit bool
if util.IsRunningSystemd() {
var err error
if fromUnit, err = util.RunningFromSystemService(); err != nil {
return "", errwrap.Wrap(errors.New("could not determine if we're running from a unit file"), err)
}
}
if fromUnit {
slice, err := util.GetRunningSlice()
if err != nil {
return "", errwrap.Wrap(errors.New("could not get slice name"), err)
}
slicePath, err := common.SliceToPath(slice)
if err != nil {
return "", errwrap.Wrap(errors.New("could not convert slice name to path"), err)
}
unit, err := util.CurrentUnitName()
if err != nil {
return "", errwrap.Wrap(errors.New("could not get unit name"), err)
}
subcgroup := filepath.Join(slicePath, unit)
if unified {
return filepath.Join(subcgroup, "payload"), nil
}
return subcgroup, nil
}
escapedmID := strings.Replace(machineID, "-", "\\x2d", -1)
machineDir := "machine-" + escapedmID + ".scope"
if canMachinedRegister {
// we are not in the final cgroup yet: systemd-nspawn will move us
// to the correct cgroup later during registration so we can't
// look it up in /proc/self/cgroup
return filepath.Join("machine.slice", machineDir), nil
}
if unified {
subcgroup, err := v2.GetOwnCgroupPath()
if err != nil {
return "", errwrap.Wrap(errors.New("could not get own v2 cgroup path"), err)
}
return subcgroup, nil
}
// when registration is disabled the container will be directly
// under the current cgroup so we can look it up in /proc/self/cgroup
ownV1CgroupPath, err := v1.GetOwnCgroupPath("name=systemd")
if err != nil {
return "", errwrap.Wrap(errors.New("could not get own v1 cgroup path"), err)
}
// systemd-nspawn won't work if we are in the root cgroup. In addition,
// we want all rkt instances to be in distinct cgroups. Create a
// subcgroup and add ourselves to it.
return filepath.Join(ownV1CgroupPath, machineDir), nil
}
开发者ID:joshix,项目名称:rkt,代码行数:62,代码来源:init.go
示例13: getArgsEnv
//.........这里部分代码省略.........
}
versionStr := strings.SplitN(string(versionBytes), "\n", 2)[0]
var version int
n, err := fmt.Sscanf(versionStr, "systemd %d", &version)
if err != nil {
return nil, nil, fmt.Errorf("cannot parse version: %q", versionStr)
}
if n != 1 || version < 220 {
return nil, nil, fmt.Errorf("rkt needs systemd-nspawn >= 220. %s version not supported: %v", hostNspawnBin, versionStr)
}
// Copy systemd, bash, etc. in stage1 at run-time
if err := installAssets(); err != nil {
return nil, nil, errwrap.Wrap(errors.New("cannot install assets from the host"), err)
}
args = append(args, hostNspawnBin)
args = append(args, "--boot") // Launch systemd in the pod
args = append(args, fmt.Sprintf("--register=true"))
if version >= 231 {
args = append(args, "--notify-ready=yes") // From systemd v231
}
if context := os.Getenv(common.EnvSELinuxContext); context != "" {
args = append(args, fmt.Sprintf("-Z%s", context))
}
if context := os.Getenv(common.EnvSELinuxMountContext); context != "" {
args = append(args, fmt.Sprintf("-L%s", context))
}
default:
return nil, nil, fmt.Errorf("unrecognized stage1 flavor: %q", flavor)
}
machineIDBytes := append([]byte(machineID), '\n')
if err := ioutil.WriteFile(mPath, machineIDBytes, 0644); err != nil {
log.FatalE("error writing /etc/machine-id", err)
}
// link journal only if the host is running systemd
if util.IsRunningSystemd() {
args = append(args, "--link-journal=try-guest")
keepUnit, err := util.RunningFromSystemService()
if err != nil {
if err == dlopen.ErrSoNotFound {
log.Print("warning: libsystemd not found even though systemd is running. Cgroup limits set by the environment (e.g. a systemd service) won't be enforced.")
} else {
return nil, nil, errwrap.Wrap(errors.New("error determining if we're running from a system service"), err)
}
}
if keepUnit {
args = append(args, "--keep-unit")
}
} else {
args = append(args, "--link-journal=no")
}
if !debug {
args = append(args, "--quiet") // silence most nspawn output (log_warning is currently not covered by this)
env = append(env, "SYSTEMD_LOG_LEVEL=err") // silence log_warning too
}
env = append(env, "SYSTEMD_NSPAWN_CONTAINER_SERVICE=rkt")
// TODO (alepuccetti) remove this line when rkt will use cgroup namespace
// If the kernel has the cgroup namespace enabled, systemd v232 will use it by default.
// This was introduced by https://github.com/systemd/systemd/pull/3809 and it will cause
// problems in rkt when cgns is enabled and cgroup-v1 is used. For more information see
// https://github.com/systemd/systemd/pull/3589#discussion_r70277625.
// The following line tells systemd-nspawn not to use cgroup namespace using the environment variable
// introduced by https://github.com/systemd/systemd/pull/3809.
env = append(env, "SYSTEMD_NSPAWN_USE_CGNS=no")
if insecureOptions.DisablePaths {
env = append(env, "SYSTEMD_NSPAWN_API_VFS_WRITABLE=yes")
}
if len(privateUsers) > 0 {
args = append(args, "--private-users="+privateUsers)
}
nsargs, err := stage1initcommon.PodToNspawnArgs(p, insecureOptions)
if err != nil {
return nil, nil, errwrap.Wrap(errors.New("failed to generate nspawn args"), err)
}
args = append(args, nsargs...)
// Arguments to systemd
args = append(args, "--")
args = append(args, "--default-standard-output=tty") // redirect all service logs straight to tty
if !debug {
args = append(args, "--log-target=null") // silence systemd output inside pod
args = append(args, "--show-status=0") // silence systemd initialization status output
}
return args, env, nil
}
开发者ID:joshix,项目名称:rkt,代码行数:101,代码来源:init.go
示例14: Main
func Main() {
cfg := NewConfig()
err := cfg.Parse(os.Args[1:])
if err != nil {
plog.Errorf("error verifying flags, %v. See 'etcd --help'.", err)
switch err {
case errUnsetAdvertiseClientURLsFlag:
plog.Errorf("When listening on specific address(es), this etcd process must advertise accessible url(s) to each connected client.")
}
os.Exit(1)
}
setupLogging(cfg)
var stopped <-chan struct{}
GoMaxProcs := 1
if envMaxProcs, err := strconv.Atoi(os.Getenv("GOMAXPROCS")); err == nil {
GoMaxProcs = envMaxProcs
}
plog.Infof("setting maximum number of CPUs to %d, total number of available CPUs is %d", GoMaxProcs, runtime.NumCPU())
runtime.GOMAXPROCS(GoMaxProcs)
// TODO: check whether fields are set instead of whether fields have default value
if cfg.name != defaultName && cfg.initialCluster == initialClusterFromName(defaultName) {
cfg.initialCluster = initialClusterFromName(cfg.name)
}
if cfg.dir == "" {
cfg.dir = fmt.Sprintf("%v.etcd", cfg.name)
plog.Warningf("no data-dir provided, using default data-dir ./%s", cfg.dir)
}
which := identifyDataDirOrDie(cfg.dir)
if which != dirEmpty {
plog.Noticef("the server is already initialized as %v before, starting as etcd %v...", which, which)
switch which {
case dirMember:
stopped, err = startEtcd(cfg)
case dirProxy:
err = startProxy(cfg)
default:
plog.Panicf("unhandled dir type %v", which)
}
} else {
shouldProxy := cfg.isProxy()
if !shouldProxy {
stopped, err = startEtcd(cfg)
if err == discovery.ErrFullCluster && cfg.shouldFallbackToProxy() {
plog.Noticef("discovery cluster full, falling back to %s", fallbackFlagProxy)
shouldProxy = true
}
}
if shouldProxy {
err = startProxy(cfg)
}
}
if err != nil {
switch err {
case discovery.ErrDuplicateID:
plog.Errorf("member %q has previously registered with discovery service token (%s).", cfg.name, cfg.durl)
plog.Errorf("But etcd could not find valid cluster configuration in the given data dir (%s).", cfg.dir)
plog.Infof("Please check the given data dir path if the previous bootstrap succeeded")
plog.Infof("or use a new discovery token if the previous bootstrap failed.")
os.Exit(1)
case discovery.ErrDuplicateName:
plog.Errorf("member with duplicated name has registered with discovery service token(%s).", cfg.durl)
plog.Errorf("please check (cURL) the discovery token for more information.")
plog.Errorf("please do not reuse the discovery token and generate a new one to bootstrap the cluster.")
default:
plog.Fatalf("%v", err)
}
}
osutil.HandleInterrupts()
if systemdutil.IsRunningSystemd() {
// At this point, the initialization of etcd is done.
// The listeners are listening on the TCP ports and ready
// for accepting connections.
// The http server is probably ready for serving incoming
// connections. If it is not, the connection might be pending
// for less than one second.
err := daemon.SdNotify("READY=1")
if err != nil {
plog.Errorf("failed to notify systemd for readiness")
}
}
<-stopped
osutil.Exit(0)
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:92,代码来源:etcd.go
示例15: getArgsEnv
//.........这里部分代码省略.........
args = append(args, fmt.Sprintf("--register=false"))
}
case "host":
hostNspawnBin, err := common.LookupPath("systemd-nspawn", os.Getenv("PATH"))
if err != nil {
return nil, nil, err
}
// Check dynamically which version is installed on the host
// Support version >= 220
versionBytes, err := exec.Command(hostNspawnBin, "--version").CombinedOutput()
if err != nil {
return nil, nil, errwrap.Wrap(fmt.Errorf("unable to probe %s version", hostNspawnBin), err)
}
versionStr := strings.SplitN(string(versionBytes), "\n", 2)[0]
var version int
n, err := fmt.Sscanf(versionStr, "systemd %d", &version)
if err != nil {
return nil, nil, fmt.Errorf("cannot parse version: %q", versionStr)
}
if n != 1 || version < 220 {
return nil, nil, fmt.Errorf("rkt needs systemd-nspawn >= 220. %s version not supported: %v", hostNspawnBin, versionStr)
}
// Copy systemd, bash, etc. in stage1 at run-time
if err := installAssets(); err != nil {
return nil, nil, errwrap.Wrap(errors.New("cannot install assets from the host"), err)
}
args = append(args, hostNspawnBin)
args = append(args, "--boot") // Launch systemd in the pod
args = append(args, fmt.Sprintf("--register=true"))
if context := os.Getenv(common.EnvSELinuxContext); context != "" {
args = append(args, fmt.Sprintf("-Z%s", context))
}
if context := os.Getenv(common.EnvSELinuxMountContext); context != "" {
args = append(args, fmt.Sprintf("-L%s", context))
}
default:
return nil, nil, fmt.Errorf("unrecognized stage1 flavor: %q", flavor)
}
// link journal only if the host is running systemd
if util.IsRunningSystemd() {
// we write /etc/machine-id here because systemd-nspawn needs it to link
// the container's journal to the host
mPath := filepath.Join(common.Stage1RootfsPath(p.Root), "etc", "machine-id")
mID := strings.Replace(p.UUID.String(), "-", "", -1)
if err := ioutil.WriteFile(mPath, []byte(mID), 0644); err != nil {
log.FatalE("error writing /etc/machine-id", err)
}
args = append(args, "--link-journal=try-guest")
keepUnit, err := util.RunningFromSystemService()
if err != nil {
if err == util.ErrSoNotFound {
log.Print("warning: libsystemd not found even though systemd is running. Cgroup limits set by the environment (e.g. a systemd service) won't be enforced.")
} else {
return nil, nil, errwrap.Wrap(errors.New("error determining if we're running from a system service"), err)
}
}
if keepUnit {
args = append(args, "--keep-unit")
}
}
if !debug {
args = append(args, "--quiet") // silence most nspawn output (log_warning is currently not covered by this)
env = append(env, "SYSTEMD_LOG_LEVEL=err") // silence log_warning too
}
env = append(env, "SYSTEMD_NSPAWN_CONTAINER_SERVICE=rkt")
if len(privateUsers) > 0 {
args = append(args, "--private-users="+privateUsers)
}
nsargs, err := stage1initcommon.PodToNspawnArgs(p)
if err != nil {
return nil, nil, errwrap.Wrap(errors.New("failed to generate nspawn args"), err)
}
args = append(args, nsargs...)
// Arguments to systemd
args = append(args, "--")
args = append(args, "--default-standard-output=tty") // redirect all service logs straight to tty
if !debug {
args = append(args, "--log-target=null") // silence systemd output inside pod
args = append(args, "--show-status=0") // silence systemd initialization status output
}
return args, env, nil
}
开发者ID:carriercomm,项目名称:rkt,代码行数:101,代码来源:init.go
示例16: CheckApply
// CheckApply checks the resource state and applies the resource if the bool
// input is true. It returns error info and if the state check passed or not.
func (obj *SvcRes) CheckApply(apply bool) (checkOK bool, err error) {
if !systemdUtil.IsRunningSystemd() {
return false, fmt.Errorf("Systemd is not running.")
}
conn, err := systemd.NewSystemdConnection() // needs root access
if err != nil {
return false, errwrap.Wrapf(err, "Failed to connect to systemd")
}
defer conn.Close()
var svc = fmt.Sprintf("%s.service", obj.Name) // systemd name
loadstate, err := conn.GetUnitProperty(svc, "LoadState")
if err != nil {
return false, errwrap.Wrapf(err, "Failed to get load state")
}
// NOTE: we have to compare variants with other variants, they are really strings...
var notFound = (loadstate.Value == dbus.MakeVariant("not-found"))
if notFound {
return false, errwrap.Wrapf(err, "Failed to find svc: %s", svc)
}
// XXX: check svc "enabled at boot" or not status...
//conn.GetUnitProperties(svc)
activestate, err := conn.GetUnitProperty(svc, "ActiveState")
if err != nil {
return false, errwrap.Wrapf(err, "Failed to get active state")
}
var running = (activestate.Value == dbus.MakeVariant("active"))
var stateOK = ((obj.State == "") || (obj.State == "running" && running) || (obj.State == "stopped" && !running))
var startupOK = true // XXX: DETECT AND SET
var refresh = obj.Refresh() // do we have a pending reload to apply?
if stateOK && startupOK && !refresh {
return true, nil // we are in the correct state
}
// state is not okay, no work done, exit, but without error
if !apply {
return false, nil
}
// apply portion
log.Printf("%s[%s]: Apply", obj.Kind(), obj.GetName())
var files = []string{svc} // the svc represented in a list
if obj.Startup == "enabled" {
_, _, err = conn.EnableUnitFiles(files, false, true)
} else if obj.Startup == "disabled" {
_, err = conn.DisableUnitFiles(files, false)
}
if err != nil {
return false, errwrap.Wrapf(err, "Unable to change startup status")
}
// XXX: do we need to use a buffered channel here?
result := make(chan string, 1) // catch result information
if obj.State == "running" {
_, err = conn.StartUnit(svc, "fail", result)
if err != nil {
return false, errwrap.Wrapf(err, "Failed to start unit")
}
if refresh {
log.Printf("%s[%s]: Skipping reload, due to pending start", obj.Kind(), obj.GetName())
}
refresh = false // we did a start, so a reload is not needed
} else if obj.State == "stopped" {
_, err = conn.StopUnit(svc, "fail", result)
if err != nil {
return false, errwrap.Wrapf(err, "Failed to stop unit")
}
if refresh {
log.Printf("%s[%s]: Skipping reload, due to pending stop", obj.Kind(), obj.GetName())
}
refresh = false // we did a stop, so a reload is not needed
}
status := <-result
if &status == nil {
return false, fmt.Errorf("Systemd service action result is nil")
}
if status != "done" {
return false, fmt.Errorf("Unknown systemd return string: %v", status)
}
if refresh { // we need to reload the service
// XXX: run a svc reload here!
log.Printf("%s[%s]: Reloading...", obj.Kind(), obj.GetName())
}
// XXX: also set enabled on boot
//.........这里部分代码省略.........
开发者ID:purpleidea,项目名称:mgmt,代码行数:101,代码来源:svc.go
示例17:
"UsageBytes": bounded(1*mb, 10*gb),
"WorkingSetBytes": bounded(1*mb, 10*gb),
"RSSBytes": bounded(1*mb, 1*gb),
"PageFaults": bounded(1000, 1E9),
"MajorPageFaults": bounded(0, 100000),
}),
"Rootfs": BeNil(),
"Logs": BeNil(),
"UserDefinedMetrics": BeEmpty(),
})
systemContainers := gstruct.Elements{
"kubelet": sysContExpectations,
"runtime": sysContExpectations,
}
// The Kubelet only manages the 'misc' system container if the host is not running systemd.
if !systemdutil.IsRunningSystemd() {
framework.Logf("Host not running systemd; expecting 'misc' system container.")
systemContainers["misc"] = sysContExpectations
}
// Expectations for pods.
podExpectations := gstruct.MatchAllFields(gstruct.Fields{
"PodRef": gstruct.Ignore(),
"StartTime": recent(maxStartAge),
"Containers": gstruct.MatchAllElements(summaryObjectID, gstruct.Elements{
"busybox-container": gstruct.MatchAllFields(gstruct.Fields{
"Name": Equal("busybox-container"),
"StartTime": recent(maxStartAge),
"CPU": ptrMatchAllFields(gstruct.Fields{
"Time": recent(maxStatsAge),
"UsageNanoCores": bounded(100000, 100000000),
"UsageCoreNanoSeconds": bounded(10000000, 1000000000),
开发者ID:nak3,项目名称:kubernetes,代码行数:31,代码来源:summary_test.go
示例18: TestServiceFile
func TestServiceFile(t *testing.T) {
if !sd_util.IsRunningSystemd() {
t.Skip("Systemd is not running on the host.")
}
ctx := testutils.NewRktRunCtx()
defer ctx.Cleanup()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
conn, err := sd_dbus.New()
if err != nil {
t.Fatal(err)
}
imageFile := getInspectImagePath()
image, err := filepath.Abs(imageFile)
if err != nil {
t.Fatal(err)
}
opts := "-- --print-msg=HelloWorld --sleep=1000"
cmd := fmt.Sprintf("%s --insecure-options=image run --mds-register=false --set-env=MESSAGE_LOOP=1000 %s %s", ctx.Cmd(), image, opts)
props := []sd_dbus.Property{
sd_dbus.PropExecStart(strings.Split(cmd, " "), false),
}
target := fmt.Sprintf("rkt-testing-transient-%d.service", r.Int())
reschan := make(chan string)
_, err = conn.StartTransientUnit(target, "replace", props, reschan)
if err != nil {
t.Fatal(err)
}
job := <-reschan
if job != "done" {
t.Fatal("Job is not done:", job)
}
units, err := conn.ListUnits()
var found bool
for _, u := range units {
if u.Name == target {
found = true
if u.ActiveState != "active" {
t.Fatalf("Test unit %s not active: %s (target: %s)", u.Name, u.ActiveState, target)
}
}
}
if !found {
t.Fatalf("Test unit not found in list")
}
// Run the unit for 10 seconds. You can check the logs manually in journalctl
time.Sleep(10 * time.Second)
// Stop the unit
_, err = conn.StopUnit(target, "replace", reschan)
if err != nil {
t.Fatal(err)
}
// wait for StopUnit job to complete
<-reschan
units, err = conn.ListUnits()
found = false
for _, u := range units {
if u.Name == target {
found = true
}
}
if found {
t.Fatalf("Test unit found in list, should be stopped")
}
}
开发者ID:sinfomicien,项目名称:rkt,代码行数:81,代码来源:rkt_service_file_test.go
示例19: UseSystemd
func UseSystemd() bool {
if !systemdUtil.IsRunningSystemd() {
return false
}
connLock.Lock()
defer connLock.Unlock()
if theConn == nil {
var err error
theConn, err = systemdDbus.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", nil, nil); err != nil {
if dbusError, ok := err.(dbus.Error); ok {
if dbusError.Name == "org.freedesktop.DBus.Error.UnknownMethod" {
hasStartTransientUnit = false
return hasStartTransientUnit
}
}
}
// Ensure the scope name we use doesn't exist. Use the Pid to
// avoid collisions between multiple libcontainer users on a
// single host.
scope := fmt.Sprintf("libcontainer-%d-systemd-test-default-dependencies.scope", os.Getpid())
testScopeExists := true
for i := 0; i <= testScopeWait; i++ {
if _, err := theConn.StopUnit(scope, "replace", nil); err != nil {
if dbusError, ok := err.(dbus.Error); ok {
if strings.Contains(dbusError.Name, "org.freedesktop.systemd1.NoSuchUnit") {
testScopeExists = false
break
}
}
}
time.Sleep(time.Millisecond)
}
// Bail out if we can't kill this scope without testing for DefaultDependencies
if testScopeExists {
return hasStartTransientUnit
}
// Assume StartTransientUnit on a scope allows DefaultDependencies
hasTransientDefaultDependencies = true
ddf := newProp("DefaultDependencies", false)
if _, err := theConn.StartTransientUnit(scope, "replace", []systemdDbus.Property{ddf}, nil); err != nil {
if dbusError, ok := err.(dbus.Error); ok {
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") {
hasTransientDefaultDependencies = false
}
}
}
// Not critical because of the stop unit logic above.
theConn.StopUnit(scope, "replace", nil)
// Assume StartTransientUnit on a scope allows Delegate
hasDelegate = true
dl := newProp("Delegate", true)
if _, err := theConn.StartTransientUnit(scope, "replace", []systemdDbus.Property{dl}, nil); err != nil {
if dbusError, ok := err.(dbus.Error); ok {
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") {
hasDelegate = false
}
}
}
// Assume we have the ability to start a transient unit as a slice
// This was broken until systemd v229, but has been back-ported on RHEL environments >= 219
// For details, see: https://bugzilla.redhat.com/show_bug.cgi?id=1370299
hasStartTransientSliceUnit = true
// To ensure simple clean-up, we create a slice off the root with no hierarchy
slice := fmt.Sprintf("libcontainer_%d_systemd_test_default.slice", os.Getpid())
if _, err := theConn.StartTransientUnit(slice, "replace", nil, nil); err != nil {
if _, ok := err.(dbus.Error); ok {
hasStartTransientSliceUnit = false
}
}
for i := 0; i <= testSliceWait; i++ {
if _, err := theConn.StopUnit(slice, "replace", nil); err != nil {
if dbusError, ok := err.(dbus.Error); ok {
if strings.Contains(dbusError.Name, "org.freedesktop.systemd1.NoSuchUnit") {
hasStartTransientSliceUnit = false
break
}
}
} else {
break
}
time.Sleep(time.Millisecond)
//.........这里部分代码省略.........
开发者ID:coolljt0725,项目名称:runc,代码行数:101,代码来源:apply_systemd.go
示例20: Watch
// Service watcher
func (obj *ServiceType) Watch() {
if obj.IsWatching() {
return
}
obj.SetWatching(true)
defer obj.SetWatching(false)
// obj.Name: service name
//vertex := obj.GetVertex() // stored with SetVertex
if !util.IsRunningSystemd() {
log.Fatal("Systemd is not running.")
}
conn, err := systemd.NewSystemdConnection() // needs root access
if err != nil {
log.Fatal("Failed to connect to systemd: ", err)
}
defer conn.Close()
bus, err := dbus.SystemBus()
if err != nil {
log.Fatal("Failed to connect to bus: ", err)
}
// XXX: will this detect new units?
bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0,
"type='signal',interface='org.freedesktop.systemd1.Manager',member='Reloading'")
buschan := make(chan *dbus.Signal, 10)
bus.Signal(buschan)
var service = fmt.Sprintf("%v.service", obj.Name) // systemd name
var send = false // send event?
var dirty = false
var invalid = false // does the service exist or not?
var previous bool // previous invalid value
set := conn.NewSubscriptionSet() // no error should be returned
subChannel, subErrors := set.Subscribe()
var activeSet = false
for {
// XXX: watch for an event for new units...
// XXX: detect if startup enabled/disabled value changes...
previous = invalid
invalid = false
// firstly, does service even exist or not?
loadstate, err := conn.GetUnitProperty(service, "LoadState")
if err != nil {
log.Printf("Failed to get property: %v", err)
invalid = true
}
if !invalid {
var notFound = (loadstate.Value == dbus.MakeVariant("not-found"))
if notFound { // XXX: in the loop we'll handle changes better...
log.Printf("Failed to find service: %v", service)
invalid = true // XXX ?
}
}
if previous != invalid { // if invalid changed, send signal
send = true
dirty = true
}
if invalid {
log.Printf("Waiting for: %v", service) // waiting for service to appear...
if activeSet {
activeSet = false
set.Remove(service) // no return value should ever occur
}
obj.SetState(typeWatching) // reset
select {
case _ = <-buschan: // XXX wait for new units event to unstick
obj.SetConvergedState(typeConvergedNil)
// loop so that we can see the changed invalid signal
l
|
请发表评论