Skip to content

Commit

Permalink
feat: add amazonec2 userdata for docker machines (#608)
Browse files Browse the repository at this point in the history
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 <o.shevchenko@relay42.com>
Co-authored-by: kayma <kayma@hlag.com>
  • Loading branch information
3 people committed Jan 12, 2023
1 parent 81d707c commit be789ff
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/runner-default/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ module "runner" {
# command = ["--registry-mirror", "https://mirror.gcr.io"]
# entrypoint = ["dockerd-entrypoint.sh"]
# }]


# Example how to configure runners, to utilize EC2 user-data feature
# example template, creates (configurable) swap file for the runner
# runners_userdata = templatefile("${path.module}/../../templates/swap.tpl", {
# swap_size = "512"
# })
}

resource "null_resource" "cancel_spot_requests" {
Expand Down
8 changes: 8 additions & 0 deletions examples/runner-default/templates/swap.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

dd if=/dev/zero of=/swapfile bs=1M count=${ coalesce(swap_size, "512") }
chmod 0600 /swapfile
mkswap /swapfile
swapon /swapfile

echo '/swapfile swap swap defaults 0 0' >>/etc/fstab
2 changes: 2 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ locals {
docker_machine_version = var.docker_machine_version
docker_machine_download_url = var.docker_machine_download_url
runners_config = local.template_runner_config
runners_userdata = var.runners_userdata
runners_executor = var.runners_executor
runners_install_amazon_ecr_credential_helper = var.runners_install_amazon_ecr_credential_helper
pre_install = var.userdata_pre_install
Expand Down Expand Up @@ -98,6 +99,7 @@ locals {
runners_name = var.runners_name
runners_tags = replace(replace(local.runner_tags_string, ",,", ","), "/,$/", "")
runners_token = var.runners_token
runners_userdata = var.runners_userdata
runners_executor = var.runners_executor
runners_limit = var.runners_limit
runners_concurrent = var.runners_concurrent
Expand Down
4 changes: 4 additions & 0 deletions template/gitlab-runner.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ ${runners_config}

EOF

cat > /etc/gitlab-runner/runners_userdata.sh <<- EOF
${runners_userdata}
EOF

sed -i.bak s/__PARENT_TAG__/`echo $PARENT_TAG`/g /etc/gitlab-runner/config.toml

# fetch Runner token from SSM and validate it
Expand Down
1 change: 1 addition & 0 deletions template/runner-config.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ listen_address = "${prometheus_listen_address}"
"amazonec2-iam-instance-profile=%{ if runners_iam_instance_profile_name != "" }${runners_iam_instance_profile_name}%{ else }${runners_instance_profile}%{ endif ~}",
"amazonec2-root-size=${runners_root_size}",
"amazonec2-volume-type=${runners_volume_type}",
"amazonec2-userdata=%{ if runners_userdata != "" }/etc/gitlab-runner/runners_userdata.sh%{ endif ~}",
"amazonec2-ami=${runners_ami}"
${docker_machine_options}
]
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ variable "runners_name" {
type = string
}

variable "runners_userdata" {
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."
type = string
default = ""
}

variable "runners_executor" {
description = "The executor to use. Currently supports `docker+machine` or `docker`."
type = string
Expand Down

0 comments on commit be789ff

Please sign in to comment.