Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- import boto3
- import urllib.parse
- import glob
- import os
- def wait_for_internet_connection():
- i = 0
- while True:
- if i > 10:
- return 1
- try:
- print('Waiting for internet connection')
- error = os.system('curl -s -X GET https://www.google.com --max-time 5 > /dev/null')
- sleep(1)
- if error == 0:
- return 0
- else:
- pass
- except:
- pass
- i += 1
- def wait_for_local_connection():
- i = 0
- while True:
- if i > 10:
- return 1
- try:
- print('Waiting for local connection')
- error = os.system('curl -s 192.168.12.2:8081 --max-time 1 > /dev/null')
- sleep(1)
- if error == 0:
- return 0
- else:
- pass
- except:
- pass
- i += 1
- def run_app():
- print('Launching program')
- os.chdir('./app/')
- os.system("sync")
- os.system("python3 PyBenalu_MainThread.py")
- def get_id():
- try:
- with open('./system/id.txt', 'r') as file:
- return file.readline()
- except:
- os._exit(0)
- def get_old_version():
- files = glob.glob("./version/*")
- files.sort(key=os.path.getmtime)
- files.reverse()
- index = files.index(os.readlink('./app'))
- files = files[index + 1:]
- if len(files) > 0:
- os.system('ln -sfn "' + files[0] + '" ./app')
- os.system("sync")
- def remove_old_version():
- files = glob.glob("./version/*")
- files.sort(key=os.path.getmtime)
- files.reverse()
- if len(files) >= 10:
- for file in files[10:]:
- os.system('rm -rf ' + file)
- os.system("sync")
- def get_new_version():
- os.chdir("../")
- remove_old_version()
- try:
- head = s3.head_object(Bucket='easy-security', Key='tippers/' + get_id() + '/version')
- path = urllib.parse.urlparse(head['ResponseMetadata']['HTTPHeaders']['x-amz-website-redirect-location']).path
- try:
- with open('./last-modified.txt', 'r') as file:
- last_modified = file.readline()
- if not os.path.exists('./app'):
- raise Exception()
- except:
- last_modified = ''
- head = s3.head_object(Bucket='easy-security', Key=path[1:])
- if last_modified != head['ResponseMetadata']['HTTPHeaders']['last-modified']:
- with open('last-modified.txt', 'w+') as file: # Use file to refer to the file object
- file.write(head['ResponseMetadata']['HTTPHeaders']['last-modified'])
- s3.download_file('easy-security', path[1:], './app.zip')
- os.system(
- 'unzip -o app.zip -d "./version/' + head['ResponseMetadata']['HTTPHeaders']['last-modified'] + '"')
- os.system('ln -sfn "./version/' + head['ResponseMetadata']['HTTPHeaders']['last-modified'] + '" ./app')
- os.system('rm -rf app.zip')
- with open('fw-version.txt', 'w+') as file: # Use file to refer to the file object
- file.write(path[1:])
- if wait_for_local_connection() != 0:
- os.system('sshpass -p 123456789 scp ./fw-version.txt root@192.168.12.2:./fw-version.txt')
- os.system('sshpass -p 123456789 scp -r ./app/camera root@192.168.12.2:./camera')
- os.system('sshpass -p 123456789 ssh root@192.168.12.2 "cd system && python3 ./update.py"')
- os.system('mv ./app/Default.ini ./config')
- pull = 0
- except:
- pull = 1
- if pull == 0:
- run_app()
- else:
- get_old_version()
- run_app()
- if wait_for_internet_connection() == 0:
- get_new_version()
- else:
- run_app()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement