Refine Container Startup

Issue-ID: AAF-773
Change-Id: Ia198a11eceb9bfacafde26cde97d15949a3fe78c
Signed-off-by: Instrumental <jonathan.gathman@att.com>
diff --git a/auth/auth-cass/cass_init/cmd.sh b/auth/auth-cass/cass_init/cmd.sh
index b26536d..b665012 100644
--- a/auth/auth-cass/cass_init/cmd.sh
+++ b/auth/auth-cass/cass_init/cmd.sh
@@ -149,7 +149,7 @@
 
     # Startup like normal
     echo "Cassandra Startup"
-    exec /usr/local/bin/docker-entrypoint.sh 
+    exec -c "/usr/local/bin/docker-entrypoint.sh"
   ;;
   wait)
     # Wait for initialization.  This can be called from Docker only as a check to make sure it is ready
diff --git a/auth/auth-cass/docker/Dockerfile.cass b/auth/auth-cass/docker/Dockerfile.cass
index 52aa49c..0406411 100644
--- a/auth/auth-cass/docker/Dockerfile.cass
+++ b/auth/auth-cass/docker/Dockerfile.cass
@@ -18,7 +18,7 @@
 #  ============LICENSE_END====================================================
 #
 # Use dbuild.sh input parameter to set registry
-FROM ${REGISTRY}cassandra:3.11
+FROM ${REGISTRY}/cassandra:3.11
 MAINTAINER AAF Team, AT&T 2018
 ENV VERSION=${AAF_VERSION}
 
diff --git a/auth/auth-cass/docker/dbuild.sh b/auth/auth-cass/docker/dbuild.sh
index 2913b1a..17d359c 100644
--- a/auth/auth-cass/docker/dbuild.sh
+++ b/auth/auth-cass/docker/dbuild.sh
@@ -29,10 +29,10 @@
 echo "$0: Building aaf_cass Container for aaf_cass:$VERSION"
 
 # default nexus repo only contains Amd64 images, use docker.io for multi-platform builds
-if [[ $1 && $1 == "docker.io" ]]; then
-    DOCKER_PULL_REGISTRY=''
-else 
-    DOCKER_PULL_REGISTRY='nexus3.onap.org:10001\/'
+if [ $# -gt 0 ]; then
+    if [ "$1" == "-r" ]; then
+      DOCKER_PULL_REGISTRY=$2
+    fi
 fi
 echo "$0: DOCKER_PULL_REGISTRY=${DOCKER_REGISTRY}"
 
diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsService.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsService.java
index e2317a5..02d9351 100644
--- a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsService.java
+++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsService.java
@@ -187,5 +187,4 @@
         }
         return def;
     }
-
 }
diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java
index fe610e5..ee92540 100644
--- a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java
+++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/AbsServiceStarter.java
@@ -19,7 +19,10 @@
  *
  */
 package org.onap.aaf.auth.server;
+import java.io.File;
 import java.io.IOException;
+import java.net.Inet4Address;
+import java.net.UnknownHostException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -29,6 +32,7 @@
 import org.onap.aaf.auth.rserv.RServlet;
 import org.onap.aaf.cadi.Access;
 import org.onap.aaf.cadi.Access.Level;
+import org.onap.aaf.cadi.config.Config;
 import org.onap.aaf.cadi.register.Registrant;
 import org.onap.aaf.cadi.register.Registrar;
 import org.onap.aaf.misc.env.Trans;
@@ -38,6 +42,7 @@
     private Registrar<ENV> registrar;
     private boolean do_register;
     protected AbsService<ENV,TRANS> service;
+	protected String hostname;
 
 
     public AbsServiceStarter(final AbsService<ENV,TRANS> service) {
@@ -52,6 +57,14 @@
         // for Debugging purposes without fear that real clients will start to call your debug instance
         do_register = !"TRUE".equalsIgnoreCase(access().getProperty("aaf_locate_no_register",null));
         _propertyAdjustment();
+        hostname = access().getProperty(Config.HOSTNAME, null);
+        if (hostname==null) {
+            try {
+				hostname = Inet4Address.getLocalHost().getHostName();
+			} catch (UnknownHostException e) {
+				hostname= "cannotBeDetermined";
+			}
+        }
     }
     
     public abstract void _start(RServlet<TRANS> rserv) throws Exception;
@@ -70,6 +83,8 @@
     	ExecutorService es = Executors.newSingleThreadExecutor();
     	Future<?> app = es.submit(this);
         final AbsServiceStarter<?,?> absSS = this;
+        // Docker/K8 may separately create startup Status in this dir for startup 
+        // sequencing.  If so, delete ON EXIT 
     	Runtime.getRuntime().addShutdownHook(new Thread() {
 	      @Override
           public void run() {
@@ -120,7 +135,28 @@
             registrar=null;
         } 
         if (service!=null) {
-            service.destroy();
+            File status = new File("/opt/app/aaf/status/");
+            boolean deleted = false;
+    		if(status.exists()) {
+    			int lastdot = service.app_name.lastIndexOf("aaf.");
+    			String fname;
+    			if(lastdot<0) {
+    				fname = service.app_name + '-' + hostname;
+    			} else {
+    				fname = service.app_name.substring(lastdot).replace('.', '-') 
+    						+ '-' + hostname;
+    			}
+    			status = new File(status, fname);
+    			if(status.exists()) {
+    				status.delete();
+    			}
+    		}
+    		if(deleted) {
+    			service.access.log(Level.INIT, "Deleted Status",status.getAbsolutePath());
+    		} else {
+    			service.access.log(Level.INIT, "Status not deleted: ",status.getAbsolutePath());
+    		}
+        	service.destroy();
         }
     }
 }
diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/JettyServiceStarter.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/JettyServiceStarter.java
index 1256c60..182956c 100644
--- a/auth/auth-core/src/main/java/org/onap/aaf/auth/server/JettyServiceStarter.java
+++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/server/JettyServiceStarter.java
@@ -21,7 +21,6 @@
 package org.onap.aaf.auth.server;
 
 import java.io.IOException;
-import java.net.Inet4Address;
 import java.net.InetAddress;
 import java.util.Properties;
 
@@ -93,10 +92,6 @@
 
     @Override
     public void _start(RServlet<TRANS> rserv) throws Exception {
-        String hostname = access().getProperty(Config.HOSTNAME, null);
-        if (hostname==null) {
-            hostname = Inet4Address.getLocalHost().getHostName();
-        }
         final int port = Integer.parseInt(access().getProperty("port","0"));
         final String keystore = access().getProperty(Config.CADI_KEYSTORE, null);
         final int IDLE_TIMEOUT = Integer.parseInt(access().getProperty(Config.AAF_CONN_IDLE_TIMEOUT, Config.AAF_CONN_IDLE_TIMEOUT_DEF));
diff --git a/auth/docker/Dockerfile.base b/auth/docker/Dockerfile.base
index ad8e271..35ade62 100644
--- a/auth/docker/Dockerfile.base
+++ b/auth/docker/Dockerfile.base
@@ -18,7 +18,7 @@
 #  ============LICENSE_END====================================================
 #
 # Use dbuild.sh input parameter to set registry
-FROM ${REGISTRY}openjdk:8-jre-alpine
+FROM ${REGISTRY}/openjdk:8-jre-alpine
 MAINTAINER AAF Team, AT&T 2018
 
 LABEL description="aaf_base"
diff --git a/auth/docker/Dockerfile.client b/auth/docker/Dockerfile.client
index 111c377..b62f7b4 100644
--- a/auth/docker/Dockerfile.client
+++ b/auth/docker/Dockerfile.client
@@ -18,7 +18,6 @@
 #  ============LICENSE_END====================================================
 #
 FROM ${DOCKER_REPOSITORY}/onap/aaf/aaf_base:${AAF_VERSION}
-#FROM nexus3.onap.org:10001/onap/aaf/aaf-base-xenial:latest
 MAINTAINER AAF Team, AT&T 2018
 ENV VERSION=${AAF_VERSION}
 
diff --git a/auth/docker/Dockerfile.config b/auth/docker/Dockerfile.config
index 7e442b2..27b2fff 100644
--- a/auth/docker/Dockerfile.config
+++ b/auth/docker/Dockerfile.config
@@ -18,7 +18,6 @@
 #  ============LICENSE_END====================================================
 #
 FROM ${DOCKER_REPOSITORY}/onap/aaf/aaf_base:${AAF_VERSION}
-#FROM nexus3.onap.org:10001/onap/aaf/aaf-base-xenial:latest
 MAINTAINER AAF Team, AT&T 2018
 ENV VERSION=${AAF_VERSION}
 
diff --git a/auth/docker/agent.sh b/auth/docker/agent.sh
index ce878f5..931249b 100644
--- a/auth/docker/agent.sh
+++ b/auth/docker/agent.sh
@@ -159,10 +159,10 @@
   aafcli) 
     shift
     reset_sso
-    if [ -f aaf-auth-cmd-$VERSION*-full.jar ]; then
-      java -Dcadi_prop_files="$HOME/.aaf/sso.props" -jar aaf-auth-cmd-$VERSION*-full.jar $@
+    if [ -f aaf-cadi-aaf-$VERSION-full.jar ]; then
+      java -Dcadi_prop_files="$HOME/.aaf/sso.props" -jar aaf-cadi-aaf-$VERSION-full.jar $@
     else 
-      echo "For local use, you need to have 'aaf-auth-cmd-$VERSION*-full.jar' (or newer)"
+      echo "For local use, you need to have 'aaf-cadi-aaf-$VERSION-full.jar' (or newer)"
     fi
     ;;
   local) 
@@ -185,10 +185,10 @@
     reset_sso
     sso aaf_id "$DEPLOY_FQI"
     sso aaf_password "$DEPLOY_PASSWORD"
-    if [ -f aaf-auth-cmd-$VERSION*-full.jar ]; then
-      java -Dcadi_prop_files="$HOME/.aaf/sso.props" -cp aaf-auth-cmd-$VERSION*-full.jar org.onap.aaf.cadi.configure.Agent $CMD 
+    if [ -f aaf-cadi-aaf-$VERSION-full.jar ]; then
+      java -Dcadi_prop_files="$HOME/.aaf/sso.props" -cp aaf-cadi-aaf-$VERSION-full.jar org.onap.aaf.cadi.configure.Agent $CMD 
     else 
-      echo "For local use, you need to have 'aaf-cadi-aaf-$VERSION*-full.jar' (or newer)"
+      echo "For local use, you need to have 'aaf-cadi-aaf-$VERSION-full.jar' (or newer)"
     fi
     ;;
   *)
diff --git a/auth/docker/d.props.init b/auth/docker/d.props.init
index 54a3926..b9eb016 100644
--- a/auth/docker/d.props.init
+++ b/auth/docker/d.props.init
@@ -20,6 +20,8 @@
 # Variables for building Docker entities
 ORG=onap
 PROJECT=aaf
+# Note: Override can happen on dbuild.sh Commandline, -r <registry>
+DOCKER_PULL_REGISTRY=nexus3.onap.org:10001
 DOCKER_REPOSITORY=nexus3.onap.org:10003
 VERSION=2.1.11-SNAPSHOT
 CONF_ROOT_DIR=/opt/app/osaaf
diff --git a/auth/docker/dbuild.sh b/auth/docker/dbuild.sh
index cdd97fc..b2a5d51 100755
--- a/auth/docker/dbuild.sh
+++ b/auth/docker/dbuild.sh
@@ -21,7 +21,13 @@
 # Docker Building Script.  Reads all the components generated by install, on per-version basis
 #
 
-DOCKER_PULL_REGISTRY='nexus3.onap.org:10001\/'
+# Pull in Variables from d.props
+if [ ! -e ./d.props ]; then
+    cp d.props.init d.props
+fi
+
+. ./d.props
+
 AAF_COMPONENTS=ALL
 
 # process input. originally, an optional positional parameter is used to designate a component.
@@ -45,13 +51,6 @@
 
 echo "$0: AAF_COMPONENTS=$AAF_COMPONENTS DOCKER_PULL_REGISTRY=$DOCKER_PULL_REGISTRY"
 
-# Pull in Variables from d.props
-if [ ! -e ./d.props ]; then
-    cp d.props.init d.props
-fi
-
-. ./d.props
-
 DOCKER=${DOCKER:=docker}
 
 echo "Building Containers for aaf components, version $VERSION"
diff --git a/auth/helm/aaf/aaf.sh b/auth/helm/aaf/aaf.sh
new file mode 100644
index 0000000..5bb8351
--- /dev/null
+++ b/auth/helm/aaf/aaf.sh
@@ -0,0 +1,84 @@
+. ../../docker/d.props
+IMAGE=onap/aaf/aaf_agent:$VERSION
+
+kubectl -n onap run -it --rm aaf-agent-$USER --image=$IMAGE --overrides='
+{
+    "spec": {
+        "containers": [
+            {
+                "name": "aaf-agent-'$USER'",
+		"image": "'$IMAGE'",
+                "imagePullPolicy": "IfNotPresent",
+		"command": [
+                   "bash", 
+		   "-c",
+                   "/opt/app/aaf_config/bin/agent.sh && cd /opt/app/osaaf/local && exec bash"
+                 ],
+                "env": [
+                   {
+                     "name": "aaf_locator_container",
+                     "value": "helm"
+		   },{
+                     "name": "aaf_locator_fqdn",
+                     "value": "'$HOSTNAME'"
+		   },{
+                     "name": "aaf_locator_url",
+                     "value": "https://aaf-locate:8095"
+		   },{
+                     "name": "aaf_locator_public_hostname",
+                     "value": "'$HOSTNAME'"
+		   },{
+                     "name": "AAF_ENV",
+                     "value": "'$AAF_ENV'"
+		   },{
+                     "name": "LATITUDE",
+                     "value": "'$LATITUDE'"
+		   },{
+                     "name": "LONGITUDE",
+                     "value": "'$LONGITUDE'"
+		   },{
+                     "name": "CASSANDRA_CLUSTER",
+                     "value": "'$CASSANDRA_CLUSTER'"
+		   },{
+                     "name": "CASSANDRA_USER",
+                     "value": "'$CASSANDRA_USER'"
+		   },{
+                     "name": "CASSANDRA_PASSWORD",
+                     "value": "'$CASSANDRA_PASSWORD'"
+		   },{
+                     "name": "CASSANDRA_PORT",
+                     "value": "'$CASSANDRA_PORT'"
+		   }
+                ],
+                "stdin": true,
+                "stdinOnce": true,
+                "tty": true,
+                "volumeMounts": [
+                    {
+                        "mountPath": "/opt/app/osaaf",
+                        "name": "aaf-config-vol"
+                    },
+                    {
+                        "mountPath": "/opt/app/aaf/status",
+                        "name": "aaf-status-vol"
+                    }
+                ]
+            }
+        ],
+      "volumes": [
+            {
+                "name": "aaf-config-vol",
+                "persistentVolumeClaim": {
+                    "claimName": "aaf-config-pvc"
+                }
+            },
+            {
+                "name": "aaf-status-vol",
+                "persistentVolumeClaim": {
+                    "claimName": "aaf-status-pvc"
+                }
+            }
+        ]
+   }
+}
+' --restart=Never  -- bash 
diff --git a/auth/helm/aaf/templates/aaf-fs.yaml b/auth/helm/aaf/templates/aaf-fs.yaml
index 999b8d1..d53e920 100644
--- a/auth/helm/aaf/templates/aaf-fs.yaml
+++ b/auth/helm/aaf/templates/aaf-fs.yaml
@@ -41,7 +41,7 @@
   labels:
     app: aaf-fs
 spec:
-  replicas: 0
+  replicas: {{ .Values.replicas.fs }}
   selector:
     matchLabels:
       app: aaf-fs
diff --git a/auth/helm/aaf/templates/aaf-locate.yaml b/auth/helm/aaf/templates/aaf-locate.yaml
index 1e05dcb..b09c80b 100644
--- a/auth/helm/aaf/templates/aaf-locate.yaml
+++ b/auth/helm/aaf/templates/aaf-locate.yaml
@@ -41,7 +41,7 @@
   labels:
     app: aaf-locate
 spec:
-  replicas: 0
+  replicas: {{ .Values.replicas.locate }}
   selector:
     matchLabels:
       app: aaf-locate
diff --git a/auth/helm/aaf/values.yaml b/auth/helm/aaf/values.yaml
index 417e9fb..7556f25 100644
--- a/auth/helm/aaf/values.yaml
+++ b/auth/helm/aaf/values.yaml
@@ -30,11 +30,11 @@
 
 replicas:
   cass: 1
-  service: 0
-  locator: 0
-  oauth: 0
-  cm: 0
-  gui: 0
+  service: 1
+  locator: 1
+  oauth: 1
+  cm: 1
+  gui: 1
   hello: 0
 
 ingress:
@@ -56,7 +56,7 @@
     mountSubPath: "config"
     storageClass: "manual"
   status:
-    volumeReclaimPolicy: Retain
+    volumeReclaimPolicy: Delete
     accessMode: ReadWriteOnce
     size: 10M
     mountSubPath: "status"
diff --git a/auth/sample/bin/pod_wait.sh b/auth/sample/bin/pod_wait.sh
index 07ff981..3d6a1b9 100644
--- a/auth/sample/bin/pod_wait.sh
+++ b/auth/sample/bin/pod_wait.sh
@@ -29,17 +29,17 @@
 
 function status {
   if [ -d "$DIR" ]; then
-     echo "$@" > $DIR/$APP
+     echo "$@" > $DIR/$APP-$HOSTNAME
   fi
 }
 
 
 function check {
   if [ -d "$DIR" ]; then
-    if [ -e "$DIR/$OTHER" ]; then
-      echo "$(cat $DIR/$OTHER)"
-    else 
+    if [ -z "$(ls $DIR/$OTHER* 2> /dev/null)" ]; then
       echo "$DIR/$OTHER does not exist"
+    else 
+      echo "$(cat $DIR/$OTHER*)"
     fi
   else 
     echo "$DIR does not exist"
@@ -51,13 +51,13 @@
   while [ $n -lt 40  ]; do 
      rv="$(check)"
      echo "$rv"
-     if [ "$rv" = "ready" ]; then
-       echo "$OTHER is $rv"
-       n=10000
-     else 
+     if [ -z "$(echo $rv | grep "ready")" ]; then
        (( ++n )) 
        echo "Sleep 10 (iteration $n)"
        sleep 10
+     else 
+       echo "$OTHER is $rv"
+       n=10000
      fi
   done
 }
@@ -67,15 +67,15 @@
   while [ $n -lt 40  ]; do 
      rv="$(check)"
      echo "$OTHER is $rv"
-     if [ "$rv" = "ready" ]; then
+     if [ -z "$(echo $rv | grep "ready")" ]; then
+       (( ++n )) 
+       echo "Sleep 10 (iteration $n)"
+       sleep 10
+     else 
        # This is critical.  Until status is literally "ready" in the status directory, no processes will start
        status ready
        echo "Starting $@"
        n=10000
-     else 
-       (( ++n )) 
-       echo "Sleep 10 (iteration $n)"
-       sleep 10
      fi
   done
 }
@@ -89,6 +89,10 @@
     status "ready"
     echo "Done"
     ;;
+  stop) 
+    echo "Removing $DIR/$APP-$HOSTNAME"
+    rm $DIR/$APP-$HOSTNAME
+    ;;
   wait)
     OTHER="$1"
     shift    
@@ -102,4 +106,4 @@
   ;;
 esac  
 
-eval  "$@"
+eval "$@"
diff --git a/auth/sample/etc/org.osaaf.aaf.locate.props b/auth/sample/etc/org.osaaf.aaf.locate.props
index 0979833..a132abd 100644
--- a/auth/sample/etc/org.osaaf.aaf.locate.props
+++ b/auth/sample/etc/org.osaaf.aaf.locate.props
@@ -22,7 +22,7 @@
 ## AAF Locator Properties
 ##
 cadi_prop_files=/opt/app/osaaf/local/org.osaaf.aaf.props:/opt/app/osaaf/etc/org.osaaf.aaf.log4j.props:/opt/app/osaaf/etc/org.osaaf.aaf.orgs.props:/opt/app/osaaf/local/org.osaaf.aaf.cassandra.props
-aaf_locator_entries=locator
+aaf_locator_entries=locate
 port=8095
 aaf_locator_public_port.helm=30081
 #aaf_locator_public_port.oom=