re_upload 5.14 KB
Newer Older
1
2
3
#!/bin/sh
################################################################################
# DO NOT ALTER THIS LINE
4
VERSION=16
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
################################################################################

SERVER=realearth.ssec.wisc.edu:80

CURL=`which curl 2>/dev/null`
TIMEOUT_BIN=`which timeout 2>/dev/null`
TIMEOUT=
if [ -n "${TIMEOUT_BIN}" ]; then
	TIMEOUT="${TIMEOUT_BIN} -s 9 600"
fi

showHelp() {
	echo ""
	echo "$0 [-hufprv] [-s server:port] file [name] [date] [time]"
	echo ""
	echo "   -h: Show help"
	echo "   -u: Check for update"
	echo ""
	echo "   -f: Force overwrite if there is a conflict"
	echo "   -p: Designate file as part of a larger product"
	echo ""
	echo "   -r: Send through head node proxy"
	echo "   -s: Specify the server and port ([server:port] required)"
	echo ""
	echo "   -v: Be verbose"
	echo ""
	echo " file: Path to file"
	echo "       Format: /path/to/[name]_[YYYYMMDD]_[HHMMSS].???"
	echo ""
	echo " name: Specify the product name"
	echo "       Required when the file name does not contain [name]"
	echo "       Format: Cannot contain '_'"
	echo " date: Specify the date"
	echo "       Required when the file name does not contain [date]"
	echo "       Format: YYYYMMDD"
	echo " time: Specify the time"
	echo "       Required when the file name does not contain [time]"
	echo "       Format: HHMMSS"
	echo ""
}

checkUpdate() {
47
	DL_URL="http://${SERVER}/upload/re_upload"
48
49
50
51
52
53
54
55
56
57
58
59
60
	URL="$DL_URL?version=${VERSION}"
	VERSION_CHECK=`${CURL} -s ${URL}`
	if [ -n "${VERSION_CHECK}" ]; then
		echo "A new version is available at:"
		echo "    ${DL_URL}"
		echo "Download with:"
		echo "    ${CURL} ${DL_URL} -o $0"
	else
		echo "This version is up to date"
	fi
}

# Get the options from the command line
61
62
63
64
RE_FORCE=0
RE_PART=0
RE_PROXY=0
RE_VERBOSE=0
65
66
67
68
69
70
71
72
73
74
75
76
if [ -z "$1" ]; then
	showHelp
	exit 0
fi
while getopts hufprvs: o; do
	case "$o" in
	h)	showHelp
		exit 0
		;;
	u)	checkUpdate
		exit 0
		;;
77
	f)	RE_FORCE=1
78
		;;
79
	p)	RE_PART=1
80
		;;
81
	r)	RE_PROXY=1
82
		;;
83
	v)	RE_VERBOSE=1
84
85
86
87
88
89
90
91
92
93
94
		;;
	s)	SERVER=${OPTARG}
		;;
	*)	showHelp
		exit 1
		;;
	esac
done
shift $((${OPTIND} -1))

# Set our variables
95
96
97
98
RE_FILE=$1
RE_NAME=$2
RE_DATE=$3
RE_TIME=$4
99
100

# Does the file exist?
101
102
if [ ! -f "${RE_FILE}" ]; then
	echo "ERROR: Could not find file: ${RE_FILE}"
103
104
105
106
	exit 1
fi

# Set the defaults for sending
107
108
RE_FILE_DIR=`dirname "${RE_FILE}"`
RE_FILE_NAME=`basename "${RE_FILE}"`
109
110

# Verify the product name
111
112
113
if [ -n "${RE_NAME}" ]; then
	match=`expr "${RE_NAME}" : '\([a-zA-Z0-9\-]\{1,\}\)'`
	if [ "$match" != "${RE_NAME}" ]; then
114
115
116
117
118
119
120
121
		echo ""
		echo "ERROR: Invalid product name"
		showHelp
		exit 1
	fi
fi

# Verify the product date
122
123
124
if [ -n "${RE_DATE}" ]; then
	match=`expr "${RE_DATE}" : '\([0-9]\{8\}\)'`
	if [ "$match" != "${RE_DATE}" ]; then
125
126
127
128
129
130
131
132
		echo ""
		echo "ERROR: Invalid product date"
		showHelp
		exit 1
	fi
fi

# Verify the product time
133
134
135
if [ -n "${RE_TIME}" ]; then
	match=`expr "${RE_TIME}" : '\([0-9]\{6\}\)'`
	if [ "$match" != "${RE_TIME}" ]; then
136
137
138
139
140
141
142
143
		echo ""
		echo "ERROR: Invalid product time"
		showHelp
		exit 1
	fi
fi

# Get the direct upload name (unless -r was specified)
144
if [ ${RE_PROXY} -eq 0 ]; then
145
	SERVER_DIRECT=
146
147
	if [ -n "${RE_NAME}" ]; then
		SERVER_DIRECT=`${CURL} -s http://${SERVER}/upload/re_upload?name=${RE_NAME}`
148
	else
149
		SERVER_DIRECT=`${CURL} -s http://${SERVER}/upload/re_upload?file=${RE_FILE_NAME}`
150
151
152
153
	fi
	if [ -n "${SERVER_DIRECT}" ]; then
		SERVER=${SERVER_DIRECT}
	else
154
		if [ ${RE_VERBOSE} -ne 0 ]; then
155
156
157
158
159
160
			echo "WARNING: Could not determine the head node URL for proxy upload"
		fi
	fi
fi

# Change to the dir with the file
161
cd "${RE_FILE_DIR}"
162
163
164
echo "Connecting to ${SERVER}..."

# Check if the server is ready to receive the file
165
if [ ${RE_VERBOSE} -ne 0 ]; then
166
167
	echo "Checking upload availability"
fi
168
169
BYTES=`/bin/ls -Lln "${RE_FILE_NAME}" |awk '{print $5}'`
COMMAND="${CURL} -s http://${SERVER}/upload/re_upload?bytes=${BYTES}"
170
171
172
173
174
175
176
177
178
179
180
181
SUCCESS=`${COMMAND} -o - 2>/dev/null |head -1`
if [ "${SUCCESS}" -eq "${SUCCESS}" ] 2>/dev/null; then
	if [ "${SUCCESS}" = "-1" ]; then
		echo "  Server cannot accept the file, it is too large!"
		exit 3
	fi
	while [ "${SUCCESS}" != "1" ]; do
		echo "  Server cannot accept the file at this time, trying again in 15 seconds..."
		sleep 15
		SUCCESS=`${COMMAND} -o - |head -1`
	done
else
182
	if [ ${RE_VERBOSE} -ne 0 ]; then
183
184
185
186
187
		echo "  Server does not understand file size check, continuing..."
	fi
fi

# Send the file
188
189
190
191
echo "Sending ${RE_FILE_NAME}"
COMMAND="${TIMEOUT} ${CURL} --max-time 600 -s http://${SERVER}/upload/ -F file=@${RE_FILE_NAME}"
if [ -n "${RE_NAME}" ]; then
	COMMAND="${COMMAND} -F name=${RE_NAME}"
192
fi
193
194
if [ -n "${RE_DATE}" ]; then
	COMMAND="${COMMAND} -F date=${RE_DATE}"
195
fi
196
197
if [ -n "${RE_TIME}" ]; then
	COMMAND="${COMMAND} -F time=${RE_TIME}"
198
fi
199
if [ ${RE_PART} -ne 0 ]; then
200
201
	COMMAND="${COMMAND} -F part=1"
fi
202
if [ ${RE_FORCE} -ne 0 ]; then
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	COMMAND="${COMMAND} -F force=1"
fi

# Retry a few times...
RETRIES=3
${COMMAND}
LASTEXIT=$?
while [ ${LASTEXIT} -ne 0 -a ${RETRIES} -gt 0 ]; do
	echo "Curl command failed: ${LASTEXIT}"
	SLEEP=$((${RANDOM} * 30 / 32767))
	echo "Sleeping for ${SLEEP} seconds..."
	sleep ${SLEEP}
	echo "Trying again..."
	${COMMAND}
	LASTEXIT=$?
	RETRIES=$((${RETRIES} - 1))
done
if [ ${LASTEXIT} -eq 0 ]; then
	echo "Done"
else
	echo "Giving up"
fi
exit ${LASTEXIT}