Merge lp:~unifield-team/unifield-server/restart-and-update-cto into lp:unifield-server/sprint5

Proposed by Samus CTO (OpenERP)
Status: Merged
Merged at revision: 3415
Proposed branch: lp:~unifield-team/unifield-server/restart-and-update-cto
Merge into: lp:unifield-server/sprint5
Diff against target: 192 lines (+151/-6)
2 files modified
bin/openerp-server.py (+21/-6)
bin/updater.py (+130/-0)
To merge this branch: bzr merge lp:~unifield-team/unifield-server/restart-and-update-cto
Reviewer Review Type Date Requested Status
Samus CTO (OpenERP) (community) Needs Information
jftempo Needs Resubmitting
Review via email: mp+117602@code.launchpad.net

Description of the change

Hello,

This is the Auto-Updater for OpenERP Server Sprint5

To post a comment you must log in.
Revision history for this message
jftempo (jfb-tempo-consulting) wrote :

Please create an issue in Jira, with a description and a link to this branch.
This issue has to be "Runbot Validated" and assigned to me.

And please replace the print statement in your code by a clean log.

review: Needs Resubmitting
Revision history for this message
Samus CTO (OpenERP) (cto-openerp) wrote :

> Please create an issue in Jira, with a description and a link to this branch.
> This issue has to be "Runbot Validated" and assigned to me.
>
> And please replace the print statement in your code by a clean log.

I'm not aware about Jira's process I will ask Duy for that.

In the other hand, I think it's hard to have a better quality code than that with the use of print.

I can use sys.stdout (or stderr) if you prefer.

The point is the Update process is launched with minimal dependencies as possible (and only Python's ones (and psycopg)). If I want to use logger I will need to import tools.config and netsvc which are OpenERP.

What's your position?

Regards

review: Needs Information

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'bin/openerp-server.py'
--- bin/openerp-server.py 2011-01-06 13:06:54 +0000
+++ bin/openerp-server.py 2012-09-27 15:11:20 +0000
@@ -30,6 +30,9 @@
30(c) 2003-TODAY, Fabien Pinckaers - OpenERP s.a.30(c) 2003-TODAY, Fabien Pinckaers - OpenERP s.a.
31"""31"""
3232
33from updater import do_update
34do_update()
35
33#----------------------------------------------------------36#----------------------------------------------------------
34# python imports37# python imports
35#----------------------------------------------------------38#----------------------------------------------------------
@@ -215,7 +218,7 @@
215if os.name == 'posix':218if os.name == 'posix':
216 signal.signal(signal.SIGQUIT, dumpstacks)219 signal.signal(signal.SIGQUIT, dumpstacks)
217220
218def quit():221def quit(restart=False):
219 netsvc.Agent.quit()222 netsvc.Agent.quit()
220 netsvc.Server.quitAll()223 netsvc.Server.quitAll()
221 if tools.config['pidfile']:224 if tools.config['pidfile']:
@@ -235,7 +238,17 @@
235 # and would present the forced shutdown238 # and would present the forced shutdown
236 thread.join(0.05)239 thread.join(0.05)
237 time.sleep(0.05)240 time.sleep(0.05)
238 sys.exit(0)241 time.sleep(1)
242 if os.name == 'nt':
243 try:
244 logger.info("Killing", thread.getName())
245 thread._Thread__stop()
246 except:
247 logger.info(str(thread.getName()) + ' could not be terminated')
248 if not restart:
249 sys.exit(0)
250 else:
251 os.execv(sys.executable, [sys.executable] + sys.argv)
239252
240if tools.config['pidfile']:253if tools.config['pidfile']:
241 fd = open(tools.config['pidfile'], 'w')254 fd = open(tools.config['pidfile'], 'w')
@@ -247,9 +260,11 @@
247260
248logger.info('OpenERP server is running, waiting for connections...')261logger.info('OpenERP server is running, waiting for connections...')
249262
250while netsvc.quit_signals_received == 0:263tools.restart_required = False
251 time.sleep(60)264
252265while netsvc.quit_signals_received == 0 and not tools.restart_required:
253quit()266 time.sleep(5)
267
268quit(restart=tools.restart_required)
254269
255# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:270# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
256271
=== added file 'bin/updater.py'
--- bin/updater.py 1970-01-01 00:00:00 +0000
+++ bin/updater.py 2012-09-27 15:11:20 +0000
@@ -0,0 +1,130 @@
1import os
2import sys
3import psycopg2
4from datetime import datetime
5
6## Unix-like find
7def find(path):
8 files = os.listdir(path)
9 for name in iter(files):
10 abspath = path+os.path.sep+name
11 if os.path.isdir( abspath ) and not os.path.islink( abspath ):
12 files.extend( map(lambda x:name+os.path.sep+x, os.listdir(abspath)) )
13 return files
14
15## Define way to forward logs
16def warn(*args):
17 sys.stderr.write(" ".join(map(lambda x:str(x), args))+"\n")
18
19## Try...Resume...
20def Try(command):
21 try:
22 command()
23 except:
24 e, msg = sys.exc_info()[0].__name__, str(sys.exc_info()[1])
25 warn(str(msg))
26 return False
27 else:
28 return True
29
30## Python free rmtree
31def rmtree(files, path=None, verbose=False):
32 if path is None and isinstance(files, str):
33 path, files = files, find(files)
34 for f in reversed(files):
35 target = os.path.join(path, f) if path is not None else f
36 if os.path.isfile(target) or os.path.islink(target):
37 warn("unlink", target)
38 os.unlink( target )
39 elif os.path.isdir(target):
40 warn("rmdir", target)
41 os.rmdir( target )
42
43def do_update():
44## We expect to be in the bin/ directory to proceed
45 if os.path.exists('update.lock'):
46 rev_file = os.path.join('.update','revisions.txt')
47 hist_file = "revision_history.txt"
48 infos = {'exec_path':os.getcwd()}
49 revisions = None
50 cur = None
51 conn = None
52 update_revisions = None
53 files = None
54 args = list(sys.argv)
55 for i, x in enumerate(args):
56 if x in ('-d', '-u'):
57 args[i] = None
58 args[i+1] = None
59 args = filter(lambda x:x is not None, args)
60 try:
61 ## Read DB name
62 f = open('update.lock')
63 infos = eval(f.read())
64 f.close()
65 revisions = ",".join( map(lambda x:"'"+str(x)+"'", infos['revisions']) )
66 ## Connect to the DB
67 conn = psycopg2.connect(database=infos['dbname'], user=infos['db_user'], password=infos['db_password'], host=infos['db_host'], port=infos['db_port'])
68 conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
69 cur = conn.cursor()
70 ## Explore .update directory
71 files = find('.update')
72 ## Prepare backup directory
73 if not os.path.exists('backup'):
74 os.mkdir('backup')
75 else:
76 rmtree('backup')
77 ## Update Files
78 warn("Updating...")
79 for f in files:
80 target = os.path.join('.update', f)
81 bak = os.path.join('backup', f)
82 if os.path.isdir(target):
83 if os.path.isfile(f) or os.path.islink(f):
84 os.unlink(f)
85 if not os.path.exists(f):
86 os.mkdir(f)
87 os.mkdir(bak)
88 else:
89 if os.path.exists(f):
90 warn("`%s' -> `%s'" % (f, bak))
91 os.rename(f, bak)
92 warn("`%s' -> `%s'" % (target, f))
93 os.rename(target, f)
94 ## Update installed revisions in DB
95 cur.execute("""UPDATE sync_client_version SET state = 'installed', applied = '%s' WHERE name in (%s)"""
96 % ( datetime.today().strftime("%Y-%m-%d %H:%M:%S"), revisions ))
97 warn("Update successful.")
98 warn("Revisions added: ", ", ".join( infos['revisions'] ))
99 args.extend(['-d', infos['dbname'], '-u', 'all'])
100 except:
101 warn("Update failure!")
102 ## Update DB to mark revisions as not-installed
103 if cur and infos:
104 Try(lambda:cur.execute("""UPDATE sync_client_version SET state = 'not-installed' WHERE name in (%s)"""
105 % ( revisions )))
106 ## Restore backup and purge .update
107 if files:
108 warn("Restoring...")
109 for f in reversed(files):
110 target = os.path.join('backup', f)
111 if os.path.isfile(target) or os.path.islink(target):
112 warn("`%s' -> `%s'" % (target, f))
113 elif os.path.isdir(target):
114 warn("rmdir", target)
115 os.rmdir( target )
116 warn("Purging...")
117 Try(lambda:rmtree(files, '.update'))
118 warn("rmdir", '.update')
119 Try(lambda:os.rmdir( '.update' ))
120 finally:
121 if cur: cur.close()
122 if conn: conn.close()
123 ## Remove lock file
124 warn("rm", 'update.lock')
125 Try(lambda:os.unlink( 'update.lock' ))
126 warn("Restart OpenERP in", infos['exec_path'], "with:",args)
127 if infos: os.chdir(infos['exec_path'])
128 os.execv(sys.executable, [sys.executable] + args)
129
130

Subscribers

People subscribed via source and target branches

to all changes: