diff --git a/.gitignore b/.gitignore index 86d63728d673aadf08a53ea9cd3844ac59178607..ad4a3aa1ea3dda9e45a6c47f21a573d9d3490bbe 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ /.terraform/ /terraform.tfstate /terraform.tfstate.backup +/id_rsa +/id_rsa.pub diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000000000000000000000000000000000000..bd8dc5b0769550966e2f6f51e86dd3dcae3cade0 --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,6 @@ +# 2023-02-13 + +- !2 Update documentation after terraform MR 1 changes: + terraform project now use destruct provisioner to unregister gitlab runner + too. + https://gitlab.inria.fr/gitlabci_gallery/orchestration/terraform/-/merge_requests/1 diff --git a/README.md b/README.md index 1d431d117ac8e727ba7019a86cc610d054b6988f..85fbc09328fb5bb62e1a1f77f88aa5a2ebceb7d0 100644 --- a/README.md +++ b/README.md @@ -22,26 +22,9 @@ resources, but only when they are needed. ## Prerequisites -In addition to the prerequisites listed for the +This project has the same prerequisites as those listed for the [terraform](https://gitlab.inria.fr/gitlabci_gallery/orchestration/terraform) -project, this project needs a pair of passphrase-less SSH private/public keys -for the GitLab shared runner to be able to connect to the deployed -runners to unregister them from GitLab before deletion. - -- You can use the following command to create a pair of SSH private/public keys - without passphrase in the current directory (files `id_rsa` and `id_rsa.pub`): - `ssh-keygen -b 4096 -f id_rsa -N ""`. - -- The contents of the private key file `id_rsa` should be added as a variable - `SSH_PRIVATE_KEY` of type **File** in CI/CD settings. - See the - [intro](https://gitlab.inria.fr/gitlabci_gallery/intro#cicd-variables) - project for details on how to set a CI/CD variable. - -- The public key file `id_rsa.pub` should be registered on ci.inria.fr portal - to allow the dedicated user to connect to the hosted virtual machines - ([portal documentation](https://ci.inria.fr/doc/page/slaves_access_tutorial/#register-your-ssh-public-key)). - for details on how to register a public key on the portal. +project. ## The Terraform configuration file [`main.tf`](main.tf) @@ -50,21 +33,12 @@ configuration file described for the [`terraform](https://gitlab.inria.fr/gitlabci_gallery/orchestration/terraform#the-terraform-configuration-file-maintf) project. -There is two additional variables: `SSH_PUBLIC_KEY`, of type `string`, -and `runner_count`, of type `number` . +There is one additional variable: `runner_count`, of type `number` . ```terraform -variable "SSH_PUBLIC_KEY" { - type = string -} - variable "runner_count" { type = number } ``` -The value of the `SSH_PUBLIC_KEY` variable will be stored in the file -`~/.ssh/authorized_keys` in virtual machines, so that Terraform can -connect to the virtual machines with the private key to unregister the -runners before destroying the machines. The variable `runner_count` has two purposes: - It allows to deploy a virtual machine conditionally. @@ -85,7 +59,7 @@ resource "cloudstack_instance" "runner" { count = var.runner_count name = "gitlabcigallery-terraform-runner-${count.index}" service_offering = "Custom" - template = "ubuntu-20.04-cloudinit" + template = "ubuntu-20.04-lts" zone = "zone-ci" details = { cpuNumber = 1 @@ -122,20 +96,7 @@ We then use the index of the virtual machine available through `count.index` so that each virtual machine is named uniquely, and we pass the `index` to the template file so that each runner can be registered with a different tag `runner-${index}` by the script -[`cloudinit.sh.tftpl`](cloudinit.sh.tftpl). -We pass also the `SSH_PUBLIC_KEY` to the template file to have its -value written in the `~/.ssh/authorized_keys` file. -We configure the connection via ssh to the runner: `gter001` is the -login of the dedicated user on ci.inria.fr, and we will make sure -in the next section that the private key is written in the file -`id_rsa`. -We cannot use a variable for passing the path to this file, -since the connection is used by a destroy provisioner, that -cannot refer to variables. -This destroy provisioner executes `gitlab-runner unregister` -before the destruction of the virtual machine; failures are ignored -in case of the `gitlab-runner` command was not yet installed -when destroying occurs. +[`cloud-init.sh.tftpl`](cloud-init.sh.tftpl). ## The pipeline specification file [`.gitlab-ci.yml`](.gitlab-ci.yml) diff --git a/cloud-init.sh.tftpl b/cloud-init.sh.tftpl index e2b5d89a6cd432ef4491da136a91bf2a4260104a..986d4c41de7097efb683de81479fa95886e670a7 100644 --- a/cloud-init.sh.tftpl +++ b/cloud-init.sh.tftpl @@ -1,10 +1,20 @@ #!/bin/sh -( +# Standard output and errors are redirected to /root/log.txt to ease +# debugging. +>>/root/log.txt 2>&1 ( + # To be able to run `sudo gitlab-runner unregister --all-runners` on + # VM destruction. echo 'ci ALL=(ALL) NOPASSWD:ALL' >/etc/sudoers.d/90-ci - mkdir -p -m 700 $HOME/.ssh + mkdir -p -m 700 ~/.ssh echo ${SSH_PUBLIC_KEY} >>~ci/.ssh/authorized_keys + # GitLab needs a recent version of `gitlab-runner` to be compatible with + # the instance running on gitlab.inria.fr. The version packaged by default + # on Ubuntu is regularly out of date. curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash - apt-get update + # apt-get update performed by the script above + # We install docker.io to be able to register a docker executor apt-get install --yes gitlab-runner docker.io - gitlab-runner register --non-interactive --tag-list terraform,runner-${index},docker --executor docker --docker-image alpine --url https://gitlab.inria.fr --registration-token ${REGISTRATION_TOKEN} -) >/root/log.txt 2>&1 + gitlab-runner register --non-interactive --tag-list terraform,docker \ + --executor docker --docker-image alpine --url https://gitlab.inria.fr \ + --registration-token ${REGISTRATION_TOKEN} +) diff --git a/main.tf b/main.tf index 96d7f640f5763a0b1d94bc91d31f61e4666f773f..7683dcfb4c5448845fb9659ca0863208fca2b191 100644 --- a/main.tf +++ b/main.tf @@ -32,7 +32,7 @@ resource "cloudstack_instance" "runner" { count = var.runner_count name = "gitlabcigallery-terraform-runner-${count.index}" service_offering = "Custom" - template = "ubuntu-20.04-cloudinit" + template = "ubuntu-20.04-lts" zone = "zone-ci" details = { cpuNumber = 1