Skip to content

Commit 31af450

Browse files
committed
code
1 parent 567c3a7 commit 31af450

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+10360
-4730
lines changed

examples/main.tf

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ terraform {
99

1010
provider "iterative" {}
1111

12-
/* resource "iterative_machine" "machine-aws" {
12+
13+
resource "iterative_machine" "machine-aws" {
1314
driver = "aws"
1415
region = "us-west"
1516
instance_type = "t2.micro" //fallback to known instance type
16-
} */
17+
}
18+
1719

1820
resource "iterative_machine" "machine-azure" {
1921
driver = "azure"
@@ -22,13 +24,34 @@ resource "iterative_machine" "machine-azure" {
2224

2325
provisioner "remote-exec" {
2426
inline = [
25-
"ls",
27+
"echo 'hello azure'"
28+
]
29+
30+
connection {
31+
user = "ubuntu"
32+
private_key = "${self.key_private}"
33+
host = "${self.instance_ip}"
34+
}
35+
}
36+
}
37+
38+
39+
/* resource "iterative_machine" "machine-azure-gpu" {
40+
driver = "azure"
41+
region = "us-east"
42+
instance_type = "m"
43+
instance_gpu = "k80"
44+
45+
provisioner "remote-exec" {
46+
inline = [
47+
"echo 'hello azure'",
48+
"nvidia-smi"
2649
]
2750
2851
connection {
29-
user = "ubuntu"
52+
user = "ubuntu"
3053
private_key = "${self.key_private}"
31-
host = "${self.instance_ip}"
54+
host = "${self.instance_ip}"
3255
}
3356
}
34-
}
57+
} */

iterative/aws/provider.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
2828
}
2929

3030
// Image
31-
ami := d.Get("ami").(string)
31+
ami := ""
32+
if ami == "" {
33+
ami = "iterative-cml"
34+
}
3235
imagesRes, imagesErr := svc.DescribeImages(&ec2.DescribeImagesInput{
3336
Filters: []*ec2.Filter{
3437
{
@@ -275,7 +278,8 @@ func getInstanceType(d *schema.ResourceData) string {
275278
instanceTypes["xltesla"] = "p3.16xlarge"
276279

277280
instanceType := d.Get("instance_type").(string)
278-
if val, ok := instanceTypes[instanceType+d.Get("instance_gpu").(string)]; ok {
281+
instanceGPU := d.Get("instance_gpu").(string)
282+
if val, ok := instanceTypes[instanceType+instanceGPU]; ok {
279283
instanceType = val
280284
}
281285

iterative/azure/provider.go

Lines changed: 71 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"strings"
8+
"time"
79

8-
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
10+
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
911
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-11-01/network"
1012
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources"
13+
1114
"github.com/Azure/go-autorest/autorest/azure/auth"
1215
"github.com/Azure/go-autorest/autorest/to"
1316

@@ -25,18 +28,20 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
2528
instanceType := getInstanceType(d)
2629
keyPublic := d.Get("key_public").(string)
2730
vmName := d.Get("instance_name").(string)
31+
hddSize := int32(d.Get("instance_hdd_size").(int))
2832

29-
publisher := "Canonical"
30-
offer := "UbuntuServer"
31-
sku := "18.04-LTS"
32-
version := "latest"
33+
image := d.Get("ami").(string)
34+
if image == "" {
35+
image = "Canonical:UbuntuServer:18.04-LTS:latest"
36+
}
37+
imageParts := strings.Split(image, ":")
3338

34-
//publisher := "NVIDIA-GPU"
35-
//offer := "GPU"
36-
//sku := "Cloud-Image"
37-
//version := "2020.06.29"
39+
publisher := imageParts[0]
40+
offer := imageParts[1]
41+
sku := imageParts[2]
42+
version := imageParts[3]
3843

39-
gpName := vmName + "-sg"
44+
gpName := vmName
4045
nsgName := vmName + "-nsg"
4146
vnetName := vmName + "-vnet"
4247
ipName := vmName + "-ip"
@@ -45,7 +50,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
4550
ipConfigName := vmName + "-ipc"
4651

4752
username := "ubuntu"
48-
password := "ubuntu"
53+
54+
d.Set("instance_id", vmName)
4955

5056
groupsClient, err := getGroupsClient(subscriptionID)
5157
_, err = groupsClient.CreateOrUpdate(
@@ -80,7 +86,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
8086
SourceAddressPrefix: to.StringPtr("0.0.0.0/0"),
8187
SourcePortRange: to.StringPtr("1-65535"),
8288
DestinationAddressPrefix: to.StringPtr("0.0.0.0/0"),
83-
DestinationPortRange: to.StringPtr("22"),
89+
DestinationPortRange: to.StringPtr("1-65535"),
8490
Access: network.SecurityRuleAccessAllow,
8591
Direction: network.SecurityRuleDirectionInbound,
8692
Priority: to.Int32Ptr(100),
@@ -93,6 +99,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
9399
futureNsg.WaitForCompletionRef(ctx, nsgClient.Client)
94100
nsg, err := nsgClient.Get(ctx, gpName, nsgName, "")
95101
if err != nil {
102+
ResourceMachineDelete(ctx, d, m)
103+
96104
diags = append(diags, diag.Diagnostic{
97105
Severity: diag.Error,
98106
Summary: fmt.Sprintf("Security group is not ready: %v", err),
@@ -119,6 +127,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
119127
futureIP.WaitForCompletionRef(ctx, ipClient.Client)
120128
ip, err := ipClient.Get(ctx, gpName, ipName, "")
121129
if err != nil {
130+
ResourceMachineDelete(ctx, d, m)
122131
diags = append(diags, diag.Diagnostic{
123132
Severity: diag.Error,
124133
Summary: fmt.Sprintf("cannot create pi: %v", err),
@@ -143,6 +152,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
143152
futureVnet.WaitForCompletionRef(ctx, vnetClient.Client)
144153
_, err = vnetClient.Get(ctx, gpName, vnetName, "")
145154
if err != nil {
155+
ResourceMachineDelete(ctx, d, m)
146156
diags = append(diags, diag.Diagnostic{
147157
Severity: diag.Error,
148158
Summary: fmt.Sprintf("cannot create vnet: %v", err),
@@ -166,6 +176,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
166176
})
167177
futureSubnet.WaitForCompletionRef(ctx, subnetsClient.Client)
168178
if err != nil {
179+
ResourceMachineDelete(ctx, d, m)
169180
diags = append(diags, diag.Diagnostic{
170181
Severity: diag.Error,
171182
Summary: fmt.Sprintf("cannot create subnet: %v", err),
@@ -175,6 +186,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
175186
}
176187
subnet, err := subnetsClient.Get(ctx, gpName, vnetName, subnetName, "")
177188
if err != nil {
189+
ResourceMachineDelete(ctx, d, m)
178190
diags = append(diags, diag.Diagnostic{
179191
Severity: diag.Error,
180192
Summary: fmt.Sprintf("cannot create subnet: %v", err),
@@ -205,6 +217,7 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
205217
futureNic.WaitForCompletionRef(ctx, nicClient.Client)
206218
nic, err := nicClient.Get(ctx, gpName, nicName, "")
207219
if err != nil {
220+
ResourceMachineDelete(ctx, d, m)
208221
diags = append(diags, diag.Diagnostic{
209222
Severity: diag.Error,
210223
Summary: fmt.Sprintf("cannot create nic: %v", err),
@@ -220,6 +233,13 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
220233
vmName,
221234
compute.VirtualMachine{
222235
Location: to.StringPtr(region),
236+
/*
237+
Plan: &compute.Plan{
238+
Name: to.StringPtr(sku),
239+
Publisher: to.StringPtr(publisher),
240+
Product: to.StringPtr(offer),
241+
},
242+
*/
223243
VirtualMachineProperties: &compute.VirtualMachineProperties{
224244
HardwareProfile: &compute.HardwareProfile{
225245
VMSize: compute.VirtualMachineSizeTypes(instanceType),
@@ -231,12 +251,21 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
231251
Sku: to.StringPtr(sku),
232252
Version: to.StringPtr(version),
233253
},
254+
OsDisk: &compute.OSDisk{
255+
Name: to.StringPtr(fmt.Sprintf(vmName + "-hdd")),
256+
Caching: compute.CachingTypesReadWrite,
257+
CreateOption: compute.DiskCreateOptionTypesFromImage,
258+
DiskSizeGB: to.Int32Ptr(hddSize),
259+
ManagedDisk: &compute.ManagedDiskParameters{
260+
StorageAccountType: compute.StorageAccountTypesStandardLRS,
261+
},
262+
},
234263
},
235264
OsProfile: &compute.OSProfile{
236-
ComputerName: to.StringPtr(vmName),
265+
ComputerName: to.StringPtr("iterative"),
237266
AdminUsername: to.StringPtr(username),
238-
AdminPassword: to.StringPtr(password),
239267
LinuxConfiguration: &compute.LinuxConfiguration{
268+
DisablePasswordAuthentication: to.BoolPtr(true),
240269
SSH: &compute.SSHConfiguration{
241270
PublicKeys: &[]compute.SSHPublicKey{
242271
{
@@ -260,9 +289,28 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
260289
},
261290
},
262291
)
263-
futureVM.WaitForCompletionRef(ctx, vmClient.Client)
292+
if err != nil {
293+
ResourceMachineDelete(ctx, d, m)
294+
diags = append(diags, diag.Diagnostic{
295+
Severity: diag.Error,
296+
Summary: fmt.Sprintf("cannot create vm: %v", err),
297+
})
298+
299+
return diags
300+
}
301+
err = futureVM.WaitForCompletionRef(ctx, vmClient.Client)
302+
if err != nil {
303+
ResourceMachineDelete(ctx, d, m)
304+
diags = append(diags, diag.Diagnostic{
305+
Severity: diag.Error,
306+
Summary: fmt.Sprintf("cannot create vm: %v", err),
307+
})
308+
309+
return diags
310+
}
264311
_, err = vmClient.Get(ctx, gpName, vmName, "")
265312
if err != nil {
313+
ResourceMachineDelete(ctx, d, m)
266314
diags = append(diags, diag.Diagnostic{
267315
Severity: diag.Error,
268316
Summary: fmt.Sprintf("cannot create vm: %v", err),
@@ -272,9 +320,8 @@ func ResourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
272320
}
273321

274322
d.SetId(gpName)
275-
d.Set("instance_id", vmName)
276323
d.Set("instance_ip", ip.IPAddress)
277-
//d.Set("instance_launch_time", vm.)
324+
d.Set("instance_launch_time", time.Now().String())
278325

279326
return diags
280327
}
@@ -285,8 +332,7 @@ func ResourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
285332

286333
subscriptionID := os.Getenv("AZURE_SUBSCRIPTION_ID")
287334
groupsClient, err := getGroupsClient(subscriptionID)
288-
future, err := groupsClient.Delete(context.Background(), d.Id())
289-
err = future.WaitForCompletionRef(ctx, groupsClient.Client)
335+
_, err = groupsClient.Delete(context.Background(), d.Get("instance_id").(string))
290336
if err != nil {
291337
diags = append(diags, diag.Diagnostic{
292338
Severity: diag.Error,
@@ -370,7 +416,7 @@ func getVMClient(subscriptionID string) (compute.VirtualMachinesClient, error) {
370416
func getRegion(d *schema.ResourceData) string {
371417
instanceRegions := make(map[string]string)
372418
instanceRegions["us-east"] = "eastus"
373-
instanceRegions["us-west"] = "westus"
419+
instanceRegions["us-west"] = "westus2"
374420
instanceRegions["eu-north"] = "northeurope"
375421
instanceRegions["eu-west"] = "westeurope"
376422

@@ -390,12 +436,13 @@ func getInstanceType(d *schema.ResourceData) string {
390436
instanceTypes["mk80"] = "Standard_NC6"
391437
instanceTypes["lk80"] = "Standard_NC12"
392438
instanceTypes["xlk80"] = "Standard_NC24"
393-
instanceTypes["mtesla"] = "Standard_NC6s_v2"
394-
instanceTypes["ltesla"] = "Standard_NC12s_v2"
395-
instanceTypes["xltesla"] = "Standard_NC24s_v2"
439+
instanceTypes["mtesla"] = "Standard_NC6s_v3"
440+
instanceTypes["ltesla"] = "Standard_NC12s_v3"
441+
instanceTypes["xltesla"] = "Standard_NC24s_v3"
396442

397443
instanceType := d.Get("instance_type").(string)
398-
if val, ok := instanceTypes[instanceType+d.Get("instance_gpu").(string)]; ok {
444+
instanceGPU := d.Get("instance_gpu").(string)
445+
if val, ok := instanceTypes[instanceType+instanceGPU]; ok {
399446
instanceType = val
400447
}
401448

iterative/resource_machine.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func resourceMachine() *schema.Resource {
3535
Type: schema.TypeString,
3636
Optional: true,
3737
ForceNew: true,
38-
Default: "iterative-cml",
38+
Default: "",
3939
},
4040
"instance_name": &schema.Schema{
4141
Type: schema.TypeString,
@@ -53,7 +53,7 @@ func resourceMachine() *schema.Resource {
5353
Type: schema.TypeInt,
5454
Optional: true,
5555
ForceNew: true,
56-
Default: 10,
56+
Default: 35,
5757
},
5858
"instance_gpu": &schema.Schema{
5959
Type: schema.TypeString,
@@ -105,10 +105,9 @@ func resourceMachine() *schema.Resource {
105105
}
106106
}
107107

108-
func resourceMachineCreate(ctx2 context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
108+
func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
109109
var diags diag.Diagnostics
110110

111-
ctx := context.Background()
112111
keyPublic := d.Get("key_public").(string)
113112
//keyPrivate := d.Get("key_private").(string)
114113

@@ -129,6 +128,7 @@ func resourceMachineCreate(ctx2 context.Context, d *schema.ResourceData, m inter
129128
}
130129

131130
driver := d.Get("driver").(string)
131+
132132
if driver == "aws" {
133133
return aws.ResourceMachineCreate(ctx, d, m)
134134
}

0 commit comments

Comments
 (0)