Пример #1
0
    def __init__(self, user, domain):
        self.user = user
        self.domain = domain

        self.socketIoHandler = SocketIoHandler()

        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.client.load_system_host_keys()

        self.ssh_connect()
Пример #2
0
	def __init__(self, user, domain):
		self.user = user
		self.domain = domain

		self.socketIoHandler = SocketIoHandler()

		self.client = paramiko.SSHClient()
		self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
		self.client.load_system_host_keys()

		self.ssh_connect()

		self.booted_nodes = []
		self.active_nodes = []

		self.reservation = Reservation(user, domain)
		self.nodes = self.reservation.get_reserved_nodes(True)

                # Fetch the latest version of opentestbed software in the shared A8 director of the SSH frontend
		self.ssh_command_exec('cd A8; rm -rf opentestbed; git clone https://github.com/malishav/opentestbed.git; cd opentestbed; git checkout origin/iotlab;')
Пример #3
0
class Reservation:

    CMD_ERROR = "cmd_error"
    SSH_RETRY_TIME = 120
    RETRY_PAUSE = 10
    CHECK_PAUSE = 30

    def __init__(self, user, domain):
        self.user = user
        self.domain = domain

        self.socketIoHandler = SocketIoHandler()

        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.client.load_system_host_keys()

        self.ssh_connect()

    def ssh_connect(self):
        self.client.connect(self.domain, username=self.user)

    def ssh_disconnect(self):
        self.client.close()

    def ssh_command_exec(self, command):
        try:
            stdin, stdout, stderr = self.client.exec_command(command)
            stdin.close()

            output = []
            for line in stdout.read().splitlines():
                output.append(line)

            error = []
            for line in stderr.read().splitlines():
                error.append(line)

            if len(error) > 0:
                print("Error: " + ''.join(error))
                raise Exception(self.CMD_ERROR)

            return ''.join(output)

        except:
            return self.CMD_ERROR

    def reserve_experiment(self, duration, nodes):
        if self.check_experiment():
            self.socketIoHandler.publish('NODE_RESERVATION',
                                         'Experiment exists')
        else:
            output = self.ssh_command_exec(
                'iotlab-experiment submit -n a8_exp -d ' + str(duration) +
                ' -l ' + nodes)
            if output != self.CMD_ERROR:
                self.experiment_id = json.loads(output)['id']
                self.socketIoHandler.publish('NODE_RESERVATION',
                                             'All nodes reserved')

    def get_reserved_nodes(self, logging):
        retries = 0
        num_of_retries = self.SSH_RETRY_TIME / self.RETRY_PAUSE

        json_output = []

        while True:

            output = self.ssh_command_exec('iotlab-experiment get -p')

            if output != self.CMD_ERROR:
                json_output = json.loads(output)['nodes']
                break
            elif retries <= num_of_retries:
                self.socketIoHandler.publish(
                    'RESERVATION_STATUS_RETRY',
                    str(retries) + "/" + str(num_of_retries))
                retries += 1
                time.sleep(self.RETRY_PAUSE)
            else:
                self.socketIoHandler.publish(
                    'RESERVATION_FAIL',
                    str(retries) + "/" + str(num_of_retries))
                break

        self.socketIoHandler.publish('RESERVATION_SUCCESS', output)
        return json_output

    def check_experiment(self):
        time.sleep(self.CHECK_PAUSE)
        output = self.ssh_command_exec('iotlab-experiment get -p')
        print("Experiment check: " + output)
        return output != self.CMD_ERROR

    def terminate_experiment(self):
        self.ssh_command_exec('iotlab-experiment stop')
        self.socketIoHandler.publish('EXP_TERMINATE', '')
        ExpTerminate().exp_terminate()
from socket_io_handler import SocketIoHandler
from exp_terminate import ExpTerminate

import time
import subprocess
import json
import os
import threading
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

socketIoHandler = SocketIoHandler()

logDir = os.path.join(os.path.dirname(__file__), "..", "openvisualizer", "build", "runui")

class MyHandler(FileSystemEventHandler):

	def __init__(self):
		self.last_timestamp = 0.0
		self.unix_timestamp = time.time()
		self.SHUT_DOWN_TIME = 20 #Max time since the last data, indicating that the experiment is over
		self.check_timestamp()

	def on_modified(self, event):
		try:
			last_line = self.get_last_line(event.src_path)
			last_line_timestamp = float(json.loads(last_line)['_timestamp'])

			if last_line_timestamp > self.last_timestamp:
				#print("event type: " + str(event.event_type) + " path : " + str(event.src_path))
				socketIoHandler.publish('LOG_MODIFICATION', last_line)
Пример #5
0
class OTBoxStartup:

	CMD_ERROR = "cmd_error"
	SSH_RETRY_TIME = 600
	RETRY_PAUSE = 6

	def __init__(self, user, domain):
		self.user = user
		self.domain = domain

		self.socketIoHandler = SocketIoHandler()

		self.client = paramiko.SSHClient()
		self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
		self.client.load_system_host_keys()

		self.ssh_connect()

		self.booted_nodes = []
		self.active_nodes = []

		self.reservation = Reservation(user, domain)
		self.nodes = self.reservation.get_reserved_nodes(True)

                # Fetch the latest version of opentestbed software in the shared A8 director of the SSH frontend
		self.ssh_command_exec('cd A8; rm -rf opentestbed; git clone https://github.com/malishav/opentestbed.git; cd opentestbed; git checkout origin/iotlab;')

	def ssh_connect(self):
		self.client.connect(self.domain, username=self.user)

	def ssh_disconnect(self):
		self.client.close()

	def ssh_command_exec(self, command):
		try:
			stdin, stdout, stderr = self.client.exec_command(command)
			stdin.close()

			output = []
			for line in stdout.read().splitlines():
				output.append(line)

			error = []
			for line in stderr.read().splitlines():
				error.append(line)

			if len(error) > 0:
				raise Exception(self.CMD_ERROR)

			return ''.join(output)

		except:
			return self.CMD_ERROR


	def boot_wait(self):
		for ind, node in enumerate(self.nodes):

			node_name = 'node-' + node.split('.')[0]
			print("Probing node: " + node_name)

			retries = 0
			num_of_retries = self.SSH_RETRY_TIME / self.RETRY_PAUSE

			while True:
				try:
					boot_op = self.ssh_command_exec('ssh -o "StrictHostKeyChecking no" root@' + node_name + ' "cd A8;"')
				except:
					print 'Error executing command: ssh -o "StrictHostKeyChecking no" root@' + node_name

				if boot_op == self.CMD_ERROR and retries <= num_of_retries:
					print("Node " + node_name + ": retrying")
					self.socketIoHandler.publish('BOOT_RETRY', node_name + ": " + str(retries) + "/" + str(num_of_retries))
					retries += 1
					time.sleep(self.RETRY_PAUSE)
				elif retries > num_of_retries:
					self.socketIoHandler.publish('BOOT_FAIL', node_name)
					break
				else:
					self.socketIoHandler.publish('NODE_BOOTED', node_name)
					self.booted_nodes.append(node)
					break


	def start(self):
		print("OTBox startup commencing...")
		self.boot_wait()

		try:
			for ind, node in enumerate(self.booted_nodes):
				node_name = 'node-' + node.split('.')[0]
				print("Starting otbox.py on " + node_name + "...")
                                self.ssh_command_exec('ssh -o "StrictHostKeyChecking no" root@' + node_name + ' "source /etc/profile; cd A8; cd opentestbed; pip install requests; python otbox.py >& otbox-' + node_name + '.log &"')

				self.active_nodes.append(node)
				self.socketIoHandler.publish('NODE_ACTIVE', node_name)
		except:
			self.socketIoHandler.publish('NODE_ACTIVE_FAIL', node_name)
			print("Exception happened!")