예제 #1
0
func getReferencedImgs(s *store.Store) (map[string]struct{}, error) {
	imgs := map[string]struct{}{}
	walkErrors := []error{}
	// Consider pods in preparing, prepared, run, exitedgarbage state
	if err := walkPods(includeMostDirs, func(p *pod) {
		appImgs, err := p.getAppsHashes()
		if err != nil {
			// Ignore errors reading/parsing pod file
			return
		}
		for _, appImg := range appImgs {
			key, err := s.ResolveKey(appImg.String())
			if err != nil && err != store.ErrKeyNotFound {
				walkErrors = append(walkErrors, fmt.Errorf("bad imageID %q in pod definition: %v", appImg.String(), err))
				return
			}
			imgs[key] = struct{}{}
		}
	}); err != nil {
		return nil, fmt.Errorf("failed to get pod handles: %v", err)
	}
	if len(walkErrors) > 0 {
		return nil, fmt.Errorf("errors occured walking pods. errors: %v", walkErrors)

	}
	return imgs, nil
}
예제 #2
0
파일: image_rm.go 프로젝트: ngorskig/rkt
func rmImages(s *store.Store, images []string) error {
	done := 0
	errors := 0
	staleErrors := 0
	for _, pkey := range images {
		errors++
		h, err := types.NewHash(pkey)
		if err != nil {
			stderr("rkt: wrong image ID %q: %v", pkey, err)
			continue
		}
		key, err := s.ResolveKey(h.String())
		if err != nil {
			stderr("rkt: image ID %q not valid: %v", pkey, err)
			continue
		}
		if key == "" {
			stderr("rkt: image ID %q doesn't exist", pkey)
			continue
		}

		if err = s.RemoveACI(key); err != nil {
			if serr, ok := err.(*store.StoreRemovalError); ok {
				staleErrors++
				stderr("rkt: some files cannot be removed for image ID %q: %v", pkey, serr)
			} else {
				stderr("rkt: error removing aci for image ID %q: %v", pkey, err)
				continue
			}
		}
		stdout("rkt: successfully removed aci for image ID: %q", pkey)
		errors--
		done++
	}

	if done > 0 {
		stderr("rkt: %d image(s) successfully removed", done)
	}

	// If anything didn't complete, return exit status of 1
	if (errors + staleErrors) > 0 {
		if staleErrors > 0 {
			stderr("rkt: %d image(s) removed but left some stale files", staleErrors)
		}
		if errors > 0 {
			stderr("rkt: %d image(s) cannot be removed", errors)
		}
		return fmt.Errorf("error(s) found while removing images")
	}

	return nil
}
예제 #3
0
파일: images.go 프로젝트: ChengTiesheng/rkt
func getStoreKeyFromAppOrHash(s *store.Store, input string) (string, error) {
	var key string
	if _, err := types.NewHash(input); err == nil {
		key, err = s.ResolveKey(input)
		if err != nil {
			return "", fmt.Errorf("cannot resolve key: %v", err)
		}
	} else {
		key, err = getStoreKeyFromApp(s, input)
		if err != nil {
			return "", fmt.Errorf("cannot find image: %v", err)
		}
	}
	return key, nil
}
예제 #4
0
func getStoreKeyFromAppOrHash(s *store.Store, input string) (string, error) {
	var key string
	if _, err := types.NewHash(input); err == nil {
		key, err = s.ResolveKey(input)
		if err != nil {
			return "", errwrap.Wrap(errors.New("cannot resolve image ID"), err)
		}
	} else {
		key, err = getStoreKeyFromApp(s, input)
		if err != nil {
			return "", errwrap.Wrap(errors.New("cannot find image"), err)
		}
	}
	return key, nil
}
예제 #5
0
파일: image.go 프로젝트: jsarenik/rkt
func getKeyFromAppOrHash(s *store.Store, input string) (string, error) {
	var key string
	if _, err := types.NewHash(input); err == nil {
		key, err = s.ResolveKey(input)
		if err != nil {
			return "", fmt.Errorf("cannot resolve key: %v", err)
		}
	} else {
		app, err := discovery.NewAppFromString(input)
		if err != nil {
			return "", fmt.Errorf("cannot parse the image name: %v", err)
		}
		labels, err := types.LabelsFromMap(app.Labels)
		if err != nil {
			return "", fmt.Errorf("invalid labels in the name: %v", err)
		}
		key, err = s.GetACI(app.Name, labels)
		if err != nil {
			return "", fmt.Errorf("cannot find image: %v", err)
		}
	}

	return key, nil
}
예제 #6
0
// getImageInfo for a given image ID, returns the *v1alpha.Image object.
func getImageInfo(store *store.Store, imageID string) (*v1alpha.Image, error) {
	key, err := store.ResolveKey(imageID)
	if err != nil {
		stderr.PrintE(fmt.Sprintf("failed to resolve the image ID %q", imageID), err)
		return nil, err
	}

	aciInfo, err := store.GetACIInfoWithBlobKey(key)
	if err != nil {
		stderr.PrintE(fmt.Sprintf("failed to get ACI info for image %q", key), err)
		return nil, err
	}

	image, _, err := aciInfoToV1AlphaAPIImage(store, aciInfo)
	if err != nil {
		stderr.PrintE(fmt.Sprintf("failed to convert ACI to v1alphaAPIImage for image %q", key), err)
		return nil, err
	}
	return image, nil
}
예제 #7
0
// fillAppInfo fills the apps' state and image info of the pod.
func fillAppInfo(store *store.Store, p *pod, v1pod *v1alpha.Pod) error {
	statusDir, err := p.getStatusDir()
	if err != nil {
		stderr.PrintE("failed to get pod exit status directory", err)
		return err
	}

	for _, app := range v1pod.Apps {
		// Fill app's image info (id, name, version).
		fullImageID, err := store.ResolveKey(app.Image.Id)
		if err != nil {
			stderr.PrintE(fmt.Sprintf("failed to resolve the image ID %q", app.Image.Id), err)
			return err
		}

		im, err := p.getAppImageManifest(*types.MustACName(app.Name))
		if err != nil {
			stderr.PrintE(fmt.Sprintf("failed to get image manifests for app %q", app.Name), err)
			return err
		}

		version, ok := im.Labels.Get("version")
		if !ok {
			version = "latest"
		}

		app.Image = &v1alpha.Image{
			BaseFormat: &v1alpha.ImageFormat{
				// Only support appc image now. If it's a docker image, then it
				// will be transformed to appc before storing in the disk store.
				Type:    v1alpha.ImageType_IMAGE_TYPE_APPC,
				Version: schema.AppContainerVersion.String(),
			},
			Id:      fullImageID,
			Name:    im.Name.String(),
			Version: version,
			// Other information are not available because they require the image
			// info from store.
		}

		// Fill app's state and exit code.
		value, err := p.readIntFromFile(filepath.Join(statusDir, app.Name))
		if err == nil {
			app.State = v1alpha.AppState_APP_STATE_EXITED
			app.ExitCode = int32(value)
			continue
		}

		if !os.IsNotExist(err) {
			stderr.PrintE(fmt.Sprintf("failed to read status for app %q", app.Name), err)
			return err
		}
		// If status file does not exit, that means the
		// app is either running or aborted.
		//
		// FIXME(yifan): This is not acttually true, the app can be aborted while
		// the pod is still running if the spec changes.
		switch p.getState() {
		case Running:
			app.State = v1alpha.AppState_APP_STATE_RUNNING
		default:
			app.State = v1alpha.AppState_APP_STATE_UNDEFINED
		}

	}
	return nil
}
예제 #8
0
파일: image_rm.go 프로젝트: Snaveena/rkt
func rmImages(s *store.Store, images []string) error {
	done := 0
	errors := 0
	staleErrors := 0
	imageMap := make(map[string]string)
	imageCounter := make(map[string]int)

	for _, pkey := range images {
		errors++
		h, err := types.NewHash(pkey)
		if err != nil {
			var found bool
			keys, found, err := s.ResolveName(pkey)
			if len(keys) > 0 {
				errors += len(keys) - 1
			}
			if err != nil {
				stderr("rkt: %v", err)
				continue
			}
			if !found {
				stderr("rkt: image name %q not found", pkey)
				continue
			}
			for _, key := range keys {
				imageMap[key] = pkey
				imageCounter[key]++
			}
		} else {
			key, err := s.ResolveKey(h.String())
			if err != nil {
				stderr("rkt: image ID %q not valid: %v", pkey, err)
				continue
			}
			if key == "" {
				stderr("rkt: image ID %q doesn't exist", pkey)
				continue
			}

			aciinfo, err := s.GetACIInfoWithBlobKey(key)
			if err != nil {
				stderr("rkt: error retrieving aci infos for image %q: %v", key, err)
				continue
			}
			imageMap[key] = aciinfo.Name
			imageCounter[key]++
		}
	}

	// Adjust the error count by subtracting duplicate IDs from it,
	// therefore allowing only one error per ID.
	for _, c := range imageCounter {
		if c > 1 {
			errors -= c - 1
		}
	}

	for key, name := range imageMap {
		if err := s.RemoveACI(key); err != nil {
			if serr, ok := err.(*store.StoreRemovalError); ok {
				staleErrors++
				stderr("rkt: some files cannot be removed for image %q (%q): %v", key, name, serr)
			} else {
				stderr("rkt: error removing aci for image %q (%q): %v", key, name, err)
				continue
			}
		}
		stdout("rkt: successfully removed aci for image: %q (%q)", key, name)
		errors--
		done++
	}

	if done > 0 {
		stderr("rkt: %d image(s) successfully removed", done)
	}

	// If anything didn't complete, return exit status of 1
	if (errors + staleErrors) > 0 {
		if staleErrors > 0 {
			stderr("rkt: %d image(s) removed but left some stale files", staleErrors)
		}
		if errors > 0 {
			stderr("rkt: %d image(s) cannot be removed", errors)
		}
		return fmt.Errorf("error(s) found while removing images")
	}

	return nil
}
예제 #9
0
파일: image_rm.go 프로젝트: matomesc/rkt
func rmImages(s *store.Store, images []string) error {
	done := 0
	errors := 0
	staleErrors := 0
	for _, pkey := range images {
		var (
			keys []string
			name string
		)
		errors++
		h, err := types.NewHash(pkey)
		if err != nil {
			var found bool
			keys, found, err = s.ResolveName(pkey)
			if len(keys) > 0 {
				errors += len(keys) - 1
			}
			if err != nil {
				stderr("rkt: %v", err)
				continue
			}
			if !found {
				stderr("rkt: image name %q not found", pkey)
				continue
			}
			name = pkey
		} else {
			key, err := s.ResolveKey(h.String())
			if err != nil {
				stderr("rkt: image ID %q not valid: %v", pkey, err)
				continue
			}
			if key == "" {
				stderr("rkt: image ID %q doesn't exist", pkey)
				continue
			}

			aciinfo, err := s.GetACIInfoWithBlobKey(key)
			if err != nil {
				stderr("rkt: error retrieving aci infos for image %q: %v", key, err)
				continue
			}
			name = aciinfo.Name
			keys = append(keys, key)
		}

		for _, key := range keys {
			if err = s.RemoveACI(key); err != nil {
				if serr, ok := err.(*store.StoreRemovalError); ok {
					staleErrors++
					stderr("rkt: some files cannot be removed for image %q (%q): %v", key, name, serr)
				} else {
					stderr("rkt: error removing aci for image %q (%q): %v", key, name, err)
					continue
				}
			}
			stdout("rkt: successfully removed aci for image: %q (%q)", key, name)
			errors--
			done++
		}
	}

	if done > 0 {
		stderr("rkt: %d image(s) successfully removed", done)
	}

	// If anything didn't complete, return exit status of 1
	if (errors + staleErrors) > 0 {
		if staleErrors > 0 {
			stderr("rkt: %d image(s) removed but left some stale files", staleErrors)
		}
		if errors > 0 {
			stderr("rkt: %d image(s) cannot be removed", errors)
		}
		return fmt.Errorf("error(s) found while removing images")
	}

	return nil
}