Advertisement
ShintaroGr

Untitled

Feb 19th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. from time import sleep
  2. import boto3
  3. import urllib.parse
  4. import glob
  5. import os
  6.  
  7.  
  8.  
  9. def wait_for_internet_connection():
  10. i = 0
  11. while True:
  12. if i > 10:
  13. return 1
  14. try:
  15. print('Waiting for internet connection')
  16. error = os.system('curl -s -X GET https://www.google.com --max-time 5 > /dev/null')
  17. sleep(1)
  18. if error == 0:
  19. return 0
  20. else:
  21. pass
  22. except:
  23. pass
  24. i += 1
  25.  
  26.  
  27. def wait_for_local_connection():
  28. i = 0
  29. while True:
  30. if i > 10:
  31. return 1
  32. try:
  33. print('Waiting for local connection')
  34. error = os.system('curl -s 192.168.12.2:8081 --max-time 1 > /dev/null')
  35. sleep(1)
  36. if error == 0:
  37. return 0
  38. else:
  39. pass
  40. except:
  41. pass
  42. i += 1
  43.  
  44.  
  45. def run_app():
  46. print('Launching program')
  47. os.chdir('./app/')
  48. os.system("sync")
  49. os.system("python3 PyBenalu_MainThread.py")
  50.  
  51.  
  52. def get_id():
  53. try:
  54. with open('./system/id.txt', 'r') as file:
  55. return file.readline()
  56. except:
  57. os._exit(0)
  58.  
  59.  
  60. def get_old_version():
  61. files = glob.glob("./version/*")
  62. files.sort(key=os.path.getmtime)
  63. files.reverse()
  64. index = files.index(os.readlink('./app'))
  65. files = files[index + 1:]
  66. if len(files) > 0:
  67. os.system('ln -sfn "' + files[0] + '" ./app')
  68. os.system("sync")
  69.  
  70.  
  71. def remove_old_version():
  72. files = glob.glob("./version/*")
  73. files.sort(key=os.path.getmtime)
  74. files.reverse()
  75. if len(files) >= 10:
  76. for file in files[10:]:
  77. os.system('rm -rf ' + file)
  78. os.system("sync")
  79.  
  80.  
  81. def get_new_version():
  82. os.chdir("../")
  83. remove_old_version()
  84. try:
  85. head = s3.head_object(Bucket='easy-security', Key='tippers/' + get_id() + '/version')
  86. path = urllib.parse.urlparse(head['ResponseMetadata']['HTTPHeaders']['x-amz-website-redirect-location']).path
  87. try:
  88. with open('./last-modified.txt', 'r') as file:
  89. last_modified = file.readline()
  90. if not os.path.exists('./app'):
  91. raise Exception()
  92. except:
  93. last_modified = ''
  94. head = s3.head_object(Bucket='easy-security', Key=path[1:])
  95. if last_modified != head['ResponseMetadata']['HTTPHeaders']['last-modified']:
  96. with open('last-modified.txt', 'w+') as file: # Use file to refer to the file object
  97. file.write(head['ResponseMetadata']['HTTPHeaders']['last-modified'])
  98. s3.download_file('easy-security', path[1:], './app.zip')
  99. os.system(
  100. 'unzip -o app.zip -d "./version/' + head['ResponseMetadata']['HTTPHeaders']['last-modified'] + '"')
  101. os.system('ln -sfn "./version/' + head['ResponseMetadata']['HTTPHeaders']['last-modified'] + '" ./app')
  102. os.system('rm -rf app.zip')
  103. with open('fw-version.txt', 'w+') as file: # Use file to refer to the file object
  104. file.write(path[1:])
  105. if wait_for_local_connection() != 0:
  106. os.system('sshpass -p 123456789 scp ./fw-version.txt root@192.168.12.2:./fw-version.txt')
  107. os.system('sshpass -p 123456789 scp -r ./app/camera root@192.168.12.2:./camera')
  108. os.system('sshpass -p 123456789 ssh root@192.168.12.2 "cd system && python3 ./update.py"')
  109. os.system('mv ./app/Default.ini ./config')
  110. pull = 0
  111. except:
  112. pull = 1
  113.  
  114. if pull == 0:
  115. run_app()
  116. else:
  117. get_old_version()
  118. run_app()
  119.  
  120.  
  121. if wait_for_internet_connection() == 0:
  122. get_new_version()
  123. else:
  124. run_app()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement