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')
36 @when('openmano.available')
37 def openmano_available(openmano
):
38 # TODO make this configurable via charm config
39 openmano
.configure(port
=9090)
42 @when('openmano.installed')
43 @when('db.available', 'db.installed')
44 @when('openvim-controller.available')
45 @when('openmano.running')
46 def openvim_available(openvim
, db
):
47 for service
in openvim
.services():
48 for endpoint
in service
['hosts']:
49 host
= endpoint
['hostname']
50 port
= endpoint
['port']
51 user
= endpoint
['user']
53 openvim_uri
= '{}:{}'.format(host
, port
)
54 if kvdb
.get('openvim_uri') == openvim_uri
:
57 # TODO: encapsulate the logic in create-datacenter.sh into python
59 cmd
= './scripts/create-datacenter.sh {} {} {} {}'.format(
60 host
, port
, user
, kvdb
.get('openmano-tenant'))
62 except subprocess
.CalledProcessError
as e
:
63 # Ignore the error if the datacenter already exists.
64 if e
.returncode
!= 153:
67 kvdb
.set('openvim_uri', openvim_uri
)
68 if not is_state('db.available'):
69 status_set('waiting', 'Waiting for database')
74 @when('openmano.installed')
75 @when('db.available', 'db.installed')
76 @when('openvim-controller.available')
77 @when_not('openmano.running')
79 # TODO: if the service fails to start, we should raise an error to the op
80 # Right now, it sets the state as running and the charm dies. Because
81 # service-openmano returns 0 when it fails.
82 cmd
= "/home/{}/bin/service-openmano start".format(USER
)
85 if not kvdb
.get('openmano-tenant'):
86 out
, err
= _run('./scripts/create-tenant.sh')
87 kvdb
.set('openmano-tenant', out
.strip())
91 'Up on {host}:{port}'.format(
92 host
=hookenv
.unit_public_ip(),
95 set_state('openmano.running')
98 @when('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')
154 @when_not('openvim-controller.available')
156 status_set('waiting', 'Waiting for OpenVIM')
159 @when_not('db.available')
161 status_set('waiting', 'Waiting for database')
164 @when_not('db.available')
165 @when_not('openvim-controller.available')
166 def need_everything():
167 status_set('waiting', 'Waiting for database and OpenVIM')
170 @when_not('openmano.installed')
171 def install_layer_openmano():
172 status_set('maintenance', 'Installing')
176 # TODO change user home
177 # XXX security issue!
178 host
.adduser(USER
, password
=USER
)
180 if os
.path
.isdir(INSTALL_PATH
):
186 branch
=cfg
['branch'],
197 os
.mkdir(os
.path
.join(INSTALL_PATH
, 'logs'))
198 chownr(INSTALL_PATH
, USER
, USER
)
199 kvdb
.set('repo', INSTALL_PATH
)
201 os
.mkdir('/home/{}/bin'.format(USER
))
204 "{}/openmano".format(INSTALL_PATH
),
205 "/home/{}/bin/openmano".format(USER
))
207 "{}/scripts/openmano-report.sh".format(INSTALL_PATH
),
208 "/home/{}/bin/openmano-report.sh".format(USER
))
210 "{}/scripts/service-openmano.sh".format(INSTALL_PATH
),
211 "/home/{}/bin/service-openmano".format(USER
))
214 set_state('openmano.installed')
217 def _run(cmd
, env
=None):
218 if isinstance(cmd
, str):
219 cmd
= cmd
.split() if ' ' in cmd
else [cmd
]
222 p
= subprocess
.Popen(cmd
,
224 stdout
=subprocess
.PIPE
,
225 stderr
=subprocess
.PIPE
)
226 stdout
, stderr
= p
.communicate()
229 raise subprocess
.CalledProcessError(
232 output
=stderr
.decode("utf-8").strip())
233 return (stdout
.decode('utf-8'), stderr
.decode('utf-8'))