5012a52d200c0f47e5a4f1ea341f8e40963b80b7
[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,
17                       vid, sdnc, portal, policy, appc
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
30 create_namespace() {
31   cmd=`echo kubectl create namespace $1-$2`
32   eval ${cmd}
33   check_return_code $cmd
34 }
35
36 create_registry_key() {
37   cmd=`echo kubectl --namespace $1-$2 create secret docker-registry $3 --docker-server=$4 --docker-username=$5 --docker-password=$6 --docker-email=$7`
38   eval ${cmd}
39   check_return_code $cmd
40 }
41
42 create_certs_secret() {
43   if [ -d $LOCATION/config/certs/$i/ ]; then
44     printf "\nCreating certs and keys secret **********\n"
45     _CERTS_FILES=$(find $LOCATION/config/certs/$2/ -type f | awk '$0="--from-file="$0' ORS=' ')
46     kubectl create secret generic secret-$1-$2 $_CERTS_FILES -n $1-$2
47   fi
48 }
49
50 create_onap_helm() {
51   HELM_VALUES_ADDITION=""
52   if [[ ! -z $HELM_VALUES_FILEPATH ]]; then
53     HELM_VALUES_ADDITION="--values=$HELM_VALUES_FILEPATH"
54   fi
55   cmd=`echo helm install $LOCATION/$2/ --name $1-$2 --namespace $1 --set nsPrefix=$1,nodePortPrefix=$3 ${HELM_VALUES_ADDITION}`
56   eval ${cmd}
57   check_return_code $cmd
58 }
59
60 #MAINs
61 NS=
62 HELM_VALUES_FILEPATH=""
63 LOCATION="../"
64 INCL_SVC=true
65 APP=
66 INSTANCE=1
67 MAX_INSTANCE=5
68 DU=$ONAP_DOCKER_USER
69 DP=$ONAP_DOCKER_PASS
70
71 while getopts ":n:u:s:i:a:du:dp:l:v:" PARAM; do
72   case $PARAM in
73     u)
74       usage
75       exit 1
76       ;;
77     n)
78       NS=${OPTARG}
79       ;;
80     v)
81       HELM_VALUES_FILEPATH=${OPTARG}
82       ;;
83     i)
84       INSTANCE=${OPTARG}
85       ;;
86     l)
87       LOCATION=${OPTARG}
88       ;;
89     a)
90       APP=${OPTARG}
91       if [[ -z $APP ]]; then
92         usage
93         exit 1
94       fi
95       ;;
96     du)
97       DU=${OPTARG}
98       ;;
99     dp)
100       DP=${OPTARG}
101       ;;
102     ?)
103       usage
104       exit
105       ;;
106   esac
107 done
108
109 if [[ -z $NS ]]; then
110   usage
111   exit 1
112 fi
113
114 if [[ ! -z "$APP" ]]; then
115   HELM_APPS=($APP)
116 fi
117
118
119 if [ "$INSTANCE" -gt "$MAX_INSTANCE" ];then
120   printf "\n********** You choose to create ${INSTANCE}th instance of ONAP \n"
121   printf "\n********** Due to port allocation only ${MAX_INSTANCE} instances of ONAP is allowed per kubernetes deployment\n"
122   exit 1
123 fi
124
125 start=$((300+2*INSTANCE))
126 end=$((start+1))
127
128 printf "\n********** Creating instance ${INSTANCE} of ONAP with port range ${start}00 and ${end}99\n"
129
130
131 printf "\n********** Creating ONAP: ${ONAP_APPS[*]}\n"
132
133
134 printf "\n\n********** Creating deployments for ${HELM_APPS[*]} ********** \n"
135
136 for i in ${HELM_APPS[@]}; do
137   printf "\nCreating namespace **********\n"
138   create_namespace $NS $i
139
140   printf "\nCreating registry secret **********\n"
141   create_registry_key $NS $i ${NS}-docker-registry-key $ONAP_DOCKER_REGISTRY $DU $DP $ONAP_DOCKER_MAIL
142
143   create_certs_secret $NS $i
144
145   printf "\nCreating deployments and services **********\n"
146   create_onap_helm $NS $i $start
147
148   printf "\n"
149 done
150
151 printf "\n**** Done ****\n"