Ejemplo n.º 1
0
// CreateImageStore creates an image store
func CreateImageStore(storename string) error {
	defer trace.End(trace.Begin(storename))

	transport := httptransport.New(options.host, "/", []string{"http"})
	client := apiclient.New(transport, nil)

	log.Debugf("Creating a store from input %s", storename)

	body := &models.ImageStore{Name: storename}

	_, err := client.Storage.CreateImageStore(
		storage.NewCreateImageStoreParamsWithContext(ctx).WithBody(body),
	)
	if _, ok := err.(*storage.CreateImageStoreConflict); ok {
		log.Debugf("Store already exists")
		return nil
	}
	if err != nil {
		log.Debugf("Creating a store failed: %s", err)

		return err
	}
	log.Debugf("Created a store %#v", body)

	return nil
}
Ejemplo n.º 2
0
// Update runs only once at startup to hydrate the image cache
func (ic *ICache) Update(client *client.PortLayer) error {
	log.Debugf("Updating image cache...")

	host, err := sys.UUID()
	if host == "" {
		host, err = os.Hostname()
	}
	if err != nil {
		return fmt.Errorf("Unexpected error getting hostname: %s", err)
	}

	// attempt to create the image store if it doesn't exist
	store := &models.ImageStore{Name: host}
	_, err = client.Storage.CreateImageStore(
		storage.NewCreateImageStoreParamsWithContext(ctx).WithBody(store),
	)

	if err != nil {
		if _, ok := err.(*storage.CreateImageStoreConflict); ok {
			log.Debugf("Store already exists")
		} else {
			log.Debugf("Creating a store failed: %#v", err)
			return err
		}
	}

	params := storage.NewListImagesParamsWithContext(ctx).WithStoreName(host)

	layers, err := client.Storage.ListImages(params)
	if err != nil {
		return fmt.Errorf("Failed to retrieve image list from portlayer: %s", err)
	}

	for _, layer := range layers.Payload {
		imageConfig := &metadata.ImageConfig{}
		if err := json.Unmarshal([]byte(layer.Metadata["metaData"]), imageConfig); err != nil {
			derr.NewErrorWithStatusCode(fmt.Errorf("Failed to unmarshal image config: %s", err),
				http.StatusInternalServerError)
		}

		if imageConfig.ImageID != "" {
			ic.AddImage(imageConfig)
		}
	}

	return nil
}
Ejemplo n.º 3
0
func createImageStore() error {
	// TODO(jzt): we should move this to a utility package or something
	host, err := sys.UUID()
	if err != nil {
		log.Errorf("Failed to determine host UUID")
		return err
	}

	// attempt to create the image store if it doesn't exist
	store := &models.ImageStore{Name: host}
	_, err = portLayerClient.Storage.CreateImageStore(
		storage.NewCreateImageStoreParamsWithContext(ctx).WithBody(store),
	)

	if err != nil {
		if _, ok := err.(*storage.CreateImageStoreConflict); ok {
			log.Debugf("Store already exists")
			return nil
		}
		return err
	}
	log.Infof("Image store created successfully")
	return nil
}