Newer
Older
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Launch a docker image but bind the local directory inside the container
and define the module path automatically.
"""
import argparse
import logging
from subprocess import call
import sys
import os
import ConfigParser
from netifaces import interfaces, ifaddresses, AF_INET
# TODO auto create list of module
_logger = logging.getLogger(__name__)
__version__ = '0.1.0'
__date__ = '2017-08-11'
__updated__ = '2017-08-11'
def main(argv=None): # IGNORE:C0111
"""Parse arguments and launch conversion
"""
if argv is None:
argv = sys.argv
else:
sys.argv.extend(argv)
program_version = __version__
program_build_date = str(__updated__)
program_version_message = '%%(prog)s %s (%s)' % (
program_version, program_build_date)
program_shortdesc = __doc__.split("\n")[1]
program_license = '''%s
Created by Vincent Hatakeyama on %s.
Copyright 2017 XCG Consulting. All rights reserved.
Licensed under the MIT License
Distributed on an "AS IS" basis without warranties
or conditions of any kind, either express or implied.
USAGE
''' % (
program_shortdesc,
str(__date__),
)
# Argument parsing
parser = argparse.ArgumentParser(
description=program_license,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'-V', '--version', action='version',
version=program_version_message)
parser.add_argument(
'-v', '--verbose', dest='verbose', action='count',
help="set verbosity level [default: %(default)s]")
parser.add_argument(
'--db_user',
help="Database user [default: %(default)s]",
default=None,
)
parser.add_argument(
'--db_password',
help="Database user password [default: %(default)s]",
default=None,
)
# XXX add auto create user and password?
# TODO add a way to add options to docker
# TODO add a way to add arg to odoo
nmspc = parser.parse_args()
verbose = nmspc.verbose
if not verbose:
logging.basicConfig(level=logging.WARN)
if verbose == 1:
logging.basicConfig(level=logging.INFO)
if verbose and verbose > 1:
logging.basicConfig(level=logging.DEBUG)
db_user = nmspc.db_user
db_password = nmspc.db_password
c = ConfigParser.ConfigParser()
# TODO the script assume it is launched from the parent super project
project_path = os.path.realpath('.')
setup_path = 'setup.cfg'
logging.debug('setup file path %s', setup_path)
# TODO test that setup file exists
c.read(setup_path)
addon_dirs = c.get('odoo_scripts', 'addon_dirs', '').split()
logging.debug("addon directories: %s", addon_dirs)
registry = (
c.has_section('odoo_scripts') and
c.has_option('odoo_scripts', 'registry') and
c.get('odoo_scripts', 'registry')) or 'dockerhub.xcg.io'
project = (
c.has_section('odoo_scripts') and
c.has_option('odoo_scripts', 'image') and
c.get('odoo_scripts', 'image')) or os.path.basename(project_path)
image="%s/%s" % (registry, project)
logging.debug("Docker image: %s", image)
# TODO detect if docker image already exists
options = [
'--rm',
'--publish',
'8069:8069',
'--tty',
'--interactive',
]
arg = [
'start',
]
if db_user:
arg.append('--db_user %s' % db_user)
if db_password:
arg.append('--db_password %s' % db_user)
# auto detect local ip
ifaceName='docker0'
addresses = [i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )]
if addresses:
local_ip = addresses[0]
else:
import socket
local_ip = [(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
arg.append('--db_host')
arg.append(local_ip)
# auto detect local conf
local_conf_path = 'conf/dev/odoo.conf'
if os.path.isfile(local_conf_path):
logging.info('Local configuration file found: %s' % local_conf_path)
options.append('--volume')
options.append('%s:/opt/odoo/etc/odoo.conf' % os.path.join(
project_path, local_conf_path))
# volume magic
for addons_dir in addon_dirs:
options.append('--volume')
options.append('%s:/mnt/%s' % (os.path.join(project_path, addons_dir), addons_dir))
all_addons_dir = [
'/opt/odoo/sources/odoo/addons',
]
all_addons_dir.extend('/mnt/%s' % addons_dir for addons_dir in addon_dirs)
arg.append('--addons-path')
arg.append(','.join(all_addons_dir))
cmd = ['docker', 'run']
cmd.extend(options)
cmd.append(image)
cmd.extend(arg)
logging.debug(cmd)
call(cmd)
# TODO add handling of signal to reload
if __name__ == "__main__":
main()