wmsupload 5.16 KB
Newer Older
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
#!/bin/sh
################################################################################
# DO NOT ALTER THIS LINE
VERSION=15
################################################################################

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() {
	DL_URL="http://${SERVER}/wmsupload"
	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
WMS_FORCE=0
WMS_PART=0
WMS_PROXY=0
WMS_VERBOSE=0
if [ -z "$1" ]; then
	showHelp
	exit 0
fi
while getopts hufprvs: o; do
	case "$o" in
	h)	showHelp
		exit 0
		;;
	u)	checkUpdate
		exit 0
		;;
	f)	WMS_FORCE=1
		;;
	p)	WMS_PART=1
		;;
	r)	WMS_PROXY=1
		;;
	v)	WMS_VERBOSE=1
		;;
	s)	SERVER=${OPTARG}
		;;
	*)	showHelp
		exit 1
		;;
	esac
done
shift $((${OPTIND} -1))

# Set our variables
WMS_FILE=$1
WMS_NAME=$2
WMS_DATE=$3
WMS_TIME=$4

# Does the file exist?
if [ ! -f "${WMS_FILE}" ]; then
	echo "ERROR: Could not find file: ${WMS_FILE}"
	exit 1
fi

# Set the defaults for sending
WMS_FILE_DIR=`dirname "${WMS_FILE}"`
WMS_FILE_NAME=`basename "${WMS_FILE}"`

# Verify the product name
if [ -n "${WMS_NAME}" ]; then
	match=`expr "${WMS_NAME}" : '\([a-zA-Z0-9\-]\{1,\}\)'`
	if [ "$match" != "${WMS_NAME}" ]; then
		echo ""
		echo "ERROR: Invalid product name"
		showHelp
		exit 1
	fi
fi

# Verify the product date
if [ -n "${WMS_DATE}" ]; then
	match=`expr "${WMS_DATE}" : '\([0-9]\{8\}\)'`
	if [ "$match" != "${WMS_DATE}" ]; then
		echo ""
		echo "ERROR: Invalid product date"
		showHelp
		exit 1
	fi
fi

# Verify the product time
if [ -n "${WMS_TIME}" ]; then
	match=`expr "${WMS_TIME}" : '\([0-9]\{6\}\)'`
	if [ "$match" != "${WMS_TIME}" ]; then
		echo ""
		echo "ERROR: Invalid product time"
		showHelp
		exit 1
	fi
fi

# Get the direct upload name (unless -r was specified)
if [ ${WMS_PROXY} -eq 0 ]; then
	SERVER_DIRECT=
	if [ -n "${WMS_NAME}" ]; then
		SERVER_DIRECT=`${CURL} -s http://${SERVER}/wmsupload?name=${WMS_NAME}`
	else
		SERVER_DIRECT=`${CURL} -s http://${SERVER}/wmsupload?file=${WMS_FILE_NAME}`
	fi
	if [ -n "${SERVER_DIRECT}" ]; then
		SERVER=${SERVER_DIRECT}
	else
		if [ ${WMS_VERBOSE} -ne 0 ]; then
			echo "WARNING: Could not determine the head node URL for proxy upload"
		fi
	fi
fi

# Change to the dir with the file
cd "${WMS_FILE_DIR}"
echo "Connecting to ${SERVER}..."

# Check if the server is ready to receive the file
if [ ${WMS_VERBOSE} -ne 0 ]; then
	echo "Checking upload availability"
fi
BYTES=`/bin/ls -Lln "${WMS_FILE_NAME}" |awk '{print $5}'`
COMMAND="${CURL} -s http://${SERVER}/wmsupload?bytes=${BYTES}"
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
	if [ ${WMS_VERBOSE} -ne 0 ]; then
		echo "  Server does not understand file size check, continuing..."
	fi
fi

# Send the file
echo "Sending ${WMS_FILE_NAME}"
COMMAND="${TIMEOUT} ${CURL} --max-time 600 -s http://${SERVER}/upload/ -F file=@${WMS_FILE_NAME}"
if [ -n "${WMS_NAME}" ]; then
	COMMAND="${COMMAND} -F name=${WMS_NAME}"
fi
if [ -n "${WMS_DATE}" ]; then
	COMMAND="${COMMAND} -F date=${WMS_DATE}"
fi
if [ -n "${WMS_TIME}" ]; then
	COMMAND="${COMMAND} -F time=${WMS_TIME}"
fi
if [ ${WMS_PART} -ne 0 ]; then
	COMMAND="${COMMAND} -F part=1"
fi
if [ ${WMS_FORCE} -ne 0 ]; then
	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}