func (a *Api) SnapshotCreate(snapshotName, description string, sourceSnapshots []string, packageRefs []string) (*deb.Snapshot, error) { var err error snapshotCollection := a.Ctx().CollectionFactory().SnapshotCollection() snapshotCollection.Lock() defer snapshotCollection.Unlock() sources := make([]*deb.Snapshot, len(sourceSnapshots)) for i := range sourceSnapshots { sources[i], err = snapshotCollection.ByName(sourceSnapshots[i]) if err != nil { return nil, err } err = snapshotCollection.LoadComplete(sources[i]) if err != nil { return nil, err } } list := deb.NewPackageList() // verify package refs and build package list for _, ref := range packageRefs { var p *deb.Package p, err = a.Ctx().CollectionFactory().PackageCollection().ByKey([]byte(ref)) if err != nil { if err == database.ErrNotFound { return nil, fmt.Errorf("package %s: %s", ref, err) } else { return nil, err } } err = list.Add(p) if err != nil { return nil, err } } snapshot := deb.NewSnapshotFromRefList(snapshotName, sources, deb.NewPackageRefListFromPackageList(list), description) err = snapshotCollection.Add(snapshot) if err != nil { return nil, err } return snapshot, nil }
func aptlySnapshotVerify(cmd *commander.Command, args []string) error { var err error if len(args) < 1 { cmd.Usage() return err } snapshots := make([]*deb.Snapshot, len(args)) for i := range snapshots { snapshots[i], err = context.CollectionFactory().SnapshotCollection().ByName(args[i]) if err != nil { return fmt.Errorf("unable to verify: %s", err) } err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshots[i]) if err != nil { return fmt.Errorf("unable to verify: %s", err) } } context.Progress().Printf("Loading packages...\n") packageList, err := deb.NewPackageListFromRefList(snapshots[0].RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) if err != nil { fmt.Errorf("unable to load packages: %s", err) } sourcePackageList := deb.NewPackageList() err = sourcePackageList.Append(packageList) if err != nil { fmt.Errorf("unable to merge sources: %s", err) } var pL *deb.PackageList for i := 1; i < len(snapshots); i++ { pL, err = deb.NewPackageListFromRefList(snapshots[i].RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) if err != nil { fmt.Errorf("unable to load packages: %s", err) } err = sourcePackageList.Append(pL) if err != nil { fmt.Errorf("unable to merge sources: %s", err) } } sourcePackageList.PrepareIndex() var architecturesList []string if len(context.ArchitecturesList()) > 0 { architecturesList = context.ArchitecturesList() } else { architecturesList = packageList.Architectures(true) } if len(architecturesList) == 0 { return fmt.Errorf("unable to determine list of architectures, please specify explicitly") } context.Progress().Printf("Verifying...\n") missing, err := packageList.VerifyDependencies(context.DependencyOptions(), architecturesList, sourcePackageList, context.Progress()) if err != nil { return fmt.Errorf("unable to verify dependencies: %s", err) } if len(missing) == 0 { context.Progress().Printf("All dependencies are satisfied.\n") } else { context.Progress().Printf("Missing dependencies (%d):\n", len(missing)) deps := make([]string, len(missing)) i := 0 for _, dep := range missing { deps[i] = dep.String() i++ } sort.Strings(deps) for _, dep := range deps { context.Progress().Printf(" %s\n", dep) } } return err }
// POST /api/snapshots func apiSnapshotsCreate(c *gin.Context) { var ( err error snapshot *deb.Snapshot ) var b struct { Name string `binding:"required"` Description string SourceSnapshots []string PackageRefs []string } if !c.Bind(&b) { return } if b.Description == "" { if len(b.SourceSnapshots)+len(b.PackageRefs) == 0 { b.Description = "Created as empty" } } snapshotCollection := context.CollectionFactory().SnapshotCollection() snapshotCollection.Lock() defer snapshotCollection.Unlock() sources := make([]*deb.Snapshot, len(b.SourceSnapshots)) for i := range b.SourceSnapshots { sources[i], err = snapshotCollection.ByName(b.SourceSnapshots[i]) if err != nil { c.Fail(404, err) return } err = snapshotCollection.LoadComplete(sources[i]) if err != nil { c.Fail(500, err) return } } list := deb.NewPackageList() // verify package refs and build package list for _, ref := range b.PackageRefs { var p *deb.Package p, err = context.CollectionFactory().PackageCollection().ByKey([]byte(ref)) if err != nil { if err == database.ErrNotFound { c.Fail(404, fmt.Errorf("package %s: %s", ref, err)) } else { c.Fail(500, err) } return } err = list.Add(p) if err != nil { c.Fail(400, err) return } } snapshot = deb.NewSnapshotFromRefList(b.Name, sources, deb.NewPackageRefListFromPackageList(list), b.Description) err = snapshotCollection.Add(snapshot) if err != nil { c.Fail(400, err) return } c.JSON(201, snapshot) }
func aptlySnapshotPull(cmd *commander.Command, args []string) error { var err error if len(args) < 4 { cmd.Usage() return commander.ErrCommandError } noDeps := context.flags.Lookup("no-deps").Value.Get().(bool) noRemove := context.flags.Lookup("no-remove").Value.Get().(bool) // Load <name> snapshot snapshot, err := context.CollectionFactory().SnapshotCollection().ByName(args[0]) if err != nil { return fmt.Errorf("unable to pull: %s", err) } err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) if err != nil { return fmt.Errorf("unable to pull: %s", err) } // Load <source> snapshot source, err := context.CollectionFactory().SnapshotCollection().ByName(args[1]) if err != nil { return fmt.Errorf("unable to pull: %s", err) } err = context.CollectionFactory().SnapshotCollection().LoadComplete(source) if err != nil { return fmt.Errorf("unable to pull: %s", err) } context.Progress().Printf("Dependencies would be pulled into snapshot:\n %s\nfrom snapshot:\n %s\nand result would be saved as new snapshot %s.\n", snapshot, source, args[2]) // Convert snapshot to package list context.Progress().Printf("Loading packages (%d)...\n", snapshot.RefList().Len()+source.RefList().Len()) packageList, err := deb.NewPackageListFromRefList(snapshot.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) if err != nil { return fmt.Errorf("unable to load packages: %s", err) } sourcePackageList, err := deb.NewPackageListFromRefList(source.RefList(), context.CollectionFactory().PackageCollection(), context.Progress()) if err != nil { return fmt.Errorf("unable to load packages: %s", err) } context.Progress().Printf("Building indexes...\n") packageList.PrepareIndex() sourcePackageList.PrepareIndex() // Calculate architectures var architecturesList []string if len(context.ArchitecturesList()) > 0 { architecturesList = context.ArchitecturesList() } else { architecturesList = packageList.Architectures(false) } sort.Strings(architecturesList) if len(architecturesList) == 0 { return fmt.Errorf("unable to determine list of architectures, please specify explicitly") } // Initial dependencies out of arguments initialDependencies := make([]deb.Dependency, len(args)-3) for i, arg := range args[3:] { initialDependencies[i], err = deb.ParseDependency(arg) if err != nil { return fmt.Errorf("unable to parse argument: %s", err) } } // Perform pull for _, arch := range architecturesList { dependencies := make([]deb.Dependency, len(initialDependencies), 2*len(initialDependencies)) for i := range dependencies { dependencies[i] = initialDependencies[i] dependencies[i].Architecture = arch } // Go over list of initial dependencies + list of dependencies found for i := 0; i < len(dependencies); i++ { dep := dependencies[i] // Search for package that can satisfy dependencies pkg := sourcePackageList.Search(dep) if pkg == nil { context.Progress().ColoredPrintf("@y[!]@| @!Dependency %s can't be satisfied with source %s@|", &dep, source) continue } if !noRemove { // Remove all packages with the same name and architecture for p := packageList.Search(deb.Dependency{Architecture: pkg.Architecture, Pkg: pkg.Name}); p != nil; { packageList.Remove(p) context.Progress().ColoredPrintf("@r[-]@| %s removed", p) p = packageList.Search(deb.Dependency{Architecture: pkg.Architecture, Pkg: pkg.Name}) } } // Add new discovered package packageList.Add(pkg) context.Progress().ColoredPrintf("@g[+]@| %s added", pkg) if noDeps { continue } // Find missing dependencies for single added package pL := deb.NewPackageList() pL.Add(pkg) var missing []deb.Dependency missing, err = pL.VerifyDependencies(context.DependencyOptions(), []string{arch}, packageList, nil) if err != nil { context.Progress().ColoredPrintf("@y[!]@| @!Error while verifying dependencies for pkg %s: %s@|", pkg, err) } // Append missing dependencies to the list of dependencies to satisfy for _, misDep := range missing { found := false for _, d := range dependencies { if d == misDep { found = true break } } if !found { dependencies = append(dependencies, misDep) } } } } if context.flags.Lookup("dry-run").Value.Get().(bool) { context.Progress().Printf("\nNot creating snapshot, as dry run was requested.\n") } else { // Create <destination> snapshot destination := deb.NewSnapshotFromPackageList(args[2], []*deb.Snapshot{snapshot, source}, packageList, fmt.Sprintf("Pulled into '%s' with '%s' as source, pull request was: '%s'", snapshot.Name, source.Name, strings.Join(args[3:], " "))) err = context.CollectionFactory().SnapshotCollection().Add(destination) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } context.Progress().Printf("\nSnapshot %s successfully created.\nYou can run 'aptly publish snapshot %s' to publish snapshot as Debian repository.\n", destination.Name, destination.Name) } return err }
func aptlySnapshotCreate(cmd *commander.Command, args []string) error { var ( err error snapshot *deb.Snapshot ) if len(args) == 4 && args[1] == "from" && args[2] == "mirror" { // aptly snapshot create snap from mirror mirror var repo *deb.RemoteRepo repoName, snapshotName := args[3], args[0] repo, err = context.CollectionFactory().RemoteRepoCollection().ByName(repoName) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } err = context.CollectionFactory().RemoteRepoCollection().LoadComplete(repo) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } snapshot, err = deb.NewSnapshotFromRepository(snapshotName, repo) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } } else if len(args) == 4 && args[1] == "from" && args[2] == "repo" { // aptly snapshot create snap from repo repo var repo *deb.LocalRepo localRepoName, snapshotName := args[3], args[0] repo, err = context.CollectionFactory().LocalRepoCollection().ByName(localRepoName) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } err = context.CollectionFactory().LocalRepoCollection().LoadComplete(repo) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } snapshot, err = deb.NewSnapshotFromLocalRepo(snapshotName, repo) if err != nil { return fmt.Errorf("unable to create snapshot: %s", err) } } else if len(args) == 2 && args[1] == "empty" { // aptly snapshot create snap empty snapshotName := args[0] packageList := deb.NewPackageList() snapshot = deb.NewSnapshotFromPackageList(snapshotName, nil, packageList, "Created as empty") } else { cmd.Usage() return err } err = context.CollectionFactory().SnapshotCollection().Add(snapshot) if err != nil { return fmt.Errorf("unable to add snapshot: %s", err) } fmt.Printf("\nSnapshot %s successfully created.\nYou can run 'aptly publish snapshot %s' to publish snapshot as Debian repository.\n", snapshot.Name, snapshot.Name) return err }