Advertisement
Guest User

Untitled

a guest
May 30th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 61.29 KB | None | 0 0
  1. # Imports
  2. from __future__ import print_function
  3.  
  4. import argparse
  5. import platform
  6. import shutil
  7. import socket
  8. import subprocess
  9. import time
  10. import zipfile
  11. import os.path
  12. import mysql.connector
  13. import psutil
  14. import atexit
  15. import os
  16. import functools
  17. import signal
  18. import sys
  19. from uptime import uptime
  20. import datetime
  21. import inspect
  22. import pdb
  23.  
  24. db = None
  25. cursor = None
  26. cmonrevbaseline = 1
  27. nmrcalcrevbaseline = 1
  28. nmrcalirevbaseline = 1
  29. nmrmeasurerevbaseline = 1
  30.  
  31.  
  32. class DbSetup:
  33. def __init__(self, defaultrev):
  34. self.dbstart = 'CREATE DATABASE IF NOT EXISTS `apphandler`'
  35. self.sqlprocctr = ("CREATE TABLE IF NOT EXISTS `procctr` (\n"
  36. " `id` INT(11) NOT NULL AUTO_INCREMENT,\n"
  37. " `appname` VARCHAR(20) DEFAULT '',\n"
  38. "`cmd` ENUM(\"\", \"start\", \"stop\", \"update\", \"ready\") DEFAULT 'start',\n "
  39. " `status` VARCHAR(100) DEFAULT 'none',\n"
  40. " `revision` VARCHAR(100) DEFAULT '{}',\n"
  41. " PRIMARY KEY (`id`)\n"
  42. ") DEFAULT CHARSET=utf8 COMMENT='Table that controls the processes' AUTO_INCREMENT=1 ;").format(
  43. defaultrev)
  44. self.sqlprocapps = ('CREATE TABLE IF NOT EXISTS `procapps` (\n'
  45. ' `id` INT(11) NOT NULL AUTO_INCREMENT,\n'
  46. ' `name` VARCHAR(255) DEFAULT \'\',\n'
  47. ' `revcode` VARCHAR(255) DEFAULT \'\',\n'
  48. ' `appblobs` LONGBLOB,\n'
  49. ' PRIMARY KEY (`id`)\n'
  50. ') DEFAULT CHARSET=utf8 COMMENT=\'Table with information about apps\' AUTO_INCREMENT=1 ;')
  51. self.procappschecker = 'SELECT COUNT(*) AS antal FROM `procapps`'
  52. self.proctrchecker = 'SELECT COUNT(*) AS antal FROM `procctr`'
  53. self.procctrsetup = ('INSERT INTO procctr (appname)\n'
  54. 'VALUES\n'
  55. '("cmonapp"),\n'
  56. '("nmrcalculator"),\n'
  57. '("nmrcalibrate"),\n'
  58. '("nmrmeasure")')
  59. self.procctrcheck = "SHOW TABLES LIKE 'procctr'"
  60. self.changedefault = "UPDATE procctr SET cmd='ready' WHERE appname='nmrcalibrate';"
  61. self.bufferextender = 'SET GLOBAL net_buffer_length=1000000;'
  62. self.packetextender = 'SET GLOBAL max_allowed_packet=1000000000'
  63. self.dropprocctr = 'DROP TABLE IF EXISTS procctr;'
  64.  
  65. def dropctr(self):
  66. try:
  67. wlog("M", "Attempting to drop procctr table")
  68. dbcursor = db.cursor(buffered=True)
  69. dbcursor.execute(self.dropprocctr)
  70. db.commit()
  71. dbcursor.close()
  72. wlog("M", "Success")
  73. except mysql.connector.Error as err:
  74. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  75.  
  76. def dbinitializer(self):
  77. try:
  78. wlog("M", "Attempting to initialize database")
  79. cursor.execute(self.dbstart)
  80. db.commit()
  81. wlog("M", "Success")
  82. except mysql.connector.Error as err:
  83. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  84.  
  85. def restrictionremover(self):
  86. try:
  87. wlog("M", "Attempting to remove database restrictions")
  88. cursor.execute(self.bufferextender)
  89. db.commit()
  90. cursor.execute(self.packetextender)
  91. db.commit()
  92. wlog("M", "Success")
  93. except mysql.connector.Error as err:
  94. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  95.  
  96. def changedefaults(self):
  97. try:
  98. wlog("M", "Attempting to change defaults")
  99. dbcursor = db.cursor(buffered=True)
  100. dbcursor.execute(self.changedefault)
  101. db.commit()
  102. wlog("M", "Success")
  103. except mysql.connector.Error as err:
  104. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  105.  
  106. def dbtablesetup(self):
  107. try:
  108. wlog("M", "Attempting to setup tables")
  109. global cursor
  110. cursor.close()
  111. cursor = db.cursor(buffered=True)
  112. cursor.execute(self.proctrchecker)
  113. db.commit()
  114. procctr = cursor.fetchone()[0]
  115. if procctr is 0:
  116. wlog("M", "Setting up procctr table with default values")
  117. cursor.execute(self.procctrsetup)
  118. db.commit()
  119. elif procctr is not 0:
  120. wlog("M", "Procctr table defaults already in place")
  121. cursor.close()
  122. wlog("M", "Success")
  123. except mysql.connector.Error as err:
  124. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  125.  
  126. def crtproccontroller(self):
  127. try:
  128. wlog("M", "Attempting to create procctr table")
  129. # create proc controller if it doesn't exist
  130. global cursor
  131. cursor.close()
  132. cursor = db.cursor(buffered=True)
  133. cursor.execute(self.sqlprocctr)
  134. db.commit()
  135. wlog("M", "Success")
  136. except mysql.connector.Error as err:
  137. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  138.  
  139. def crtprocapps(self):
  140. try:
  141. wlog("M", "Attempting to create procctr table")
  142. global cursor
  143. cursor = db.cursor(buffered=True)
  144. cursor.execute(self.procctrcheck)
  145. db.commit()
  146. if cursor.fetchone()[0] is None:
  147. wlog("D", "Couldn't fetch procctr because it doesn't exist, creating it")
  148. cursor.execute(self.sqlprocapps)
  149. db.commit()
  150. cursor.close()
  151. wlog("M", "Success")
  152. elif cursor.fetchone()[0] > 0:
  153. wlog("M", "Procctr table already exists, no reason to create it")
  154. else:
  155. cursor.execute(self.sqlprocapps)
  156. db.commit()
  157. cursor.close()
  158. wlog("M", "Success")
  159. except mysql.connector.Error as err:
  160. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  161. except Exception as err:
  162. wlog("D", "Caught exception: {}".format(err.message))
  163.  
  164.  
  165. class InitialReader:
  166. def __init__(self, host, database):
  167. self.host = host
  168. self.database = database
  169. self.connectsucces = False
  170.  
  171. @staticmethod
  172. def connect(self):
  173. attempts = 0
  174. while True:
  175. try:
  176. dbconfig = {
  177. 'user': "catmin",
  178. 'password': "nano4a",
  179. 'host': self.host,
  180. 'database': self.database,
  181. 'raise_on_warnings': True,
  182. 'use_pure': False,
  183. }
  184. wlog("M", "Attempting to connect to {}, currently at attempt: {}".format(self.host, attempts))
  185. global db
  186. global cursor
  187. db = mysql.connector.connect(**dbconfig)
  188. cursor = db.cursor(buffered=True)
  189. db.database = self.database
  190. self.connectsucces = True
  191. wlog("M", "Succesfully connected to {} on attempt #{}".format(self.host, attempts))
  192. except Exception as err:
  193. wlog("D", "Caught an exception: {}".format(err.message))
  194. wlog("D", "Try number: {} | Retrying on ip: {}".format(attempts, self.host))
  195. time.sleep(7)
  196. self.connectsucces = False
  197. attempts += 1
  198. if attempts == 20:
  199. wlog("D", "Rebooting because max attempts reached")
  200. os.system("shutdown -t 0 -r -f")
  201. continue
  202. wlog("M", "Done with attempt")
  203. break
  204.  
  205. def rdefrev(self):
  206. if self.connectsucces:
  207. try:
  208. result = self.defrevfetch(self.getprocappids())
  209.  
  210. wlog("M", "Selected revision from procctr table: {}".format(result))
  211.  
  212. return result
  213. except Exception as err:
  214. wlog("D", "Caught an exception: ".format(err.message))
  215.  
  216. def defrevfetch(self, procappsids):
  217. result = None
  218. try:
  219. wlog("M", "Attempting to get revision from procctr table")
  220. statement = 'SELECT revision FROM procctr WHERE appname = "cmonapp"'
  221. cursor.execute(statement)
  222. db.commit()
  223. result = cursor.fetchone()[0]
  224. wlog("M", "Succesfully retrieved cmonrev value from procctr table")
  225. wlog("M", "Result revision: {}".format(result))
  226. except mysql.connector.Error as err:
  227. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  228. global cmonrevbaseline
  229. global nmrcalcrevbaseline
  230. global nmrmeasurerevbaseline
  231. global nmrcalirevbaseline
  232. try:
  233. if result in procappsids:
  234. wlog("M", "Attempting to assign result to global baseline")
  235. cmonrevbaseline = result
  236. nmrcalcrevbaseline = result
  237. nmrmeasurerevbaseline = result
  238. nmrcalirevbaseline = result
  239. wlog("M", "Succesfully assigned result to global baseline")
  240. return result
  241. else:
  242. wlog("M", "Attempting to assign max id from procapps to global baseline")
  243. statement = 'SELECT MAX(id) FROM procapps;'
  244. cursor.execute(statement)
  245. result = cursor.fetchone()[0]
  246. cmonrevbaseline = result
  247. nmrcalcrevbaseline = result
  248. nmrmeasurerevbaseline = result
  249. nmrcalirevbaseline = result
  250. wlog("M", "Succesfully assigned max id from procapps to global baseline")
  251. return result
  252. except mysql.connector.Error as err:
  253. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  254.  
  255. def getprocappids(self):
  256. try:
  257. wlog("M", "Attempting to fetch all ids from procapps")
  258. statement = 'SELECT id FROM procapps'
  259. cursor.execute(statement)
  260. db.commit()
  261. return [str(item[0]) for item in cursor.fetchall()]
  262. except Exception as err:
  263. wlog("D", err.message)
  264.  
  265.  
  266. class DbInteractor:
  267. def __init__(self):
  268. self.cnx = None
  269. self.dbcursor = None
  270. self.sqlprocapps = ('CREATE TABLE IF NOT EXISTS `procapps` (\n'
  271. ' `id` INT(11) NOT NULL AUTO_INCREMENT,\n'
  272. ' `name` VARCHAR(20) DEFAULT \'\',\n'
  273. ' `revcode` VARCHAR(255) DEFAULT \'\',\n'
  274. ' `appblobs` LONGBLOB,\n'
  275. ' PRIMARY KEY (`id`)\n'
  276. ') DEFAULT CHARSET=utf8 COMMENT=\'Table with information about apps\' AUTO_INCREMENT=1 ;')
  277. self.dbstart = 'CREATE DATABASE IF NOT EXISTS `apphandler`'
  278. self.dbcheck = 'SHOW DATABASES LIKE "apphandler";'
  279. self.tablecheck = 'SHOW TABLES LIKE "procapps";'
  280.  
  281. def iblob(self, filepath, ip, username, password, appname, revcode, autoupdateall):
  282. confignodb = {
  283. 'user': username,
  284. 'password': password,
  285. 'host': ip,
  286. 'raise_on_warnings': True,
  287. 'use_pure': False,
  288. }
  289. connectsucces = None
  290. insertsuccess = False
  291. last_id = 0
  292. try:
  293. wlog("M", "Attempting to connect on {}".format(ip))
  294. self.cnx = mysql.connector.connect(**confignodb)
  295. self.dbcursor = self.cnx.cursor(buffered=True)
  296. connectsucces = True
  297. wlog("M", "Connected succesfully")
  298. except mysql.connector.Error as err:
  299. if err.errno == 2003:
  300. connectsucces = False
  301. if connectsucces:
  302. self.dbcursor.execute(self.dbcheck)
  303. self.cnx.commit()
  304. if not self.dbcursor.rowcount:
  305. wlog("M", "No database")
  306. self.dbcursor.execute(self.dbstart)
  307. self.cnx.commit()
  308. else:
  309. wlog("M", "Database already exists")
  310. packetextender = 'SET GLOBAL max_allowed_packet=1073741824;'
  311. self.dbcursor.execute(packetextender)
  312. self.cnx.commit()
  313. self.cnx.database = "apphandler"
  314. self.dbcursor = self.cnx.cursor(buffered=True)
  315. self.dbcursor.execute(self.tablecheck)
  316. self.cnx.commit()
  317. if not self.dbcursor.rowcount:
  318. try:
  319. # Create procapps table if it doesn't exist
  320. wlog("M", "Attempting to create procapps table")
  321. self.dbcursor.execute(self.sqlprocapps)
  322. self.cnx.commit()
  323. wlog("M", "Succesfully created procapps table")
  324. except mysql.connector.Error as err:
  325. wlog("D", "Something went wrong with creating the procapps table")
  326. wlog("D", "Error number: {} | Error Message {}".format(err.errno, err.msg))
  327. else:
  328. wlog("M", "Procapps table already exist already exists")
  329. if self.cnx and self.dbcursor is not None:
  330. wlog("M", "Deploying {} onto {}".format(filepath, ip))
  331. try:
  332. fileread = open(filepath, 'rb').read()
  333. sql = 'INSERT INTO procapps (appblobs, name, revcode) VALUES (_binary %s,_binary %s,_binary %s)'
  334. self.dbcursor.execute(sql, (fileread, appname, revcode))
  335. self.cnx.commit()
  336. last_id = self.dbcursor.getlastrowid()
  337. insertsuccess = True
  338. self.dbcursor.close()
  339. except mysql.connector.Error as err:
  340. wlog("M", "Error number: {} | Error Message {}".format(err.errno, err.msg))
  341.  
  342. if insertsuccess and autoupdateall:
  343. update_success = self.autoupdater(self.cnx, 'all', last_id)
  344. if not update_success:
  345. wlog("M", "failed to autostart")
  346. else:
  347. wlog("M", "successfully managed to autostart")
  348.  
  349.  
  350. else:
  351. wlog("M", "Could not connect to chosen host, please attempt a different ip")
  352.  
  353. def autoupdater(self, cnx, apptype, revid):
  354. CTRL_TABLE_EXIST = 'SHOW TABLES LIKE "procctr"'
  355. CTRL_TABLE_UPDATE_ALL = 'UPDATE procctr set revision = {revcode}, cmd="update"'
  356. update_success = False
  357. poll_cnt = 60
  358. cursor = cnx.cursor()
  359. cursor.execute(CTRL_TABLE_EXIST)
  360. cursor.fetchall()
  361. if cursor.rowcount > 0:
  362. q = CTRL_TABLE_UPDATE_ALL.format(revcode=revid)
  363. cursor.execute(q)
  364. cnx.commit()
  365. while True:
  366. q = 'SELECT COUNT(id) from procctr WHERE cmd = "ready" AND status = "running" AND revision = {}'
  367. cursor.execute(q.format(revid))
  368. res = cursor.fetchall()
  369. cnx.commit()
  370. if res[0][0] >= 4:
  371. update_success = True
  372. break
  373. if poll_cnt < 0:
  374. break
  375. poll_cnt = poll_cnt - 1
  376. time.sleep(5)
  377. wlog("M", "waiting for update (and autostart all) to complete of revision =" + str(revid))
  378.  
  379. return update_success
  380.  
  381. def readblob(self, outputpath, idtag, outputname):
  382. wlog("M", "Reading blob with id: {}".format(idtag))
  383. ospath = os.path.join(outputpath, str(idtag)).replace('\\', '/')
  384. ospathwithname = os.path.join(outputpath, str(idtag), outputname).replace('\\', '/')
  385. while True:
  386. try:
  387. if not os.path.exists(ospath):
  388. wlog("M", "Creating directory: {}".format(ospath))
  389. try:
  390. os.makedirs(ospath)
  391. wlog("M", "Succesfully created path to place blob into")
  392. except Exception as err:
  393. wlog("D",
  394. "Exception caught while creating: {} | Error Message {}".format(ospath, err.message))
  395. with open(ospathwithname, "wb+") as output_file:
  396. try:
  397. global cursor
  398. cursor = db.cursor(buffered=True)
  399. sql = 'SELECT appblobs FROM procapps WHERE id = %s'
  400. cursor.execute(sql, (idtag,))
  401. db.commit()
  402. ablob = cursor.fetchone()
  403. output_file.write(ablob[0])
  404. except Exception as err:
  405. print(type(err).__name__)
  406. wlog("D", "Exception caught while unzipping: {} | Error Message {}".format(ospathwithname,
  407. err.message))
  408. except Exception as err:
  409. wlog("D", "Could not find a blob with id: {}".format(idtag))
  410. wlog("D", "Error: {}".format(err.message))
  411. time.sleep(10)
  412. continue
  413. break
  414.  
  415.  
  416. class ZipHandler:
  417. def __init__(self):
  418. pass
  419.  
  420. def unzip(self, outpath, outname, revision, archivename):
  421. try:
  422. zippath = os.path.join(outpath, str(revision), outname)
  423. archivepath = os.path.join(outpath, str(revision), archivename)
  424. wlog("M", "Zip path: {}".format(zippath))
  425. wlog("M", "Zip unzip path: {}".format(archivepath))
  426. with zipfile.ZipFile(zippath, "r") as zip_ref:
  427. if os.path.exists(archivepath) is False:
  428. zip_ref.extractall(archivepath)
  429. elif os.path.exists(archivepath):
  430. shutil.rmtree(archivepath, ignore_errors=True)
  431. zip_ref.extractall(archivepath)
  432. except Exception as err:
  433. wlog("D", "Caught exception, message: {}".format(err.message))
  434.  
  435.  
  436. class ProcHandler:
  437. def __init__(self):
  438. pass
  439.  
  440. def running(self, appname):
  441. name = None
  442. if appname == "cmonapp.exe":
  443. name = appname.replace(".exe", "")
  444. elif appname == "nmrcalibrate.exe":
  445. name = appname.replace(".exe", "")
  446. elif appname == "nmrcalculatorapp.exe":
  447. name = appname.replace("app.exe", "")
  448. elif appname == "nmrmeasureapp.exe":
  449. name = appname.replace("app.exe", "")
  450. dbcursor = db.cursor()
  451. wlog("M", "Process not running: {}".format(name))
  452. statussql = 'UPDATE apphandler.procctr SET status = "running" WHERE appname = %s'
  453. dbcursor.execute(statussql, (name,))
  454. db.commit()
  455.  
  456. def startproc(self, revision, firstpath, secondpath, ip, exename):
  457. si = subprocess.STARTUPINFO()
  458. # si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  459. si.wShowWindow = subprocess.SW_HIDE
  460. if exename == 'cmonapp.exe':
  461. start = True
  462. procs = list(psutil.process_iter())
  463. for proc in procs:
  464. if exename == proc.name():
  465. start = False
  466. break
  467. if start:
  468. wlog("M", "Starting {}".format(exename))
  469. path = os.path.join(firstpath, str(revision), secondpath).replace("/", "\\")
  470. result = subprocess.Popen([path, "ip", ip],
  471. creationflags=subprocess.CREATE_NEW_CONSOLE, startupinfo=si)
  472. wlog("M", result)
  473. if result is not None:
  474. wlog("M", "{} succesfully started".format(exename))
  475. self.running(exename.replace(".exe", ""))
  476. return result
  477. elif exename == 'nmrcalculatorapp.exe':
  478. start = True
  479. procs = list(psutil.process_iter())
  480. for proc in procs:
  481. if exename == proc.name():
  482. start = False
  483. break
  484. if start:
  485. wlog("M", "Starting {}".format(exename))
  486. path = os.path.join(firstpath, str(revision), secondpath).replace("/", "\\")
  487. result = subprocess.Popen(
  488. [path, "ip", ip, "treatAsLocal", "True"],
  489. creationflags=subprocess.CREATE_NEW_CONSOLE, startupinfo=si)
  490. wlog("M", result)
  491. if result is not None:
  492. wlog("M", "{} succesfully started".format(exename))
  493. self.running(exename.replace(".exe", ""))
  494. return result
  495. elif exename == 'nmrcalibrate.exe':
  496. start = True
  497. procs = list(psutil.process_iter())
  498. for proc in procs:
  499. if exename == proc.name():
  500. start = False
  501. break
  502. if start:
  503. wlog("M", "Starting {}".format(exename))
  504. path = os.path.join(firstpath, str(revision), secondpath).replace("/", "\\")
  505. result = subprocess.Popen([path, "ip", ip],
  506. creationflags=subprocess.CREATE_NEW_CONSOLE, startupinfo=si)
  507. wlog("M", result)
  508. if result is not None:
  509. wlog("M", "{} succesfully started".format(exename))
  510. self.running(exename.replace(".exe", ""))
  511. return result
  512. elif exename == 'nmrmeasureapp.exe':
  513. start = True
  514. procs = list(psutil.process_iter())
  515. for proc in procs:
  516. if exename == proc.name():
  517. start = False
  518. break
  519. if start:
  520. wlog("M", "Starting {}".format(exename))
  521. path = os.path.join(firstpath, str(revision), secondpath).replace("/", "\\")
  522. result = subprocess.Popen(
  523. [path, "ip", ip, "treatAsLocal", "True"],
  524. creationflags=subprocess.CREATE_NEW_CONSOLE, startupinfo=si)
  525. wlog("M", result)
  526. if result is not None:
  527. wlog("M", "{} succesfully started".format(exename))
  528. self.running(exename.replace(".exe", ""))
  529. return result
  530. else:
  531. wlog("M", "No recognizeable executeable name")
  532.  
  533. def stpprocwname(self, exename):
  534. printvar = False
  535. for proc in psutil.process_iter():
  536. if proc.name() == exename:
  537. try:
  538. proc.kill()
  539. printvar = True
  540. except Exception as err:
  541. print("D", "Caught exception: {}".format(err))
  542. if printvar is True:
  543. wlog("M", "Killing {}".format(exename))
  544. elif printvar is False:
  545. wlog("M", "No {} to kill".format(exename))
  546.  
  547. def stpprocwproc(self, proc, appname):
  548. try:
  549. global cursor
  550. cursor = db.cursor(buffered=True)
  551. db.database = "apphandler"
  552. if proc is not None:
  553. proc.kill()
  554. time.sleep(1)
  555. wlog("M", "Process not running: {}".format(appname))
  556. statussql = 'UPDATE procctr SET status = "stopped" WHERE appname = %s'
  557. cursor.execute(statussql, (appname,))
  558. db.commit()
  559. cursor.close()
  560. else:
  561. wlog("M", "Did not find any process to kill: {}".format(appname))
  562. except Exception as err:
  563. print("D", "Caught exception: {}".format(err))
  564.  
  565. def stpappswproc(self, cmonproc, calcproc, calibrateproc, measureproc):
  566. if cmonproc is not None:
  567. self.stpprocwproc(cmonproc, "cmonapp")
  568. else:
  569. wlog("M", "Can't stop because process doesn't exist")
  570. if calcproc is not None:
  571. self.stpprocwproc(calcproc, "nmrcalculator")
  572. else:
  573. wlog("M", "Can't stop because process doesn't exist")
  574. if calibrateproc is not None:
  575. self.stpprocwproc(calibrateproc, "nmrcalibrate")
  576. else:
  577. wlog("M", "Can't stop because process doesn't exist")
  578. if measureproc is not None:
  579. self.stpprocwproc(measureproc, "nmrmeasure")
  580. else:
  581. wlog("M", "Can't stop because process doesn't exist")
  582.  
  583. def stopallapps(self):
  584. self.stpprocwname("cmonapp.exe")
  585. self.stpprocwname("nmrcalculatorapp.exe")
  586. self.stpprocwname("nmrcalibrate.exe")
  587. self.stpprocwname("nmrmeasureapp.exe")
  588. time.sleep(3)
  589.  
  590.  
  591. class DbCmdChecker:
  592. def __init__(self, connectionobject):
  593. self.statuschecker = True
  594. self.setallcmdstostart = False
  595. self.cmonproc = None
  596. self.nmrmeasureproc = None
  597. self.nmrcalculatorproc = None
  598. self.nmrcalibrateproc = None
  599. self.cmoncmd = None
  600. self.nmrcalccmd = None
  601. self.nmrcalicmd = None
  602. self.nmrmeasurecmd = None
  603. self.cmonrev = None
  604. self.nmrcalcrev = None
  605. self.nmrcalirev = None
  606. self.nmrmeasurerev = None
  607. self.connectionobject = connectionobject
  608. self.attempts = 0
  609.  
  610. def checkdb(self, interval, firstpathtocmon, secondpathtocmon, cmonexename, firstpathtocalibrate,
  611. secondpathtocalibrate, calibrateexename, firstpathtocalc, secondpathtocalc,
  612. calcexename, firstpathtomeasure, secondpathtomeasure, measureexename, ip):
  613. while True:
  614. wlog("M", "--------------------------------------------")
  615. programstabilizer()
  616. self.fetchcmds()
  617. self.ftchrevs()
  618. self.gbcollector()
  619. self.insertip()
  620. if self.setallcmdstostart:
  621. self.updatecmds()
  622. self.setallcmdstostart = False
  623.  
  624. # Cmon handling
  625. if firstpathtocmon is None:
  626. wlog("M", "No executable path to cmon")
  627. elif firstpathtocmon is not None:
  628. self.dchndlrcmon(self.cmonrev, firstpathtocmon, secondpathtocmon, ip, cmonexename)
  629.  
  630. # calibrate handling
  631. if firstpathtocalibrate is None:
  632. wlog("M", "No executable path to nmrcalibrate")
  633. elif firstpathtocalibrate is not None:
  634. self.dchandlrcali(self.nmrcalirev, firstpathtocalibrate, secondpathtocalibrate, ip,
  635. calibrateexename)
  636. # calchandling
  637. if firstpathtocalc is None:
  638. wlog("M", "No executable path to nmrcalculator")
  639. elif firstpathtocalc is not None:
  640. self.dchandlrcalc(self.nmrcalcrev, firstpathtocalc, secondpathtocalc, ip, calcexename)
  641.  
  642. # measurehandling
  643. if firstpathtomeasure is None:
  644. wlog("M", "No executable path to nmrmeasure")
  645. elif firstpathtomeasure is not None:
  646. self.dchandlrmeasure(self.nmrmeasurerev, firstpathtomeasure, secondpathtomeasure, ip,
  647. measureexename)
  648.  
  649. time.sleep(interval)
  650.  
  651. cursor.close()
  652.  
  653. def fetchcmds(self):
  654. while True:
  655. try:
  656. wlog("M", "Fetching commands")
  657. global cursor
  658. cursor = db.cursor(buffered=True)
  659. cmonsql = "SELECT cmd FROM procctr WHERE appname = 'cmonapp'"
  660. nmrcalcsql = "SELECT cmd FROM procctr WHERE appname = 'nmrcalculator'"
  661. nmrcalibratesql = "SELECT cmd FROM procctr WHERE appname = 'nmrcalibrate'"
  662. nmrmeasureappsql = "SELECT cmd FROM procctr WHERE appname = 'nmrmeasure'"
  663. cursor.execute(cmonsql)
  664. db.commit()
  665. self.cmoncmd = cursor.fetchone()[0]
  666. cursor.execute(nmrcalcsql)
  667. db.commit()
  668. self.nmrcalccmd = cursor.fetchone()[0]
  669. cursor.execute(nmrcalibratesql)
  670. db.commit()
  671. self.nmrcalicmd = cursor.fetchone()[0]
  672. cursor.execute(nmrmeasureappsql)
  673. db.commit()
  674. self.nmrmeasurecmd = cursor.fetchone()[0]
  675. wlog("M", "Got cmds: Cmon {} | Nmrcalc {} | Nmrcalicmd {} | Nmrmeasure {}".format(self.cmoncmd,
  676. self.nmrcalccmd,
  677. self.nmrcalicmd,
  678. self.nmrmeasurecmd))
  679. except Exception as err:
  680. wlog("D", "Caught an exception: {}".format(err.message))
  681. InitialReader.connect(self.connectionobject)
  682. continue
  683. break
  684.  
  685. def ftchrevs(self):
  686. while True:
  687. try:
  688. wlog("M", "Fetching revisions")
  689. global cursor
  690. cursor = db.cursor(buffered=True)
  691. cmonrevsql = "SELECT revision FROM procctr WHERE appname = 'cmonapp'"
  692. nmrcalcrevsql = "SELECT revision FROM procctr WHERE appname = 'nmrcalculator'"
  693. nmrcalirevsql = "SELECT revision FROM procctr WHERE appname = 'nmrcalibrate'"
  694. nmrmeasurerevsql = "SELECT revision FROM procctr WHERE appname = 'nmrmeasure'"
  695. cursor.execute(cmonrevsql)
  696. db.commit()
  697. self.cmonrev = cursor.fetchone()[0]
  698. cursor.execute(nmrcalcrevsql)
  699. db.commit()
  700. self.nmrcalcrev = cursor.fetchone()[0]
  701. cursor.execute(nmrcalirevsql)
  702. db.commit()
  703. self.nmrcalirev = cursor.fetchone()[0]
  704. cursor.execute(nmrmeasurerevsql)
  705. db.commit()
  706. self.nmrmeasurerev = cursor.fetchone()[0]
  707. wlog("M", "Got revisions: Cmon {} | Nmrcalc {} | Nmrcalicmd {} | Nmrmeasure {}".format(self.cmonrev,
  708. self.nmrcalcrev,
  709. self.nmrcalirev,
  710. self.nmrmeasurerev))
  711. except Exception as err:
  712. wlog("D", "Caught an exception: {}".format(err.message))
  713. InitialReader.connect(self.connectionobject)
  714. continue
  715. break
  716.  
  717. def insertip(self):
  718. ip = socket.gethostbyname(socket.gethostname())
  719. try:
  720. global db
  721. global cursor
  722. db.database = "tsl"
  723. checktable = "SHOW TABLES like 'ipinfo'"
  724. createtable = ('CREATE TABLE IF NOT EXISTS `ipinfo` (\n'
  725. ' `id` INT(11) NOT NULL AUTO_INCREMENT,\n'
  726. ' `ip` VARCHAR(255) DEFAULT \'\',\n'
  727. ' `platform` VARCHAR(255) DEFAULT \'\',\n'
  728. ' `uptime` LONGBLOB,\n'
  729. ' PRIMARY KEY (`id`)\n'
  730. ') DEFAULT CHARSET=utf8 COMMENT=\'Table with information about apps\' AUTO_INCREMENT=1 ;')
  731. # updateip = "UPDATE {} SET {}=\"{}\" WHERE id={}".format("ipinfo", "ip", ip, 1)
  732. update = "REPLACE INTO {} ({}, {}, {}, {}) VALUES ({}, '{}', '{}', '{}');".format("ipinfo", "id", "ip",
  733. "platform", "uptime", 1,
  734. ip, platform.platform(),
  735. uptime())
  736.  
  737. cursor.execute(checktable)
  738. if cursor.fetchone()[0] > 0:
  739. wlog("M", "Ipinfo table already exists, no reason to create it")
  740. else:
  741. wlog("M", "Creating Ipinfo table and replacing ip")
  742. cursor.execute(createtable)
  743. db.commit()
  744. cursor.execute(update)
  745. db.commit()
  746. db.database = "apphandler"
  747. except mysql.connector.Error as err:
  748. db.rollback()
  749. wlog("D", "Caught a mysql exception | number: {} | message: {}".format(err.errno, err.msg))
  750.  
  751. def updatebundler(self, revision):
  752. global cmonrevbaseline
  753. global nmrcalcrevbaseline
  754. global nmrmeasurerevbaseline
  755. global nmrcalirevbaseline
  756. wlog("M",
  757. "BU| Base / Cmonrev {} / {} | Base / Measurerev {} / {} | Base / Calcrev {} / {} | Base / Calirev {} / {}".format(
  758. cmonrevbaseline, self.cmonrev, nmrmeasurerevbaseline, self.nmrmeasurerev, nmrcalcrevbaseline,
  759. self.nmrcalcrev, nmrcalirevbaseline, self.nmrcalirev))
  760. if self.cmonrev != cmonrevbaseline:
  761. self.updateaction(revision, self.cmonrev)
  762. elif self.nmrcalcrev != nmrcalcrevbaseline:
  763. self.updateaction(revision, self.nmrcalcrev)
  764. elif self.nmrmeasurerev != nmrmeasurerevbaseline:
  765. self.updateaction(revision, self.nmrmeasurerev)
  766. elif self.nmrcalirev != nmrcalirevbaseline:
  767. self.updateaction(revision, self.nmrcalirev)
  768. wlog("M",
  769. "AU| Base / Cmonrev {} / {} | Base / Measurerev {} / {} | Base / Calcrev {} / {} | Base / Calirev {} / {}".format(
  770. cmonrevbaseline, self.cmonrev, nmrmeasurerevbaseline, self.nmrmeasurerev, nmrcalcrevbaseline,
  771. self.nmrcalcrev, nmrcalirevbaseline, self.nmrcalirev))
  772.  
  773. def settoupdateall(self):
  774. names = ["cmonapp", "nmrcalculator", "nmrmeasure", "nmrcalibrate"]
  775. for element in names:
  776. try:
  777. wlog("M", "Attempting to set status to update")
  778. db.database = "apphandler"
  779. name = element
  780. wlog("M", "Appname: {}".format(name))
  781. cmdupdate = 'UPDATE procctr SET status = "updating" WHERE appname = "{}"'.format(name)
  782. cur = db.cursor(buffered=True)
  783. cur.execute(cmdupdate)
  784. db.commit()
  785. wlog("M", "Succesfully set status for {} to update".format(name))
  786. except mysql.connector.Error as err:
  787. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  788. except Exception as err:
  789. wlog("D", "Caught an exception: {}".format(err.message))
  790.  
  791. def settoupdate(self, appname):
  792. try:
  793. wlog("M", "Attempting to set status to update")
  794. db.database = "apphandler"
  795. name = appname.split('.')[0]
  796. wlog("M", "Appname: {}".format(name))
  797. cmdupdate = 'UPDATE procctr SET status = "updating" WHERE appname = "{}"'.format(name)
  798. cur = db.cursor(buffered=True)
  799. cur.execute(cmdupdate)
  800. db.commit()
  801. wlog("M", "Succesfully set status for {} to update".format(name))
  802. except mysql.connector.Error as err:
  803. wlog("D", "Mysql Connection Error: {} | Message: {}".format(err.msg, err.errno))
  804. except Exception as err:
  805. wlog("D", "Caught an exception: {}".format(err.message))
  806.  
  807. def updateaction(self, revision, procrevision):
  808. # Reset process state
  809. prochandler = ProcHandler()
  810. prochandler.stpappswproc(self.cmonproc, self.nmrcalculatorproc, self.nmrcalibrateproc,
  811. self.nmrmeasureproc)
  812. wlog("M", "Attempting to update procrev: {}".format(procrevision))
  813. self.settoupdateall()
  814. # Read specified blob from database
  815. dbint = DbInteractor()
  816. dbint.readblob(os.path.expanduser("~/artifacts/"), revision, "output.zip")
  817. # Unzip the zip file
  818. zipper = ZipHandler()
  819. zipper.unzip(os.path.expanduser("~/artifacts/"), "output.zip", revision, "")
  820. self.updateglobalrevs(procrevision)
  821. self.updateobjectrevs(procrevision)
  822. self.updaterevisions(revision)
  823. self.setallcmdstostart = True
  824. wlog("M", "Succesfully updated to procrev: {}".format(procrevision))
  825. wlog("M", "procrev: {}".format(procrevision))
  826.  
  827. def updateglobalrevs(self, procrevision):
  828. global cmonrevbaseline
  829. global nmrmeasurerevbaseline
  830. global nmrcalirevbaseline
  831. global nmrcalcrevbaseline
  832. cmonrevbaseline = procrevision
  833. nmrmeasurerevbaseline = procrevision
  834. nmrcalirevbaseline = procrevision
  835. nmrcalcrevbaseline = procrevision
  836.  
  837. def updateobjectrevs(self, procrevision):
  838. self.cmonrev = procrevision
  839. self.nmrcalirev = procrevision
  840. self.nmrcalcrev = procrevision
  841. self.nmrmeasurerev = procrevision
  842.  
  843. def ready(self, appname):
  844. if appname == "cmonapp.exe":
  845. name = appname.replace(".exe", "")
  846. if appname == "nmrcalibrate.exe":
  847. name = appname.replace(".exe", "")
  848. if appname == "nmrcalculatorapp.exe":
  849. name = appname.replace("app.exe", "")
  850. if appname == "nmrmeasureapp.exe":
  851. name = appname.replace("app.exe", "")
  852. dbcursor = db.cursor()
  853. cmdreset = 'UPDATE apphandler.procctr SET cmd = "ready" WHERE appname = %s'
  854. dbcursor.execute(cmdreset, (name,))
  855. db.commit()
  856.  
  857.  
  858. def updatecmds(self):
  859. try:
  860. wlog("M", "Updating commands")
  861. self.cmoncmd = "start"
  862. self.nmrcalccmd = "start"
  863. self.nmrmeasurecmd = "start"
  864. self.nmrcalicmd = "start"
  865. wlog("M", "Succesfully updated commands")
  866. except Exception as err:
  867. wlog("D", "Caught an exception: {}".format(err.message))
  868.  
  869. def updaterevisions(self, revision):
  870. try:
  871. wlog("M", "Attempting to update revisions")
  872. revcodesql = 'UPDATE procctr SET revision = %s WHERE appname = %s'
  873. cur = db.cursor(buffered=True)
  874. cur.execute(revcodesql, (revision, "cmonapp"))
  875. db.commit()
  876. cur.execute(revcodesql, (revision, "nmrcalculator"))
  877. db.commit()
  878. cur.execute(revcodesql, (revision, "nmrcalibrate"))
  879. db.commit()
  880. cur.execute(revcodesql, (revision, "nmrmeasure"))
  881. db.commit()
  882. wlog("M", "Fetched revisions successfully")
  883. except Exception as err:
  884. wlog("D", "Caught an exception: {}".format(err.message))
  885.  
  886. def dchndlrcmon(self, revision, firstpath, secondpath, ip, exename):
  887. if self.cmoncmd == 'start':
  888. wlog("M", "Attempting to launch cmon")
  889. prochandler = ProcHandler()
  890. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  891. if localproc is not None:
  892. self.cmonproc = localproc
  893. self.ready(exename)
  894. elif self.cmoncmd == 'stop':
  895. prochandler = ProcHandler()
  896. prochandler.stpprocwproc(self.cmonproc, "cmonapp")
  897. self.ready(exename)
  898. elif self.cmoncmd == 'update':
  899. self.statuschecker = None
  900. self.updatebundler(revision)
  901. self.ready(exename)
  902. self.statusupdater(self.cmonproc, "cmonapp", firstpath, secondpath, ip, exename, revision)
  903.  
  904. def dchandlrcalc(self, revision, firstpath, secondpath, ip, exename):
  905. if self.nmrcalccmd == 'start':
  906. wlog("M", "Attempting to launch nmrcalculator")
  907. prochandler = ProcHandler()
  908. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  909. if localproc is not None:
  910. self.nmrcalculatorproc = localproc
  911. self.ready(exename)
  912. elif self.nmrcalccmd == 'stop':
  913. prochandler = ProcHandler()
  914. prochandler.stpprocwproc(self.nmrcalculatorproc, "nmrcalculator")
  915. self.ready(exename)
  916. elif self.nmrcalccmd == 'update':
  917. self.updatebundler(revision)
  918. self.ready(exename)
  919. self.statusupdater(self.nmrcalculatorproc, "nmrcalculator", firstpath, secondpath, ip, exename, revision)
  920.  
  921. def dchandlrcali(self, revision, firstpath, secondpath, ip, exename):
  922. if self.nmrcalicmd == 'start':
  923. wlog("M", "Attempting to launch nmrcalibrate")
  924. prochandler = ProcHandler()
  925. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  926. if localproc is not None:
  927. self.nmrcalibrateproc = localproc
  928. self.ready(exename)
  929. elif self.nmrcalicmd == 'stop':
  930. prochandler = ProcHandler()
  931. prochandler.stpprocwproc(self.nmrcalibrateproc, "nmrcalibrate")
  932. self.ready(exename)
  933. elif self.nmrcalicmd == 'update':
  934. self.updatebundler(revision)
  935. self.ready(exename)
  936. self.statusupdater(self.nmrcalibrateproc, "nmrcalibrate", firstpath, secondpath, ip, exename, revision)
  937.  
  938. def dchandlrmeasure(self, revision, firstpath, secondpath, ip, exename):
  939. if self.nmrmeasurecmd == 'start':
  940. wlog("M", "Attempting to launch nmrmeasure")
  941. prochandler = ProcHandler()
  942. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  943. if localproc is not None:
  944. self.nmrmeasureproc = localproc
  945. self.ready(exename)
  946. elif self.nmrmeasurecmd == 'stop':
  947. prochandler = ProcHandler()
  948. prochandler.stpprocwproc(self.nmrmeasureproc, "nmrmeasure")
  949. self.ready(exename)
  950. elif self.nmrmeasurecmd == 'update':
  951. self.updatebundler(revision)
  952. self.ready(exename)
  953. self.statusupdater(self.nmrmeasureproc, "nmrmeasure", firstpath, secondpath, ip, exename, revision)
  954.  
  955. def statusupdater(self, proc, appname, firstpath, secondpath, ip, exename, revision):
  956. global cursor
  957. cursor = db.cursor(buffered=True)
  958. db.database = "apphandler"
  959.  
  960. cmonsql = "SELECT status FROM procctr WHERE appname = 'cmonapp'"
  961. nmrcalcsql = "SELECT status FROM procctr WHERE appname = 'nmrcalculator'"
  962. nmrcalibratesql = "SELECT status FROM procctr WHERE appname = 'nmrcalibrate'"
  963. nmrmeasureappsql = "SELECT status FROM procctr WHERE appname = 'nmrmeasure'"
  964.  
  965. if proc is not None:
  966. status = proc.poll()
  967. wlog("M", "Status for proc: {}".format(status))
  968. time.sleep(2)
  969. statussecond = proc.poll()
  970. wlog("M", "Status for proc: {}".format(statussecond))
  971. else:
  972. status = 1
  973. statussecond = 1
  974.  
  975. self.supdatenmrcmon(appname, cmonsql, cursor, exename, firstpath, ip, revision, secondpath, status,
  976. statussecond)
  977.  
  978. self.supdatenmrcalc(appname, cursor, exename, firstpath, ip, nmrcalcsql, revision, secondpath, status,
  979. statussecond)
  980.  
  981. self.supdatenmrcali(appname, cursor, exename, firstpath, ip, nmrcalibratesql, revision, secondpath, status,
  982. statussecond)
  983.  
  984. self.supdatenmrmeasure(appname, cursor, exename, firstpath, ip, nmrmeasureappsql, revision, secondpath, status,
  985. statussecond)
  986.  
  987. if proc is not None:
  988. if status is None:
  989. try:
  990. cursor = db.cursor(buffered=True)
  991. wlog("M", "Proc running, attempting to update cmd for: {}".format(appname))
  992. statussql = "UPDATE procctr SET status = 'running' WHERE appname = '{}'".format(appname)
  993. cursor.execute(statussql)
  994. db.commit()
  995. wlog("M", "Succesfully updated cmd for {}".format(appname))
  996. except Exception as err:
  997. wlog("D", "Caught exception while updating cmd for: {}".format(appname))
  998. wlog("D", "Exception: {}".format(err))
  999. else:
  1000. try:
  1001. wlog("M", "Process not running attempting to update cmd for: {}".format(appname))
  1002. cursor = db.cursor(buffered=True)
  1003. statussql = "UPDATE procctr SET status = 'stopped' WHERE appname = '{}'".format(appname)
  1004. cursor.execute(statussql)
  1005. db.commit()
  1006. wlog("M", "Succesfully updated cmd for {}".format(appname))
  1007. except Exception as err:
  1008. wlog("D", "Caught exception while updating cmd for: {}".format(appname))
  1009. wlog("D", "Exception: {}".format(err))
  1010.  
  1011. cursor.close()
  1012.  
  1013. def supdatenmrcmon(self, appname, cmonsql, dbcursor, exename, firstpath, ip, revision, secondpath, status, statussecond):
  1014. if appname == 'cmonapp':
  1015. if status is not None:
  1016. # Status for cmon
  1017. dbcursor.execute(cmonsql)
  1018. db.commit()
  1019. cmonstatus = dbcursor.fetchone()[0]
  1020. wlog("M", "Status for: {} - {}".format(appname, cmonstatus))
  1021. if cmonstatus == 'running':
  1022. if statussecond is None:
  1023. wlog("M", "{} has uptime".format(appname))
  1024. else:
  1025. wlog("M", "{} no uptime, attempting to start".format(appname))
  1026. self.cmoncmd = 'failed'
  1027. prochandler = ProcHandler()
  1028. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  1029. if localproc is not None:
  1030. self.cmonproc = localproc
  1031. time.sleep(3)
  1032. if self.nmrcalibrateproc.poll() is not None:
  1033. self.supdatenmrcmon(appname, dbcursor, exename, firstpath, ip, cmonsql, revision, secondpath, status, statussecond)
  1034.  
  1035. def supdatenmrcalc(self, appname, dbcursor, exename, firstpath, ip, nmrcalcsql, revision, secondpath, status, statussecond):
  1036. if appname == 'nmrcalculator':
  1037. if status is not None:
  1038. # Status for cmon
  1039. dbcursor.execute(nmrcalcsql)
  1040. db.commit()
  1041. nmrcalcstatus = dbcursor.fetchone()[0]
  1042. ("M", "Status for: {} - {}".format(appname, nmrcalcstatus))
  1043. if nmrcalcstatus == 'running':
  1044. if statussecond is None:
  1045. wlog("M", "{} has uptime".format(appname))
  1046. else:
  1047. wlog("M", "{} no uptime, attempting to start".format(appname))
  1048. self.nmrcalccmd = 'failed'
  1049. prochandler = ProcHandler()
  1050. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  1051. if localproc is not None:
  1052. self.nmrcalculatorproc = localproc
  1053. time.sleep(3)
  1054. if localproc.poll() is not None:
  1055. self.supdatenmrcalc(appname, dbcursor, exename, firstpath, ip, nmrcalcsql, revision, secondpath, status, statussecond)
  1056. else:
  1057. wlog("M", "{} started correctly".format(appname))
  1058.  
  1059. def supdatenmrcali(self, appname, dbcursor, exename, firstpath, ip, nmrcalibratesql, revision, secondpath, status, statussecond):
  1060. if appname == 'nmrcalibrate':
  1061. if status is not None:
  1062. # Status for cmon
  1063. dbcursor.execute(nmrcalibratesql)
  1064. db.commit()
  1065. nmrcalistatus = dbcursor.fetchone()[0]
  1066. ("M", "Status for: {} - {}".format(appname, nmrcalistatus))
  1067. if nmrcalistatus == 'running':
  1068. if statussecond is None:
  1069. wlog("M", "{} has uptime".format(appname))
  1070. else:
  1071. wlog("M", "{} no uptime, attempting to start".format(appname))
  1072. self.nmrcalicmd = 'failed'
  1073. prochandler = ProcHandler()
  1074. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  1075. if localproc is not None:
  1076. self.nmrcalibrateproc = localproc
  1077. time.sleep(3)
  1078. if localproc.poll() is not None:
  1079. self.supdatenmrcali(appname, dbcursor, exename, firstpath, ip, nmrcalibratesql, revision, secondpath, status, statussecond)
  1080. else:
  1081. wlog("M", "{} started correctly".format(appname))
  1082.  
  1083. def supdatenmrmeasure(self, appname, dbcursor, exename, firstpath, ip, nmrmeasureappsql, revision, secondpath, status, statussecond):
  1084. if appname == 'nmrmeasure':
  1085. if status is not None:
  1086. # Status for cmon
  1087. dbcursor.execute(nmrmeasureappsql)
  1088. db.commit()
  1089. nmrmeasurestatus = dbcursor.fetchone()[0]
  1090. ("M", "Status for: {} - {}".format(appname, nmrmeasurestatus))
  1091. if nmrmeasurestatus == 'running':
  1092. if statussecond is None:
  1093. wlog("M", "{} has uptime".format(appname))
  1094. else:
  1095. wlog("M", "{} no uptime, attempting to start".format(appname))
  1096. self.nmrmeasurecmd = 'failed'
  1097. prochandler = ProcHandler()
  1098. localproc = prochandler.startproc(revision, firstpath, secondpath, ip, exename)
  1099. if localproc is not None:
  1100. self.nmrmeasureproc = localproc
  1101. time.sleep(3)
  1102. if localproc.poll() is not None:
  1103. self.supdatenmrmeasure(appname, dbcursor, exename, firstpath, ip, nmrmeasureappsql, revision, secondpath, status, statussecond)
  1104. else:
  1105. wlog("M", "{} started correctly".format(appname))
  1106.  
  1107. def scanartifacts(self):
  1108. import os
  1109. root = os.path.expanduser("~/artifacts/")
  1110. return [item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item))]
  1111.  
  1112. def gbcollector(self):
  1113. global cmonrevbaseline
  1114. global nmrcalcrevbaseline
  1115. global nmrcalirevbaseline
  1116. global nmrmeasurerevbaseline
  1117. revlist = [str(cmonrevbaseline), str(nmrcalcrevbaseline), str(nmrcalirevbaseline), str(nmrmeasurerevbaseline)]
  1118. directorylist = self.scanartifacts()
  1119. removallist = []
  1120. for element in directorylist:
  1121. if element not in revlist:
  1122. wlog("M", "Directory set to be deleted: {}".format(element))
  1123. removallist.append(element)
  1124. if not removallist:
  1125. wlog("M", "Nothing to remove")
  1126. else:
  1127. for element in removallist:
  1128. wlog("M", "Removing directory: {}".format(element))
  1129. shutil.rmtree(os.path.expanduser("~/artifacts/{}".format(element)))
  1130.  
  1131.  
  1132. def connectionkiller():
  1133. try:
  1134. wlog("M", "Attempting to kill cursor and connection")
  1135. global cursor
  1136. global db
  1137. cursor.close()
  1138. db.close()
  1139. except mysql.connector.Error as err:
  1140. wlog("D", "Caught mysql error number: {} | message: {}".format(err.errno, err.msg))
  1141.  
  1142.  
  1143. WATCHED_FILES = [__file__]
  1144. WATCHED_FILES_MTIMES = [(f, os.path.getmtime(f)) for f in WATCHED_FILES]
  1145.  
  1146.  
  1147. def programstabilizer():
  1148. global WATCHED_FILES
  1149. global WATCHED_FILES_MTIMES
  1150. for f, mtime in WATCHED_FILES_MTIMES:
  1151. if os.path.getmtime(f) != mtime:
  1152. wlog("M", "script has changed restarting it")
  1153. if platform.system() == 'Linux':
  1154. os.system('clear')
  1155. os.execv(__file__, sys.argv)
  1156. elif platform.system() == 'Windows':
  1157. os.system('cls')
  1158. os.execv(sys.executable, ['python'] + sys.argv)
  1159.  
  1160.  
  1161. def restartifclosed():
  1162. wlog("M", "script has changed, restarting it")
  1163. # When running the script via `./pywin.py` (e.g. Linux/Mac OS), use
  1164. if platform.system() == 'Linux':
  1165. os.execv(__file__, sys.argv)
  1166. elif platform.system() == 'Windows':
  1167. # When running the script via `python pywin.py` (e.g. Windows), use
  1168. os.execv(sys.executable, ['python'] + sys.argv)
  1169.  
  1170.  
  1171. def argumentparsing():
  1172. """Function that handles how the script handles arugments"""
  1173. par = argparse.ArgumentParser(
  1174. description="Pywin script for Nanonord")
  1175. blobgrp = par.add_argument_group()
  1176. blobgrp.add_argument("-bd", dest='bd', help="Deploy blob to database", action="store_true")
  1177. blobgrp.add_argument("-fp", dest='fp', help="Specify filepath for blob", type=str)
  1178. blobgrp.add_argument("-ho", dest='ho', help="Specify host ip", type=str)
  1179. blobgrp.add_argument("-db", dest='db', help="Specify database", type=str)
  1180. blobgrp.add_argument("-u", dest='u', default="catmin", help="Specify username", type=str)
  1181. blobgrp.add_argument("-pw", dest='pw', default="nano4a", help="Specify password", type=str)
  1182. blobgrp.add_argument("-pn", dest='pn', default="package", help="Specify packagename", type=str)
  1183. blobgrp.add_argument("-r", dest='r', default="norev", help="Specify revision", type=str)
  1184. blobgrp.add_argument(
  1185. "--autoupdateall",
  1186. dest='autoupdateall',
  1187. help="Automatically force update if deploy is sucessfull",
  1188. action="store_true")
  1189. agentgrp = par.add_argument_group()
  1190. agentgrp.add_argument("-a", help="runs the script as an agent", action="store_true")
  1191. agentgrp.add_argument("-adb", dest='agentdb', default="192.168.210.120", help="Specify database where controller is", type=str)
  1192. agentgrp.add_argument("-apdb", dest='appsdb', default="192.168.210.120", help="Specify what database apps should point to", type=str)
  1193.  
  1194. args = par.parse_args()
  1195. return args
  1196.  
  1197.  
  1198. def executer(args):
  1199. if args.a is True and args.agentdb and args.appsdb is not None:
  1200. agentflow(args.agentdb, args.appsdb)
  1201. if args.bd and args.fp and args.ho and args.db and args.u and args.pw and args.pn and args.r is not None:
  1202. dbint = DbInteractor()
  1203. dbint.iblob(args.fp, args.ho, args.u, args.pw, args.pn, args.r, args.autoupdateall)
  1204. elif args.bd and args.ho and args.db and args.u and args.pw and args.pn and args.r is not None:
  1205. wlog("M", "You didn't specify filepath")
  1206. elif args.bd and args.fp and args.db and args.u and args.pw and args.pn and args.r is not None:
  1207. wlog("M", "You didn't specify the host")
  1208. elif args.bd and args.fp and args.ho and args.u and args.pw and args.pn and args.r is not None:
  1209. wlog("M", "You didn't specify the database")
  1210.  
  1211.  
  1212. _registered_exit_funs = set()
  1213. _executed_exit_funs = set()
  1214.  
  1215.  
  1216. def register_exit_fun(fun=None, signals=[signal.SIGTERM], logfun=lambda s: print(s, file=sys.stderr)):
  1217. def stringify_sig(signum):
  1218. if sys.version_info < (3, 5):
  1219. smap = dict([(getattr(signal, x), x) for x in dir(signal)
  1220. if x.startswith('SIG')])
  1221. return smap.get(signum, signum)
  1222. else:
  1223. return signum
  1224.  
  1225. def fun_wrapper():
  1226. if fun not in _executed_exit_funs:
  1227. try:
  1228. fun()
  1229. finally:
  1230. _executed_exit_funs.add(fun)
  1231.  
  1232. def signal_wrapper(signum=None, frame=None):
  1233. if signum is not None:
  1234. if logfun is not None:
  1235. logfun("signal {} received by process with PID {}".format(
  1236. stringify_sig(signum), os.getpid()))
  1237. fun_wrapper()
  1238. # Only return the original signal this process was hit with
  1239. # in case fun returns with no errors, otherwise process will
  1240. # return with sig 1.
  1241. if signum is not None:
  1242. if signum == signal.SIGINT:
  1243. raise KeyboardInterrupt
  1244. # XXX - should we do the same for SIGTERM / SystemExit?
  1245. print(signum)
  1246. sys.exit(signum)
  1247.  
  1248. def register_fun(fun, signals):
  1249. if not callable(fun):
  1250. raise TypeError("{!r} is not callable".format(fun))
  1251. set([fun]) # raise exc if obj is not hash-able
  1252.  
  1253. signals = set(signals)
  1254. for sig in signals:
  1255. # Register function for this signal and pop() the previously
  1256. # registered one (if any). This can either be a callable,
  1257. # SIG_IGN (ignore signal) or SIG_DFL (perform default action
  1258. # for signal).
  1259. old_handler = signal.signal(sig, signal_wrapper)
  1260. if old_handler not in (signal.SIG_DFL, signal.SIG_IGN):
  1261. # ...just for extra safety.
  1262. if not callable(old_handler):
  1263. continue
  1264. # This is needed otherwise we'll get a KeyboardInterrupt
  1265. # strace on interpreter exit, even if the process exited
  1266. # with sig 0.
  1267. if (sig == signal.SIGINT and
  1268. old_handler is signal.default_int_handler):
  1269. continue
  1270. # There was a function which was already registered for this
  1271. # signal. Register it again so it will get executed (after our
  1272. # new fun).
  1273. if old_handler not in _registered_exit_funs:
  1274. atexit.register(old_handler)
  1275. _registered_exit_funs.add(old_handler)
  1276.  
  1277. # This further registration will be executed in case of clean
  1278. # interpreter exit (no signals received).
  1279. if fun not in _registered_exit_funs or not signals:
  1280. atexit.register(fun_wrapper)
  1281. _registered_exit_funs.add(fun)
  1282.  
  1283. # This piece of machinery handles 3 usage cases. register_exit_fun()
  1284. # used as:
  1285. # - a function
  1286. # - a decorator without parentheses
  1287. # - a decorator with parentheses
  1288. if fun is None:
  1289. @functools.wraps
  1290. def outer(fun):
  1291. return register_fun(fun, signals)
  1292.  
  1293. return outer
  1294. else:
  1295. register_fun(fun, signals)
  1296. return fun
  1297.  
  1298.  
  1299. def wlog(messagetype, message):
  1300. messagecaller = inspect.stack()[1][3]
  1301. timevar = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  1302. if messagetype == "M":
  1303. print("[{}][{}]{} - {}".format(timevar, messagetype, messagecaller.center(22), message))
  1304. elif messagetype == "D":
  1305. debugline = format(sys.exc_info()[-1].tb_lineno)
  1306. print("[{}][{}L{}]{} - {}".format(timevar, messagetype, debugline, messagecaller.center(18), message))
  1307.  
  1308.  
  1309. def agentflow(ctrdatabase, appsdatabase):
  1310. # Setting up database and default table
  1311. wlog("AFLOW", "Starting initial database connection & read")
  1312. reader = InitialReader(ctrdatabase, "apphandler")
  1313. InitialReader.connect(reader)
  1314. value = reader.rdefrev()
  1315. wlog("M", "Default startup value: {}".format(value))
  1316.  
  1317. wlog("AFLOW", "Setting up DbSetup connection")
  1318. dbsetup = DbSetup(value)
  1319. dbsetup.dropctr()
  1320. dbsetup.crtprocapps()
  1321. dbsetup.crtproccontroller()
  1322. dbsetup.dbtablesetup()
  1323.  
  1324. # Set base state, that no apps are running
  1325. prochandler = ProcHandler()
  1326. prochandler.stopallapps()
  1327. # Db interaction
  1328. wlog("AFLOW", "Retrieving default blob from value: {}".format(value))
  1329.  
  1330. dbinteractor = DbInteractor()
  1331. wlog("M", "Using this value to read default blob with: {}".format(value))
  1332. dbinteractor.readblob(os.path.expanduser("~/artifacts/"), value, "output.zip")
  1333. # Ziphandler
  1334. wlog("AFLOW", "Default zip section with value: {}".format(value))
  1335. zipper = ZipHandler()
  1336. zipper.unzip(os.path.expanduser("~/artifacts/"), "output.zip", value, "")
  1337. # Db pinger
  1338. wlog("AFLOW", "Starting to ping database")
  1339. pinger = DbCmdChecker(reader)
  1340. pinger.checkdb(2, os.path.expanduser("~/artifacts/"), "archive/build/matlab/app/cmonapp/cmonapp.exe",
  1341. "cmonapp.exe",
  1342. os.path.expanduser("~/artifacts/"), "archive/build/matlab/app/nmrcalibrate/nmrcalibrate.exe",
  1343. "nmrcalibrate.exe",
  1344. os.path.expanduser("~/artifacts/"), "archive/build/matlab/app/nmrcalculatorapp/nmrcalculatorapp.exe",
  1345. "nmrcalculatorapp.exe",
  1346. os.path.expanduser("~/artifacts/"), "archive/build/matlab/app/nmrmeasureapp/nmrmeasureapp.exe",
  1347. "nmrmeasureapp.exe",
  1348. appsdatabase)
  1349.  
  1350. cursor.close()
  1351. db.close()
  1352.  
  1353. register_exit_fun(connectionkiller())
  1354. register_exit_fun(restartifclosed())
  1355.  
  1356.  
  1357. def main():
  1358. args = argumentparsing()
  1359. executer(args)
  1360.  
  1361.  
  1362. if __name__ == "__main__":
  1363. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement