df2c1e707097b1a50f9f228086e2a4aed49bfd2d
[infra/cicd.git] / jjb / cloud-infra / scripts / cloud-infra-ansible-lint.sh
1 #!/bin/bash
2
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 # source engine-vars.sh so we can install the correct versions of
22 # pip, ansible, and ansible-lint
23 source $WORKSPACE/engine/config/engine-vars.sh
24
25 # install dependencies
26 sudo apt update > /dev/null 2>&1
27 sudo apt install -y -q=3 gcc libffi-dev libssl-dev lsb-release libpython3-dev \
28     python3-minimal python3-pip python3-yaml virtualenv > /dev/null 2>&1
29
30 cd $WORKSPACE
31 echo "Info: Install pip==$ENGINE_PIP_VERSION ansible==$ENGINE_ANSIBLE_VERSION and ansible-lint==$ENGINE_ANSIBLE_LINT_VERSION"
32 echo "----------------------------------------------------"
33
34 # install pip, ansible, and ansible-lint in venv
35 export ANSIBLE_VENV=$WORKSPACE/.venv
36 virtualenv -p python3 --quiet --no-site-packages ${ANSIBLE_VENV}
37 source ${ANSIBLE_VENV}/bin/activate
38 pip -q install --upgrade pip==$ENGINE_PIP_VERSION
39 pip -q install --upgrade virtualenv pip setuptools shade \
40     ansible==$ENGINE_ANSIBLE_VERSION ansible-lint==$ENGINE_ANSIBLE_LINT_VERSION
41
42 echo "----------------------------------------------------"
43 echo "Info: Done"
44 echo "Info: Run ansible-lint on playbooks"
45 echo "----------------------------------------------------"
46
47 # the locations of the engine playbooks
48 playbooks_dir=(engine/playbooks engine/provisioner/bifrost/playbooks engine/provisioner/heat/playbooks \
49     engine/installer/kolla/playbooks engine/installer/kubespray/playbooks)
50
51 # set exit_code so we exit ourselves rather than ansible-lint bailing out without
52 # verifying all the playbooks
53 declare -i exit_code=0
54
55 # lint the playbooks
56 for dir in ${playbooks_dir[@]}; do
57   for play in $(ls ${WORKSPACE}/${dir}/*.yml); do
58     echo "--> Checking '${play}' playbook..."
59     ansible-lint --nocolor ${play}
60     lint_exit_code=$?
61     if [[ $lint_exit_code != 0 ]]; then
62         exit_code=$lint_exit_code
63     else
64         echo [OK]
65     fi
66     echo
67   done
68 done
69 echo "----------------------------------------------------"
70 echo "Info: Done"
71 exit $exit_code
72
73 # vim: set ts=2 sw=2 expandtab: