aboutsummaryrefslogtreecommitdiffstats
path: root/deployment/aks/create_nfs.sh
blob: 8262d1e8cd31b2cdfe19c6b451e7fe2a066c90d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/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 <<EOF
#cloud-config
package_upgrade: true
packages:
  - nfs-kernel-server
  - portmap
runcmd:
  - echo "/dockerdata-nfs $AKS_POD_CIDR(rw,async,no_root_squash,no_subtree_check)" >> /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"