Skip to content

Commit 2f9e5ca

Browse files
authored
Merge pull request #1 from rhythmictech/init
init
2 parents 4efbb58 + 1906198 commit 2f9e5ca

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Rhythmic Technologies, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

cloudformation.yml.tpl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Resources:
2+
ImageBuildComponent:
3+
Type: AWS::ImageBuilder::Component
4+
Properties:
5+
Name: ${name}
6+
Version: ${version}
7+
%{~ if change_description != null ~}
8+
ChangeDescription: ${change_description}
9+
%{~ endif ~}
10+
%{~ if description != null ~}
11+
Description: ${description}
12+
%{~ endif ~}
13+
%{~ if kms_key_id != null ~}
14+
KmsKeyId: ${kms_key_id}
15+
%{~ endif ~}
16+
Platform: ${platform}
17+
Tags:
18+
${ indent(8, chomp(yamlencode(tags))) }
19+
%{~ if uri != null ~}
20+
Uri: ${uri}
21+
%{~ endif ~}
22+
%{~ if data != null ~}
23+
Data: |
24+
${indent(8, data)}
25+
%{~ endif ~}
26+
Outputs:
27+
ComponentArn:
28+
Description: ARN of the created component
29+
Value: !Ref "ImageBuildComponent"

component.yml.tpl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: ${name}-document
2+
%{ if description != null ~}
3+
description: ${description}
4+
%{ endif ~}
5+
schemaVersion: 1.0
6+
phases:
7+
- name: build
8+
steps:
9+
- name: ansible-install
10+
action: ExecuteBash
11+
inputs:
12+
commands:
13+
# Install Ansible dependencies
14+
- sudo yum install -y python python3 python-pip python3-pip git
15+
# Enable Ansible repository
16+
- sudo amazon-linux-extras enable ansible2
17+
# Install Ansible
18+
- sudo yum install -y ansible
19+
- name: get-playbook
20+
action: ExecuteBash
21+
inputs:
22+
commands:
23+
- git clone --depth 1 ${playbook_repo}
24+
- name: run-playbook
25+
action: ExecuteBash
26+
inputs:
27+
commands:
28+
%{~ if playbook_dir != null ~}
29+
- cd ${playbook_dir}
30+
%{~ endif ~}
31+
# Install playbook dependencies
32+
- ansible-galaxy install -f -r requirements.yml || true
33+
# Wait for cloud-init
34+
- while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done
35+
# Run playbook
36+
- ansible-playbook ${playbook_file}
37+
- name: cleanup
38+
action: ExecuteBash
39+
inputs:
40+
commands:
41+
- sudo yum remove -y ansible
42+
- sudo yum autoremove -y
43+
- sudo rm -rf packer-generic-images
44+
- sudo rm -rf ~/.ansible/roles /usr/share/anisble/roles /etc/ansible/roles

main.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
locals {
2+
data = templatefile("${path.module}/component.yml.tpl", {
3+
description = var.description
4+
name = var.name
5+
playbook_dir = var.playbook_dir
6+
playbook_file = var.playbook_file
7+
playbook_repo = var.playbook_repo
8+
})
9+
}
10+
11+
resource "aws_cloudformation_stack" "this" {
12+
name = var.name
13+
on_failure = "ROLLBACK"
14+
timeout_in_minutes = var.cloudformation_timeout
15+
16+
tags = merge(
17+
var.tags,
18+
{ Name : "${var.name}-stack" }
19+
)
20+
21+
template_body = templatefile("${path.module}/cloudformation.yml.tpl", {
22+
change_description = var.change_description
23+
data = local.data
24+
description = var.description
25+
kms_key_id = var.kms_key_id
26+
name = var.name
27+
platform = var.platform
28+
uri = var.data_uri
29+
version = var.component_version
30+
31+
tags = merge(
32+
var.tags,
33+
{ Name : var.name }
34+
)
35+
})
36+
}

outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "component_arn" {
2+
value = aws_cloudformation_stack.this.outputs["ComponentArn"]
3+
}

variables.tf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
variable "change_description" {
2+
default = null
3+
description = "description of changes since last version"
4+
type = string
5+
}
6+
7+
variable "cloudformation_timeout" {
8+
default = 10
9+
description = "How long to wait (in minutes) for CFN to apply before giving up"
10+
type = number
11+
}
12+
13+
variable "component_version" {
14+
description = "Version of the component"
15+
type = string
16+
}
17+
18+
variable "data_uri" {
19+
default = null
20+
description = "Use this to override the component document with one at a particualar URL endpoint"
21+
type = string
22+
}
23+
24+
variable "description" {
25+
default = null
26+
description = "description of component"
27+
type = string
28+
}
29+
30+
variable "kms_key_id" {
31+
default = null
32+
description = "KMS key to use for encryption"
33+
type = string
34+
}
35+
36+
variable "name" {
37+
description = "name to use for component"
38+
type = string
39+
}
40+
41+
# TODO: add validation
42+
variable "platform" {
43+
default = "Linux"
44+
description = "platform of component (Linux or Windows)"
45+
type = string
46+
}
47+
48+
variable "playbook_dir" {
49+
default = null
50+
description = "directory where playbook and requirements are found (if not root of repo)"
51+
type = string
52+
}
53+
54+
variable "playbook_file" {
55+
default = "provision.yml"
56+
description = "path to playbook file, relative to `playbook_dir`"
57+
}
58+
59+
variable "playbook_repo" {
60+
description = "git url for repo where ansible code lives"
61+
type = string
62+
}
63+
64+
variable "tags" {
65+
default = {}
66+
description = "map of tags to use for CFN stack and component"
67+
type = map(string)
68+
}

0 commit comments

Comments
 (0)