yle_tf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.rubocop.yml +26 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +442 -0
- data/Rakefile +6 -0
- data/bin/tf +7 -0
- data/examples/envs/prod.tfvars +5 -0
- data/examples/envs/test.tfvars +2 -0
- data/examples/main.tf +10 -0
- data/examples/tf.yaml +4 -0
- data/examples/tf_hooks/pre/get_current_hash.sh +26 -0
- data/examples/variables.tf +22 -0
- data/lib/yle_tf.rb +70 -0
- data/lib/yle_tf/action.rb +29 -0
- data/lib/yle_tf/action/builder.rb +9 -0
- data/lib/yle_tf/action/command.rb +25 -0
- data/lib/yle_tf/action/copy_root_module.rb +22 -0
- data/lib/yle_tf/action/generate_vars_file.rb +27 -0
- data/lib/yle_tf/action/load_config.rb +17 -0
- data/lib/yle_tf/action/terraform_init.rb +63 -0
- data/lib/yle_tf/action/tf_hooks.rb +48 -0
- data/lib/yle_tf/action/tmpdir.rb +35 -0
- data/lib/yle_tf/action/verify_terraform_version.rb +41 -0
- data/lib/yle_tf/action/verify_tf_env.rb +24 -0
- data/lib/yle_tf/backend_config.rb +41 -0
- data/lib/yle_tf/cli.rb +88 -0
- data/lib/yle_tf/config.rb +45 -0
- data/lib/yle_tf/config/defaults.rb +35 -0
- data/lib/yle_tf/config/erb.rb +22 -0
- data/lib/yle_tf/config/file.rb +26 -0
- data/lib/yle_tf/config/loader.rb +108 -0
- data/lib/yle_tf/error.rb +4 -0
- data/lib/yle_tf/logger.rb +48 -0
- data/lib/yle_tf/plugin.rb +55 -0
- data/lib/yle_tf/plugin/action_hook.rb +23 -0
- data/lib/yle_tf/plugin/loader.rb +59 -0
- data/lib/yle_tf/plugin/manager.rb +49 -0
- data/lib/yle_tf/system.rb +22 -0
- data/lib/yle_tf/tf_hook.rb +90 -0
- data/lib/yle_tf/tf_hook/runner.rb +48 -0
- data/lib/yle_tf/vars_file.rb +42 -0
- data/lib/yle_tf/version.rb +3 -0
- data/lib/yle_tf/version_requirement.rb +25 -0
- data/lib/yle_tf_plugins/backends/file/command.rb +31 -0
- data/lib/yle_tf_plugins/backends/file/config.rb +17 -0
- data/lib/yle_tf_plugins/backends/file/plugin.rb +16 -0
- data/lib/yle_tf_plugins/backends/s3/command.rb +19 -0
- data/lib/yle_tf_plugins/backends/s3/plugin.rb +16 -0
- data/lib/yle_tf_plugins/commands/__default/command.rb +14 -0
- data/lib/yle_tf_plugins/commands/__default/plugin.rb +14 -0
- data/lib/yle_tf_plugins/commands/_config/command.rb +11 -0
- data/lib/yle_tf_plugins/commands/_config/plugin.rb +19 -0
- data/lib/yle_tf_plugins/commands/_shell/command.rb +15 -0
- data/lib/yle_tf_plugins/commands/_shell/plugin.rb +14 -0
- data/lib/yle_tf_plugins/commands/help/command.rb +55 -0
- data/lib/yle_tf_plugins/commands/help/plugin.rb +18 -0
- data/lib/yle_tf_plugins/commands/version/command.rb +20 -0
- data/lib/yle_tf_plugins/commands/version/plugin.rb +18 -0
- data/vendor/hash_deep_merge.rb +59 -0
- data/vendor/logger_level_patch.rb +29 -0
- data/vendor/middleware/LICENSE +23 -0
- data/vendor/middleware/builder.rb +149 -0
- data/vendor/middleware/runner.rb +69 -0
- data/yle_tf.gemspec +37 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b756a2e77feec940d0faac15ac402b1b624e0cbc
|
4
|
+
data.tar.gz: 6202a1cbe6e3aba3337b908bbb0c72cbd56eaef2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d417c9827c142b9d38d08e82f2574771edd50f664c4a4d4e8a5dec0edf7c3d9facca02efad74a56a2813e1b168b71c202743f961b13d27c42b3807f5e10ddc2
|
7
|
+
data.tar.gz: '09842437220a7b72909c19b951219b798b30def5c41fa59e06502ccac760740f75f9e90a0c03951babd5db7b2c9598fa14fa00a94672e7877bf716c2c08657b2'
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Metrics/BlockLength:
|
2
|
+
Exclude:
|
3
|
+
- 'spec/**/*'
|
4
|
+
|
5
|
+
Metrics/ClassLength:
|
6
|
+
Severity: warning
|
7
|
+
|
8
|
+
Metrics/LineLength:
|
9
|
+
Max: 100
|
10
|
+
Severity: warning
|
11
|
+
|
12
|
+
Metrics/MethodLength:
|
13
|
+
Max: 12
|
14
|
+
Severity: warning
|
15
|
+
|
16
|
+
Style/Documentation:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Style/GuardClause:
|
20
|
+
MinBodyLength: 3
|
21
|
+
|
22
|
+
Style/NegatedIf:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/TrailingCommaInLiteral:
|
26
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at teemu.matilainen@reaktor.fi. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016-2017 Yleisradio Oy
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,442 @@
|
|
1
|
+
# YleTf
|
2
|
+
|
3
|
+
A wrapper for [Terraform](https://www.terraform.io/) with support for hooks and environments.
|
4
|
+
|
5
|
+
Offers a command-line helper tool, `tf`
|
6
|
+
|
7
|
+
## Getting started
|
8
|
+
|
9
|
+
YleTf requires Terraform which can be installed according to their [instructions](https://www.terraform.io/intro/getting-started/install.html). On MacOS (and OSX) you can use [Homebrew](https://brew.sh/). You can also easily install and manage multiple versions of Terraform with [homebrew-terraforms](https://github.com/Yleisradio/homebrew-terraforms).
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
YleTf can be installed as a gem:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
$ gem install yle_tf
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The syntax to run YleTf is much like vanilla [Terraform](https://terraform.io). The main difference is that you must include desired environment as the first argument:
|
22
|
+
|
23
|
+
```
|
24
|
+
tf <environment> <command> [<args>]
|
25
|
+
```
|
26
|
+
|
27
|
+
For example:
|
28
|
+
|
29
|
+
```
|
30
|
+
$ tf test plan
|
31
|
+
|
32
|
+
$ tf prod apply
|
33
|
+
|
34
|
+
$ tf stage destroy
|
35
|
+
```
|
36
|
+
|
37
|
+
For a full list of available options, run `tf --help`.
|
38
|
+
|
39
|
+
When `tf` is executed, it creates a temporary directory where your project is copied and initialized.
|
40
|
+
|
41
|
+
### Example project
|
42
|
+
|
43
|
+
The following is a really basic example on what you need in order to use YleTf. The project is pretty much the same as the [example project](https://www.terraform.io/intro/getting-started/build.html) in Terraform documentation but with the added support for environments. Introduction to [hooks](#hooks) is in it's own section.
|
44
|
+
|
45
|
+
#### Project Config
|
46
|
+
|
47
|
+
The root of your project directory will look like this:
|
48
|
+
|
49
|
+
```
|
50
|
+
.
|
51
|
+
├── envs
|
52
|
+
│ ├── prod.tfvars
|
53
|
+
│ └── test.tfvars
|
54
|
+
├── main.tf
|
55
|
+
├── tf.yaml
|
56
|
+
└── variables.tf
|
57
|
+
```
|
58
|
+
|
59
|
+
And here are the contents of the files:
|
60
|
+
|
61
|
+
##### main.tf
|
62
|
+
|
63
|
+
```hcl
|
64
|
+
provider "aws" {
|
65
|
+
access_key = "${var.aws_access_key}"
|
66
|
+
secret_key = "${var.aws_secret_key}"
|
67
|
+
region = "${var.region}"
|
68
|
+
}
|
69
|
+
|
70
|
+
resource "aws_instance" "example" {
|
71
|
+
ami = "ami-2757f631"
|
72
|
+
instance_type = "t2.micro"
|
73
|
+
}
|
74
|
+
```
|
75
|
+
|
76
|
+
_Please note that the AMI identifier changes based on your desired OS and region. For more information see [AWS documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html)._
|
77
|
+
|
78
|
+
##### variables.tf
|
79
|
+
|
80
|
+
```hcl
|
81
|
+
variable "region" {
|
82
|
+
description = "The AWS region for the resources"
|
83
|
+
default = "eu-west-1"
|
84
|
+
}
|
85
|
+
|
86
|
+
variable "env" {
|
87
|
+
# passed by `tf`
|
88
|
+
description = "The environment"
|
89
|
+
}
|
90
|
+
|
91
|
+
variable "aws_access_key" {
|
92
|
+
description = "Your AWS access key id"
|
93
|
+
}
|
94
|
+
|
95
|
+
variable "aws_secret_key" {
|
96
|
+
description = "Your AWS secret key"
|
97
|
+
}
|
98
|
+
|
99
|
+
variable "instance_type" {
|
100
|
+
description = "Instance type to use"
|
101
|
+
default = "t2.micro"
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
##### envs/test.tfvars
|
106
|
+
|
107
|
+
```ini
|
108
|
+
aws_access_key = "TEST_ACCOUNT_ACCESS_KEY_HERE"
|
109
|
+
aws_secret_key = "TEST_ACCOUNT_SECRET_KEY_HERE"
|
110
|
+
```
|
111
|
+
|
112
|
+
##### tf.yaml
|
113
|
+
|
114
|
+
```yaml
|
115
|
+
backend:
|
116
|
+
type: file
|
117
|
+
terraform:
|
118
|
+
version_requirement: "~> 0.9.11"
|
119
|
+
```
|
120
|
+
|
121
|
+
#### Execution
|
122
|
+
|
123
|
+
With all the above in order, there's nothing more to do than to try and run the commands:
|
124
|
+
|
125
|
+
First plan:
|
126
|
+
|
127
|
+
```
|
128
|
+
$ tf test plan
|
129
|
+
INFO: Symlinking state to '/usr/local/src/yle_tf/examples/examples_test.tfstate'
|
130
|
+
|
131
|
+
Terraform has been successfully initialized!
|
132
|
+
|
133
|
+
You may now begin working with Terraform. Try running "terraform plan" to see
|
134
|
+
any changes that are required for your infrastructure. All Terraform commands
|
135
|
+
should now work.
|
136
|
+
|
137
|
+
If you ever set or change modules or backend configuration for Terraform,
|
138
|
+
rerun this command to reinitialize your environment. If you forget, other
|
139
|
+
commands will detect it and remind you to do so if necessary.
|
140
|
+
Refreshing Terraform state in-memory prior to plan...
|
141
|
+
The refreshed state will be used to calculate this plan, but will not be
|
142
|
+
persisted to local or remote state storage.
|
143
|
+
|
144
|
+
The Terraform execution plan has been generated and is shown below.
|
145
|
+
Resources are shown in alphabetical order for quick scanning. Green resources
|
146
|
+
will be created (or destroyed and then created if an existing resource
|
147
|
+
exists), yellow resources are being changed in-place, and red resources
|
148
|
+
will be destroyed. Cyan entries are data sources to be read.
|
149
|
+
|
150
|
+
Note: You didn't specify an "-out" parameter to save this plan, so when
|
151
|
+
"apply" is called, Terraform can't guarantee this is what will execute.
|
152
|
+
|
153
|
+
+ aws_instance.example
|
154
|
+
ami: "ami-d7b9a2b1"
|
155
|
+
associate_public_ip_address: "<computed>"
|
156
|
+
availability_zone: "<computed>"
|
157
|
+
ebs_block_device.#: "<computed>"
|
158
|
+
ephemeral_block_device.#: "<computed>"
|
159
|
+
instance_state: "<computed>"
|
160
|
+
instance_type: "t2.micro"
|
161
|
+
ipv6_address_count: "<computed>"
|
162
|
+
ipv6_addresses.#: "<computed>"
|
163
|
+
key_name: "<computed>"
|
164
|
+
network_interface.#: "<computed>"
|
165
|
+
network_interface_id: "<computed>"
|
166
|
+
placement_group: "<computed>"
|
167
|
+
primary_network_interface_id: "<computed>"
|
168
|
+
private_dns: "<computed>"
|
169
|
+
private_ip: "<computed>"
|
170
|
+
public_dns: "<computed>"
|
171
|
+
public_ip: "<computed>"
|
172
|
+
root_block_device.#: "<computed>"
|
173
|
+
security_groups.#: "<computed>"
|
174
|
+
source_dest_check: "true"
|
175
|
+
subnet_id: "<computed>"
|
176
|
+
tenancy: "<computed>"
|
177
|
+
volume_tags.%: "<computed>"
|
178
|
+
vpc_security_group_ids.#: "<computed>"
|
179
|
+
|
180
|
+
|
181
|
+
Plan: 1 to add, 0 to change, 0 to destroy.
|
182
|
+
```
|
183
|
+
|
184
|
+
Then apply:
|
185
|
+
|
186
|
+
```
|
187
|
+
$ tf test apply
|
188
|
+
|
189
|
+
< SNIP >
|
190
|
+
|
191
|
+
aws_instance.example: Creating...
|
192
|
+
ami: "" => "ami-d7b9a2b1"
|
193
|
+
associate_public_ip_address: "" => "<computed>"
|
194
|
+
availability_zone: "" => "<computed>"
|
195
|
+
ebs_block_device.#: "" => "<computed>"
|
196
|
+
ephemeral_block_device.#: "" => "<computed>"
|
197
|
+
instance_state: "" => "<computed>"
|
198
|
+
instance_type: "" => "t2.micro"
|
199
|
+
ipv6_address_count: "" => "<computed>"
|
200
|
+
ipv6_addresses.#: "" => "<computed>"
|
201
|
+
key_name: "" => "<computed>"
|
202
|
+
network_interface.#: "" => "<computed>"
|
203
|
+
network_interface_id: "" => "<computed>"
|
204
|
+
placement_group: "" => "<computed>"
|
205
|
+
primary_network_interface_id: "" => "<computed>"
|
206
|
+
private_dns: "" => "<computed>"
|
207
|
+
private_ip: "" => "<computed>"
|
208
|
+
public_dns: "" => "<computed>"
|
209
|
+
public_ip: "" => "<computed>"
|
210
|
+
root_block_device.#: "" => "<computed>"
|
211
|
+
security_groups.#: "" => "<computed>"
|
212
|
+
source_dest_check: "" => "true"
|
213
|
+
subnet_id: "" => "<computed>"
|
214
|
+
tenancy: "" => "<computed>"
|
215
|
+
volume_tags.%: "" => "<computed>"
|
216
|
+
vpc_security_group_ids.#: "" => "<computed>"
|
217
|
+
aws_instance.example: Still creating... (10s elapsed)
|
218
|
+
aws_instance.example: Still creating... (20s elapsed)
|
219
|
+
aws_instance.example: Creation complete (ID: i-0f1327bbc53fd6bab)
|
220
|
+
|
221
|
+
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
|
222
|
+
|
223
|
+
< SNIP >
|
224
|
+
```
|
225
|
+
|
226
|
+
And when you're done using the infrastructure, destroy:
|
227
|
+
|
228
|
+
```
|
229
|
+
$ tf test destroy
|
230
|
+
|
231
|
+
< SNIP >
|
232
|
+
|
233
|
+
Do you really want to destroy?
|
234
|
+
Terraform will delete all your managed infrastructure.
|
235
|
+
There is no undo. Only 'yes' will be accepted to confirm.
|
236
|
+
|
237
|
+
Enter a value: yes
|
238
|
+
|
239
|
+
aws_instance.example: Refreshing state... (ID: i-0f1327bbc53fd6bab)
|
240
|
+
aws_instance.example: Destroying... (ID: i-0f1327bbc53fd6bab)
|
241
|
+
aws_instance.example: Still destroying... (ID: i-0f1327bbc53fd6bab, 10s elapsed)
|
242
|
+
aws_instance.example: Still destroying... (ID: i-0f1327bbc53fd6bab, 20s elapsed)
|
243
|
+
aws_instance.example: Still destroying... (ID: i-0f1327bbc53fd6bab, 30s elapsed)
|
244
|
+
aws_instance.example: Still destroying... (ID: i-0f1327bbc53fd6bab, 40s elapsed)
|
245
|
+
aws_instance.example: Still destroying... (ID: i-0f1327bbc53fd6bab, 50s elapsed)
|
246
|
+
aws_instance.example: Still destroying... (ID: i-0f1327bbc53fd6bab, 1m0s elapsed)
|
247
|
+
aws_instance.example: Still destroying... (ID: i-0f1327bbc53fd6bab, 1m10s elapsed)
|
248
|
+
aws_instance.example: Destruction complete
|
249
|
+
|
250
|
+
Destroy complete! Resources: 1 destroyed.
|
251
|
+
```
|
252
|
+
|
253
|
+
#### Production environment
|
254
|
+
|
255
|
+
Now that we've tried that everything works in the test environment, let's do the same in production. Note that you can override variables if needed:
|
256
|
+
|
257
|
+
##### envs/prod.tfvars
|
258
|
+
|
259
|
+
```ini
|
260
|
+
aws_access_key = "PRODUCTION_ACCOUNT_ACCESS_KEY_HERE"
|
261
|
+
aws_secret_key = "PRODUCTION_ACCOUNT_SECRET_KEY_HERE"
|
262
|
+
|
263
|
+
# let's use a bigger instance type in prod
|
264
|
+
instance_type = "t2.medium"
|
265
|
+
```
|
266
|
+
|
267
|
+
And run the commands just like with _test_:
|
268
|
+
|
269
|
+
```
|
270
|
+
$ tf prod plan
|
271
|
+
|
272
|
+
< SNIP >
|
273
|
+
|
274
|
+
$ tf prod apply
|
275
|
+
|
276
|
+
< SNIP >
|
277
|
+
|
278
|
+
$ tf prod destroy
|
279
|
+
|
280
|
+
< SNIP >
|
281
|
+
```
|
282
|
+
|
283
|
+
_Note that the instance type is now t2.medium_
|
284
|
+
|
285
|
+
## Default components
|
286
|
+
|
287
|
+
### tf.yaml
|
288
|
+
|
289
|
+
Here be dragons that configure your project. The tools serches for `tf.yaml`'s up your path all the way to root `/`. Configs made in subdirectories override those made upper in the path. This makes it easy to define common settings without having to edit `tf.yaml` in every project.
|
290
|
+
|
291
|
+
By defalt the following configuration options are supported:
|
292
|
+
* [`hooks`](#hooks) - Pre and Post hooks.
|
293
|
+
* [`backend`](#backend) - Configuration of remote state location.
|
294
|
+
* [`terraform`](#terraform) - Terraform specific configuration.
|
295
|
+
|
296
|
+
#### Hooks
|
297
|
+
|
298
|
+
There are cases when it would be beneficial to run a task at the same time as terrafrom, but building native support for that would be quite cumbersome. The support for hooks was build into YleTf having those cases in mind.
|
299
|
+
|
300
|
+
Essentially hooks are just scripts and small applications to extend the functionality of Terraform. Hooks can be either
|
301
|
+
* [local](#local-hooks) or
|
302
|
+
* [remote](#remote-hooks) i.e. stored into git.
|
303
|
+
|
304
|
+
Real world usecases for hooks include at least the following:
|
305
|
+
* Automatically register ACM certificates and link them to desired resources
|
306
|
+
* Automatically generate dns record resources that are managed by separate configuration
|
307
|
+
* Package lambda applications for deployment via Terraform
|
308
|
+
* Manage SES authorisations and rules
|
309
|
+
* Modify parameters not yet supported by Terraform
|
310
|
+
|
311
|
+
Currently two kinds of hooks are supported:
|
312
|
+
* `pre` - Hooks that run before terraform execution.
|
313
|
+
* `post` - Hooks that run after terraform execution.
|
314
|
+
|
315
|
+
#### Local hooks
|
316
|
+
|
317
|
+
For local hooks, add a directory called `tf_hooks` to your tf project root. You also need a folder to determine wether the hook is run before or after the execution of terraform. The folders are `pre` and/or `post`:
|
318
|
+
|
319
|
+
```
|
320
|
+
.
|
321
|
+
├── envs
|
322
|
+
│ ├── prod.tfvars
|
323
|
+
│ └── test.tfvars
|
324
|
+
├── main.tf
|
325
|
+
├── tf.yaml
|
326
|
+
├── tf_hooks
|
327
|
+
│ ├── pre
|
328
|
+
│ │ └── pre-hook.sh
|
329
|
+
│ └── post
|
330
|
+
│ │ └── post-hook.rb
|
331
|
+
└── variables.tf
|
332
|
+
```
|
333
|
+
|
334
|
+
For example, let's say you wish to have a variable `current_git_hash` and you want to populate it with a value of the latest git commit hash. The hook could be something like this:
|
335
|
+
|
336
|
+
##### tf_hooks/pre/get_current_hash.sh
|
337
|
+
|
338
|
+
```bash
|
339
|
+
#!/bin/bash
|
340
|
+
|
341
|
+
set -eu
|
342
|
+
|
343
|
+
current_hash() {
|
344
|
+
CURRENT_GIT_HASH="$(git rev-parse HEAD)"
|
345
|
+
|
346
|
+
cat <<EOF > current_git_hash.tf.json
|
347
|
+
{
|
348
|
+
"variable": {
|
349
|
+
"current_git_hash": {
|
350
|
+
"value": "$CURRENT_GIT_HASH"
|
351
|
+
}
|
352
|
+
}
|
353
|
+
}
|
354
|
+
EOF
|
355
|
+
}
|
356
|
+
|
357
|
+
# Execute only for commands `plan` and `apply`
|
358
|
+
case "$TF_COMMAND" in
|
359
|
+
plan|apply)
|
360
|
+
current_hash
|
361
|
+
;;
|
362
|
+
*)
|
363
|
+
;;
|
364
|
+
esac
|
365
|
+
```
|
366
|
+
|
367
|
+
_Please note that the script in the [examples](examples/) directory is intentionnally without the execute bit._
|
368
|
+
|
369
|
+
Once you set the hook script executable (`chmod +x tf_hooks/pre/get_current_hash.sh`) and run `tf` with either `plan` or `apply`, you'll have the following file during runtime:
|
370
|
+
|
371
|
+
##### current_git_hash.tf.json
|
372
|
+
|
373
|
+
```json
|
374
|
+
{
|
375
|
+
"variable": {
|
376
|
+
"current_git_hash": {
|
377
|
+
"value": "c6a02baf0597e55d7698f78d70299ca4a65776cd"
|
378
|
+
}
|
379
|
+
}
|
380
|
+
}
|
381
|
+
```
|
382
|
+
|
383
|
+
_Naturraly the actual value varies according to your repository._
|
384
|
+
|
385
|
+
_**Please note that files generated into the temporary working directory while running hooks are removed once the execution has ended.**_
|
386
|
+
|
387
|
+
#### Remote hooks
|
388
|
+
|
389
|
+
Hooks can also be stored in common git repositories. This is a handy way to avoid duplication in codebase.
|
390
|
+
To use a hook from git just add your script to a suitable repository (making sure it's still executable) and add following config to your `tf.yaml`:
|
391
|
+
|
392
|
+
##### tf.yaml
|
393
|
+
|
394
|
+
```yaml
|
395
|
+
hooks:
|
396
|
+
pre:
|
397
|
+
- description: "Get your current git commit hash"
|
398
|
+
source: "git@github.com:your-org/tf-hooks.git//git-hash/get_current_hash.sh"
|
399
|
+
vars:
|
400
|
+
defaults:
|
401
|
+
MESSAGE: "You can also define environment variables to your hooks"
|
402
|
+
test:
|
403
|
+
MESSAGE: "And the variables can also be environment spesific"
|
404
|
+
```
|
405
|
+
|
406
|
+
#### Backend
|
407
|
+
|
408
|
+
Configure where Terraform [remote state](https://www.terraform.io/docs/state/remote.html) is stored
|
409
|
+
|
410
|
+
* `type` - Backend type where the Terraform state is stored.
|
411
|
+
* `bucket` - The name of the S3 bucket.
|
412
|
+
* `file` - The name of the state file.
|
413
|
+
* `region` - The region of the S3 bucket.
|
414
|
+
* `encrypt` - Whether to enable server side encryption of the state file.
|
415
|
+
|
416
|
+
```yaml
|
417
|
+
backend:
|
418
|
+
type: s3
|
419
|
+
```
|
420
|
+
|
421
|
+
#### Terraform
|
422
|
+
|
423
|
+
* `version_requirement` - The version requirement of Terraform in ruby gem syntax.
|
424
|
+
|
425
|
+
```yaml
|
426
|
+
terraform:
|
427
|
+
version_requirement: "~> 0.9.11"
|
428
|
+
```
|
429
|
+
|
430
|
+
## Development
|
431
|
+
|
432
|
+
After checking out the repo, run `bundle update` to install and update the dependencies. Then, run `bundle exec rake spec` to run the tests.
|
433
|
+
|
434
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
435
|
+
|
436
|
+
## Contributing
|
437
|
+
|
438
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Yleisradio/yle_tf. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
439
|
+
|
440
|
+
## License
|
441
|
+
|
442
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|