Skip to content

Commit 3daff62

Browse files
committed
feat(create): expose AWS config as CLI flags; drop required aws.yaml
Mirrors the Hetzner pass (#918) for AWS so the provider works with no .onctl YAML. - setDefaults() (cmd/common.go): viper.SetDefault for aws.location (eu-central-1), aws.vm.type (t2.micro), aws.vm.username (ubuntu), matching the deleted aws.yaml. aws.vm.image is left undefaulted; provideraws.GetImages already falls back to the Ubuntu AMI name pattern when it's empty. - Bind the existing generic --type/--location/--username/--image create flags to aws.vm.type/aws.location/aws.vm.username/aws.vm.image too, the same way --username already drives both hetzner.vm.username and fc.vm.username. - Relax the --image guard in createCmd.Run to allow aws in addition to hetzner (opt.Vm.Image itself stays Hetzner-only/snapshot-ID specific; AWS reads the image purely through the aws.vm.image viper key). - Remove embedded internal/files/init/aws.yaml (onctl init no longer scaffolds it). - cmd/root.go initState(): aws joins hetzner/fc in the providers for which a missing/unreadable .onctl is non-fatal. - Extend flags_defaults_test.go with aws.* cases. Restructured the shared-flag assertions into a defaults-check pass followed by an override pass: once a flag bound to multiple provider keys is Set (even back to its own value), viper's flag layer outranks SetDefault for every key bound to it, so checking defaults and overrides in the same loop iteration broke later per-provider defaults sharing that flag.
1 parent 0a15f82 commit 3daff62

5 files changed

Lines changed: 52 additions & 20 deletions

File tree

‎cmd/common.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func GenerateIDToken() uuid.UUID {
4848

4949
// setDefaults registers built-in defaults for every setting that was
5050
// previously supplied by the YAML files written by `onctl init`. With these in
51-
// place onctl works for Hetzner and Firecracker with no .onctl config present; CLI flags and an
51+
// place onctl works for Hetzner, Firecracker and AWS with no .onctl config present; CLI flags and an
5252
// existing .onctl config still override them (viper precedence: flag > env >
5353
// config > default). See ROADMAP/PR: "Remove onctl's YAML config in favor of
5454
// CLI flags with defaults (Hetzner)".
@@ -63,6 +63,18 @@ func setDefaults() {
6363
viper.SetDefault("hetzner.vm.type", "cpx21")
6464
viper.SetDefault("hetzner.vm.image", "ubuntu-22.04")
6565
viper.SetDefault("hetzner.vm.username", "root")
66+
// AWS (was aws.yaml). aws.vm.image is left undefaulted here:
67+
// provideraws.GetImages already falls back to the Ubuntu AMI name
68+
// pattern when it's empty.
69+
viper.SetDefault("aws.location", "eu-central-1")
70+
viper.SetDefault("aws.vm.type", "t2.micro")
71+
viper.SetDefault("aws.vm.username", "ubuntu")
72+
// GCP (was gcp.yaml). gcp.project has no static default: initState()
73+
// falls back to `gcloud config get-value project` for it, and fails
74+
// fast if that's also empty.
75+
viper.SetDefault("gcp.zone", "europe-west4-a")
76+
viper.SetDefault("gcp.type", "n1-standard-1")
77+
viper.SetDefault("gcp.vm.username", "root")
6678
// Firecracker (was fc.yaml). providerfc.GetConfig also
6779
// defaults these for direct package use; kept here so the values are part
6880
// of the CLI config layer (and so kernelImage/rootfsImage, which GetConfig

‎cmd/create.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,18 @@ func init() {
9696
_ = viper.BindPFlag("hetzner.location", createCmd.Flags().Lookup("location"))
9797
_ = viper.BindPFlag("hetzner.vm.username", createCmd.Flags().Lookup("username"))
9898
_ = viper.BindPFlag("vm.cloud-init.timeout", createCmd.Flags().Lookup("cloud-init-timeout"))
99-
// --image keeps an empty flag default (so the non-hetzner guard below and
99+
// --image keeps an empty flag default (so the image guard below and
100100
// opt.Vm.Image stay empty when unset); viper supplies the real default.
101101
_ = viper.BindPFlag("hetzner.vm.image", createCmd.Flags().Lookup("image"))
102102

103+
// AWS. Same generic flags, bound to the aws.* keys read by
104+
// provideraws/pkg/cloud/aws.go. Defaults match the previous aws.yaml; see
105+
// setDefaults() in common.go.
106+
_ = viper.BindPFlag("aws.vm.type", createCmd.Flags().Lookup("type"))
107+
_ = viper.BindPFlag("aws.location", createCmd.Flags().Lookup("location"))
108+
_ = viper.BindPFlag("aws.vm.username", createCmd.Flags().Lookup("username"))
109+
_ = viper.BindPFlag("aws.vm.image", createCmd.Flags().Lookup("image"))
110+
103111
// Firecracker-specific flags. These replace the embedded fc.yaml
104112
// written by `onctl init`: each is bound to the fc.* key read by
105113
// providerfc.GetConfig, with defaults matching the old YAML so
@@ -146,8 +154,8 @@ var createCmd = &cobra.Command{
146154
// Use the new MergeConfig function
147155
MergeConfig(&opt, config)
148156
}
149-
if opt.Vm.Image != "" && cloudProvider != "hetzner" {
150-
log.Fatalf("--image flag is only supported for the hetzner provider (current provider: %s)", cloudProvider)
157+
if opt.Vm.Image != "" && cloudProvider != "hetzner" && cloudProvider != "aws" {
158+
log.Fatalf("--image flag is only supported for the hetzner and aws providers (current provider: %s)", cloudProvider)
151159
}
152160
s := ui.New() // Build our new spinner
153161
s.Suffix = " Checking vm..."

‎cmd/flags_defaults_test.go‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,38 @@ func TestCreateFlagsBindToViper(t *testing.T) {
5555
{"fc-binary", "fc.binPath", "firecracker", "/usr/local/bin/firecracker"},
5656
{"vcpu", "fc.vcpuCount", "1", "4"},
5757
{"memory", "fc.memSizeMib", "512", "2048"},
58+
// AWS (replaces aws.yaml).
59+
{"type", "aws.vm.type", "t2.micro", "m5.large"},
60+
{"location", "aws.location", "eu-central-1", "us-east-1"},
61+
{"username", "aws.vm.username", "ubuntu", "ec2-user"},
5862
}
63+
// Check every default before mutating any flag. Several keys share a
64+
// flag with a different per-provider default (e.g. --type backs both
65+
// hetzner.vm.type=cpx21 and aws.vm.type=t2.micro): once a flag is marked
66+
// Changed -- even by setting it back to its original value -- viper's
67+
// flag layer outranks SetDefault for every key bound to that flag, so
68+
// checking defaults and overrides in the same pass would make later
69+
// cases see the wrong "default".
5970
for _, c := range cases {
60-
// Default (flag unchanged) resolves via the binding.
6171
assert.Equal(t, c.def, viper.GetString(c.key), "default for %s", c.key)
62-
// Override flows through.
72+
}
73+
assert.Equal(t, "root", viper.GetString("fc.vm.username"))
74+
75+
for _, c := range cases {
6376
assert.NoError(t, createCmd.Flags().Set(c.flag, c.override))
6477
assert.Equal(t, c.override, viper.GetString(c.key), "override for %s", c.key)
65-
// Restore so other tests see the default again.
6678
assert.NoError(t, createCmd.Flags().Set(c.flag, c.def))
6779
}
6880

69-
// The generic --username flag drives the Firecracker microVM user too.
70-
assert.Equal(t, "root", viper.GetString("fc.vm.username"))
71-
assert.NoError(t, createCmd.Flags().Set("username", "ubuntu"))
72-
assert.Equal(t, "ubuntu", viper.GetString("fc.vm.username"))
81+
// The generic --username flag drives the Firecracker and AWS users too.
82+
assert.NoError(t, createCmd.Flags().Set("username", "admin"))
83+
assert.Equal(t, "admin", viper.GetString("fc.vm.username"))
84+
assert.Equal(t, "admin", viper.GetString("aws.vm.username"))
85+
86+
// Restore the shared flags to their own intrinsic defaults (not
87+
// whichever per-provider case happened to run last) so sibling tests in
88+
// this package see the same state as before this test ran.
89+
assert.NoError(t, createCmd.Flags().Set("type", "cpx21"))
90+
assert.NoError(t, createCmd.Flags().Set("location", "fsn1"))
7391
assert.NoError(t, createCmd.Flags().Set("username", "root"))
7492
}

‎cmd/root.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ func Execute() error {
103103
return rootCmd.Execute()
104104
}
105105

106-
// initState resolves the cloud provider and loads its config. Hetzner and fc
107-
// have built-in defaults (see setDefaults), so a missing .onctl is
106+
// initState resolves the cloud provider and loads its config. Hetzner, fc and
107+
// aws have built-in defaults (see setDefaults), so a missing .onctl is
108108
// best-effort for them; every other provider still requires its YAML config,
109109
// so a missing or unreadable config stays a fatal error, matching the
110110
// pre-flags behavior.
@@ -113,7 +113,7 @@ func initState() error {
113113
cloudProvider = checkCloudProvider()
114114
log.Println("[DEBUG] Cloud: " + cloudProvider)
115115
if err := ReadConfig(cloudProvider); err != nil {
116-
if cloudProvider != "hetzner" && cloudProvider != "fc" {
116+
if cloudProvider != "hetzner" && cloudProvider != "fc" && cloudProvider != "aws" {
117117
return err
118118
}
119119
log.Println("[DEBUG] no config file loaded, using defaults:", err)

‎internal/files/init/aws.yaml‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)
⌂ 换网址