示例#1
0
func generateLayersData(sublayersCount, packagesCount, packagesPerBranchesCount int) string {
	var startPackages []string
	var allPackages []*database.Package
	for i := 0; i < packagesCount; i++ {
		for j := 0; j < packagesPerBranchesCount; j++ {
			p := &database.Package{
				OS:      "testOS",
				Name:    "p" + strconv.Itoa(i),
				Version: types.NewVersionUnsafe(strconv.Itoa(j)),
			}
			allPackages = append(allPackages, p)

			if j == 0 {
				startPackages = append(startPackages, p.GetNode())
			}
		}
	}
	err := database.InsertPackages(allPackages)
	if err != nil {
		panic(err)
	}

	var allLayers []*database.Layer
	var packagesCursor int
	for i := 0; i < sublayersCount; i++ {
		parentNode := ""
		if i > 0 {
			parentNode = allLayers[i-1].GetNode()
		}

		var installedPackagesNodes []string
		if i == sublayersCount-1 {
			if packagesCursor <= packagesCount-1 {
				installedPackagesNodes = startPackages[packagesCursor:packagesCount]
			}
		} else if (packagesCount / sublayersCount) > 0 {
			upperPackageCursor := int(math.Min(float64(packagesCursor+(packagesCount/sublayersCount)), float64(packagesCount)))
			installedPackagesNodes = startPackages[packagesCursor:upperPackageCursor]
			packagesCursor = upperPackageCursor
		}

		layer := &database.Layer{
			ID:         "l" + strconv.Itoa(i),
			ParentNode: parentNode,
			Content: database.LayerContent{
				TarSum: "lc" + strconv.Itoa(i),
				OS:     "testOS",
				InstalledPackagesNodes: installedPackagesNodes,
			},
		}
		err := database.InsertLayer(layer)
		if err != nil {
			panic(err)
		}
		allLayers = append(allLayers, layer)
	}

	return allLayers[sublayersCount-1].ID
}
示例#2
0
文件: worker.go 项目: kinvolk/clair
// Process detects the OS of a layer, the packages it installs/removes, and
// then stores everything in the database.
func Process(ID, parentID, path string) error {
	if ID == "" {
		return cerrors.NewBadRequestError("could not process a layer which does not have ID")
	}
	if path == "" {
		return cerrors.NewBadRequestError("could not process a layer which does not have a path")
	}

	log.Debugf("layer %s: processing (Location: %s, Engine version: %d, Parent: %s)", ID, utils.CleanURL(path), Version, parentID)

	// Check to see if the layer is already in the database.
	layer, err := database.FindOneLayerByID(ID, []string{database.FieldLayerEngineVersion})
	if err != nil && err != cerrors.ErrNotFound {
		return err
	}

	var parent *database.Layer

	if layer != nil {
		// The layer is already in the database, check if we need to update it.
		if layer.EngineVersion >= Version {
			log.Debugf("layer %s: layer content has already been processed in the past with engine %d. Current engine is %d. skipping analysis", ID, layer.EngineVersion, Version)
			return nil
		}

		log.Debugf("layer %s: layer content has been analyzed in the past with engine %d. Current engine is %d. analyzing again", ID, layer.EngineVersion, Version)
	} else {
		// The layer is a new one, create a base struct that we will fill.
		layer = &database.Layer{ID: ID, EngineVersion: Version}

		// Check to make sure that the parent's layer has already been processed.
		if parentID != "" {
			parent, err = database.FindOneLayerByID(parentID, []string{database.FieldLayerOS, database.FieldLayerPackages, database.FieldLayerPackages})
			if err != nil && err != cerrors.ErrNotFound {
				return err
			}
			if parent == nil {
				log.Warningf("layer %s: the parent layer (%s) is unknown. it must be processed first", ID, parentID)
				return ErrParentUnknown
			}
			layer.ParentNode = parent.GetNode()
		}
	}

	// Analyze the content.
	layer.OS, layer.InstalledPackagesNodes, layer.RemovedPackagesNodes, err = detectContent(ID, path, parent)
	if err != nil {
		return err
	}

	return database.InsertLayer(layer)
}