~launchpad-baby-gnu/ubumirror/feature-set-config-and-log-files-from-command-line

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
#!/bin/sh
#
#   ubucdimage - provides an easy way to sync daily ISO files from an
#                Ubuntu CD image mirror.
#
#   Copyright (C) 2006 - 2009 Nafallo Bjälevik
#
#   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/>.
#

#------------------------------------------------------------------------
# Changes:
#    0.3ubuntu3  Manage command line options

#------------------------------------------------------------------------
# Usage: ubucdimage [OPTION... ]
#
# Provides an easy way to sync daily ISO files from an Ubuntu CD image mirror.
#
# Options:
# --------
#
#     -c, --conf  <filename>  Use the configuration file ”<filename>”.
#                             Default: “/etc/ubumirror.conf"
#     -l, --log   <filename>  Write log to file “<filename>”.
#                             Default: “/etc/ubumirror.conf"
#     -h, --help              Show this message
#     -v, --version           Display version and copyright information
#         --copyright         Display copyright information
#         --license           Display license information
#         --changes           Display ChangeLog information
#
# Mandatory dependencies:
# -----------------------
# * rsync
# * “sh” like shell
# * getopt
# * sed
#
# Optional dependencies:
# ----------------------
# None
#
# Bugs:
# ----
# Report bug to https://bugs.launchpad.net/ubuntu/+source/ubumirror

set -e

SELF_PATH=$(readlink -e "${0}")
SELF=$(basename "${SELF_PATH}")

usage() {
    sed -n '/^# Usage/,/^$/ {s/^#[[:space:]]\?//p}' "${SELF_PATH}"
}
description() {
    self_version
    echo -n "ubuarchive ${SELF_VERSION} - "
    usage | sed -n '/^Usage/,/^Options/{//b; /^$/d; p}'
}
license() {
    description
    echo
    sed -n '/^#[[:space:]]*Copyright/,/^$/ s/^#[[:space:]]\{0,3\}//p' "${SELF_PATH}"
}
changes() {
    echo 'ubuarchive\n'
    sed -n '/^#[[:space:]]*Changes/,/^$/ s/^#[[:space:]]\?//p' "${SELF_PATH}"
}
self_version() {
    [ -z "${SELF_VERSION}" ] || return
    SELF_VERSION=$(changes \
        | sed -n '/^[[:space:]]\+[0-9.]*/ {s/^[[:space:]]\+\([^[:space:]]\+\).*/\1/p; q}')
}

#------------------------------------------------------------------------
# Global variables:
#
# Use “export” to make them available to subprocesses
#
# Default log file
CONF=/etc/ubumirror.conf
LOGDIR=/var/log/ubumirror
LOGFILE="$LOGDIR/ubucdimage.log"

#------------------------------------------------------------------------
# Options
TEMP=$(getopt -o c:l:hv --long conf:,log:,help,version,copyright,license,changes -- "$@")

test $? = 0 || exit 1
eval set -- "${TEMP}"

while true
do
    case "${1}" in
        # Default options for utilities
        -h|--help)
            usage
            exit 0
            ;;
        -v|--version)
            license | head -n 3
            exit 0
            ;;
        --copyright)
            license | head -n +3
            exit 0
            ;;
        --license)
            license
            exit 0
            ;;
        --changes)
            changes
            exit 0
            ;;

        # Program options
        -c|--conf)
            CONF="${2}"
            shift 2
            ;;
        -l|--log)
            LOGDIR=$(dirname "${2}")
            LOGFILE="${2}"
            shift 2
            ;;
        # End of options
        --)
            shift
            break
            ;;
        *)
            echo "Error: unknown argument “${1}”"
            exit 1
            ;;
    esac
done

if [ ! -f "${CONF}" ]; then
    echo "Configuration file “${CONF}” not found."
    exit 2
fi

if [ ! -w "${LOGDIR}" ]; then
    echo "Log directory “${LOGDIR}” not writable."
    exit 2
fi

if [ -f "${LOGFILE}" -a ! -w "${LOGFILE}" ]; then
    echo "Log file “${LOGFILE}” exists but is not writable."
    exit 2
fi

. ${CONF}

# Log all activity to file.
exec >> $LOGFILE 2>&1

if [ -z "$UBUCDI_DIR" ]; then
    echo "No Ubuntu cdimage target directory (UBUCDI_DIR) set in “${CONF}”"
    exit 2
fi

LOCK="${UBUCDI_DIR}/Archive-Update-in-Progress-${HOSTNAME}"

trap 'rm -f $LOCK > /dev/null 2>&1; savelog -c 28 -n $LOGFILE > /dev/null' EXIT

# Get in the right directory and set the umask to be group writable
cd $HOME
umask 022

echo "$(date -R): Initiating Ubuntu daily CD image mirror operations..."

# Check to see if another sync is in progress
if ! ( set -o noclobber; echo "$$" > "${LOCK}") 2> /dev/null; then
        if ! $(kill -0 $(cat ${LOCK}) 2>/dev/null); then
                # Process does either not exist or is not owned by us.
                echo "$$" > "${LOCK}"
        else
                echo "$(date -R): Unable to proceed with operations; lock file still exists for PID $(cat ${LOCK})."
                exit 1
        fi
fi

echo "$(date -R): Lock established for process $(cat $LOCK)."

set +e

echo "$(date -R): Initiating cdimage sync..."

mkdir -p $UBUCDI_DIR

rsync -av --partial \
    --timeout=$IO_TIMEOUT \
    --bwlimit=$SPEED \
    --exclude ".trace/${HOSTNAME}" \
    $UBUCDI_EXCLUDE \
    $UBUCDI_MIRROR $UBUCDI_DIR

if [ $? -ne 0 ]; then
    ( echo "CDimage sync failed. Please check logs."; \
        egrep '^write failed|@ERROR' $LOGFILE ) | mail -s "Ubuntu cdimage sync failed" $EMAIL
    echo "$(date -R): sync for cdimage failed."
    exit 2
fi

echo "$(date -R): sync for cdimage completed."

echo "$(date -R): Releasing lock file..."
rm -f $LOCK > /dev/null 2>&1

echo "$(date -R): Ubuntu daily CD image mirror operations completed."