-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limactl: add
--cpus
, --memory
, --mount-type
, --vm-type
, ...
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
- Loading branch information
1 parent
8498a19
commit 9868bfc
Showing
6 changed files
with
231 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package editflags | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
flag "github.com/spf13/pflag" | ||
) | ||
|
||
// RegisterEdit registers flags related to in-place YAML modification. | ||
func RegisterEdit(cmd *cobra.Command) { | ||
flags := cmd.Flags() | ||
|
||
flags.Int("cpus", 0, "number of CPUs") // Similar to colima's --cpu, but the flag name is slightly different (cpu vs cpus) | ||
_ = cmd.RegisterFlagCompletionFunc("cpus", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"1", "2", "4", "8"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
flags.IPSlice("dns", nil, "specify custom DNS (disable host resolver)") // colima-compatible | ||
|
||
flags.Float32("memory", 0, "memory in GiB") // colima-compatible | ||
_ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"1", "2", "4", "8", "16", "32"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
flags.StringSlice("mount", nil, "directories to mount, suffix ':w' for writable (Do not specify directories that overlap with the existing mounts)") // colima-compatible | ||
|
||
flags.String("mount-type", "", "mount type (reverse-sshfs, 9p, virtiofs)") // Similar to colima's --mount-type=(sshfs|9p|virtiofs), but "reverse-sshfs" is Lima is called "sshfs" in colima | ||
_ = cmd.RegisterFlagCompletionFunc("mount-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"reverse-sshfs", "9p", "virtiofs"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
flags.Bool("mount-writable", false, "make all mounts writable") | ||
|
||
flags.StringSlice("network", nil, "additional networks, e.g., \"vzNAT\" or \"lima:shared\" to assign vmnet IP") | ||
_ = cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
// TODO: retrieve the lima:* network list from networks.yaml | ||
return []string{"lima:shared", "lima:bridged", "lima:host", "lima:user-v2", "vzNAT"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
flags.Bool("rosetta", false, "enable Rosetta (For vz instances)") | ||
|
||
flags.String("set", "", "modify the template inplace, using yq syntax") | ||
} | ||
|
||
// RegisterStart registers flags related to in-place YAML modification. | ||
func RegisterStart(cmd *cobra.Command) { | ||
RegisterEdit(cmd) | ||
flags := cmd.Flags() | ||
|
||
flags.String("arch", "", "machine architecture (x86_64, aarch64, riscv64)") // colima-compatible | ||
_ = cmd.RegisterFlagCompletionFunc("arch", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"x86_64", "aarch64", "riscv64"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
flags.String("containerd", "", "containerd mode (user, system, user+system, none)") | ||
_ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"user", "system", "user+system", "none"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
flags.Float32("disk", 0, "disk size in GiB") // colima-compatible | ||
_ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"10", "30", "50", "100", "200"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
flags.String("vm-type", "", "virtual machine type (qemu, vz)") // colima-compatible | ||
_ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"qemu", "vz"}, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
} | ||
|
||
func defaultExprFunc(expr string) func(v *flag.Flag) (string, error) { | ||
return func(v *flag.Flag) (string, error) { | ||
return fmt.Sprintf(expr, v.Value), nil | ||
} | ||
} | ||
|
||
// YQExpressions returns YQ expressions. | ||
func YQExpressions(flags *flag.FlagSet) ([]string, error) { | ||
type def struct { | ||
flagName string | ||
exprFunc func(*flag.Flag) (string, error) | ||
experimental bool | ||
} | ||
d := defaultExprFunc | ||
defs := []def{ | ||
{"cpus", d(".cpus = %s"), false}, | ||
{"dns", | ||
func(_ *flag.Flag) (string, error) { | ||
ipSlice, err := flags.GetIPSlice("dns") | ||
if err != nil { | ||
return "", err | ||
} | ||
expr := `.dns += [` | ||
for i, ip := range ipSlice { | ||
expr += fmt.Sprintf("%q", ip) | ||
if i < len(ipSlice)-1 { | ||
expr += "," | ||
} | ||
} | ||
expr += `] | .dns |= unique | .hostResolver.enabled=false` | ||
logrus.Warnf("Disabling HostResolver, as custom DNS addresses (%v) are specified", ipSlice) | ||
return expr, nil | ||
}, | ||
false}, | ||
{"memory", d(".memory = \"%sGiB\""), false}, | ||
{"mount", | ||
func(_ *flag.Flag) (string, error) { | ||
ss, err := flags.GetStringSlice("mount") | ||
if err != nil { | ||
return "", err | ||
} | ||
expr := `.mounts += [` | ||
for i, s := range ss { | ||
writable := strings.HasSuffix(s, ":w") | ||
loc := strings.TrimSuffix(s, ":w") | ||
expr += fmt.Sprintf(`{"location": %q, "writable": %v}`, loc, writable) | ||
if i < len(ss)-1 { | ||
expr += "," | ||
} | ||
} | ||
expr += `] | .mounts |= unique_by(.location)` | ||
return expr, nil | ||
}, | ||
false}, | ||
{"mount-type", d(".mountType = %q"), false}, | ||
{"mount-writable", d(".mounts[].writable = %s"), false}, | ||
{"network", | ||
func(_ *flag.Flag) (string, error) { | ||
ss, err := flags.GetStringSlice("network") | ||
if err != nil { | ||
return "", err | ||
} | ||
expr := `.networks += [` | ||
for i, s := range ss { | ||
// CLI syntax is still experimental (YAML syntax is out of experimental) | ||
switch { | ||
case s == "vzNAT": | ||
expr += `{"vzNAT": true}` | ||
case strings.HasPrefix(s, "lima:"): | ||
network := strings.TrimPrefix(s, "lima:") | ||
expr += fmt.Sprintf(`{"lima": %q}`, network) | ||
default: | ||
return "", fmt.Errorf("network name must be \"vzNAT\" or \"lima:*\", got %q", s) | ||
} | ||
if i < len(ss)-1 { | ||
expr += "," | ||
} | ||
} | ||
expr += `] | .networks |= unique_by(.lima)` | ||
return expr, nil | ||
}, | ||
true}, | ||
{"rosetta", | ||
func(_ *flag.Flag) (string, error) { | ||
b, err := flags.GetBool("rosetta") | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf(".rosetta.enabled = %v | .rosetta.binfmt = %v", b, b), nil | ||
}, | ||
true}, | ||
{"set", d("%s"), true}, | ||
{"arch", d(".arch = %q"), false}, | ||
{"containerd", | ||
func(_ *flag.Flag) (string, error) { | ||
s, err := flags.GetString("containerd") | ||
if err != nil { | ||
return "", err | ||
} | ||
switch s { | ||
case "user": | ||
return `.containerd.user = true | .containerd.system = false`, nil | ||
case "system": | ||
return `.containerd.user = false | .containerd.system = true`, nil | ||
case "user+system", "system+user": | ||
return `.containerd.user = true | .containerd.system = true`, nil | ||
case "none": | ||
return `.containerd.user = false | .containerd.system = false`, nil | ||
default: | ||
return "", fmt.Errorf(`expected one of ["user", "system", "user+system", "none"], got %q`, s) | ||
} | ||
}, | ||
false}, | ||
|
||
{"disk", d(".disk= \"%sGiB\""), false}, | ||
{"vm-type", d(".vmType = %q"), false}, | ||
} | ||
var exprs []string | ||
for _, def := range defs { | ||
v := flags.Lookup(def.flagName) | ||
if v != nil && v.Changed { | ||
if def.experimental { | ||
logrus.Warnf("`--%s` is experimental", def.flagName) | ||
} | ||
expr, err := def.exprFunc(v) | ||
if err != nil { | ||
return exprs, fmt.Errorf("error while processing flag %q: %w", def.flagName, err) | ||
} | ||
exprs = append(exprs, expr) | ||
} | ||
} | ||
return exprs, 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
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