-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathexec_command_in_pod.yml
More file actions
160 lines (147 loc) · 6.07 KB
/
Copy pathexec_command_in_pod.yml
File metadata and controls
160 lines (147 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
# Copyright Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: Command execution block
block:
- name: Kill any previous background command and clear markers
kubernetes.core.k8s_exec:
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
namespace: "{{ namespace }}"
pod: "{{ pod_name }}"
command: >-
bash -c 'if [ -f /tmp/cifmw_cmd_pid ]; then
kill $(cat /tmp/cifmw_cmd_pid) 2>/dev/null;
wait $(cat /tmp/cifmw_cmd_pid) 2>/dev/null; fi;
rm -f /tmp/cifmw_cmd_rc /tmp/cifmw_cmd_pid
/tmp/cifmw_cmd_script.sh /tmp/cifmw_cmd_output.log'
changed_when: false
failed_when: false
- name: Copy the command script into the pod
kubernetes.core.k8s_cp:
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
namespace: "{{ namespace }}"
pod: "{{ pod_name }}"
remote_path: /tmp/cifmw_cmd_script.sh
content: |
#!/bin/bash
{{ command }}
state: to_pod
- name: Execute detached in the pod - {{ command }}
kubernetes.core.k8s_exec:
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
namespace: "{{ namespace }}"
pod: "{{ pod_name }}"
command: >-
bash -c 'nohup bash -c "bash /tmp/cifmw_cmd_script.sh;
echo \$? > /tmp/cifmw_cmd_rc"
> /tmp/cifmw_cmd_output.log 2>&1 &
echo $! > /tmp/cifmw_cmd_pid && cat /tmp/cifmw_cmd_pid'
register: pod_command_result
- name: Wait for the command to complete in the pod
environment:
PATH: "{{ cifmw_path }}"
KUBECONFIG: "{{ cifmw_openshift_kubeconfig }}"
ansible.builtin.command:
cmd: >-
oc exec -n {{ namespace }} {{ pod_name }} --
bash -c 'if cat /tmp/cifmw_cmd_rc 2>/dev/null; then exit 0;
elif [ -f /tmp/cifmw_cmd_pid ] && kill -0 $(cat /tmp/cifmw_cmd_pid) 2>/dev/null; then exit 1;
else exit 2; fi'
register: pod_exit_code
changed_when: false
failed_when: false
until: pod_exit_code.rc == 0 or pod_exit_code.rc == 2
retries: "{{ cifmw_shiftstack_exec_retries }}"
delay: "{{ cifmw_shiftstack_exec_delay }}"
- name: Fail if the background process died without writing the marker
ansible.builtin.fail:
msg: >-
The background process in pod {{ pod_name }} exited without writing a result.
The command may have been killed by OOM or a signal.
when: pod_exit_code.rc == 2
- name: Fail if polling timed out
ansible.builtin.fail:
msg: >-
Timed out waiting for "{{ command }}" to complete
in pod {{ pod_name }} after {{ cifmw_shiftstack_exec_retries }} attempts.
when: pod_exit_code.rc != 0 and pod_exit_code.rc != 2
- name: Fail if the command failed inside the pod
ansible.builtin.fail:
msg: >-
"{{ command }}" failed inside the pod with
exit code {{ pod_exit_code.stdout | trim }}.
when:
- pod_exit_code.rc == 0
- (pod_exit_code.stdout | default('') | trim) != '0'
rescue:
- name: Kill background process on failure
kubernetes.core.k8s_exec:
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
namespace: "{{ namespace }}"
pod: "{{ pod_name }}"
command: >-
bash -c 'if [ -f /tmp/cifmw_cmd_pid ]; then
kill $(cat /tmp/cifmw_cmd_pid) 2>/dev/null; fi'
failed_when: false
changed_when: false
- name: Retrieve command output from pod
kubernetes.core.k8s_exec:
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
namespace: "{{ namespace }}"
pod: "{{ pod_name }}"
command: >-
bash -c 'tail -c 8192 /tmp/cifmw_cmd_output.log 2>/dev/null
|| echo "(output not available)"'
register: pod_output_log
failed_when: false
changed_when: false
- name: Fail with diagnostic information
ansible.builtin.fail:
msg: >-
"{{ command }}" failed.
pod exit code: {{ pod_exit_code.stdout | default('N/A') | trim }},
output (last 8KB): {{ pod_output_log.stdout | default('(not retrieved)') }}
always:
- name: Command execution log saving block
when: log_file_name | default(false)
block:
- name: Retrieve command output for log
kubernetes.core.k8s_exec:
kubeconfig: "{{ cifmw_openshift_kubeconfig }}"
namespace: "{{ namespace }}"
pod: "{{ pod_name }}"
command: >-
bash -c 'tail -c 16384 /tmp/cifmw_cmd_output.log 2>/dev/null
|| echo "(output not available)"'
register: pod_output_for_log
failed_when: false
changed_when: false
- name: Get current date and time
ansible.builtin.command:
cmd: date +"%Y_%m_%d-%H_%M_%S"
register: current_date
- name: Save the command execution information in the log file in '{{ cifmw_shiftstack_ansible_command_logs_dir }}'
vars:
command_info:
namespace: "{{ namespace }}"
pod_name: "{{ pod_name }}"
command: "{{ command }}"
pod_command_result: "{{ pod_command_result | default('N/A') }}"
pod_exit_code: "{{ pod_exit_code | default('N/A') }}"
pod_output: "{{ pod_output_for_log.stdout | default('N/A') }}"
ansible.builtin.copy:
content: "{{ command_info | to_nice_yaml }}"
dest: "{{ cifmw_shiftstack_ansible_command_logs_dir }}/{{ current_date.stdout }}.{{ log_file_name }}"
mode: "0644"