Skip to content

Commit

Permalink
add modify command to modify hw specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed May 20, 2020
1 parent 1b85a4d commit ddef8c1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
80 changes: 80 additions & 0 deletions cmd/modify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
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"
)

// modifyCmd represents the modify command
var modifyCmd = &cobra.Command{
Use: "modify",
Short: "Modify a VM HW specs (cpu, memory)",
Long: "Modify a VM HW specs (cpu, memory)",
Example: `
To change the VM to use 2 cores and 512MB memory
$ vermin modify vm_01 --cpus 2 --mem 512
`,
Run: func(cmd *cobra.Command, args []string) {
vmName := args[0]
var script string
if len(args) > 1 {
script = args[1]
checkFilePath(script)
}
cpus, _ := cmd.Flags().GetInt("cpus")
mem, _ := cmd.Flags().GetInt("mem")

if err := vms.Modify(vmName, cpus, mem); 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")
}

cpus, _ := cmd.Flags().GetInt("cpus")
mem, _ := cmd.Flags().GetInt("mem")

if cpus == 0 && mem == 0 {
return errors.New("should specify cpus and/or mem specs")
}
return nil
},
ValidArgsFunction: listImages,
}

func init() {
rootCmd.AddCommand(modifyCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// modifyCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
modifyCmd.Flags().IntP("cpus", "c", 0, "Number of cpu cores")
modifyCmd.Flags().IntP("mem", "m", 0, "Memory size in mega bytes")
}
21 changes: 21 additions & 0 deletions vms/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func Start(vmName string) error {
return err
}

if isRunningVM(vmName) {
return fmt.Errorf(`VM already running, use "vermin ssh %s" to ssh into the VM.`, vmName)
}

if err := start(vmName); err != nil {
return err
}
Expand Down Expand Up @@ -136,3 +140,20 @@ func IP(vmName string, purge bool, global bool) (string, error) {
}
return ip.Find(vmName, purge)
}

func Modify(vmName string, cpus int, mem int) error {
if isRunningVM(vmName) {
return fmt.Errorf(`Cannot Modify running VM, use "vermin stop %s" to stop the VM first.`, vmName)
}

var params = []string{"modifyvm", vmName}
if cpus > 0 {
params = append(params, "--cpus", fmt.Sprintf("%d", cpus))
}
if mem > 0 {
params = append(params, "--memory", fmt.Sprintf("%d", mem))
}

_, err := command.VBoxManage(params...).Call()
return err
}

0 comments on commit ddef8c1

Please sign in to comment.