Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame^] | 1 | """ Script to control the deployment of LNT to Google Cloud Instance. |
| 2 | |
| 3 | |
| 4 | """ |
| 5 | |
| 6 | import os |
| 7 | from os.path import expanduser |
| 8 | |
| 9 | from fabric.api import env, cd, task, run, put |
| 10 | from fabric.api import sudo |
| 11 | |
| 12 | here = os.path.dirname(os.path.realpath(__file__)) |
| 13 | |
| 14 | home = expanduser("~") |
| 15 | |
| 16 | env.use_ssh_config = True |
| 17 | |
| 18 | # The remote LNT venv location. |
| 19 | LNT_VENV = "/srv/lnt/sandbox" |
| 20 | |
| 21 | # Prefix a command with this to get to the venv. |
| 22 | IN_VENV = "source " + LNT_VENV + "/bin/activate; " |
| 23 | |
| 24 | |
| 25 | def in_venv(command): |
| 26 | """Run the command inside the LNT venv.""" |
| 27 | return IN_VENV + command |
| 28 | |
| 29 | |
| 30 | # The remote location of the LNT repo checkout. |
| 31 | LNT_PATH = "/srv/lnt/src/lnt/" |
| 32 | LNT_CONF_PATH = "/srv/lnt/install" |
| 33 | |
| 34 | |
| 35 | @task |
| 36 | def update(): |
| 37 | """Update the svn repo, then reinstall LNT.""" |
| 38 | with cd(LNT_PATH): |
| 39 | with cd(LNT_PATH + "/docs/"): |
| 40 | sudo('rm -rf _build') |
| 41 | with cd(LNT_PATH + "/lnt/server/ui/static"): |
| 42 | sudo('rm -rf docs') |
| 43 | sudo('git checkout docs') |
| 44 | |
| 45 | sudo("git pull --rebase") |
| 46 | run("git log -1 --pretty=%B") |
| 47 | with cd(LNT_PATH + "/docs/"): |
| 48 | sudo(in_venv("make")) |
| 49 | sudo(IN_VENV + "python setup.py install --server") |
| 50 | |
| 51 | put(here + "/blacklist", "/tmp/blacklist") |
| 52 | sudo("mv /tmp/blacklist /srv/lnt/install/blacklist") |
| 53 | put(here + "/kill_zombies.py", "/tmp/kill_zombies.py") |
| 54 | sudo("mv /tmp/kill_zombies.py /etc/cron.hourly/kill_zombies") |
| 55 | sudo("chmod +x /etc/cron.hourly/kill_zombies") |
| 56 | |
| 57 | service_restart() |
| 58 | |
| 59 | |
| 60 | @task |
| 61 | def log(): |
| 62 | sudo('tail -n 500 /srv/lnt/install/lnt.log') |
| 63 | |
| 64 | |
| 65 | @task |
| 66 | def ps(): |
| 67 | sudo('ps auxxxf | grep gunicorn') |
| 68 | |
| 69 | |
| 70 | @task |
| 71 | def df(): |
| 72 | sudo('df -h') |
| 73 | |
| 74 | |
| 75 | @task |
| 76 | def kill_zombies(): |
| 77 | import re |
| 78 | out = sudo("ps auxxxf") |
| 79 | stranded = re.compile(r"^lnt\s+(?P<pid>\d+).*00\sgunicorn:\swork") |
| 80 | pids = [] |
| 81 | for line in out.split('\n'): |
| 82 | m = stranded.match(line) |
| 83 | if m: |
| 84 | pid = m.groupdict()['pid'] |
| 85 | pids.append(pid) |
| 86 | else: |
| 87 | print ">", line |
| 88 | for pid in pids: |
| 89 | sudo("kill -9 {}".format(pid)) |
| 90 | |
| 91 | |
| 92 | @task |
| 93 | def service_restart(): |
| 94 | """Restarting LNT service with Launchctl""" |
| 95 | sudo("systemctl restart gunicorn") |