Advertisement
Sergio_Istea

digitalocean_api.sh

Nov 23rd, 2022
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. func_logger () {
  9.     # Common Log Format
  10.     # host ident authuser date request status bytes
  11.     # 127.0.0.1 user-identifier frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
  12.    
  13.     local host=$(hostname -i)
  14.     local ident="-"
  15.     local authuser=$USER
  16.     local timestamp=$(date)
  17.     local request="$1"
  18.     local status="$2"           # codigo de error devuelto
  19.     local bytes="$3"            # tamaño en bytes del mensaje devuelto
  20.     echo "$host $ident $authuser [$timestamp] $request $status $bytes" >> /var/log/domain_admin.log
  21. }
  22.  
  23.  
  24. func_domain () {
  25.     local func_command=$1
  26.     local domain=$2
  27.     local output
  28.     local stat
  29.     case $func_command in
  30.  
  31.         list)output="$(doctl compute domain list)"
  32.             echo $output
  33.  
  34.             func_logger $func_command $? $(echo $output | wc -c)
  35.             ;;
  36.         create) doctl compute domain create $domain
  37.             ;;
  38.         retrieve) if func_check $domain;then
  39.                
  40.                 output=$(doctl compute domain get $domain;stat=$?)
  41.                 echo $output
  42.                 func_logger $func_command $stat $(echo $output | wc -c)
  43.             else
  44.                 func_logger $func_command $? $(echo $output | wc -c)
  45.  
  46.             fi
  47.             ;;
  48.     esac
  49. }
  50.  
  51.  
  52. func_check () {
  53.     local domain
  54.     local arg2
  55.  
  56.     if doctl compute domain get $domain &> /dev/null; then
  57.  
  58.         return 0
  59.     else
  60.         return 1
  61.     fi
  62. }
  63.  
  64. script_context=$1       # domain|registers
  65. context_command=$2      # list|delete|retrieve|create
  66. domain=$3
  67.  
  68.  
  69. case $script_context in
  70.  
  71.  
  72.     domain)              # $1 en la funcion          $2
  73.         #func_domain list|create|retrieve|delete <domain>
  74.         func_domain $context_command $domain
  75.         ;;
  76.  
  77. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement