~loyalinfo/openerp-tools/trunk

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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python2
import glob
import os
import optparse
import signal
import shutil
import socket
import subprocess
import time
import xmlrpclib


#----------------------------------------------------------
# Utils
#----------------------------------------------------------
join = os.path.join

def mkdir(d):
    if not os.path.isdir(d):
        os.makedirs(d)

def url2dir(n):
    return n.replace('/','_').replace(':','').replace('~','')

def system(l,chdir=None):
    print l
    if chdir:
        cwd = os.getcwd()
        os.chdir(chdir)
    if isinstance(l,list):
        rc=os.spawnvp(os.P_WAIT, l[0], l)
    elif isinstance(l,str):
        tmp=['sh','-c',l]
        rc=os.spawnvp(os.P_WAIT, tmp[0], tmp)
    if chdir:
        os.chdir(cwd)
    return rc

#----------------------------------------------------------
# Stages
#----------------------------------------------------------

def init(o):
    for i in [o.repo, o.work, o.pkg, o.pub, join(o.pub,"src"), join(o.pub,"deb"), join(o.pub,"exe"), join(o.pub,"rpm")]:
        mkdir(i)
    if not os.path.isdir(join(o.repo,'.bzr')):
        system(['bzr','init-repo',o.repo])

def branch_or_update(b,d):
    if os.path.isdir(d):
        system(['bzr','pull','-d',d,'--overwrite'])
    else:
        system(['bzr','branch',b,d])

def update(o):
    branch_or_update(o.addons_branch,o.addons_dir)
    branch_or_update(o.server_branch,o.server_dir)
    branch_or_update(o.web_branch,o.web_dir)

def rsync(o):
    system(["rsync","-a","--exclude",".bzr","--delete", "%s/"%o.server_dir, o.work])
    system(["rsync","-a","--exclude",".bzr", "%s/"%o.addons_dir, o.work_addons])
    system(["rsync","-a","--exclude",".bzr", "%s/addons/"%o.web_dir, o.work_addons])

def add_bzr_version(o, n, w):
    b = getattr(o, n + '_branch')
    d = getattr(o, n + '_dir')
    system('echo "# %s bzr version: $(bzr revision-info --tree -d %s) on %s" >> %s' % (n, d, b, join(w, 'release.py')))

def version(o):
    open(join(o.work,'openerp','release.py'),'a').write('version = "%s-%s"\n'%(o.version,o.timestamp))
    add_bzr_version(o, 'server', join(o.work, 'openerp'))
    add_bzr_version(o, 'addons', join(o.work, 'openerp'))
    add_bzr_version(o, 'web', join(o.work, 'openerp'))

def sdist(o):
    cmd=['python2','setup.py', '--quiet', 'sdist', '-d','../pkg']
    system(cmd,o.work)

def bdist_rpm(o):
    cmd=['python2','setup.py', '--quiet', 'bdist_rpm', '-d','../pkg']
    system(cmd,o.work)
    shutil.rmtree(join(o.work,'dist'))

def debian(o):
    cmd=['sed','-i','1s/^.*$/openerp (%s-%s-1) testing; urgency=low/'%(o.version,o.timestamp),'debian/changelog']
    system(cmd,o.work)
    cmd=['dpkg-buildpackage','-rfakeroot']
    system(cmd,o.work)
    l = glob.glob(join(o.build,'openerp_*'))
    for i in l:
        shutil.move(i,o.pkg)

class KVM(object):
    def __init__(self, o, image, ssh_key=''):
        self.o = o
        self.image = image
        self.ssh_key = ssh_key
        self.login = 'openerp'

    def timeout(self,signum,frame):
        print "vm timeout kill",self.pid
        os.kill(self.pid,15)

    def start(self):
        l="kvm -net nic -net user,hostfwd=tcp:127.0.0.1:10022-:22,hostfwd=tcp:127.0.0.1:18069-:8069,hostfwd=tcp:127.0.0.1:15432-:5432 -drive".split(" ")
        #l.append('file=%s,if=virtio,index=0,boot=on,snapshot=on'%self.image)
        l.append('file=%s,snapshot=on'%self.image)
        #l.extend(['-vnc','127.0.0.1:1'])
        l.append('-nographic')
        print " ".join(l)
        self.pid=os.spawnvp(os.P_NOWAIT, l[0], l)
        time.sleep(5)
        signal.alarm(2400)
        signal.signal(signal.SIGALRM, self.timeout)
        try:
            self.run()
        finally:
            signal.signal(signal.SIGALRM, signal.SIG_DFL)
            os.kill(self.pid,15)
            time.sleep(5)

    def ssh(self,cmd):
        l=['ssh','-o','UserKnownHostsFile=/dev/null','-o','StrictHostKeyChecking=no','-p','10022','-i',self.ssh_key,'%s@127.0.0.1'%self.login,cmd]
        system(l)

    def rsync(self,args,options='--delete --exclude .bzrignore'):
        cmd ='rsync -rtv -e "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 10022 -i %s" %s %s' % (self.ssh_key, options, args)
        system(cmd)

    def run(self):
        pass

class KVMDebianTestTgz(KVM):
    def run(self):
        l = glob.glob(join(self.o.pkg,'*%s.tar.gz'%self.o.timestamp))
        self.rsync('%s openerp@127.0.0.1:src/'%l[0])
        script = """
            tar xzvf src/*.tar.gz
            cd openerp*
            sudo python setup.py install
            sudo su - postgres -c "createuser -s $USER"
            createdb t1
            openerp-server --stop-after-init -d t1 -i ` python -c "import os;print ','.join([i for i in os.listdir('openerp/addons') if i not in ['auth_openid','caldav','document_ftp','base_gengo']]),;" `
        """
        self.ssh(script)
        self.ssh('nohup openerp-server >/dev/null 2>&1 &')
        time.sleep(5)
        l = xmlrpclib.ServerProxy('http://127.0.0.1:18069/xmlrpc/object').execute('t1',1,'admin','ir.module.module','search',[('state','=','installed')])
        i = len(l)
        if i >= 190:
            print "Tgz install: ",i," module installed"
        else:
            raise Exception("Tgz install failed only %s installed"%i)
        time.sleep(2)

class KVMDebianTestDeb(KVM):
    def run(self):
        l = glob.glob(join(self.o.pkg,'*%s*.deb'%self.o.timestamp))
        self.rsync('%s openerp@127.0.0.1:deb/'%l[0])
        script = """
            sudo dpkg -i deb/*
            sudo su - postgres -c "createuser -s $USER"
            createdb t1
            openerp-server --stop-after-init -d t1 -i base
        """
        #` python -c "import os;print ','.join([i for i in os.listdir('/usr/share/pyshared/openerp/addons') if i not in ['auth_openid','caldav','document_ftp']]),;" `
        self.ssh(script)
        time.sleep(5)
        l = xmlrpclib.ServerProxy('http://127.0.0.1:18069/xmlrpc/object').execute('t1',1,'admin','ir.module.module','search',[('state','=','installed')])
        i = len(l)
        if i >= 1:
            print "Deb install: ",i," module installed"
        else:
            raise Exception("Tgz install failed only %s installed"%i)
        time.sleep(2)

class KVMWinBuildExe(KVM):
    def run(self):
        self.login = 'Naresh'
        self.ssh("mkdir -p build")
        self.rsync('%s/ Naresh@127.0.0.1:build/server/'%self.o.work)
        f = open('windows/Makefile.version','w')
        f.write("VERSION=%s\n" % self.o.version_full)
        f.close()
        self.rsync('windows/ Naresh@127.0.0.1:build/windows/')
        self.rsync('windows/wkhtmltopdf/ Naresh@127.0.0.1:build/server/win32/wkhtmltopdf/')
        self.ssh("cd build/windows;time make allinone;")
        self.rsync('Naresh@127.0.0.1:build/windows/files/ %s/'%self.o.pkg,'')
        print "KVMWinBuildExe.run(): done"

class KVMWinTestExe(KVM):
    def run(self):
        self.login = 'Naresh'
        setuppath = "%s/openerp-allinone-setup-%s.exe" % (self.o.pkg, self.o.version_full)
        setuplog = setuppath.replace('exe','log')
        self.rsync('"%s" Naresh@127.0.0.1:'%setuppath)
        self.ssh("TEMP=/tmp ./openerp-allinone-setup-%s.exe /S" % self.o.version_full)
        self.ssh('ls /cygdrive/c/"Program Files"/"OpenERP %s"/' % self.o.version_full)
        self.rsync('"repo/lpopenerp_openobject-addons_7.0/" Naresh@127.0.0.1:/cygdrive/c/Program?Files/OpenERP?%s/Server/server/openerp/addons/' % self.o.version_full, options='--exclude .bzrignore --exclude .bzr')
        self.ssh('ls /cygdrive/c/"Program Files"/"OpenERP %s"/Server/server/openerp/addons/' % self.o.version_full)

        self.ssh('PGPASSWORD=openpgpwd /cygdrive/c/"Program Files"/"OpenERP %s"/PostgreSQL/bin/createdb.exe -e -U openpg pack' % self.o.version_full)
        self.ssh('net stop "PostgreSQL_For_OpenERP"')
        self.ssh('net start "PostgreSQL_For_OpenERP"')
        self.ssh('mkdir test-reports')

        self.ssh('/cygdrive/c/"Program Files"/"OpenERP %s"/Server/server/openerp-server.exe -d pack -i base,report_webkit,product --stop-after-init --test-enable --log-level=test --test-report-directory=test-reports'%self.o.version_full)
        self.rsync('Naresh@127.0.0.1:/cygdrive/c/Program?Files/OpenERP?%s/Server/server/openerp-server.log "%s"'%(self.o.version_full, setuplog))
        self.rsync('Naresh@127.0.0.1:test-reports/ "%s"' % self.o.pkg, options='')

def publish_move(o,srcs,dest):
    for i in srcs:
        shutil.move(i,dest)
        # do the symlink
        bn = os.path.basename(i)
        latest = bn.replace(o.timestamp,'latest')
        latest_full = join(dest,latest)
        if bn != latest:
            if os.path.islink(latest_full):
                os.unlink(latest_full)
            os.symlink(bn,latest_full)

def publish(o):
    l = glob.glob(join(o.pkg,'*%s-1*.rpm'%o.timestamp.replace('-','_')))
    publish_move(o,l,join(o.pub,'rpm'))

    l = glob.glob(join(o.pkg,'*_*%s-1*'%o.timestamp))
    publish_move(o,l,join(o.pub,'deb'))
    system('dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz',join(o.pub,'deb'))

    l = glob.glob(join(o.pkg,'*%s*.tar.gz'%o.timestamp))
    publish_move(o,l,join(o.pub,'src'))

    l = glob.glob(join(o.pkg,'*all*%s*.exe'%o.timestamp))
    publish_move(o,l,join(o.pub,'exe'))

    l = glob.glob(join(o.pkg,'*client*%s*.exe'%o.timestamp))
    publish_move(o,l,join(o.pub,'exe'))

def cleanup(o):
    shutil.rmtree(o.work)

#----------------------------------------------------------
# Options and Main
#----------------------------------------------------------

def options():
    op = optparse.OptionParser()
    op.add_option("-b", "--build", default='.', help="build directory (%default)", metavar="DIR")
    op.add_option("-v", "--version", default='7.0', help="version (%default)")
    op.add_option("-a", "--addons-branch", default='lp:~openerp/openobject-addons/7.0', help="%default")
    op.add_option("-s", "--server-branch", default='lp:~openerp/openobject-server/7.0', help="%default")
    op.add_option("-w", "--web-branch", default='lp:~openerp/openerp-web/7.0', help="%default")
    op.add_option("", "--vm-debian-image", default='/home/odoo/vm/debian6/debian6.vmdk', help="%default")
    op.add_option("", "--vm-debian-ssh-key", default='/home/odoo/vm/debian6/debian6_id_rsa', help="%default")
    op.add_option("", "--vm-winxp-image", default='/home/odoo/vm/winxp26/winxp26.vdi', help="%default")
    op.add_option("", "--vm-winxp-ssh-key", default='/home/odoo/vm/winxp26/id_rsa', help="%default")
    (o, args) = op.parse_args()
    # derive other options
    o.timestamp = time.strftime("%Y%m%d-%H%M%S",time.gmtime())
    o.repo = join(o.build, 'repo')
    o.pkg = join(o.build, 'pkg')
    o.pub = join(o.build, 'pub')
    o.version_full = '%s-%s'%(o.version,o.timestamp)
    o.work = join(o.build, 'openerp-%s'%o.version_full)
    o.work_addons = join(o.work, 'openerp', 'addons')
    o.addons_dir = join(o.repo, url2dir(o.addons_branch))
    o.server_dir = join(o.repo, url2dir(o.server_branch))
    o.web_dir = join(o.repo, url2dir(o.web_branch))
    return o

def main():
    o = options()
    init(o)
    update(o)
    rsync(o)
    try:
        version(o)
        # exe
        if os.path.isfile(o.vm_winxp_image):
            KVMWinBuildExe(o, o.vm_winxp_image, o.vm_winxp_ssh_key).start()
            KVMWinTestExe(o, o.vm_winxp_image, o.vm_winxp_ssh_key).start()
        # tgz
        sdist(o)
        if os.path.isfile(o.vm_debian_image):
            KVMDebianTestTgz(o, o.vm_debian_image, o.vm_debian_ssh_key).start()
        # rpm
        bdist_rpm(o)
        # deb
        debian(o)
        if os.path.isfile(o.vm_debian_image):
            KVMDebianTestDeb(o, o.vm_debian_image, o.vm_debian_ssh_key).start()
        # publish
        publish(o)
    finally:
        cleanup(o)

if __name__ == '__main__':
    main()