| 1 | #!@BASH@ |
|---|
| 2 | # |
|---|
| 3 | # Copyright (c) 2008, Christopher Cowart and contributors |
|---|
| 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # Redistribution and use in source and binary forms, with or without |
|---|
| 7 | # modification, are permitted provided that the following conditions |
|---|
| 8 | # are met: |
|---|
| 9 | # * Redistributions of source code must retain the above copyright |
|---|
| 10 | # notice, this list of conditions and the following disclaimer. |
|---|
| 11 | # * Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | # documentation and/or other materials provided with the distribution. |
|---|
| 14 | # |
|---|
| 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 16 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 17 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 18 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 19 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 20 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
|---|
| 21 | # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|---|
| 22 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|---|
| 23 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|---|
| 24 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 25 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 26 | # |
|---|
| 27 | # $Id$ |
|---|
| 28 | # |
|---|
| 29 | # This script prepares an "export" of the confman repository for system |
|---|
| 30 | # processes to be able to pull. It is intended to be prepared regularly |
|---|
| 31 | # by an automated process like cron. |
|---|
| 32 | |
|---|
| 33 | if [ -r @pkgdatadir@/confmancommon.sh ] ; then |
|---|
| 34 | . @pkgdatadir@/confmancommon.sh |
|---|
| 35 | else |
|---|
| 36 | echo "Can't find confmancommon.sh. Exiting." >&2 |
|---|
| 37 | exit 17 |
|---|
| 38 | fi |
|---|
| 39 | |
|---|
| 40 | # This changes the behavior of some library functions (e.g. URIs, locks) |
|---|
| 41 | CONF_EXPORT=true |
|---|
| 42 | |
|---|
| 43 | function print_help { |
|---|
| 44 | echo "usage: $0 [-f] [-d]" >&4 |
|---|
| 45 | echo "Type '$0 -h' to display this help and exit." >&4 |
|---|
| 46 | conf_cleanExit ${1:-0} |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | FORCE=false |
|---|
| 50 | DEBUG=false |
|---|
| 51 | |
|---|
| 52 | while getopts "fdh" OPT 2>&4 ; do |
|---|
| 53 | case "$OPT" in |
|---|
| 54 | f) FORCE=true ;; |
|---|
| 55 | d) DEBUG=true ;; |
|---|
| 56 | h) print_help 0;; |
|---|
| 57 | *) print_help 1;; |
|---|
| 58 | esac |
|---|
| 59 | done |
|---|
| 60 | |
|---|
| 61 | # If we're in debug mode, we should log stderr messages to the logger |
|---|
| 62 | # as well. Otherwise, we send them to the bit bucket. |
|---|
| 63 | if $DEBUG ; then |
|---|
| 64 | exec 1>&2 |
|---|
| 65 | else |
|---|
| 66 | exec 1>/dev/null |
|---|
| 67 | fi |
|---|
| 68 | |
|---|
| 69 | echo "Export operation started" | conf_logger |
|---|
| 70 | |
|---|
| 71 | confexport_lock_system |
|---|
| 72 | |
|---|
| 73 | umask 077 |
|---|
| 74 | |
|---|
| 75 | case ${CONF_EXPORT_STYLE} in |
|---|
| 76 | repository) |
|---|
| 77 | temp_conf_export=$(conf_tmp_file) |
|---|
| 78 | ;; |
|---|
| 79 | module|recipe) |
|---|
| 80 | temp_conf_export=$(conf_tmp_dir) |
|---|
| 81 | ;; |
|---|
| 82 | *) |
|---|
| 83 | echo "Unsupported export style: ${CONF_EXPORT_STYLE}" >&2 |
|---|
| 84 | exit 1 |
|---|
| 85 | ;; |
|---|
| 86 | esac |
|---|
| 87 | |
|---|
| 88 | if [ -z "${temp_conf_export}" ] ; then |
|---|
| 89 | echo "Could not create tempfile. Exiting." >&2 |
|---|
| 90 | exit 1 |
|---|
| 91 | fi |
|---|
| 92 | |
|---|
| 93 | if [ -z "${CONF_EXPORT_WORK_PATH}" ]; then |
|---|
| 94 | WORK_PATH=$(conf_tmp_dir) |
|---|
| 95 | conf_debug "Using temporary export working copy at ${WORK_PATH}" |
|---|
| 96 | else |
|---|
| 97 | WORK_PATH="${CONF_EXPORT_WORK_PATH}" |
|---|
| 98 | conf_debug "Using persistent export working copy at ${WORK_PATH}" |
|---|
| 99 | fi |
|---|
| 100 | |
|---|
| 101 | conf_checkout_tree |
|---|
| 102 | |
|---|
| 103 | repo_export="${CONF_EXPORT_STYLE}:$(conf_revision_svn)" |
|---|
| 104 | sys_export="$(conf_export_sysrev)" |
|---|
| 105 | |
|---|
| 106 | conf_debug "repo_export: ${repo_export}" |
|---|
| 107 | conf_debug "sys_export: ${sys_export}" |
|---|
| 108 | |
|---|
| 109 | if [ "${sys_export}" = "${repo_export}" ]; then |
|---|
| 110 | if ! ${FORCE}; then |
|---|
| 111 | echo "System is up to date." | conf_logger |
|---|
| 112 | conf_cleanExit 0 |
|---|
| 113 | fi |
|---|
| 114 | fi |
|---|
| 115 | |
|---|
| 116 | repo_method=$(echo ${repo_export} | cut -d ':' -f 1) |
|---|
| 117 | sys_method=$(echo ${sys_export} | cut -d ':' -f 1) |
|---|
| 118 | repo_revision=$(echo ${repo_export} | cut -d ':' -f 2) |
|---|
| 119 | sys_revision=$(echo ${sys_export} | cut -d ':' -f 2) |
|---|
| 120 | |
|---|
| 121 | if [ "${sys_method}" != "${repo_method}" ]; then |
|---|
| 122 | echo "Export style change detected: ${sys_method} -> ${repo_method}" |
|---|
| 123 | if ${CONF_EXPORT_INCREMENTAL}; then |
|---|
| 124 | echo "Ignoring CONF_EXPORT_INCREMENTAL during export style change" |
|---|
| 125 | CONF_EXPORT_INCREMENTAL="false" |
|---|
| 126 | fi |
|---|
| 127 | fi |
|---|
| 128 | |
|---|
| 129 | if [ "${sys_revision}" -gt "${repo_revision}" ]; then |
|---|
| 130 | echo "Error: The last export was newer than the current repository. Exiting without making changes." >&2 |
|---|
| 131 | conf_cleanExit 1 |
|---|
| 132 | fi |
|---|
| 133 | |
|---|
| 134 | if ! conf_export "${temp_conf_export}" "${sys_revision}" "${repo_revision}"; then |
|---|
| 135 | echo "Error: The export failed. Exiting." >&2 |
|---|
| 136 | rm -rf "${temp_conf_export}" |
|---|
| 137 | conf_cleanExit 1 |
|---|
| 138 | fi |
|---|
| 139 | |
|---|
| 140 | # Prepare the new export |
|---|
| 141 | chown -R ${CONF_EXPORT_USER}:${CONF_EXPORT_GROUP} "${temp_conf_export}" || exit 1 |
|---|
| 142 | chmod -R ${CONF_EXPORT_MODE} "${temp_conf_export}" || exit 1 |
|---|
| 143 | |
|---|
| 144 | # Switch to the new export |
|---|
| 145 | old_conf_export=$(conf_tmp_dir) |
|---|
| 146 | if [ -e "${CONF_EXPORT_FILE}" ]; then |
|---|
| 147 | mv "${CONF_EXPORT_FILE}" "${old_conf_export}" || exit 1 |
|---|
| 148 | fi |
|---|
| 149 | mv "${temp_conf_export}" "${CONF_EXPORT_FILE}" |
|---|
| 150 | |
|---|
| 151 | conf_record_export |
|---|
| 152 | |
|---|
| 153 | echo "Export operation finished successfully" | conf_logger |
|---|
| 154 | |
|---|
| 155 | confexport_unlock_system |
|---|
| 156 | |
|---|
| 157 | # Remove the old export |
|---|
| 158 | rm -rf "${old_conf_export}" |
|---|
| 159 | |
|---|
| 160 | # If CONF_EXPORT_WORK_PATH is not set, we were using a temporary |
|---|
| 161 | # directory, so clean it up |
|---|
| 162 | if [ -z "${CONF_EXPORT_WORK_PATH}" ]; then |
|---|
| 163 | rm -rf "${WORK_PATH}" |
|---|
| 164 | fi |
|---|
| 165 | |
|---|
| 166 | conf_cleanExit 0 |
|---|