Files
@ f05151d6f802
Branch filter:
Location: majic-ansible-roles/roles/common/files/pip_check_requirements_upgrades.sh
f05151d6f802
8.7 KiB
text/x-sh
MAR-132: Added support for Debian 9 (Stretch) to wsgi_website role:
- Set the shell for application system account explicitly (workaround
for Debian bug 865762 in Stretch).
- Updated Molecule tests to cover Debian 9.
- Updated Molecule test preparation playbook to account for a number
of differences between Jessie and Stretch (mainly related to mailing
functionality).
- Renamed a couple of variables in test for sending out mails to make
it clearer what is being looked up as part of regex matching.
- Updated Molecule tests where certain paths depend on what Debian
release they are ran against.
- Split-up Jessie-specific tests into separate file.
- Remove the /bin/ss utility instead of renaming it (testinfra socket
tests do not work with /bin/ss).
- Set the shell for application system account explicitly (workaround
for Debian bug 865762 in Stretch).
- Updated Molecule tests to cover Debian 9.
- Updated Molecule test preparation playbook to account for a number
of differences between Jessie and Stretch (mainly related to mailing
functionality).
- Renamed a couple of variables in test for sending out mails to make
it clearer what is being looked up as part of regex matching.
- Updated Molecule tests where certain paths depend on what Debian
release they are ran against.
- Split-up Jessie-specific tests into separate file.
- Remove the /bin/ss utility instead of renaming it (testinfra socket
tests do not work with /bin/ss).
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | #!/bin/bash
#
# pip_check_requirements_upgrades.sh
#
# Copyright (C) 2017, Branko Majic <branko@majic.rs>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
program="pip_check_requirements_upgrades.sh"
function usage() {
cat <<EOF
$program, a non-interactive utility for checking for available upgrades in
Python pip requirements files based on pip-tools
Usage: $program [OPTIONS] configuration_directory
$program a non-interactive utility for checking for available upgrades in Python
pip requirements files based on pip-tools. Utility is written specifically with
pip-tools in mind, and pip-tools must be available when running the utility.
If you are unfamiliar with pip-tools, please read its documentation first, and
make sure you fully understand how it works.
Utility accepts a single positionl argument - path to configuration
directory. Configuration directory should contain one or more sub-directories
where each sub-directory is treated as describing a separate Python virtual
environment. This allows the checks to be run for multiple virtual environments
in a modular manner.
Each sub-directory should contain one or more .in files with corresponding .txt
file. Base names must match (i.e. if you have production.in, the requirements
file must be called production.txt).
Utility iterates over each .in/.txt pair, calculates new requirements
based on the .in file, and diffs this against existing .txt file. If a
.in file does not have a corresponding .txt file, it is ignored
(making it possible to use the '-r base.in' syntax for including base
requirements etc).
Utility creates copy of existing requirements file, stripping it from all
comments, then calculates new requirements file (storing result in temporary
location as well), and runs a diff between them.
If newer pacakges are available that would satisfiy the provided .in file, a
diff is shown on standard output in addition to a warning message.
$program accepts the following options:
-V virtualenv
Path to virtual environment with pre-installed pip-tools. If specified,
the virtual environment will be activated prior to running the utility.
-q
Quiet mode. Output a message only if newer packages are available.
-d
Enable debug mode.
-v
Show script licensing information.
-h
Show usage help.
Please report bugs and send feature requests to <branko@majic.rs>.
EOF
}
function version() {
cat <<EOF
$program
+-----------------------------------------------------------------------+
| Copyright (C) 2017, Branko Majic <branko@majic.rs> |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/>. |
+-----------------------------------------------------------------------+
EOF
}
# Set-up colours for message printing if we're not piping and terminal is
# capable of outputting the colors.
_color_terminal=$(tput colors 2>&1)
if [[ -t 1 ]] && (( ${_color_terminal} > 0 )); then
_text_bold=$(tput bold)
_text_white=$(tput setaf 7)
_text_blue=$(tput setaf 6)
_text_green=$(tput setaf 2)
_text_yellow=$(tput setaf 3)
_text_red=$(tput setaf 1)
_text_reset=$(tput sgr0)
else
_text_bold=""
_text_white=""
_text_blue=""
_text_green=""
_text_yellow=""
_text_red=""
_text_reset=""
fi
# Set-up functions for printing coloured messages.
function debug() {
if [[ $debug != 0 ]]; then
echo -e "${_text_bold}${_text_blue}[DEBUG]${_text_reset}" "$@"
fi
}
function info() {
echo -e "${_text_bold}${_text_white}[INFO] ${_text_reset}" "$@"
}
function success() {
echo -e "${_text_bold}${_text_green}[OK] ${_text_reset}" "$@"
}
function warning() {
echo -e "${_text_bold}${_text_yellow}[WARN] ${_text_reset}" "$@"
}
function error() {
echo -e "${_text_bold}${_text_red}[ERROR]${_text_reset}" "$@" >&2
}
# Clean-up command for temporary files.
function on_exit() {
debug "Cleaning-up temporary file: $tmp_current"
[[ -f $tmp_current ]] && rm "$tmp_current"
debug "Cleaning-up temporary file: $tmp_new"
[[ -f $tmp_new ]] && rm "$tmp_new"
debug "Cleaning-up temporary file: $tmp_new_sorted"
[[ -f $tmp_new_sorted ]] && rm "$tmp_new_sorted"
}
trap on_exit EXIT
# Define error codes.
SUCCESS=0
ERROR_ARGUMENTS=1
ERROR_CONFIG_DIR=2
ERROR_PIP_TOOLS_MISSING=3
# Disable debug and quiet modes by default.
debug=0
quiet=0
# Set-up defaults.
virtualenv=""
# If no arguments were given, just show usage help.
if [[ -z $1 ]]; then
usage
exit "$SUCCESS"
fi
# Parse the arguments
while getopts "V:qdvh" opt; do
case "$opt" in
V) virtualenv="$OPTARG";;
q) quiet=1;;
d) debug=1;;
v) version
exit "$SUCCESS";;
h) usage
exit "$SUCCESS";;
*) usage
exit "$ERROR_ARGUMENTS";;
esac
done
i=$OPTIND
shift $(($i-1))
if [[ ${#@} == 0 ]]; then
error "Must pass configuration directory."
usage
exit "$ERROR_ARGUMENTS"
fi
# Verfiy positional arguments.
config_dir="$1"
if [[ ! -d $config_dir ]]; then
error "No such directory: $config_dir"
exit "$ERROR_ARGUMENTS"
fi
# Make sure virtual environment exists.
if [[ -n $virtualenv && ! -f $virtualenv/bin/activate ]]; then
error "Invalid virtual environment specified: $virtualenv"
exit "$ERROR_ARGUMENTS"
fi
# Activate the virtual environment if it was specified.
[[ -n $virtualenv ]] && source "$virtualenv/bin/activate"
# Verify pip-compile is available.
if ! which pip-compile >/dev/null 2>&1; then
error "Could not find command pip-compile from packagep pip-tools. Package might not be available in the virtual environment."
exit "$ERROR_PIP_TOOLS_MISSING"
fi
# Create temporary files where files where "normalised" outputs will be
# stored.
tmp_current=$(mktemp)
tmp_new=$(mktemp)
tmp_new_sorted=$(mktemp)
# Process each environment.
for environment in "$config_dir"/*; do
# Empty directory.
if [[ ! -e $environment ]]; then
error "Configuration directory is empty: $config_dir"
exit "$ERROR_CONFIG_DIR"
fi
# Process each .in file.
for req_in in "$environment"/*.in; do
# Directory without .in files.
if [[ ! -e $req_in ]]; then
error "No .in files in directory, skipping: $environment"
continue
fi
# Figure out if .txt file exists.
req_txt="${req_in%.in}.txt"
if [[ ! -f $req_txt ]]; then
[[ $quiet == 0 ]] && info "Skipping input file with missing .txt file: $req_in"
continue
fi
# Deploy the existing requirements file and the new one.
sed -e 's/[[:blank:]]*#.*//' "$req_txt" | grep -v "^$" | sort -u > "$tmp_current"
if ! pip-compile --no-header --no-annotate --no-index --output-file "$tmp_new" --upgrade "$req_in" > /dev/null; then
error "Failed while running pip-compile command against (see error stack trace above): $req_in"
continue
fi
# Sort the new requirements file.
sed -e 's/[[:blank:]]*#.*//' "$tmp_new" | grep -v "^$" | sort -u > "$tmp_new_sorted"
debug "Current requirements:\n$(cat "$tmp_current")"
debug "New requirements:\n$(cat "$tmp_new_sorted")"
# Run diff, storing the output and result.
diff=$(diff -u "$tmp_current" "$tmp_new_sorted")
result="$?"
# Show warning about available updates.
if [[ $result == 0 ]]; then
[[ $quiet == 0 ]] && info "No upgrades available for: $req_txt"
else
warning "Upgrades available for: $req_txt"
echo
echo "$diff"
echo
fi
done
done
|