/usr/lib/python2.7/dist-packages/gplugs/upgrade.py is in gozerbot 0.99.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 156 157 158 159 160 161 | # plugs/upgrade.py
#
#
__copyright__ = 'this file is in the public domain'
from gozerbot.aliases import aliasset
from gozerbot.fleet import fleet
from gozerbot.plugins import plugins
from gozerbot.commands import cmnds
from gozerbot.examples import examples
from gozerbot.generic import reboot_stateful, gozerpopen, lockdec, \
handle_exception
from gozerbot.eventhandler import mainhandler
from gozerbot.plughelp import plughelp
from gozerbot.config import config
from gozerbot.utils.lockmanager import lockmanager
from gozerbot.partyline import partyline
import os, os.path, time
plughelp.add('upgrade', "do a mercurial hg pull -u and see if \
we need to reboot .. if only plugins are updated we don't need to reboot \
because we can reload them")
def gethgrev(path=None):
rev = None
try:
if not path:
proces = gozerpopen(['hg', 'tip'])
else:
proces = gozerpopen(['hg', 'tip', '-R%s' % path])
rev = int(proces.fromchild.readlines()[0].split(':')[1].strip())
proces.close()
except (OSError, IndexError):
return rev
except Exception, ex:
handle_exception()
return rev
def handle_hgupgrade(bot, ievent, silent=None, path=None):
""" upgrade .. do a mercurial pull -u .. see if we need to reboot \
otherwise reload plugins """
if not path:
path = '.'
startrev = gethgrev(path)
if not startrev:
ievent.reply("can't fetch current revision")
return
ievent.reply('upgrading from revision %s' % str(startrev))
args = ['hg', 'pull', '-u', '-R%s' % path]
userargs = []
try:
proces = gozerpopen(args, userargs)
except Exception, ex:
ievent.reply('error running popen: %s' % str(ex))
return
nochange = 0
lines = proces.fromchild.readlines()
proces.close()
res = []
for i in lines:
if 'abort: error:' in i:
ievent.reply('failed to update ==> %s' % i)
return
if 'no changes' in i:
nochange = 1
res.append(i.strip())
if nochange:
ievent.reply('no changes')
return
else:
not silent and ievent.reply(' .. '.join(res))
rev = gethgrev(path)
ievent.reply("new revision is %s" % rev)
args = ['hg', 'diff', '-r%s' % startrev, '-R%s' % path]
try:
proces = gozerpopen(args)
except Exception, ex:
ievent.reply('error running popen: %s' % str(ex))
return
data = proces.fromchild.readlines()
returncode = proces.close()
if returncode != 0:
ievent.reply("can't run hg diff")
return
files = []
plugs = []
needreboot = 0
for i in data:
if i.startswith('+++') or i.startswith('---'):
if len(i.split()) < 2:
continue
filename = '%s' % os.sep
filename = filename.join(i.split()[1].split(os.sep)[1:])
if filename == 'dev/null':
continue
if filename not in files:
files.append(filename)
if not filename.endswith('.py'):
continue
if filename.startswith('gozerbot') and not 'plugs' in filename:
needreboot = 1
elif filename.find('plugs') != -1 or path == 'gplugs':
if filename not in plugs:
plugs.append(filename)
not silent and ievent.reply('files: ' + ' '.join(files))
summary = []
args = ['hg', 'log', '-r%s:%s' % (rev, startrev+1), '-R%s' % path]
try:
proces = gozerpopen(args)
except Exception, ex:
ievent.reply('error running popen: %s' % str(ex))
return
data = proces.fromchild.readlines()
returncode = proces.close()
if returncode == 0:
for i in data:
if i.startswith('summary:'):
summary.append(i.split('summary:')[1].strip())
not silent and ievent.reply("summaries: %s" % ' .. '.join(summary))
if needreboot:
ievent.reply('rebooting')
time.sleep(4)
try:
plugins.exit()
fleet.save()
finally:
time.sleep(1)
mainhandler.put(0, reboot_stateful, bot, ievent, fleet, partyline)
return
config.load()
if not plugs:
ievent.reply('nothing to reload')
return
ievent.reply("reloading %s" % " .. ".join(plugs))
failed = plugins.listreload(plugs)
if failed:
ievent.reply("failed to reload %s" % ' .. '.join(failed))
return
else:
ievent.reply('done')
cmnds.add('upgrade-hg', handle_hgupgrade, ['OPER', 'UPGRADE'])
examples.add('upgrade-hg', 'do a mercurial upgrade', 'upgrade-hg')
def handle_upgradeloud(bot, ievent):
""" do a verbose upgrade """
if ievent.rest == 'gozerplugs':
path = 'gozerplugs'
else:
path = None
lockmanager.acquire('up')
try:
handle_hgupgrade(bot, ievent, False, path)
except Exception, ex:
handle_exception()
lockmanager.release('up')
return
cmnds.add('upgrade-loud', handle_upgradeloud, ['OPER', 'UPGRADE'])
examples.add('upgrade-loud', 'do a mercurial upgrade', 'upgrade-loud')
|