Bug 159 : Update charm script to ssh to VNF and do http
authorPhilip Joseph <philip.joseph@riftio.com>
Thu, 19 Jan 2017 20:41:09 +0000 (02:11 +0530)
committerPhilip Joseph <philip.joseph@riftio.com>
Thu, 19 Jan 2017 20:41:09 +0000 (02:11 +0530)
Signed-off-by: Philip Joseph <philip.joseph@riftio.com>
src/nsd/ping_pong_ns/ping_pong_nsd.yaml
src/nsd/ping_pong_ns/scripts/start_traffic.py
src/vnfd/ping_vnf/ping_vnfd.yaml
src/vnfd/pong_vnf/pong_vnfd.yaml

index 0791576..7f213cd 100644 (file)
@@ -22,7 +22,7 @@ nsd:nsd-catalog:
         nsd:name: ping_pong_ns
         nsd:short-name: ping_pong_ns
         nsd:vendor: RIFT.io
-        nsd:version: '1.0'
+        nsd:version: '1.1'
         nsd:description: RIFT.io sample ping pong network service
         nsd:constituent-vnfd:
         -   nsd:member-vnf-index: '1'
@@ -34,6 +34,10 @@ nsd:nsd-catalog:
             nsd:parameter:
             -   nsd:name: port
                 nsd:value: 5555
+            -   nsd:name: ssh-username
+                nsd:value: fedora
+            -   nsd:name: ssh-password
+                nsd:value: fedora
             nsd:seq: '1'
             nsd:user-defined-script: start_traffic.py
         nsd:input-parameter-xpath:
index f98bece..87c309f 100755 (executable)
@@ -19,6 +19,7 @@
 
 import argparse
 import logging
+import paramiko
 import os
 import subprocess
 import sys
@@ -27,55 +28,71 @@ import time
 import yaml
 
 
+def ssh(cmd, host, user, password):
+    """ Run an arbitrary command over SSH. """
+
+    client = paramiko.SSHClient()
+    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+
+    client.connect(host, port=22, username=user, password=password)
+
+    stdin, stdout, stderr = client.exec_command(cmd, get_pty=True)
+    retcode = stdout.channel.recv_exit_status()
+    client.close()
+
+    return (
+        retcode,
+        stdout.read().decode('utf-8').strip(),
+        stderr.read().decode('utf-8').strip()
+    )
+
+
 def start_traffic(yaml_cfg, logger):
     '''Use curl and set admin status to enable on pong and ping vnfs'''
 
     curl_fmt = 'curl -D /dev/stdout -H "Accept: application/vnd.yang.data' \
                    '+xml" -H "Content-Type: application/vnd.yang.data+json" ' \
-                   '-X POST -d "{{ {data} }}" http://{mgmt_ip}:' \
+                   '-X POST -d "{{ {data} }}" http://127.0.0.1:' \
                    '{mgmt_port}/api/v1/{vnf_type}/{url}'
 
-    def setup_service(mgmt_ip, port, vnf_type, service_ip, service_port):
-        data = '\\"ip\\":\\"{}\\", \\"port\\":5555'.format(service_ip)
-        curl_cmd = curl_fmt.format(
-            mgmt_ip=mgmt_ip,
-            mgmt_port=port,
-            vnf_type=vnf_type,
-            data=data,
-            url='server'
-        )
-
-        logger.debug("Executing cmd: %s", curl_cmd)
-        proc = subprocess.run(curl_cmd, shell=True,
-                              stdout=subprocess.PIPE,
-                              stderr=subprocess.PIPE)
+    # Get userid and password for the VNF
+    user = yaml_cfg['parameter']['ssh-username']
+    passwd = yaml_cfg['parameter']['ssh-password']
 
-        logger.debug("Process: {}".format(proc))
+    # Get port from user parameter
+    service_port = yaml_cfg['parameter']['port']
 
-        return proc.returncode
+    service_ip = None
 
-    def enable_service(mgmt_ip, port, vnf_type):
+    def exec_cmd(vnf_type, mgmt_ip, port, url, data):
         curl_cmd = curl_fmt.format(
-            mgmt_ip=mgmt_ip,
             mgmt_port=port,
             vnf_type=vnf_type,
-            data='\\"enable\\":true',
-            url='adminstatus/state'
+            data=data,
+            url=url
         )
 
         logger.debug("Executing cmd: %s", curl_cmd)
-        proc = subprocess.run(curl_cmd, shell=True,
-                              stdout=subprocess.PIPE,
-                              stderr=subprocess.PIPE)
+        rc, out, err = ssh(curl_cmd, mgmt_ip, user, passwd)
 
-        logger.debug("Process: {}".format(proc))
+        if rc != 0:
+            logger.error("cmd={}, rc={}, stderr={}, stdout={}".
+                         format(curl_cmd, rc, err, out))
+        else:
+            logger.debug("cmd={}, rc={}, stderr={}, stdout={}".
+                         format(curl_cmd, rc, err, out))
 
-        return proc.returncode
+        return rc
 
-    # Get port from user parameter
-    service_port = yaml_cfg['parameter']['port']
+    def setup_service(mgmt_ip, port, vnf_type):
+        data = '\\"ip\\":\\"{}\\", \\"port\\":5555'.format(service_ip)
+        return exec_cmd(vnf_type, mgmt_ip, port, 'server', data)
+
+    def enable_service(mgmt_ip, port, vnf_type):
+        data='\\"enable\\":true'
+        url='adminstatus/state'
+        return exec_cmd(vnf_type, mgmt_ip, port, url, data)
 
-    service_ip = None
     # Enable pong service first
     for index, vnfr in yaml_cfg['vnfr'].items():
         logger.debug("VNFR {}: {}".format(index, vnfr))
@@ -95,7 +112,7 @@ def start_traffic(yaml_cfg, logger):
             max_tries = 60
             tries = 0
             while tries < max_tries:
-                rc = setup_service(mgmt_ip, port, vnf_type, service_ip, service_port)
+                rc = setup_service(mgmt_ip, port, vnf_type)
                 tries += 1
                 if rc != 0:
                     logger.error("Setup service for pong failed ({}): {}".
@@ -104,13 +121,14 @@ def start_traffic(yaml_cfg, logger):
                         return rc
                     else:
                         time.sleep(1) # Sleep for 1 seconds
+                else:
+                    break
 
             rc = enable_service(mgmt_ip, port, vnf_type)
             if rc != 0:
                 logger.error("Enable service for pong failed: {}".
                              format(rc))
                 return rc
-            break
 
     # Add a delay to provide pong port to come up
     time.sleep(1)
@@ -131,7 +149,7 @@ def start_traffic(yaml_cfg, logger):
             max_tries = 30
             tries = 0
             while tries < max_tries:
-                rc = setup_service(mgmt_ip, port, vnf_type, service_ip, service_port)
+                rc = setup_service(mgmt_ip, port, vnf_type)
                 tries += 1
                 if rc != 0:
                     logger.error("Setup service for ping failed ({}): {}".
@@ -140,12 +158,13 @@ def start_traffic(yaml_cfg, logger):
                         return rc
                     else:
                         time.sleep(1) # Sleep for 1 seconds
+                else:
+                    break
 
             rc = enable_service(mgmt_ip, port, vnf_type)
             if rc != 0:
                 logger.error("Enable service for ping failed: {}".
                              format(rc))
-            break
 
     return rc
 
index 44f09ab..f5e577c 100644 (file)
@@ -22,7 +22,7 @@ vnfd:vnfd-catalog:
         short-name: ping_vnf
         logo: rift_logo.png
         vendor: RIFT.io
-        version: '1.0'
+        version: '1.1'
         description: This is an example RIFT.ware VNF
         connection-point:
         -   name: ping_vnfd/cp0
index fb7c53c..50f537f 100644 (file)
@@ -22,7 +22,7 @@ vnfd:vnfd-catalog:
         short-name: pong_vnf
         logo: rift_logo.png
         vendor: RIFT.io
-        version: '1.0'
+        version: '1.1'
         description: This is an example RIFT.ware VNF
         connection-point:
         -   name: pong_vnfd/cp0