This repository was archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathvm.py
155 lines (113 loc) · 3.78 KB
/
vm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from fabric.api import env, execute, hide, hosts, parallel, run, sudo, task
from fabric.utils import error
import re
import nagios
@task
def uptime():
"""Show uptime and load"""
run('uptime')
@task
def free():
"""Show memory stats"""
run('free')
@task
def top_mem_proc():
"""Show top processes by memory usage"""
run('ps aux |sort -rk4 |head')
@task
def disk():
"""Show disk usage"""
run('df -kh')
@task
def os_version():
"""Show operating system"""
run('facter lsbdistcodename lsbdistdescription operatingsystem operatingsystemrelease')
@task
def deprecated_library(name):
"""
Find processes that are using a deprecated library and need restarting
For example:
- Processes using a non-upgraded version of libssl:
vm.deprecated_library:libssl
- Processes that are using any deleted/upgraded library:
vm.deprecated_library:/lib
"""
sudo("lsof -d DEL | awk '$8 ~ /{0}/'".format(re.escape(name)))
@task
def stopped_jobs():
"""Find stopped govuk application jobs"""
with hide('running'):
run('grep -l govuk_spinup /etc/init/*.conf | xargs -n1 basename | while read line; do sudo status "${line%%.conf}"; done | grep stop || :')
@task
def bodge_unicorn(name):
"""
Manually kill off (and restart) unicorn processes by name
e.g. vm.bodge_unicorn:content-tagger
Yes. This is a bodge. Sorry.
"""
pid = run("ps auxwww | grep '/%s/' | grep -F 'unicorn master' | grep -v grep | awk '{ print $2 }' | xargs" % name)
if pid:
sudo("kill -9 %s" % pid)
sudo("start '{0}' || restart '{0}'".format(name))
@task
def reload_unicorn(name):
error("task deprecated by 'app.reload'")
def reboot_required():
"""
Check whether a reboot is required
"""
result = run("/usr/local/bin/check_reboot_required 30 0", warn_only=True)
return (not result.succeeded)
@task
def reboot():
"""Schedule a host for downtime in nagios and reboot (if required)
Usage:
fab production -H frontend-1.frontend.production vm.reboot
"""
if reboot_required():
# we need to ensure we only execute this task on the current
# host we're operating on, not every host in env.hosts
execute(force_reboot, hosts=[env['host_string']])
@task
def force_reboot():
"""Schedule a host for downtime in nagios and force reboot (even if not required)"""
execute(nagios.schedule_downtime, env['host_string'])
run("sudo shutdown -r now")
@task
def poweroff():
"""Schedule a host for downtime in nagios and shutdown the VM
Usage:
fab production -H frontend-1.frontend.production vm.poweroff
"""
execute(nagios.schedule_downtime, env['host_string'])
run("sudo poweroff")
@task
@hosts('puppetmaster-1.management')
def host_key(hostname):
"""
Check the SSH host key of a machine. This task runs on the Puppetmaster because
it's the only machine that knows about all host keys.
Usage:
fab production vm.host_key:backend-1.backend
"""
with hide('running', 'stdout'):
ssh_key = run("grep {0} /etc/ssh/ssh_known_hosts | head -1".format(hostname))
if ssh_key == '':
print 'Machine {0} not found in ssh_known_hosts file'.format(hostname)
else:
with hide('running'):
run("ssh-keygen -l -f /dev/stdin <<< '{0}'".format(ssh_key))
@task
@parallel(pool_size=5)
def connected_users():
"""
Output a list of users who are logged in to a machine
"""
with hide('running', 'stdout'):
username = run('whoami')
users = run('who | grep -v {0} || true'.format(username))
if users:
print 'There are users connected to {0}:'.format(env.host_string)
for user in users.split("\n"):
print ' - {0}'.format(user.split(' ')[0])
print "\n"