|
|
|
@ -16,17 +16,12 @@
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
vpcs device management (creates command line, processes, files etc.) in
|
|
|
|
|
order to run an vpcs instance.
|
|
|
|
|
VPCS device management (creates command line, processes, files etc.) in
|
|
|
|
|
order to run an VPCS instance.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import signal
|
|
|
|
|
import subprocess
|
|
|
|
|
import argparse
|
|
|
|
|
import threading
|
|
|
|
|
import configparser
|
|
|
|
|
import sys
|
|
|
|
|
import socket
|
|
|
|
|
from .vpcs_error import VPCSError
|
|
|
|
@ -40,12 +35,12 @@ log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
class VPCSDevice(object):
|
|
|
|
|
"""
|
|
|
|
|
vpcs device implementation.
|
|
|
|
|
VPCS device implementation.
|
|
|
|
|
|
|
|
|
|
:param path: path to vpcs executable
|
|
|
|
|
:param path: path to VPCS executable
|
|
|
|
|
:param working_dir: path to a working directory
|
|
|
|
|
:param host: host/address to bind for console and UDP connections
|
|
|
|
|
:param name: name of this vpcs device
|
|
|
|
|
:param name: name of this VPCS device
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_instances = []
|
|
|
|
@ -54,7 +49,7 @@ class VPCSDevice(object):
|
|
|
|
|
|
|
|
|
|
# find an instance identifier (1 <= id <= 255)
|
|
|
|
|
# This 255 limit is due to a restriction on the number of possible
|
|
|
|
|
# mac addresses given in vpcs using the -m option
|
|
|
|
|
# mac addresses given in VPCS using the -m option
|
|
|
|
|
self._id = 0
|
|
|
|
|
for identifier in range(1, 256):
|
|
|
|
|
if identifier not in self._instances:
|
|
|
|
@ -63,12 +58,12 @@ class VPCSDevice(object):
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if self._id == 0:
|
|
|
|
|
raise VPCSError("Maximum number of vpcs instances reached")
|
|
|
|
|
raise VPCSError("Maximum number of VPCS instances reached")
|
|
|
|
|
|
|
|
|
|
if name:
|
|
|
|
|
self._name = name
|
|
|
|
|
else:
|
|
|
|
|
self._name = "vpcs{}".format(self._id)
|
|
|
|
|
self._name = "VPCS{}".format(self._id)
|
|
|
|
|
self._path = path
|
|
|
|
|
self._console = None
|
|
|
|
|
self._working_dir = None
|
|
|
|
@ -78,35 +73,35 @@ class VPCSDevice(object):
|
|
|
|
|
self._host = "127.0.0.1"
|
|
|
|
|
self._started = False
|
|
|
|
|
|
|
|
|
|
# vpcs settings
|
|
|
|
|
# VPCS settings
|
|
|
|
|
self._script_file = ""
|
|
|
|
|
self._ethernet_adapters = [EthernetAdapter()] # one adapter = 1 interfaces
|
|
|
|
|
self._slots = self._ethernet_adapters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# update the working directory
|
|
|
|
|
self.working_dir = working_dir
|
|
|
|
|
|
|
|
|
|
log.info("vpcs device {name} [id={id}] has been created".format(name=self._name,
|
|
|
|
|
log.info("VPCS device {name} [id={id}] has been created".format(name=self._name,
|
|
|
|
|
id=self._id))
|
|
|
|
|
|
|
|
|
|
def defaults(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns all the default attribute values for vpcs.
|
|
|
|
|
Returns all the default attribute values for VPCS.
|
|
|
|
|
|
|
|
|
|
:returns: default values (dictionary)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
vpcs_defaults = {"name": self._name,
|
|
|
|
|
"path": self._path,
|
|
|
|
|
"script_file": self._script_file,
|
|
|
|
|
"console": self._console}
|
|
|
|
|
"path": self._path,
|
|
|
|
|
"script_file": self._script_file,
|
|
|
|
|
"console": self._console}
|
|
|
|
|
|
|
|
|
|
return vpcs_defaults
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def id(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the unique ID for this vpcs device.
|
|
|
|
|
Returns the unique ID for this VPCS device.
|
|
|
|
|
|
|
|
|
|
:returns: id (integer)
|
|
|
|
|
"""
|
|
|
|
@ -124,7 +119,7 @@ class VPCSDevice(object):
|
|
|
|
|
@property
|
|
|
|
|
def name(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the name of this vpcs device.
|
|
|
|
|
Returns the name of this VPCS device.
|
|
|
|
|
|
|
|
|
|
:returns: name
|
|
|
|
|
"""
|
|
|
|
@ -134,22 +129,22 @@ class VPCSDevice(object):
|
|
|
|
|
@name.setter
|
|
|
|
|
def name(self, new_name):
|
|
|
|
|
"""
|
|
|
|
|
Sets the name of this vpcs device.
|
|
|
|
|
Sets the name of this VPCS device.
|
|
|
|
|
|
|
|
|
|
:param new_name: name
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self._name = new_name
|
|
|
|
|
log.info("vpcs {name} [id={id}]: renamed to {new_name}".format(name=self._name,
|
|
|
|
|
log.info("VPCS {name} [id={id}]: renamed to {new_name}".format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
new_name=new_name))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the path to the vpcs executable.
|
|
|
|
|
Returns the path to the VPCS executable.
|
|
|
|
|
|
|
|
|
|
:returns: path to vpcs
|
|
|
|
|
:returns: path to VPCS
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return(self._path)
|
|
|
|
@ -157,13 +152,13 @@ class VPCSDevice(object):
|
|
|
|
|
@path.setter
|
|
|
|
|
def path(self, path):
|
|
|
|
|
"""
|
|
|
|
|
Sets the path to the vpcs executable.
|
|
|
|
|
Sets the path to the VPCS executable.
|
|
|
|
|
|
|
|
|
|
:param path: path to vpcs
|
|
|
|
|
:param path: path to VPCS
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self._path = path
|
|
|
|
|
log.info("vpcs {name} [id={id}]: path changed to {path}".format(name=self._name,
|
|
|
|
|
log.info("VPCS {name} [id={id}]: path changed to {path}".format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
path=path))
|
|
|
|
|
|
|
|
|
@ -180,7 +175,7 @@ class VPCSDevice(object):
|
|
|
|
|
@working_dir.setter
|
|
|
|
|
def working_dir(self, working_dir):
|
|
|
|
|
"""
|
|
|
|
|
Sets the working directory for vpcs.
|
|
|
|
|
Sets the working directory for VPCS.
|
|
|
|
|
|
|
|
|
|
:param working_dir: path to the working directory
|
|
|
|
|
"""
|
|
|
|
@ -195,7 +190,7 @@ class VPCSDevice(object):
|
|
|
|
|
raise VPCSError("Could not create working directory {}: {}".format(working_dir, e))
|
|
|
|
|
|
|
|
|
|
self._working_dir = working_dir
|
|
|
|
|
log.info("vpcs {name} [id={id}]: working directory changed to {wd}".format(name=self._name,
|
|
|
|
|
log.info("VPCS {name} [id={id}]: working directory changed to {wd}".format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
wd=self._working_dir))
|
|
|
|
|
|
|
|
|
@ -218,33 +213,33 @@ class VPCSDevice(object):
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self._console = console
|
|
|
|
|
log.info("vpcs {name} [id={id}]: console port set to {port}".format(name=self._name,
|
|
|
|
|
log.info("VPCS {name} [id={id}]: console port set to {port}".format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
port=console))
|
|
|
|
|
|
|
|
|
|
def command(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the vpcs command line.
|
|
|
|
|
Returns the VPCS command line.
|
|
|
|
|
|
|
|
|
|
:returns: vpcs command line (string)
|
|
|
|
|
:returns: VPCS command line (string)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return " ".join(self._build_command())
|
|
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
|
"""
|
|
|
|
|
Deletes this vpcs device.
|
|
|
|
|
Deletes this VPCS device.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self.stop()
|
|
|
|
|
self._instances.remove(self._id)
|
|
|
|
|
log.info("vpcs device {name} [id={id}] has been deleted".format(name=self._name,
|
|
|
|
|
log.info("VPCS device {name} [id={id}] has been deleted".format(name=self._name,
|
|
|
|
|
id=self._id))
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def started(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns either this vpcs device has been started or not.
|
|
|
|
|
Returns either this VPCS device has been started or not.
|
|
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
|
"""
|
|
|
|
@ -253,20 +248,20 @@ class VPCSDevice(object):
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
"""
|
|
|
|
|
Starts the vpcs process.
|
|
|
|
|
Starts the VPCS process.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if not self.is_running():
|
|
|
|
|
|
|
|
|
|
if not os.path.isfile(self._path):
|
|
|
|
|
raise VPCSError("vpcs image '{}' is not accessible".format(self._path))
|
|
|
|
|
raise VPCSError("VPCS image '{}' is not accessible".format(self._path))
|
|
|
|
|
|
|
|
|
|
if not os.access(self._path, os.X_OK):
|
|
|
|
|
raise VPCSError("vpcs image '{}' is not executable".format(self._path))
|
|
|
|
|
raise VPCSError("VPCS image '{}' is not executable".format(self._path))
|
|
|
|
|
|
|
|
|
|
self._command = self._build_command()
|
|
|
|
|
try:
|
|
|
|
|
log.info("starting vpcs: {}".format(self._command))
|
|
|
|
|
log.info("starting VPCS: {}".format(self._command))
|
|
|
|
|
self._vpcs_stdout_file = os.path.join(self._working_dir, "vpcs.log")
|
|
|
|
|
log.info("logging to {}".format(self._vpcs_stdout_file))
|
|
|
|
|
with open(self._vpcs_stdout_file, "w") as fd:
|
|
|
|
@ -274,35 +269,35 @@ class VPCSDevice(object):
|
|
|
|
|
stdout=fd,
|
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
|
cwd=self._working_dir)
|
|
|
|
|
log.info("vpcs instance {} started PID={}".format(self._id, self._process.pid))
|
|
|
|
|
log.info("VPCS instance {} started PID={}".format(self._id, self._process.pid))
|
|
|
|
|
self._started = True
|
|
|
|
|
except OSError as e:
|
|
|
|
|
vpcs_stdout = self.read_vpcs_stdout()
|
|
|
|
|
log.error("could not start vpcs {}: {}\n{}".format(self._path, e, vpcs_stdout))
|
|
|
|
|
raise VPCSError("could not start vpcs {}: {}\n{}".format(self._path, e, vpcs_stdout))
|
|
|
|
|
log.error("could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout))
|
|
|
|
|
raise VPCSError("could not start VPCS {}: {}\n{}".format(self._path, e, vpcs_stdout))
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
|
"""
|
|
|
|
|
Stops the vpcs process.
|
|
|
|
|
Stops the VPCS process.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# stop the vpcs process
|
|
|
|
|
# stop the VPCS process
|
|
|
|
|
if self.is_running():
|
|
|
|
|
log.info("stopping vpcs instance {} PID={}".format(self._id, self._process.pid))
|
|
|
|
|
log.info("stopping VPCS instance {} PID={}".format(self._id, self._process.pid))
|
|
|
|
|
try:
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
sock.connect((self._host, self._console))
|
|
|
|
|
sock.send(bytes("quit\n", 'UTF-8'))
|
|
|
|
|
sock.close()
|
|
|
|
|
except TypeError as e:
|
|
|
|
|
log.warn("vpcs instance {} PID={} is still running. Error: {}".format(self._id,
|
|
|
|
|
self._process.pid, e))
|
|
|
|
|
log.warn("VPCS instance {} PID={} is still running. Error: {}".format(self._id,
|
|
|
|
|
self._process.pid, e))
|
|
|
|
|
self._process = None
|
|
|
|
|
self._started = False
|
|
|
|
|
|
|
|
|
|
def read_vpcs_stdout(self):
|
|
|
|
|
"""
|
|
|
|
|
Reads the standard output of the vpcs process.
|
|
|
|
|
Reads the standard output of the VPCS process.
|
|
|
|
|
Only use when the process has been stopped or has crashed.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
@ -317,7 +312,7 @@ class VPCSDevice(object):
|
|
|
|
|
|
|
|
|
|
def is_running(self):
|
|
|
|
|
"""
|
|
|
|
|
Checks if the vpcs process is running
|
|
|
|
|
Checks if the VPCS process is running
|
|
|
|
|
|
|
|
|
|
:returns: True or False
|
|
|
|
|
"""
|
|
|
|
@ -346,7 +341,7 @@ class VPCSDevice(object):
|
|
|
|
|
try:
|
|
|
|
|
adapter = self._slots[slot_id]
|
|
|
|
|
except IndexError:
|
|
|
|
|
raise VPCSError("Slot {slot_id} doesn't exist on vpcs {name}".format(name=self._name,
|
|
|
|
|
raise VPCSError("Slot {slot_id} doesn't exist on VPCS {name}".format(name=self._name,
|
|
|
|
|
slot_id=slot_id))
|
|
|
|
|
|
|
|
|
|
if not adapter.port_exists(port_id):
|
|
|
|
@ -354,7 +349,7 @@ class VPCSDevice(object):
|
|
|
|
|
port_id=port_id))
|
|
|
|
|
|
|
|
|
|
adapter.add_nio(port_id, nio)
|
|
|
|
|
log.info("vpcs {name} [id={id}]: {nio} added to {slot_id}/{port_id}".format(name=self._name,
|
|
|
|
|
log.info("VPCS {name} [id={id}]: {nio} added to {slot_id}/{port_id}".format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
nio=nio,
|
|
|
|
|
slot_id=slot_id,
|
|
|
|
@ -371,7 +366,7 @@ class VPCSDevice(object):
|
|
|
|
|
try:
|
|
|
|
|
adapter = self._slots[slot_id]
|
|
|
|
|
except IndexError:
|
|
|
|
|
raise VPCSError("Slot {slot_id} doesn't exist on vpcs {name}".format(name=self._name,
|
|
|
|
|
raise VPCSError("Slot {slot_id} doesn't exist on VPCS {name}".format(name=self._name,
|
|
|
|
|
slot_id=slot_id))
|
|
|
|
|
|
|
|
|
|
if not adapter.port_exists(port_id):
|
|
|
|
@ -380,7 +375,7 @@ class VPCSDevice(object):
|
|
|
|
|
|
|
|
|
|
nio = adapter.get_nio(port_id)
|
|
|
|
|
adapter.remove_nio(port_id)
|
|
|
|
|
log.info("vpcs {name} [id={id}]: {nio} removed from {slot_id}/{port_id}".format(name=self._name,
|
|
|
|
|
log.info("VPCS {name} [id={id}]: {nio} removed from {slot_id}/{port_id}".format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
nio=nio,
|
|
|
|
|
slot_id=slot_id,
|
|
|
|
@ -388,10 +383,10 @@ class VPCSDevice(object):
|
|
|
|
|
|
|
|
|
|
def _build_command(self):
|
|
|
|
|
"""
|
|
|
|
|
Command to start the vpcs process.
|
|
|
|
|
Command to start the VPCS process.
|
|
|
|
|
(to be passed to subprocess.Popen())
|
|
|
|
|
|
|
|
|
|
vpcs command line:
|
|
|
|
|
VPCS command line:
|
|
|
|
|
usage: vpcs [options] [scriptfile]
|
|
|
|
|
Option:
|
|
|
|
|
-h print this help then exit
|
|
|
|
@ -413,7 +408,7 @@ class VPCSDevice(object):
|
|
|
|
|
hypervisor mode option:
|
|
|
|
|
-H port run as the hypervisor listening on the tcp 'port'
|
|
|
|
|
|
|
|
|
|
If no 'scriptfile' specified, vpcs will read and execute the file named
|
|
|
|
|
If no 'scriptfile' specified, VPCS will read and execute the file named
|
|
|
|
|
'startup.vpc' if it exsits in the current directory.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
@ -444,7 +439,7 @@ class VPCSDevice(object):
|
|
|
|
|
@property
|
|
|
|
|
def script_file(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns the script-file for this vpcs instance.
|
|
|
|
|
Returns the script-file for this VPCS instance.
|
|
|
|
|
|
|
|
|
|
:returns: path to script-file file
|
|
|
|
|
"""
|
|
|
|
@ -454,12 +449,12 @@ class VPCSDevice(object):
|
|
|
|
|
@script_file.setter
|
|
|
|
|
def script_file(self, script_file):
|
|
|
|
|
"""
|
|
|
|
|
Sets the script-file for this vpcs instance.
|
|
|
|
|
Sets the script-file for this VPCS instance.
|
|
|
|
|
|
|
|
|
|
:param script_file: path to script-file file
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
self._script_file = script_file
|
|
|
|
|
log.info("vpcs {name} [id={id}]: script_file set to {config}".format(name=self._name,
|
|
|
|
|
log.info("VPCS {name} [id={id}]: script_file set to {config}".format(name=self._name,
|
|
|
|
|
id=self._id,
|
|
|
|
|
config=self._script_file))
|
|
|
|
|