Ejemplo n.º 1
0
func listUnits(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		args = []string{LocalHostName}
	}
	ids, err := NewHostLocators(args...)
	if err != nil {
		Fail(1, "You must pass zero or more valid host names (use '%s' or pass no arguments for the current server): %s\n", LocalHostName, err.Error())
	}

	if len(ids) == 1 && !ids[0].IsRemote() {
		fmt.Fprintf(os.Stderr, "You can also display the set of containers via 'systemctl list-units'\n")
	}
	data, errors := Executor{
		On: ids,
		Group: func(on ...Locator) jobs.Job {
			return &http.HttpListContainersRequest{
				Label: string(on[0].HostIdentity()),
				ListContainersRequest: jobs.ListContainersRequest{},
			}
		},
		Output:    os.Stdout,
		LocalInit: needsSystemd,
	}.Gather()

	combined := http.ListContainersResponse{}
	for i := range data {
		if r, ok := data[i].(*http.ListContainersResponse); ok {
			combined.Append(&r.ListContainersResponse)
		} else if j, ok := data[i].(*jobs.ListContainersResponse); ok {
			combined.Append(j)
		}
	}
	combined.Sort()
	combined.WriteTableTo(os.Stdout)
	if len(errors) > 0 {
		for i := range errors {
			fmt.Fprintf(os.Stderr, "Error: %s\n", errors[i])
		}
		os.Exit(1)
	}
	os.Exit(0)
}
Ejemplo n.º 2
0
func listUnits(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		args = []string{transport.Local.String()}
	}
	servers, err := NewHostLocators(defaultTransport.Get(), args[0:]...)
	if err != nil {
		Fail(1, "You must pass zero or more valid host names (use '%s' or pass no arguments for the current server): %s", transport.Local.String(), err.Error())
	}

	data, errors := Executor{
		On: servers,
		Group: func(on ...Locator) jobs.Job {
			return &cjobs.ListContainersRequest{on[0].TransportLocator().String()}
		},
		Output:    os.Stdout,
		LocalInit: needsSystemd,
		Transport: defaultTransport.Get(),
	}.Gather()

	combined := http.ListContainersResponse{}
	for i := range data {
		if r, ok := data[i].(*http.ListContainersResponse); ok {
			combined.Append(&r.ListContainersResponse)
		} else if j, ok := data[i].(*cjobs.ListContainersResponse); ok {
			combined.Append(j)
		}
	}
	combined.Sort()
	combined.WriteTableTo(os.Stdout)
	if len(errors) > 0 {
		for i := range errors {
			fmt.Fprintf(os.Stderr, "Error: %s\n", errors[i])
		}
		os.Exit(1)
	}
	os.Exit(0)
}