Create a role to run jobs in OpenShift

Inspired by Koschei's `koschei/job` role.
Adapt the MirrorManager move-to-archive job to it.

Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
This commit is contained in:
Aurélien Bompard 2024-08-06 07:49:53 +02:00
parent be65fcee91
commit 5778e0d999
No known key found for this signature in database
GPG key ID: 31584CFEB9BF64AD
4 changed files with 38 additions and 22 deletions

View file

@ -16,8 +16,6 @@
- /srv/web/infra/ansible/vars/apps/mirrormanager.yml
handlers:
- import_tasks: "{{ handlers_path }}/restart_services.yml"
vars:
job_name: move-to-archive-{{ product|lower|replace(" ", "-") }}-{{ version|lower|replace(" ", "-") }}-{{ ansible_date_time.epoch }}
pre_tasks:
- name: Validate the product {{ product }}
@ -33,16 +31,7 @@
fail_msg: "The 'version' variable is not defined"
roles:
- role: openshift/object
- role: openshift/job
app: mirrormanager
template: cmd-move-to-archive.yml
objectname: cmd-move-to-archive.yml
always_apply: true
tasks:
- debug:
msg: "Job started. You can watch the logs with: oc -n mirrormanager logs -f job/{{job_name}}"
- debug:
msg: "You can cancel it with: oc -n mirrormanager delete job/{{job_name}}"
- debug:
msg: "When it's done, clean it up with: oc -n mirrormanager delete job/{{job_name}}"
name: move-to-archive-{{ product|lower|replace(" ", "-") }}-{{ version|lower|replace(" ", "-") }}
command: "/opt/app-root/bin/mm2_move-to-archive --product {{ product }} --version {{ version }}"

View file

@ -1,10 +1,10 @@
{% from "_macros.yml" import common_volume_mounts, common_volumes, common_env, security_context with context %}
# Manual command: move-to-archive
# A template for manual commands
---
apiVersion: batch/v1
kind: Job
metadata:
name: "{{ job_name }}"
name: "job-{{ name }}"
spec:
parallelism: 1
completions: 1
@ -16,12 +16,7 @@ spec:
containers:
- name: mirrormanager
image: image-registry.openshift-image-registry.svc:5000/mirrormanager/mirrormanager2:latest
command:
- "/opt/app-root/bin/mm2_move-to-archive"
- "--product"
- "{{ product }}"
- "--version"
- "{{ version }}"
command: ["/bin/bash", "-euxc", "{{command}}"]
env:
{{ common_env() }}
volumeMounts:

View file

@ -0,0 +1,2 @@
os_app: "{{app}}"
template: job.yml

View file

@ -0,0 +1,30 @@
- name: Validate the job name {{ name }}
assert:
that:
- name is defined
fail_msg: "The 'name' variable is not defined"
- name: Validate the command {{ command }}
assert:
that:
- command is defined
fail_msg: "The 'command' variable is not defined"
- name: Copy job template to a temporary file
template:
src: "{{roles_path}}/openshift-apps/{{app}}/templates/{{template}}"
dest: "/etc/openshift_apps/{{app}}/job-{{name}}.yml"
- name: Delete previous job (if any)
shell: oc -n {{os_app}} delete --ignore-not-found=true -f /etc/openshift_apps/{{app}}/job-{{name}}.yml
- name: Start job
shell: oc -n {{os_app}} create -f /etc/openshift_apps/{{app}}/job-{{name}}.yml
- debug:
msg: "Job started. You can watch the logs with: oc -n {{os_app}} logs -f job/job-{{name}}"
- debug:
msg: "You can cancel it with: oc -n {{os_app}} delete job/job-{{name}}"
- name: Wait for job to complete
shell: oc -n {{os_app}} wait job/job-{{name}} --for condition=complete