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 | |
Chris Matthews | 5574aba | 2017-09-01 21:02:09 +0000 | [diff] [blame] | 9 | import re |
Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame] | 10 | from fabric.api import env, cd, task, run, put |
| 11 | from fabric.api import sudo |
Chris Matthews | 4d07f3d | 2017-08-17 22:42:43 +0000 | [diff] [blame] | 12 | from fabric.context_managers import hide |
| 13 | from fabric.operations import get, local |
Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame] | 14 | |
| 15 | here = os.path.dirname(os.path.realpath(__file__)) |
| 16 | |
| 17 | home = expanduser("~") |
| 18 | |
| 19 | env.use_ssh_config = True |
Chris Matthews | 2b4a5c0 | 2017-08-17 22:27:49 +0000 | [diff] [blame] | 20 | env.output_prefix = False |
Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame] | 21 | |
| 22 | # The remote LNT venv location. |
| 23 | LNT_VENV = "/srv/lnt/sandbox" |
| 24 | |
| 25 | # Prefix a command with this to get to the venv. |
| 26 | IN_VENV = "source " + LNT_VENV + "/bin/activate; " |
| 27 | |
| 28 | |
| 29 | def in_venv(command): |
| 30 | """Run the command inside the LNT venv.""" |
| 31 | return IN_VENV + command |
| 32 | |
| 33 | |
| 34 | # The remote location of the LNT repo checkout. |
| 35 | LNT_PATH = "/srv/lnt/src/lnt/" |
| 36 | LNT_CONF_PATH = "/srv/lnt/install" |
| 37 | |
| 38 | |
| 39 | @task |
| 40 | def update(): |
| 41 | """Update the svn repo, then reinstall LNT.""" |
| 42 | with cd(LNT_PATH): |
| 43 | with cd(LNT_PATH + "/docs/"): |
| 44 | sudo('rm -rf _build') |
| 45 | with cd(LNT_PATH + "/lnt/server/ui/static"): |
| 46 | sudo('rm -rf docs') |
| 47 | sudo('git checkout docs') |
| 48 | |
| 49 | sudo("git pull --rebase") |
| 50 | run("git log -1 --pretty=%B") |
| 51 | with cd(LNT_PATH + "/docs/"): |
| 52 | sudo(in_venv("make")) |
| 53 | sudo(IN_VENV + "python setup.py install --server") |
| 54 | |
| 55 | put(here + "/blacklist", "/tmp/blacklist") |
| 56 | sudo("mv /tmp/blacklist /srv/lnt/install/blacklist") |
| 57 | put(here + "/kill_zombies.py", "/tmp/kill_zombies.py") |
| 58 | sudo("mv /tmp/kill_zombies.py /etc/cron.hourly/kill_zombies") |
| 59 | sudo("chmod +x /etc/cron.hourly/kill_zombies") |
Chris Matthews | 5574aba | 2017-09-01 21:02:09 +0000 | [diff] [blame] | 60 | rotate_log() |
Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame] | 61 | service_restart() |
| 62 | |
| 63 | |
| 64 | @task |
| 65 | def log(): |
Chris Matthews | 4d07f3d | 2017-08-17 22:42:43 +0000 | [diff] [blame] | 66 | get('/srv/lnt/install/lnt.log', 'lnt.log') |
| 67 | local('cat lnt.log') |
Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | @task |
Chris Matthews | 4d07f3d | 2017-08-17 22:42:43 +0000 | [diff] [blame] | 71 | def new_log(): |
| 72 | """Show only lines since the last time the log was echoed.""" |
| 73 | if os.path.exists("lnt.log"): |
| 74 | lines = {line for line in open("lnt.log", 'r').readlines()} |
| 75 | else: |
Chris Matthews | 5666e80 | 2017-08-17 22:46:18 +0000 | [diff] [blame] | 76 | lines = set() |
Chris Matthews | 4d07f3d | 2017-08-17 22:42:43 +0000 | [diff] [blame] | 77 | with hide('warnings'): |
| 78 | get('/srv/lnt/install/lnt.log', 'lnt.log', ) |
| 79 | new_lines = {line for line in open("lnt.log", 'r').readlines()} |
| 80 | for l in new_lines - lines: |
| 81 | print ' '.join(l.split()[2:]), |
| 82 | |
Chris Matthews | 5574aba | 2017-09-01 21:02:09 +0000 | [diff] [blame] | 83 | |
Chris Matthews | 4d07f3d | 2017-08-17 22:42:43 +0000 | [diff] [blame] | 84 | @task |
Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame] | 85 | def ps(): |
| 86 | sudo('ps auxxxf | grep gunicorn') |
| 87 | |
| 88 | |
| 89 | @task |
Chris Matthews | 5574aba | 2017-09-01 21:02:09 +0000 | [diff] [blame] | 90 | def rotate_log(): |
| 91 | """Rotate the LNT log.""" |
| 92 | sudo('rm -rf /srv/lnt/install/gunicorn.error.log') |
| 93 | out = sudo('ps auxxxf | grep "gunicorn: master"') |
| 94 | pid = re.search(r'lnt\s+(?P<pid>\d+)\s+', out).groupdict()['pid'] |
| 95 | print pid |
| 96 | sudo('kill -USR1 ' + pid) |
| 97 | |
| 98 | |
| 99 | @task |
Chris Matthews | 4d39f84 | 2017-08-17 20:26:33 +0000 | [diff] [blame] | 100 | def df(): |
| 101 | sudo('df -h') |
| 102 | |
| 103 | |
| 104 | @task |
| 105 | def kill_zombies(): |
| 106 | import re |
| 107 | out = sudo("ps auxxxf") |
| 108 | stranded = re.compile(r"^lnt\s+(?P<pid>\d+).*00\sgunicorn:\swork") |
| 109 | pids = [] |
| 110 | for line in out.split('\n'): |
| 111 | m = stranded.match(line) |
| 112 | if m: |
| 113 | pid = m.groupdict()['pid'] |
| 114 | pids.append(pid) |
| 115 | else: |
| 116 | print ">", line |
| 117 | for pid in pids: |
| 118 | sudo("kill -9 {}".format(pid)) |
| 119 | |
| 120 | |
| 121 | @task |
| 122 | def service_restart(): |
| 123 | """Restarting LNT service with Launchctl""" |
| 124 | sudo("systemctl restart gunicorn") |