c4rt0 / centos-git-common

Forked from centos-git-common 2 years ago
Clone

Blame lookaside_upload_sig

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