Skip to content

Commit a01c40a

Browse files
authored
AWS prerelease (#4)
* releaser * no error keypar * no keypar delete * no keypar delete * packer * endline * credentials * fix typo * sudo * no packer * dollar sign * public and no vpc * image * local provider and reset machine * SubnetId and packer regions * v0.3.3 * clone amis
1 parent be13cf7 commit a01c40a

File tree

7 files changed

+209
-41
lines changed

7 files changed

+209
-41
lines changed

.github/workflows/cml-ami.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: cml-ami
2+
on:
3+
push:
4+
tags:
5+
- 'ami*'
6+
7+
jobs:
8+
build-ami:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
14+
- name: Packer
15+
env:
16+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
17+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
18+
AWS_INSTANCE_TYPE: g2.2xlarge
19+
run: |
20+
sudo apt install unzip
21+
22+
cd packer
23+
packer validate ami.json
24+
packer build ami.json
25+
./clone-amis.go

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
TEST?=$$(go list ./... | grep -v 'vendor')
22
HOSTNAME=github.com
3-
NAMESPACE=iterative
3+
NAMESPACE=DavidGOrtega
44
NAME=iterative
5-
VERSION=0.1
6-
OS_ARCH=linux_amd64
7-
#OS_ARCH=darwin_amd64
5+
VERSION=0.3
6+
#OS_ARCH=linux_amd64
7+
OS_ARCH=darwin_amd64
88
BINARY=terraform-provider-${NAME}
9+
VERSION2=0.3.3
910

1011
default: install
1112

examples/main.tf

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
1+
12
terraform {
23
required_providers {
34
iterative = {
4-
versions = ["0.1"]
5-
source = "github.com/iterative/iterative"
5+
source = "DavidGOrtega/iterative"
6+
version = "0.4.0"
67
}
78
}
89
}
910

1011
provider "iterative" {}
1112

1213
resource "iterative_machine" "machine" {
13-
region = "us-west-1"
14-
aws_security_group = "default"
15-
key_public = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCcztk+/ibMWJH7LTjcw5KhlDKW1y/gJVB3ivk3D0YMa84ylL5pc8/3zd4PkAvX4eJX0Yi/dE9r4dY+8+ws/1pIuAO9y9Fu3ev+Cj8CKbFbgxaLDlaWHCV/y295rhmgfArtTB4KCbQ2EihXgodzddA9FGiyPoeYWilUUNDsi9wBTsouGahaFpiDVeAkexkUgGtGUWTW7OcTgvagGmKoNogWEpo9VBU3gGEoWj/I1TecQmOs09NFMyj1DdtRfsKYhQUfYz1W38ht0zCuPHKOnVGLDK4vd3nI2KzKJu0/CcFbjlJNqPrHHooIGJtmQRQIsSyI6hCUPK3ZCI7o+viaGqP+Awbo7XSKyexqd81bhgha98jqy6304jOG5qSUewgeK7VNq2FEXQ0D7ox0Yci/TgM7w+XVpjOf6XEUjkUyoLoL1xkxcINdZozWzeXK/dykvfXo+nwALT4UhjMx7fk46e2lRyExBuD4L0ah8rDT1ZUORsDkEVvmCx/tJqO1drrUPLT846Cb0E6oebcYCUCN9r8qo2BeipG44VkX0jL9BLB2IZeP5BpXFT+bo3zjXqEtX3l/5iJ42jOJodmw70gaf/7c2NWumydR2STuDQLUSvJC2Xtka5M/CHfNX3ShssrJaR/oKacU8F5DaPqTH9RSJ3oSs8Kr247E20i1BzDTwrWicQ== g.ortega.david@gmail.com"
16-
}
17-
/*
18-
resource "iterative_machine" "machine2" {
19-
region = "us-west-1"
20-
aws_security_group = "default"
21-
} */
22-
23-
24-
# resource "iterative_machine" "machine3" {
25-
# region = "us-west-1"
26-
# }
27-
28-
# resource "iterative_machine" "machine4" {
29-
# region = "us-west-2"
30-
# instance_hdd_size = 110
31-
# }
32-
33-
/* resource "iterative_machine" "machine5" {
34-
region = "us-west-2"
35-
} */
36-
14+
region = "us-east-1"
15+
16+
17+
18+
}

iterative/resource_machine.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package iterative
22

33
import (
44
"context"
5+
"fmt"
6+
"log"
57
"sort"
68
"time"
79

@@ -36,7 +38,7 @@ func resourceMachine() *schema.Resource {
3638
Type: schema.TypeInt,
3739
Optional: true,
3840
ForceNew: true,
39-
Default: 100,
41+
Default: 10,
4042
},
4143
"instance_id": &schema.Schema{
4244
Type: schema.TypeString,
@@ -94,7 +96,7 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
9496
Filters: []*ec2.Filter{
9597
{
9698
Name: aws.String("name"),
97-
Values: []*string{aws.String("Deep*Ubuntu 18.04*")},
99+
Values: []*string{aws.String("iterative-cml")},
98100
},
99101
{
100102
Name: aws.String("architecture"),
@@ -130,6 +132,7 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
130132

131133
pairName := "cml_" + id
132134
var keyMaterial string
135+
var vpcID string
133136

134137
// key-pair
135138
if len(keyPublic) != 0 {
@@ -156,7 +159,7 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
156159

157160
vpcsDesc, _ := svc.DescribeVpcs(&ec2.DescribeVpcsInput{})
158161
vpc := vpcsDesc.Vpcs[0]
159-
vpcID := *vpc.VpcId
162+
vpcID = *vpc.VpcId
160163

161164
gpResult, ee := svc.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
162165
GroupName: aws.String(securityGroup),
@@ -193,15 +196,42 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
193196
}
194197
}
195198

199+
sgDesc, sgDescErr := svc.DescribeSecurityGroupsWithContext(ctx, &ec2.DescribeSecurityGroupsInput{
200+
Filters: []*ec2.Filter{
201+
{
202+
Name: aws.String("group-name"),
203+
Values: []*string{aws.String(securityGroup)},
204+
},
205+
},
206+
})
207+
208+
if sgDescErr != nil {
209+
return diag.FromErr(sgDescErr)
210+
}
211+
212+
sgID := *sgDesc.SecurityGroups[0].GroupId
213+
214+
subDesc, _ := svc.DescribeSubnetsWithContext(ctx, &ec2.DescribeSubnetsInput{
215+
Filters: []*ec2.Filter{
216+
{
217+
Name: aws.String("vpc-id"),
218+
Values: []*string{aws.String(vpcID)},
219+
},
220+
},
221+
})
222+
223+
log.Printf("[ERROR] %s %s %s", instanceAmi, instanceType, sgID)
196224
runResult, err := svc.RunInstancesWithContext(ctx, &ec2.RunInstancesInput{
197225
ImageId: aws.String(instanceAmi),
198226
KeyName: aws.String(pairName),
199227
InstanceType: aws.String(instanceType),
200228
MinCount: aws.Int64(1),
201229
MaxCount: aws.Int64(1),
202-
SecurityGroups: []*string{
203-
aws.String(securityGroup),
204-
},
230+
//SecurityGroups: []*string{
231+
//aws.String(securityGroup),
232+
//},
233+
SubnetId: aws.String(*subDesc.Subnets[0].SubnetId),
234+
SecurityGroupIds: []*string{aws.String(sgID)},
205235
BlockDeviceMappings: []*ec2.BlockDeviceMapping{
206236
{
207237
//VirtualName: aws.String("Root"),
@@ -217,7 +247,17 @@ func resourceMachineCreate(ctx context.Context, d *schema.ResourceData, m interf
217247
},
218248
})
219249
if err != nil {
220-
return diag.FromErr(err)
250+
diags = append(diags, diag.Diagnostic{
251+
Severity: diag.Error,
252+
Summary: "Unable to create HashiCups client",
253+
Detail: fmt.Sprintf("[ERROR] %s %s %s", instanceAmi, instanceType, sgID),
254+
})
255+
256+
diags = append(diags, diag.FromErr(err)[0])
257+
258+
//return diag.FromErr(err)
259+
260+
return diags
221261
}
222262

223263
// Add tags to the created instance
@@ -279,12 +319,9 @@ func resourceMachineDelete(ctx context.Context, d *schema.ResourceData, m interf
279319
pairName := d.Get("key_name").(string)
280320
instanceID := d.Get("instance_id").(string)
281321

282-
_, erro := svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
322+
svc.DeleteKeyPair(&ec2.DeleteKeyPairInput{
283323
KeyName: aws.String(pairName),
284324
})
285-
if erro != nil {
286-
diag.FromErr(erro)
287-
}
288325

289326
input := &ec2.TerminateInstancesInput{
290327
InstanceIds: []*string{

packer/ami.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"variables" : {
3+
"instance_type" : "{{env `AWS_INSTANCE_TYPE`}}"
4+
},
5+
"builders" : [
6+
{
7+
"ami_name" : "iterative-cml",
8+
"ami_description" : "CML (Continous Machine Learning)",
9+
"ami_groups": ["all"],
10+
"force_deregister": "true",
11+
"force_delete_snapshot": "true",
12+
"region" : "us-west-1",
13+
"ssh_username" : "ubuntu",
14+
"type" : "amazon-ebs",
15+
"instance_type" : "{{user `instance_type`}}",
16+
"source_ami_filter": {
17+
"filters": {
18+
"virtualization-type": "hvm",
19+
"name": "ubuntu/images/*ubuntu-*-18.04-amd64-server-*",
20+
"root-device-type": "ebs"
21+
},
22+
"owners": ["099720109477"],
23+
"most_recent": true
24+
},
25+
"run_tags" : {
26+
"Author" : "iterative"
27+
}
28+
}
29+
],
30+
"provisioners" : [
31+
{
32+
"type" : "shell",
33+
"environment_vars": ["DEBIAN_FRONTEND=noninteractive"],
34+
"script" : "./setup.sh"
35+
},
36+
{
37+
"type": "shell",
38+
"inline": [
39+
"sudo shutdown -r now",
40+
"sleep 60"
41+
],
42+
"start_retry_timeout": "10m",
43+
"expect_disconnect": true
44+
}
45+
]
46+
}

packer/clone-amis.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//usr/bin/env go run $0 "$@"; exit
2+
package main
3+
4+
import (
5+
"fmt"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/session"
9+
"github.com/aws/aws-sdk-go/service/ec2"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
)
12+
13+
func main() {
14+
region := "us-west-1"
15+
amiName := "iterative-cml"
16+
regions := []string{"us-east-1", "eu-central-1", "eu-west-1"}
17+
18+
sess, _ := session.NewSession(&aws.Config{
19+
Region: aws.String(region)},
20+
)
21+
svc := ec2.New(sess)
22+
23+
amiParams := &ec2.DescribeImagesInput{
24+
Filters: []*ec2.Filter{
25+
{
26+
Name: aws.String("name"),
27+
Values: []*string{aws.String(amiName)},
28+
},
29+
{
30+
Name: aws.String("architecture"),
31+
Values: []*string{aws.String("x86_64")},
32+
},
33+
},
34+
}
35+
imagesRes, imagesErr := svc.DescribeImages(amiParams)
36+
if imagesErr != nil {
37+
diag.FromErr(imagesErr)
38+
}
39+
40+
ami := imagesRes.Images[0]
41+
amiID := *ami.ImageId
42+
amiDesc := *ami.Description
43+
44+
for _, value := range regions {
45+
fmt.Println("Cloning", value)
46+
47+
sess, _ := session.NewSession(&aws.Config{
48+
Region: aws.String(value)},
49+
)
50+
51+
svc := ec2.New(sess)
52+
53+
_, err := svc.CopyImage(&ec2.CopyImageInput{
54+
SourceImageId: aws.String(amiID),
55+
SourceRegion: aws.String(region),
56+
Name: aws.String(amiName),
57+
Description: aws.String(amiDesc),
58+
})
59+
60+
if err != nil {
61+
fmt.Println(err)
62+
}
63+
}
64+
}

packer/setup.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#/bin/sh
2+
3+
echo "APT::Get::Assume-Yes \"true\";" | sudo tee -a /etc/apt/apt.conf.d/90assumeyes
4+
5+
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh && \
6+
sudo usermod -aG docker \${USER}
7+
sudo setfacl --modify user:\${USER}:rw /var/run/docker.sock
8+
9+
curl -s -L https://nvidia.GitHub.io/nvidia-docker/gpgkey | sudo apt-key add - && \
10+
curl -s -L https://nvidia.GitHub.io/nvidia-docker/ubuntu18.04/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list && \
11+
sudo apt update && sudo apt install -y ubuntu-drivers-common && \
12+
sudo ubuntu-drivers autoinstall && \
13+
sudo apt install -y nvidia-container-toolkit

0 commit comments

Comments
 (0)