Skip to content

Commit 6ddc775

Browse files
committed
Add remove application playbook and doc
1 parent 2f7ea44 commit 6ddc775

File tree

6 files changed

+181
-0
lines changed

6 files changed

+181
-0
lines changed

docs/how-to/Remove applications.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Remove a deployed application
2+
3+
```shellsession
4+
# Conf
5+
DEPLOY_DIR=/home/ubuntu/Deployment/rs-python
6+
RSCONFIG_DIR=${DEPLOY_DIR}/rs-config
7+
RSINFRA_DIR=${DEPLOY_DIR}/rs-config/rs-infra-core
8+
9+
cd ${RSINFRA_DIR}
10+
11+
# Activate virtualenv (necessary to run ansible-playbooks):
12+
conda activate rspy
13+
14+
# Standard undeployment:
15+
# Example 1: to undeploy "02-cluster-issuer", just input "cluster-issuer" (omit the number)
16+
# Example 2: to undeploy "keycloak", input "keycloak"
17+
APP=
18+
# Choose apps dir : apps / apps-env / apps-monitoring / apps-rs-server / apps-workflow
19+
APPS_DIR=
20+
ansible-playbook remove_apps.yaml \
21+
-i inventory/mycluster/hosts.yaml \
22+
-e '{"package_paths": ["'${RSCONFIG_DIR}/${APPS_DIR}'"], "app": "'${APP}'"}'
23+
```

remove_apps.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2025 CS Group
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
- name: Remove apps
16+
hosts: localhost
17+
gather_facts: false
18+
roles:
19+
- { role: "app-remover" }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Keep the temporary folder containing the applications resources to deploy
2+
# /!\ DO NOT FORGET TO DELETE THIS TEMPORARY FOLDER MANUALLY AFTER DEBUGGGING
3+
# AS IT MAY CONTAINS SENSITIVE DATA.
4+
debug: false

roles/app-remover/tasks/main.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- name: Browse each package
2+
include_tasks: remove_package.yaml
3+
vars:
4+
package_name: "{{ package_path | basename }}"
5+
loop: "{{ package_paths }}"
6+
loop_control:
7+
loop_var: package_path
8+
when: package is undefined or package == package_name
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
- name: Remove app
2+
block:
3+
- name: "{{ package_name }} - {{ app_name }} | Check whether kustomization.yaml file exists"
4+
stat:
5+
path: "{{ app_dir }}/kustomization.yaml"
6+
delegate_to: bastion
7+
register: kustomization_file
8+
9+
- name: "{{ package_name }} - {{ app_name }} | Fail if no kustomization file found"
10+
fail:
11+
msg: "No kustomization.yaml file found in {{ app_dir }}. Documentation available at https://github.com/COPRS/infrastructure/blob/release/0.3.0/doc/how-to/Add%20an%20application.md"
12+
when: not kustomization_file.stat.exists
13+
14+
- name: "{{ package_name }} - {{ app_name }} | Include vars from kustomization.yaml"
15+
include_vars:
16+
file: "{{ app_dir }}/kustomization.yaml"
17+
name: kustomization
18+
delegate_to: bastion
19+
20+
- name: "{{ package_name }} - {{ app_name }} | Check whether .helm_repository_config.yaml exists"
21+
stat:
22+
path: "{{ app_dir }}/.helm_repository_config.yaml"
23+
register: repository_config
24+
delegate_to: bastion
25+
26+
- name: "{{ package_name }} - {{ app_name }} | Include vars from .helm_repository_config.yaml"
27+
include_vars:
28+
file: "{{ app_dir }}/.helm_repository_config.yaml"
29+
when: repository_config.stat.exists
30+
delegate_to: bastion
31+
32+
- name: "{{ package_name }} - {{ app_name }} | Create tmp directory for k8s resources"
33+
tempfile:
34+
state: directory
35+
suffix: "k8s_resources_{{ app_name }}"
36+
register: resources_dir
37+
delegate_to: bastion
38+
39+
- name: "{{ package_name }} - {{ app_name }} | Template and send resources to remote"
40+
template:
41+
src: "{{ item }}"
42+
dest: "{{ resources_dir.path }}/{{ item | basename }}"
43+
with_fileglob: "{{ app_dir }}/*"
44+
when:
45+
- item not in ['.helm_repository_config.yaml', 'resources.txt']
46+
delegate_to: bastion
47+
48+
- name: "{{ package_name }} - {{ app_name }} | Copy subdirectories to remote"
49+
copy:
50+
src: "{{ app_dir }}/{{ item.path }}"
51+
dest: "{{ resources_dir.path }}"
52+
with_community.general.filetree: "{{ app_dir }}/"
53+
when:
54+
- item.state == 'directory'
55+
delegate_to: bastion
56+
57+
- name: "{{ package_name }} - {{ app_name }} | Pull charts from private repositories"
58+
shell: |
59+
helm pull \
60+
--untar --untardir {{ resources_dir.path }}/charts \
61+
--repo {{ item.0.repo }} {{ item.0.name }} \
62+
{{ "--version " + item.0.version if item.0.version is defined else omit }} \
63+
{{ "--username " + item.1.username if item.1.username is defined else omit }} \
64+
{{ "--password " + item.1.password if item.1.password is defined else omit }}
65+
loop: "{{ kustomization.helmCharts | product(helm_repositories) }}"
66+
when:
67+
- repository_config.stat.exists
68+
- helm_repositories is defined
69+
- kustomization.helmCharts is defined
70+
- item.0.repo == item.1.name
71+
delegate_to: bastion
72+
73+
- name: "{{ package_name }} - {{ app_name }} | Remove resources"
74+
shell: |
75+
kustomize build \
76+
--enable-helm {{ resources_dir.path }} \
77+
| kubectl delete -f - \
78+
-l app.kubernetes.io/instance={{ app_name }}
79+
register: result
80+
delegate_to: bastion
81+
82+
- name: "{{ package_name }} - {{ app_name }} | reset facts"
83+
set_fact:
84+
resources: []
85+
delegate_to: bastion
86+
87+
- name: "{{ package_name }} - {{ app_name }} | Delete resources.txt"
88+
file:
89+
path: "{{ app_dir }}/resources.txt"
90+
state: absent
91+
delegate_to: bastion
92+
93+
always:
94+
- name: "{{ package_name }} - {{ app_name }} | Remove resources tmp dir"
95+
file:
96+
path: "{{ resources_dir.path }}"
97+
state: absent
98+
when:
99+
- resources_dir.path is defined
100+
- not debug
101+
delegate_to: bastion
102+
103+
- name: "{{ package_name }} - {{ app_name }} | Warn about not deleted tmp folder"
104+
debug:
105+
msg:
106+
- "DEBUG: the temporary folder containing the k8s resources has been kept tfor debugging purpose"
107+
- "{{ resources_dir.path }}"
108+
- "DO NOT FORGET TO DELETE IT WHEN YOU ARE DONE"
109+
when:
110+
- debug
111+
delegate_to: bastion
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- name: "{{ package_name }} | Collect apps in package"
2+
find:
3+
file_type: directory
4+
paths: "{{ package_path }}"
5+
delegate_to: bastion
6+
register: result
7+
8+
- name: "{{ package_name }} | Browse each app"
9+
include_tasks: remove_app.yaml
10+
vars:
11+
app_name: "{{ app_dir | basename | regex_search('^[0-9-]*([\\w-]+)', '\\1') | first }}"
12+
regex: '.+/[\w-]+[\w\.-]+$'
13+
loop: "{{ result.files | sort(attribute='path') | map(attribute='path') | map('regex_search', regex) }}"
14+
loop_control:
15+
loop_var: "app_dir"
16+
when: app is undefined or app == app_name

0 commit comments

Comments
 (0)