## vim:ft=zsh ## put - make a file available via HTTP ## supports as many 'failover' hosts as an array can hold ## Usage: put ## Copyright (C) 2008 by Daniel Friesel ## License: WTFPL autoload check_com fdie warn typeset hostname typeset -a ssh_options hosts typeset target_dir server_prefix typeset file remote_file illegal typeset -i n_hosts n_ping n_curl if ! check_com curl; then function curl {return 0} warn "curl not present, won't be able to check upload success" fi function put_aneurysm_vpn { hostname='aneurysm.wildernet.org' ssh_options=(-q) target_dir='public_html/tmp/' server_prefix='http://aneurysm.wildernet.org/~derf/tmp/' illegal='*.(cgi|pl|php|py)' } function put_aneurysm_int { put_aneurysm hostname='aneurysm' } function put_aneurysm () { hostname='derf.homelinux.org' ssh_options=(-q) target_dir='public_html/tmp/' server_prefix='https://derf.homelinux.org/~derf/tmp/' illegal='*.(cgi|pl|php|py)' } function put_sievert () { hostname='sievert' ssh_options=(-q) target_dir='www/tmp/' server_prefix='https://tabularazor.org/~derf/tmp/' } hosts_vpn=(aneurysm_vpn) hosts=(aneurysm_int aneurysm sievert) while [[ $1 == -* ]] { case $1 in *) (( $(eval echo '$#'hosts_${1#-}) )) && eval hosts=\(\$hosts_${1#-}\) ;; esac shift } n_hosts=$#hosts if [[ -f $1 ]] { file=$1 while (( $#hosts )) { remote_file=${file:t} put_$hosts[1] ping -c 1 $hostname &> /dev/null || ping6 -c 1 $hostname &> /dev/null if (( ? == 0 )) { if [[ $remote_file == ${~illegal} ]] { remote_file=${remote_file:r} } scp $ssh_options $file $hostname:$target_dir/$remote_file ssh $ssh_options $hostname "chmod 644 $target_dir$remote_file" if $(curl -fI $server_prefix$remote_file &> /dev/null); then echo $server_prefix$remote_file return 0 else (( n_curl++ )) fi } else { (( n_ping++ )) } shift hosts } fdie "Tried uploading to $n_hosts hosts without success\n" \ " - $n_ping hosts are unreachable\n" \ " - $n_curl errors while uploading" } else { fdie "$1: No such file" }