/[osn-commons]/dist/distupload
ViewVC logotype

Contents of /dist/distupload

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations)
Thu Aug 1 17:02:25 2024 UTC (8 months ago) by rakinar2
File size: 8671 byte(s)
feat: add distupload helper script
1 #!/bin/sh
2 #
3 # distupload -- a script to upload software distributions to a server
4 #
5 # This script is part of OSN Commons. Copyright (C) 2024 OSN, Inc.
6 #
7 # OSN Commons is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # OSN Commons is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with OSN Commons. If not, see <https://www.gnu.org/licenses/>.
19 #
20
21 me=$0
22 canonical_name=$(echo $me | rev | cut -d'/' -f1 | rev)
23
24 if [ $(echo $me | cut -c1) = "/" ]; then
25 me=$canonical_name
26 fi
27
28 version="1.0.0"
29
30 check_requirements() {
31 if [ -z "$(command -v grep)" ]; then
32 echo "$me: GNU grep is required to use this script" >&2
33 exit 1
34 fi
35 }
36
37 show_help() {
38 cat <<EOF
39 Usage:
40 $me [options] <file...>
41
42 Options:
43 -h, --help Display this help and exit
44 -v, --version Display version information and exit
45 -r, --remote=REMOTE Specify the remote domain to connect to.
46 Defaults to 'ssh.onesoftnet.eu.org'.
47 -m, --method=METHOD Specify the method to use to upload the files.
48 Supported methods are: scp (default), ftp, sftp
49 -u, --username=[USERNAME] Set your username to authenticate with the
50 remote.
51 If no parameter is passed to this option, it
52 will default to your current system username.
53 -p, --password=PASSWORD Specify a password to authenticate as USERNAME,
54 when required.
55 Note that when the METHOD is set to scp, you
56 must specify the password when scp explicitly
57 asks you in your terminal, command line options
58 have no effect in this case.
59 -l, --location=LOCATION Specify a location where the files will be
60 uploaded on the remote server.
61 When using ftp as METHOD, the server might have
62 chroot confinement, so the "/" might point to a
63 different directory. In that case, specify a
64 location accordingly.
65 -q, --quiet Do not print any output generated from
66 ftp/sftp/scp.
67
68 Feedback, bug reports and general questions should be sent
69 to <[email protected]>.
70 EOF
71 }
72
73 show_version() {
74 cat <<EOF
75 $canonical_name v$version (OSN Commons v$version)
76
77 This program is free software: you can redistribute it and/or modify it
78 under the terms of the GNU General Public License as published by
79 the Free Software Foundation, either version 3 of the License,
80 or (at your option) any later version.
81
82 Written by Ar Rakin.
83 EOF
84 }
85
86 check_optarg() {
87 if [ -z "$2" ]; then
88 echo "$me: option '$1' requires an argument" >&2
89 echo "Try '$me --help' for more information." >&2
90 exit 1
91 fi
92 }
93
94 validate_domain() {
95 pattern='^[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])?)*$'
96
97 if echo "$1" | grep -E "^$pattern$" >/dev/null 2>&1; then
98 return
99 fi
100
101 echo "$me: invalid domain '$1'" >&2
102 exit 1
103 }
104
105 check_command() {
106 if [ -z "$(command -v $1)" ]; then
107 echo "$me: could not find $1 in \$PATH" >&2
108 echo "$me: please install $1 to use this script" >&2
109 exit 1
110 fi
111 }
112
113 check_files() {
114 local output="";
115
116 for file in "$@"; do
117 output=$(stat "$file" 2>&1)
118
119 if [ $? -ne 0 ]; then
120 local msg=$(echo $output | cut -c 7-)
121 echo "$me: $msg" >&2
122 exit 1
123 fi
124 done
125 }
126
127 check_requirements
128
129 remote="ssh.onesoftnet.eu.org"
130 method="scp"
131 username=""
132 password=""
133 files=""
134 location="/srv/www"
135 quiet=0
136
137 posargs=0
138
139 while [ $# -gt 0 ]; do
140 ended=0
141
142 if [ $posargs -eq 0 ]; then
143 case "$1" in
144 -h|-\?|--help)
145 show_help
146 exit
147 ;;
148 -v|--version)
149 show_version
150 exit
151 ;;
152 -m|--method)
153 check_optarg $@
154 method="$2"
155 shift 2
156 ;;
157 -q|--quiet)
158 quiet=1
159 shift
160 ;;
161 -r|--remote)
162 check_optarg $@
163 validate_domain "$2"
164 remote="$2"
165 shift 2
166 ;;
167 -l|--location)
168 check_optarg $@
169 location="$2"
170 shift 2
171 ;;
172 -p|--password)
173 check_optarg $@
174 password="$2"
175 shift 2
176 ;;
177 -u|--username)
178 if [ -z "$2" ]; then
179 username="$USER"
180 else
181 username="$2"
182 fi
183
184 shift 2
185 ;;
186 --)
187 posargs=1
188 shift
189 ;;
190 -*)
191 echo "$me: invalid option -- '$1'" >&2
192 echo "Try '$me --help' for more information" >&2
193 exit 1
194 ;;
195 *)
196 ended=1
197 ;;
198 esac
199 else
200 ended=1
201 fi
202
203 if [ $ended -eq 1 ]; then
204 if [ ! -z "$files" ]; then
205 files="$files "
206 fi
207
208 if echo "$file_name" | grep -q "[\ \'\"]"; then
209 echo "$me: file names must not contain spaces or quotes" >&2
210 exit 1
211 fi
212
213 files="$files$1"
214 shift
215 fi
216 done
217
218 if [ -z "$files" ]; then
219 echo "$me: missing file operand" >&2
220 echo "Try '$me --help' for more information." >&2
221 exit 1
222 fi
223
224 check_files "$files"
225
226 target="$remote"
227
228 if [ ! -z "$username" ]; then
229 target="$username@$target"
230 fi
231
232 echo "$me: using $method to upload files to $target:$location"
233
234 if [ ! -z "$username" ]; then
235 echo "$me: authenticating as $username"
236 fi
237
238 echo "$me: files to upload: $files"
239
240 time_start=$(date +%s)
241
242 case $method in
243 scp)
244 check_command scp
245
246 if [ ! -z "$password" ]; then
247 echo "$me: cannot make any use of the password provided via command line options when using scp"
248 echo "$me: please enter the password when asked by scp"
249 fi
250
251 if [ $quiet -eq 1 ]; then
252 scp -- $files "$target:$location" > /dev/null 2>&1
253 else
254 scp -- $files "$target:$location"
255 fi
256
257 code=$?
258
259 if [ $code -ne 0 ]; then
260 echo "$me: scp failed with exit code $code" >&2
261 exit 1
262 fi
263 ;;
264
265 ftp|sftp)
266 check_command $method
267
268 if [ -z "$username" ]; then
269 echo "$me: no username provided, using 'anonymous' as default"
270 username=anonymous
271 fi
272
273 batch_file=$(mktemp)
274
275 if [ $? -ne 0 ]; then
276 echo "$me: failed to create temporary file" >&2
277 exit 1
278 fi
279
280 if [ "$method" = "ftp" ]; then
281 echo "user \"$username\" \"$password\"" > "$batch_file"
282 echo "passive on" >> "$batch_file"
283 fi
284
285 echo "cd $location" >> "$batch_file"
286
287 for file in $files; do
288 echo "put $file" >> "$batch_file"
289 done
290
291 echo "bye" >> "$batch_file"
292
293 if [ "$method" = "sftp" ]; then
294 if [ $quiet -eq 1 ]; then
295 sftp -oBatchMode=no -b "$batch_file" "$target" > /dev/null 2>&1
296 else
297 sftp -oBatchMode=no -b "$batch_file" "$target"
298 fi
299 else
300 if [ $quiet -eq 1 ]; then
301 cat "$batch_file" | $method -inv "$remote" > /dev/null 2>&1
302 else
303 cat "$batch_file" | $method -inv "$remote"
304 fi
305 fi
306
307 code=$?
308
309 if [ $code -ne 0 ]; then
310 echo "$me: $method failed with exit code $code" >&2
311 exit 1
312 fi
313 ;;
314
315 *)
316 echo "$me: unsupported method '$method'" >&2
317 exit 1
318 ;;
319 esac
320
321 time_end=$(date +%s)
322 time_diff=$((time_end - time_start))
323
324 echo "$me: upload complete in ${time_diff}s"

Properties

Name Value
svn:executable *

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26