github.com/scaleway/scaleway-cli@v1.11.1/examples/image-quick-checks.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # I'm a script used to check some basic operations with images.
     4  
     5  # Params
     6  if [ $# -ne 1 ]; then
     7      echo "usage: $0 image-id"
     8      exit 1
     9  fi
    10  
    11  # Globals
    12  
    13  IMAGE_UUID=$1
    14  IMAGE_NAME=""
    15  SERVER_UUID=""
    16  SERVER_NAME=""
    17  WORKDIR=$(mktemp -d 2>/dev/null || mktemp -d -t /tmp)
    18  
    19  # Printing helpers
    20  
    21  function _log_msg {
    22      echo -ne "${*}" >&2
    23  }
    24  
    25  function einfo {
    26      _log_msg "\033[1;36m>>> \033[0m${*}\n"
    27  }
    28  
    29  function echeck {
    30      einfo $@
    31      echo ""
    32  }
    33  
    34  function esuccess {
    35      _log_msg "\033[1;32m>>> \033[0m${*}\n"
    36  }
    37  
    38  function ewarn {
    39      _log_msg "\033[1;33m>>> \033[0m${*}\n"
    40  }
    41  
    42  function eerror {
    43      _log_msg "\033[1;31m>>> ${*}\033[0m\n"
    44  }
    45  
    46  function eedie_on_error {
    47      failed=$1
    48      if [ $failed -ne 0 ]
    49      then
    50  	eerror $2
    51  	cleanup
    52  	exit 1
    53      fi
    54  }
    55  
    56  # Super helper to print server output to stdout
    57  
    58  function watch_server {
    59      einfo "Attaching to server $SERVER_UUID"
    60      mkfifo ${WORKDIR}/stop_watching_server
    61      scw attach $SERVER_UUID 2>/dev/null &
    62      killme=$!
    63      sleep 5
    64      (
    65  	cat ${WORKDIR}/stop_watching_server
    66  	kill -9 $killme
    67  	echo ""
    68      ) &>/dev/null &
    69  }
    70  
    71  function stop_watching {
    72      echo "" > ${WORKDIR}/stop_watching_server
    73      rm -f ${WORKDIR}/stop_watching_server
    74      sleep 1
    75  }
    76  
    77  # Checks
    78  
    79  function check_image {
    80      echeck "Checking image..."
    81      IMAGE_NAME=$(scw inspect -f '{{ .Name }}' $IMAGE_UUID 2> /dev/null)
    82      eedie_on_error $? "Unable to find image behind $IMAGE_UUID"
    83      esuccess "Image name is $IMAGE_NAME"
    84      SERVER_NAME="qa-image-$$"
    85  }
    86  
    87  function check_create {
    88      echeck "Checking server creation with image..."
    89      SERVER_UUID=$(scw create --name "$SERVER_NAME" $IMAGE_UUID)
    90      eedie_on_error $? "Unable to create server with image $IMAGE_NAME"
    91      esuccess "Created server $SERVER_UUID with image $IMAGE_NAME"
    92  }
    93  
    94  function check_boot {
    95      echeck "Checking server boot (can take up to 300 seconds)..."
    96      watch_server $SERVER_UUID
    97      scw start -w -T 300 $SERVER_UUID &> /dev/null
    98      eedie_on_error $? "Unable to boot server $SERVER_UUID"
    99      stop_watching
   100      esuccess "Server $SERVER_UUID properly boots"
   101  }
   102  
   103  function check_hostname {
   104      echeck "Checking server hostname..."
   105      HOSTNAME=$(scw exec $SERVER_UUID hostname)
   106      eedie_on_error $? "Unable to fetch hostname from server $SERVER_UUID"
   107      if [ "$HOSTNAME" -ne "$SERVER_NAME" ]
   108      then
   109  	ewarn "Server hostname is *NOT* properly set (expected=${SERVR_NAME}, found=${HOSTNAME})"
   110      else
   111  	esuccess "Server hostname is properly set"
   112      fi
   113  }
   114  
   115  function check_reboot {
   116      echeck "Checking reboot..."
   117      watch_server
   118      scw exec $SERVER_UUID reboot &> /dev/null
   119      scw exec -T 300 -w $SERVER_UUID uptime
   120      stop_watching
   121      eedie_on_error $? "Unable to reboot server $SERVER_NAME"
   122      esuccess "Server $SERVER_NAME properly reboots"
   123  }
   124  
   125  # Cleanup
   126  
   127  function cleanup {
   128      if [ "$SERVER_UUID" != "" ]
   129      then
   130  	einfo "Cleaning up server $SERVER_UUID"
   131  	scw stop -t $SERVER_UUID &> /dev/null
   132  	scw rm $SERVER_UUID &> /dev/null
   133  	SERVER_UUID=""
   134      fi
   135      stop_watching
   136      einfo "Cleaning up temporary directory $WORKDIR"
   137      rm -rf $WORKDIR
   138      kill -9 $(jobs -p)
   139      einfo "Killed all survivor processes"
   140  }
   141  
   142  # Main
   143  
   144  function main {
   145      check_image
   146      check_create
   147      check_boot
   148      check_hostname
   149      check_reboot
   150      cleanup
   151  }
   152  
   153  main