Last active
February 5, 2019 03:33
-
-
Save hc0d3r/439fc07bb972081a146709480df4e882 to your computer and use it in GitHub Desktop.
chmod +x deproy.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
help(){ | |
cat <<B | |
deproy.sh | |
Usage: deproy.sh [cmd] | |
Command list: | |
start - start deploy | |
force - force deploy (skip sha256 verification) | |
init - create an empty configuration file | |
connection options: | |
ssh-user [string] - set ssh user (required) | |
ssh-host [host] - set ssh host (required) | |
ssh-pass [password] - set ssh password | |
ssh-key [filename] - set ssh key | |
ssh-port [port] - set ssh port | |
deploy options: | |
remote-dst-folder [folder] - folder name to extract file (required) | |
remote-tmp [folder] - remote temporary file location | |
dont-unpack [bool] - dont unpack file | |
B | |
exit | |
} | |
init(){ | |
> .deproy-conf | |
exit | |
} | |
fatal(){ | |
echo "$@" | |
exit | |
} | |
update_or_create(){ | |
local var="$1" | |
local value="$2" | |
var="${var//-/_}" | |
value="${value////\\/}" | |
grep -q "^$var=.*" .deproy-conf && sed "s/^$var=.*/$var=$value/" -i .deproy-conf || | |
echo "$var=$value" >> .deproy-conf | |
} | |
[[ $# -lt 1 ]] && fatal 'deproy.sh: you must set an action, run `deproy.sh help` for futher information' | |
case $1 in | |
init) | |
init | |
;; | |
help) | |
help | |
;; | |
start|force) | |
;; | |
*) | |
update_or_create "$1" "$2" | |
exit | |
;; | |
esac | |
if [[ -f .deproy-conf ]];then | |
. .deproy-conf | |
else | |
fatal 'deproy.sh: .deproy-conf not found' | |
fi | |
[[ -z "$ssh_user" ]] && fatal "deproy.sh: ssh-user not set" | |
[[ -z "$ssh_host" ]] && fatal "deproy.sh: ssh-host not set" | |
[[ -z "$remote_dst_folder" ]] && fatal "deproy.sh: remote-dst-folder not set" | |
if [[ ! -z "$ssh_pass" ]];then | |
cmd+=(sshpass -p "$ssh_pass") | |
fi | |
if [[ -z "$remote_tmp" ]];then | |
remote_tmp='/tmp' | |
fi | |
tmp_name=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1) | |
tmp_file="/tmp/$tmp_name.tar.gz" | |
remote_tmp_name="$remote_tmp/$tmp_name.tar.gz" | |
if [[ -f .deproy-time ]];then | |
taropts=(--newer "`LANG=en date -r .deproy-time`") | |
fi | |
if [[ ! -z "$ssh_port" ]];then | |
sshcustom=(-p $ssh_port) | |
scpcustom=(-P $ssh_port) | |
fi | |
if [[ ! -z "$ssh_key" ]];then | |
sshcustom+=(-i "$ssh_key") | |
scpcustom+=(-i "$ssh_key") | |
fi | |
echo "[*] packing ..." | |
tar --exclude-vcs-ignores -czf "$tmp_file" "${taropts[@]}" --exclude='.deproy*' . | |
sha=($(sha256sum "$tmp_file")) | |
read oldsum < .deproy-sha256sum | |
echo "${sha[0]}" > .deproy-sha256sum | |
if [[ "${sha[0]}" == "$oldsum" && "$1" != "force" ]];then | |
echo "[+] nothing to do" | |
exit | |
fi | |
echo -e "[*] uploading ..." | |
"${cmd[@]}" scp "${scpcustom[@]}" "$tmp_file" "$ssh_user"@"$ssh_host":"$remote_tmp" | |
echo "[+] remote filename: $remote_tmp_name" | |
if [[ -z "$dont_unpack" ]];then | |
echo "[*] remote unpacking ..." | |
"${cmd[@]}" ssh "${sshcustom[@]}" "$ssh_user"@"$ssh_host" "tar -xvf $remote_tmp_name -C \"$remote_dst_folder\";rm -f \"$remote_tmp_name\"" | |
fi | |
> .deproy-time | |
rm -f "$tmp_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment