1 from git
import Repo
as gitrepo
2 from shutil
import rmtree
7 from charmhelpers
.core
import host
8 from charmhelpers
.core
import hookenv
9 from charmhelpers
.core
import templating
10 from charmhelpers
.core
.unitdata
import kv
11 from charmhelpers
.core
.hookenv
import (
18 from charmhelpers
.core
.host
import (
22 from charms
.reactive
import (
31 INSTALL_PATH
= '/opt/openmano'
35 @when('openmano.installed', 'openmano.available')
36 def openmano_available(openmano
):
37 # TODO make this configurable via charm config
38 openmano
.configure(port
=9090)
41 @when('openvim-controller.available',
47 def openvim_available(openvim
, db
):
48 for service
in openvim
.services():
49 for endpoint
in service
['hosts']:
50 host
= endpoint
['hostname']
51 port
= endpoint
['port']
52 user
= endpoint
['user']
54 openvim_uri
= '{}:{}'.format(host
, port
)
55 if kvdb
.get('openvim_uri') == openvim_uri
:
58 # TODO: encapsulate the logic in create-datacenter.sh into python
60 cmd
= './scripts/create-datacenter.sh {} {} {} {}'.format(
61 host
, port
, user
, kvdb
.get('openmano-tenant'))
63 except subprocess
.CalledProcessError
as e
:
64 # Ignore the error if the datacenter already exists.
65 if e
.returncode
!= 153:
68 kvdb
.set('openvim_uri', openvim_uri
)
69 if not is_state('db.available'):
70 status_set('waiting', 'Waiting for database')
75 @when('openmano.installed',
77 'openvim-controller.available')
78 @when_not('openmano.running')
80 # TODO: if the service fails to start, we should raise an error to the op
81 # Right now, it sets the state as running and the charm dies. Because
82 # service-openmano returns 0 when it fails.
83 cmd
= "/home/{}/bin/service-openmano start".format(USER
)
86 if not kvdb
.get('openmano-tenant'):
87 out
, err
= _run('./scripts/create-tenant.sh')
88 kvdb
.set('openmano-tenant', out
.strip())
92 'Up on {host}:{port}'.format(
93 host
=hookenv
.unit_public_ip(),
96 set_state('openmano.running')
99 @when('db.available', 'openmano.installed')
100 @when_not('db.installed')
102 """Setup the database
105 db_uri
= 'mysql://{}:{}@{}:{}/{}'.format(
113 if kvdb
.get('db_uri') == db_uri
:
114 # We're already configured
117 status_set('maintenance', 'Initializing database')
120 # HACK: use a packed version of init_mano_db until bug https://osm.etsi.org/bugzilla/show_bug.cgi?id=56 is fixed
121 # cmd = "{}/database_utils/init_mano_db.sh --createdb ".format(kvdb.get('repo'))
122 cmd
= "./scripts//init_mano_db.sh --createdb "
123 cmd
+= "-u {} -p{} -h {} -d {} -P {}".format(
130 output
, err
= _run(cmd
)
131 except subprocess
.CalledProcessError
:
132 # Eat this. init_mano_db.sh will return error code 1 on success
137 'password': db
.password(),
139 'database': db
.database(),
144 os
.path
.join(kvdb
.get('repo'), 'openmanod.cfg'),
149 kvdb
.set('db_uri', db_uri
)
151 status_set('active', 'Database installed.')
152 set_state('db.installed')
155 @when_not('openvim-controller.available')
157 status_set('waiting', 'Waiting for OpenVIM')
160 @when_not('db.available')
162 status_set('waiting', 'Waiting for database')
165 @when_not('db.available')
166 @when_not('openvim-controller.available')
167 def need_everything():
168 status_set('waiting', 'Waiting for database and OpenVIM')
171 @when_not('openmano.installed')
172 def install_layer_openmano():
173 status_set('maintenance', 'Installing')
177 # TODO change user home
178 # XXX security issue!
179 host
.adduser(USER
, password
=USER
)
181 if os
.path
.isdir(INSTALL_PATH
):
187 branch
=cfg
['branch'],
198 os
.mkdir(os
.path
.join(INSTALL_PATH
, 'logs'))
199 chownr(INSTALL_PATH
, USER
, USER
)
200 kvdb
.set('repo', INSTALL_PATH
)
202 os
.mkdir('/home/{}/bin'.format(USER
))
205 "{}/openmano".format(INSTALL_PATH
),
206 "/home/{}/bin/openmano".format(USER
))
208 "{}/scripts/openmano-report.sh".format(INSTALL_PATH
),
209 "/home/{}/bin/openmano-report.sh".format(USER
))
211 "{}/scripts/service-openmano.sh".format(INSTALL_PATH
),
212 "/home/{}/bin/service-openmano".format(USER
))
215 set_state('openmano.installed')
218 def _run(cmd
, env
=None):
219 if isinstance(cmd
, str):
220 cmd
= cmd
.split() if ' ' in cmd
else [cmd
]
223 p
= subprocess
.Popen(cmd
,
225 stdout
=subprocess
.PIPE
,
226 stderr
=subprocess
.PIPE
)
227 stdout
, stderr
= p
.communicate()
230 raise subprocess
.CalledProcessError(
233 output
=stderr
.decode("utf-8").strip())
234 return (stdout
.decode('utf-8'), stderr
.decode('utf-8'))