1
|
#!/bin/sh
|
2
|
# version : 2012-06-07
|
3
|
# author : CMRI zhangzhihong
|
4
|
#
|
5
|
|
6
|
|
7
|
# if CACHE_ANY is set, image donwloaded will be cached locally
|
8
|
CACHE_ANY="true"
|
9
|
|
10
|
SCRIPT_NAME=`basename $0`
|
11
|
SED="sed -r"
|
12
|
|
13
|
|
14
|
# Log function that knows how to deal with severities and adds the
|
15
|
# script name
|
16
|
function log_function
|
17
|
{
|
18
|
echo "$1: $SCRIPT_NAME: $2" 1>&2
|
19
|
}
|
20
|
|
21
|
# Logs an info message
|
22
|
function log_info
|
23
|
{
|
24
|
log_function "INFO" "$1"
|
25
|
}
|
26
|
|
27
|
# Logs a message, alias to log_info
|
28
|
function log
|
29
|
{
|
30
|
log_info "$1"
|
31
|
}
|
32
|
|
33
|
# Logs an error message
|
34
|
function log_error
|
35
|
{
|
36
|
log_function "ERROR" "$1"
|
37
|
}
|
38
|
|
39
|
|
40
|
# This function is used to pass error message to the mad
|
41
|
function error_message
|
42
|
{
|
43
|
(
|
44
|
echo "ERROR MESSAGE --8<------"
|
45
|
echo "$1"
|
46
|
echo "ERROR MESSAGE ------>8--"
|
47
|
) 1>&2
|
48
|
}
|
49
|
|
50
|
# Executes a command, if it fails returns error message and exits
|
51
|
# If a second parameter is present it is used as the error message when
|
52
|
# the command fails
|
53
|
function exec_and_log
|
54
|
{
|
55
|
message=$2
|
56
|
output=`$1 2>&1 1>/dev/null`
|
57
|
code=$?
|
58
|
if [ "x$code" != "x0" ]; then
|
59
|
log_error "Command \"$1\" failed."
|
60
|
log_error "$output"
|
61
|
if [ -z "$message" ]; then
|
62
|
error_message "$output"
|
63
|
else
|
64
|
error_message "$message"
|
65
|
fi
|
66
|
exit $code
|
67
|
fi
|
68
|
log "Executed \"$1\"."
|
69
|
}
|
70
|
|
71
|
|
72
|
# Gets the host from an argument
|
73
|
function arg_host
|
74
|
{
|
75
|
echo $1 | $SED 's/^([^:]*):.*$/\1/'
|
76
|
}
|
77
|
|
78
|
# Gets the path from an argument
|
79
|
function arg_path
|
80
|
{
|
81
|
echo $1 | $SED 's/^[^:]*:(.*)$/\1/'
|
82
|
}
|
83
|
function arg_first_dir
|
84
|
{
|
85
|
temp="/`echo $1 | cut -d/ -f2`"
|
86
|
echo $temp
|
87
|
}
|
88
|
|
89
|
#try to lock image file
|
90
|
#return 0 if success lock file
|
91
|
#return 1 if file is locked by others
|
92
|
function lock_file
|
93
|
{
|
94
|
lockfile -r 0 ${1}.lock >/dev/null 2>&1
|
95
|
return $?
|
96
|
}
|
97
|
|
98
|
function release_lock
|
99
|
{
|
100
|
exec_and_log "rm -f ${1}.lock" "the lock is released by others!!!"
|
101
|
}
|
102
|
|
103
|
|
104
|
function mv_image
|
105
|
{
|
106
|
exec_and_log "mv $1 $2"
|
107
|
}
|
108
|
|
109
|
function download_image
|
110
|
{
|
111
|
exec_and_log "wget $1 $2 $3"
|
112
|
}
|
113
|
|
114
|
function prefetch_cache_image
|
115
|
{
|
116
|
LOCAL_CACHE_IMAGE=$1
|
117
|
CACHE_IMAGE=$2
|
118
|
if [ -f $LOCAL_CACHE_IMAGE ] && lock_file $CACHE_IMAGE
|
119
|
then
|
120
|
exec_and_log "cp $LOCAL_CACHE_IMAGE $CACHE_IMAGE"
|
121
|
release_lock $CACHE_IMAGE
|
122
|
fi
|
123
|
}
|
124
|
|
125
|
function prefetch_local_cache
|
126
|
{
|
127
|
OPT=$1
|
128
|
LOCAL_CACHE_IMAGE=$2
|
129
|
SOURCE=$3
|
130
|
CACHE_IMAGE=$4
|
131
|
if lock_file $LOCAL_CACHE_IMAGE
|
132
|
then
|
133
|
exec_and_log "wget $OPT $LOCAL_CACHE_IMAGE $SOURCE "
|
134
|
release_lock $LOCAL_CACHE_IMAGE
|
135
|
prefetch_cache_image $LOCAL_CACHE_IMAGE $CACHE_IMAGE
|
136
|
fi
|
137
|
}
|
138
|
|
139
|
|
140
|
if [ $# -lt 3 ]
|
141
|
then
|
142
|
echo "usage $0 -O TARGET SOURCE_URL"
|
143
|
exit 1
|
144
|
fi
|
145
|
#main
|
146
|
OPT=$1
|
147
|
TARGET=$2
|
148
|
SOURCE=$3
|
149
|
|
150
|
SOURCE_IMAGE=`basename $SOURCE`
|
151
|
CACHE_DIR="`arg_first_dir $TARGET`/.cache"
|
152
|
CACHE_IMAGE="${CACHE_DIR}/${SOURCE_IMAGE}"
|
153
|
LOCAL_CACHE_DIR="`arg_first_dir $TARGET`/.local_cache"
|
154
|
LOCAL_CACHE_IMAGE="${LOCAL_CACHE_DIR}/${SOURCE_IMAGE}"
|
155
|
|
156
|
if [ ! -d $CACHE_DIR ] || [ ! -d $LOCAL_CACHE_DIR ]
|
157
|
then
|
158
|
mkdir -p $CACHE_DIR
|
159
|
mkdir -p $LOCAL_CACHE_DIR
|
160
|
fi
|
161
|
|
162
|
if [ -f $CACHE_IMAGE ] && lock_file $CACHE_IMAGE
|
163
|
then
|
164
|
mv_image $CACHE_IMAGE $TARGET
|
165
|
release_lock $CACHE_IMAGE
|
166
|
prefetch_cache_image $LOCAL_CACHE_IMAGE $CACHE_IMAGE >/dev/null 2>&1 &
|
167
|
else
|
168
|
if [ -f $LOCAL_CACHE_IMAGE ] && lock_file $LOCAL_CACHE_IMAGE
|
169
|
then
|
170
|
release_lock $LOCAL_CACHE_IMAGE
|
171
|
exec_and_log "cp $LOCAL_CACHE_IMAGE $TARGET"
|
172
|
prefetch_cache_image $LOCAL_CACHE_IMAGE $CACHE_IMAGE >/dev/null 2>&1 &
|
173
|
else
|
174
|
download_image $OPT $TARGET $SOURCE
|
175
|
if [ "$CACHE_ANY"=="true" ]
|
176
|
then
|
177
|
prefetch_local_cache $OPT $LOCAL_CACHE_IMAGE $SOURCE $CACHE_IMAGE >/dev/null 2>&1 &
|
178
|
fi
|
179
|
fi
|
180
|
fi
|
181
|
|