~ubuntu-branches/ubuntu/precise/gozerbot/precise

« back to all changes in this revision

Viewing changes to build/lib/gozerplugs/plugs/upgrade.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2008-06-02 19:26:39 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080602192639-3rn65nx4q1sgd6sy
Tags: 0.8.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# plugs/upgrade.py
2
 
#
3
 
#
4
 
 
5
 
""" upgrade command .. determine on svn/mercurial update's output if plugs can
6
 
    be reloaded or that the bot has to be rebooted
7
 
"""
8
 
 
9
 
__copyright__ = 'this file is in the public domain'
10
 
 
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
23
 
 
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")
27
 
 
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 """
31
 
    
32
 
    startrev = gethgrev()
33
 
    if not startrev:
34
 
        ievent.reply("can't fetch current revision")
35
 
        return
36
 
    url = None
37
 
    if ievent.rest:
38
 
        if not ievent.rest.startswith('http://'):
39
 
            ievent.reply('i need a url as argument')
40
 
            return
41
 
        url = ievent.rest
42
 
    else:
43
 
        url = config['upgradeurl']
44
 
    args = ['hg', 'pull', '-u']
45
 
    userargs = []
46
 
    if url:
47
 
        userargs.append(url)
48
 
    try:
49
 
        proces = gozerpopen(args, userargs)
50
 
    except Exception, ex:
51
 
        ievent.reply('error running popen: %s' % str(ex))
52
 
        return
53
 
    nochange = 0
54
 
    lines = proces.fromchild.readlines()
55
 
    proces.close()
56
 
    res = []
57
 
    for i in lines:
58
 
        if 'abort: error:' in i:
59
 
            ievent.reply('failed to update ==>  %s' % i)
60
 
            return
61
 
        if 'no changes' in i:
62
 
            nochange = 1
63
 
        res.append(i.strip())
64
 
    if nochange:
65
 
        ievent.reply('no changes')
66
 
        return
67
 
    else:
68
 
        not silent and ievent.reply(' .. '.join(res))
69
 
    rev = gethgrev()
70
 
    ievent.reply('revision is %s' % rev)
71
 
    args = ['hg', 'diff', '-r%s' % startrev]
72
 
    try:
73
 
        proces = gozerpopen(args)
74
 
    except Exception, ex:
75
 
        ievent.reply('error running popen: %s' % str(ex))
76
 
        return
77
 
    data = proces.fromchild.readlines()
78
 
    returncode = proces.close()
79
 
    if returncode != 0:
80
 
        ievent.reply("can't run hg diff")
81
 
        return
82
 
    files = []
83
 
    plugs = []
84
 
    dbplugs = []
85
 
    needreboot = 0
86
 
    for i in data:
87
 
        if i.startswith('+++') or i.startswith('---'):
88
 
            if len(i.split()) < 2:
89
 
                continue
90
 
            filename = '%s' % os.sep
91
 
            filename = filename.join(i.split()[1].split(os.sep)[1:])
92
 
            if filename == 'dev/null':
93
 
                continue
94
 
            if filename not in files:
95
 
                files.append(filename)
96
 
            if not filename.endswith('.py'):
97
 
                continue
98
 
            if filename.startswith('gozerbot'): 
99
 
                needreboot = 1
100
 
            elif filename.find('dbplugs') != -1:
101
 
                if not config['dbenable']:
102
 
                    continue
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))
109
 
    summary = []
110
 
    args = ['hg', 'log', '-r%s:%s' % (rev, startrev+1)]
111
 
    try:
112
 
        proces = gozerpopen(args)
113
 
    except Exception, ex:
114
 
        ievent.reply('error running popen: %s' % str(ex))
115
 
        return
116
 
    data = proces.fromchild.readlines()
117
 
    returncode = proces.close()
118
 
    if returncode == 0:
119
 
        for i in data:
120
 
            if i.startswith('summary:'):
121
 
                summary.append(i.split('summary:')[1].strip())
122
 
        not silent and ievent.reply("summaries: %s" % ' .. '.join(summary))
123
 
    if needreboot:
124
 
        ievent.reply('rebooting')
125
 
        time.sleep(4)
126
 
        try:
127
 
            plugins.exit()
128
 
            fleet.save()
129
 
        finally:
130
 
            time.sleep(1)
131
 
            mainhandler.put(0, reboot_stateful, bot, ievent, fleet, partyline)
132
 
            return
133
 
    config.load()
134
 
    if not plugs and not dbplugs:
135
 
        ievent.reply('nothing to reload')
136
 
        return
137
 
    ievent.reply("reloading %s" %  " .. ".join(dbplugs + plugs))
138
 
    failed = plugins.listreload(plugs)
139
 
    if config['dbenable']:
140
 
        failed += plugins.listreload(dbplugs)
141
 
    if failed:
142
 
        ievent.reply("failed to reload %s" % ' .. '.join(failed))
143
 
        return
144
 
    else:
145
 
        ievent.reply('done')
146
 
 
147
 
cmnds.add('upgrade-hg', handle_hgupgrade, ['OPER', 'UPGRADE'])
148
 
examples.add('upgrade-hg', 'do a mercurial upgrade', 'upgrade-hg')
149
 
 
150
 
def handle_svnupgrade(bot, ievent, silent=None):
151
 
    """ do a svn update and check if we have to reboot or that plugs reload
152
 
        is enough
153
 
    """
154
 
    ievent.reply("doing svn upgrade")
155
 
    args = ['svn', 'update']
156
 
    try:
157
 
        proces = gozerpopen(args)
158
 
    except Exception, ex:
159
 
        ievent.reply('error running popen: %s' % str(ex))
160
 
        return
161
 
    data = proces.fromchild.readlines()
162
 
    returncode = proces.close()
163
 
    if returncode:
164
 
        ievent.reply('error running svn update')
165
 
        return
166
 
    plugs = []
167
 
    dbplugs = []
168
 
    needreboot = 0
169
 
    res = []
170
 
    for line in data:
171
 
        res.append(line.strip())
172
 
    if res:
173
 
        ievent.reply(" .. ".join(res))
174
 
    # see if we need to reboot and if not what plugins need to be reloaded
175
 
    for line in res:
176
 
        try:
177
 
            (svnaction, filename) = line.split()
178
 
        except ValueError:
179
 
            continue
180
 
        if svnaction == 'D':
181
 
            continue
182
 
        if filename.startswith('gozerbot'): 
183
 
            needreboot = 1
184
 
            break
185
 
        elif filename.find('dbplugs') != -1:
186
 
            if not config['dbenable']:
187
 
                continue
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)
193
 
    if needreboot:
194
 
        ievent.reply('need reboot')
195
 
        time.sleep(4)
196
 
        try:
197
 
            plugins.exit()
198
 
            fleet.save()
199
 
        finally:
200
 
            time.sleep(1)
201
 
            mainhandler.put(0, reboot_stateful, bot, ievent, fleet, partyline)
202
 
    if not plugs and not dbplugs:
203
 
        ievent.reply('nothing to reload')
204
 
        return
205
 
    ievent.reply("reloading %s" %  " .. ".join(dbplugs + plugs))
206
 
    failed = plugins.listreload(plugs)
207
 
    if config['dbenable']:
208
 
        failed += plugins.listreload(dbplugs)
209
 
    if failed:
210
 
        ievent.reply("failed to reload %s" % ' .. '.join(failed))
211
 
        return
212
 
    else:
213
 
        ievent.reply('done')
214
 
 
215
 
cmnds.add('upgrade-svn', handle_svnupgrade, ['OPER', 'UPGRADE'])
216
 
examples.add('upgrade-svn', 'upgrade the bot with svn', 'upgrade-svn')
217
 
 
218
 
def handle_upgradesilent(bot, ievent):
219
 
    """ do a 'silent' upgrade """
220
 
    lockmanager.acquire('up')
221
 
    if os.path.isdir('.hg'):
222
 
        try:
223
 
            handle_hgupgrade(bot, ievent, True)
224
 
        except Exception, ex: 
225
 
            handle_exception(ievent=ievent)
226
 
        lockmanager.release('up')
227
 
        return
228
 
    if os.path.isdir('.svn'):
229
 
        try:
230
 
            handle_svnupgrade(bot, ievent, True)
231
 
        except Exception, ex: 
232
 
            handle_exception(ievent=ievent)
233
 
        lockmanager.release('up')
234
 
        return
235
 
    ievent.reply('i need a svn or mercurial repository for upgrade to work')
236
 
    lockmanager.release('up')
237
 
    
238
 
cmnds.add('upgrade', handle_upgradesilent, ['OPER', 'UPGRADE'])
239
 
examples.add('upgrade', 'do a svn or mercurial upgrade', \
240
 
'upgrade')
241
 
 
242
 
def handle_upgradeloud(bot, ievent):
243
 
    """ do a verbose upgrade """
244
 
    lockmanager.acquire('up')
245
 
    if os.path.isdir('.hg'):
246
 
        try:
247
 
            handle_hgupgrade(bot, ievent)
248
 
        except Exception, ex: 
249
 
            handle_exception(ievent=ievent)
250
 
        lockmanager.release('up')
251
 
        return
252
 
    if os.path.isdir('.svn'):
253
 
        try:
254
 
            handle_svnupgrade(bot, ievent)
255
 
        except Exception, ex: 
256
 
            handle_exception(ievent=ievent)
257
 
        lockmanager.release('up')
258
 
        return
259
 
    ievent.reply('i need a svn or mercurial repository for upgrade to work')
260
 
    lockmanager.release('up')
261
 
    
262
 
cmnds.add('upgrade-loud', handle_upgradeloud, ['OPER'])
263
 
examples.add('upgrade-loud', 'do a svn or mercurial upgrade', 'upgrade-loud')