2021-08-11 08:31:09 +00:00
|
|
|
// Copyright © 2020-2021 Ettore Di Giacinto <mudler@gentoo.org>
|
2021-03-09 10:37:22 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-08-11 08:31:09 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/go-units"
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
"github.com/mudler/luet/cmd/util"
|
2021-08-11 08:31:09 +00:00
|
|
|
"github.com/mudler/luet/pkg/helpers/docker"
|
2021-03-09 10:37:22 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-08-11 08:31:09 +00:00
|
|
|
func NewUnpackCommand() *cobra.Command {
|
|
|
|
|
|
|
|
c := &cobra.Command{
|
|
|
|
Use: "unpack image path",
|
|
|
|
Short: "Unpack a docker image natively",
|
|
|
|
Long: `unpack doesn't need the docker daemon to run, and unpacks a docker image in the specified directory:
|
|
|
|
|
|
|
|
luet util unpack golang:alpine /alpine
|
|
|
|
`,
|
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
if len(args) != 2 {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Expects an image and a path")
|
2021-08-11 08:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
|
|
|
|
image := args[0]
|
|
|
|
destination, err := filepath.Abs(args[1])
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Error("Invalid path %s", destination)
|
2021-08-11 08:31:09 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
verify, _ := cmd.Flags().GetBool("verify")
|
|
|
|
user, _ := cmd.Flags().GetString("auth-username")
|
|
|
|
pass, _ := cmd.Flags().GetString("auth-password")
|
|
|
|
authType, _ := cmd.Flags().GetString("auth-type")
|
|
|
|
server, _ := cmd.Flags().GetString("auth-server-address")
|
|
|
|
identity, _ := cmd.Flags().GetString("auth-identity-token")
|
|
|
|
registryToken, _ := cmd.Flags().GetString("auth-registry-token")
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
temp, err := util.DefaultContext.Config.GetSystem().TempDir("contentstore")
|
2021-08-11 08:31:09 +00:00
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Fatal("Cannot create a tempdir", err.Error())
|
2021-08-11 08:31:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Info("Downloading", image, "to", destination)
|
2021-08-11 08:31:09 +00:00
|
|
|
auth := &types.AuthConfig{
|
|
|
|
Username: user,
|
|
|
|
Password: pass,
|
|
|
|
ServerAddress: server,
|
|
|
|
Auth: authType,
|
|
|
|
IdentityToken: identity,
|
|
|
|
RegistryToken: registryToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := docker.DownloadAndExtractDockerImage(temp, image, destination, auth, verify)
|
|
|
|
if err != nil {
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Error(err.Error())
|
2021-08-11 08:31:09 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-10-20 22:13:02 +00:00
|
|
|
util.DefaultContext.Info(fmt.Sprintf("Pulled: %s %s", info.Target.Digest, info.Name))
|
|
|
|
util.DefaultContext.Info(fmt.Sprintf("Size: %s", units.BytesSize(float64(info.Target.Size))))
|
2021-08-11 08:31:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Flags().String("auth-username", "", "Username to authenticate to registry/notary")
|
|
|
|
c.Flags().String("auth-password", "", "Password to authenticate to registry")
|
|
|
|
c.Flags().String("auth-type", "", "Auth type")
|
|
|
|
c.Flags().String("auth-server-address", "", "Authentication server address")
|
|
|
|
c.Flags().String("auth-identity-token", "", "Authentication identity token")
|
|
|
|
c.Flags().String("auth-registry-token", "", "Authentication registry token")
|
|
|
|
c.Flags().Bool("verify", false, "Verify signed images to notary before to pull")
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-03-09 10:37:22 +00:00
|
|
|
var utilGroup = &cobra.Command{
|
|
|
|
Use: "util [command] [OPTIONS]",
|
|
|
|
Short: "General luet internal utilities exposed",
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(utilGroup)
|
|
|
|
|
|
|
|
utilGroup.AddCommand(
|
|
|
|
NewUnpackCommand(),
|
|
|
|
)
|
|
|
|
}
|