Example #1
0
func runStop(cmd *cobra.Command, args []string) (exit int) {
	var podUUIDs []string
	var errors int

	ret := 0
	switch {
	case len(args) == 0 && flagUUIDFile != "":
		podUUID, err := pkgPod.ReadUUIDFromFile(flagUUIDFile)
		if err != nil {
			stderr.PrintE("unable to resolve UUID from file", err)
			ret = 1
		} else {
			podUUIDs = append(podUUIDs, podUUID)
		}

	case len(args) > 0 && flagUUIDFile == "":
		podUUIDs = args

	default:
		cmd.Usage()
		return 254
	}

	for _, podUUID := range podUUIDs {
		p, err := pkgPod.PodFromUUIDString(getDataDir(), podUUID)
		if err != nil {
			errors++
			stderr.PrintE("cannot get pod", err)
			continue
		}

		if p.AfterRun() {
			stdout.Printf("pod %q is already stopped", p.UUID)
			continue
		}

		if p.State() != pkgPod.Running {
			stderr.Error(fmt.Errorf("pod %q is not running", p.UUID))
			errors++
			continue
		}

		if err := stage0.StopPod(p.Path(), flagForce, p.UUID); err == nil {
			stdout.Printf("%q", p.UUID)
		} else {
			stderr.PrintE(fmt.Sprintf("error stopping %q", p.UUID), err)
			errors++
		}
	}

	if errors > 0 {
		stderr.Error(fmt.Errorf("failed to stop %d pod(s)", errors))
		return 254
	}

	return ret
}
Example #2
0
File: stop.go Project: nak3/rkt
func runStop(cmd *cobra.Command, args []string) (exit int) {
	var podUUID *types.UUID
	var podUUIDs []*types.UUID
	var err error
	var errors int

	ret := 0
	switch {
	case len(args) == 0 && flagUUIDFile != "":
		podUUID, err = readUUIDFromFile(flagUUIDFile)
		if err != nil {
			stderr.PrintE("unable to resolve UUID from file", err)
			ret = 1
		} else {
			podUUIDs = append(podUUIDs, podUUID)
		}

	case len(args) > 0 && flagUUIDFile == "":
		for _, uuid := range args {
			podUUID, err := resolveUUID(uuid)
			if err != nil {
				stderr.PrintE("unable to resolve UUID", err)
				ret = 1
			} else {
				podUUIDs = append(podUUIDs, podUUID)
			}
		}

	default:
		cmd.Usage()
		return 1
	}

	for _, podUUID = range podUUIDs {
		p, err := getPod(podUUID)
		if err != nil {
			errors++
			stderr.PrintE("cannot get pod", err)
		}

		if !p.isRunning() {
			stderr.Error(fmt.Errorf("pod %q is not running", p.uuid))
			errors++
			continue
		}

		if err := stage0.StopPod(p.path(), flagForce, podUUID); err == nil {
			stdout.Printf("%q", p.uuid)
		} else {
			stderr.PrintE(fmt.Sprintf("error stopping %q", p.uuid), err)
			errors++
		}
	}

	if errors > 0 {
		stderr.Error(fmt.Errorf("failed to stop %d pod(s)", errors))
		return 1
	}

	return ret
}