Move AAF Poms to 2.1.16
[aaf/authz.git] / auth / docker / agent.sh
1 #!/bin/bash
2 #########
3 #  ============LICENSE_START====================================================
4 #  org.onap.aaf
5 #  ===========================================================================
6 #  Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
7 #  ===========================================================================
8 #  Licensed under the Apache License, Version 2.0 (the "License");
9 #  you may not use this file except in compliance with the License.
10 #  You may obtain a copy of the License at
11 #
12 #       http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #  Unless required by applicable law or agreed to in writing, software
15 #  distributed under the License is distributed on an "AS IS" BASIS,
16 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #  See the License for the specific language governing permissions and
18 #  limitations under the License.
19 #  ============LICENSE_END====================================================
20 #
21
22
23 # Fill out "aaf.props" if not filled out already
24 if [ ! -e aaf.props ]; then
25   > ./aaf.props
26 fi
27  
28 . ./aaf.props
29
30 DOCKER=${DOCKER:=docker}
31 CADI_VERSION=${CADI_VERSION:=2.1.16}
32
33 for V in VERSION DOCKER_REPOSITORY HOSTNAME CONTAINER_NS AAF_FQDN AAF_FQDN_IP DEPLOY_FQI APP_FQDN APP_FQI VOLUME DRIVER LATITUDE LONGITUDE; do
34    if [ "$(grep $V ./aaf.props)" = "" ]; then
35       unset DEF
36       case $V in
37          DOCKER_REPOSITORY) 
38                 PROMPT="Docker Repo"
39                 DEF="nexus3.onap.org:10003"
40                 ;;
41          HOSTNAME) 
42                 PROMPT="HOSTNAME (blank for Default)"
43                 DEF=""
44                 ;;
45          AAF_FQDN)   PROMPT="AAF's FQDN";;
46          DEPLOY_FQI) PROMPT="Deployer's FQI";;
47          AAF_FQDN_IP)
48                 # Need AAF_FQDN's IP, because not might not be available in mini-container
49                 PROMPT="AAF FQDN IP"
50                 LOOKUP=$(host "${AAF_FQDN}" | grep "has address")
51                 if [ -n "${LOOKUP}" ]; then
52                     DEF=$(echo ${LOOKUP} | tail -1 | cut -f 4 -d ' ')
53                 fi
54                 ;;
55          APP_FQDN)   PROMPT="App's Root FQDN";;
56          APP_FQI)    PROMPT="App's FQI"
57                      if [[ "${APP_FQDN}" != *"."* ]]; then
58                        DEF="${APP_FQDN}@${APP_FQDN}.onap.org"
59                      fi
60                      ;; 
61          VOLUME)     PROMPT="APP's AAF Configuration Volume"
62                      if [[ "${APP_FQDN}" != *"."* ]]; then
63                        DEF="${APP_FQDN}_config"
64                      fi
65                  ;;
66          DRIVER)     PROMPT=$V;DEF=local;;
67          CONTAINER_NS)     
68                      PROMPT=$V;DEF=onap;;
69          VERSION)    PROMPT="CADI Version";DEF=$CADI_VERSION;;
70          LATITUDE|LONGITUDE) PROMPT="$V of Node";;
71          *)          PROMPT=$V;;
72       esac
73       if [ "$DEF" = "" ]; then
74            PROMPT="$PROMPT: "
75       else 
76            PROMPT="$PROMPT ($DEF): "
77       fi
78       read -p "$PROMPT" VAR 
79       if [ "$VAR" = "" ]; then
80          if [ "$DEF" = "" ]; then
81             if [ "$V" != "HOSTNAME" ]; then
82               echo "agent.sh needs each value queried.  Please start again."
83               exit
84             fi
85          else
86             VAR=$DEF
87          fi
88       fi
89       echo "$V=$VAR" >> ./aaf.props
90       declare "$V"="$VAR"
91    fi
92 done
93 . ./aaf.props
94
95 # Make sure Container Volume exists
96 if [ "$($DOCKER volume ls | grep ${VOLUME})" = "" ]; then
97   echo -n "Creating Volume: " 
98   $DOCKER volume create -d ${DRIVER} ${VOLUME}
99 fi
100
101 if [ -n "$DOCKER_REPOSITORY" ]; then
102   PREFIX="$DOCKER_REPOSITORY/"
103 else
104   PREFIX=""
105 fi 
106
107 function run_it() {
108   if [ -n "${DUSER}" ]; then
109     USER_LINE="--user ${DUSER}"
110   fi
111   $DOCKER run  -it  --rm \
112     ${USER_LINE} \
113     -v "${VOLUME}:/opt/app/osaaf" \
114     --add-host="$AAF_FQDN:$AAF_FQDN_IP" \
115     --env AAF_FQDN=${AAF_FQDN} \
116     --env DEPLOY_FQI=${DEPLOY_FQI} \
117     --env DEPLOY_PASSWORD=${DEPLOY_PASSWORD} \
118     --env APP_FQI=${APP_FQI} \
119     --env APP_FQDN=${APP_FQDN} \
120     --env LATITUDE=${LATITUDE} \
121     --env LONGITUDE=${LONGITUDE} \
122     --env aaf_locator_container_ns=${CONTAINER_NS} \
123     --env aaf_locator_container=docker \
124     --link aaf-service --link aaf-locate --link aaf-oauth --link aaf-cm \
125     --name aaf-agent-$USER \
126     "$PREFIX"onap/aaf/aaf_agent:$VERSION \
127     bash -c "bash /opt/app/aaf_config/bin/agent.sh $PARAMS"
128 }
129
130 function sso {
131   if [ -n "$2" ]; then
132     echo "$1=$2" >> $HOME/.aaf/sso.props
133   fi
134 }
135
136 function reset_sso {
137     mkdir -p ~/.aaf
138     > $HOME/.aaf/sso.props
139     sso aaf_locate_url "https://$AAF_FQDN:8095"
140     sso cadi_latitude "$LATITUDE"
141     sso cadi_longitude "$LONGITUDE"
142     sso cadi_loglevel "DEBUG"
143     TRUSTSTORE="$(ls truststore*.jks | tail -1)"
144     if [ -z "$TRUSTSTORE" ]; then
145       echo "Place a truststore*.jar which has YOUR CA in it here"
146       exit
147     fi
148     sso cadi_truststore "${PWD}/${TRUSTSTORE}"
149     sso cadi_truststore_password changeit
150 }
151
152 PARAMS=$@
153 case "$1" in 
154   bash)
155     PARAMS="&& cd /opt/app/osaaf/local && exec bash"
156     run_it -it --rm  
157     ;;
158   taillog)
159     run_it -it --rm 
160     ;;
161   aafcli) 
162     shift
163     reset_sso
164     if [ -f aaf-cadi-aaf-$VERSION-full.jar ]; then
165       java -Dcadi_prop_files="$HOME/.aaf/sso.props" -jar aaf-cadi-aaf-$VERSION-full.jar $@
166     else 
167       echo "For local use, you need to have 'aaf-cadi-aaf-$VERSION-full.jar' (or newer)"
168     fi
169     ;;
170   local) 
171     shift
172     CMD="$1"
173     if [ -z "$2" ]; then 
174         CMD="$CMD $APP_FQI $APP_FQDN"
175     else 
176       if [ "-" = "$2" ]; then
177          CMD="$CMD $APP_FQI"
178       else
179          CMD="$CMD $2"
180       fi
181       if [ "-" = "$3" ]; then
182          CMD="$CMD $APP_FQDN"
183       else
184          CMD="$CMD $3"
185       fi
186     fi
187     reset_sso
188     sso aaf_id "$DEPLOY_FQI"
189     sso aaf_password "$DEPLOY_PASSWORD"
190     if [ -f aaf-cadi-aaf-$VERSION-full.jar ]; then
191       java -Dcadi_prop_files="$HOME/.aaf/sso.props" -cp aaf-cadi-aaf-$VERSION-full.jar org.onap.aaf.cadi.configure.Agent $CMD 
192     else 
193       echo "For local use, you need to have 'aaf-cadi-aaf-$VERSION-full.jar' (or newer)"
194     fi
195     ;;
196   *)
197     run_it --rm 
198     ;;
199 esac
200