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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
#!/bin/bash
set -e
migration_command="puppetdb config-migration"
#############
# FUNCTIONS #
#############
# Display usage information and exit
#
# This function simply displays the usage information to the screen and exits
# with exit code 0.
#
# Example:
#
# usage
#
function usage() {
echo "Usage: ${migration_command}"
echo "Configuration helper for migrating config files for PuppetDB upgrade."
echo
echo "This tool will attempt to copy the necessary PuppetDB config files into "\
"place for use by the PuppetDB HTTPS service. It updates the pathing of "\
"the files to align with new path expectations in the PuppetDB 3 series. "\
"It will ensure the config files contain either existing edits or up "\
"to date default settings. If the default settings are changed, the script "\
"will also migrate any associated data into the new default location."
echo
echo "Options:"
echo " -h Help"
exit 0
}
# Backs up a file, if it hasn't already been backed up
#
# $1 - file to backup
#
# Example:
#
# backupfile "/etc/myconfig"
#
function backupfile {
# Create the global array if it doesn't already exist
if [ -z "$backupfile_list" ]; then
backupfile_list=()
fi
# We check the array to make sure the file isn't already backed up
if ! contains ${backupfile_list[@]} "$1"; then
local backup_path="$1.bak.$(date +%s)"
echo "Backing up $1 to ${backup_path} before making changes"
cp -p "$1" "$backup_path"
# Append to the array, so we don't need to back it up again later
backupfile_list+=($1)
fi
}
# This function searches for an element in an array returning 1 if it exists.
#
# $1 - array
# $2 - item to search for
#
# Example:
#
# myarray=('element1', 'element2')
# if contains ${myarray[@]}, "element1'; then
# echo "element1 exists in the array"
# fi
#
function contains {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)); do
if [ "${!i}" == "${value}" ]; then
return 0
fi
done
return 1
}
# This function wraps sed for a line focused search and replace.
#
# * Makes sure its atomic by writing to a temp file and moving it _after_
# * Escapes any forward slashes and ampersands on the RHS for you
#
# $1 - regexp to match
# $2 - line to replace
# $3 - file to operate on
#
# Example:
#
# replaceline "^$mysetting.*" "mysetting = myvalue" /etc/myconfig
#
function replaceline {
backupfile "$3"
tmp="$3.tmp.$(date +%s)"
sed "s/$1/$(echo "$2" | sed -e 's/[\/&]/\\&/g')/g" "$3" > "$tmp"
mv "$tmp" "$3"
chmod 644 "$3"
}
# This funtion gives a generic error message if a given path or file isn't found
#
# $1 - missing path
#
function path_not_found {
echo "Warning: Unable to find the path to copy"
echo
echo " This tool requires the following to exist:"
echo
echo " * $1"
echo
echo " Re-run this tool then restart PuppetDB to complete the config"
echo " setup:"
echo
echo " ${migration_command}"
}
# This function copies the give file to the given destination
#
# $1 - file or directory to be moved
# $2 - destination
#
# Example:
#
# migrate_file "/etc/puppetdb/conf.d/config.ini" "/etc/puppetlabs/puppetdb/conf.d"
#
function migrate {
origin=$1
destination=$2
if [ "${destination##*/}" = "${origin##*/}" ] ; then
destination_path="${destination%/*}"
else
destination_path="$destination"
destination="${destination}/${origin##*/}"
fi
if [ ! -e "$origin" ]; then
path_not_found "$origin"
else
if [ -e "$destination" ] ; then
echo "Replacing: ${destination} with ${origin}"
cp -pr "$origin" "$destination_path"
else
echo "Copying: ${origin} to ${destination}"
mkdir -p "$destination_path"
cp -pr "$origin" "$destination_path"
fi
fi
}
########
# MAIN #
########
# Gather command line options
while getopts "h" opt;
do
case $opt in
h)
usage ;;
*)
usage ;;
esac
done
# Figure out where the config files are we want to deal with
# Either they exist in the archive created by the preinst script
# during the upgrade, or the user is running this script by hand
# and they exist in the PDB 2 series location
if [ -e '/tmp/puppetdb-upgrade-config-files.tgz' ] ; then
orig_config_dir=/tmp/puppetdb-orig-configs
mkdir -p $orig_config_dir
tar -xzf '/tmp/puppetdb-upgrade-config-files.tgz' -C $orig_config_dir
elif [ -e '/etc/puppetdb/conf.d/config.ini' -a -e '/etc/puppetdb/conf.d/database.ini' -a -e '/etc/puppetdb/conf.d/jetty.ini' ] ; then
orig_config_dir=/etc/puppetdb/conf.d
else
echo "Config archive not found. Not proceeding with migration"
exit 0
fi
orig_jetty_file=${orig_config_dir}/jetty.ini
orig_database_file=${orig_config_dir}/database.ini
orig_config_file=${orig_config_dir}/config.ini
orig_ini_files=($orig_jetty_file $orig_database_file $orig_config_file)
config_dir=/etc/puppetlabs/puppetdb/conf.d
database_file=${config_dir}/database.ini
jetty_file=${config_dir}/jetty.ini
config_file=${config_dir}/config.ini
ini_files=($config_file $jetty_file $database_file)
echo "Migrating configuration files ${orig_ini_files[*]} to ${config_dir}"
for orig_ini_file in "${orig_ini_files[@]}" ; do
migrate "$orig_ini_file" "$config_dir"
done
echo "Checking to see if we need to update default settings"
for ini_file in "${ini_files[@]}"; do
if [ -f "${ini_file}" ] ; then
# Check settings are correct and fix or warn. Do not move vardir behind
# subname.
orig_settings=(
"vardir:/var/lib/puppetdb"
"logging-config:/etc/puppetdb/logback.xml"
"subname:file:/var/lib/puppetdb/db/db;hsqldb.tx=mvcc;sql.syntax_pgs=true"
"ssl-key:/etc/puppetdb/ssl/private.pem"
"ssl-cert:/etc/puppetdb/ssl/public.pem"
"ssl-ca-cert:/etc/puppetdb/ssl/ca.pem"
)
new_settings=(
"vardir:/opt/puppetlabs/server/data/puppetdb"
"logging-config:/etc/puppetlabs/puppetdb/logback.xml"
"subname:file:/opt/puppetlabs/server/data/puppetdb/db/db;hsqldb.tx=mvcc;sql.syntax_pgs=true"
"ssl-key:/etc/puppetlabs/puppetdb/ssl/private.pem"
"ssl-cert:/etc/puppetlabs/puppetdb/ssl/public.pem"
"ssl-ca-cert:/etc/puppetlabs/puppetdb/ssl/ca.pem"
)
for i in "${orig_settings[@]}"; do
setting="${i%%:*}"
orig_value="${i#*:}"
for j in "${new_settings[@]}"; do
if [ "${j%%:*}" == "${setting}" ] ; then
new_value="${j#*:}"
if grep -qe "^${setting}" "$ini_file"; then
if grep -qe "^${setting}[[:space:]]*=[[:space:]]*${orig_value}$" "$ini_file"; then
replaceline "^${setting}.*" "${setting} = ${new_value}" "$ini_file"
echo "Updated setting ${setting} in ${ini_file}."
case "$setting" in
vardir|ssl-*)
migrate "$orig_value" "$new_value"
rm -rf "$orig_value"
;;
subname)
orig_hsql_path="${orig_value#file:}"
orig_hsql_dir="${orig_hsql_path%/db;hsqldb.tx=mvcc;sql.syntax_pgs=true}"
new_hsql_path="${new_value#file:}"
new_hsql_dir="${new_hsql_path%/db;hsqldb.tx=mvcc;sql.syntax_pgs=true}"
## if the default vardir still exists, then the vardir was
## changed, but the subname still points to the default
## vardir.
if [ -e "$orig_hsql_dir" ] ; then
echo "Copying over the contents of ${orig_hsql_dir} to ${new_hsql_dir}"
migrate "$orig_hsql_dir" "$new_hsql_dir"
rm -rf "$orig_hsql_dir"
fi
;;
esac
else
echo "Setting ${setting} in ${ini_file} has been changed from the default, so will not be updated."
fi
else
if grep -qE "^# ?${setting} = <[A-Za-z_]+>$" "$ini_file"; then
replaceline "^# ?${setting}.*" "${setting} = ${new_value}" "$ini_file"
echo "Updated default settings from package installation for ${setting} in ${ini_file}."
fi
fi
fi
done
done
else
echo "Error: Unable to find PuppetDB configuration file at ${ini_file} so unable to provide automatic configuration for that file."
echo
echo " Confirm the file exists before running the tool again."
echo " The file should have been created automatically when"
echo " the package was installed."
fi
done
echo "cleaning up old config files"
old_config_dir=/etc/puppetdb/conf.d
old_jetty_file=${old_config_dir}/jetty.ini
old_database_file=${old_config_dir}/database.ini
old_config_file=${old_config_dir}/config.ini
old_ini_files=($old_jetty_file $old_database_file $old_config_file)
for old_ini_file in "${old_ini_files[@]}" ; do
if [ -e "$old_ini_file" ] ; then
echo "Removing $old_ini_file"
rm "$old_ini_file"
fi
if [ -e "${old_ini_file}.rpmsave" ] ; then
echo "Removing ${old_ini_file}.rpmsave"
rm "${old_ini_file}.rpmsave"
fi
done
echo "Modifications may have been made to PuppetDB config files. To ensure these changes have been picked up, please do the following: "
echo " 1) upgrade the puppetdb-termini package"
echo " 2) restart the puppetdb service"
echo " 3) restart the puppetserver service"
|