Blame SOURCES/lookaside_upload_sig

fad222
#!/bin/bash
fad222
fad222
# This script will let you upload sources/blobs to new CentOS lookaside cache
fad222
# requirements:
fad222
#  - curl
fad222
#  - valid TLS certs from https://accounts.centos.org (or dev instance for testing)
fad222
fad222
# Some variables, switch for new url
fad222
lookaside_baseurl=$LOOKASIDE_BASEURL
fad222
hash_parameter="sha512"
fad222
fad222
if [ -z $LOOKASIDE_BASEURL ]; then
fad222
	lookaside_baseurl="https://git.centos.org"
fad222
	echo "Base URL set to default: $lookaside_baseurl"
fad222
fi
fad222
fad222
function usage {
fad222
fad222
	cat <
fad222
fad222
	You need to call the script like this : $0 -arguments
fad222
	
fad222
			-f : filename/source to upload (required, default:none)
fad222
			-n : package name for that source (requred, default:none, example "httpd")
fad222
			-h : display this help
fad222
fad222
	It is also possible to amend the default base url (currently set to https://git.centos.org):
fad222
	LOOKASIDE_BASEURL=<urlOfYourChoice> ./lookaside_upload_sig ...
fad222
EOF
fad222
}
fad222
fad222
function varcheck {
fad222
	if [ -z "$1" ]; then
fad222
		usage
fad222
		exit 1
fad222
	fi
fad222
fad222
}
fad222
fad222
function f_log {
fad222
	echo "[+] CentOS Lookaside upload tool -> $*"
fad222
}
fad222
fad222
while getopts “hf:n:” OPTION; do
fad222
	case $OPTION in
fad222
	h)
fad222
		usage
fad222
		exit 1
fad222
		;;
fad222
	f)
fad222
		file=$OPTARG
fad222
		;;
fad222
	n)
fad222
		pkgname=$OPTARG
fad222
		;;
fad222
	?)
fad222
		usage
fad222
		exit
fad222
		;;
fad222
	esac
fad222
done
fad222
fad222
varcheck $file
fad222
varcheck $pkgname
fad222
fad222
if [ ! -f ~/.centos.cert ]; then
fad222
	f_log "No mandatory TLS cert found (~/.centos.cert) .."
fad222
	f_log "please use centos-cert to retrieve your ACO TLS cert"
fad222
	exit 1
fad222
fi
fad222
fad222
if [ ! -f "${file}" ]; then
fad222
	f_log "Source to upload ${file} not found"
fad222
	exit 2
fad222
fi
fad222
fad222
checksum="$(${hash_parameter}sum ${file} | awk '{print $1}')"
fad222
fad222
f_log "Checking if file already uploaded"
fad222
local_size=$(stat -c %s ${file})
fad222
http_code=$(curl -s -o /dev/null -w "%{http_code}" ${lookaside_baseurl}/sources/${pkgname}/${file}/${hash_parameter}/${checksum}/${file})
fad222
remote_size=$(curl --silent -i --head ${lookaside_baseurl}/sources/${pkgname}/${file}/${hash_parameter}/${checksum}/${file} | grep "Content-Length" | cut -f 2 -d ':' | tr -d [:blank:] | tr -d '\r')
fad222
fad222
if [ "$http_code" -eq 200 ] && [ "$local_size" -eq "$remote_size" ]; then
fad222
	f_log "File already uploaded"
fad222
	exit 3
fad222
fi
fad222
fad222
f_log "Initialing new upload to lookaside"
fad222
f_log "URL : $lookaside_baseurl"
fad222
f_log "Source to upload : ${file} "
fad222
f_log "Hash parameter : ${hash_parameter}"
fad222
f_log "Package name: $pkgname"
fad222
f_log "${hash_parameter}sum: ${checksum}"
fad222
f_log " ====== Trying to upload ======="
fad222
echo ""
fad222
fad222
# Concatenating sha512
fad222
hash_cmd="$(${hash_parameter}sum ${file} | awk '{print $1}')"
fad222
curl ${lookaside_baseurl}/sources/upload_sig.cgi \
fad222
	--fail \
fad222
	--cert ~/.centos.cert \
fad222
	--form "name=${pkgname}" \
fad222
	--form "hash=${hash_parameter}" \
fad222
	--form "${hash_parameter}sum=${hash_cmd}" \
fad222
	--form "file=@${file}" \
fad222
	--progress-bar | tee /dev/null
fad222
fad222
upload_result="${PIPESTATUS[0]}"
fad222
fad222
if [ "$upload_result" -ne "0" ]; then
fad222
	f_log "[ERROR] Something didn't work to push to ${lookaside_baseurl}/sources/${pkgname}/${checksum}"
fad222
	f_log "[ERROR] Verify at the server side"
fad222
	exit 1
fad222
fi
fad222
fad222
f_log "Validating that source was correctly uploaded ...."
fad222
remote_size=$(curl --silent -i --head ${lookaside_baseurl}/sources/${pkgname}/${file}/${hash_parameter}/${checksum}/${file} | grep "Content-Length" | cut -f 2 -d ':' | tr -d [:blank:] | tr -d '\r')
fad222
if [ "$local_size" -eq "$remote_size" ]; then
fad222
	f_log "[SUCCESS] Source should be available at ${lookaside_baseurl}/sources/${pkgname}/${file}/${hash_parameter}/${checksum}/${file}"
fad222
else
fad222
	f_log "[ERROR] it seems there is a mismatch with source size and remote file size"
fad222
fi