MSO K8S-Helm Parameterization
[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 -s false            : Exclude services (default: true)
12 -i [INSTANCE]       : ONAP deployment instance # (default: 1)
13 -a [APP]            : Specify a specific ONAP component (default: all)
14                       from the following choices:
15                       sdc, aai ,mso, message-router, robot,
16                       vid, sdnc, portal, policy, appc
17 EOF
18 }
19
20 create_namespace() {
21   kubectl create namespace $1-$2
22 }
23
24 create_registry_key() {
25   kubectl --namespace $1-$2 create secret docker-registry $3 --docker-server=$4 --docker-username=$5 --docker-password=$6 --docker-email=$7
26 }
27
28 create_service() {
29   sed -i -- 's/nodePort: [0-9]\{2\}[02468]\{1\}/nodePort: '"$3"'/g' ../$2/all-services.yaml
30   sed -i -- 's/nodePort: [0-9]\{2\}[13579]\{1\}/nodePort: '"$4"'/g' ../$2/all-services.yaml
31   kubectl --namespace $1-$2 create -f ../$2/all-services.yaml
32   mv ../$2/all-services.yaml-- ../$2/all-services.yaml
33 }
34
35
36 create_onap_helm() {
37   helm install ../$2/ --name $2
38 }
39
40 configure_app() {
41   # if previous configuration exists put back original template file
42   for file in $3/*.yaml; do
43     if [ -e "$file-template" ]; then
44       mv "$file-template" "${file%}"
45     fi
46   done
47   
48   if [ -e "$2/Chart.yaml" ]; then
49     sed -i -- 's/nodePort: [0-9]\{2\}[02468]\{1\}/nodePort: '"$4"'/g' $3/all-services.yaml
50     sed -i -- 's/nodePort: [0-9]\{2\}[13579]\{1\}/nodePort: '"$5"'/g' $3/all-services.yaml
51     sed -i "s/onap-/$1-/g" ../$2/values.yaml
52   fi
53
54
55   # replace the default 'onap' namespace qualification of K8s hostnames within
56   # the config files
57   # note: this will create a '-template' file within the component's directory
58   #       this is not ideal and should be addressed (along with the replacement
59   #       of sed commands for configuration) by the future configuration
60   #       user stories (ie. OOM-51 to OOM-53)
61   find $3 -type f -exec sed -i -- "s/onap-/$1-/g" {} \;
62
63   # replace the default '/dockerdata-nfs/onapdemo' volume mount paths
64   find $3 -iname "*.yaml" -type f -exec sed -i -e 's/dockerdata-nfs\/[a-zA-Z0-9\\-]*\//dockerdata-nfs\/'"$1"'\//g' {} \;
65   rm -f $3/*.yaml-e
66 }
67
68
69 #MAINs
70 NS=
71 INCL_SVC=true
72 APP=
73 INSTANCE=1
74 MAX_INSTANCE=5
75 DU=$ONAP_DOCKER_USER
76 DP=$ONAP_DOCKER_PASS
77
78 while getopts ":n:u:s:i:a:du:dp:" PARAM; do
79   case $PARAM in
80     u)
81       usage
82       exit 1
83       ;;
84     n)
85       NS=${OPTARG}
86       ;;
87     s)
88       INCL_SVC=${OPTARG}
89       ;;
90     i)
91       INSTANCE=${OPTARG}
92       ;;
93     a)
94       APP=${OPTARG}
95       if [[ -z $APP ]]; then
96         usage
97         exit 1
98       fi
99       ;;
100     du)
101       DU=${OPTARG}
102       ;;
103     dp)
104       DP=${OPTARG}
105       ;;
106     ?)
107       usage
108       exit
109       ;;
110   esac
111 done
112
113 if [[ -z $NS ]]; then
114   usage
115   exit 1
116 fi
117
118 if [[ ! -z "$APP" ]]; then
119   ONAP_APPS=($APP)
120 fi
121
122 if [[ "$INCL_SVC" == true ]]; then
123
124   if [ "$INSTANCE" -gt "$MAX_INSTANCE" ];then
125     printf "\n********** You choose to create ${INSTANCE}th instance of ONAP \n"
126     printf "\n********** Due to port allocation only ${MAX_INSTANCE} instances of ONAP is allowed per kubernetes deployment\n"
127     exit 1
128   fi
129
130   start=$((300+2*INSTANCE))
131   end=$((start+1))
132   printf "\n********** Creating instance ${INSTANCE} of ONAP with port range ${start}00 and ${end}99\n"
133
134 fi
135
136 printf "\n********** Creating ONAP: ${ONAP_APPS[*]}\n"
137
138 for i in ${ONAP_APPS[@]}; do
139   printf "\nCreating namespace **********\n"
140   create_namespace $NS $i
141
142   printf "\nCreating registry secret **********\n"
143   create_registry_key $NS $i $ONAP_DOCKER_REGISTRY_KEY $ONAP_DOCKER_REGISTRY $DU $DP $ONAP_DOCKER_MAIL
144
145   if [[ "$INCL_SVC" == true ]]; then
146     printf "\nCreating service **********\n"
147     create_service $NS $i $start $end
148   fi
149
150   printf "\n"
151 done
152
153 printf "\n\n********** Creating deployments for ${ONAP_APPS[*]} ********** \n"
154 for i in ${ONAP_APPS[@]}; do
155   _FILES_PATH=$(echo ../$i)
156   configure_app $NS $i $_FILES_PATH $start $end
157   /bin/bash $i.sh $NS $i 'create'
158 done
159
160 for i in ${HELM_APPS[@]}; do
161   printf "\nCreating namespace **********\n"
162   create_namespace $NS $i 
163
164   printf "\nCreating registry secret **********\n"
165   create_registry_key $NS $i $ONAP_DOCKER_REGISTRY_KEY $ONAP_DOCKER_REGISTRY $DU $DP $ONAP_DOCKER_MAIL
166
167   printf "\nCreating deployments and services **********\n"
168   _FILES_PATH=$(echo ../$i/templates)
169   configure_app $NS $i $_FILES_PATH $start $end
170   create_onap_helm $NS $i
171
172   printf "\n"
173 done
174
175 printf "\n**** Done ****\n"
176