Skip to content

Python Management Commands

Siqi Tian edited this page Feb 5, 2017 · 1 revision

src/management/commands

Contains Django management command scripts.

backup.py

  • Backs up static files, MySQL, Apache, and local config/ files into backup/ folder.
  • Uses subprocess.check_call() for mysqldump, tarfile for creating TGZ files.
  • Due to lack of permissions of www-data user to /etc/apache2/, Apache settings are archived by a cron job under ubuntu.
  • The cron job is triggered weekly. Summary is sent to admin after job finish either via email.

bday.py

  • Checks if anyone's birthday is today and sends out wishes in Slack. Alumni members are excluded.
  • The cron job is triggered daily.

cache.py

  • Caches analytics data into cache/. Accepts an interval argument for category, choose from: 3, 15, or 30.
  • Results are saved as *.json and *.pickle files as a memory dump of variable. HTTP errors (500, 502, 503, connection time-out) are reported to Slack but with exemption.
  • The cron job is triggered every 3, 15, 30 minutes.

duty.py

  • Checks if any duty reminders should be sent out today. Accepts an interval argument for category, choose from: week, month, or quarter.
  • Dropbox and GitHub payment notice is sent on October annually.
  • Birthday reminder includes people's birthdays for the upcoming 60 days.
  • In DEBUG mode, all Slack messages will be sent to admin instead of original receiver.
  • The weekly cron job is triggered daily to capture the 2 reminder dates. The monthly and quarterly cron jobs are also triggered daily, but only executed fully on first week of each month and the set date. Because checking time inside python is easier than coding in bash.

gdrive.py

  • Uploads most recent backup onto daslab Google Drive and cleans up obsolete backup files.
  • Uses subprocess.check_call() for drive. Files are renamed with date as prefix.
  • Old files are determined by their last modified date and removed (default 45 days).
  • The cron job is triggered weekly. Summary is sent to admin after job finish either via email or Slack.

meeting.py

  • Sets up Flash Slides and sends out Group Meeting Reminder.
  • Only on weeks that are not N/A will Flash Slides be created. Instead of creating a Google Presentation, it actually copies over an existing blank one specified by TEMPLATE_PRESENTATION_ID in config/oauth.conf, because it's easier this way to have the aspect ratio correct.
  • The Group Meeting Reminder describes the meeting this week, next week, as well as headers and footers. Presenters' names will be matched by find_slack_id() in src/console.py, and addressed in the message.
  • Admin will be notified for adding MySQL entries for Eterna Special, Journal Club, and Rotation Student. Presenter of next week will be notified for preparation.
  • In DEBUG mode, all Slack messages will be sent to admin instead of original receiver.
  • The cron job is triggered daily, but only executed fully on day before meeting. Because checking time inside python is easier than coding in bash.

report.py

  • Sends cron error report to admin if not IS_SLACK. Also compresses cron log and cleans up old Slack Messages.
  • It zips cache/log_cron.log, and removes Slack Messages that are older than 15 days in MySQL database.
  • The cron job is triggered weekly. Summary of cache/log_alert_admin.log is sent to admin after job finish either via email or Slack.

restore.py

  • Restores static files, MySQL, Apache, and local config/ files from local backup/ files.
  • Uses subprocess.check_call() for mysql, tarfile and shutil for unpacking TGZ files.

versions.py

  • Collects system installed package versions into cache/stat_sys.json and cache/stat_ver.json. Accepts a flag argument for overriding IS_VERSIONS in config/bot.conf, chooses from: 0 or 1.
  • Retrieves SSL Certificate expiration date by calling dash_ssl() from src/dash.py.
  • Uses subprocess.Popen() for bash commands. Some commands need to write to cache/temp.txt temporarily for output handling. Some values, e.g. WebAuth, mod_wsgi, and wallet are implemented for production only. Commands for memory readings are dependent on DEBUG as well.
  • Renders cache/sys_ver.txt (only in production) based on information in cache/stat_ver.json. It is displayed in bash upon login.
  • Calls get_backup_stat() from src/console.py too.

Clone this wiki locally