from rapid_sshclient import SSHClient
class prox_ctrl(object):
- def __init__(self, ip, key=None, user=None, password = None):
+ def __init__(self, ip, key=None, user=None, password = None, \
+ admin_port = 22, socket_port = 8474):
self._ip = ip
+ self._admin_port = admin_port
+ self._socket_port = socket_port
self._key = key
self._user = user
self._password = password
self._proxsock = []
self._sshclient = SSHClient(ip = ip, user = user, password = password,
- rsa_private_key = key, timeout = None)
+ rsa_private_key = key, timeout = None, ssh_port = admin_port)
def ip(self):
return self._ip
def connect_socket(self):
attempts = 1
- RapidLog.debug("Trying to connect to PROX (just launched) on %s, \
- attempt: %d" % (self._ip, attempts))
+ RapidLog.debug("Trying to connect to PROX (just launched) on %s:%d, \
+ attempt: %d" % (self._ip, self._socket_port, attempts))
sock = None
while True:
sock = self.prox_sock()
break
attempts += 1
if attempts > 20:
- RapidLog.exception("Failed to connect to PROX on %s after %d \
- attempts" % (self._ip, attempts))
+ RapidLog.exception("Failed to connect to PROX on %s:%d after %d \
+ attempts" % (self._ip, self._socket_port, attempts))
time.sleep(2)
- RapidLog.debug("Trying to connect to PROX (just launched) on %s, \
- attempt: %d" % (self._ip, attempts))
+ RapidLog.debug("Trying to connect to PROX (just launched) on %s:%d, \
+ attempt: %d" % (self._ip, self._socket_port, attempts))
RapidLog.info("Connected to PROX on %s" % self._ip)
return sock
self._sshclient.run_cmd(command)
return self._sshclient.get_output()
- def prox_sock(self, port=8474):
+ def prox_sock(self):
"""Connect to the PROX instance on remote system.
Return a prox_sock object on success, None on failure.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
- sock.connect((self._ip, port))
+ sock.connect((self._ip, self._socket_port))
prox = prox_sock(sock)
self._proxsock.append(prox)
return prox
else:
pod_dp_subnet = "24"
- pod = Pod(pod_name, self._namespace)
+ # Search for POD nodeport service
+ if self._create_config.has_option("POD%d" % i,
+ "nodeport"):
+ pod_nodeport = self._create_config.get(
+ "POD%d" % i, "nodeport")
+ else:
+ pod_nodeport = None
+
+ pod = Pod(pod_name, pod_nodeport, self._namespace)
pod.set_nodeselector(pod_nodeselector_hostname)
pod.set_spec_file_name(pod_spec_file_name)
pod.set_dp_ip(pod_dp_ip)
self._runtime_config.add_section("M%d" % pod.get_id())
self._runtime_config.set("M%d" % pod.get_id(),
"admin_ip", pod.get_admin_ip())
+ self._runtime_config.set("M%d" % pod.get_id(),
+ "admin_port", pod.get_admin_port())
+ self._runtime_config.set("M%d" % pod.get_id(),
+ "socket_port", pod.get_socket_port())
self._runtime_config.set("M%d" % pod.get_id(),
"dp_mac1", pod.get_dp_mac())
self._runtime_config.set("M%d" % pod.get_id(),
_sriov_vf = None
_sriov_vf_mac = None
+ _ssh_port = 22
+ _socket_port = 8474
- def __init__(self, name, namespace = "default", logger_name = "k8srapid"):
+ def __init__(self, name, pod_nodeport = None, namespace = "default",
+ logger_name = "k8srapid"):
self._log = logging.getLogger(logger_name)
self._name = name
self._namespace = namespace
self._ssh_client = SSHClient(logger_name = logger_name)
self.qat_vf = []
+ self._pod_nodeport = pod_nodeport
def __del__(self):
"""Destroy POD. Do a cleanup.
self.body = yaml.safe_load(yaml_file)
self.body["metadata"]["name"] = self._name
+ self.body["metadata"]["labels"] = {'app': self._pod_nodeport}
if (self._nodeSelector_hostname is not None):
if ("nodeSelector" not in self.body["spec"]):
"""Check for admin IP address assigned by k8s.
"""
try:
- pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name, namespace = self._namespace)
- self._admin_ip = pod.status.pod_ip
+ if self._pod_nodeport:
+ service= self.k8s_CoreV1Api.read_namespaced_service_status(name = self._pod_nodeport, namespace = self._namespace)
+ self._admin_ip = service.spec.cluster_ip
+ for service_port in service.spec.ports:
+ if service_port.name == 'control-port':
+ self._ssh_port = service_port.node_port
+ if service_port.name == 'socket-port':
+ self._socket_port = service_port.node_port
+ else:
+ pod = self.k8s_CoreV1Api.read_namespaced_pod_status(name = self._name, namespace = self._namespace)
+ self._admin_ip = pod.status.pod_ip
except client.rest.ApiException as e:
self._log.error("Couldn't update POD %s admin IP!\n%s\n" % (self._name, e))
def get_admin_ip(self):
return self._admin_ip
+ def get_admin_port(self):
+ return self._ssh_port
+
+ def get_socket_port(self):
+ return self._socket_port
+
def get_dp_ip(self):
return self._dp_ip
self.update_admin_ip()
self._ssh_client.set_credentials(ip = self._admin_ip,
user = user,
- rsa_private_key = rsa_private_key)
+ rsa_private_key = rsa_private_key,
+ ssh_port = self._ssh_port)
machine_params, configonly):
self.name = machine_params['name']
self.ip = machine_params['admin_ip']
+ self.admin_port = machine_params['admin_port']
+ self.socket_port = machine_params['socket_port']
self.key = key
self.user = user
self.password = password
RapidLog.debug('devbind.sh running for port {} on {} {}'.format(index, self.name, result))
def generate_lua(self, appendix = ''):
- self.LuaFileName = 'parameters-{}.lua'.format(self.ip)
+ self.LuaFileName = 'parameters-{}-{}.lua'.format(self.ip, self.admin_port)
with open(self.LuaFileName, "w") as LuaFile:
LuaFile.write('require "helper"\n')
LuaFile.write('name="%s"\n'% self.name)
def start_prox(self, autostart=''):
if self.machine_params['prox_socket']:
self._client = prox_ctrl(self.ip, self.key, self.user,
- self.password)
+ self.password, self.admin_port, self.socket_port)
self._client.test_connection()
if self.vim in ['OpenStack']:
self.devbind()
section = 'M%d'%machine_index[test_machine-1]
options = config.options(section)
for option in options:
- machine[option] = config.get(section, option)
+ if option in ['admin_port','socket_port']:
+ machine[option] = int(config.get(section, option))
+ else:
+ machine[option] = config.get(section, option)
machines.append(dict(machine))
for machine in machines:
dp_ports = []
_ip = None
_user = None
_rsa_private_key = None
+ _ssh_port = 22
_timeout = None
_ssh = None
_connected = False
_error = None
def __init__(self, ip=None, user=None, rsa_private_key=None, timeout=15,
- logger_name=None, password = None):
+ logger_name=None, password = None, ssh_port = 22):
self._ip = ip
+ self._ssh_port = ssh_port
self._user = user
self._password = password
self._rsa_private_key = rsa_private_key
self._connected = False
- def set_credentials(self, ip, user, rsa_private_key, password = None):
+ def set_credentials(self, ip, user, rsa_private_key, password = None, ssh_port = 22):
self._ip = ip
self._user = user
self._password = password
self._rsa_private_key = rsa_private_key
+ self._ssh_port = ssh_port
def connect(self):
try:
self._ssh.connect(hostname = self._ip, username = self._user,
- password = self._password, pkey = private_key)
+ password = self._password, pkey = private_key, port = self._ssh_port)
except Exception as e:
if (self._log is not None):
self._log.error("Failed to connect to the host! IP %s, user %s, RSA private key %s\n%s"