Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Add exec field
Browse files Browse the repository at this point in the history
Closes #85
  • Loading branch information
DAlperin committed Oct 4, 2022
1 parent 4f6efdf commit 2843871
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 9 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 13 additions & 1 deletion internal/provider/machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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},
Expand All @@ -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,
Expand Down Expand Up @@ -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,
},
},
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
},
},
}
Expand Down Expand Up @@ -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,
}
Expand Down
51 changes: 50 additions & 1 deletion internal/provider/machine_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"),
),
},
},
Expand All @@ -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"
Expand All @@ -187,6 +218,7 @@ resource "fly_machine" "testMachine" {
}
cmd = ["cmdText"]
entrypoint = ["entrypointText"]
exec = ["execText"]
}
`, app, name)
}
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion internal/wg/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
5 changes: 4 additions & 1 deletion pkg/apiv1/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 2843871

Please sign in to comment.