Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #PREFIX='myprefix-'
- PREFIX='test1'
- ROUTER_NAME="${PREFIX}border-router"
- PRIVATE_NETWORK_NAME="pe1private-net"
- PRIVATE_SUBNET_NAME="pe1private-subnet"
- SSH_KEY_NAME="${PREFIX}first-instance-key"
- INSTANCE_NAME="${PREFIX}first-instance"
- SECURITY_GROUP_NAME="${PREFIX}first-instance-sg"
- # Network portion of /24 you wish to use in the subnet
- NETWORK="192.168.1"
- POOL_START_OCT="10"
- POOL_END_OCT="200"
- FLAVOR_NAME="m1.small"
- IMAGE_NAME="Ubuntu Xenial 16.04"
- SSH_PUBLIC_KEY=~/.ssh/id_rsa.pub
- CC_NAMESERVER_1=8.8.8.8
- CC_NAMESERVER_2=8.8.4.4
- # valid ip function
- valid_ip() {
- regex="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
- echo "$1" | egrep "$regex" &>/dev/null
- return $?
- }
- echo Finding your external ip:
- hash dig 2>/dev/null && {
- CC_REMOTE_IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
- }
- for curl_ip in http://ipinfo.io/ip http://ifconfig.me/ip http://curlmyip.com; do
- CC_REMOTE_IP=$( curl -s $curl_ip )
- if valid_ip "$CC_REMOTE_IP"; then
- break
- fi
- done
- if ! valid_ip "$CC_REMOTE_IP"; then
- echo "Could not determine your external IP address, please find it and edit CC_REMOTE_IP before proceeding";
- exit 1;
- fi
- echo "$CC_REMOTE_IP"
- CC_REMOTE_CIDR_NETWORK="$CC_REMOTE_IP/32"
- # everything is in order, lets build a stack!
- echo Creating a new router:
- openstack --insecure router create $ROUTER_NAME
- echo Setting router gateway.
- openstack --insecure router set $ROUTER_NAME
- echo Creating a new private network:
- openstack --insecure network create "$PRIVATE_NETWORK_NAME"
- echo Creating a router interface on the subnet.
- openstack --insecure router add subnet "$ROUTER_NAME" "pe1private-subnet"
- echo Selecting a flavour.
- CC_FLAVOR_ID=$( openstack --insecure flavor show "$FLAVOR_NAME" -f value -c id )
- echo Selecting an image.
- CC_IMAGE_ID=$( openstack --insecure image show "$IMAGE_NAME" -f value -c id )
- echo Uploading a key:
- openstack --insecure keypair create --public-key $SSH_PUBLIC_KEY $SSH_KEY_NAME
- echo Getting network ids.
- CC_PUBLIC_NETWORK_ID=$( openstack --insecure network show vlan1288 -f value -c id )
- CC_PRIVATE_NETWORK_ID=$( openstack --insecure network show "$PRIVATE_NETWORK_NAME" -f value -c id )
- echo Creating security group:
- openstack --insecure security group create --description 'Network access for our first instance.' "$SECURITY_GROUP_NAME"
- echo Getting security group id.
- CC_SECURITY_GROUP_ID=$( openstack --insecure security group show "$SECURITY_GROUP_NAME" -f value -c id )
- echo Creating security group rule for ssh access:
- openstack --insecure security group rule create \
- --ingress \
- --protocol tcp \
- --dst-port 22 \
- --remote-ip "$CC_REMOTE_CIDR_NETWORK" \
- "$CC_SECURITY_GROUP_ID"
- echo Creating security group rule for icmp access:
- openstack --insecure security group rule create \
- --ingress \
- --protocol icmp \
- --remote-ip "$CC_REMOTE_CIDR_NETWORK" \
- "$CC_SECURITY_GROUP_ID"
- echo Booting first instance:
- openstack --insecure server create \
- --flavor "$CC_FLAVOR_ID" \
- --image "$CC_IMAGE_ID" \
- --key-name "$SSH_KEY_NAME" \
- --security-group default \
- --security-group "$SECURITY_GROUP_NAME" \
- --nic "net-id=$CC_PRIVATE_NETWORK_ID" \
- "$INSTANCE_NAME"
- INSTANCE_STATUS=$( openstack --insecure server show "$INSTANCE_NAME" -f value -c status )
- sleep 10;
- echo You can now connect to your instance using the following command:
- echo "ssh ubuntu@$CC_PUBLIC_IP";
- sleep 50;
- echo Sending the mysql script to the instance and giving it execute rights
- sudo scp installMysql.sh ubuntu@$CC_PUBLIC_IP:~/installMysql.sh
- ssh ubuntu@$CC_PUBLIC_IP 'chmod u+x installMysql.sh'
- ssh ubuntu@$CC_PUBLIC_IP './installMysql.sh'
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement