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 (
"fmt"
"os"
2022-09-06 16:43:24 +00:00
"path/filepath"
2022-05-20 14:36:36 +00:00
"runtime"
2022-04-21 16:28:50 +00:00
"strings"
2022-04-19 12:01:08 +00:00
"github.com/c2h5oh/datasize"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.linka.cloud/d2vm"
"go.linka.cloud/d2vm/pkg/docker"
)
var (
2022-09-06 16:43:24 +00:00
raw bool
2022-08-07 16:24:02 +00:00
pull = false
cmdLineExtra = ""
2022-04-21 16:28:50 +00:00
2022-04-19 12:01:08 +00:00
convertCmd = & cobra . Command {
Use : "convert [docker image]" ,
2022-04-21 16:28:50 +00:00
Short : "Convert Docker image to vm image" ,
2022-04-19 12:01:08 +00:00
Args : cobra . ExactArgs ( 1 ) ,
SilenceUsage : true ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-05-20 14:36:36 +00:00
if runtime . GOOS != "linux" {
2022-09-06 16:43:24 +00:00
abs , err := filepath . Abs ( output )
if err != nil {
return err
}
out := filepath . Dir ( abs )
dargs := os . Args [ 2 : ]
for i , v := range dargs {
if v == output {
dargs [ i ] = filepath . Join ( "/out" , filepath . Base ( output ) )
break
}
}
return docker . RunD2VM ( cmd . Context ( ) , d2vm . Image , d2vm . Version , out , out , cmd . Name ( ) , dargs ... )
2022-05-20 14:36:36 +00:00
}
2022-04-19 12:01:08 +00:00
img := args [ 0 ]
2022-04-21 16:28:50 +00:00
tag := "latest"
if parts := strings . Split ( img , ":" ) ; len ( parts ) > 1 {
img , tag = parts [ 0 ] , parts [ 1 ]
}
2022-04-19 12:01:08 +00:00
size , err := parseSize ( size )
if err != nil {
return err
}
if _ , err := os . Stat ( output ) ; err == nil || ! os . IsNotExist ( err ) {
if ! force {
return fmt . Errorf ( "%s already exists" , output )
}
}
2022-04-21 16:28:50 +00:00
found := false
if ! pull {
2022-04-24 14:27:04 +00:00
imgs , err := docker . ImageList ( cmd . Context ( ) , img )
2022-04-21 16:28:50 +00:00
if err != nil {
return err
}
2022-04-24 14:27:04 +00:00
found = len ( imgs ) == 1 && imgs [ 0 ] == fmt . Sprintf ( "%s:%s" , img , tag )
2022-04-21 16:28:50 +00:00
if found {
logrus . Infof ( "using local image %s:%s" , img , tag )
}
}
if pull || ! found {
logrus . Infof ( "pulling image %s" , img )
2022-04-24 14:27:04 +00:00
if err := docker . Pull ( cmd . Context ( ) , img ) ; err != nil {
2022-04-21 16:28:50 +00:00
return err
}
2022-04-19 12:01:08 +00:00
}
2022-09-06 16:43:24 +00:00
return d2vm . Convert (
cmd . Context ( ) ,
img ,
d2vm . WithSize ( size ) ,
d2vm . WithPassword ( password ) ,
d2vm . WithOutput ( output ) ,
d2vm . WithCmdLineExtra ( cmdLineExtra ) ,
d2vm . WithNetworkManager ( d2vm . NetworkManager ( networkManager ) ) ,
d2vm . WithRaw ( raw ) ,
)
2022-04-19 12:01:08 +00:00
} ,
}
)
func parseSize ( s string ) ( int64 , error ) {
var v datasize . ByteSize
if err := v . UnmarshalText ( [ ] byte ( s ) ) ; err != nil {
return 0 , err
}
return int64 ( v ) , nil
}
func init ( ) {
2022-04-21 16:28:50 +00:00
convertCmd . Flags ( ) . BoolVar ( & pull , "pull" , false , "Always pull docker image" )
2022-08-04 16:09:11 +00:00
convertCmd . Flags ( ) . StringVarP ( & output , "output" , "o" , output , "The output image, the extension determine the image format, raw will be used if none. Supported formats: " + strings . Join ( d2vm . OutputFormats ( ) , " " ) )
2022-04-19 12:01:08 +00:00
convertCmd . Flags ( ) . StringVarP ( & password , "password" , "p" , "root" , "The Root user password" )
2022-04-21 16:28:50 +00:00
convertCmd . Flags ( ) . StringVarP ( & size , "size" , "s" , "10G" , "The output image size" )
2022-04-19 12:01:08 +00:00
convertCmd . Flags ( ) . BoolVarP ( & force , "force" , "f" , false , "Override output qcow2 image" )
2022-08-07 16:24:02 +00:00
convertCmd . Flags ( ) . StringVar ( & cmdLineExtra , "append-to-cmdline" , "" , "Extra kernel cmdline arguments to append to the generated one" )
2022-08-08 15:58:49 +00:00
convertCmd . Flags ( ) . StringVar ( & networkManager , "network-manager" , "" , "Network manager to use for the image: none, netplan, ifupdown" )
2022-09-06 16:43:24 +00:00
convertCmd . Flags ( ) . BoolVar ( & raw , "raw" , false , "Just convert the container to virtual machine image without installing anything more" )
2022-04-19 12:01:08 +00:00
rootCmd . AddCommand ( convertCmd )
}