-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement commit function to commit VM into an image
- Loading branch information
Showing
10 changed files
with
270 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright © 2020 NAME HERE <EMAIL ADDRESS> | ||
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 cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/mhewedy/vermin/vms" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
// commitCmd represents the commit command | ||
var commitCmd = &cobra.Command{ | ||
Use: "commit", | ||
Short: "Commit a VM into a new Image", | ||
Long: `Commit a VM into a new Image to be used later as a template to create VMs from.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
vmName := args[0] | ||
imageName := args[1] | ||
override, _ := cmd.Flags().GetBool("override") | ||
|
||
err := vms.Commit(vmName, imageName, override) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("vm required") | ||
} | ||
if len(args) < 2 { | ||
return errors.New("new image name required, and should be in format base/name, example k8s/worker") | ||
} | ||
return nil | ||
}, | ||
ValidArgsFunction: listStoppedVms, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(commitCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// commitCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
commitCmd.Flags().BoolP("override", "", false, "override any existing image") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package images | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/mhewedy/vermin/command" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func Commit(vmName, imageName string, override bool) error { | ||
|
||
if err := validateName(imageName); err != nil { | ||
return err | ||
} | ||
|
||
if !override { | ||
existingImgs, _ := List() | ||
if contains(existingImgs, imageName) { | ||
return errors.New("Image with same name already exists, either choose a new name or use the --override flag") | ||
} | ||
} | ||
|
||
tmpDir, err := ioutil.TempDir("", "vermin_commit") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
ovaFile := tmpDir + strings.ReplaceAll(imageName, "/", "_") + ova | ||
|
||
export := command.VBoxManage("export", vmName, "--ovf20", "-o", ovaFile) | ||
_, err = export.CallWithProgress(fmt.Sprintf("Committing %s into image %s", vmName, imageName)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tmpFile, err := os.Open(ovaFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer tmpFile.Close() | ||
|
||
if err := writeNewImage(tmpFile, imageName); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("\nImage is ready, to create a VM from it use:\n$ vermin create %s\n", imageName) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package images | ||
|
||
import ( | ||
"fmt" | ||
"github.com/schollz/progressbar/v3" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func Download(image string) error { | ||
// check image against cached | ||
cached, err := listCachedImages() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if contains(cached, image) { | ||
return nil | ||
} | ||
|
||
remote, err := listRemoteImages(false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dbImage, err := remote.findByName(image) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return download(dbImage) | ||
} | ||
|
||
func download(r *dbImage) error { | ||
fmt.Printf("Image '%s' could not be found. Attempting to find and install \n", r.Name) | ||
|
||
// download to a temp file | ||
tmpFile, err := ioutil.TempFile("", strings.ReplaceAll(r.Name, "/", "_")) | ||
if err != nil { | ||
return err | ||
} | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
resp, err := http.Get(r.URL) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
bar := buildDownloadBar(resp) | ||
|
||
if _, err = io.Copy(io.MultiWriter(tmpFile, bar), resp.Body); err != nil { | ||
return err | ||
} | ||
|
||
return writeNewImage(tmpFile, r.Name) | ||
} | ||
|
||
func buildDownloadBar(resp *http.Response) *progressbar.ProgressBar { | ||
bar := progressbar.DefaultBytes( | ||
resp.ContentLength, | ||
"Downloading", | ||
) | ||
progressbar.OptionSetTheme(progressbar.Theme{ | ||
Saucer: "#", SaucerPadding: ".", BarStart: "[", BarEnd: "]"})(bar) | ||
return bar | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package images | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
func contains(a []string, s string) bool { | ||
for i := range a { | ||
if a[i] == s { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func validateName(imageName string) error { | ||
if len(strings.Split(imageName, "/")) != 2 { | ||
return errors.New("Name doesn't follow pattern <distro>/<version>") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.