Support stopping and restarting for a AWS-based cluster instance.

Change-Id: I77fff4a8a5f6e5d8ebee52eb66d86cc18f32cd12
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1643
Reviewed-by: Michael Blow <mblow@apache.org>
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-server/src/main/assembly/binary-assembly.xml b/asterixdb/asterix-server/src/main/assembly/binary-assembly.xml
index d0bd88a..37f0e00 100644
--- a/asterixdb/asterix-server/src/main/assembly/binary-assembly.xml
+++ b/asterixdb/asterix-server/src/main/assembly/binary-assembly.xml
@@ -57,10 +57,6 @@
       <includes>
         <include>**/*.yml</include>
       </includes>
-      <excludes>
-        <exclude>**/instance_start.yml</exclude>
-        <exclude>**/gen_conf.yml</exclude>
-      </excludes>
       <filtered>true</filtered>
     </fileSet>
     <fileSet>
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/bin/stop.sh b/asterixdb/asterix-server/src/main/opt/ansible/bin/stop.sh
index 200d6e6..247c32a 100755
--- a/asterixdb/asterix-server/src/main/opt/ansible/bin/stop.sh
+++ b/asterixdb/asterix-server/src/main/opt/ansible/bin/stop.sh
@@ -26,7 +26,7 @@
 
 INVENTORY=$ANSB_PATH/conf/inventory
 
-# Starts the cluster
+# Stops the cluster
 export ANSIBLE_HOST_KEY_CHECKING=false
 ansible-playbook -i $INVENTORY $ANSB_PATH/yaml/instance_stop.yml
 
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml b/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml
index 09ff365..663f371 100644
--- a/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml
+++ b/asterixdb/asterix-server/src/main/opt/ansible/yaml/deploy_all.yml
@@ -17,8 +17,17 @@
 # under the License.
 # ------------------------------------------------------------
 
+- hosts: [localhost,]
+  vars:
+       temp_dir: "{{ playbook_dir }}/../conf/instance"
+       inventory: "{{ temp_dir }}/inventory"
+       ccconf: "{{ temp_dir }}/cc.conf"
+  tasks:
+    - include_vars: ../conf/instance_settings.yml
+    - include: gen_conf.yml
+
 - hosts: all
   tasks:
     - include_vars: ../conf/instance_settings.yml
+    - include: deploy.yml
 
-    - include: deploy.yml
\ No newline at end of file
diff --git a/asterixdb/asterix-server/src/main/opt/ansible/yaml/instance_start.yml b/asterixdb/asterix-server/src/main/opt/ansible/yaml/instance_start.yml
index d6661ca..80a6b65 100644
--- a/asterixdb/asterix-server/src/main/opt/ansible/yaml/instance_start.yml
+++ b/asterixdb/asterix-server/src/main/opt/ansible/yaml/instance_start.yml
@@ -18,26 +18,9 @@
 # ------------------------------------------------------------
 
 - hosts: [localhost,]
-  vars:
-       temp_dir: "{{ playbook_dir }}/../conf/instance"
-       inventory: "{{ temp_dir }}/inventory"
-       ccconf: "{{ temp_dir }}/cc.conf"
   tasks:
     - include: wait_ssh.yml
 
-    - include_vars: ../conf/instance_settings.yml
-
-    - include: gen_conf.yml
-
-- hosts: all
-  tasks:
-    - include_vars: ../conf/instance_settings.yml
-
-    - name: Ensure the log directory exists
-      file:
-        path: "{{ binarydir }}/logs"
-        state: directory
-
 - hosts: ncs
   tasks:
     - include_vars: ../conf/instance_settings.yml
@@ -49,4 +32,3 @@
     - include_vars: ../conf/instance_settings.yml
 
     - include: start_cc.yml
-
diff --git a/asterixdb/asterix-server/src/main/opt/aws/bin/deploy.sh b/asterixdb/asterix-server/src/main/opt/aws/bin/deploy.sh
new file mode 100755
index 0000000..d68ca87
--- /dev/null
+++ b/asterixdb/asterix-server/src/main/opt/aws/bin/deploy.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+# ------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# ------------------------------------------------------------
+
+# Gets the absolute path so that the script can work no matter where it is invoked.
+pushd `dirname $0` > /dev/null
+SCRIPT_PATH=`pwd -P`
+popd > /dev/null
+AWS_PATH=`dirname "${SCRIPT_PATH}"`
+OPT_PATH=`dirname "${AWS_PATH}"`
+DIST_PATH=`dirname "${OPT_PATH}"`
+
+# Starts an AWS cluster.
+ansible-playbook -i "localhost," $AWS_PATH/yaml/aws_start.yml
+
+# Generates an Ansible inventory file and an AsterixDB configuration file.
+temp=$AWS_PATH/conf/instance
+inventory=$temp/inventory
+
+# Deploys asterixdb on all AWS instances.
+export ANSIBLE_HOST_KEY_CHECKING=false
+ansible-playbook -i $inventory $AWS_PATH/yaml/instance_init.yml
diff --git a/asterixdb/asterix-server/src/main/opt/aws/bin/start.sh b/asterixdb/asterix-server/src/main/opt/aws/bin/start.sh
index 8bffaa4..7c0609e 100755
--- a/asterixdb/asterix-server/src/main/opt/aws/bin/start.sh
+++ b/asterixdb/asterix-server/src/main/opt/aws/bin/start.sh
@@ -22,18 +22,10 @@
 pushd `dirname $0` > /dev/null
 SCRIPT_PATH=`pwd -P`
 popd > /dev/null
-AWS_PATH=`dirname "${SCRIPT_PATH}"`
-OPT_PATH=`dirname "${AWS_PATH}"`
-DIST_PATH=`dirname "${OPT_PATH}"`
+ANSB_PATH=`dirname "${SCRIPT_PATH}"`
 
-# Starts an AWS cluster.
-ansible-playbook -i "localhost," $AWS_PATH/yaml/aws_start.yml
+INVENTORY=$ANSB_PATH/conf/instance/inventory
 
-# Generates an Ansible inventory file and an AsterixDB configuration file.
-temp=$AWS_PATH/conf/instance
-inventory=$temp/inventory
-
-# Installs asterixdb on all AWS instances.
+# Starts the cluster
 export ANSIBLE_HOST_KEY_CHECKING=false
-ansible-playbook -i $inventory $AWS_PATH/yaml/instance_start.yml
-
+ansible-playbook -i $INVENTORY $ANSB_PATH/yaml/instance_start.yml
diff --git a/asterixdb/asterix-server/src/main/opt/aws/bin/stop.sh b/asterixdb/asterix-server/src/main/opt/aws/bin/stop.sh
index 48f4ff7..ab9d342 100755
--- a/asterixdb/asterix-server/src/main/opt/aws/bin/stop.sh
+++ b/asterixdb/asterix-server/src/main/opt/aws/bin/stop.sh
@@ -22,7 +22,11 @@
 pushd `dirname $0` > /dev/null
 SCRIPT_PATH=`pwd -P`
 popd > /dev/null
-AWS_PATH=`dirname "${SCRIPT_PATH}"`
+ANSB_PATH=`dirname "${SCRIPT_PATH}"`
 
-# Terminates an AWS cluster.
-ansible-playbook -i "localhost," $AWS_PATH/yaml/aws_stop.yml
+INVENTORY=$ANSB_PATH/conf/instance/inventory
+
+# Stops the cluster
+export ANSIBLE_HOST_KEY_CHECKING=false
+ansible-playbook -i $INVENTORY $ANSB_PATH/yaml/instance_stop.yml
+
diff --git a/asterixdb/asterix-server/src/main/opt/aws/yaml/aws_stop.yml b/asterixdb/asterix-server/src/main/opt/aws/bin/terminate.sh
old mode 100644
new mode 100755
similarity index 63%
copy from asterixdb/asterix-server/src/main/opt/aws/yaml/aws_stop.yml
copy to asterixdb/asterix-server/src/main/opt/aws/bin/terminate.sh
index b466ad4..58ff965
--- a/asterixdb/asterix-server/src/main/opt/aws/yaml/aws_stop.yml
+++ b/asterixdb/asterix-server/src/main/opt/aws/bin/terminate.sh
@@ -1,3 +1,4 @@
+#!/bin/bash
 # ------------------------------------------------------------
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements.  See the NOTICE file
@@ -17,21 +18,11 @@
 # under the License.
 # ------------------------------------------------------------
 
-- name: Stop AWS cluster
-  hosts: localhost
-  gather_facts: false
-  tasks:
-    - include_vars: ../conf/aws_settings.yml
-    - name: Stop all instance
-      ec2:
-        key_name: "{{ keypair }}"
-        group: "{{ group }}"
-        instance_type: "{{ instance_type }}"
-        image: "{{ image }}"
-        wait: true
-        region: "{{ region }}"
-        aws_access_key: "{{ access_key_id }}"
-        aws_secret_key: "{{ secret_access_key }}"
-        exact_count: 0
-        count_tag:
-          Name: "{{ tag }}"
+# Gets the absolute path so that the script can work no matter where it is invoked.
+pushd `dirname $0` > /dev/null
+SCRIPT_PATH=`pwd -P`
+popd > /dev/null
+AWS_PATH=`dirname "${SCRIPT_PATH}"`
+
+# Terminates an AWS cluster.
+ansible-playbook -i "localhost," $AWS_PATH/yaml/aws_terminate.yml
diff --git a/asterixdb/asterix-server/src/main/opt/aws/yaml/aws_stop.yml b/asterixdb/asterix-server/src/main/opt/aws/yaml/aws_terminate.yml
similarity index 95%
rename from asterixdb/asterix-server/src/main/opt/aws/yaml/aws_stop.yml
rename to asterixdb/asterix-server/src/main/opt/aws/yaml/aws_terminate.yml
index b466ad4..c4e3050 100644
--- a/asterixdb/asterix-server/src/main/opt/aws/yaml/aws_stop.yml
+++ b/asterixdb/asterix-server/src/main/opt/aws/yaml/aws_terminate.yml
@@ -17,12 +17,12 @@
 # under the License.
 # ------------------------------------------------------------
 
-- name: Stop AWS cluster
+- name: Terminate AWS cluster
   hosts: localhost
   gather_facts: false
   tasks:
     - include_vars: ../conf/aws_settings.yml
-    - name: Stop all instance
+    - name: Terminate all instances
       ec2:
         key_name: "{{ keypair }}"
         group: "{{ group }}"
diff --git a/asterixdb/asterix-server/src/main/opt/aws/yaml/instance_start.yml b/asterixdb/asterix-server/src/main/opt/aws/yaml/instance_init.yml
similarity index 85%
rename from asterixdb/asterix-server/src/main/opt/aws/yaml/instance_start.yml
rename to asterixdb/asterix-server/src/main/opt/aws/yaml/instance_init.yml
index 3a9fd5a..8e68b80 100644
--- a/asterixdb/asterix-server/src/main/opt/aws/yaml/instance_start.yml
+++ b/asterixdb/asterix-server/src/main/opt/aws/yaml/instance_init.yml
@@ -38,15 +38,4 @@
 
     - include: deploy.yml
 
-- hosts: ncs
-  tasks:
-    - include_vars: ../conf/instance_settings.yml
-
-    - include: start_ncservice.yml
-
-- hosts: cc
-  tasks:
-    - include_vars: ../conf/instance_settings.yml
-
-    - include: start_cc.yml