You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fixer.py 3.3KB

  1. import os
  2. from subprocess import check_output, call
  3. if not os.path.exists("/opt/sixfab/.agent-fixes"):
  4. os.system("sudo touch /opt/sixfab/.agent-fixes")
  5. fixes_list = check_output(["sudo", "cat", "/opt/sixfab/.agent-fixes"]).decode().split("\n")
  6. def execute_fix(name, command):
  7. if name in fixes_list:
  8. return
  9. os.system("echo {} | sudo tee -a /opt/sixfab/.agent-fixes".format(name))
  10. call(command, shell=True, executable='/bin/bash')
  11. # change old service file with new one
  12. execute_fix("3004-service-file", r'printf "[Unit]\nDescription=Sixfab PMS Agent\nAfter=network.target network-online.target\nRequires=network-online.target\n\n[Service]\nExecStart=/usr/bin/python3 -u agent.py\nWorkingDirectory=/opt/sixfab/pms/agent\nStandardOutput=inherit\nStandardError=inherit\nRestart=always\nRestartSec=3\nUser=pi\n\n[Install]\nWantedBy=multi-user.target" | sudo tee /etc/systemd/system/pms_agent.service && sudo systemctl daemon-reload && sudo systemctl restart pms_agent')
  13. execute_fix("10.07.2020-distribution-service", r"""pip3 uninstall -y sixfab-power-python-api; sudo pip3 uninstall -y sixfab-power-python-api; pip3 install -U sixfab-power-python-api;API_LOCATION=$(cd /opt/sixfab/pms/api&&sudo git remote show origin);if [[ $API_LOCATION == *"sixfab.com/sixfab-power/api"* ]];then sudo rm -r /opt/sixfab/pms/api;sudo git clone https://github.com/sixfab/power_distribution-service.git /opt/sixfab/pms/api;pip3 install -r /opt/sixfab/pms/api/requirements.txt;sudo touch /etc/systemd/system/sixfab_power_api.service;echo "[Unit]
  14. Description=Sixfab UPS HAT Distributed API
  15. [Service]
  16. User=pi
  17. ExecStart=/usr/bin/python3 /opt/sixfab/pms/api/run_server.py
  18. [Install]
  19. WantedBy=multi-user.target"|sudo tee /etc/systemd/system/sixfab_power_api.service;sudo systemctl daemon-reload;sudo systemctl enable sixfab_power_api;sudo systemctl start sixfab_power_api;fi""")
  20. execute_fix("23.10.2020-update-service-names", r'if [ -f "/etc/systemd/system/sixfab_power_api.service" ];then sudo systemctl disable sixfab_power_api;sudo systemctl stop sixfab_power_api;sudo mv /etc/systemd/system/sixfab_power_api.service /etc/systemd/system/power_request.service;sudo systemctl daemon-reload;sudo systemctl enable power_request;sudo systemctl start power_request;fi;if [ -f "/etc/systemd/system/pms_agent.service" ];then sudo systemctl disable pms_agent;sudo mv /etc/systemd/system/pms_agent.service /etc/systemd/system/power_agent.service;sudo systemctl daemon-reload;sudo systemctl enable power_agent;sudo systemctl start power_agent;sudo systemctl stop pms_agent;fi')
  21. execute_fix("23.10.2020-update-service-names", r'if [ -f "/etc/systemd/system/sixfab_power_api.service" ];then sudo systemctl disable sixfab_power_api;sudo systemctl stop sixfab_power_api;sudo mv /etc/systemd/system/sixfab_power_api.service /etc/systemd/system/power_request.service;sudo systemctl daemon-reload;sudo systemctl enable power_request;sudo systemctl start power_request;fi;if [ -f "/etc/systemd/system/pms_agent.service" ];then sudo systemctl disable pms_agent;sudo mv /etc/systemd/system/pms_agent.service /etc/systemd/system/power_agent.service;sudo systemctl daemon-reload;sudo systemctl enable power_agent;sudo systemctl start power_agent;sudo systemctl stop pms_agent;fi')
  22. execute_fix("19.12.2020-restart-request-service", r'sudo systemctl restart power_request')