upgrade.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/python
  2. import ConfigParser
  3. import argparse
  4. import requests
  5. from urlparse import urlparse
  6. import sys
  7. CONFIG_PATH='/etc/airtime/airtime.conf'
  8. GENERAL_CONFIG_SECTION = "general"
  9. def read_config_file(config_path):
  10. """Parse the application's config file located at config_path."""
  11. config = ConfigParser.SafeConfigParser()
  12. try:
  13. config.readfp(open(config_path))
  14. except IOError as e:
  15. print "Failed to open config file at " + config_path + ": " + e.strerror
  16. exit(-1)
  17. except Exception:
  18. print e.strerror
  19. exit(-1)
  20. return config
  21. if __name__ == '__main__':
  22. config = read_config_file(CONFIG_PATH)
  23. api_key = config.get(GENERAL_CONFIG_SECTION, 'api_key')
  24. base_url = config.get(GENERAL_CONFIG_SECTION, 'base_url')
  25. base_dir = config.get(GENERAL_CONFIG_SECTION, 'base_dir')
  26. action = "upgrade"
  27. airtime_url = ""
  28. parser = argparse.ArgumentParser()
  29. parser.add_argument('--downgrade', help='Downgrade the station', action="store_true")
  30. parser.add_argument('station_url', help='station URL', nargs='?', default='')
  31. args = parser.parse_args()
  32. if args.downgrade:
  33. action = "downgrade"
  34. if airtime_url == "":
  35. airtime_url = "http://%s%s" % (base_url, base_dir)
  36. # Add http:// if you were lazy and didn't pass a scheme to this script
  37. url = urlparse(airtime_url)
  38. if not url.scheme:
  39. airtime_url = "http://%s" % airtime_url
  40. print "Requesting %s..." % action
  41. r = requests.get("%s/%s" % (airtime_url, action), auth=(api_key, ''))
  42. print r.text
  43. r.raise_for_status()