Ansible git move
[csit.git] / fdio.infra.ansible / roles / cleanup / files / reset_vppdevice.sh
1 #!/usr/bin/env bash
2
3 set -euo pipefail
4
5 function die () {
6     # Print the message to standard error end exit with error code specified
7     # by the second argument.
8     #
9     # Hardcoded values:
10     # - The default error message.
11     # Arguments:
12     # - ${1} - The whole error message, be sure to quote. Optional
13     # - ${2} - the code to exit with, default: 1.
14
15     set +eu
16     warn "${1:-Unspecified run-time error occurred!}"
17     exit "${2:-1}"
18 }
19
20
21 function set_eligibility_off {
22     # Set Nomad eligibility to ineligible for scheduling. Fail otherwise.
23
24     set -euo pipefail
25
26     node_id="$(nomad node status | grep $(hostname) | cut -d ' ' -f 1)" || die
27     node_status="$(nomad node status | grep $(hostname))" || die
28
29     if [[ "${node_status}" != *"ineligible"* ]]; then
30         nomad node eligibility -disable "${node_id}" || die
31         node_status="$(nomad node status | grep $(hostname))" || die
32         if [[ "${node_status}" != *"ineligible"* ]]; then
33             die "Set eligibility off failed!"
34         fi
35     fi
36 }
37
38
39 function set_eligibility_on {
40     # Set Nomad eligibility to eligible for scheduling. Fail otherwise.
41
42     set -euo pipefail
43
44     node_id="$(nomad node status | grep $(hostname) | cut -d ' ' -f 1)" || die
45     node_status="$(nomad node status | grep $(hostname))" || die
46
47     if [[ "${node_status}" == *"ineligible"* ]]; then
48         nomad node eligibility -enable "${node_id}" || die
49         node_status="$(nomad node status | grep $(hostname))" || die
50         if [[ "${node_status}" == *"ineligible"* ]]; then
51             die "Set eligibility on failed!"
52         fi
53     fi
54 }
55
56
57 function restart_vfs_service {
58     # Stop and start VF serice. This will reinitialize VFs and driver mappings.
59
60     set -euo pipefail
61
62     warn "Restarting VFs service (this may take few minutes)..."
63     sudo service csit-initialize-vfs stop || die "Failed to stop VFs service!"
64     sudo service csit-initialize-vfs start || die "Failed to start VFs service!"
65 }
66
67
68 function wait_for_pending_containers {
69     # Wait in loop for defined amount of time for pending containers to
70     # gracefully quit them. If parameter force is specified. Force kill them.
71
72     # Arguments:
73     # - ${@} - Script parameters.
74
75     set -euo pipefail
76
77     retries=60
78     wait_time=60
79     containers=(docker ps --quiet --filter name=csit*)
80
81     for i in $(seq 1 ${retries}); do
82         mapfile -t pending_containers < <( ${containers[@]} ) || die
83         warn "Waiting for pending containers [${pending_containers[@]}] ..."
84         if [ ${#pending_containers[@]} -eq 0 ]; then
85             break
86         fi
87         sleep "${wait_time}" || die
88     done
89     if [ ${#pending_containers[@]} -ne 0 ]; then
90         if [[ "${1-}" == "force" ]]; then
91             warn "Force killing [${pending_containers[@]}] ..."
92             docker rm --force ${pending_containers[@]} || die
93         else
94             die "Still few containers running!"
95         fi
96     fi
97 }
98
99
100 function warn () {
101     # Print the message to standard error.
102     #
103     # Arguments:
104     # - ${@} - The text of the message.
105
106     echo "$@" >&2
107 }
108
109
110 set_eligibility_off || die
111 wait_for_pending_containers "${@}" || die
112 restart_vfs_service || die
113 set_eligibility_on || die