diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a96f7f751..d40a29f15 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,7 @@ jobs: strategy: fail-fast: false matrix: - terraform: [1.3.9, latest] + terraform: [ 1.3.9, latest ] example: [ "runner-default", diff --git a/examples/runner-certificates/main.tf b/examples/runner-certificates/main.tf index e7c9c5875..c0bef14ae 100644 --- a/examples/runner-certificates/main.tf +++ b/examples/runner-certificates/main.tf @@ -51,7 +51,12 @@ module "runner" { # cp /etc/gitlab-runner/certs/* /usr/local/share/ca-certificates/ # update-ca-certificates # Or similar OS-dependent commands. The above are an example for Ubuntu. - runners_additional_volumes = ["/etc/gitlab-runner/certs/:/etc/gitlab-runner/certs:ro"] + runners_docker_options = { + volumes = [ + "/cache", + "/etc/gitlab-runner/certs/:/etc/gitlab-runner/certs:ro" + ] + } ############################################### # Registration diff --git a/examples/runner-default/main.tf b/examples/runner-default/main.tf index 39a49fbe1..5038a9e9e 100644 --- a/examples/runner-default/main.tf +++ b/examples/runner-default/main.tf @@ -79,9 +79,6 @@ module "runner" { "tf-aws-gitlab-runner:instancelifecycle" = "spot:yes" } - runners_privileged = "true" - runners_additional_volumes = ["/certs/client"] - runners_volumes_tmpfs = [ { volume = "/var/opt/cache", @@ -106,6 +103,11 @@ module "runner" { } ] + runners_docker_options = { + privileged = "true" + volumes = ["/cache", "/certs/client"] + } + runners_pre_build_script = < value if value != null && key != "volumes" + }, { + volumes = local.runners_volumes + }) + } + ) + + # Ensure max builds is optional runners_max_builds_string = var.runners_max_builds == 0 ? "" : format("MaxBuilds = %d", var.runners_max_builds) @@ -64,17 +74,13 @@ locals { name_sg = var.overrides["name_sg"] == "" ? local.tags["Name"] : var.overrides["name_sg"] name_iam_objects = lookup(var.overrides, "name_iam_objects", "") == "" ? local.tags["Name"] : var.overrides["name_iam_objects"] - runners_additional_volumes = <<-EOT - %{~if var.runners_add_dind_volumes~},"/certs/client", "/builds", "/var/run/docker.sock:/var/run/docker.sock"%{endif~}%{~for volume in var.runners_additional_volumes~},"${volume}"%{endfor~} - EOT + runners_volumes = concat(var.runners_docker_options.volumes, var.runners_add_dind_volumes ? ["/certs/client", "/builds", "/var/run/docker.sock:/var/run/docker.sock"] : []) runners_docker_services = templatefile("${path.module}/template/runners_docker_services.tftpl", { runners_docker_services = var.runners_docker_services } ) - runners_pull_policies = "[\"${join("\",\"", var.runners_pull_policies)}\"]" - /* determines if the docker machine executable adds the Name tag automatically (versions >= 0.16.2) */ # make sure to skip pre-release stuff in the semver by ignoring everything after "-" docker_machine_version_used = split(".", split("-", var.docker_machine_version)[0]) diff --git a/main.tf b/main.tf index 9248df253..910441d39 100644 --- a/main.tf +++ b/main.tf @@ -91,7 +91,6 @@ locals { gitlab_url = var.runners_gitlab_url gitlab_clone_url = var.runners_clone_url tls_ca_file = length(var.runners_gitlab_certificate) > 0 ? "tls-ca-file=\"/etc/gitlab-runner/certs/gitlab.crt\"" : "" - runners_extra_hosts = var.runners_extra_hosts runners_vpc_id = var.vpc_id runners_subnet_id = var.subnet_id runners_aws_zone = data.aws_availability_zone.runners.name_suffix @@ -102,7 +101,6 @@ locals { runners_monitoring = var.runners_monitoring runners_ebs_optimized = var.runners_ebs_optimized runners_instance_profile = var.runners_executor == "docker+machine" ? aws_iam_instance_profile.docker_machine[0].name : "" - runners_additional_volumes = local.runners_additional_volumes docker_machine_options = length(local.docker_machine_options_string) == 1 ? "" : local.docker_machine_options_string docker_machine_name = format("%s-%s", local.runner_tags_merged["Name"], "%s") # %s is always needed runners_name = var.runners_name @@ -112,13 +110,6 @@ locals { runners_executor = var.runners_executor runners_limit = var.runners_limit runners_concurrent = var.runners_concurrent - runners_image = var.runners_image - runners_privileged = var.runners_privileged - runners_disable_cache = var.runners_disable_cache - runners_docker_runtime = var.runners_docker_runtime - runners_helper_image = var.runners_helper_image - runners_shm_size = var.runners_shm_size - runners_pull_policies = local.runners_pull_policies runners_idle_count = var.runners_idle_count runners_idle_time = var.runners_idle_time runners_max_builds = local.runners_max_builds_string @@ -135,6 +126,7 @@ locals { runners_request_concurrency = var.runners_request_concurrency runners_output_limit = var.runners_output_limit runners_check_interval = var.runners_check_interval + runners_docker_options = local.runners_docker_options_toml runners_volumes_tmpfs = join("\n", [for v in var.runners_volumes_tmpfs : format("\"%s\" = \"%s\"", v.volume, v.options)]) runners_services_volumes_tmpfs = join("\n", [for v in var.runners_services_volumes_tmpfs : format("\"%s\" = \"%s\"", v.volume, v.options)]) runners_docker_services = local.runners_docker_services diff --git a/migrations/migrate-to-7-0-0.sh b/migrations/migrate-to-7-0-0.sh index de69a715d..540743aa4 100755 --- a/migrations/migrate-to-7-0-0.sh +++ b/migrations/migrate-to-7-0-0.sh @@ -28,3 +28,54 @@ sed -i '/asg_terminate_lifecycle_lambda_timeout/d' "$converted_file" # PR #711 feat!: refactor Docker Machine autoscaling options # sed -i 's/runners_machine_autoscaling/runners_machine_autoscaling_options/g' "$converted_file" + +# +# PR #710 chore!: remove old variable `runners_pull_policy` +# +sed -i '/runners_pull_policy/d' "$converted_file" + +# +# PR #511 feat!: allow to set all docker options for the Executor +# +extracted_variables=$(grep -E '(runners_docker_runtime|runners_helper_image|runners_shm_size|runners_shm_size|runners_extra_hosts|runners_disable_cache|runners_image|runners_privileged)' "$converted_file") + +sed -i '/runners_image/d' "$converted_file" +sed -i '/runners_privileged/d' "$converted_file" +sed -i '/runners_disable_cache/d' "$converted_file" +sed -i '/runners_extra_hosts/d' "$converted_file" +sed -i '/runners_shm_size/d' "$converted_file" +sed -i '/runners_docker_runtime/d' "$converted_file" +sed -i '/runners_helper_image/d' "$converted_file" + +# content to be added to `volumes` +volumes=$(grep "runners_additional_volumes" "$converted_file" | cut -d '=' -f 2 | tr -d '[]') + +if [ -n "$volumes" ]; then + extracted_variables="$extracted_variables + volumes = [\"/cache\", $volumes]" +fi + +sed -i '/runners_additional_volumes/d' "$converted_file" + + +# rename the variables +extracted_variables=$(echo "$extracted_variables" | \ + sed 's/runners_image/image/g' | \ + sed 's/runners_privileged/privileged/g' | \ + sed 's/runners_disable_cache/disable_cache/g' | \ + sed 's/runners_extra_hosts/extra_hosts/g' | \ + sed 's/runners_shm_size/shm_size/g' | \ + sed 's/runners_docker_runtime/runtime/g' | \ + sed 's/runners_helper_image/helper_image/g' + ) + +# add new block runners_docker_options at the end +echo "$(head -n -1 "$converted_file") +runners_docker_options { + $extracted_variables +} +}" > x + +mv x "$converted_file" + +echo "Module call converted. Output: $converted_file" \ No newline at end of file diff --git a/template/runner-config.tftpl b/template/runner-config.tftpl index eca80c910..9439a3c62 100644 --- a/template/runner-config.tftpl +++ b/template/runner-config.tftpl @@ -18,22 +18,17 @@ listen_address = "${prometheus_listen_address}" request_concurrency = ${runners_request_concurrency} output_limit = ${runners_output_limit} limit = ${runners_limit} - [runners.docker] - tls_verify = false - image = "${runners_image}" - privileged = ${runners_privileged} - disable_cache = ${runners_disable_cache} - volumes = ["/cache"${runners_additional_volumes}] - extra_hosts = ${jsonencode(runners_extra_hosts)} - shm_size = ${runners_shm_size} - pull_policy = ${runners_pull_policies} - runtime = "${runners_docker_runtime}" - helper_image = "${runners_helper_image}" - ${runners_docker_services} + + ${runners_docker_options} + + ${runners_docker_services} + [runners.docker.tmpfs] ${runners_volumes_tmpfs} + [runners.docker.services_tmpfs] ${runners_services_volumes_tmpfs} + [runners.cache] Type = "s3" Shared = ${shared_cache} @@ -43,6 +38,7 @@ listen_address = "${prometheus_listen_address}" BucketName = "${bucket_name}" BucketLocation = "${aws_region}" Insecure = false + [runners.machine] IdleCount = ${runners_idle_count} IdleTime = ${runners_idle_time} diff --git a/template/runners_docker_options.tftpl b/template/runners_docker_options.tftpl new file mode 100644 index 000000000..2a4dcac4a --- /dev/null +++ b/template/runners_docker_options.tftpl @@ -0,0 +1,4 @@ + [runners.docker] +%{ for key, value in options ~} + ${key} = ${jsonencode(value)} +%{ endfor ~} diff --git a/variables.tf b/variables.tf index 5d551bf97..a718bdf17 100644 --- a/variables.tf +++ b/variables.tf @@ -188,64 +188,77 @@ variable "runners_max_builds" { default = 0 } -variable "runners_image" { - description = "Image to run builds, will be used in the runner config.toml" - type = string - default = "docker:18.03.1-ce" -} - -variable "runners_privileged" { - description = "Runners will run in privileged mode, will be used in the runner config.toml" - type = bool - default = true -} - -variable "runners_disable_cache" { - description = "Runners will not use local cache, will be used in the runner config.toml" - type = bool - default = false -} - variable "runners_add_dind_volumes" { description = "Add certificates and docker.sock to the volumes to support docker-in-docker (dind)" type = bool default = false } -variable "runners_additional_volumes" { - description = "Additional volumes that will be used in the runner config.toml, e.g Docker socket" - type = list(any) - default = [] -} - -variable "runners_extra_hosts" { - description = "Extra hosts that will be used in the runner config.toml, e.g other-host:127.0.0.1" - type = list(any) - default = [] -} - -variable "runners_shm_size" { - description = "shm_size for the runners, will be used in the runner config.toml" - type = number - default = 0 -} +variable "runners_docker_options" { + description = <