Advertisement
Nestor10

Proxmox+ACME mk_cert.sh

Jun 2nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.68 KB | None | 0 0
  1. #!/bin/bash
  2. guest_name=""
  3. guest_id=""
  4. guest_config=""
  5. domain=""
  6. if [ ! $1 ]; then
  7.   echo "Must specify a hostname or container ID!"
  8.   exit 1
  9. elif [[ $1 =~ ^[0-9]{3}$ ]]; then
  10.   guest_id=$1
  11.   guest_config="/etc/pve/lxc/$guest_id.conf"
  12.   if [[ ! -f $guest_config ]]; then
  13.     echo "Guest config does not exist!"
  14.     exit 1
  15.   fi
  16.   guest_name=$(cat $guest_config | grep -m 1 hostname | cut -d' ' -f2)
  17.   if [ ! $guest_name ]; then
  18.     echo "No guest found with that ID!"
  19.     exit 1
  20.   fi
  21.   echo "Found guest named $guest_name with that ID."
  22. else
  23.   guest_name=$1
  24.   guest_id=$(grep -l "hostname: $guest_name" /etc/pve/lxc/*.conf | egrep -o [0-9]{3})
  25.   guest_config="/etc/pve/lxc/$guest_id.conf"
  26.   if [ ! $guest_id ]; then
  27.     echo "No guest found with that name!"
  28.     exit 1
  29.   fi
  30.   echo "Found guest with ID $guest_id matching that name."
  31. fi
  32.  
  33. if [ $(egrep '^\[.*\]$' $guest_config) ]; then
  34.   echo "Cannot add mounts to container with snapshots!"
  35.   exit 1
  36. fi
  37.  
  38. /root/.acme.sh/acme.sh --issue --dns dns_cf -d $guest_name.$domain
  39.  
  40. if [ $? -eq 0 ]; then
  41.   echo "Certificate creation succeeded! Adding mount point to container..."
  42. else
  43.   echo "Certificate creation failed!"
  44.   exit 1
  45. fi
  46.  
  47. mount_list=$(egrep -o 'mp([0-9]{1,}): /' $guest_config)
  48. mount_target=0
  49. for mount in $mount_list; do
  50.   m_num=$(echo $mount | egrep -o '[0-9]{1,}')
  51.   if [[ $m_num -ge $mount_target ]]; then
  52.     mount_target=$(($m_num+1))
  53.   fi
  54. done
  55. mount_point="mp$mount_target: /root/.acme.sh/$guest_name.$domain/,mp=/certs"
  56. echo $mount_point >> $guest_config
  57. echo "Added mount point to $guest_config:"
  58. echo $mount_point
  59. echo "Restart the container and you should see the new certificate and key in /certs"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement