core.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. dever-manage tools
  5. name:Core.py
  6. author:rabin
  7. """
  8. import time
  9. import datetime
  10. import os
  11. import sys
  12. import getopt
  13. import configparser
  14. import re
  15. import subprocess
  16. class Args(object):
  17. @classmethod
  18. def init(self):
  19. self.action = ''
  20. self.name = ''
  21. self.index = ''
  22. self.param = ''
  23. self.argv()
  24. @classmethod
  25. def argv(self):
  26. slen = len(sys.argv)
  27. if slen >= 2:
  28. self.action = sys.argv[1]
  29. if slen >= 3:
  30. self.name = sys.argv[2]
  31. if slen >= 4:
  32. self.param = sys.argv[3]
  33. self.options()
  34. if self.action == '':
  35. print('action error')
  36. sys.exit()
  37. @classmethod
  38. def options(self):
  39. try:
  40. options, args = getopt.getopt(sys.argv[1:], "ha:n:p:", ['help', "action=", "name=", "param="])
  41. for name, value in options:
  42. if name in ('-h', '--help'):
  43. self.usage()
  44. elif name in ('-a', '--action'):
  45. self.method = value
  46. elif name in ('-n', '--name'):
  47. Args.name = value
  48. elif name in ('-p', '--param'):
  49. self.param = value
  50. except getopt.GetoptError:
  51. self.usage()
  52. @classmethod
  53. def usage(self):
  54. print(File.read(Core.path, 'usage'))
  55. sys.exit()
  56. class Env(object):
  57. dm_use = 'base.use'
  58. dm_store = 'base.store'
  59. dm_cluster = 'base.cluster'
  60. dm_dever = 'base.dever'
  61. dm_dever2 = 'base.dever2'
  62. dm_val = 'val.'
  63. data = {}
  64. @staticmethod
  65. def key(name):
  66. return name.split('.')
  67. @classmethod
  68. def init(self, name):
  69. if not self.data:
  70. self.data = Config.runtime()
  71. return self.key(name)
  72. @classmethod
  73. def read(self, name):
  74. key, name = self.init(name)
  75. if key in self.data and name in self.data[key]:
  76. return self.data[key][name]
  77. else:
  78. return False
  79. @classmethod
  80. def write(self, name, value):
  81. key, name = self.init(name)
  82. self.data[key][name] = value
  83. Config.runtime(key, name, value)
  84. return value
  85. @classmethod
  86. def use(self, value=None):
  87. if value:
  88. return self.write(self.dm_use, value)
  89. return self.read(self.dm_use)
  90. @classmethod
  91. def dever(self, value=None):
  92. if value:
  93. return self.write(self.dm_dever, value)
  94. return self.read(self.dm_dever)
  95. @classmethod
  96. def dever2(self, value=None):
  97. if value:
  98. return self.write(self.dm_dever2, value)
  99. return self.read(self.dm_dever2)
  100. @classmethod
  101. def store(self, value=None):
  102. if value:
  103. return self.write(self.dm_store, value)
  104. return self.read(self.dm_store)
  105. @classmethod
  106. def cluster(self, value=None):
  107. if value:
  108. return self.write(self.dm_cluster, value)
  109. return self.read(self.dm_cluster)
  110. @classmethod
  111. def val(self, name='', value=None):
  112. #name = self.dm_val + name.capitalize()
  113. name = self.dm_val + name
  114. if value:
  115. return self.write(name, value)
  116. return self.read(name)
  117. class Config(object):
  118. @classmethod
  119. def runtime(self, key='', name='', value=''):
  120. runtime = Core.path + 'data/runtime.conf'
  121. if File.exists(runtime):
  122. config = configparser.ConfigParser()
  123. config.read(runtime)
  124. if key and name:
  125. config.set(key, name, value)
  126. config.write(open(runtime, 'w'))
  127. else:
  128. result = {}
  129. for item in config.sections():
  130. result[item] = self.readOption(config, item)
  131. return result
  132. else:
  133. print(runtime + ' is not exists')
  134. sys.exit()
  135. @classmethod
  136. def core(self, path):
  137. core = Core.path + path + 'core.conf'
  138. if File.exists(core):
  139. config = configparser.ConfigParser()
  140. config.read(core, encoding='utf-8')
  141. result = {}
  142. for item in config.sections():
  143. result[item] = self.readOption(config, item)
  144. return result
  145. else:
  146. print(core + ' is not exists')
  147. sys.exit()
  148. @classmethod
  149. def read(self, path):
  150. if '-' in Args.name:
  151. temp = Args.name.split('-')
  152. Args.name = temp[0]
  153. Args.index = temp[1]
  154. filename = Core.path + path + 'conf/' + Args.name + '.conf'
  155. if File.exists(filename):
  156. config = configparser.ConfigParser()
  157. config.read(filename, encoding='utf-8')
  158. result = {}
  159. result['server'] = []
  160. result['config'] = {}
  161. result['base'] = {}
  162. for item in config.sections():
  163. if item == 'base':
  164. result['base'] = self.readOption(config, item)
  165. else:
  166. result['server'].append(item)
  167. result['config'][item] = self.readOption(config, item)
  168. result['base']['path'] = Core.replace('{base}', Core.path, result['base']['path'])
  169. return result
  170. else:
  171. print(filename + ' is not exists')
  172. sys.exit()
  173. @staticmethod
  174. def readOption(config, type):
  175. value = config.options(type)
  176. result = {}
  177. for item in value:
  178. result[item] = config.get(type, item)
  179. return result
  180. class Alias(object):
  181. @classmethod
  182. def delete(self, config, name, cluster=False):
  183. if Core.platform() == False:
  184. return
  185. result = self.get(config, name)
  186. for key in result:
  187. action = self.action(name, key)
  188. if action[0] != 'sh' and File.exists(action[1]):
  189. content = File.get(action[1])
  190. string = 'docker exec -it ' + name + ' ' + action[0] + ' $@'
  191. if string in content:
  192. content = content.replace(string, '')
  193. File.write(action[1], content.strip())
  194. if 'docker' not in content:
  195. Core.popen('rm -rf ' + action[1], bg=True)
  196. Core.popen('rm -rf ' + action[2], bg=True)
  197. #Core.popen('rm -rf ' + action[1], bg=True)
  198. #Core.popen('rm -rf ' + action[2], bg=True)
  199. @classmethod
  200. def add(self, config, name, content, type, cluster=False):
  201. if Core.platform() == False:
  202. return
  203. result = self.get(config, name)
  204. for key in result:
  205. action = self.action(name, key)
  206. old = ''
  207. if File.exists(action[1]):
  208. old = File.get(action[1])
  209. env = '#!/usr/bin/env sh \nset -e\n'
  210. if type != 'call':
  211. dexec = 'docker exec -it ' + name + ' ' + action[0] + ' $@'
  212. if cluster:
  213. dexec = 'name=`ds name ' + name + '`\n'
  214. dexec = dexec + 'docker exec -it $name ' + action[0] + ' $@'
  215. if action[0] == 'sh':
  216. content = env + self.define(name, cluster) + \
  217. 'else\n' + \
  218. dexec + '\n' + \
  219. 'fi'
  220. elif old:
  221. content = dexec
  222. if content not in old:
  223. content = old + '\n' + content
  224. else:
  225. content = ''
  226. else:
  227. content = env + dexec
  228. else:
  229. content = env + ' $@'
  230. if content:
  231. File.write(action[1], content)
  232. Core.popen('ln -sf ' + action[1] + ' ' + action[2])
  233. @staticmethod
  234. def define(name, cluster=False):
  235. if Core.platform() == False:
  236. return
  237. conf = ['logs', 'inspect', 'restart', 'stop', 'rm', 'rmb', 'run', 'uprun', 'show']
  238. result = ''
  239. for key in conf:
  240. control = 'elif'
  241. command = 'dm'
  242. if cluster:
  243. command = 'ds'
  244. shell = command + ' ' + key + ' ' + name + '\n'
  245. if key == 'logs':
  246. control = 'if'
  247. result = result + control + ' [ "$1" = "'+key+'" ];then\n' + shell
  248. return result
  249. @classmethod
  250. def get(self, config, name):
  251. if Core.platform() == False:
  252. return
  253. self.path = Core.path + 'data/alias/'
  254. result = []
  255. default = 'sh->' + name
  256. if 'alias' in config:
  257. config['alias'] = config['alias'] + ',' + default
  258. if ',' in config['alias']:
  259. result = config['alias'].split(',');
  260. else:
  261. result = [config['alias']]
  262. else:
  263. result = [default]
  264. return result
  265. @classmethod
  266. def action(self, name, key):
  267. if Core.platform() == False:
  268. return
  269. file = key
  270. if '->' in key:
  271. temp = key.split('->')
  272. key = temp[0]
  273. file = temp[1]
  274. link = '/usr/bin/' + file
  275. file = self.path + file
  276. return [key, file, link]
  277. class File(object):
  278. @staticmethod
  279. def write(file, content):
  280. if type(content) == list:
  281. content = "\r\n".join(content)
  282. handle = open(file, 'w')
  283. handle.write(content)
  284. handle.close()
  285. Core.popen('chmod +x ' + file)
  286. @staticmethod
  287. def read(path, name):
  288. handle = open(path + name, 'r')
  289. content = handle.read()
  290. handle.close()
  291. return content
  292. @staticmethod
  293. def get(file):
  294. handle = open(file, 'r')
  295. content = handle.read()
  296. handle.close()
  297. return content
  298. @staticmethod
  299. def getFiles(path):
  300. return os.listdir(path)
  301. @staticmethod
  302. def path():
  303. return os.path.split(os.path.realpath(__file__))[0] + '/'
  304. @staticmethod
  305. def cur():
  306. return os.getcwd()
  307. @staticmethod
  308. def exists(name):
  309. return os.path.exists(name)
  310. @staticmethod
  311. def rename(old, new):
  312. return os.rename(old, new)
  313. @staticmethod
  314. def remove(file):
  315. return os.remove(file)
  316. @staticmethod
  317. def mkdir(path):
  318. if File.exists(path) == False:
  319. os.mkdir(path)
  320. return path
  321. @staticmethod
  322. def mkdirs(path):
  323. if File.exists(path) == False:
  324. os.makedirs(path)
  325. return path
  326. @staticmethod
  327. def tmp():
  328. tmp = '/tmp/'
  329. if not File.exists(tmp):
  330. File.mkdir(tmp)
  331. return tmp
  332. class Git(object):
  333. @staticmethod
  334. def update(git, path):
  335. if git and File.exists(path) == False:
  336. Core.popen('git clone ' + git + ' ' + path)
  337. print('init:' + path + ' finished!')
  338. else:
  339. file = path + '/' + '.git/'
  340. if not File.exists(file):
  341. tmp = File.tmp() + path
  342. Core.popen('git clone ' + git + ' ' + tmp + ' && cp -R ' + tmp + '/.git/ ' + file + ' && rm -rf ' + tmp)
  343. Core.popen('cd ' + path + ' && git pull')
  344. print('update:' + path + ' finished!')
  345. @classmethod
  346. def set(self, git, path):
  347. Core.popen('cd ' + path + ' && git pull && git remote set-url origin ' + git, bg=True)
  348. @classmethod
  349. def add(self, git, path):
  350. Core.popen('cd ' + path + ' && git pull && git remote add origin ' + git, bg=True)
  351. @classmethod
  352. def push(self, path, branch, msg):
  353. if not msg:
  354. msg = 'edit'
  355. if not branch:
  356. branch = 'master'
  357. Core.popen('cd ' + path + ' && git pull && git commit -m "'+msg+'" * && git push -u origin ' + branch, True)
  358. class Service(object):
  359. @staticmethod
  360. def set(git, path):
  361. if File.exists(path) == False:
  362. Core.popen('git clone ' + git + ' ' + path, bg=True)
  363. print('init:' + path + ' finished!')
  364. else:
  365. Core.popen('cd ' + path + ' && git pull', bg=True)
  366. print('update:' + path + ' finished!')
  367. class Core(object):
  368. path = ''
  369. @classmethod
  370. def curl(self, url = '', param={}, method = 'get'):
  371. import requests
  372. if method == 'get':
  373. req = requests.get(url, params=param)
  374. else:
  375. req = requests.post(url, params=param)
  376. result = req.text
  377. return result
  378. @classmethod
  379. def getClass(self, name, path=''):
  380. obj = self.getObject(name, path)
  381. if path:
  382. if not hasattr(obj, name):
  383. print('error ' + name)
  384. sys.exit()
  385. obj = getattr(obj, name)
  386. name = name.capitalize()
  387. if not hasattr(obj, name):
  388. print('error ' + name)
  389. sys.exit()
  390. return getattr(obj, name)
  391. @staticmethod
  392. def getObject(name, path = ''):
  393. return __import__(path + name)
  394. @staticmethod
  395. def getMethod(obj, name):
  396. if not hasattr(obj, name):
  397. print('error ' + name)
  398. sys.exit()
  399. return getattr(obj, name)
  400. @classmethod
  401. def shell(self, command, sub=False, bg=False):
  402. shell = self.path + 'src/shell/' + command.replace('.', '/', 1)
  403. return self.popen(shell, sub, bg)
  404. @staticmethod
  405. def popen(command, sub=False, bg=False):
  406. string = command
  407. if Core.platform() == False:
  408. if 'chmod' in command or 'ln -' in command:
  409. return False
  410. '''
  411. if 'shell' in command:
  412. temp = command.split(' ')
  413. n = len(temp)
  414. if n == 3:
  415. command = temp[0] + '.bat ' + temp[1] + ' ' + temp[2]
  416. if n == 2:
  417. command = temp[0] + '.bat ' + temp[1]
  418. else:
  419. command = temp[0] + '.bat '
  420. '''
  421. if 'grep' in command and '| wc -l' in command:
  422. command = command.replace('grep', 'find /C')
  423. command = command.replace('| wc -l', '')
  424. if 'grep' in command:
  425. command = command.replace('grep', 'findstr')
  426. elif bg == True:
  427. command = command + ' 1>/dev/null 2>&1 &'
  428. if sub == False:
  429. process = os.popen(command)
  430. output = process.buffer.read().decode('utf-8')
  431. process.close()
  432. return output
  433. else:
  434. popen = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
  435. output = ''
  436. print(string)
  437. while True:
  438. output = popen.stdout.readline()
  439. print(output)
  440. if popen.poll() is not None:
  441. break
  442. return output
  443. @classmethod
  444. def install(self, soft):
  445. print('install ' + soft + '...')
  446. if soft == 'docker':
  447. self.shell('install.docker', True)
  448. else:
  449. self.shell('install.package ' + soft, True)
  450. @classmethod
  451. def check(self, soft):
  452. result = int(Core.popen('which '+soft+' | wc -l'))
  453. if result != 0:
  454. return 1
  455. else:
  456. self.install(soft)
  457. return 0
  458. @staticmethod
  459. def replace(old, new, string):
  460. if old in string:
  461. string = string.replace(old, new)
  462. return string
  463. @staticmethod
  464. def isset(v):
  465. try :
  466. type(eval(v))
  467. except :
  468. return 0
  469. else :
  470. return 1
  471. @staticmethod
  472. def ip(ifname = 'eth0'):
  473. return ''
  474. import socket, fcntl, struct
  475. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  476. inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
  477. ret = socket.inet_ntoa(inet[20:24])
  478. return ret
  479. @staticmethod
  480. def platform():
  481. import platform
  482. if platform.system().lower() == 'windows' :
  483. return False;
  484. else :
  485. return True;