Innovenergy_trunk/firmware/Venus_Release/update_Venus.py

104 lines
3.4 KiB
Python
Raw Normal View History

2024-07-01 09:49:57 +00:00
import subprocess
import sys
import asyncio
import os
async def run_remote_command(remote_host, command):
ssh_command = ['ssh', f'root@{remote_host}', command]
process = await asyncio.create_subprocess_exec(
*ssh_command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
stdout_decoded = stdout.decode()
stderr_decoded = stderr.decode()
if process.returncode == 0:
return stdout_decoded + stderr_decoded
else:
return f"Failed to run the command: {command}, error: {stderr_decoded}"
async def check_GX_type(remote_host):
command = "cat /etc/venus/machine"
return await run_remote_command(remote_host, command)
async def run_rclocal(remote_host):
command = "chmod +x /data/rc.local && /data/rc.local"
return await run_remote_command(remote_host, command)
async def upload_files(remote_host):
file_location_mappings = {
"rc.local": "/data/",
"dbus-fzsonick-48tl": "/data/",
"pika-0.13.1": "/data/innovenergy/"
}
venus_release_files_folder = os.path.join(os.getcwd(), "VenusReleaseFiles")
if not os.path.exists(venus_release_files_folder):
return "VenusReleaseFiles folder does not exist."
try:
tasks = []
for file_name, remote_location in file_location_mappings.items():
file_path = os.path.join(venus_release_files_folder, file_name)
if not os.path.exists(file_path):
raise FileNotFoundError(f"File {file_name} not found in {venus_release_files_folder}.")
command = [
"rsync",
"-r",
file_path,
f"root@{remote_host}:{remote_location}"
]
tasks.append(command)
# Execute rsync commands asynchronously
for task in tasks:
subprocess.run(task, check=True)
return "All files uploaded successfully."
except FileNotFoundError as e:
return str(e)
except subprocess.CalledProcessError as e:
return f"Error occurred while uploading files: {e}"
except Exception as e:
return f"An error occurred while uploading files: {str(e)}"
async def import_pika(remote_host):
change_dir_command = "cd /data/innovenergy/pika-0.13.1/"
install_command = "python2 setup.py install --user"
command = f"{change_dir_command} && {install_command}"
return await run_remote_command(remote_host, command)
async def main(remote_host):
##### 1. check whether it's Venus ######
gx_type = await check_GX_type(remote_host)
if gx_type == "beaglebone\n":
##### 2. upload VPN and battery files ######
print("Upload pika and battery files!")
if(await upload_files(remote_host)!="All files uploaded successfully."):
sys.exit("Failed to upload files!")
else:
print(await upload_files(remote_host))
#### 3. import pika ####
print("Import pika!")
print(await import_pika(remote_host))
##### 4. run rc.local ######
print("Run rc.local to set password, timezone and cp battery folder!")
print(await run_rclocal(remote_host))
#### restart battery service??????#####
else:
sys.exit("It's not Venus GX!")
if __name__ == "__main__":
remote_host = sys.argv[1]
asyncio.run(main(remote_host))