Skip to content

Commit

Permalink
Add function that nydusify invoke "nydus-image chunkdict generate"
Browse files Browse the repository at this point in the history
Signed-off-by: Zhao Yuan <[email protected]>
  • Loading branch information
newthifans committed Nov 20, 2023
1 parent 5831a0a commit d3bf9fb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
21 changes: 21 additions & 0 deletions contrib/nydusify/pkg/build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ type CompactOption struct {

type SaveOption struct {
BootstrapPath string
DatabasePath string
}

type GenerateOption struct {
DatabasePath string
}

type Builder struct {
Expand Down Expand Up @@ -157,6 +162,22 @@ func (builder *Builder) Save(option SaveOption) error {
"warn",
"--bootstrap",
option.BootstrapPath,
"--database",
option.DatabasePath,
}
return builder.run(args, "")
}

// Generate calls `nydus-image chunkdict generate` to get chunkdict
func (builder *Builder) Generate(option GenerateOption) error {
logrus.Infof("Invoking 'nydus-image chunkdict generate' subcommand")
args := []string{
"chunkdict",
"generate",
"--log-level",
"warn",
"--database",
option.DatabasePath,
}
return builder.run(args, "")
}
38 changes: 31 additions & 7 deletions contrib/nydusify/pkg/chunkdict/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (generator *Generator) Generate(ctx context.Context) error {
}
}
}
if err := generator.deduplicating(ctx); err != nil {
return err
}
return nil
}

Expand All @@ -81,26 +84,47 @@ func (generator *Generator) save(ctx context.Context, index int) error {

// Create a directory to store the image bootstrap
nydusImageName := strings.Replace(generator.Sources[index], "/", ":", -1)
folderPath := filepath.Join(generator.WorkDir, nydusImageName)
if err := os.MkdirAll(folderPath, fs.ModePerm); err != nil {
bootstrapFolderPath := filepath.Join(generator.WorkDir, nydusImageName)
if err := os.MkdirAll(bootstrapFolderPath, fs.ModePerm); err != nil {
return errors.Wrap(err, "creat work directory")
}
if err := generator.Output(ctx, sourceParsed, folderPath, index); err != nil {
if err := generator.Output(ctx, sourceParsed, bootstrapFolderPath, index); err != nil {
return errors.Wrap(err, "output image information")
}

databaseName := "chunkdict.db"
databaseType := "sqlite"
currentDir, _ := os.Getwd()
DatabasePath := databaseType + "://" + filepath.Join(currentDir, generator.WorkDir, databaseName)

// Invoke "nydus-image save" command
builder := build.NewBuilder(generator.NydusImagePath)
if err := builder.Save(build.SaveOption{
BootstrapPath: filepath.Join(folderPath, "nydus_bootstrap"),
BootstrapPath: filepath.Join(bootstrapFolderPath, "nydus_bootstrap"),
DatabasePath: DatabasePath,
}); err != nil {
return errors.Wrap(err, "invalid nydus bootstrap format")
}

logrus.Infof("Save chunk information from image %s", generator.sourcesParser[index].Remote.Ref)
logrus.Infof("Saving chunk information from image %s", generator.sourcesParser[index].Remote.Ref)

// if err := os.RemoveAll(folderPath); err != nil {
// return errors.Wrap(err, "remove work directory")
// }
return nil
}

if err := os.RemoveAll(folderPath); err != nil {
return errors.Wrap(err, "remove work directory")
func (generator *Generator) deduplicating(ctx context.Context) error {
builder := build.NewBuilder(generator.NydusImagePath)
currentDir, _ := os.Getwd()

databaseName := "chunkdict.db"
databaseType := "sqlite"
DatabasePath := databaseType + "://" + filepath.Join(currentDir, generator.WorkDir, databaseName)
if err := builder.Generate(build.GenerateOption{
DatabasePath: DatabasePath,
}); err != nil {
return errors.Wrap(err, "invalid nydus bootstrap format")
}
return nil
}

0 comments on commit d3bf9fb

Please sign in to comment.