-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabfile.py
More file actions
102 lines (87 loc) · 2.88 KB
/
fabfile.py
File metadata and controls
102 lines (87 loc) · 2.88 KB
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
#!/usr/bin/env python
# coding: utf-8
import MySQLdb
import ConfigParser
from fabric.api import run, env, execute, task
from fabric.state import output
from fabric.colors import red, white
from fabric.context_managers import show, warn_only
from os.path import expanduser
# suppress output
output.running = False
output.stdout = False
output.status = False
output.warnings = False
# import ssh aliases
env.use_ssh_config = True
env.skip_bad_hosts = True
# tasks here
@task
def get_hosts():
"""Get list of servers with upgrades available"""
print(', '.join(_get_hosts()))
@task(alias='du')
def do_upgrades():
"""Run `apt-get dist-upgrade -y` on all servers"""
if env.hosts:
execute(_do_upgrades, hosts=env.host_string)
else:
hostlist = _get_hosts()
if hostlist:
execute(_do_upgrades, hosts=hostlist)
else:
print('No hosts with upgrades.')
@task(alias='lu')
def list_upgrades():
"""Get all servers and the available upgrades"""
if env.hosts:
execute(_list_upgrades, hosts=env.host_string)
else:
hostlist = _get_hosts()
if hostlist:
execute(_list_upgrades, hosts=hostlist)
else:
print('No hosts with upgrades.')
# helper functions
def _list_upgrades():
with warn_only():
res = run('apt-get --simulate --verbose-versions dist-upgrade \
| grep " "')
host = red(env.host_string, bold=True)
print('[{}]'.format(host))
if res:
# clean up return values, prettier arrows!
res = [x.lstrip().replace('=>', '→') for x in res.split("\n")]
for package in res:
package = white(package, bold=True)
print('Update for {}'.format(package))
else:
print('No updates.')
def _do_upgrades():
with show('stdout'), warn_only():
run('apt-get --quiet --assume-yes dist-upgrade \
| grep --invert-match "(Reading database"')
def _get_hosts():
config = ConfigParser.ConfigParser()
config.read(expanduser('~/.remoteaptrc'))
db = MySQLdb.connect(host=config.get('icinga', 'host'),
user=config.get('icinga', 'user'),
passwd=config.get('icinga', 'passwd'),
db=config.get('icinga', 'db'))
cur = db.cursor()
cur.execute('SELECT objects.name1 FROM icinga_servicestatus AS status \
JOIN icinga_objects AS objects ON \
(status.service_object_id = objects.object_id) \
WHERE status.current_state != 0 AND \
objects.name2 LIKE "APT status";')
hosts = []
for row in cur.fetchall():
# data from rows
hosts.append(str(row[0]).lower())
# clean up
cur.close()
db.close()
return hosts
# if called directly, list all upgrades
if __name__ == '__main__':
list_upgrades()