OpenStack in a snap!
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.
 
 
 
 

77 lines
2.5 KiB

  1. #!/bin/bash
  2. set -e
  3. # Check for microstack.init. TODO: just run microstack.init ...
  4. if ! [ "$(snapctl get initialized)" == "true" ]; then
  5. echo "Microstack is not initialized. Please run microstack.init!"
  6. exit 1;
  7. fi
  8. source $SNAP_COMMON/etc/microstack.rc
  9. if [ -z "$1" ]; then
  10. echo "Please specify a name for the server."
  11. exit 1
  12. else
  13. SERVER=$1
  14. fi
  15. if [[ ! $(openstack keypair list | grep "| microstack |") ]]; then
  16. echo "creating keypair ($HOME/.ssh/id_microstack)"
  17. mkdir -p $HOME/.ssh
  18. chmod 700 $HOME/.ssh
  19. openstack keypair create microstack > $HOME/.ssh/id_microstack
  20. chmod 600 $HOME/.ssh/id_microstack
  21. fi
  22. echo "Launching instance ..."
  23. openstack server create --flavor m1.tiny --image cirros --nic net-id=test --key-name microstack $SERVER
  24. echo "Checking security groups ..."
  25. SECGROUP_ID=`openstack security group list --project admin -f value -c ID`
  26. if [[ ! $(openstack security group rule list | grep icmp | grep $SECGROUP_ID) ]]; then
  27. echo "Creating security group rule for ping."
  28. openstack security group rule create $SECGROUP_ID --proto icmp
  29. fi
  30. if [[ ! $(openstack security group rule list | grep tcp | grep $SECGROUP_ID) ]]; then
  31. echo "Creating security group rule for ssh."
  32. openstack security group rule create $SECGROUP_ID --proto tcp --dst-port 22
  33. fi
  34. TRIES=0
  35. while [[ $(openstack server list | grep $SERVER | grep ERROR) ]]; do
  36. TRIES=$(($TRIES + 1))
  37. if test $TRIES -gt 3; then
  38. break
  39. fi
  40. echo "I ran into an issue launching an instance. Retrying ... (try $TRIES of 3)"
  41. openstack server delete $SERVER
  42. openstack server create --flavor m1.tiny --image cirros --nic net-id=test --key-name microstack $SERVER
  43. while [[ $(openstack server list | grep $SERVER | grep BUILD) ]]; do
  44. sleep 1;
  45. done
  46. done
  47. echo "Allocating floating ip ..."
  48. ALLOCATED_FIP=`openstack floating ip create -f value -c floating_ip_address external`
  49. openstack server add floating ip $SERVER $ALLOCATED_FIP
  50. echo "Waiting for server to become ACTIVE."
  51. while :; do
  52. if [[ $(openstack server list | grep $SERVER | grep ACTIVE) ]]; then
  53. openstack server list
  54. echo "Access your server with 'ssh -i $HOME/.ssh/id_microstack cirros@$ALLOCATED_FIP'"
  55. break
  56. fi
  57. if [[ $(openstack server list | grep $SERVER | grep ERROR) ]]; then
  58. openstack server list
  59. echo "Uh-oh. There was an error. Run `journalctl -xe` for details."
  60. exit 1
  61. fi
  62. done
  63. extgateway=`snapctl get questions.ext-gateway`
  64. echo "You can also visit the openstack dashboard at 'http://$extgateway/'"