forked from akretion/ak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ak.py
executable file
·278 lines (220 loc) · 8.41 KB
/
ak.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# coding: utf-8
"""AK."""
import logging
from plumbum import cli, local
from plumbum.cmd import (test, python, grep, gunzip, pg_isready,
createdb, psql, dropdb, pg_restore)
from plumbum.commands.modifiers import RETCODE, FG, TEE, TF
import os
import ConfigParser
BUILDOUT_URL = ('https://raw.github.com/buildout/'
'buildout/master/bootstrap/bootstrap.py')
OPENRPCFG = 'etc/openerp.cfg'
class Ak(cli.Application):
PROGNAME = "ak"
VERSION = "1.0"
dryrunFlag = cli.Flag(["dry-run"], help="Dry run mode")
def log_and_run(self, cmd, retcode=FG):
"""Log cmd before exec."""
logging.info(cmd)
if (self.dryrunFlag):
print cmd
return True
return cmd & RETCODE
def log_and_exec(self, cmd, args=[], env=None):
"""Log cmd and execve."""
logging.info([cmd, args])
if (self.dryrunFlag):
print "os.execvpe (%s, %s, env)" % (cmd, [cmd] + args)
return True
os.execvpe(cmd, [cmd] + args, env)
@cli.switch("--verbose", help="Verbose mode")
def set_log_level(self):
logging.root.setLevel(logging.INFO)
logging.info('Verbose mode activated')
def main(self, *args):
if args:
print "Unkown command %r" % (args[0],)
return 1 # return error
if not self.nested_command:
print "No command given"
return 1
@Ak.subcommand("run")
class AkRun(cli.Application):
"""Start openerp."""
db = cli.SwitchAttr(["d"], str, help="Database")
debugFlag = cli.Flag(["D", "debug"], help="Debug mode")
consoleFlag = cli.Flag(['console'], help="Console mode")
updateFlag = cli.SwitchAttr(
["u", "update"], list=True, help="Update module")
def main(self, *args):
params = []
if self.db:
params += ['--db-filter', self.db]
if self.debugFlag:
params += ['--debug']
if self.updateFlag:
params += ['-u', str.join(',', self.updateFlag)]
if self.consoleFlag:
command = 'bin/python_openerp'
else:
command = 'bin/start_openerp'
return self.parent.log_and_exec(command, params, local.env)
@Ak.subcommand("build")
class AkBuild(cli.Application):
"Build dependencies for odoo"
freezeFlag = cli.Flag(
["freeze"], help="Freeze dependencies to frozen.cfg")
configFlag = cli.SwitchAttr(
["c", "config"], default=OPENRPCFG, help="Config flag")
def freeze(self):
cmd = local['bin/buildout']['-o', 'openerp:freeze-to=frozen.cfg']
print cmd
def build(self):
cmd = local['bin/buildout']['-c', configFlag]
print cmd
def main(self, *args):
True
# TODO: is it really ak's job to install buildout ? why not pgsql also ?
# if not self.is_installed():
# if cli.terminal.ask(
# "Buildout not found. Download it ?", default="Y"):
# self.download_and_install()
# else:
# raise Exception("Can't continue without buildout")
#
#
# def is_installed(self):
# logging.info('Will check if buildout is installed')
# return (test["-f", 'bin/buildosut'] & RETCODE)
#
# def download_and_install(self):
# logging.info('Will download buildout from %s' % BUILDOUT_URL)
# wget = local['wget']
# cmd = wget["BUILDOUT_URL", "-O -"] | python
# print "c bon"
@Ak.subcommand("db")
class AkDb(cli.Application):
"""Db tools.
Run without args to get a psql prompt
Credentials are extracted from etc/openerp.cfg
"""
db = cli.SwitchAttr(["-d"], str, help="Database")
force = cli.Flag('--force', help="Force", group="IO")
loadFlag = cli.Flag(["load"], group="IO",
help="Load a dump")
dumpFlag = cli.Flag(["dump"], group="IO", requires=['p'],
help="Export a dump")
path = cli.SwitchAttr(["path", "p"], str, group="IO",
help="Path to a file dump")
waitFlag = cli.Flag(["wait"], group="Other",
help="pg_isready")
infoFlag = cli.Flag(["info"], group="Other",
help="info on db crendentials")
passFlag = cli.Flag(["admin-password"], group="Other",
help="Change odoo admin password")
dbParams = {
"db_host": "PGHOST",
"db_port": "PGPORT",
"db_name": "PGDATABASE",
"db_user": "PGUSER",
"db_password": "PGPASSWORD"
}
def psql(self):
"""Run psql."""
self.log_and_exec('psql', [], local.env)
def load(self, afile, force):
"""Load (restore) a dump from a file.
Will create a database if not exist already
:param afile: path to dump (can be .gz or .tar)
:param foce: db will be dropped before the load
"""
p = local.path(afile)
if not p.is_file():
raise Exception("input file not found")
# check if db exists
cmd = psql["-c", ""]
if (self.log_and_run(cmd, TF)): # TF = result of cmd as True or False
if force:
logging.info('DB already exists. Drop and create it')
self.log_and_run(dropdb)
self.log_and_run(createdb)
else:
logging.info('DB does ont exists. Create it')
self.log_and_run(createdb)
if p.suffix == '.gz':
self.log_and_run(gunzip['-c', p] | psql)
else:
self.log_and_run(pg_restore["-O", p])
# set cron to inactive
# TODO give a flag for that
self.log_and_run(psql["-c", "'UPDATE ir_cron SET active=False;'"])
def dump(self, afile, force):
"""Dump database to file with pg_dump then gzip.
:param afile: path to dump file
:param force: overwrite the file if exists
"""
# TODO choose format
p = local.path(afile)
if p.is_file() and not force:
raise Exception("output file already exists. Use --force")
self.log_and_run(local['pg_dump'] | local['gzip'] > afile)
def wait(self):
"""Run pg_isready."""
self.log_and_run(pg_isready)
def info(self):
"""Print db informations from etc/buildout.cfg."""
for ini_key, pg_key in self.dbParams.iteritems():
print ini_key, local.env.get(pg_key, '')
def changeAdminPassword(self, args):
"""Change admin password."""
new_pass = (args or [False])[0]
if (not new_pass): # not provided by cli
new_pass = cli.terminal.prompt(
'New admin password ?', str, "admin")
print "New admin password for %s is : '%s'" % (self.db, new_pass)
code = """session.open(db='%s')"
user_ids = session.registry('res.users').search(session.cr, 1, [])",
for user in session.registry('res.users').browse(session.cr, 1, user_ids):",
user.write({'password': '%s' })",
session.cr.commit()
""" % (self.db, new_pass)
print local['echo'][code] | local['ak']['console']
# TODO: run this command and test it !
def determine_db(self):
"""Extract db parameters from openerp.cfg."""
# internal func
# read ini file
if not local.path(OPENRPCFG).is_file():
logging.warn("OPENRPCFG not found")
else:
config = ConfigParser.ConfigParser()
config.readfp(open(OPENRPCFG))
for ini_key, pg_key in self.dbParams.iteritems():
val = config.get('options', ini_key)
if not val == "False":
logging.info('Set %s to %s' % (pg_key, val))
local.env[pg_key] = val
if self.db: # if db is forced by flag
logging.info("PGDATABASE overwitten by %s", self.db)
local.env["PGDATABASE"] = self.db
def main(self, *args):
# bind functions with AK
self.log_and_run = self.parent.log_and_run
self.log_and_exec = self.parent.log_and_exec
self.determine_db() # get credentials
if (self.loadFlag):
self.load(self.path, self.force)
elif (self.dumpFlag):
self.dump(self.path, self.force)
elif (self.waitFlag):
self.wait()
elif (self.infoFlag):
self.info()
elif (self.passFlag):
self.changeAdminPassword(args)
else:
self.psql()
if __name__ == "__main__":
Ak.run()