Beispiel #1
0
// getAppImageID returns the image id to enter
// If one was supplied in the flags then it's simply returned
// If the PM contains a single image, that image's id is returned
// If the PM has multiple images, the ids and names are printed and an error is returned
func getAppImageID(p *pod) (*types.Hash, error) {
	if !flagAppImageID.Empty() {
		return &flagAppImageID, nil
	}

	// figure out the image id, or show a list if multiple are present
	b, err := ioutil.ReadFile(common.PodManifestPath(p.path()))
	if err != nil {
		return nil, fmt.Errorf("error reading pod manifest: %v", err)
	}

	m := schema.PodManifest{}
	if err = m.UnmarshalJSON(b); err != nil {
		return nil, fmt.Errorf("unable to load manifest: %v", err)
	}

	switch len(m.Apps) {
	case 0:
		return nil, fmt.Errorf("pod contains zero apps")
	case 1:
		return &m.Apps[0].Image.ID, nil
	default:
	}

	stderr("Pod contains multiple apps:")
	for _, ra := range m.Apps {
		stderr("\t%s: %s", types.ShortHash(ra.Image.ID.String()), ra.Name.String())
	}

	return nil, fmt.Errorf("specify app using \"rkt enter --imageid ...\"")
}
Beispiel #2
0
// getAppName returns the app name to enter
// If one was supplied in the flags then it's simply returned
// If the PM contains a single app, that app's name is returned
// If the PM has multiple apps, the names are printed and an error is returned
func getAppName(p *pod) (*types.ACName, error) {
	if flagAppName != "" {
		return types.NewACName(flagAppName)
	}

	// figure out the app name, or show a list if multiple are present
	b, err := ioutil.ReadFile(common.PodManifestPath(p.path()))
	if err != nil {
		return nil, fmt.Errorf("error reading pod manifest: %v", err)
	}

	m := schema.PodManifest{}
	if err = m.UnmarshalJSON(b); err != nil {
		return nil, fmt.Errorf("unable to load manifest: %v", err)
	}

	switch len(m.Apps) {
	case 0:
		return nil, fmt.Errorf("pod contains zero apps")
	case 1:
		return &m.Apps[0].Name, nil
	default:
	}

	stderr("Pod contains multiple apps:")
	for _, ra := range m.Apps {
		stderr("\t%v", ra.Name)
	}

	return nil, fmt.Errorf("specify app using \"rkt enter --app= ...\"")
}
Beispiel #3
0
// getApps returns a list of apps in the pod
func (p *pod) getApps() (schema.AppList, error) {
	pmb, err := p.readFile("pod")
	if err != nil {
		return nil, fmt.Errorf("error reading pod manifest: %v", err)
	}
	pm := new(schema.PodManifest)
	if err = pm.UnmarshalJSON(pmb); err != nil {
		return nil, fmt.Errorf("invalid pod manifest: %v", err)
	}
	return pm.Apps, nil
}
Beispiel #4
0
func runList(cmd *cobra.Command, args []string) (exit int) {
	if !flagNoLegend {
		fmt.Fprintf(tabOut, "UUID\tACI\tSTATE\tNETWORKS\n")
	}

	if err := walkPods(includeMostDirs, func(p *pod) {
		m := schema.PodManifest{}
		app_zero := ""

		if !p.isPreparing && !p.isAbortedPrepare && !p.isExitedDeleting {
			// TODO(vc): we should really hold a shared lock here to prevent gc of the pod
			manifFile, err := p.readFile(common.PodManifestPath(""))
			if err != nil {
				stderr("Unable to read manifest: %v", err)
				return
			}

			err = m.UnmarshalJSON(manifFile)
			if err != nil {
				stderr("Unable to load manifest: %v", err)
				return
			}

			if len(m.Apps) == 0 {
				stderr("Pod contains zero apps")
				return
			}
			app_zero = m.Apps[0].Name.String()
		}

		uuid := ""
		if flagFullOutput {
			uuid = p.uuid.String()
		} else {
			uuid = p.uuid.String()[:8]
		}

		fmt.Fprintf(tabOut, "%s\t%s\t%s\t%s\n", uuid, app_zero, p.getState(), fmtNets(p.nets))
		for i := 1; i < len(m.Apps); i++ {
			fmt.Fprintf(tabOut, "\t%s\n", m.Apps[i].Name.String())
		}
	}); err != nil {
		stderr("Failed to get pod handles: %v", err)
		return 1
	}

	tabOut.Flush()
	return 0
}
Beispiel #5
0
// getAppCount returns the app count of a pod. It can only be called on prepared pods.
func (p *pod) getAppCount() (int, error) {
	if !p.isPrepared {
		return -1, fmt.Errorf("error: only prepared pods can get their app count")
	}

	b, err := ioutil.ReadFile(common.PodManifestPath(p.path()))
	if err != nil {
		return -1, fmt.Errorf("error reading pod manifest: %v", err)
	}

	m := schema.PodManifest{}
	if err = m.UnmarshalJSON(b); err != nil {
		return -1, fmt.Errorf("unable to load manifest: %v", err)
	}

	return len(m.Apps), nil
}
Beispiel #6
0
func runList(cmd *cobra.Command, args []string) int {
	s, err := store.NewStore(globalFlags.Dir)
	if err != nil {
		stderr("list: cannot open store: %v", err)
		return 1
	}

	var errors []error
	tabBuffer := new(bytes.Buffer)
	tabOut := getTabOutWithWriter(tabBuffer)

	if !flagNoLegend {
		if flagFullOutput {
			fmt.Fprintf(tabOut, "UUID\tAPP\tIMAGE NAME\tIMAGE ID\tSTATE\tNETWORKS\n")
		} else {
			fmt.Fprintf(tabOut, "UUID\tAPP\tIMAGE NAME\tSTATE\tNETWORKS\n")
		}
	}

	if err := walkPods(includeMostDirs, func(p *pod) {
		pm := schema.PodManifest{}

		if !p.isPreparing && !p.isAbortedPrepare && !p.isExitedDeleting {
			// TODO(vc): we should really hold a shared lock here to prevent gc of the pod
			pmf, err := p.readFile(common.PodManifestPath(""))
			if err != nil {
				errors = append(errors, newPodListReadError(p, err))
				return
			}

			if err := pm.UnmarshalJSON(pmf); err != nil {
				errors = append(errors, newPodListLoadError(p, err, pmf))
				return
			}

			if len(pm.Apps) == 0 {
				errors = append(errors, newPodListZeroAppsError(p))
				return
			}
		}

		type printedApp struct {
			uuid    string
			appName string
			imgName string
			imgID   string
			state   string
			nets    string
		}

		var appsToPrint []printedApp
		uuid := p.uuid.String()
		state := p.getState()
		nets := fmtNets(p.nets)
		if !flagFullOutput {
			uuid = uuid[:8]
		}
		for _, app := range pm.Apps {
			// Retrieve the image from the store.
			imj, err := s.GetImageManifestJSON(app.Image.ID.String())
			if err != nil {
				errors = append(errors, newPodListImageStoreFailure(p, err, &pm, app))
				return
			}
			var im *schema.ImageManifest
			if err = json.Unmarshal(imj, &im); err != nil {
				errors = append(errors, newPodListImageLoadFailure(p, err, &pm, imj, app))
				return
			}

			imageName := im.Name.String()
			if version, ok := im.Labels.Get("version"); ok {
				imageName = fmt.Sprintf("%s:%s", imageName, version)
			}

			var imageID string
			if flagFullOutput {
				imageID = app.Image.ID.String()[:19]
			}

			appsToPrint = append(appsToPrint, printedApp{
				uuid:    uuid,
				appName: app.Name.String(),
				imgName: imageName,
				imgID:   imageID,
				state:   state,
				nets:    nets,
			})
			// clear those variables so they won't be
			// printed for another apps in the pod as they
			// are actually describing a pod, not an app
			uuid = ""
			state = ""
			nets = ""
		}
		// if we reached that point, then it means that the
		// pod and all its apps are valid, so they can be
		// printed
		for _, app := range appsToPrint {
			if flagFullOutput {
				fmt.Fprintf(tabOut, "%s\t%s\t%s\t%s\t%s\t%s\n", app.uuid, app.appName, app.imgName, app.imgID, app.state, app.nets)
			} else {
				fmt.Fprintf(tabOut, "%s\t%s\t%s\t%s\t%s\n", app.uuid, app.appName, app.imgName, app.state, app.nets)
			}
		}

	}); err != nil {
		stderr("Failed to get pod handles: %v", err)
		return 1
	}

	if len(errors) > 0 {
		sep := "----------------------------------------"
		stderr("%d error(s) encountered when listing pods:", len(errors))
		stderr("%s", sep)
		for _, err := range errors {
			stderr("%s", err.Error())
			stderr("%s", sep)
		}
		stderr("Misc:")
		stderr("  rkt's appc version: %s", schema.AppContainerVersion)
		stderr("%s", sep)
		// make a visible break between errors and the listing
		stderr("")
	}

	tabOut.Flush()
	stdout("%s", tabBuffer.String())
	return 0
}
Beispiel #7
0
func runList(cmd *cobra.Command, args []string) (exit int) {
	s, err := store.NewStore(globalFlags.Dir)
	if err != nil {
		stderr("list: cannot open store: %v", err)
		return 1
	}

	if !flagNoLegend {
		fmt.Fprintf(tabOut, "UUID\tAPP\tACI\tSTATE\tNETWORKS\n")
	}

	if err := walkPods(includeMostDirs, func(p *pod) {
		pm := schema.PodManifest{}

		if !p.isPreparing && !p.isAbortedPrepare && !p.isExitedDeleting {
			// TODO(vc): we should really hold a shared lock here to prevent gc of the pod
			pmf, err := p.readFile(common.PodManifestPath(""))
			if err != nil {
				stderr("Unable to read pod manifest: %v", err)
				return
			}

			if err := pm.UnmarshalJSON(pmf); err != nil {
				stderr("Unable to load manifest: %v", err)
				return
			}

			if len(pm.Apps) == 0 {
				stderr("Pod contains zero apps")
				return
			}
		}

		uuid := ""
		if flagFullOutput {
			uuid = p.uuid.String()
		} else {
			uuid = p.uuid.String()[:8]
		}

		for i, app := range pm.Apps {
			// Retrieve the image from the store.
			im, err := s.GetImageManifest(app.Image.ID.String())
			if err != nil {
				stderr("Unable to load image manifest: %v", err)
			}

			if i == 0 {
				fmt.Fprintf(tabOut, "%s\t%s\t%s\t%s\t%s\n", uuid, app.Name.String(), im.Name.String(), p.getState(), fmtNets(p.nets))
			} else {
				fmt.Fprintf(tabOut, "\t%s\t%s\t\t\n", app.Name.String(), im.Name.String())
			}
		}

	}); err != nil {
		stderr("Failed to get pod handles: %v", err)
		return 1
	}

	tabOut.Flush()
	return 0
}