Blame SOURCES/lookaside_upload

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
#  - valid group membership to let you upload to specific "branch"
fad222
fad222
# Some variables, switch for new url
fad222
lookaside_baseurl="https://git.centos.org"
fad222
fad222
function usage {
fad222
fad222
cat <<  EOF
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
        -b : "branch" where to upload to (required, default:none, example "c7-sig-core")
fad222
        -h : display this help
fad222
fad222
EOF
fad222
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
fad222
fad222
while getopts “hf:n:b:” OPTION
fad222
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
         b)
fad222
             branch=$OPTARG
fad222
             ;;
fad222
         ?)
fad222
             usage
fad222
             exit
fad222
             ;;
fad222
     esac
fad222
done
fad222
fad222
varcheck $file
fad222
varcheck $pkgname
fad222
varcheck $branch
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=$(sha1sum ${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}/${branch}/${checksum})
fad222
remote_size=$(curl --silent -i --head ${lookaside_baseurl}/sources/${pkgname}/${branch}/${checksum}|grep "Content-Length"|cut -f 2 -d ':'|tr -d [:blank:]|tr -d '\r')
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 "Package name: $pkgname"
fad222
f_log "sha1sum: ${checksum}"
fad222
f_log "Remote branch: ${branch}" 
fad222
f_log " ====== Trying to upload ======="
fad222
echo ""
fad222
curl ${lookaside_baseurl}/sources/upload.cgi \
fad222
	--fail \
fad222
	--cert ~/.centos.cert \
fad222
	--form "name=${pkgname}" \
fad222
	--form "branch=${branch}" \
fad222
	--form "sha1sum=${checksum}" \
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}/${branch}/${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}/${branch}/${checksum}|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}/${branch}/${checksum}"
fad222
else
fad222
  f_log "[ERROR] it seems there is a mismatch with source size and remote file size"
fad222
fi
fad222
fad222