def _init_server(self): if not hasattr(self, 'connection'): self.CDP_PROMPT = 'CDP>' self.CDP_COMMANDS = { 'login': '******', 'terminate': 'terminate -y', 'suites': 'suites', 'update': 'get', 'get_suite': 'show', } self.host = HostFactory().create_host(self.host_settings) self.base_path = os.path.join(self.host.dataPath, 'SMS_SERVERS', self.alias) self.server_dir = os.path.join(self.base_path, 'SERVER_START_DIR') self.suites_dir = os.path.join(self.base_path, 'SUITES') self.connection = self._connect_to_cdp() server_started = self._start_server() if server_started == 0: login = self.login() else: login = 1 return login
def _init_server(self): if not hasattr(self, 'connection'): self.CDP_PROMPT = 'CDP>' self.CDP_COMMANDS = { 'login' : 'login %s %s %s', 'terminate' : 'terminate -y', 'suites' : 'suites', 'update' : 'get', 'get_suite' : 'show', } self.host = HostFactory().create_host(self.host_settings) self.base_path = os.path.join(self.host.dataPath, 'SMS_SERVERS', self.alias) self.server_dir = os.path.join(self.base_path, 'SERVER_START_DIR') self.suites_dir = os.path.join(self.base_path, 'SUITES') self.connection = self._connect_to_cdp() server_started = self._start_server() if server_started == 0: login = self.login() else: login = 1 return login
class SMSServer(models.Model): alias = models.CharField(max_length=100) rpc_num = models.IntegerField() host_settings = models.ForeignKey(Host) @staticmethod def get_server(alias): ''' Get the server object and initialize it in one go ''' s = SMSServer.objects.get(alias=alias) login_result = s._init_server() return s, login_result def _init_server(self): if not hasattr(self, 'connection'): self.CDP_PROMPT = 'CDP>' self.CDP_COMMANDS = { 'login': '******', 'terminate': 'terminate -y', 'suites': 'suites', 'update': 'get', 'get_suite': 'show', } self.host = HostFactory().create_host(self.host_settings) self.base_path = os.path.join(self.host.dataPath, 'SMS_SERVERS', self.alias) self.server_dir = os.path.join(self.base_path, 'SERVER_START_DIR') self.suites_dir = os.path.join(self.base_path, 'SUITES') self.connection = self._connect_to_cdp() server_started = self._start_server() if server_started == 0: login = self.login() else: login = 1 return login def list_suites(self): '''Return a list with the names of the suites in the server.''' out = self._get_cdp_result(self.CDP_COMMANDS['suites']) suites = out[4].split() return suites def _get_suite_definition(self, suite_name): '''Return a string with the CDP commands that defined a suite.''' update = self._get_cdp_result(self.CDP_COMMANDS['update']) out = self._get_cdp_result(self.CDP_COMMANDS['get_suite']) suite_def = '' suite_started = False for line in out: if line.startswith('suite') and suite_name in line: suite_started = True if suite_started: suite_def += line + '\n' if suite_started and line.startswith('endsuite'): break return suite_def def __unicode__(self): return '%s:%s_%s' % (self.alias, self.host_settings.name, self.rpc_num) def _create_directories(self): for d in (self.server_dir, self.suites_dir): self.host.make_dir(d) def _delete_directories(self): self.init_server() for d in (self.server_dir, self.suites_dir): self.host.remove_dir(d) def login(self): out = self._get_cdp_result(self.CDP_COMMANDS['login'] % (self.host.host, 'ricardo', 'pass')) result = 0 for line in out: if 'ERR' in line: result = 1 return result def _get_cdp_result(self, command): self.connection.sendline(command) self.connection.expect(self.CDP_PROMPT) result = self.connection.before.split('\r\n') return result def _connect_to_cdp(self): os.environ['SMS_PROG'] = str(self.rpc_num) c = pexpect.spawn('cdp') c.expect(self.CDP_PROMPT) return c def _start_server(self): retcode = self._ping_server() if retcode == 0: print('the server is already running') result = 0 else: print('starting the sms server...') self._create_directories() os.environ['SMS_PROG'] = str(self.rpc_num) result = self.host.start_sms_server(self.server_dir) return result def _ping_server(self): os.environ['SMS_PROG'] = str(self.rpc_num) stdout, stderr, retcode = self.host.run_program('smsping %s' % \ self.host.host) return retcode def _terminate_server(self): ''' Terminate the SMS server. This completely kills the SMS server instance. ''' result = self._get_cdp_result(self.CDP_COMMANDS['terminate']) del self.connection
class SMSServer(models.Model): alias = models.CharField(max_length=100) rpc_num = models.IntegerField() host_settings = models.ForeignKey(Host) @staticmethod def get_server(alias): ''' Get the server object and initialize it in one go ''' s = SMSServer.objects.get(alias=alias) login_result = s._init_server() return s, login_result def _init_server(self): if not hasattr(self, 'connection'): self.CDP_PROMPT = 'CDP>' self.CDP_COMMANDS = { 'login' : 'login %s %s %s', 'terminate' : 'terminate -y', 'suites' : 'suites', 'update' : 'get', 'get_suite' : 'show', } self.host = HostFactory().create_host(self.host_settings) self.base_path = os.path.join(self.host.dataPath, 'SMS_SERVERS', self.alias) self.server_dir = os.path.join(self.base_path, 'SERVER_START_DIR') self.suites_dir = os.path.join(self.base_path, 'SUITES') self.connection = self._connect_to_cdp() server_started = self._start_server() if server_started == 0: login = self.login() else: login = 1 return login def list_suites(self): '''Return a list with the names of the suites in the server.''' out = self._get_cdp_result(self.CDP_COMMANDS['suites']) suites = out[4].split() return suites def _get_suite_definition(self, suite_name): '''Return a string with the CDP commands that defined a suite.''' update = self._get_cdp_result(self.CDP_COMMANDS['update']) out = self._get_cdp_result(self.CDP_COMMANDS['get_suite']) suite_def = '' suite_started = False for line in out: if line.startswith('suite') and suite_name in line: suite_started = True if suite_started: suite_def += line + '\n' if suite_started and line.startswith('endsuite'): break return suite_def def __unicode__(self): return '%s:%s_%s' % (self.alias, self.host_settings.name, self.rpc_num) def _create_directories(self): for d in (self.server_dir, self.suites_dir): self.host.make_dir(d) def _delete_directories(self): self.init_server() for d in (self.server_dir, self.suites_dir): self.host.remove_dir(d) def login(self): out = self._get_cdp_result(self.CDP_COMMANDS['login'] % (self.host.host, 'ricardo', 'pass')) result = 0 for line in out: if 'ERR' in line: result = 1 return result def _get_cdp_result(self, command): self.connection.sendline(command) self.connection.expect(self.CDP_PROMPT) result = self.connection.before.split('\r\n') return result def _connect_to_cdp(self): os.environ['SMS_PROG'] = str(self.rpc_num) c = pexpect.spawn('cdp') c.expect(self.CDP_PROMPT) return c def _start_server(self): retcode = self._ping_server() if retcode == 0: print('the server is already running') result = 0 else: print('starting the sms server...') self._create_directories() os.environ['SMS_PROG'] = str(self.rpc_num) result = self.host.start_sms_server(self.server_dir) return result def _ping_server(self): os.environ['SMS_PROG'] = str(self.rpc_num) stdout, stderr, retcode = self.host.run_program('smsping %s' % \ self.host.host) return retcode def _terminate_server(self): ''' Terminate the SMS server. This completely kills the SMS server instance. ''' result = self._get_cdp_result(self.CDP_COMMANDS['terminate']) del self.connection