component_controler.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #coding: utf-8
  2. import sys
  3. import getpass
  4. import os
  5. import time
  6. import re
  7. import subprocess
  8. try:
  9. from screenutils import list_screens, Screen
  10. except ImportError:
  11. print 'please install screenutils module. for example :' \
  12. 'pip install screenutils'
  13. sys.exit(1)
  14. try:
  15. import psutil
  16. except ImportError:
  17. print 'please install psutil module. for example :' \
  18. 'pip install psutil'
  19. sys.exit(1)
  20. if getpass.getuser() != 'room':
  21. print 'must run as "room", please change user to "room"!'
  22. sys.exit(1)
  23. #命令行获取组件名称和范围, ChatRoom10-20
  24. def get_components_list(components_name,*range_type):
  25. components_list = []
  26. if '-' in components_name:
  27. component_base_name,start_num,end_num = re.search('([a-zA-z]+)(\d+)-(\d+)',components_name).groups()
  28. for num in xrange(int(start_num),int(end_num) + 1):
  29. if range_type:
  30. if range_type[0] == 'odd':
  31. if num % 2 :
  32. components_list.append(component_base_name + str(num))
  33. if range_type[0] == 'even':
  34. if not num % 2 :
  35. components_list.append(component_base_name + str(num))
  36. else:
  37. components_list.append(component_base_name + str(num))#['ChatRoom31']
  38. else:
  39. components_list.append(components_name)
  40. return components_list
  41. class Component_base(object):
  42. def __init__(self,component_name):
  43. self.rooms_path = '/home/room/server'
  44. self.component_name = component_name
  45. #判断screen是否存在
  46. def get_screens(self):
  47. for screen in list_screens():
  48. if self.component_name.lower() in screen.name.lower():
  49. return screen
  50. return
  51. #创建新的screen
  52. def NewScreen(self):
  53. self.screen = Screen(self.component_name,True)
  54. return self.screen
  55. #获取组件所在路径
  56. def getpwd(self):
  57. allroom = os.listdir(self.rooms_path)
  58. for room in allroom:
  59. if room.lower() == self.component_name.lower():
  60. self.room_path = os.path.join(self.rooms_path,room)
  61. return self.room_path #'/home/room/server/ChatRoom31'
  62. return
  63. #进程是否存活
  64. def isalived(self):
  65. ps = psutil.process_iter()
  66. for self.psinfo in ps:
  67. try:
  68. if self.component_name.lower() == self.psinfo.name().lower():
  69. return self.psinfo
  70. except:
  71. continue
  72. return
  73. class Action(Component_base):
  74. def __init__(self,component_name):
  75. Component_base.__init__(self,component_name)
  76. self.screen = self.get_screens()
  77. def start(self):
  78. if not self.isalived():
  79. if not self.get_screens():
  80. self.NewScreen()
  81. path = self.getpwd()
  82. self.screen.send_commands('cd '+ path)
  83. self.screen.send_commands('./'+self.component_name)
  84. result = '%s started success!' % self.component_name
  85. print result
  86. return result
  87. else:
  88. result = '%s is already running !' % self.component_name
  89. print result
  90. return result
  91. def stop(self):
  92. if self.isalived():
  93. self.component_process = self.isalived()
  94. self.component_process.kill()
  95. result = '%s stopped success' % self.component_name
  96. print result
  97. return result
  98. else:
  99. result = '%s was not started' % self.component_name
  100. print result
  101. return result
  102. def sendcmd(self,segment):
  103. if self.isalived():
  104. #screen = self.get_screens()
  105. self.screen.send_commands(segment)
  106. result = 'send command to %s success!' % self.component_name
  107. print result
  108. return result
  109. def checkhideroom(self):
  110. if self.isalived():
  111. path = self.getpwd()
  112. cmdlist = ['cd ' + path + ' && ' + 'rm -f dumproom_*.log',
  113. 'cd ' + path + ' && ' + 'ls dumproom_*.log',
  114. 'cd ' + path + ' && ' + 'cat dumproom_*.log|grep "is_hide:1"'
  115. ]
  116. p = subprocess.Popen(cmdlist[0], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  117. self.screen.send_commands('dumproomlist')
  118. p.communicate()
  119. #dumproom_2105_06_01_13_27_51.log
  120. #p = subprocess.Popen(cmdlist[1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  121. while 1:
  122. p = subprocess.Popen(cmdlist[1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  123. dumpfile,err = p.communicate()
  124. if dumpfile:
  125. break
  126. #at dumproom_2105_05_29_17_33_44.log|grep 'is_hide:1'
  127. p = subprocess.Popen(cmdlist[2], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  128. print self.screen.name,'-->',p.stdout.read()
  129. if __name__ == '__main__':
  130. components_name = sys.argv[1]
  131. segment = sys.argv[2]
  132. if len(sys.argv) < 3:
  133. print 'segment error! '
  134. sys.exit(1)
  135. if len(sys.argv) > 3:
  136. range_type = sys.argv[3]
  137. components_list = get_components_list(components_name,range_type)
  138. else:
  139. components_list = get_components_list(components_name)
  140. for components_name in components_list:
  141. component = Action(components_name)
  142. if segment == 'start':
  143. component.start()
  144. elif segment == 'stop':
  145. component.stop()
  146. elif segment == 'restart':
  147. component.stop()
  148. time.sleep(0.5)
  149. component.start()
  150. elif segment == 'checkhideroom':
  151. component.checkhideroom()
  152. else:
  153. component.sendcmd(segment)