Skip to content

Commit 202957f

Browse files
committed
terraformscaffold v1.3.0
1 parent 31262ff commit 202957f

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.3.0 (26/07/2017)
2+
3+
* Introduce the group variables file functionality
4+
15
## 1.2.0 (09/06/2017)
26

37
* Merge bootstrap functionality into the main script

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Scaffold uses AWS S3 for storage of tfstate files. The bucket and location are s
3535
Scaffold provides a logical separation of several types of environment variable:
3636
* Global variables
3737
* Region-scoped global variables
38+
* Group variables
3839
* Static environment variables
3940
* Frequently-changing versions variables
4041
* Dynamic (S3 stored) variables
@@ -103,6 +104,7 @@ bin/terraform.sh \
103104
-b/--bucket-prefix `bucket_prefix` \
104105
-c/--component `component_name` \
105106
-e/--environment `environment` \
107+
-g/--group `group` (optional) \
106108
-i/--build-id `build_id` (optional) \
107109
-p/--project `project` \
108110
-r/--region `region` \
@@ -123,6 +125,7 @@ Where:
123125
* It is usual to provide, for example, the Jenkins _$BUILD_ID_ parameter to Plan jobs, and then manually reference that particular Job ID when running a corresponding apply job.
124126
* `component_name`: The name of the terraform component in the components directory to run the `action` against.
125127
* `environment`: The name of the environment the component is to be actioned against, therefore implying the variables file(s) to be included
128+
* `group` (optional): The name of the group to which the environment belongs, permitting the use of a group tfvars file as a "meta-environment" shared by more than one environment
126129
* `project`: The name of the project being deployed, as per the default bucket-prefix and state file keyspace
127130
* `region` (optional): The AWS region name unique to all components and terraform processes. Defaults to the value of the _AWS_DEFAULT_REGION_ environment variable.
128131
* `additional arguments`: Any arguments provided after "--" will be passed directly to terraform as its own arguments, e.g. allowing the provision of a 'target=value' parameter.

bin/terraform.sh

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
##
99
# Set Script Version
1010
##
11-
readonly script_ver="1.2.1";
11+
readonly script_ver="1.3.0";
1212

1313
##
1414
# Standardised failure function
@@ -36,6 +36,7 @@ Usage: ${0} \\
3636
-b/--bucket-prefix [bucket_prefix] \\
3737
-c/--component [component_name] \\
3838
-e/--environment [environment] \\
39+
-g/--group [group]
3940
-i/--build-id [build_id] (optional) \\
4041
-p/--project [project] \\
4142
-r/--region [region] \\
@@ -70,6 +71,11 @@ environment:
7071
- prod
7172
- management
7273
74+
group:
75+
- dev
76+
- live
77+
- mytestgroup
78+
7379
project:
7480
- The name of the project being deployed
7581
@@ -87,8 +93,8 @@ EOF
8793
##
8894
readonly raw_arguments="${*}";
8995
ARGS=$(getopt \
90-
-o hva:b:c:e:i:p:r: \
91-
-l "help,version,bootstrap,action:,bucket-prefix:,build-id:,component:,environment:,project:,region:" \
96+
-o hva:b:c:e:g:i:p:r: \
97+
-l "help,version,bootstrap,action:,bucket-prefix:,build-id:,component:,environment:,group:,project:,region:" \
9298
-n "${0}" \
9399
-- \
94100
"$@");
@@ -105,6 +111,7 @@ declare bootstrap="false";
105111
declare component_arg;
106112
declare region_arg;
107113
declare environment_arg;
114+
declare group;
108115
declare action;
109116
declare bucket_prefix;
110117
declare build_id;
@@ -141,6 +148,13 @@ while true; do
141148
shift;
142149
fi;
143150
;;
151+
-g|--group)
152+
shift;
153+
if [ -n "${1}" ]; then
154+
group="${1}";
155+
shift;
156+
fi;
157+
;;
144158
-a|--action)
145159
shift;
146160
if [ -n "${1}" ]; then
@@ -229,6 +243,7 @@ else
229243
[ -n "${environment_arg}" ] \
230244
|| error_and_die "Required argument missing: -e/--environment";
231245
readonly environment="${environment_arg}";
246+
232247
fi
233248

234249
[ -n "${action}" ] \
@@ -407,6 +422,12 @@ else
407422
# Check for presence of a region variables file, and use it if readable
408423
readonly region_vars_file_name="${region}.tfvars";
409424
readonly region_vars_file_path="${base_path}/etc/${region_vars_file_name}";
425+
426+
# Check for presence of a group variables file if specified, and use it if readable
427+
if [ -n "${group}" ]; then
428+
readonly group_vars_file_name="group_${group}.tfvars";
429+
readonly group_vars_file_path="${base_path}/etc/${group_vars_file_name}";
430+
fi;
410431

411432
# Collect the paths of the variables files to use
412433
declare -a tf_var_file_paths;
@@ -417,6 +438,18 @@ else
417438
# being declared in multiple locations, and we warn when we find any duplicates
418439
[ -f "${global_vars_file_path}" ] && tf_var_file_paths+=("${global_vars_file_path}");
419440
[ -f "${region_vars_file_path}" ] && tf_var_file_paths+=("${region_vars_file_path}");
441+
442+
# If a group has been specified, load the vars for the group. If we are to assume
443+
# terraform correctly handles override-ordering (which to be fair we don't hence
444+
# the warning about duplicate variables below) we add this to the list after
445+
# global and region-global variables, but before the environment variables
446+
# so that the environment can explicitly override variables defined in the group.
447+
if [ -n "${group}" ]; then
448+
[ -f "${group_vars_file_path}" ] \
449+
|| error_and_die "Group \"${group}\" has been specified, but no group variables file is available at ${group_vars_file_path}";
450+
451+
tf_var_file_paths+=("${group_vars_file_path}");
452+
fi;
420453

421454
# We've already checked this is readable and its presence is mandatory
422455
tf_var_file_paths+=("${env_file_path}");

etc/group_examplegroup.tfvars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Variables shared by any environment that chooses to be subscribed to it

0 commit comments

Comments
 (0)