Merge "scripts & kubectl are executable for consul-agent"
[oom.git] / kubernetes / oneclick / createAll.bash
1 #!/bin/bash
2
3 . $(dirname "$0")/setenv.bash
4
5
6 usage() {
7   cat <<EOF
8 Usage: $0 [PARAMs]
9 -u                  : Display usage
10 -n [NAMESPACE]      : Kubernetes namespace (required)
11 -v [VALUES]         : HELM values filepath (usefull when deploying one component at a time)
12 -l [LOCATION]       : Location of oom project
13 -i [INSTANCE]       : ONAP deployment instance # (default: 1)
14 -a [APP]            : Specify a specific ONAP component (default: all)
15                       from the following choices:
16                       sdc, aai ,mso, message-router, robot, vid, aaf, uui
17                       sdnc, portal, policy, appc, multicloud, clamp, consul, vnfsdk
18 EOF
19 }
20
21 check_return_code(){
22   ret=$?
23   if [ $ret -ne 0 ]; then
24     printf "The command $1 returned with error code $ret \n" 1>&2
25     exit $ret
26   fi
27 }
28
29 create_service_account() {
30   cmd=`echo kubectl create clusterrolebinding $1-$2-admin-binding --clusterrole=cluster-admin --serviceaccount=$1-$2:default`
31   eval ${cmd}
32   check_return_code $cmd
33 }
34
35 create_namespace() {
36   cmd=`echo kubectl create namespace $1-$2`
37   eval ${cmd}
38   check_return_code $cmd
39 }
40
41 create_registry_key() {
42   cmd=`echo kubectl --namespace $1-$2 create secret docker-registry $3 --docker-server=$4 --docker-username=$5 --docker-password=$6 --docker-email=$7`
43   eval ${cmd}
44   check_return_code $cmd
45 }
46
47 configure_dcaegen2() {
48   if [ ! -s "$OPENSTACK_PRIVATE_KEY_PATH" ]
49   then
50     echo "ERROR: $OPENSTACK_PRIVATE_KEY_PATH does not exist or is empty.  Cannot launch dcae gen2."
51     return 1
52   fi
53
54   cmd=`echo kubectl --namespace $1-$2 create secret generic $2-openstack-ssh-private-key --from-file=key=${OPENSTACK_PRIVATE_KEY_PATH}`
55   eval ${cmd}
56   check_return_code $cmd
57
58   if [ ! -s "$DCAEGEN2_CONFIG_INPUT_FILE_PATH" ]
59   then
60     echo "ERROR: $DCAEGEN2_CONFIG_INPUT_FILE_PATH does not exist or is empty.  Cannot launch dcae gen2."
61     return 1
62   fi
63
64   cmd=`echo kubectl --namespace $1-$2 create configmap $2-config-inputs --from-file=inputs.yaml=${DCAEGEN2_CONFIG_INPUT_FILE_PATH}`
65   eval ${cmd}
66   check_return_code $cmd
67 }
68
69 create_onap_helm() {
70   HELM_VALUES_ADDITION=""
71   if [[ ! -z $HELM_VALUES_FILEPATH ]]; then
72     HELM_VALUES_ADDITION="--values=$HELM_VALUES_FILEPATH"
73   fi
74   # Have to put a check for dcaegen2 because it requires external files to helm
75   # which should not be part of the Chart.
76   if [ "$2" = "dcaegen2" ];
77   then
78     configure_dcaegen2 $1 $2
79     local result=$?
80     if [ $result -ne 0 ]
81     then
82       echo "ERROR: dcaegen2 failed to configure: Pre-requisites not met.  Skipping deploying it and continue"
83       return
84     fi
85   fi
86
87   cmd=`echo helm install $LOCATION/$2/ --name $1-$2 --namespace $1 --set nsPrefix=$1,nodePortPrefix=$3 ${HELM_VALUES_ADDITION}`
88   eval ${cmd}
89   check_return_code $cmd
90 }
91
92 #MAINs
93 NS=
94 HELM_VALUES_FILEPATH=""
95 LOCATION="../"
96 INCL_SVC=true
97 APP=
98 INSTANCE=1
99 MAX_INSTANCE=5
100 DU=$ONAP_DOCKER_USER
101 DP=$ONAP_DOCKER_PASS
102
103 while getopts ":n:u:s:i:a:du:dp:l:v:" PARAM; do
104   case $PARAM in
105     u)
106       usage
107       exit 1
108       ;;
109     n)
110       NS=${OPTARG}
111       ;;
112     v)
113       HELM_VALUES_FILEPATH=${OPTARG}
114       ;;
115     i)
116       INSTANCE=${OPTARG}
117       ;;
118     l)
119       LOCATION=${OPTARG}
120       ;;
121     a)
122       APP=${OPTARG}
123       if [[ -z $APP ]]; then
124         usage
125         exit 1
126       fi
127       ;;
128     du)
129       DU=${OPTARG}
130       ;;
131     dp)
132       DP=${OPTARG}
133       ;;
134     ?)
135       usage
136       exit
137       ;;
138   esac
139 done
140
141 if [[ -z $NS ]]; then
142   usage
143   exit 1
144 fi
145
146 if [[ ! -z "$APP" ]]; then
147   HELM_APPS=($APP)
148 fi
149
150
151 if [ "$INSTANCE" -gt "$MAX_INSTANCE" ];then
152   printf "\n********** You choose to create ${INSTANCE}th instance of ONAP \n"
153   printf "\n********** Due to port allocation only ${MAX_INSTANCE} instances of ONAP is allowed per kubernetes deployment\n"
154   exit 1
155 fi
156
157 start=$((300+2*INSTANCE))
158 end=$((start+1))
159
160 printf "\n********** Creating instance ${INSTANCE} of ONAP with port range ${start}00 and ${end}99\n"
161
162
163 printf "\n********** Creating ONAP: ${ONAP_APPS[*]}\n"
164
165
166 printf "\n\n********** Creating deployments for ${HELM_APPS[*]} ********** \n"
167
168 for i in ${HELM_APPS[@]}; do
169   printf "\nCreating namespace **********\n"
170   create_namespace $NS $i
171
172   printf "\nCreating service account **********\n"
173   create_service_account $NS $i
174
175   printf "\nCreating registry secret **********\n"
176   create_registry_key $NS $i ${NS}-docker-registry-key $ONAP_DOCKER_REGISTRY $DU $DP $ONAP_DOCKER_MAIL
177
178   printf "\nCreating deployments and services **********\n"
179   create_onap_helm $NS $i $start
180
181   printf "\n"
182 done
183
184 printf "\n**** Done ****\n"