|
| 1 | +package editflags |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/bits" |
| 6 | + "runtime" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/pbnjay/memory" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + flag "github.com/spf13/pflag" |
| 14 | +) |
| 15 | + |
| 16 | +// RegisterEdit registers flags related to in-place YAML modification, for `limactl edit`. |
| 17 | +func RegisterEdit(cmd *cobra.Command) { |
| 18 | + registerEdit(cmd, "") |
| 19 | +} |
| 20 | + |
| 21 | +func registerEdit(cmd *cobra.Command, commentPrefix string) { |
| 22 | + flags := cmd.Flags() |
| 23 | + |
| 24 | + flags.Int("cpus", 0, commentPrefix+"number of CPUs") // Similar to colima's --cpu, but the flag name is slightly different (cpu vs cpus) |
| 25 | + _ = cmd.RegisterFlagCompletionFunc("cpus", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 26 | + var res []string |
| 27 | + for _, f := range completeCPUs(runtime.NumCPU()) { |
| 28 | + res = append(res, strconv.Itoa(f)) |
| 29 | + } |
| 30 | + return res, cobra.ShellCompDirectiveNoFileComp |
| 31 | + }) |
| 32 | + |
| 33 | + flags.IPSlice("dns", nil, commentPrefix+"specify custom DNS (disable host resolver)") // colima-compatible |
| 34 | + |
| 35 | + flags.Float32("memory", 0, commentPrefix+"memory in GiB") // colima-compatible |
| 36 | + _ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 37 | + var res []string |
| 38 | + for _, f := range completeMemoryGiB(memory.TotalMemory()) { |
| 39 | + res = append(res, fmt.Sprintf("%.1f", f)) |
| 40 | + } |
| 41 | + return res, cobra.ShellCompDirectiveNoFileComp |
| 42 | + }) |
| 43 | + |
| 44 | + flags.StringSlice("mount", nil, commentPrefix+"directories to mount, suffix ':w' for writable (Do not specify directories that overlap with the existing mounts)") // colima-compatible |
| 45 | + |
| 46 | + flags.String("mount-type", "", commentPrefix+"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 |
| 47 | + _ = cmd.RegisterFlagCompletionFunc("mount-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 48 | + return []string{"reverse-sshfs", "9p", "virtiofs"}, cobra.ShellCompDirectiveNoFileComp |
| 49 | + }) |
| 50 | + |
| 51 | + flags.Bool("mount-writable", false, commentPrefix+"make all mounts writable") |
| 52 | + |
| 53 | + flags.StringSlice("network", nil, commentPrefix+"additional networks, e.g., \"vzNAT\" or \"lima:shared\" to assign vmnet IP") |
| 54 | + _ = cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 55 | + // TODO: retrieve the lima:* network list from networks.yaml |
| 56 | + return []string{"lima:shared", "lima:bridged", "lima:host", "lima:user-v2", "vzNAT"}, cobra.ShellCompDirectiveNoFileComp |
| 57 | + }) |
| 58 | + |
| 59 | + flags.Bool("rosetta", false, commentPrefix+"enable Rosetta (for vz instances)") |
| 60 | + |
| 61 | + flags.String("set", "", commentPrefix+"modify the template inplace, using yq syntax") |
| 62 | +} |
| 63 | + |
| 64 | +// RegisterCreate registers flags related to in-place YAML modification, for `limactl create`. |
| 65 | +func RegisterCreate(cmd *cobra.Command, commentPrefix string) { |
| 66 | + registerEdit(cmd, commentPrefix) |
| 67 | + flags := cmd.Flags() |
| 68 | + |
| 69 | + flags.String("arch", "", commentPrefix+"machine architecture (x86_64, aarch64, riscv64)") // colima-compatible |
| 70 | + _ = cmd.RegisterFlagCompletionFunc("arch", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 71 | + return []string{"x86_64", "aarch64", "riscv64"}, cobra.ShellCompDirectiveNoFileComp |
| 72 | + }) |
| 73 | + |
| 74 | + flags.String("containerd", "", commentPrefix+"containerd mode (user, system, user+system, none)") |
| 75 | + _ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 76 | + return []string{"user", "system", "user+system", "none"}, cobra.ShellCompDirectiveNoFileComp |
| 77 | + }) |
| 78 | + |
| 79 | + flags.Float32("disk", 0, commentPrefix+"disk size in GiB") // colima-compatible |
| 80 | + _ = cmd.RegisterFlagCompletionFunc("memory", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 81 | + return []string{"10", "30", "50", "100", "200"}, cobra.ShellCompDirectiveNoFileComp |
| 82 | + }) |
| 83 | + |
| 84 | + flags.String("vm-type", "", commentPrefix+"virtual machine type (qemu, vz)") // colima-compatible |
| 85 | + _ = cmd.RegisterFlagCompletionFunc("vm-type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 86 | + return []string{"qemu", "vz"}, cobra.ShellCompDirectiveNoFileComp |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func defaultExprFunc(expr string) func(v *flag.Flag) (string, error) { |
| 91 | + return func(v *flag.Flag) (string, error) { |
| 92 | + return fmt.Sprintf(expr, v.Value), nil |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// YQExpressions returns YQ expressions. |
| 97 | +func YQExpressions(flags *flag.FlagSet) ([]string, error) { |
| 98 | + type def struct { |
| 99 | + flagName string |
| 100 | + exprFunc func(*flag.Flag) (string, error) |
| 101 | + experimental bool |
| 102 | + } |
| 103 | + d := defaultExprFunc |
| 104 | + defs := []def{ |
| 105 | + {"cpus", d(".cpus = %s"), false}, |
| 106 | + {"dns", |
| 107 | + func(_ *flag.Flag) (string, error) { |
| 108 | + ipSlice, err := flags.GetIPSlice("dns") |
| 109 | + if err != nil { |
| 110 | + return "", err |
| 111 | + } |
| 112 | + expr := `.dns += [` |
| 113 | + for i, ip := range ipSlice { |
| 114 | + expr += fmt.Sprintf("%q", ip) |
| 115 | + if i < len(ipSlice)-1 { |
| 116 | + expr += "," |
| 117 | + } |
| 118 | + } |
| 119 | + expr += `] | .dns |= unique | .hostResolver.enabled=false` |
| 120 | + logrus.Warnf("Disabling HostResolver, as custom DNS addresses (%v) are specified", ipSlice) |
| 121 | + return expr, nil |
| 122 | + }, |
| 123 | + false}, |
| 124 | + {"memory", d(".memory = \"%sGiB\""), false}, |
| 125 | + {"mount", |
| 126 | + func(_ *flag.Flag) (string, error) { |
| 127 | + ss, err := flags.GetStringSlice("mount") |
| 128 | + if err != nil { |
| 129 | + return "", err |
| 130 | + } |
| 131 | + expr := `.mounts += [` |
| 132 | + for i, s := range ss { |
| 133 | + writable := strings.HasSuffix(s, ":w") |
| 134 | + loc := strings.TrimSuffix(s, ":w") |
| 135 | + expr += fmt.Sprintf(`{"location": %q, "writable": %v}`, loc, writable) |
| 136 | + if i < len(ss)-1 { |
| 137 | + expr += "," |
| 138 | + } |
| 139 | + } |
| 140 | + expr += `] | .mounts |= unique_by(.location)` |
| 141 | + return expr, nil |
| 142 | + }, |
| 143 | + false}, |
| 144 | + {"mount-type", d(".mountType = %q"), false}, |
| 145 | + {"mount-writable", d(".mounts[].writable = %s"), false}, |
| 146 | + {"network", |
| 147 | + func(_ *flag.Flag) (string, error) { |
| 148 | + ss, err := flags.GetStringSlice("network") |
| 149 | + if err != nil { |
| 150 | + return "", err |
| 151 | + } |
| 152 | + expr := `.networks += [` |
| 153 | + for i, s := range ss { |
| 154 | + // CLI syntax is still experimental (YAML syntax is out of experimental) |
| 155 | + switch { |
| 156 | + case s == "vzNAT": |
| 157 | + expr += `{"vzNAT": true}` |
| 158 | + case strings.HasPrefix(s, "lima:"): |
| 159 | + network := strings.TrimPrefix(s, "lima:") |
| 160 | + expr += fmt.Sprintf(`{"lima": %q}`, network) |
| 161 | + default: |
| 162 | + return "", fmt.Errorf("network name must be \"vzNAT\" or \"lima:*\", got %q", s) |
| 163 | + } |
| 164 | + if i < len(ss)-1 { |
| 165 | + expr += "," |
| 166 | + } |
| 167 | + } |
| 168 | + expr += `] | .networks |= unique_by(.lima)` |
| 169 | + return expr, nil |
| 170 | + }, |
| 171 | + true}, |
| 172 | + {"rosetta", |
| 173 | + func(_ *flag.Flag) (string, error) { |
| 174 | + b, err := flags.GetBool("rosetta") |
| 175 | + if err != nil { |
| 176 | + return "", err |
| 177 | + } |
| 178 | + return fmt.Sprintf(".rosetta.enabled = %v | .rosetta.binfmt = %v", b, b), nil |
| 179 | + }, |
| 180 | + true}, |
| 181 | + {"set", d("%s"), true}, |
| 182 | + {"arch", d(".arch = %q"), false}, |
| 183 | + {"containerd", |
| 184 | + func(_ *flag.Flag) (string, error) { |
| 185 | + s, err := flags.GetString("containerd") |
| 186 | + if err != nil { |
| 187 | + return "", err |
| 188 | + } |
| 189 | + switch s { |
| 190 | + case "user": |
| 191 | + return `.containerd.user = true | .containerd.system = false`, nil |
| 192 | + case "system": |
| 193 | + return `.containerd.user = false | .containerd.system = true`, nil |
| 194 | + case "user+system", "system+user": |
| 195 | + return `.containerd.user = true | .containerd.system = true`, nil |
| 196 | + case "none": |
| 197 | + return `.containerd.user = false | .containerd.system = false`, nil |
| 198 | + default: |
| 199 | + return "", fmt.Errorf(`expected one of ["user", "system", "user+system", "none"], got %q`, s) |
| 200 | + } |
| 201 | + }, |
| 202 | + false}, |
| 203 | + |
| 204 | + {"disk", d(".disk= \"%sGiB\""), false}, |
| 205 | + {"vm-type", d(".vmType = %q"), false}, |
| 206 | + } |
| 207 | + var exprs []string |
| 208 | + for _, def := range defs { |
| 209 | + v := flags.Lookup(def.flagName) |
| 210 | + if v != nil && v.Changed { |
| 211 | + if def.experimental { |
| 212 | + logrus.Warnf("`--%s` is experimental", def.flagName) |
| 213 | + } |
| 214 | + expr, err := def.exprFunc(v) |
| 215 | + if err != nil { |
| 216 | + return exprs, fmt.Errorf("error while processing flag %q: %w", def.flagName, err) |
| 217 | + } |
| 218 | + exprs = append(exprs, expr) |
| 219 | + } |
| 220 | + } |
| 221 | + return exprs, nil |
| 222 | +} |
| 223 | + |
| 224 | +func isPowerOfTwo(x int) bool { |
| 225 | + return bits.OnesCount(uint(x)) == 1 |
| 226 | +} |
| 227 | + |
| 228 | +func completeCPUs(hostCPUs int) []int { |
| 229 | + var res []int |
| 230 | + for i := 1; i <= hostCPUs; i *= 2 { |
| 231 | + res = append(res, i) |
| 232 | + } |
| 233 | + if !isPowerOfTwo(hostCPUs) { |
| 234 | + res = append(res, hostCPUs) |
| 235 | + } |
| 236 | + return res |
| 237 | +} |
| 238 | + |
| 239 | +func completeMemoryGiB(hostMemory uint64) []float32 { |
| 240 | + hostMemoryHalfGiB := int(hostMemory / 2 / 1024 / 1024 / 1024) |
| 241 | + var res []float32 |
| 242 | + if hostMemoryHalfGiB < 1 { |
| 243 | + res = append(res, 0.5) |
| 244 | + } |
| 245 | + for i := 1; i <= hostMemoryHalfGiB; i *= 2 { |
| 246 | + res = append(res, float32(i)) |
| 247 | + } |
| 248 | + if hostMemoryHalfGiB > 1 && !isPowerOfTwo(hostMemoryHalfGiB) { |
| 249 | + res = append(res, float32(hostMemoryHalfGiB)) |
| 250 | + } |
| 251 | + return res |
| 252 | +} |
0 commit comments