Skip to content

Instantly share code, notes, and snippets.

@Ram-Z
Created May 13, 2015 21:55
Show Gist options
  • Save Ram-Z/6d986fd3ab1e34ab6d18 to your computer and use it in GitHub Desktop.
Save Ram-Z/6d986fd3ab1e34ab6d18 to your computer and use it in GitHub Desktop.
#!/bin/bash
# upload.sh
#
# Copyright © 2015 Samir Benmendil <[email protected]>
# Copyright © 2011-2012 Manuel Tortosa <manutortosa[at]chakra-project[dot]org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# global vars
#
_script_name="Upload Package(s)"
version="2.9"
function usage ()
{
echo "Usage : $0 -r REPO [PKGS...]
Upload packages to REPO Any PKGS given as arguments will be searched for with
'find', if the argument does not contain a globbing character, it is
surrounded by '*'.
Examples:
$0 upload all pkgs
$0 foo bar upload pkgs containing 'foo' or 'bar'
invokes \`find -name '*foo*' -o -name '*bar*'\`
$0 'foobar*' upload pkgs starting with 'foobar'
invokes \`find -name 'foobar*'\`
Options:
-r,--repo Which repo to upload to. (mandatory)
-h,--help Display this message
-v,--version Display script version"
}
# helper functions
_needed_functions="config_handling helpers messages buildsystem"
for subroutine in $_needed_functions ; do
source ~/bin/functions/$subroutine
done
#
# Functions
#
confirm_targets() {
local -n pkgs=upload_list
local cnt=$(echo "${pkgs[@]}" | tr ' ' '\n' | grep -v '.sig$' -c)
local cnt_sig=$(echo "${pkgs[@]}" | tr ' ' '\n' | grep '.sig$' -c)
newline
warning "You are about to upload ($cnt) packages and ($cnt_sig) signatures to «$repo-$(get_arch)»:"
echo "${pkgs[@]}" | tr ' ' '\n'
newline
question "Do you really want to upload the package(s)? (y/n) "
while true ; do
read yn
case $yn in
[yY]*) return 0 ;;
[nNq]*) return 1 ;;
*) echo "Enter (y)es or (n)o" ;;
esac
done
}
upload_pkgs() {
# $1 repo to move to.
# $* list of absolute paths to pkgs
if [[ $# -eq 0 ]]; then
error "No repo given!"
exit 1
fi
local repo=$1; shift
local -a pkgs=( $@ )
lock_repo "$repo" || exit 1
status_done
status_start "uploading to «$repo-$(get_arch)»"
if [[ ${#pkgs} -eq 0 ]]; then
error "No files given!"
exit 1
fi
upload_files "$repo" "${pkgs[@]}"
if [[ $? -ne 0 ]]; then
# exits with and error, unlocking the repo and killing the ssh agent
error "failed performing the current operation, aborting..."
unlock_repo "$repo"
exit 1
fi
status_done
local -a file_names=( ${pkgs[@]##*/} )
local upload
upload=$(ssh "$_ssh_user@$_rsync_server" -p "$_ssh_port" "akbm --repo-name $repo --arch $(get_arch) --repo-add" "${file_names[@]}" 2>&1 | tee /dev/tty)
if [[ "$upload" == *"::SUCCESS::"* ]]; then
status_done
else
status_fail
error "$upload"
unlock_repo "$repo"
exit 1
fi
move_to_cache "${pkgs[@]}"
# Sign the new updated database
sign_online_database "$repo"
unlock_repo "$repo"
}
#
# startup
#
# Parse arguments
declare -a args
while [[ $# -gt 0 ]]; do
opt="$1"
case $opt in
-r|--repo) repo=$2; shift 2 ;;
-h|--help) usage; exit 0 ;;
-v|--version) echo "$0 -- Version $version"; exit 0 ;;
-* )
echo -e "\n Option does not exist : $opt\n"
usage; exit 1 ;;
*) args+=("$opt"); shift ;;
esac
done
if [[ -z "$repo" ]]; then
echo -e "\n Repo not specified.\n"
usage; exit 1
fi
title "$_script_name v$version - $_cur_repo-$_build_arch"
source_safe ~/user.conf
check_rsync
check_accounts
# noglob because apparently it is very difficult to generate find arguments and quote them properly
upload_list=( $(set -o noglob; find ~/_repo/ -name '*.pkg.tar.*' \( $(find_args ${args[@]}) \) -print) )
if [[ -z "$upload_list" ]] ; then
error "No packages found in «_repo/$CHROOT», there's nothing to upload"
exit 1
fi
if confirm_targets; then
time upload_pkgs "$repo" "${upload_list[@]}"
else
error "Aborting!"
exit 1
fi
# vim: set et sw=2 ts=2:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment