Skip to content

Commit be789ff

Browse files
FelikZAlexey Shevchenkokayman-mk
authored
feat: add amazonec2 userdata for docker machines (#608)
Introducing support for amazonec2-userdata flag that is not mentioned on driver readme page, but it is there and useful. For example, it can be used to introduce swap for the runner instance or any other user command. Co-authored-by: Alexey Shevchenko <[email protected]> Co-authored-by: kayma <[email protected]>
1 parent 81d707c commit be789ff

File tree

6 files changed

+28
-0
lines changed

6 files changed

+28
-0
lines changed

examples/runner-default/main.tf

+7
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ module "runner" {
142142
# command = ["--registry-mirror", "https://mirror.gcr.io"]
143143
# entrypoint = ["dockerd-entrypoint.sh"]
144144
# }]
145+
146+
147+
# Example how to configure runners, to utilize EC2 user-data feature
148+
# example template, creates (configurable) swap file for the runner
149+
# runners_userdata = templatefile("${path.module}/../../templates/swap.tpl", {
150+
# swap_size = "512"
151+
# })
145152
}
146153

147154
resource "null_resource" "cancel_spot_requests" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
dd if=/dev/zero of=/swapfile bs=1M count=${ coalesce(swap_size, "512") }
4+
chmod 0600 /swapfile
5+
mkswap /swapfile
6+
swapon /swapfile
7+
8+
echo '/swapfile swap swap defaults 0 0' >>/etc/fstab

main.tf

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ locals {
5757
docker_machine_version = var.docker_machine_version
5858
docker_machine_download_url = var.docker_machine_download_url
5959
runners_config = local.template_runner_config
60+
runners_userdata = var.runners_userdata
6061
runners_executor = var.runners_executor
6162
runners_install_amazon_ecr_credential_helper = var.runners_install_amazon_ecr_credential_helper
6263
pre_install = var.userdata_pre_install
@@ -98,6 +99,7 @@ locals {
9899
runners_name = var.runners_name
99100
runners_tags = replace(replace(local.runner_tags_string, ",,", ","), "/,$/", "")
100101
runners_token = var.runners_token
102+
runners_userdata = var.runners_userdata
101103
runners_executor = var.runners_executor
102104
runners_limit = var.runners_limit
103105
runners_concurrent = var.runners_concurrent

template/gitlab-runner.tpl

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ ${runners_config}
1414

1515
EOF
1616

17+
cat > /etc/gitlab-runner/runners_userdata.sh <<- EOF
18+
${runners_userdata}
19+
EOF
20+
1721
sed -i.bak s/__PARENT_TAG__/`echo $PARENT_TAG`/g /etc/gitlab-runner/config.toml
1822

1923
# fetch Runner token from SSM and validate it

template/runner-config.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ listen_address = "${prometheus_listen_address}"
6565
"amazonec2-iam-instance-profile=%{ if runners_iam_instance_profile_name != "" }${runners_iam_instance_profile_name}%{ else }${runners_instance_profile}%{ endif ~}",
6666
"amazonec2-root-size=${runners_root_size}",
6767
"amazonec2-volume-type=${runners_volume_type}",
68+
"amazonec2-userdata=%{ if runners_userdata != "" }/etc/gitlab-runner/runners_userdata.sh%{ endif ~}",
6869
"amazonec2-ami=${runners_ami}"
6970
${docker_machine_options}
7071
]

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ variable "runners_name" {
124124
type = string
125125
}
126126

127+
variable "runners_userdata" {
128+
description = "Cloud-init user data that will be passed to the runner ec2 instance. Available only for `docker+machine` driver. Should not be base64 encrypted."
129+
type = string
130+
default = ""
131+
}
132+
127133
variable "runners_executor" {
128134
description = "The executor to use. Currently supports `docker+machine` or `docker`."
129135
type = string

0 commit comments

Comments
 (0)