#!/bin/bash # Copyright 2019 AT&T Intellectual Property. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" NO_PROMPT=0 RANDOM_PREFIX="ONAP" RANDOM_STRING="$RANDOM_PREFIX"-`cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-zA-Z0-9' | head -c 4` NFS_NAME= NFS_RG= NFS_VM_SIZE= NFS_LOCATION= SUBNET_CIDR= ADMIN_USER= BUILD_DIR= NFS_VNET_NAME= PUBLIC_KEY= USER_PUBLIC_IP_PREFIX= NFS_SUBNET_NAME= AKS_POD_CIDR= NFS_DISK_SIZE= function check_required_parameter() { # arg1 = parameter # arg2 = parameter name if [ -z "$1" ]; then echo "$2 was not was provided. This parameter is required." exit 1 fi } function check_optional_paramater() { # arg1 = parameter # arg2 = parameter name if [ -z "$1" ]; then echo "$2" else echo "$1" fi } while test $# -gt 0; do case "$1" in -h|--help) echo "./create_nfs.sh [options]" echo " " echo " " echo "required:" echo "--public-key public key to add for admin user [required]" echo "--user-public-ip public ip that will be granted access to VM [required]" echo "-l, --location location to deploy VM [required]" echo "-u, --admin-user admin user to create on VM [required]" echo "--aks-node-cidr CIDR for Kubernetes nodes [required]. This is used during the NFS deploy to grant access to the NFS server from Kubernetes." echo " " echo "additional options:" echo "-f, --no-prompt executes with no prompt for confirmation" echo "-h, --help provide brief overview of script" echo "-n, --name VM name [optional]" echo "-g, --resource-group resource group that will be created [optional]" echo "-s, --size Azure flavor size for VM [optional]" echo "-c, --cidr cidr for VNET to create for VM [optional]." echo "-d, --directory directory to store cloud config data [optional]" echo "--vnet-name name of Vnet to create for VM [optional]" echo "--nfs-subnet-name subnet name created on VNET [optional]" echo "--nfs-disk-size size of external disk to be mounted on NFS VM [optional]" echo "" exit 0 ;; -f|--no-prompt) shift NO_PROMPT=1 ;; -n|--name) shift NFS_NAME=$1 shift ;; -g|--resource-group) shift NFS_RG=$1 shift ;; -s|--size) shift NFS_VM_SIZE=$1 shift ;; -l|--location) shift NFS_LOCATION=$1 shift ;; -c|--cidr) shift SUBNET_CIDR=$1 shift ;; -u|--admin-user) shift ADMIN_USER=$1 shift ;; -d|--directory) shift BUILD_DIR=$1 shift ;; --vnet-name) shift NFS_VNET_NAME=$1 shift ;; --public-key) shift PUBLIC_KEY=$1 shift ;; --user-public-ip) shift USER_PUBLIC_IP_PREFIX=$1 shift ;; --aks-node-cidr) shift AKS_POD_CIDR=$1 shift ;; --nfs-subnet-name) shift NFS_SUBNET_NAME=$1 shift ;; --nfs-disk-size) shift NFS_DISK_SIZE=$1 shift ;; *) echo "Unknown Argument $1. Try running with --help." exit 0 ;; esac done check_required_parameter "$ADMIN_USER" "--admin-user" check_required_parameter "$PUBLIC_KEY" "--public-key" check_required_parameter "$NFS_LOCATION" "--location" check_required_parameter "$USER_PUBLIC_IP_PREFIX" "--user-public-ip" check_required_parameter "$AKS_POD_CIDR" "--aks-node-cidr" NFS_RG=$(check_optional_paramater "$NFS_RG" $RANDOM_STRING"-NFS-RG") NFS_NAME=$(check_optional_paramater "$NFS_NAME" $RANDOM_STRING"-NFS") NFS_VM_SIZE=$(check_optional_paramater "$NFS_VM_SIZE" "Standard_DS4_v2") SUBNET_CIDR=$(check_optional_paramater "$SUBNET_CIDR" "174.0.0.0/24") BUILD_DIR=$(check_optional_paramater "$BUILD_DIR" /tmp/nfs-$RANDOM_STRING) NFS_VNET_NAME=$(check_optional_paramater "$NFS_VNET_NAME" $RANDOM_STRING"-NFS-VNET") NFS_SUBNET_NAME=$(check_optional_paramater "$NFS_SUBNET_NAME" $RANDOM_STRING"-NFS-VNET-SUBNET") NFS_DISK_SIZE=$(check_optional_paramater "$NFS_DISK_SIZE" "256") if [ $NO_PROMPT = 0 ]; then read -p "Would you like to proceed? [y/n]" -n 1 -r echo " " if [[ ! $REPLY =~ ^[Yy]$ ]] then exit 0 fi fi set -x set -e NFS_IMAGE="UbuntuLTS" NFS_SECURITY_GROUP=$NFS_NAME"-SG" DATA_FILE=$BUILD_DIR/cloud-cfg.yaml if [ ! -d $BUILD_DIR ]; then echo "running script standalone..." mkdir -p "$BUILD_DIR" fi $DIR/create_resource_group.sh "$NFS_RG" "$NFS_LOCATION" cat > $DATA_FILE <> /etc/exports - mkdir /dockerdata-nfs - chmod 777 -R /dockerdata-nfs - chown nobody:nogroup /dockerdata-nfs - exportfs -ra - systemctl restart nfs-kernel-server EOF az network nsg create --resource-group "$NFS_RG" \ --name "$NFS_SECURITY_GROUP" $DIR/create_sg_rule.sh "$NFS_RG" "$NFS_SECURITY_GROUP" '*' "22" "$USER_PUBLIC_IP_PREFIX" '*' '*' "SSH" "100" az vm create --name "$NFS_NAME" \ --resource-group "$NFS_RG" \ --size "$NFS_VM_SIZE" \ --os-disk-size-gb "$NFS_DISK_SIZE" \ --admin-username "$ADMIN_USER" \ --ssh-key-value @"$PUBLIC_KEY" \ --image "UbuntuLTS" \ --location "$NFS_LOCATION" \ --subnet-address-prefix "$SUBNET_CIDR" \ --subnet "$NFS_SUBNET_NAME" \ --vnet-address-prefix "$SUBNET_CIDR" \ --vnet-name "$NFS_VNET_NAME" \ --custom-data "$DATA_FILE" \ --nsg "$NFS_SECURITY_GROUP" echo "" az network vnet subnet update --resource-group "$NFS_RG" \ --name "$NFS_SUBNET_NAME" \ --vnet-name "$NFS_VNET_NAME" \ --network-security-group "$NFS_SECURITY_GROUP"