You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
4.0 KiB
161 lines
4.0 KiB
#!/bin/bash
|
|
|
|
ssh_included=true
|
|
|
|
function sshValidate {
|
|
if [ "$ssh_stage_user" == "" ] || [ "$ssh_stage_domain" == "" ] || [ "$ssh_live_user" == "" ] || [ "$ssh_live_domain" == "" ]
|
|
then
|
|
echo >&2
|
|
echoError "Please configure $project_manager_dir/data/$customer/$project/etc/plugins/ssh/config" >&2
|
|
echo >&2
|
|
exit
|
|
fi
|
|
|
|
if [ "$git_included" == "" ]
|
|
then
|
|
echo >&2
|
|
echoError "Plugin \"git\" has to be included" >&2
|
|
echo >&2
|
|
exit
|
|
fi
|
|
|
|
if [ "$git_ssh_user" == "" ] || [ "$git_ssh_domain" == "" ]
|
|
then
|
|
echo >&2
|
|
echoError "Please configure $project_manager_dir/data/$customer/$project/etc/plugins/git/config" >&2
|
|
echo >&2
|
|
exit
|
|
fi
|
|
}
|
|
|
|
|
|
function sshGetConfig {
|
|
local env=$(getArgument "$1" "Usage getEnvVar [live|stage|git] var" "live stage git")
|
|
local suffix=$(getArgument "$2" "Usage getEnvVar [live|stage|git] var" true)
|
|
|
|
if [ "$env" == "live" ] || [ "$env" == "stage" ]
|
|
then
|
|
echo "$(eval "echo \"\$ssh_${env}_$suffix\"")"
|
|
else
|
|
echo "$(eval "echo \"\$${env}_ssh_$suffix\"")"
|
|
fi
|
|
}
|
|
|
|
function sshGetPrivateKey
|
|
{
|
|
local env=$(getArgument "$1" "Usage: sshGetPrivateKey [live|stage|git]" "live stage git")
|
|
|
|
if [ "$env" == "live" ] || [ "$env" == "stage" ]
|
|
then
|
|
echo "$(sshGetConfig "$env" "private_key")"
|
|
else
|
|
echo "$ssh_private_key"
|
|
fi
|
|
}
|
|
|
|
function sshGetPublicKey
|
|
{
|
|
local env=$(getArgument "$1" "Usage: addSSHPublic [live|stage|git]" "live stage git")
|
|
|
|
if [ "$env" == "live" ] || [ "$env" == "stage" ]
|
|
then
|
|
echo "$(sshGetConfig "$env" "private_key")"
|
|
else
|
|
echo "$ssh_private_key"
|
|
fi
|
|
}
|
|
|
|
function sshAddKey {
|
|
local env="$(getArgument "$1" "Usage: sshAddKey [live|stage|git]" "live stage git")"
|
|
local ssh_private_key="$(sshGetPrivateKey "$env")"
|
|
local ssh_public_key="$(sshGetPublicKey "$env")"
|
|
|
|
if [ "$ssh_private_key" != "" ] && [ "$ssh_public_key" != "" ]
|
|
then
|
|
if [ ! -f "$app_dir/.ssh/$env" ] && [ ! -f "$app_dir/.ssh/$env.pub" ]
|
|
then
|
|
sshCopyKeys "$env"
|
|
fi
|
|
else
|
|
if [ ! -f "$app_dir/.ssh/$env" ] && [ ! -f "$app_dir/.ssh/$env.pub" ]
|
|
then
|
|
sshInstallKeys "$env"
|
|
fi
|
|
fi
|
|
ssh-add "$app_dir/.ssh/$env"
|
|
}
|
|
|
|
function sshCopyKeys {
|
|
local env=$(getArgument "$1" "Usage: sshCopyKeys [live|stage|git]" "live stage git")
|
|
local ssh_private_key="$(sshGetPrivateKey "$env")"
|
|
local ssh_public_key="$(sshGetPublicKey "$env")"
|
|
|
|
if [ ! -f "$ssh_private_key" ] && [ ! -f "$ssh_public_key" ]
|
|
then
|
|
echo
|
|
echoError "Configureg $env key files don't exists" >&2
|
|
echo
|
|
exit
|
|
fi
|
|
|
|
if [ -f "$app_dir/.ssh/$env" ]
|
|
then
|
|
echo
|
|
echoError "SSH $env private key already exists" >&2
|
|
echo
|
|
exit
|
|
fi
|
|
|
|
if [ -f "$app_dir/.ssh/$env.pub" ]
|
|
then
|
|
echo
|
|
echoError "SSH $env public key already exists" >&2
|
|
echo
|
|
exit
|
|
fi
|
|
|
|
cp "$ssh_private_key" "$app_dir/.ssh/$env"
|
|
chmod 0600 "$app_dir/.ssh/$env"
|
|
|
|
cp "$ssh_public_key" "$app_dir/.ssh/$env.pub"
|
|
chmod 0600 "$app_dir/.ssh/$env.pub"
|
|
}
|
|
|
|
function sshInstallKeys {
|
|
local env=$(getArgument "$1" "Usage: sshInstallKeys [live|stage|git]" "live stage git")
|
|
local ssh_private_key="$(sshGetPrivateKey "$env")"
|
|
local ssh_public_key="$(sshGetPublicKey "$env")"
|
|
local user="$(sshGetConfig "$env" "user")"
|
|
local domain="$(sshGetConfig "$env" "domain")"
|
|
|
|
if [ -f "$ssh_private_key" ] && [ -f "$ssh_public_key" ]
|
|
then
|
|
echo
|
|
echoError "Can't create $env key files, there are already some configured" >&2
|
|
echo
|
|
exit
|
|
fi
|
|
|
|
if [ -f "$app_dir/.ssh/$env" ]
|
|
then
|
|
echo
|
|
echoError "SSH $env private key already exists" >&2
|
|
echo
|
|
exit
|
|
fi
|
|
|
|
if [ -f "$app_dir/.ssh/$env.pub" ]
|
|
then
|
|
echo
|
|
echoError "SSH $env public key already exists" >&2
|
|
echo
|
|
exit
|
|
fi
|
|
|
|
ssh-keygen -b 4096 -t rsa -f "$app_dir/.ssh/$env" -q -N ""
|
|
chmod 0600 "$app_dir/.ssh/$env"
|
|
chmod 0600 "$app_dir/.ssh/$env.pub"
|
|
|
|
echo "Please enter SSH $env system password:"
|
|
ssh-copy-id -i "$app_dir/.ssh/$env.pub" "$user"@"$domain"
|
|
}
|