tfmod-generator 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/tfmod +10 -2
- data/lib/tfmod/generator/version.rb +1 -1
- data/templates/main.tf.erb +70 -0
- data/templates/user-data.sh.erb +128 -0
- data/tfmod-generator.gemspec +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ff260788fbe5e9f46965ee305d4b5bb5e49afea
|
4
|
+
data.tar.gz: f24f2c13161e1f266ae328a281d803de1449a22c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 138959c998eb3bbee70c93a7b45f9869c8ebd179f0d4200fbef7a4c5f9464ddb583acde060b222a4a684c6232ade2ed273dbe55bb6a95ac020888cc7a9b82e5d
|
7
|
+
data.tar.gz: 5e2b6cb06f14ad4a0e7e0c96b0ad7d515e413660f0ef66c614d36b07b34b7a8049e514a6247aab8f77b54f9bcd199a66cb86e715e51a6d24ab30fabe08572733
|
data/bin/tfmod
CHANGED
@@ -4,7 +4,7 @@ require 'thor'
|
|
4
4
|
require 'tfmod/generator'
|
5
5
|
|
6
6
|
# Main class
|
7
|
-
class
|
7
|
+
class TfModuleGenerator < Thor::Group
|
8
8
|
include Thor::Actions
|
9
9
|
|
10
10
|
argument :name
|
@@ -37,6 +37,14 @@ class TfModule < Thor::Group
|
|
37
37
|
template('../templates/variables.tf.erb', "#{name}/variables.tf")
|
38
38
|
end
|
39
39
|
|
40
|
+
def create_remote_scripts_dir
|
41
|
+
empty_directory("#{name}/remote_scripts")
|
42
|
+
end
|
43
|
+
|
44
|
+
def create_user_data_file
|
45
|
+
template('../templates/user-data.sh.erb', "#{name}/remote_scripts/user-data.sh.tpl")
|
46
|
+
end
|
47
|
+
|
40
48
|
def create_test_dir
|
41
49
|
empty_directory("#{name}/test")
|
42
50
|
end
|
@@ -46,4 +54,4 @@ class TfModule < Thor::Group
|
|
46
54
|
end
|
47
55
|
end
|
48
56
|
|
49
|
-
|
57
|
+
TfModuleGenerator.start
|
data/templates/main.tf.erb
CHANGED
@@ -1 +1,71 @@
|
|
1
1
|
// main tf file for <%= name.capitalize %>
|
2
|
+
|
3
|
+
// example ec2 resource that leverages Terraform template provider to update user-data
|
4
|
+
// some variables presented here are not in the variables.tf generated but are here for example usage
|
5
|
+
|
6
|
+
resource "aws_instance" "<%= name -%>" {
|
7
|
+
count = "${var.<%= name %>_count}"
|
8
|
+
ami = "${var.custom_ebs_ami != "" ? var.custom_ebs_ami : data.aws_ami.ubuntu.id}"
|
9
|
+
instance_type = "${var.<%= name %>_instance_type}"
|
10
|
+
iam_instance_profile = "${var.instance_profile}"
|
11
|
+
subnet_id = "${element(var.subnet_id, count.index)}"
|
12
|
+
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
|
13
|
+
key_name = "${var.key_name}"
|
14
|
+
user_data = "${element(data.template_cloudinit_config.userdata.*.rendered, count.index)}"
|
15
|
+
|
16
|
+
root_block_device {
|
17
|
+
volume_type = "${var.root_block_device_volume_type}"
|
18
|
+
volume_size = "${var.root_block_device_volume_size}"
|
19
|
+
delete_on_termination = "${var.root_block_device_delete_on_termination}"
|
20
|
+
}
|
21
|
+
|
22
|
+
lifecycle {
|
23
|
+
ignore_changes = ["ami", "user_data"]
|
24
|
+
}
|
25
|
+
|
26
|
+
tags {
|
27
|
+
Name = "<%= name %>${count.index + 1}-${var.team}-${var.product}-${var.environment}"
|
28
|
+
Environment = "${var.environment}"
|
29
|
+
Team = "${var.team}"
|
30
|
+
Service = "${var.service}"
|
31
|
+
Product = "${var.product}"
|
32
|
+
Owner = "${var.owner}"
|
33
|
+
Description = "${var.description}"
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
data "template_file" "script" {
|
38
|
+
count = "${var.<%= name %>_count}"
|
39
|
+
template = "${file("${path.module}/remote_scripts/user-data.sh.tpl")}"
|
40
|
+
|
41
|
+
vars {
|
42
|
+
environment = "${var.environment}"
|
43
|
+
team = "${var.team}"
|
44
|
+
domain = "${var.domain}"
|
45
|
+
service = "${var.service}"
|
46
|
+
product = "${var.product}"
|
47
|
+
<%= name %>_count = "${count.index + 1}"
|
48
|
+
consul_server_tag_key = "${var.consul_server_tag_key}"
|
49
|
+
consul_server_tag_value = "${var.consul_server_tag_value}"
|
50
|
+
consul_datacenter = "${var.consul_datacenter}"
|
51
|
+
consul_agent_version = "${var.consul_agent_version}"
|
52
|
+
consul_template_version = "${var.consul_template_version}"
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
data "template_cloudinit_config" "userdata" {
|
57
|
+
count = "${var.<%= name %>_count}"
|
58
|
+
gzip = true
|
59
|
+
base64_encode = true
|
60
|
+
|
61
|
+
part {
|
62
|
+
content_type = "text/x-shellscript"
|
63
|
+
content = "${element(data.template_file.script.*.rendered, count.index)}"
|
64
|
+
}
|
65
|
+
|
66
|
+
// optional script you can pass in as a variable to append to managed user-data template
|
67
|
+
part {
|
68
|
+
content_type = "${var.post_script_type}"
|
69
|
+
content = "${var.post_script}"
|
70
|
+
}
|
71
|
+
}
|
@@ -0,0 +1,128 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
# Example user-data bootstrap
|
4
|
+
|
5
|
+
# User data script bootstrap takes variables from Terraform template provider
|
6
|
+
# Requires some parameters to be passed in via Terraform template variables.
|
7
|
+
# Requires a working local consul agent that can connect to a consul cluster to join via Ec2 tags
|
8
|
+
|
9
|
+
# userdata template variables passed in via Terraform
|
10
|
+
# Environment: ${environment}
|
11
|
+
# Domain: ${domain}
|
12
|
+
# Team: ${team}
|
13
|
+
# Service: ${service}
|
14
|
+
# Product: ${product}
|
15
|
+
# Count: ${<%= name %>_count}
|
16
|
+
# Consul template version: ${consul_template_version}
|
17
|
+
# Consul cluster AWS tag key: ${consul_server_tag_key}
|
18
|
+
# Consul cluster AWS tag value: ${consul_server_tag_value}
|
19
|
+
# Consul agent version: ${consul_agent_version}
|
20
|
+
|
21
|
+
export IP_ADDRESS=$(curl http://169.254.169.254/latest/meta-data/local-ipv4)
|
22
|
+
export REGION=$(curl -s 169.254.169.254/latest/meta-data/placement/availability-zone)
|
23
|
+
export AWS_DEFAULT_REGION=$\{REGION::-1\}
|
24
|
+
|
25
|
+
function set_hostname {
|
26
|
+
echo "SETTING HOSTNAME ${service}${<%= name %>_count}-${team}-${product}-${environment}.${domain}"
|
27
|
+
hostname ${service}${<%= name %>_count}-${team}-${product}-${environment}.${domain}
|
28
|
+
echo "Updating /etc/hostname"
|
29
|
+
echo "${service}${<%= name %>_count}-${team}-${product}-${environment}.${domain}" > /etc/hostname
|
30
|
+
echo "Updating /etc/hosts"
|
31
|
+
echo "127.0.0.1 ${service}${<%= name %>_count}-${team}-${product}-${environment}.${domain}" >> /etc/hosts
|
32
|
+
}
|
33
|
+
|
34
|
+
function install_awscli {
|
35
|
+
apt-get update
|
36
|
+
apt-get install -qq python-pip
|
37
|
+
pip install awscli
|
38
|
+
}
|
39
|
+
|
40
|
+
function install_ssm_agent {
|
41
|
+
curl https://amazon-ssm-us-west-2.s3.amazonaws.com/latest/debian_amd64/amazon-ssm-agent.deb \
|
42
|
+
-o amazon-ssm-agent.deb
|
43
|
+
dpkg -i amazon-ssm-agent.deb
|
44
|
+
systemctl start amazon-ssm-agent
|
45
|
+
}
|
46
|
+
|
47
|
+
function install_consul {
|
48
|
+
# Install consul agent function
|
49
|
+
apt-get update
|
50
|
+
apt-get install -qq curl unzip
|
51
|
+
mkdir -p /var/lib/consul
|
52
|
+
wget https://releases.hashicorp.com/consul/${consul_agent_version}/consul_${consul_agent_version}_linux_amd64.zip
|
53
|
+
unzip consul_${consul_agent_version}_linux_amd64.zip -d /usr/local/bin
|
54
|
+
rm consul_${consul_agent_version}_linux_amd64.zip
|
55
|
+
}
|
56
|
+
|
57
|
+
function install_consul_template {
|
58
|
+
# Install consul template
|
59
|
+
apt-get update
|
60
|
+
apt-get install -qq curl unzip
|
61
|
+
wget https://releases.hashicorp.com/consul-template/${consul_template_version}/consul-template_${consul_template_version}_linux_amd64.zip
|
62
|
+
unzip consul-template_${consul_template_version}_linux_amd64.zip -d /usr/local/bin
|
63
|
+
rm consul-template_${consul_template_version}_linux_amd64.zip
|
64
|
+
}
|
65
|
+
|
66
|
+
function prep_consul_agent_service {
|
67
|
+
#Setup consul service
|
68
|
+
cat > consul.service <<'CONSUL_AGENT_SYSTEMD'
|
69
|
+
[Unit]
|
70
|
+
Description=consul
|
71
|
+
Documentation=https://consul.io/docs/
|
72
|
+
|
73
|
+
[Service]
|
74
|
+
ExecStart=/usr/local/bin/consul agent \
|
75
|
+
-advertise=ADVERTISE_ADDR \
|
76
|
+
-bind=ADVERTISE_ADDR \
|
77
|
+
-client=ADVERTISE_ADDR \
|
78
|
+
-data-dir=/var/lib/consul \
|
79
|
+
-config-dir=/etc/consul.d \
|
80
|
+
-retry-join-ec2-tag-key=${consul_server_tag_key} \
|
81
|
+
-retry-join-ec2-tag-value=${consul_server_tag_value} \
|
82
|
+
-datacenter=${consul_datacenter}
|
83
|
+
|
84
|
+
ExecReload=/bin/kill -HUP $MAINPID
|
85
|
+
LimitNOFILE=65536
|
86
|
+
Restart=always
|
87
|
+
RestartSec=5
|
88
|
+
|
89
|
+
[Install]
|
90
|
+
WantedBy=multi-user.target
|
91
|
+
CONSUL_AGENT_SYSTEMD
|
92
|
+
sed -i "s/ADVERTISE_ADDR/$\{IP_ADDRESS\}/" consul.service
|
93
|
+
mv consul.service /etc/systemd/system/consul.service
|
94
|
+
mkdir /etc/consul.d
|
95
|
+
}
|
96
|
+
|
97
|
+
function prep_dnsmasq {
|
98
|
+
mkdir -p /etc/dnsmasq.d
|
99
|
+
cat > /etc/dnsmasq.d/10-consul <<'DNSMASQ_CONFIG'
|
100
|
+
server=/consul/127.0.0.1#8600
|
101
|
+
DNSMASQ_CONFIG
|
102
|
+
}
|
103
|
+
|
104
|
+
function enable_services {
|
105
|
+
/bin/systemctl enable consul
|
106
|
+
/bin/systemctl enable dnsmasq
|
107
|
+
}
|
108
|
+
|
109
|
+
function start_services {
|
110
|
+
/bin/systemctl start consul
|
111
|
+
/bin/systemctl start dnsmasq
|
112
|
+
}
|
113
|
+
|
114
|
+
function main {
|
115
|
+
set_hostname
|
116
|
+
install_awscli
|
117
|
+
install_ssm_agent
|
118
|
+
install_consul
|
119
|
+
install_consul_template
|
120
|
+
|
121
|
+
prep_consul_agent_service
|
122
|
+
prep_dnsmasq
|
123
|
+
enable_services
|
124
|
+
start_services
|
125
|
+
}
|
126
|
+
|
127
|
+
#Entry point
|
128
|
+
main
|
data/tfmod-generator.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
f.match(%r{^(test|spec|features)/})
|
24
24
|
end
|
25
25
|
spec.bindir = "bin"
|
26
|
-
spec.executables =
|
26
|
+
spec.executables = "tfmod"
|
27
27
|
spec.require_paths = ["lib"]
|
28
28
|
|
29
29
|
spec.add_development_dependency "bundler", "~> 1.13"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tfmod-generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,8 +84,6 @@ description: A Terraform module generator from command line.
|
|
84
84
|
email:
|
85
85
|
- zane.williamson@gmail.com
|
86
86
|
executables:
|
87
|
-
- console
|
88
|
-
- setup
|
89
87
|
- tfmod
|
90
88
|
extensions: []
|
91
89
|
extra_rdoc_files: []
|
@@ -107,6 +105,7 @@ files:
|
|
107
105
|
- templates/gitignore.erb
|
108
106
|
- templates/main.tf.erb
|
109
107
|
- templates/outputs.tf.erb
|
108
|
+
- templates/user-data.sh.erb
|
110
109
|
- templates/variables.tf.erb
|
111
110
|
- tfmod-generator.gemspec
|
112
111
|
homepage: https://github.com/sepulworld/tfmod-generator
|