Skip to content

Commit

Permalink
Merge pull request #5 from XiaowenJiang/master
Browse files Browse the repository at this point in the history
Bugfix: Fix vpdu threads belongings
  • Loading branch information
Robert authored Mar 21, 2017
2 parents d743f2a + db80d4a commit 04a6792
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 37 deletions.
36 changes: 21 additions & 15 deletions infrasim-pduserv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,9 @@
import ctypes
import math
import getopt
import pdusim.common.helper as helper

install_data_dir = [
os.path.join(os.environ['HOME'], '.pdusim'),
os.path.join(sys.prefix, 'pdusim'),
os.path.join(sys.prefix, 'share', 'pdusim'),
os.path.join(os.path.split(__file__)[0], 'pdusim'),
os.path.dirname(os.path.abspath(__file__))
]

for dir in install_data_dir:
path = os.path.join(dir, 'third-party')
if os.path.exists(path):
for d in os.listdir(path):
sys.path.insert(0, os.path.join(path, d))
helper.add_third_party_to_path()

from texttable import Texttable, get_color_string, bcolors
import pdusim.password as password
Expand Down Expand Up @@ -193,8 +182,13 @@ class vPDUHandler(SSHHandler):

def __init__(self):
super(vPDUHandler, self).__init__()
self.config_instance = config.get_conf_instance()
self.mapping_file_handle = mapping_file.get_mapping_file_handle()
install_dir = helper.get_install_dir()
self.config_instance = config.get_conf_instance() \
or config.Config(install_dir)
config.set_conf_instance(self.config_instance)
self.mapping_file_handle = mapping_file.get_mapping_file_handle() \
or mapping_file.MappingFileHandle(install_dir)
mapping_file.set_mapping_file_handle(self.mapping_file_handle)

@command(['config'])
def command_config(self, params):
Expand Down Expand Up @@ -276,6 +270,18 @@ def command_config(self, params):
table_str = table.draw()
self.writeresponse(table_str)
logger.info("\n" + table_str)
elif params[1] == 'list':
self.config_instance.init()
table = Texttable()
table.add_row(['pdu name', self.config_instance.pdu_name])
table.add_row(['dbtype', self.config_instance.db_type])
table.add_row(['database', self.config_instance.db_file])
table.add_row(['snmpdata', self.config_instance.snmp_data_dir])
table.add_row(['simfile', self.config_instance.sim_file])
table_str = table.draw()
self.writeresponse(table_str)
logger.info("\n" + table_str)

else:
logger.error("Unknown command {0}".format(params[0]))
elif params[0] == "esxi":
Expand Down
29 changes: 29 additions & 0 deletions pdusim/common/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This script contains useful functions
import sys
import os

install_data_dir = [
os.path.join(os.environ['HOME'], '.pdusim'),
os.path.join(sys.prefix, 'pdusim'),
os.path.join(sys.prefix, 'share', 'pdusim'),
os.path.join(os.path.split(__file__)[0], 'pdusim'),
os.path.dirname(os.path.abspath(__file__))
]


def get_install_dir():
configdir_found = False
for dir in install_data_dir:
path = os.path.join(dir, 'conf', 'host.conf')
if os.path.exists(path):
return dir
if not configdir_found:
return None


def add_third_party_to_path():
for dir in install_data_dir:
path = os.path.join(dir, 'third-party')
if os.path.exists(path):
for d in os.listdir(path):
sys.path.insert(0, os.path.join(path, d))
27 changes: 5 additions & 22 deletions pdusim/pdusim.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import os
import sys
import common.helper as helper

install_datadir = [
os.path.join(os.environ['HOME'], '.pdusim'),
os.path.join(sys.prefix, 'pdusim'),
os.path.join(sys.prefix, 'share', 'pdusim'),
os.path.join(os.path.split(__file__)[0], 'pdusim'),
os.path.dirname(os.path.abspath(__file__))
]

for dir in install_datadir:
path = os.path.join(dir, 'third-party')
if os.path.exists(path):
for d in os.listdir(path):
sys.path.insert(0, os.path.join(path, d))
helper.add_third_party_to_path()

import common.config as config
from oid import FileOIDHandler, SqliteOIDHandler
Expand All @@ -33,20 +22,13 @@ def __init__(self):
self.name = "vPDU Service"
self.__vpdu_handler = None
self.__snmp_sim_serv = None
self.init()

def set_daemon(self):
self.daemon = True

def init(self):
configdir_found = False
for dir in install_datadir:
path = os.path.join(dir, 'conf', 'host.conf')
if os.path.exists(path):
configdir_found = True
break

if not configdir_found:
dir = helper.get_install_dir()
if not dir:
logger.error("Don't find the configuration dir.")
sys.exit(1)

Expand Down Expand Up @@ -86,6 +68,7 @@ def init(self):
self.__snmp_sim_serv = SNMPSimService()

def run(self):
self.init()
retcode = self.__snmp_sim_serv.start()
if retcode < 0:
logger.error("Failed to start snmpsimd service!")
Expand Down

0 comments on commit 04a6792

Please sign in to comment.