c4rt0 / centos-git-common

Forked from centos-git-common 2 years ago
Clone

Blame lookaside_upload_sig

03f8bc
#!/bin/bash
03f8bc
 
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)
03f8bc
#  - valid group membership to let you upload to specific "branch"
03f8bc
 
03f8bc
# Some variables, switch for new url
03f8bc
lookaside_baseurl="https://git.centos.org"
03f8bc
 
03f8bc
function usage {
03f8bc
 
03f8bc
cat <<  EOF
03f8bc
03f8bc
You need to call the script like this : $0 -arguments
03f8bc
 
03f8bc
        -f : filename/source to upload (required, default:none)
03f8bc
        -a : hash parameter (optional, default: none, example "b6804fa")
03f8bc
        -n : package name for that source (requred, default:none, example "httpd")
03f8bc
        -b : "branch" where to upload to (optional, default:none, example "c7-sig-core")
03f8bc
        -h : display this help
03f8bc
As far as branch and hash parameters are optional, one of these needs to be specified.
03f8bc
03f8bc
EOF
03f8bc
 
03f8bc
}
03f8bc
 
03f8bc
function varcheck {
03f8bc
if [ -z "$1" ] ; then
03f8bc
        usage
03f8bc
        exit 1
03f8bc
fi
03f8bc
 
03f8bc
}
03f8bc
 
03f8bc
function f_log {
03f8bc
  echo "[+] CentOS Lookaside upload tool -> $*"
03f8bc
}
03f8bc
 
03f8bc
03f8bc
while getopts “hf:a:n:b:” OPTION
03f8bc
do
03f8bc
     case $OPTION in
03f8bc
         h)
03f8bc
             usage
03f8bc
             exit 1
03f8bc
             ;;
03f8bc
         a)
03f8bc
             hash=$OPTARG
03f8bc
             ;;
03f8bc
         f)
03f8bc
             file=$OPTARG
03f8bc
             ;;
03f8bc
         n)
03f8bc
             pkgname=$OPTARG
03f8bc
             ;;
03f8bc
         b)
03f8bc
             branch=$OPTARG
03f8bc
             ;;
03f8bc
         ?)
03f8bc
             usage
03f8bc
             exit
03f8bc
             ;;
03f8bc
     esac
03f8bc
done
03f8bc
 
03f8bc
varcheck $file
03f8bc
varcheck $hash
03f8bc
varcheck $pkgname
03f8bc
varcheck $branch
03f8bc
 
03f8bc
if [ ! -f ~/.centos.cert ] ;then
03f8bc
  f_log "No mandatory TLS cert found (~/.centos.cert) .."
03f8bc
  f_log "please use centos-cert to retrieve your ACO TLS cert"
03f8bc
  exit 1
03f8bc
fi
03f8bc
 
03f8bc
if [ ! -f "${file}" ] ;then
03f8bc
  f_log "Source to upload ${file} not found"
03f8bc
  exit 2
03f8bc
fi
03f8bc
03f8bc
checksum=$(sha1sum ${file}|awk '{print $1}')
03f8bc
 
03f8bc
f_log "Checking if file already uploaded"
03f8bc
local_size=$(stat -c %s ${file})
03f8bc
03f8bc
# -z parameter optional #
03f8bc
03f8bc
if [ -z "${branch}" ] ;then
03f8bc
  f_log "Branch parameter not given"
03f8bc
  http_code=$(curl -s -o /dev/null -w "%{http_code}" ${lookaside_baseurl}/sources/${pkgname}/${checksum})
03f8bc
  remote_size=$(curl --silent -i --head ${lookaside_baseurl}/sources/${pkgname}/${checksum}|grep "Content-Length"|cut -f 2 -d ':'|tr -d [:blank:]|tr -d '\r')
03f8bc
  exit 0
03f8bc
else	
03f8bc
  http_code=$(curl -s -o /dev/null -w "%{http_code}" ${lookaside_baseurl}/sources/${pkgname}/${branch}/${checksum})
03f8bc
  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')
03f8bc
fi
03f8bc
03f8bc
03f8bc
if [ "$http_code" -eq 200 ] && [ "$local_size" -eq "$remote_size" ] ; then
03f8bc
  f_log "File already uploaded"
03f8bc
  exit 3
03f8bc
fi
03f8bc
 
03f8bc
f_log "Initialing new upload to lookaside"
03f8bc
f_log "URL : $lookaside_baseurl"
03f8bc
f_log "Source to upload : ${file} "
03f8bc
03f8bc
if [ -z "${hash}" ]; then
03f8bc
  f_log "No hash parameter was specified"
03f8bc
else
03f8bc
  f_log "Hash parameter : ${hash}"
03f8bc
fi
03f8bc
03f8bc
f_log "Package name: $pkgname"
03f8bc
f_log "sha1sum: ${checksum}"
03f8bc
03f8bc
# Ugly way of implementing conditional parameter
03f8bc
03f8bc
if [ -z "${branch}" && "${hash}" -gt 0 ] ;then
03f8bc
  f_log "Remote branch not specified" 
03f8bc
  f_log " ====== Trying to upload ======="
03f8bc
  echo "" 
03f8bc
  curl ${lookaside_baseurl}/sources/upload.cgi \
03f8bc
    --fail \
03f8bc
    --cert ~/.centos.cert \
03f8bc
    --form "name=${pkgname}" \
03f8bc
    --form "hash=${hash}" \
03f8bc
    --form "sha1sum=${checksum}" \
03f8bc
    --form "file=@${file}" \
03f8bc
    --progress-bar | tee /dev/null \
03f8bc
# Saving a copy
03f8bc
  > lookaside_upload_sig
03f8bc
03f8bc
  upload_result="${PIPESTATUS[0]}"
03f8bc
03f8bc
  if [ "$upload_result" -ne "0" ] ;then
03f8bc
    f_log "[ERROR] Something didn't work to push to ${lookaside_baseurl}/sources/${pkgname}/${checksum}"
03f8bc
    f_log "[ERROR] Verify at the server side"
03f8bc
    exit 1
03f8bc
  fi
03f8bc
03f8bc
  f_log "Validating that source was correctly uploaded ...."
03f8bc
  remote_size=$(curl --silent -i --head ${lookaside_baseurl}/sources/${pkgname}/${checksum}|grep "Content-Length"|cut -f 2 -d ':'|tr -d [:blank:]|tr -d '\r')
03f8bc
  if [ "$local_size" -eq "$remote_size" ] ; then
03f8bc
    f_log "[SUCCESS] Source should be available at ${lookaside_baseurl}/sources/${pkgname}/${checksum}"
03f8bc
  else
03f8bc
    f_log "[ERROR] it seems there is a mismatch with source size and remote file size"
03f8bc
  fi
03f8bc
elif [ -z "${hash}" && "${branch}" -gt 0 ] ;then
03f8bc
  f_log "Remote branch: ${branch}" 
03f8bc
  f_log " ====== Trying to upload ======="
03f8bc
  echo "" 
03f8bc
03f8bc
  curl ${lookaside_baseurl}/sources/upload.cgi \
03f8bc
    --fail \
03f8bc
    --cert ~/.centos.cert \
03f8bc
    --form "name=${pkgname}" \
03f8bc
    --form "branch=${branch}" \
03f8bc
    --form "sha1sum=${checksum}" \
03f8bc
    --form "file=@${file}" \
03f8bc
    --progress-bar | tee /dev/null
03f8bc
  
03f8bc
  # Saving a copy
03f8bc
  > lookaside_upload_sig
03f8bc
	 
03f8bc
  upload_result="${PIPESTATUS[0]}"
03f8bc
	 
03f8bc
  if [ "$upload_result" -ne "0" ] ;then
03f8bc
    f_log "[ERROR] Something didn't work to push to ${lookaside_baseurl}/sources/${pkgname}/${branch}/${checksum}"
03f8bc
    f_log "[ERROR] Verify at the server side"
03f8bc
    exit 1
03f8bc
  fi
03f8bc
	 
03f8bc
  f_log "Validating that source was correctly uploaded ...."
03f8bc
  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')
03f8bc
  if [ "$local_size" -eq "$remote_size" ] ; then
03f8bc
    f_log "[SUCCESS] Source should be available at ${lookaside_baseurl}/sources/${pkgname}/${branch}/${checksum}"
03f8bc
  else
03f8bc
    f_log "[ERROR] it seems there is a mismatch with source size and remote file size"
03f8bc
  fi
03f8bc
else
03f8bc
  f_log "[ERROR] Neither branch or hash parameters were specified"
03f8bc
  exit 1
03f8bc
fi