Merge "Refine Agent.sh and Docs"
[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 [ -z ${LOOKUP} ]; then
52                     DEF= 
53                 else 
54                     DEF=$(echo ${LOOKUP} | tail -1 | cut -f 4 -d ' ')
55                 fi
56                 ;;
57          APP_FQI)    PROMPT="App's FQI";; 
58          APP_FQDN)   PROMPT="App's Root FQDN";; 
59          VOLUME)     PROMPT="APP's AAF Configuration Volume";;
60          DRIVER)     PROMPT=$V;DEF=local;;
61          VERSION)    PROMPT="CADI Version";DEF=$CADI_VERSION;;
62          LATITUDE|LONGITUDE) PROMPT="$V of Node";;
63          *)          PROMPT=$V;;
64       esac
65       if [ "$DEF" = "" ]; then
66            PROMPT="$PROMPT: "
67       else 
68            PROMPT="$PROMPT ($DEF): "
69       fi
70       read -p "$PROMPT" VAR 
71       if [ "$VAR" = "" ]; then
72          if [ "$DEF" = "" ]; then
73             if [ "$V" != "HOSTNAME" ]; then
74               echo "agent.sh needs each value queried.  Please start again."
75               exit
76             fi
77          else
78             VAR=$DEF
79          fi
80       fi
81       echo "$V=$VAR" >> ./aaf.props
82       declare "$V"="$VAR"
83    fi
84 done
85 . ./aaf.props
86
87 # Make sure Container Volume exists
88 if [ "$($DOCKER volume ls | grep ${VOLUME})" = "" ]; then
89   echo -n "Creating Volume: " 
90   $DOCKER volume create -d ${DRIVER} ${VOLUME}
91 fi
92
93 if [ -n "$DOCKER_REPOSITORY" ]; then
94   PREFIX="$DOCKER_REPOSITORY/"
95 else
96   PREFIX=""
97 fi 
98
99 function run_it() {
100   if [ -n "${DUSER}" ]; then
101     USER_LINE="--user ${DUSER}"
102   fi
103   $DOCKER run  -it  --rm \
104     ${USER_LINE} \
105     -v "${VOLUME}:/opt/app/osaaf" \
106     --add-host="$AAF_FQDN:$AAF_FQDN_IP" \
107     --env AAF_FQDN=${AAF_FQDN} \
108     --env DEPLOY_FQI=${DEPLOY_FQI} \
109     --env DEPLOY_PASSWORD=${DEPLOY_PASSWORD} \
110     --env APP_FQI=${APP_FQI} \
111     --env APP_FQDN=${APP_FQDN} \
112     --env LATITUDE=${LATITUDE} \
113     --env LONGITUDE=${LONGITUDE} \
114     --name aaf-agent-$USER \
115     "$PREFIX"onap/aaf/aaf_agent:$VERSION \
116     bash -c "bash /opt/app/aaf_config/bin/agent.sh $PARAMS"
117 }
118
119 PARAMS=$@
120 case "$1" in 
121   bash)
122     PARAMS="&& cd /opt/app/osaaf/local && exec bash"
123     run_it -it --rm  
124     ;;
125   taillog)
126     run_it -it --rm 
127     ;;
128   *)
129     run_it --rm 
130     ;;
131 esac
132