5
""" upgrade command .. determine on svn/mercurial update's output if plugs can
6
be reloaded or that the bot has to be rebooted
9
__copyright__ = 'this file is in the public domain'
11
from gozerbot.fleet import fleet
12
from gozerbot.plugins import plugins
13
from gozerbot.commands import cmnds
14
from gozerbot.examples import examples
15
from gozerbot.generic import reboot_stateful, gozerpopen, lockdec, \
16
handle_exception, gethgrev, getsvnrev
17
from gozerbot.eventhandler import mainhandler
18
from gozerbot.plughelp import plughelp
19
from gozerbot.config import config
20
from gozerbot.lockmanager import lockmanager
21
from gozerbot.partyline import partyline
22
import os, os.path, time
24
plughelp.add('upgrade', "do a mercurial hg pull -u / svn update and see if \
25
we need to reboot .. if only plugins are updated we don't need to reboot \
26
because we can reload them")
28
def handle_hgupgrade(bot, ievent, silent=None):
29
""" upgrade .. do a mercurial pull -u .. see if we need to reboot \
30
otherwise reload plugins """
34
ievent.reply("can't fetch current revision")
38
if not ievent.rest.startswith('http://'):
39
ievent.reply('i need a url as argument')
43
url = config['upgradeurl']
44
args = ['hg', 'pull', '-u']
49
proces = gozerpopen(args, userargs)
51
ievent.reply('error running popen: %s' % str(ex))
54
lines = proces.fromchild.readlines()
58
if 'abort: error:' in i:
59
ievent.reply('failed to update ==> %s' % i)
65
ievent.reply('no changes')
68
not silent and ievent.reply(' .. '.join(res))
70
ievent.reply('revision is %s' % rev)
71
args = ['hg', 'diff', '-r%s' % startrev]
73
proces = gozerpopen(args)
75
ievent.reply('error running popen: %s' % str(ex))
77
data = proces.fromchild.readlines()
78
returncode = proces.close()
80
ievent.reply("can't run hg diff")
87
if i.startswith('+++') or i.startswith('---'):
88
if len(i.split()) < 2:
90
filename = '%s' % os.sep
91
filename = filename.join(i.split()[1].split(os.sep)[1:])
92
if filename == 'dev/null':
94
if filename not in files:
95
files.append(filename)
96
if not filename.endswith('.py'):
98
if filename.startswith('gozerbot'):
100
elif filename.find('dbplugs') != -1:
101
if not config['dbenable']:
103
if filename not in dbplugs:
104
dbplugs.append(filename)
105
elif filename.find('plugs') != -1:
106
if filename not in plugs:
107
plugs.append(filename)
108
not silent and ievent.reply('files: ' + ' '.join(files))
110
args = ['hg', 'log', '-r%s:%s' % (rev, startrev+1)]
112
proces = gozerpopen(args)
113
except Exception, ex:
114
ievent.reply('error running popen: %s' % str(ex))
116
data = proces.fromchild.readlines()
117
returncode = proces.close()
120
if i.startswith('summary:'):
121
summary.append(i.split('summary:')[1].strip())
122
not silent and ievent.reply("summaries: %s" % ' .. '.join(summary))
124
ievent.reply('rebooting')
131
mainhandler.put(0, reboot_stateful, bot, ievent, fleet, partyline)
134
if not plugs and not dbplugs:
135
ievent.reply('nothing to reload')
137
ievent.reply("reloading %s" % " .. ".join(dbplugs + plugs))
138
failed = plugins.listreload(plugs)
139
if config['dbenable']:
140
failed += plugins.listreload(dbplugs)
142
ievent.reply("failed to reload %s" % ' .. '.join(failed))
147
cmnds.add('upgrade-hg', handle_hgupgrade, ['OPER', 'UPGRADE'])
148
examples.add('upgrade-hg', 'do a mercurial upgrade', 'upgrade-hg')
150
def handle_svnupgrade(bot, ievent, silent=None):
151
""" do a svn update and check if we have to reboot or that plugs reload
154
ievent.reply("doing svn upgrade")
155
args = ['svn', 'update']
157
proces = gozerpopen(args)
158
except Exception, ex:
159
ievent.reply('error running popen: %s' % str(ex))
161
data = proces.fromchild.readlines()
162
returncode = proces.close()
164
ievent.reply('error running svn update')
171
res.append(line.strip())
173
ievent.reply(" .. ".join(res))
174
# see if we need to reboot and if not what plugins need to be reloaded
177
(svnaction, filename) = line.split()
182
if filename.startswith('gozerbot'):
185
elif filename.find('dbplugs') != -1:
186
if not config['dbenable']:
188
if filename not in dbplugs:
189
dbplugs.append(filename)
190
elif filename.find('plugs') != -1:
191
if filename not in plugs:
192
plugs.append(filename)
194
ievent.reply('need reboot')
201
mainhandler.put(0, reboot_stateful, bot, ievent, fleet, partyline)
202
if not plugs and not dbplugs:
203
ievent.reply('nothing to reload')
205
ievent.reply("reloading %s" % " .. ".join(dbplugs + plugs))
206
failed = plugins.listreload(plugs)
207
if config['dbenable']:
208
failed += plugins.listreload(dbplugs)
210
ievent.reply("failed to reload %s" % ' .. '.join(failed))
215
cmnds.add('upgrade-svn', handle_svnupgrade, ['OPER', 'UPGRADE'])
216
examples.add('upgrade-svn', 'upgrade the bot with svn', 'upgrade-svn')
218
def handle_upgradesilent(bot, ievent):
219
""" do a 'silent' upgrade """
220
lockmanager.acquire('up')
221
if os.path.isdir('.hg'):
223
handle_hgupgrade(bot, ievent, True)
224
except Exception, ex:
225
handle_exception(ievent=ievent)
226
lockmanager.release('up')
228
if os.path.isdir('.svn'):
230
handle_svnupgrade(bot, ievent, True)
231
except Exception, ex:
232
handle_exception(ievent=ievent)
233
lockmanager.release('up')
235
ievent.reply('i need a svn or mercurial repository for upgrade to work')
236
lockmanager.release('up')
238
cmnds.add('upgrade', handle_upgradesilent, ['OPER', 'UPGRADE'])
239
examples.add('upgrade', 'do a svn or mercurial upgrade', \
242
def handle_upgradeloud(bot, ievent):
243
""" do a verbose upgrade """
244
lockmanager.acquire('up')
245
if os.path.isdir('.hg'):
247
handle_hgupgrade(bot, ievent)
248
except Exception, ex:
249
handle_exception(ievent=ievent)
250
lockmanager.release('up')
252
if os.path.isdir('.svn'):
254
handle_svnupgrade(bot, ievent)
255
except Exception, ex:
256
handle_exception(ievent=ievent)
257
lockmanager.release('up')
259
ievent.reply('i need a svn or mercurial repository for upgrade to work')
260
lockmanager.release('up')
262
cmnds.add('upgrade-loud', handle_upgradeloud, ['OPER'])
263
examples.add('upgrade-loud', 'do a svn or mercurial upgrade', 'upgrade-loud')