Skip to content

Commit

Permalink
Introduce image flag for cluster create
Browse files Browse the repository at this point in the history
* Shikari now allows you to override the image name that is present in the template file
* Set the default template file name to `./hashibox.yaml`
  • Loading branch information
Ranjandas committed May 31, 2024
1 parent c59b323 commit ad91e74
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -35,6 +36,22 @@ carrying the name as a prefix to easily identify.

userDefinedEnvs := generateEnvArgs(cmd)

// Validate the Image path and type
if len(imagePath) > 0 {
absolutePath, err := filepath.Abs(imagePath)
if err != nil {
fmt.Printf("EROR: Cannot find the absolute path of the image: %s", imagePath)
return
}

qcow2, _ := isQCOW2(absolutePath)

if !qcow2 {
fmt.Printf("Error: Image %s is not of type qCOW2\n", absolutePath)
return
}
}

var wg sync.WaitGroup
errCh := make(chan error, len(totalVMs))

Expand All @@ -59,7 +76,7 @@ carrying the name as a prefix to easily identify.
}

var servers, clients int
var name, template string
var name, template, imagePath string

func init() {
rootCmd.AddCommand(createCmd)
Expand All @@ -76,8 +93,9 @@ func init() {
createCmd.Flags().IntVarP(&servers, "servers", "s", 1, "number of servers")
createCmd.Flags().IntVarP(&clients, "clients", "c", 1, "number of clients")
createCmd.Flags().StringVarP(&name, "name", "n", "shikari", "name of the cluster")
createCmd.Flags().StringVarP(&template, "template", "t", "alpine", "name of lima template for the VMs")
createCmd.Flags().StringVarP(&template, "template", "t", "./hashibox.yaml", "name of lima template for the VMs")
createCmd.Flags().StringSliceP("env", "e", []string{}, "provide environment vars in the for key=value (can be used multiple times)")
createCmd.Flags().StringVarP(&imagePath, "image", "i", "", "path to the cqow2 images to be used for the VMs, overriding the one in the template")
createCmd.MarkFlagRequired("name")
createCmd.MarkFlagRequired("servers")
createCmd.MarkFlagRequired("clients")
Expand All @@ -103,6 +121,13 @@ func spawnLimaVM(vmName string, modeEnv string, userEnv string, wg *sync.WaitGro
yqExpression = fmt.Sprintf("%s | %s", yqExpression, userEnv)
}

// Override the image from the template
if len(imagePath) > 0 {
absolutePath, _ := filepath.Abs(imagePath)
imageArg := fmt.Sprintf(`.images=[{"location": "%s"}]`, absolutePath)
yqExpression = fmt.Sprintf("%s | %s", yqExpression, imageArg)
}

// Define the command to spawn a Lima VM
limaCmd := fmt.Sprintf("limactl start --name %s %s --tty=false --set '%s'", vmName, tmpl, yqExpression)
cmd := exec.Command("/bin/sh", "-c", limaCmd)
Expand Down Expand Up @@ -176,3 +201,27 @@ func getInstanceMode(instanceName string) string {

return mode
}

func isQCOW2(filePath string) (bool, error) {
// Open the file
file, err := os.Open(filePath)
if err != nil {
return false, err
}
defer file.Close()

// Read the first 4 bytes
header := make([]byte, 4)
_, err = file.Read(header)
if err != nil {
return false, err
}

// Check if the header matches the QCOW2 magic number
qcow2Magic := []byte{'Q', 'F', 'I', 0xfb}
if string(header) == string(qcow2Magic) {
return true, nil
}

return false, nil
}

0 comments on commit ad91e74

Please sign in to comment.