This commit is contained in:
emre.akkaya 2016-05-17 15:36:41 +03:00
commit 7eb8907b94
4 changed files with 67 additions and 45 deletions

View file

@ -10,6 +10,7 @@ import stat
import subprocess import subprocess
import uuid import uuid
import urllib.request import urllib.request
import errno
from base.Scope import Scope from base.Scope import Scope
from base.messaging.ssh_file_transfer import FileTransfer from base.messaging.ssh_file_transfer import FileTransfer
@ -57,16 +58,21 @@ class ExecutionManager(object):
temp_file = self.config_manager.get('CONNECTION', 'receivefileparam') + str(uuid.uuid4()) + '.deb' temp_file = self.config_manager.get('CONNECTION', 'receivefileparam') + str(uuid.uuid4()) + '.deb'
if str(plugin['protocol']).lower() == 'ssh': if str(plugin['protocol']).lower() == 'ssh':
self.logger.debug('[ExecutionManager] Distribution protocol is {}'.format(str(plugin['protocol']).lower())) try:
host = parameter_map['host'] self.logger.debug('[ExecutionManager] Distribution protocol is {}'.format(str(plugin['protocol']).lower()))
username = parameter_map['username'] host = parameter_map['host']
password = parameter_map['password'] username = parameter_map['username']
port = parameter_map['port'] password = parameter_map['password']
path = parameter_map['path'] port = parameter_map['port']
path = parameter_map['path']
transfer = FileTransfer(host, port, username, password) transfer = FileTransfer(host, port, username, password)
transfer.connect() transfer.connect()
transfer.get_file(temp_file, path) transfer.get_file(temp_file, path)
except Exception as e:
self.logger.error('[ExecutionManager] Plugin package could not fetch. Error Message: {}.'.format(str(e)))
self.logger.error('[ExecutionManager] Plugin Installation is cancelling')
return
elif plugin['protocol'].lower() == 'http': elif plugin['protocol'].lower() == 'http':
self.logger.debug('[ExecutionManager] Distribution protocol is {}.'.format(str(plugin['protocol']).lower())) self.logger.debug('[ExecutionManager] Distribution protocol is {}.'.format(str(plugin['protocol']).lower()))
@ -75,10 +81,19 @@ class ExecutionManager(object):
self.logger.debug('[ExecutionManager] Unsupported protocol is {}.'.format(str(plugin['protocol']).lower())) self.logger.debug('[ExecutionManager] Unsupported protocol is {}.'.format(str(plugin['protocol']).lower()))
self.logger.debug('[ExecutionManager] Plugin package downloaded via {}.'.format(str(plugin['protocol']).lower())) self.logger.debug('[ExecutionManager] Plugin package downloaded via {}.'.format(str(plugin['protocol']).lower()))
self.install_deb(temp_file) try:
self.logger.debug('[ExecutionManager] Plugin installed.') self.install_deb(temp_file)
self.remove_file(temp_file) self.logger.debug('[ExecutionManager] Plugin installed.')
self.logger.debug('[ExecutionManager] Temp files were removed.') except Exception as e:
self.logger.error('[ExecutionManager] Could not install plugin. Error Message: {}'.format(str(e)))
return
try:
self.remove_file(temp_file)
self.logger.debug('[ExecutionManager] Temp files were removed.')
except Exception as e:
self.logger.error('[ExecutionManager] Could not remove temp file. Error Message: {}'.format(str(e)))
self.plugin_manager.loadSinglePlugin(plugin_name) self.plugin_manager.loadSinglePlugin(plugin_name)
except Exception as e: except Exception as e:

View file

@ -43,9 +43,10 @@ class FileTransfer(object):
self.logger.debug('[FileTransfer] File was downloaded to {} from {}'.format(local_path, remote_path)) self.logger.debug('[FileTransfer] File was downloaded to {} from {}'.format(local_path, remote_path))
except Exception as e: except Exception as e:
self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: {}'.format(str(e))) self.logger.error('[FileTransfer] A problem occurred while downloading file. Exception message: {}'.format(str(e)))
raise
finally: finally:
self.connection.close() self.connection.close()
self.logger.debug('[FileTransfer] Connection is closed successfully') self.logger.debug('[FileTransfer] Connection is closed successfully')
def connect(self): def connect(self):
self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.port)) self.logger.debug('[FileTransfer] Connecting to {} via {}'.format(self.target_hostname, self.port))

View file

@ -38,18 +38,22 @@ class PluginManager(object):
self.logger.info('[PluginManager] Loading plugins...') self.logger.info('[PluginManager] Loading plugins...')
self.plugins = [] self.plugins = []
self.logger.debug('[PluginManager] Lookup for possible plugins...') self.logger.debug('[PluginManager] Lookup for possible plugins...')
possibleplugins = os.listdir(self.configManager.get("PLUGIN", "pluginFolderPath"))
self.logger.debug('[PluginManager] Possible plugins.. ' + str(possibleplugins)) try:
for pname in possibleplugins: possible_plugins = os.listdir(self.configManager.get("PLUGIN", "pluginFolderPath"))
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pname) self.logger.debug('[PluginManager] Possible plugins: {} '.format(str(possible_plugins)))
if not os.path.isdir(location) or not self.configManager.get("PLUGIN", "mainModuleName") + ".py" in os.listdir(location): for plugin_name in possible_plugins:
self.logger.debug('It is not a plugin location ! There is no main module - ' + str(location)) location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name)
continue if not os.path.isdir(location) or not self.configManager.get("PLUGIN", "mainModuleName") + ".py" in os.listdir(location):
try: self.logger.debug('It is not a plugin location ! There is no main module - {}'.format(str(location)))
self.loadSinglePlugin(pname) continue
except Exception as e: try:
self.logger.error('[PluginManager] Exception occured when loading plugin ! Plugin name : ' + str(pname) + ' Exception : ' + str(e)) self.loadSinglePlugin(plugin_name)
self.logger.info('[PluginManager] Loaded plugins successfully.') except Exception as e:
self.logger.error('[PluginManager] Exception occurred when loading plugin ! Plugin name : {} .Error Message: {}'.format(str(plugin_name), str(e)))
self.logger.info('[PluginManager] Loaded plugins successfully.')
except Exception as e:
self.logger.warning('[PluginManager] Plugin folder path not found. Error Message: {}'.format(str(e)))
def loadSinglePlugin(self, plugin_name): def loadSinglePlugin(self, plugin_name):
# TODO check already loaded plugin # TODO check already loaded plugin

View file

@ -98,31 +98,32 @@ class Registration():
def get_registration_params(self): def get_registration_params(self):
print(System.Hardware.Network.ip_addresses())
params = { params = {
'ipAddresses': self.get_ip_address(), 'ipAddresses': str(System.Hardware.Network.ip_addresses()).replace('\'localhost\'', '').replace('\'127.0.0.1\'', ''),
'macAddresses': System.Hardware.mac_address(), 'macAddresses': System.Hardware.mac_address(),
'hostname': str(socket.gethostname()), 'hostname': System.Os.hostname(),
'system': str(platform.system()), 'os.name': System.Os.name(),
'node': str(platform.node()), 'os.version': System.Os.version(),
'release': str(platform.release()), 'os.kernel': System.Os.kernel_release(),
'version': str(platform.version()), 'os.distributionName': System.Os.distribution_name(),
'machine': str(platform.machine()), 'os.distributionId': System.Os.distribution_id(),
'processor': str(platform.processor()), 'os.distributionVersion': System.Os.distribution_version(),
'architecture': str(platform.architecture()), 'os.architecture': System.Os.architecture(),
'cpuCount': str(psutil.cpu_count()), 'hardware.cpu.architecture': System.Hardware.Cpu.architecture(),
'diskInfo': self.get_disks() 'hardware..cpu.logicalCoreCount': System.Hardware.Cpu.logical_core_count(),
'hardware..cpu.physicalCoreCount': System.Hardware.Cpu.physical_core_count(),
'hardware.disk.total': System.Hardware.Disk.total(),
'hardware.disk.used': System.Hardware.Disk.used(),
'hardware.disk.free': System.Hardware.Disk.free(),
'hardware.disk.partitions': System.Hardware.Disk.partitions(),
'hardware.memory.total': System.Hardware.Memory.total(),
'hardware.network.ipAddresses': System.Hardware.Network.ip_addresses(),
'sessions.userNames': System.Sessions.user_name(),
} }
return json.dumps(params) return json.dumps(params)
def get_ip_address(self): """
arr = []
for ip in System.Hardware.Network.ip_addresses():
if ip is not 'localhost' and ip is not '127.0.0.1':
arr.append(ip)
return str(arr).replace('[','').replace(']','')
def get_disks(self): def get_disks(self):
disk_info = [] disk_info = []
for _disk in psutil.disk_partitions(): for _disk in psutil.disk_partitions():
@ -134,6 +135,7 @@ class Registration():
json_data = json.dumps(disk) json_data = json.dumps(disk)
disk_info.append(json_data) disk_info.append(json_data)
return disk_info return disk_info
"""
def unregister(self): def unregister(self):
self.logger.debug('[Registration] Ahenk is unregistering...') self.logger.debug('[Registration] Ahenk is unregistering...')