2022-04-19 12:01:08 +00:00
|
|
|
// Copyright 2022 Linka Cloud All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-09-06 16:43:24 +00:00
|
|
|
"fmt"
|
2022-05-20 14:36:36 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2022-04-21 16:28:50 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-04-19 12:01:08 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"go.linka.cloud/d2vm"
|
|
|
|
"go.linka.cloud/d2vm/pkg/docker"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-11-23 18:21:58 +00:00
|
|
|
file = "Dockerfile"
|
|
|
|
tag = "d2vm-" + uuid.New().String()
|
|
|
|
buildArgs []string
|
|
|
|
buildCmd = &cobra.Command{
|
2022-04-19 12:01:08 +00:00
|
|
|
Use: "build [context directory]",
|
2022-04-21 16:28:50 +00:00
|
|
|
Short: "Build a vm image from Dockerfile",
|
2022-04-19 12:01:08 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-05-20 14:36:36 +00:00
|
|
|
// TODO(adphi): resolve context path
|
2022-09-13 13:02:38 +00:00
|
|
|
if runtime.GOOS != "linux" || !isRoot() {
|
2022-09-06 16:43:24 +00:00
|
|
|
ctxAbsPath, err := filepath.Abs(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-09 12:26:08 +00:00
|
|
|
if file == "" {
|
|
|
|
file = filepath.Join(args[0], "Dockerfile")
|
|
|
|
}
|
2022-09-06 16:43:24 +00:00
|
|
|
dockerFileAbsPath, err := filepath.Abs(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(dockerFileAbsPath, ctxAbsPath) {
|
|
|
|
return fmt.Errorf("Dockerfile must be in the context directory path")
|
|
|
|
}
|
|
|
|
outputPath, err := filepath.Abs(output)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
in = ctxAbsPath
|
|
|
|
out = filepath.Dir(outputPath)
|
|
|
|
)
|
|
|
|
dargs := os.Args[2:]
|
|
|
|
for i, v := range dargs {
|
|
|
|
switch v {
|
|
|
|
case file:
|
2022-09-10 17:31:40 +00:00
|
|
|
rel, err := filepath.Rel(in, dockerFileAbsPath)
|
2022-09-06 16:43:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to construct Dockerfile container paths: %w", err)
|
|
|
|
}
|
|
|
|
dargs[i] = filepath.Join("/in", rel)
|
|
|
|
case output:
|
2022-09-09 12:26:08 +00:00
|
|
|
dargs[i] = filepath.Join("/out", filepath.Base(output))
|
2022-09-06 16:43:24 +00:00
|
|
|
case args[0]:
|
|
|
|
dargs[i] = "/in"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return docker.RunD2VM(cmd.Context(), d2vm.Image, d2vm.Version, in, out, cmd.Name(), os.Args[2:]...)
|
2022-05-20 14:36:36 +00:00
|
|
|
}
|
2023-02-23 14:02:12 +00:00
|
|
|
if luksPassword != "" && !splitBoot {
|
|
|
|
logrus.Warnf("luks password is set: enabling split boot")
|
|
|
|
splitBoot = true
|
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
size, err := parseSize(size)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-20 14:36:36 +00:00
|
|
|
if file == "" {
|
|
|
|
file = filepath.Join(args[0], "Dockerfile")
|
|
|
|
}
|
2022-11-23 18:21:58 +00:00
|
|
|
if push && tag == "" {
|
|
|
|
return fmt.Errorf("tag is required when pushing container disk image")
|
|
|
|
}
|
2022-09-06 16:43:24 +00:00
|
|
|
if _, err := os.Stat(output); err == nil || !os.IsNotExist(err) {
|
|
|
|
if !force {
|
|
|
|
return fmt.Errorf("%s already exists", output)
|
|
|
|
}
|
|
|
|
}
|
2022-08-07 16:24:02 +00:00
|
|
|
logrus.Infof("building docker image from %s", file)
|
2022-04-24 14:27:04 +00:00
|
|
|
if err := docker.Build(cmd.Context(), tag, file, args[0], buildArgs...); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-09-13 13:02:38 +00:00
|
|
|
if err := d2vm.Convert(
|
2022-09-06 16:43:24 +00:00
|
|
|
cmd.Context(),
|
|
|
|
tag,
|
|
|
|
d2vm.WithSize(size),
|
|
|
|
d2vm.WithPassword(password),
|
|
|
|
d2vm.WithOutput(output),
|
|
|
|
d2vm.WithCmdLineExtra(cmdLineExtra),
|
|
|
|
d2vm.WithNetworkManager(d2vm.NetworkManager(networkManager)),
|
|
|
|
d2vm.WithRaw(raw),
|
2023-02-22 16:26:32 +00:00
|
|
|
d2vm.WithSplitBoot(splitBoot),
|
|
|
|
d2vm.WithBootSize(bootSize),
|
2023-02-23 14:02:12 +00:00
|
|
|
d2vm.WithLuksPassword(luksPassword),
|
2022-09-13 13:02:38 +00:00
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-23 18:21:58 +00:00
|
|
|
if uid, ok := sudoUser(); ok {
|
|
|
|
if err := os.Chown(output, uid, uid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-13 13:02:38 +00:00
|
|
|
}
|
2022-11-23 18:21:58 +00:00
|
|
|
return maybeMakeContainerDisk(cmd.Context())
|
2022-04-19 12:01:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(buildCmd)
|
|
|
|
|
2022-05-20 14:36:36 +00:00
|
|
|
buildCmd.Flags().StringVarP(&file, "file", "f", "", "Name of the Dockerfile")
|
2022-04-21 16:28:50 +00:00
|
|
|
buildCmd.Flags().StringArrayVar(&buildArgs, "build-arg", nil, "Set build-time variables")
|
2022-04-19 12:01:08 +00:00
|
|
|
|
2022-11-23 18:21:58 +00:00
|
|
|
buildCmd.Flags().AddFlagSet(buildFlags())
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|