oc4.0: fix cassandra.backup

As the yaml parsing python script never got into the
container in several cases cassandra ip was not properly
retrieved making it impossible to create a backup.

Another issue with wrong port used for OC4.0 was fixed
by taking the correct one from cassandra config.

This patch introduced some refactoring substituting the
not-so-meaningful script for an inline call to python
yaml parser to fetch params from cassandra config and
using env vars in runner script instead of duplicating
code.

PROD-22642
PROD-22826

Change-Id: I011c7a1f120972a0e4a9bfc3c6b3661ffa88325a
diff --git a/cassandra/backup.sls b/cassandra/backup.sls
index 947fb5d..833e049 100644
--- a/cassandra/backup.sls
+++ b/cassandra/backup.sls
@@ -8,13 +8,6 @@
   pkg.installed:
   - names: {{ backup.pkgs }}
 
-cassandra_get_listen_addr_script:
-  file.managed:
-  - name: /usr/local/bin/cas_get_listen_addr
-  - source: salt://cassandra/files/backup/cas_get_listen_addr.py
-  - template: jinja
-  - mode: 555
-
 cassandra_backup_runner_script:
   file.managed:
   - name: /usr/local/bin/cassandra-backup-runner.sh
@@ -23,7 +16,6 @@
   - mode: 655
   - require:
     - pkg: cassandra_backup_client_packages
-    - file: cassandra_get_listen_addr_script
 
 cassandra_call_backup_runner_script:
   file.managed:
@@ -33,7 +25,6 @@
   - mode: 655
   - require:
     - pkg: cassandra_backup_client_packages
-    - file: cassandra_get_listen_addr_script
 
 cassandra_backup_dir:
   file.directory:
@@ -97,7 +88,6 @@
   - mode: 655
   - require:
     - pkg: cassandra_backup_client_packages
-    - file: cassandra_get_listen_addr_script
 
 cassandra_backup_call_restore_script:
   file.managed:
diff --git a/cassandra/files/backup/cassandra-backup-client-runner-call.sh b/cassandra/files/backup/cassandra-backup-client-runner-call.sh
index 2f37c5e..06ae68f 100644
--- a/cassandra/files/backup/cassandra-backup-client-runner-call.sh
+++ b/cassandra/files/backup/cassandra-backup-client-runner-call.sh
@@ -28,6 +28,7 @@
     TMPDIR="$( pwd )/${PROGNAME}.tmp${RANDOM}"
     CLITMPFILE="${TMPDIR}/cqlschema"
     CASIP="127.0.0.1"
+    CASPORT="9042"
     JMXIP="127.0.0.1"
     HOSTNAME="$( hostname )"
     SNAPCREATE=false
@@ -77,7 +78,8 @@
         mkdir -p "$RSYNCLOGDIR"
     fi
 
-    listen_address=$(/usr/local/bin/cas_get_listen_addr < $CASCFG)
+    listen_address="$(python -c 'import yaml; import sys; print yaml.load(sys.stdin).get("listen_address")' < $CASCFG)"
+    cas_port="$(python -c 'import yaml; import sys; print yaml.load(sys.stdin).get("native_transport_port")' < $CASCFG)"
     # Get local Cassandra listen address.  Should be loaded via the selected
     # cassandra.yaml file above.
     if [ -z $listen_address ]; then
@@ -87,6 +89,10 @@
     else
         CASIP=$listen_address
     fi
+    if [ -n $cas_port ]; then
+        echo "Cassandra port set in config, native_transport_port=$cas_port"
+        CASPORT=$cas_port
+    fi
 
     TIMESTAMP=$( date +"%Y%m%d%H%M%S" )
     DATESTRING=$( date )
@@ -94,10 +100,10 @@
     {%- if backup.client.containers is defined %}
     {%- for container_name in backup.client.containers %}
 
-    docker exec {{ container_name }} cqlsh $CASIP -e "DESC KEYSPACES" |perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | sed '/^$/d' > Keyspace_name_schema.cql
+    docker exec {{ container_name }} cqlsh $CASIP $CASPORT -e "DESC KEYSPACES" |perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | sed '/^$/d' > Keyspace_name_schema.cql
     sed 's/\"//g' Keyspace_name_schema.cql  > KEYSPACES_LIST
     docker cp $SCRIPTDIR/cassandra-backup-runner.sh {{ container_name }}:/
-    for i in `cat KEYSPACES_LIST`; do docker exec {{ container_name }} /cassandra-backup-runner.sh -k $i -t $TIMESTAMP -d $DATESTRING; done
+    for i in `cat KEYSPACES_LIST`; do docker exec -e CASIP=$CASIP -e CASPORT=$CASPORT {{ container_name }} /cassandra-backup-runner.sh -k $i -t $TIMESTAMP -d $DATESTRING; done
     docker cp {{ container_name }}:/$BACKUPDIR/$TIMESTAMP $BACKUPDIR
     docker exec {{ container_name }} rm -rf $BACKUPDIR/$TIMESTAMP
 
@@ -105,9 +111,9 @@
 
     {%- else %}
 
-    cqlsh $CASIP -e "DESC KEYSPACES" |perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | sed '/^$/d' > Keyspace_name_schema.cql
+    cqlsh $CASIP $CASPORT -e "DESC KEYSPACES" |perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | sed '/^$/d' > Keyspace_name_schema.cql
     sed 's/\"//g' Keyspace_name_schema.cql  > KEYSPACES_LIST
-    for i in `cat KEYSPACES_LIST`; do $SCRIPTDIR/cassandra-backup-runner.sh -k $i -t $TIMESTAMP -d $DATESTRING; done
+    for i in `cat KEYSPACES_LIST`; do CASIP=$CASIP CASPORT=$CASPORT $SCRIPTDIR/cassandra-backup-runner.sh -k $i -t $TIMESTAMP -d $DATESTRING; done
 
     {%- endif %}
 
diff --git a/cassandra/files/backup/cassandra-backup-client-runner.sh b/cassandra/files/backup/cassandra-backup-client-runner.sh
index b1f5899..846a91f 100644
--- a/cassandra/files/backup/cassandra-backup-client-runner.sh
+++ b/cassandra/files/backup/cassandra-backup-client-runner.sh
@@ -12,7 +12,6 @@
     BACKUPDIR="{{ backup.backup_dir }}/full"
     TMPDIR="$( pwd )/${PROGNAME}.tmp${RANDOM}"
     CLITMPFILE="${TMPDIR}/cqlschema"
-    CASIP="127.0.0.1"
     JMXIP="127.0.0.1"
     HOSTNAME="$( hostname )"
     SNAPCREATE=false
@@ -132,6 +131,12 @@
         exit 1
     fi
 
+    # make sure CASIP and CASPORT are passed as env variables -> fail otherwise
+    if [ -z "$CASIP" ] || [ -z "$CASPORT" ]; then
+        printf "Both env vars CASIP and CASPORT must be set\n"
+        exit 1
+    fi
+
     # Need write access to local directory to create dump file
     if [ ! -w $( pwd ) ]; then
         printf "You must have write access to the current directory $( pwd )\n"
@@ -189,17 +194,6 @@
     # Write temp command file for Cassandra CLI
     printf "desc keyspace $KEYSPACE;\n" > $CLITMPFILE
 
-    listen_address=$(/usr/local/bin/cas_get_listen_addr < $CASCFG)
-    # Get local Cassandra listen address.  Should be loaded via the selected
-    # cassandra.yaml file above.
-    if [ -z $listen_address ]; then
-        CASIP=$( hostname )
-    elif [ "$listen_address" == "0.0.0.0" ]; then
-        CASIP=127.0.0.1
-    else
-        CASIP=$listen_address
-    fi
-
     # Get local Cassandra JMX address
     # Cheating for now - this is *usually* right, but may be set to a real IP
     # in cassandra-env.sh in some environments.
@@ -252,7 +246,7 @@
     printf "$SNAPSHOT" > "$TMPDIR/$SNAPSFILE"
     printf "$HOSTNAME" > "$TMPDIR/$HOSTSFILE"
     printf "$DATESTRING" > "$TMPDIR/$DATESFILE"
-    cqlsh $CASIP -k $KEYSPACE -f $CLITMPFILE | tail -n +2 > "$TMPDIR/$SCHEMA"
+    cqlsh $CASIP $CASPORT -k $KEYSPACE -f $CLITMPFILE | tail -n +2 > "$TMPDIR/$SCHEMA"
     RC=$?
 
     mkdir -p "$BACKUPDIR/$TIMESTAMP"