Exemple #1
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
}
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
}
Exemple #3
0
func mountAction(cmd *cobra.Command, args []string) {
	if len(args) != 3 {
		onErr(errors.New("unexpected number of arguments"))
	}

	vol := parseJSONArg(args[2])

	mountdir := args[0]
	mountdev := args[1]
	oneshotsvc := devToUnitName(mountdir)
	// mountsvc := pathToMountName(mountdir)

	me, err := osext.Executable()
	if err != nil {
		onErr(err)
	}

	flags := "noatime"
	if vol.Trim {
		flags = "noatime,discard"
	}

	ch := make(chan string)
	// ch2 := make(chan string)

	sysd := connectSystemd()

	sysd.ResetFailedUnit(oneshotsvc)
	_, err = sysd.StartTransientUnit(oneshotsvc, "fail", []dbus.Property{
		dbus.Property{
			Name:  "Type",
			Value: godbus.MakeVariant("oneshot"),
		},
		dbus.PropExecStart([]string{
			me,
			"flexprepvol",
			mountdev,
			vol.FSType,
		}, false),
	}, ch)
	if err != nil {
		onErr(err)
	}
	s := <-ch
	if s == "failed" {
		onErr(errors.New(s))
	}
	// _, err = sysd.StartTransientUnit(mountsvc, "fail", []dbus.Property{
	// 	dbus.Property{
	// 		Name:  "What",
	// 		Value: godbus.MakeVariant(mountdev),
	// 	},
	// 	dbus.Property{
	// 		Name:  "Where",
	// 		Value: godbus.MakeVariant(mountdir),
	// 	},
	// 	dbus.Property{
	// 		Name:  "Type",
	// 		Value: godbus.MakeVariant(vol.FSType),
	// 	},
	// 	dbus.Property{
	// 		Name:  "Options",
	// 		Value: godbus.MakeVariant(flags),
	// 	},
	// }, ch2)
	// if err != nil {
	// 	onErr(err)
	// }
	// status := sysd.wait(mountsvc)
	// if status == "failed" {
	// 	onErr(errors.New("Couldn't attach"))
	// } else if status == "active" {
	// 	writeResponse(Response{
	// 		Status: "Success",
	// 		Device: mountdev,
	// 	})
	// }
	if err := os.MkdirAll(mountdir, os.ModeDir|0555); err != nil {
		onErr(err)
	}

	ex := exec.Command("mount", "-t", vol.FSType, "-o", flags, mountdev, mountdir)
	_, err = ex.CombinedOutput()
	if err != nil {
		onErr(err)
	}
	writeResponse(Response{
		Status: "Success",
		Device: mountdev,
	})
	os.Exit(0)
}
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")
	}
}
Exemple #5
0
//{"kubernetes.io/fsType":"ext4","kubernetes.io/readwrite":"rw","volume":"block1"}
func attachAction(cmd *cobra.Command, args []string) {
	if len(args) != 1 {
		onErr(errors.New("unexpected number of arguments"))
	}
	vol := parseJSONArg(args[0])

	dev, err := nbd.FindDevice()
	if err != nil {
		onErr(err)
	}

	sysd := connectSystemd()

	svc := devToUnitName(dev)

	me, err := osext.Executable()
	if err != nil {
		onErr(err)
	}

	cmdList := []string{
		me,
		"-C",
		vol.Etcd,
		"nbd",
		vol.VolumeName,
		dev,
	}
	if vol.WriteLevel != "" {
		_, err := torus.ParseWriteLevel(vol.WriteLevel)
		if err != nil {
			onErr(err)
		}
		cmdList = append(cmdList, []string{"--write-level", vol.WriteLevel}...)
	}
	if vol.WriteCacheSize != "" {
		cmdList = append(cmdList, []string{"--write-cache-size", vol.WriteCacheSize}...)
	}

	ch := make(chan string)

	sysd.ResetFailedUnit(svc)
	_, err = sysd.StartTransientUnit(svc, "fail", []dbus.Property{
		dbus.PropExecStart(cmdList, false),
	}, ch)
	if err != nil {
		onErr(err)
	}
	<-ch
	status := sysd.wait(svc)
	if status == "failed" {
		onErr(errors.New("Couldn't attach"))
	} else if status == "active" {
		writeResponse(Response{
			Status: "Success",
			Device: dev,
		})
	} else {
		onErr(errors.New(status))
	}
	os.Exit(0)
}