blob: 63dddb5348d8d5b10ebcfd0b62df89c55a554747 [file] [log] [blame]
Chris Matthews4d39f842017-08-17 20:26:33 +00001""" Script to control the deployment of LNT to Google Cloud Instance.
2
3
4"""
5
6import os
7from os.path import expanduser
8
Chris Matthews5574aba2017-09-01 21:02:09 +00009import re
Chris Matthews4d39f842017-08-17 20:26:33 +000010from fabric.api import env, cd, task, run, put
11from fabric.api import sudo
Chris Matthews4d07f3d2017-08-17 22:42:43 +000012from fabric.context_managers import hide
13from fabric.operations import get, local
Chris Matthews4d39f842017-08-17 20:26:33 +000014
15here = os.path.dirname(os.path.realpath(__file__))
16
17home = expanduser("~")
18
19env.use_ssh_config = True
Chris Matthews2b4a5c02017-08-17 22:27:49 +000020env.output_prefix = False
Chris Matthews4d39f842017-08-17 20:26:33 +000021
22# The remote LNT venv location.
23LNT_VENV = "/srv/lnt/sandbox"
24
25# Prefix a command with this to get to the venv.
26IN_VENV = "source " + LNT_VENV + "/bin/activate; "
27
28
29def 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.
35LNT_PATH = "/srv/lnt/src/lnt/"
36LNT_CONF_PATH = "/srv/lnt/install"
37
38
39@task
40def 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 Matthews5574aba2017-09-01 21:02:09 +000060 rotate_log()
Chris Matthews4d39f842017-08-17 20:26:33 +000061 service_restart()
62
63
64@task
65def log():
Chris Matthews4d07f3d2017-08-17 22:42:43 +000066 get('/srv/lnt/install/lnt.log', 'lnt.log')
67 local('cat lnt.log')
Chris Matthews4d39f842017-08-17 20:26:33 +000068
69
70@task
Chris Matthews4d07f3d2017-08-17 22:42:43 +000071def 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 Matthews5666e802017-08-17 22:46:18 +000076 lines = set()
Chris Matthews4d07f3d2017-08-17 22:42:43 +000077 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 Matthews5574aba2017-09-01 21:02:09 +000083
Chris Matthews4d07f3d2017-08-17 22:42:43 +000084@task
Chris Matthews4d39f842017-08-17 20:26:33 +000085def ps():
86 sudo('ps auxxxf | grep gunicorn')
87
88
89@task
Chris Matthews5574aba2017-09-01 21:02:09 +000090def 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 Matthews4d39f842017-08-17 20:26:33 +0000100def df():
101 sudo('df -h')
102
103
104@task
105def 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
122def service_restart():
123 """Restarting LNT service with Launchctl"""
124 sudo("systemctl restart gunicorn")