blob: 75a86f43fac3b7106fc0afaab5d7534abb841937 (
plain) (
tree)
|
|
#!/bin/bash
#################################################################################
# #
# rh-upload #
# Version - 0.3 #
# Copyright (c) 2007 Red Hat, Inc. All rights reserved. #
# #
# #
# Written by David Mair #
# Cleanup and generalized for any kind of file by Olivier Arsac #
# Idea stolen from Chris Snook :-) #
# #
# Purpose - To help in the automatic upload of files. #
# Specifically, this script will compress, encrypt, md5sum, #
# and upload the file automatically when invoked. #
# Items are optional and specified by command line switch. #
# ###############################################################################
## Global directives
umask 0077
## Declare some variables
Date=`/bin/date -u +%G%m%d%k%M%S | /usr/bin/tr -d ' '`
Destination="dropbox.redhat.com"
function usage {
cat <<EOF
rh-upload [-cehnNq] [-s size of hunks in MB] -f filename
rh-upload automates the upload of files to the Red Hat ftp.
The script helps the user to compress, checksum, encrypt and upload a file.
-c|--checksum : perform an md5 checksum on the file
-e|--encrypt : encrypt the file
-f|--file : file to act on (required)
-h|--help : show this usage help
-z|--compress : compress the file
-n|--noupload : Do NOT upload
-q|--quiet : Do everything I ask and do it quietly
-s|--split : split file into small hunks
EOF
}
# echo to stderr
function echoe(){
echo -e "$@" 1>&2
}
# echo unless in verbosity is too low
function echov(){
local level="$1"
shift
if [[ $Verbose -ge "$level" ]]; then echo -e "$@"; fi
}
# Parse command line options (and perform some checks)
function parse(){
if [ $# == 0 ]; then
usage
exit 0
fi
TEMP=`getopt -o heuczqs:f:n --long help,encrypt,quiet,noupload,checksum,compress,split:,file: -n 'rh-upload' -- "$@"`
if [ $? != 0 ]; then
echo "Options error -- Terminating..." >&2
usage
exit 1
fi
eval set -- "$TEMP"
Upload=1
Split=0
Verbose=1
Compress=0
Encrypt=0
Force=0
while true; do
case "$1" in
-h|--help) usage; exit 0;;
-e|--encrypt) Encrypt=1; shift;;
-N|--noupload) Upload=0; shift;;
-c|--checksum) Checksum=1; shift;;
-q|--quiet) Verbose=0; shift;;
-z|--compress) Compress=1; shift;;
-s|--split)
case $2 in
"") echoe "You must specify a hunk size."; exit 1 ;;
*) Split=$2; shift 2;;
esac ;;
-F|--force) Force=1; shift;;
-f|--file)
case "$2" in
"") echoe "You must specify a file name."; exit 1 ;;
*) File=$2; shift 2;;
esac ;;
--) shift; break ;;
*) echoe "Wrong options or flag specified"; usage; exit 1;;
esac
done
# Ensure the -f||--file flag was passed or die
if [ -z "$File" ]; then
echoe "The -f or --file flag is required! Terminating."
usage
exit 1
fi
# Validate the file exists or die
if [ ! -f "$File" ]; then
echoe "Invalid filename or file not found. Terminating."
exit 1
fi
File_dirname=${File%/*}
File_basename=${File##*/}
File_ext=${File_basename##*.}
Src_file="$File"
Dst_file="$File_basename"
}
function ticket(){
echov 1
echov 1 "We'll need to use your issue-tracker ticket number for a couple of things."
echo -n "Please enter ticket number: "
read Ticket_number
Dst_file="${Ticket_number}-${Date}-${File_basename}"
}
# Compress the file
function compress(){
if [ "$Compress" != 0 ]; then
echov 1 "Starting file compression. This may take some time."
# Begin compression of file
if [ ! -x /usr/bin/gzip ]; then
echoe "Cannot find gzip in /usr/bin/. Terminating."
exit 1
else
/usr/bin/gzip --fast "$Src_file"
fi
Src_file="${Src_file}.gz"
Dst_file="${Dst_file}.gz"
fi
}
# Encrypt the file
function encrypt(){
if [ "$Encrypt" != 0 ]; then
echov 1 "Encrypting file. This should only take a few minutes."
if [ ! -x /usr/bin/openssl ]; then
echoe "Cannot find openssl in /usr/bin. Terminating."
exit 1
fi
# Use the ticket number as the ssl keyfile name
Key_file="${Ticket_number}-${Date}.key"
/usr/bin/openssl rand -out "$Key_file" -base64 48
echov 1 "You have chosen to encrypt your file. Your passkey file is: $Key_file"
echov 1 "Please attach this key to your ticket."
/usr/bin/openssl aes-128-cbc -in $Src_file -out $Src_file.aes -pass file:"$Key_file"
Src_file="$Src_file.aes"
Dst_file="$Dst_file.aes"
fi
}
function checksum(){
if [ "$Checksum" != 0 ]; then
echov 1 "Cheksuming $Src_file... (This should only take a few minutes)"
if [ ! -x /usr/bin/md5sum ]; then
echoe "Cannot find md5sum in /usr/bin. Terminating."
exit 1
fi
MD5_file="${Ticket_number}-${Date}-checksum.out"
MD5_result=`/usr/bin/md5sum "$Src_file" | awk '{print $1}'`
echo "$MD5_result" > "$MD5_file"
echov 1 "The MD5 checksum is $MD5_result (in $MD5_file)."
fi
}
function split(){
if [ "$Split" -eq 0 ]; then
return
fi
local hunk_size="$Split"
if [[ "$hunk_size" -gt 0 ]] && [[ "$hunk_size" -lt 1001 ]]; then
if [ ! -x /usr/bin/split ]; then
echoe "Cannot find split in /usr/bin. Terminating."
exit 1
fi
Split_dir="${Ticket_number}-${Date}"
# We need to make a directory to keep things sane
echov 1 "Creating directory $Split_dir to house file hunks..."
/bin/mkdir "$Split_dir"
echov 1 "Splitting $Src_file..."
/usr/bin/split -b "$hunk_size"m -d "$Src_file" "${Split_dir}/${Dst_file}"
else
echoe "Invalid hunk size argument. Please enter a number greater than 0 and less than 1001. Terminating."
exit 1
fi
}
function upload(){
if [ "$Upload" -eq 0 ]; then
echov 1 "All file operations are complete. The file(s) is ready to upload at your convenience."
return
fi
echov 1 "The file(s) will now be uploaded."
echo -n "Please enter the destination host (default is $Destination): "
local destination_input
read destination_input
if [ "$destination_input" != "" ]; then
Destination=$destination_input
fi
echov 1
echov 1 "Uploading to $Destination... (This could take quite a while)."
if [ ! -x /usr/bin/lftp ]; then
# No lftp installed
echov 1 "lftp could not be found in /usr/bin. The file(s) will need to be uploaded manually."
Ftp_res=255
else
# Make the lftp script first
local script="/tmp/rh-upload-lftp.$$"
echo "lftp $Destination <<EOF" > $script
echo "cd incoming" >> $script
if [ "$Split" != 0 ]; then
echo "lcd $Split_dir" >> $script
echo "mirror -R" >> $script
else
echo "put $Src_file -o $Dst_file" >> $script
fi
echo "exit" >> $script
echo "EOF" >> $script
/usr/bin/lftp -f $script
Ftp_res=$?
fi
}
function closure(){
if [ "$Encrypt" != 0 ]; then
echov 1 "## File was encrypted with key $Key_file."
echov 1 "Please upload this key file to Issue Tracker or send it to your support representative for decryption after upload."
fi
if [ "$Checksum" != 0 ]; then
echov 1 "## A checksum was performed on your file."
echov 1 "The MD5 checksum is $MD5_result (in $MD5_file)."
echov 1 "Please include this when updating your ticket so your support representative can verify the copy uploaded."
fi
if [ "$Split" != 0 ]; then
echov 1 "## Your file was split and the hunks are in: $Split_dir/ "
fi
if [ -n "$Ftp_res" ] && [ "$Ftp_res" -eq 0 ]; then
echov 1 "This script has completed successfully the upload."
if [ "$Compress" != 0 ] || [ "$Split" != 0 ]; then
echov 1 "You performed file encryption and/or file splitting."
echov 1 "You may consider removing those temporary files."
fi
else
echoe "Sorry unable to upload the files to $Destination. You may want to retry or do it manually."
fi
if [ "$Compress" != 0 ]; then
echov 1 "It is NOT recommended to remove the gzipped copy of the file."
echov 1 "This is the only remaining copy of the file on your system."
echov 1 "It is recommended to retain the file until your support representative indicates that the problem has been identified and/or resolved."
fi
}
function main(){
# Run through the functions
parse $@
ticket
compress
encrypt
checksum
split
upload
closure
}
main $@
|