Example: Using a script package to store the IP address of a shared service
The example script package runs a shell script to get the IP address of a sample shared service and write it to a file.
cbscript.json
[
{
"name": "Sample Script Package",
"version": "1.0.0",
"description": "This script package will get the registry of a Sample Shared Service and then write it to a file",
"command": "sh /opt/ssc_sp/main.sh",
"log": "/opt/ssc_sp/log.log",
"location": "/opt/ssc_sp",
"timeout": "0"
}
]
main.sh
#!/bin/sh
python registryFinder.py "sample" "1.0" | sh repeater.sh
repeater.sh
#!/bin/sh
while read line ; do
case "$line" in
*"IP address = "*)
# line mentions the IP address
# cut the line to extract the ip address and output the result
echo "$line" | cut -d "'" -f 2 >> ip.txt
;;
*)
# line doesn't contain IP address
;;
esac
done
registryFinder.py
import sys # needed to point Python towards the shared service client
import os
import re
ssc_path = '/0config/nodepkgs/helper/scripts'
if not ssc_path in sys.path:
sys.path.append(ssc_path)
import sharedserviceclient
def find_IP(reg_info):
"""
Finds the Sample Shared Service IP address in the registry information.
"""
str_info = str(reg_info)
# pattern to find any ip addresses
pat = r"\d+\.\d+\.\d+\.\d+"
match = re.search(pat, reg)
if (match):
return match.group(0)
else:
return None
ssname = sys.argv[1] # shared service name
caver = sys.argv[2] # client api version
reg = sharedserviceclient.getRegistry(ssname, caver)
if (reg == '{}'):
print ssname + " not found (may not be deployed)"
os.system('echo "' + ssname + ' not found (may not be deployed)" >> /opt/ssc_sp/log.log')
sys.exit(ssname + " not found (may not be deployed)")
# sample shared service is in place
sip = find_IP(reg)
if (sip is None):
print "Unable to find IP address in response"
os.system('echo "Unable to find IP address in response" >> /opt/ssc_sp/log.log')
sys.exit("Unable to find IP address in response")
print "Found " + ssname + " - IP address = '" + sip + "'"
os.system('echo "Found ' + ssname + ' - IP address = \'"' + sip + '"\'" >> /opt/ssc_sp/log.log')