chore: slightly refactored copy and relocate commands

This commit is contained in:
Jennifer Power
2021-06-17 17:46:38 -04:00
parent 183cd3a0fb
commit cb898136ca
5 changed files with 69 additions and 64 deletions

View File

@@ -7,6 +7,14 @@ import (
"github.com/spf13/cobra"
)
var (
copyLong = `hauler copies artifacts stored on a registry to local disk`
copyExample = `
# Run Hauler
hauler copy locahost:5000/artifacts:latest`
)
type copyOpts struct {
*rootOpts
dir string
@@ -23,6 +31,8 @@ func NewCopyCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "copy",
Short: "Download artifacts from OCI registry to local disk",
Long: copyLong,
Example: copyExample,
Aliases: []string{"c", "cp"},
//Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

View File

@@ -6,14 +6,15 @@ import (
type relocateOpts struct {
inputFile string
*rootOpts
}
var rlo relocateOpts
// NewRelocateCommand creates a new sub command under
// haulterctl for relocating images and artifacts
func NewRelocateCommand() *cobra.Command {
opts := &relocateOpts{}
opts := &relocateOpts{
rootOpts: &ro,
}
cmd := &cobra.Command{
Use: "relocate",
@@ -25,12 +26,8 @@ func NewRelocateCommand() *cobra.Command {
},
}
f := cmd.PersistentFlags()
f.StringVarP(&opts.inputFile, "input", "i", "haul.tar.zst",
"package output location relative to the current directory (haul.tar.zst)")
cmd.AddCommand(NewRelocateArtifactsCommand())
cmd.AddCommand(NewRelocateImagesCommand())
cmd.AddCommand(NewRelocateArtifactsCommand(opts))
cmd.AddCommand(NewRelocateImagesCommand(opts))
return cmd
}

View File

@@ -2,65 +2,53 @@ package app
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/rancherfederal/hauler/pkg/oci"
"github.com/rancherfederal/hauler/pkg/packager"
"github.com/spf13/cobra"
)
type relocateArtifactsOpts struct {
*rootOpts
*relocateOpts
destRef string
destRef string
}
var (
relocateArtifactsLong = `hauler relocate artifacts process an archive with files
to be pushed to a registry`
relocateArtifactsExample = `
# Run Hauler
hauler relocate artifacts artifacts.tar.zst locahost:5000/artifacts:latest`
)
// NewRelocateArtifactsCommand creates a new sub command of relocate for artifacts
func NewRelocateArtifactsCommand() *cobra.Command {
func NewRelocateArtifactsCommand(relocate *relocateOpts) *cobra.Command {
opts := &relocateArtifactsOpts{
rootOpts: &ro,
relocateOpts: &rlo,
relocateOpts: relocate,
}
cmd := &cobra.Command{
Use: "artifacts",
Short: "Use artifact from bundle artifacts to populate a target file server with the artifact's contents",
Use: "artifacts",
Short: "Use artifact from bundle artifacts to populate a target file server with the artifact's contents",
Long: relocateArtifactsLong,
Example: relocateArtifactsExample,
RunE: func(cmd *cobra.Command, args []string) error {
opts.destRef = args[0]
return opts.Run(opts.destRef)
opts.inputFile = args[0]
opts.destRef = args[1]
return opts.Run(opts.destRef, opts.inputFile)
},
}
return cmd
}
func (o *relocateArtifactsOpts) Run(dst string) error {
func (o *relocateArtifactsOpts) Run(dst string, input string) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ar := packager.NewArchiver()
tmpdir, err := os.MkdirTemp("", "hauler")
if err != nil {
o.logger.Errorf("error creating temporary directory hauler: %v", err)
}
packager.Unpackage(ar, o.inputFile, tmpdir)
files, err := ioutil.ReadDir(tmpdir)
if err != nil {
o.logger.Errorf("error reading files from temporary directory: %v", err)
}
for _, f := range files {
if err := oci.Put(ctx, filepath.Join(tmpdir, f.Name()), dst); err != nil {
o.logger.Errorf("error pushing artifact to registry %s: %v", dst, err)
}
if err := oci.Put(ctx, input, dst); err != nil {
o.logger.Errorf("error pushing artifact to registry %s: %v", dst, err)
}
return nil

View File

@@ -13,46 +13,55 @@ import (
"github.com/spf13/cobra"
)
var (
relocateImagesLong = `hauler relocate images processes a bundle provides by hauler
package build and copies all of the collected images to a registry`
relocateImagesExample = `
# Run Hauler
hauler relocate images pkg.tar.zst locahost:5000`
)
type relocateImagesOpts struct {
*rootOpts
*relocateOpts
destRef string
}
// NewRelocateImagesCommand creates a new sub command of relocate for images
func NewRelocateImagesCommand() *cobra.Command {
func NewRelocateImagesCommand(relocate *relocateOpts) *cobra.Command {
opts := &relocateImagesOpts{
rootOpts: &ro,
relocateOpts: &rlo,
relocateOpts: relocate,
}
cmd := &cobra.Command{
Use: "images",
Short: "Use artifact from bundle images to populate a target registry with the artifact's images",
Use: "images",
Short: "Use artifact from bundle images to populate a target registry with the artifact's images",
Long: relocateImagesLong,
Example: relocateImagesExample,
RunE: func(cmd *cobra.Command, args []string) error {
opts.destRef = args[0]
return opts.Run(opts.destRef)
opts.inputFile = args[0]
opts.destRef = args[1]
return opts.Run(opts.destRef, opts.inputFile)
},
}
return cmd
}
func (o *relocateImagesOpts) Run(dst string) error {
ar := packager.NewArchiver()
func (o *relocateImagesOpts) Run(dst string, input string) error {
tmpdir, err := os.MkdirTemp("", "hauler")
if err != nil {
o.logger.Errorf("error making temp directory: %v", err)
return err
}
o.logger.Debugf("Using temporary working directory: %s", tmpdir)
packager.Unpackage(ar, o.inputFile, tmpdir)
a := packager.NewArchiver()
if err != nil {
o.logger.Errorf("error unpackaging bundle: %v", err)
if err := packager.Unpackage(a, input, tmpdir); err != nil {
o.logger.Errorf("error unpackaging input %s: %v", input, err)
}
o.logger.Debugf("Unpackaged %s", input)
path := filepath.Join(tmpdir, "layout")

View File

@@ -2,11 +2,12 @@ package app
import (
"fmt"
"github.com/rancherfederal/hauler/pkg/log"
"io"
"os"
"time"
"github.com/rancherfederal/hauler/pkg/log"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
@@ -30,9 +31,9 @@ var (
hauler pkg run pkg.tar.zst
hauler bundle images <images>
hauler bundle artifacts <artfiacts>
hauler relocate artifacts -i <package-name>
hauler relocate images -i <package-name> locahost:5000
hauler copy`
hauler relocate artifacts artifacts.tar.zst
hauler relocate images pkg.tar.zst locahost:5000
hauler copy local:5000/artifacts:latest`
)
type rootOpts struct {
@@ -72,7 +73,7 @@ func NewRootCommand() *cobra.Command {
cmd.AddCommand(NewPkgCommand())
f := cmd.PersistentFlags()
f.StringVarP(&loglevel, "loglevel", "l", "info",
f.StringVarP(&loglevel, "loglevel", "l", "debug",
"Log level (debug, info, warn, error, fatal, panic)")
f.StringVarP(&cfgFile, "config", "c", "./hauler.yaml",
"config file (./hauler.yaml)")