d319de4d9d5e29339bef0f04cfd8a96b70d88bb4
[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.10-SNAPSHOT}
32
33 for V in VERSION DOCKER_REPOSITORY HOSTNAME 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          VERSION)    PROMPT="CADI Version";DEF=$CADI_VERSION;;
68          LATITUDE|LONGITUDE) PROMPT="$V of Node";;
69          *)          PROMPT=$V;;
70       esac
71       if [ "$DEF" = "" ]; then
72            PROMPT="$PROMPT: "
73       else 
74            PROMPT="$PROMPT ($DEF): "
75       fi
76       read -p "$PROMPT" VAR 
77       if [ "$VAR" = "" ]; then
78          if [ "$DEF" = "" ]; then
79             if [ "$V" != "HOSTNAME" ]; then
80               echo "agent.sh needs each value queried.  Please start again."
81               exit
82             fi
83          else
84             VAR=$DEF
85          fi
86       fi
87       echo "$V=$VAR" >> ./aaf.props
88       declare "$V"="$VAR"
89    fi
90 done
91 . ./aaf.props
92
93 # Make sure Container Volume exists
94 if [ "$($DOCKER volume ls | grep ${VOLUME})" = "" ]; then
95   echo -n "Creating Volume: " 
96   $DOCKER volume create -d ${DRIVER} ${VOLUME}
97 fi
98
99 if [ -n "$DOCKER_REPOSITORY" ]; then
100   PREFIX="$DOCKER_REPOSITORY/"
101 else
102   PREFIX=""
103 fi 
104
105 function run_it() {
106   if [ -n "${DUSER}" ]; then
107     USER_LINE="--user ${DUSER}"
108   fi
109   $DOCKER run  -it  --rm \
110     ${USER_LINE} \
111     -v "${VOLUME}:/opt/app/osaaf" \
112     --add-host="$AAF_FQDN:$AAF_FQDN_IP" \
113     --env AAF_FQDN=${AAF_FQDN} \
114     --env DEPLOY_FQI=${DEPLOY_FQI} \
115     --env DEPLOY_PASSWORD=${DEPLOY_PASSWORD} \
116     --env APP_FQI=${APP_FQI} \
117     --env APP_FQDN=${APP_FQDN} \
118     --env LATITUDE=${LATITUDE} \
119     --env LONGITUDE=${LONGITUDE} \
120     --name aaf-agent-$USER \
121     "$PREFIX"onap/aaf/aaf_agent:$VERSION \
122     bash -c "bash /opt/app/aaf_config/bin/agent.sh $PARAMS"
123 }
124
125 PARAMS=$@
126 case "$1" in 
127   bash)
128     PARAMS="&& cd /opt/app/osaaf/local && exec bash"
129     run_it -it --rm  
130     ;;
131   taillog)
132     run_it -it --rm 
133     ;;
134   *)
135     run_it --rm 
136     ;;
137 esac
138