Ptit script custom qui tourne depuis 5 ans sur toutes mes infra proxmox:
$ cat /opt/scripts/link/nvh
#/bin/bash
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
bold=`tput bold`
blue=`tput setaf 4`
reset=`tput sgr0`
sslRedir='/etc/nginx/templates/ssl-bloc-redirect.txt'
###
# Get Arguments
###
args="$@"
domain="$1"
vm="$2"
port="$3"
parseArgs(){
i=0
for cmd in $args; do
arg[$i]=$cmd
[[ ${arg[$i]} == "p2vm-providing8642" ]] && echo "## Commandé depuis p2vm ##" && p2vmProvided=1
[[ ${arg[$i]} =~ "domain=" ]] && domain=$(echo ${arg[$i]} | grep "\<domain=" | awk -F '=' '{ print $2 }')
[[ ${arg[$i]} =~ "vm=" ]] && vm=$(echo ${arg[$i]} | grep "\<vm=" | awk -F '=' '{ print $2 }')
[[ ${arg[$i]} =~ "port=" ]] && port=$(echo ${arg[$i]} | grep "\<port=" | awk -F '=' '{ print $2 }')
[[ ${arg[$i]} =~ "tpl=" ]] && tpl=$(echo ${arg[$i]} | grep "\<tpl=" | awk -F '=' '{ print $2 }')
[[ ${arg[$i]} =~ "ssl=" ]] && ssl=$(echo ${arg[$i]} | grep "\<ssl=" | awk -F '=' '{ print $2 }')
((i++))
done
}
parseArgs
ipvm=$(echo "scale=1; $vm-100" | bc)
newVHost="/etc/nginx/conf.d/$vm/$domain.conf"
case $tpl in
odoo) tplVHost='/etc/nginx/templates/odoo.txt';;
nextcloud) tplVHost='/etc/nginx/templates/nextcloud.txt';;
* ) tplVHost='/etc/nginx/templates/vhost.txt';;
esac
addSSL(){
echo "${yellow}Création du certificat SSL pour $domain...${reset}"
/usr/local/bin/certbot --nginx certonly -d $domain && echo "${green}Le certificat de $domain a bien été déployé${reset}" || echo "${red}Une erreur s'est produite lors de la création du certificat SSL${reset}"
sed -i -e 's/listen 80;/listen 443 ssl;/' $newVHost
sed -i -e '/includes\/errors.conf/i \\ listen [::]:443 ssl;\n include includes/ssl.conf;' $newVHost
sed -i -e '/error_log/i \\ ssl_certificate /etc/letsencrypt/live/$domain/fullchain.pem;\n ssl_certificate_key /etc/letsencrypt/live/$domain/privkey.pem;\n' $newVHost
cat $sslRedir >> $newVHost
sed -i -e 's/$domain/'"$domain"'/g' $newVHost
}
if [[ -e $newVHost ]]; then
echo "${yellow}Le virtualhost existe déjà${reset}"
if [[ -z $(grep "ssl_certificate_key" $newVHost) ]]; then
echo "Certificat SSL non présent"
if [[ $ssl =~ ^(1|true|oui)$ ]]; then
echo "Installation du certificat SSL ..."
addSSL
service nginx reload && echo "${green}Le domaine $domain à bien été créé et configuré =)${reset}" || echo "${red}Impossible de recharger nginx${reset}"
exit 0
else
echo "${yellow}Aucune action n'a été effectué${reset}"
exit 0
fi
else
echo "${yellow}Le SSL est déjà configuré sur le VHost${reset}"
exit 0
fi
fi
create_vhost(){
[[ ! -d /etc/nginx/conf.d/$vm ]] && mkdir /etc/nginx/conf.d/$vm
echo "${yellow}Génération du nouveau VirtualHost...${reset}"
cp $tplVHost $newVHost
sed -i -e 's/$domain/'"$domain"'/g' $newVHost
sed -i -e 's/$ipvm/'"$ipvm"'/g' $newVHost
sed -i -e 's/$vm/'"$vm"'/g' $newVHost
sed -i -e 's/$port/'"$port"'/g' $newVHost
if [[ $ssl =~ ^(1|true|oui)$ ]]; then
printf "${yellow}Configuration SSL du VHost...${reset}"
addSSL && echo "${green}OK${reset}" || echo "${red}KO${reset}"
else
echo "${yellow}Aucune configuration SSL ne sera effectué.${reset}"
fi
service nginx reload && echo "${green}Le domaine $domain à bien été créé et configuré =)${reset}" || echo "${red}Impossible de recharger nginx${reset}"
}
echo "Nous allons créer le domaine ${blue}$domain ${reset}qui sera lié au port ${blue}$port ${reset}de la VM ${blue}$vm.${reset}"
if [[ $p2vmProvided != 1 ]]; then
read -p "${bold}Confirmez-vous cette action ?${reset} (${green}y${reset} ou ${red}n${reset}) : " yn
case $yn in
[Yy]* ) create_vhost break;;
[Nn]* ) echo "${red}Fermeture du script${reset}" && exit;;
* ) echo "Merci de sélectionner yes ou no";;
esac
else
create_vhost
fi
exit 0
Spécifique à mon type d’infra.
$ nvh duniter-v1-g1.axiom-team.fr 135 10020 ssl=1
Le virtualhost existe déjà
Certificat SSL non présent
Installation du certificat SSL ...
Création du certificat SSL pour duniter-v1-g1.axiom-team.fr...
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for duniter-v1-g1.axiom-team.fr
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/duniter-v1-g1.axiom-team.fr/fullchain.pem
Key is saved at: /etc/letsencrypt/live/duniter-v1-g1.axiom-team.fr/privkey.pem
This certificate expires on 2023-01-23.
These files will be updated when the certificate renews.
NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
* Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
* Donating to EFF: https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Le certificat de duniter-v1-g1.axiom-team.fr a bien été déployé
Le domaine duniter-v1-g1.axiom-team.fr à bien été créé et configuré =)
Tu veux dire qu’on fait tourner un nginx dans la vm ? C’est pas plus simple de faire comme dit tuxmain d’accéder en lecture seule à un volume dans la vm ?
Et oui, on peut attendre un jour même si ça devrait pas être trop dur de lancer le script du cron en oneshot pour voir si ça marche