Report: Add rls data
[csit.git] / resources / libraries / bash / function / ansible.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2021 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -exuo pipefail
17
18
19 function ansible_adhoc () {
20
21     # Run ansible ad-hoc command module on hosts in working topology file.
22     #
23     # Variable read:
24     # - ${WORKING_TOPOLOGY} - Reserved working topology.
25     # - ${CSIT_DIR} - CSIT main directory, where ansible playbooks are located.
26     # - ${FLAVOR} - Node flavor string, see common.sh
27
28     set -exuo pipefail
29
30     case "$FLAVOR" in
31         "aws")
32             INVENTORY_PATH="cloud_inventory"
33             ;;
34         *)
35             INVENTORY_PATH="lf_inventory"
36             ;;
37     esac
38
39     if ! installed sshpass; then
40         die "Please install sshpass!"
41     fi
42
43     hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
44         die "Failed to read hosts from working topology!"
45     }
46     pushd "${CSIT_DIR}"/fdio.infra.ansible || die "Pushd failed!"
47     export ANSIBLE_HOST_KEY_CHECKING=False
48     export ANSIBLE_STDOUT_CALLBACK=yaml
49     export ANSIBLE_PIPELINING=true
50     ansible-playbook \
51         --vault-password-file=vault_pass \
52         --extra-vars '@vault.yml' \
53         --inventory inventories/$INVENTORY_PATH/hosts site.yaml \
54         --limit "$(echo ${hosts[@]//\"})" \
55         --module-name shell \
56         --args \"$(echo $@)\" || die "Failed to run ansible on host!"
57     popd || die "Popd failed!"
58 }
59
60 function ansible_playbook () {
61
62     # Run ansible playbook on hosts in working topology file. Ansible scope is
63     # determined by tags passed as parameters to this function.
64     #
65     # Variable read:
66     # - ${WORKING_TOPOLOGY} - Reserved working topology.
67     # - ${CSIT_DIR} - CSIT main directory, where ansible playbooks are located.
68     # - ${FLAVOR} - Node flavor string, see common.sh
69
70     set -exuo pipefail
71
72     case "$FLAVOR" in
73         "aws")
74             INVENTORY_PATH="cloud_inventory"
75             ;;
76         *)
77             INVENTORY_PATH="lf_inventory"
78             ;;
79     esac
80
81     if ! installed sshpass; then
82         die "Please install sshpass!"
83     fi
84
85     hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
86         die "Failed to read hosts from working topology!"
87     }
88     pushd "${CSIT_DIR}"/fdio.infra.ansible || die "Pushd failed!"
89     export ANSIBLE_HOST_KEY_CHECKING=False
90     export ANSIBLE_STDOUT_CALLBACK=yaml
91     export ANSIBLE_PIPELINING=true
92     ansible-playbook \
93         --vault-password-file=vault_pass \
94         --extra-vars '@vault.yml' \
95         --inventory inventories/$INVENTORY_PATH/hosts site.yaml \
96         --limit "$(echo ${hosts[@]//\"})" \
97         --tags "$(echo $@)" || die "Failed to run ansible on host!"
98     popd || die "Popd failed!"
99 }
100
101
102 function installed () {
103
104     # Check if the given utility is installed. Fail if not installed.
105     #
106     # Arguments:
107     # - ${1} - Utility to check.
108     # Returns (implicitly):
109     # - 0 - If command is installed.
110     # - 1 - If command is not installed.
111
112     set -exuo pipefail
113
114     command -v "${1}"
115 }