diff --git a/.gitignore b/.gitignore index 67311e1..f1648f1 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ mail/mail_template.txt mail/mail_template_en.txt mail/mail_info.yaml passwords +solutions.patch diff --git a/Readme.md b/Readme.md index 23a7297..6cd6652 100644 --- a/Readme.md +++ b/Readme.md @@ -10,7 +10,7 @@ This terraform script will create the following resources: If you look for the GCP terraform configuration, take a look at the folder `gcp_setup`. -## Prerequisite +## Prerequisites - terraform (`v1.1.7+`) - [puttygen](https://www.puttygen.com/) (tested with Release 0.71) @@ -49,6 +49,34 @@ After the creation of the instances run `./scripts/check_connection.sh` to check Terraform state is stored locally by default. The `main.tf` contains the required backend config snippet to enable remote state with a single command. This could be useful if you have to hand over the training environment to someone else. +## Automatically download and unpack solutions + +In order to automatically download and unpack solutions, set the variable `solutions_url` to the URL provided by the Linux Foundation. The solutions will be unpacked on every machine in the `/home/student/LF*` directory. + +see [terraform.tfvars.example](terraform.tfvars.example) for an example, you'll have to provide the Linux Foundation provided username and password inside the url. + +### Bonus: Automatically patching solutions + +If you find that the official solutions contain errors, you can also automatically fix them and patch all the solutions you automatically downloaded. + +To do that: + +1. Download the solutions on your machine using the official URL (of course use the correct username and password) + ``` + wget https://training.linuxfoundation.org/cm/LFS458/LFS458_V1.22.1_SOLUTIONS.tar.xz --user=**USERNAME** --password=**PASSWORD** + ``` +1. Unpack the tarball +1. Duplicate the unpacked directory: + ``` + cp -a LFS458 LFS458patched + ``` +1. In the `LFS458patched` directory (or whatever you named it), edit, delete and create all files as needed +1. create a patch for the whole tree: + ``` + diff -ruN LFS458 LFS458patched > solutions.patch + ``` +1. Copy the `solutions.patch` file (using exactly this name) to the terraform base dir (next to your `terraform.tfvars` file) + ## Sending Mails Ensure that the [Gmail API](https://developers.google.com/gmail/api/quickstart/python#step_1_turn_on_the) is activated. diff --git a/main.tf b/main.tf index f73da86..d464491 100644 --- a/main.tf +++ b/main.tf @@ -16,14 +16,16 @@ provider "openstack" { # -backend-config="state_name=terraform-$TRAINER_NAME.stat" module "student_workspace" { - source = "./modules/student_workspace" - students = var.students - instances = var.instances - network = openstack_networking_network_v2.network.id - machine_type = var.machine_type - course_type = var.course_type - trainer = var.trainer - sec_groups = [openstack_networking_secgroup_v2.sec.name] + source = "./modules/student_workspace" + students = var.students + instances = var.instances + network = openstack_networking_network_v2.network.id + machine_type = var.machine_type + course_type = var.course_type + trainer = var.trainer + sec_groups = [openstack_networking_secgroup_v2.sec.name] + solutions_url = var.solutions_url + solutions_patch = fileexists("${path.module}/solutions.patch") ? filebase64("${path.module}/solutions.patch") : "" } module "wetty_server" { diff --git a/modules/student_workspace/cloudinit.yaml b/modules/student_workspace/cloudinit.yaml index 9d37470..bf589fb 100644 --- a/modules/student_workspace/cloudinit.yaml +++ b/modules/student_workspace/cloudinit.yaml @@ -26,6 +26,33 @@ users: ssh_authorized_keys: - ${SSH_PUB_KEY} write_files: + %{ if SOLUTIONS_PATCH != "" } + - path: "/tmp/solutions.patch" + permissions: "0666" + encoding: b64 + content: ${SOLUTIONS_PATCH} + %{ endif } + - path: "/tmp/download_solutions.sh" + permissions: "0700" + content: | + #!/bin/bash + + if [[ "${SOLUTIONS_URL}" =~ https://.+:.+@training.linuxfoundation.org/cm/.+SOLUTIONS.tar.+ ]] + then + echo "downloading solutions from ${SOLUTIONS_URL}" + wget -P /tmp -q ${SOLUTIONS_URL} + echo "unpack solutions" + tar -xvf /tmp/*SOLUTIONS.tar* -C /home/${DEFAULT_USER}/ + echo "chown solutions to ${DEFAULT_USER}" + chown -R ${DEFAULT_USER}:users /home/${DEFAULT_USER}/LF* + if [[ -f /tmp/solutions.patch ]] + then + echo "trying to patch solutions" + patch -d /home/${DEFAULT_USER} -p0 < /tmp/solutions.patch + fi + else + echo "SOLUTIONS_URL invalid or username password part missing" + fi - path: "/etc/vim/vimrc.local" permissions: "0744" content: | @@ -36,6 +63,9 @@ write_files: set shiftwidth=2 set softtabstop=2 +runcmd: + - /tmp/download_solutions.sh + power_state: mode: reboot timeout: 30 diff --git a/modules/student_workspace/main.tf b/modules/student_workspace/main.tf index ca2ff1b..82419a7 100644 --- a/modules/student_workspace/main.tf +++ b/modules/student_workspace/main.tf @@ -31,6 +31,8 @@ resource "openstack_compute_instance_v2" "instance" { { DEFAULT_USER = "student" SSH_PUB_KEY = trimspace(tls_private_key.ssh_key[split("-", each.value)[0]].public_key_openssh) + SOLUTIONS_URL = var.solutions_url + SOLUTIONS_PATCH = var.solutions_patch } ) diff --git a/modules/student_workspace/variables.tf b/modules/student_workspace/variables.tf index 603943b..f7dc5c7 100644 --- a/modules/student_workspace/variables.tf +++ b/modules/student_workspace/variables.tf @@ -25,3 +25,13 @@ variable "trainer" { variable "sec_groups" { type = list(string) } + +variable "solutions_url" { + type = string + default = "" +} + +variable "solutions_patch" { + type = string + default = "" +} \ No newline at end of file diff --git a/solutions.patch.example b/solutions.patch.example new file mode 100644 index 0000000..e58e449 --- /dev/null +++ b/solutions.patch.example @@ -0,0 +1,23 @@ +diff -ruN LFD459/SOLUTIONS/s_02/k8sWorker.sh LFD459p/SOLUTIONS/s_02/k8sWorker.sh +--- LFD459/SOLUTIONS/s_02/k8sWorker.sh 2022-02-24 20:24:03.000000000 +0000 ++++ LFD459p/SOLUTIONS/s_02/k8sWorker.sh 2022-05-20 12:33:07.621080542 +0000 +@@ -47,7 +47,7 @@ + sudo apt-get update + + # Install cri-o +-sudo apt-get install -y cri-o cri-o-runc podman buildah ++sudo apt-get install -y cri-o cri-o-runc podman + + sleep 3 + +diff -ruN LFD459/SOLUTIONS/s_02/k8scp.sh LFD459p/SOLUTIONS/s_02/k8scp.sh +--- LFD459/SOLUTIONS/s_02/k8scp.sh 2022-02-24 20:24:03.000000000 +0000 ++++ LFD459p/SOLUTIONS/s_02/k8scp.sh 2022-05-20 12:32:55.557075552 +0000 +@@ -48,7 +48,7 @@ + sudo apt-get update + + # Install cri-o +-sudo apt-get install -y cri-o cri-o-runc podman buildah ++sudo apt-get install -y cri-o cri-o-runc podman + + sleep 3 diff --git a/terraform.tfvars.example b/terraform.tfvars.example index 793e16a..c1642cf 100644 --- a/terraform.tfvars.example +++ b/terraform.tfvars.example @@ -6,3 +6,8 @@ course_type = "lfs458" instances = ["cp", "worker"] # instances = ["cp", "secondcp", "thirdcp", "ha-proxy", "worker"] network_range = "172.128.0.0/24" +wetty_config = { enabled = false, + trainer_email = "wile.e.coyote@inovex.de" +} + +#solutions_url = "https://username:password@training.linuxfoundation.org/cm/LFD459/LFD459_V1.23.1-u1_SOLUTIONS.tar.xz" \ No newline at end of file diff --git a/variables.tf b/variables.tf index 6f6d229..ac0538a 100644 --- a/variables.tf +++ b/variables.tf @@ -48,3 +48,9 @@ variable "dns_domain" { description = "domain for creating DNS records, currently only used for wetty-server" default = "training-lf-kubernetes.fra.ics.inovex.io." } + +variable "solutions_url" { + description = "URL to download and unpack solutions from LF" + type = string + default = "" +} \ No newline at end of file