From 28438713f2bdf08dbd0aa2fae9d74baaca9845f1 Mon Sep 17 00:00:00 2001 From: Dov Alperin Date: Tue, 4 Oct 2022 14:42:43 -0400 Subject: [PATCH] Add exec field Closes #85 --- .github/workflows/test.yml | 8 ++-- go.mod | 1 + go.sum | 1 + internal/provider/machine_resource.go | 14 +++++- internal/provider/machine_resource_test.go | 51 +++++++++++++++++++++- internal/wg/tunnel.go | 3 +- pkg/apiv1/machines.go | 5 ++- shell.nix | 2 +- 8 files changed, 76 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fda6ed0..6c96d1b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -50,10 +50,10 @@ jobs: with: terraform_version: ${{ matrix.terraform }} terraform_wrapper: false - - uses: superfly/flyctl-actions/setup-flyctl@master - - run: flyctl proxy 4280:4280 _api.internal -o fly-terraform-ci -a acctestapp & - env: - FLY_API_TOKEN: ${{ secrets.FLY_AUTH_TOKEN_CI }} +# - uses: superfly/flyctl-actions/setup-flyctl@master +# - run: flyctl proxy 4280:4280 _api.internal -o fly-terraform-ci -a acctestapp & +# env: +# FLY_API_TOKEN: ${{ secrets.FLY_AUTH_TOKEN_CI }} - uses: actions/checkout@v3 - run: go mod download - env: diff --git a/go.mod b/go.mod index 530a2df..28a2354 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/Khan/genqlient v0.5.0 + github.com/google/uuid v1.2.0 github.com/hashicorp/terraform-plugin-framework v0.11.1 github.com/hashicorp/terraform-plugin-go v0.14.0 github.com/hashicorp/terraform-plugin-log v0.7.0 diff --git a/go.sum b/go.sum index a88b2a1..7b4e304 100644 --- a/go.sum +++ b/go.sum @@ -385,6 +385,7 @@ github.com/google/subcommands v1.0.2-0.20190508160503-636abe8753b8/go.mod h1:Zjh github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg= diff --git a/internal/provider/machine_resource.go b/internal/provider/machine_resource.go index fbf3da8..83b4dd9 100644 --- a/internal/provider/machine_resource.go +++ b/internal/provider/machine_resource.go @@ -51,6 +51,7 @@ type flyMachineResourceData struct { Env types.Map `tfsdk:"env"` Cmd []string `tfsdk:"cmd"` Entrypoint []string `tfsdk:"entrypoint"` + Exec []string `tfsdk:"exec"` Mounts []TfMachineMount `tfsdk:"mounts"` Services []TfService `tfsdk:"services"` @@ -100,7 +101,7 @@ func (mr flyMachineResourceType) GetSchema(context.Context) (tfsdk.Schema, diag. Type: types.StringType, }, "cmd": { - MarkdownDescription: "exec command", + MarkdownDescription: "cmd", Optional: true, //Computed: true, Type: types.ListType{ElemType: types.StringType}, @@ -111,6 +112,12 @@ func (mr flyMachineResourceType) GetSchema(context.Context) (tfsdk.Schema, diag. //Computed: true, Type: types.ListType{ElemType: types.StringType}, }, + "exec": { + MarkdownDescription: "exec command", + Optional: true, + //Computed: true, + Type: types.ListType{ElemType: types.StringType}, + }, "image": { MarkdownDescription: "docker image", Required: true, @@ -289,6 +296,7 @@ func (mr flyMachineResource) Create(ctx context.Context, req resource.CreateRequ Init: apiv1.InitConfig{ Cmd: data.Cmd, Entrypoint: data.Entrypoint, + Exec: data.Exec, }, }, } @@ -352,6 +360,7 @@ func (mr flyMachineResource) Create(ctx context.Context, req resource.CreateRequ CpuType: types.String{Value: newMachine.Config.Guest.CPUKind}, Cmd: newMachine.Config.Init.Cmd, Entrypoint: newMachine.Config.Init.Entrypoint, + Exec: newMachine.Config.Init.Exec, Env: env, Services: tfservices, } @@ -423,6 +432,7 @@ func (mr flyMachineResource) Read(ctx context.Context, req resource.ReadRequest, CpuType: types.String{Value: machine.Config.Guest.CPUKind}, Cmd: machine.Config.Init.Cmd, Entrypoint: machine.Config.Init.Entrypoint, + Exec: machine.Config.Init.Exec, Env: env, Services: tfservices, } @@ -490,6 +500,7 @@ func (mr flyMachineResource) Update(ctx context.Context, req resource.UpdateRequ Init: apiv1.InitConfig{ Cmd: plan.Cmd, Entrypoint: plan.Entrypoint, + Exec: plan.Exec, }, }, } @@ -553,6 +564,7 @@ func (mr flyMachineResource) Update(ctx context.Context, req resource.UpdateRequ CpuType: types.String{Value: updatedMachine.Config.Guest.CPUKind}, Cmd: updatedMachine.Config.Init.Cmd, Entrypoint: updatedMachine.Config.Init.Entrypoint, + Exec: updatedMachine.Config.Init.Exec, Env: env, Services: tfservices, } diff --git a/internal/provider/machine_resource_test.go b/internal/provider/machine_resource_test.go index f029470..9037a2b 100644 --- a/internal/provider/machine_resource_test.go +++ b/internal/provider/machine_resource_test.go @@ -31,6 +31,12 @@ func testFlyMachineResourceConfig(name string) string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" @@ -63,6 +69,12 @@ func testFlyMachineResourceUpdateConfig(name string) string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" @@ -110,6 +122,12 @@ func testFlyMachineResourceNoServicesConfig(name string) string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" @@ -141,6 +159,12 @@ func testFlyMachineResourceEmptyServicesConfig(name string) string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" @@ -167,6 +191,7 @@ func TestAccFlyMachineInitOptions(t *testing.T) { resource.TestCheckResourceAttr("fly_machine.testMachine", "name", rName), resource.TestCheckResourceAttr("fly_machine.testMachine", "cmd.0", "cmdText"), resource.TestCheckResourceAttr("fly_machine.testMachine", "entrypoint.0", "entrypointText"), + resource.TestCheckResourceAttr("fly_machine.testMachine", "exec.0", "execText"), ), }, }, @@ -177,6 +202,12 @@ func testFlyMachineResourceInitOptionsConfig(name string) string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" @@ -187,6 +218,7 @@ resource "fly_machine" "testMachine" { } cmd = ["cmdText"] entrypoint = ["entrypointText"] + exec = ["execText"] } `, app, name) } @@ -212,6 +244,12 @@ func testFlyMachineResourceChangeImageConfig(name string) string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" @@ -256,11 +294,16 @@ func TestAccFlyMachineEmptyName(t *testing.T) { }) } - func testFlyMachineResourceEmptyNameConfig() string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" @@ -292,6 +335,12 @@ func testFlyMachineResourceEmptyNameUpdateConfig() string { app := os.Getenv("FLY_TF_TEST_APP") return fmt.Sprintf(` +provider "fly" { + useinternaltunnel = true + internaltunnelorg = "fly-terraform-ci" + internaltunnelregion = "ewr" +} + resource "fly_machine" "testMachine" { app = "%s" region = "ewr" diff --git a/internal/wg/tunnel.go b/internal/wg/tunnel.go index 0fa028f..69548dd 100644 --- a/internal/wg/tunnel.go +++ b/internal/wg/tunnel.go @@ -9,6 +9,7 @@ import ( "fmt" rawgql "github.com/Khan/genqlient/graphql" "github.com/fly-apps/terraform-provider-fly/graphql" + "github.com/google/uuid" "github.com/miekg/dns" "golang.org/x/crypto/curve25519" "golang.zx2c4.com/wireguard/conn" @@ -343,7 +344,7 @@ func (t *Tunnel) QueryDNS(ctx context.Context, msg *dns.Msg) (*dns.Msg, error) { } func Establish(ctx context.Context, org string, region string, token string, client *rawgql.Client) (*Tunnel, error) { - peerName := "terraform-tunnel-" + strconv.FormatInt(time.Now().Unix(), 10) + peerName := "terraform-tunnel-" + strconv.FormatInt(time.Now().Unix(), 10) + uuid.New().String() public, private := C25519pair() peer, err := graphql.AddWireguardPeer(ctx, *client, graphql.AddWireGuardPeerInput{ diff --git a/pkg/apiv1/machines.go b/pkg/apiv1/machines.go index 185466d..a8c4cdc 100644 --- a/pkg/apiv1/machines.go +++ b/pkg/apiv1/machines.go @@ -38,6 +38,7 @@ type Service struct { type InitConfig struct { Cmd []string `json:"cmd,omitempty"` Entrypoint []string `json:"entrypoint,omitempty"` + Exec []string `json:"exec,omitempty"` } type MachineConfig struct { @@ -71,7 +72,7 @@ type MachineResponse struct { Config struct { Env map[string]string `json:"env"` Init struct { - //Exec interface{} `json:"exec"` + Exec []string `json:"exec"` Entrypoint []string `json:"entrypoint"` Cmd []string `json:"cmd"` //Tty bool `json:"tty"` @@ -166,9 +167,11 @@ func (a *MachineAPI) UpdateMachine(req MachineCreateOrUpdateRequest, app string, req.Config.Guest.CpuType = "shared" } if req.Config.Guest.Cpus == 0 { + //You can't have a machine with no cpus req.Config.Guest.Cpus = 1 } if req.Config.Guest.MemoryMb == 0 { + //You can't have a machine with no memory req.Config.Guest.MemoryMb = 256 } lease, err := a.LockMachine(app, id, 30) diff --git a/shell.nix b/shell.nix index 190c1de..fe57e86 100644 --- a/shell.nix +++ b/shell.nix @@ -1,4 +1,4 @@ -with (import (fetchTarball https://github.com/nixos/nixpkgs/archive/592dc9ed7f049c565e9d7c04a4907e57ae17e2d9.tar.gz) {}); +with (import (fetchTarball https://github.com/nixos/nixpkgs/archive/master.tar.gz) {}); let