Internet of Things resource management service for OpenStack clouds.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

817 lines
29KB

  1. # Copyright 2017 MDSLAB - University of Messina
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. try:
  16. # allow iotronic api to run also with python3
  17. import _pickle as cpickle
  18. except Exception:
  19. # allow iotronic api to run also with python2.7
  20. import pickle as cpickle
  21. from iotronic.common import exception, designate
  22. from iotronic.common import neutron
  23. from iotronic.common import states
  24. from iotronic.conductor.provisioner import Provisioner
  25. from iotronic import objects
  26. from iotronic.objects import base as objects_base
  27. from iotronic.wamp import wampmessage as wm
  28. from oslo_config import cfg
  29. from oslo_log import log as logging
  30. import oslo_messaging
  31. import random
  32. import socket
  33. LOG = logging.getLogger(__name__)
  34. serializer = objects_base.IotronicObjectSerializer()
  35. Port = list()
  36. def get_best_agent(ctx):
  37. agents = objects.WampAgent.list(ctx, filters={'online': True})
  38. LOG.debug('found %d Agent(s).', len(agents))
  39. agent = random.choice(agents)
  40. LOG.debug('Selected agent: %s', agent.hostname)
  41. return agent.hostname
  42. def random_public_port():
  43. return random.randint(50001, 59999)
  44. def manage_result(res, wamp_rpc_call, board_uuid):
  45. if res.result == wm.SUCCESS:
  46. return res.message
  47. elif res.result == wm.WARNING:
  48. LOG.warning('Warning in the execution of %s on %s', wamp_rpc_call,
  49. board_uuid)
  50. return res.message
  51. elif res.result == wm.ERROR:
  52. LOG.error('Error in the execution of %s on %s: %s', wamp_rpc_call,
  53. board_uuid, res.message)
  54. raise exception.ErrorExecutionOnBoard(call=wamp_rpc_call,
  55. board=board_uuid,
  56. error=res.message)
  57. return res.message
  58. def create_record_dns_webservice(ctx, board, webs_name, board_dns, zone):
  59. agent = objects.WampAgent.get_by_hostname(ctx, board.agent)
  60. wsurl = agent.wsurl
  61. w_host = wsurl.split("//")[1].split(":")[0]
  62. ip = socket.gethostbyname(w_host)
  63. LOG.debug('Create dns record %s for board %s',
  64. webs_name + "." + board_dns + "." + zone,
  65. board.uuid)
  66. LOG.debug('using %s %s %s', webs_name + "." + board_dns,
  67. ip, zone)
  68. designate.create_record(webs_name + "." + board_dns, ip,
  69. zone)
  70. def create_record_dns(ctx, board, board_dns, zone):
  71. agent = objects.WampAgent.get_by_hostname(ctx, board.agent)
  72. wsurl = agent.wsurl
  73. w_host = wsurl.split("//")[1].split(":")[0]
  74. ip = socket.gethostbyname(w_host)
  75. LOG.debug('Create dns record %s for board %s',
  76. board_dns + "." + zone,
  77. board.uuid)
  78. LOG.debug('using %s %s %s', board_dns,
  79. ip, zone)
  80. designate.create_record(board_dns, ip,
  81. zone)
  82. LOG.debug('Configure Web Proxy on WampAgent %s (%s) for board %s',
  83. board.agent, ip, board.uuid)
  84. class ConductorEndpoint(object):
  85. def __init__(self, ragent):
  86. transport = oslo_messaging.get_transport(cfg.CONF)
  87. self.target = oslo_messaging.Target()
  88. self.wamp_agent_client = oslo_messaging.RPCClient(transport,
  89. self.target)
  90. self.wamp_agent_client = self.wamp_agent_client.prepare(timeout=120,
  91. topic='s4t')
  92. self.ragent = ragent
  93. def echo(self, ctx, data):
  94. LOG.info("ECHO: %s" % data)
  95. return data
  96. def registration(self, ctx, code, session_num):
  97. LOG.debug('Received registration from %s with session %s',
  98. code, session_num)
  99. try:
  100. board = objects.Board.get_by_code(ctx, code)
  101. except Exception as exc:
  102. msg = exc.message % {'board': code}
  103. LOG.error(msg)
  104. return wm.WampError(msg).serialize()
  105. try:
  106. old_ses = objects.SessionWP(ctx)
  107. old_ses = old_ses.get_session_by_board_uuid(ctx, board.uuid,
  108. valid=True)
  109. old_ses.valid = False
  110. old_ses.save()
  111. except Exception:
  112. LOG.debug('valid session for %s not found', board.uuid)
  113. session_data = {'board_id': board.id,
  114. 'board_uuid': board.uuid,
  115. 'session_id': session_num}
  116. session = objects.SessionWP(ctx, **session_data)
  117. session.create()
  118. if not board.status == states.REGISTERED:
  119. msg = "board with code %(board)s " \
  120. "already registered" % {'board': code}
  121. LOG.warning((msg))
  122. board.status = states.OFFLINE
  123. board.save()
  124. LOG.debug('sending this conf %s', board.config)
  125. wmessage = wm.WampSuccess(board.config)
  126. return wmessage.serialize()
  127. board.agent = get_best_agent(ctx)
  128. agent = objects.WampAgent.get_by_hostname(ctx, board.agent)
  129. prov = Provisioner(board)
  130. prov.conf_registration_agent(self.ragent.wsurl)
  131. prov.conf_main_agent(agent.wsurl)
  132. loc = objects.Location.list_by_board_uuid(ctx, board.uuid)[0]
  133. prov.conf_location(loc)
  134. board.config = prov.get_config()
  135. board.status = states.OFFLINE
  136. board.save()
  137. LOG.debug('sending this conf %s', board.config)
  138. wmessage = wm.WampSuccess(board.config)
  139. return wmessage.serialize()
  140. def destroy_board(self, ctx, board_id):
  141. LOG.info('Destroying board with id %s',
  142. board_id)
  143. board = objects.Board.get_by_uuid(ctx, board_id)
  144. result = None
  145. if board.is_online():
  146. prov = Provisioner()
  147. prov.conf_clean()
  148. p = prov.get_config()
  149. LOG.debug('sending this conf %s', p)
  150. try:
  151. result = self.execute_on_board(ctx,
  152. board_id,
  153. 'destroyBoard',
  154. (p,))
  155. except exception:
  156. return exception
  157. board.destroy()
  158. if result:
  159. result = manage_result(result, 'destroyBoard', board_id)
  160. LOG.debug(result)
  161. return result
  162. return
  163. def update_board(self, ctx, board_obj):
  164. board = serializer.deserialize_entity(ctx, board_obj)
  165. LOG.debug('Updating board %s', board.name)
  166. board.save()
  167. return serializer.serialize_entity(ctx, board)
  168. def create_board(self, ctx, board_obj, location_obj):
  169. new_board = serializer.deserialize_entity(ctx, board_obj)
  170. LOG.debug('Creating board %s',
  171. new_board.name)
  172. new_location = serializer.deserialize_entity(ctx, location_obj)
  173. new_board.create()
  174. new_location.board_id = new_board.id
  175. new_location.create()
  176. return serializer.serialize_entity(ctx, new_board)
  177. def execute_on_board(self, ctx, board_uuid, wamp_rpc_call, wamp_rpc_args):
  178. LOG.debug('Executing \"%s\" on the board: %s',
  179. wamp_rpc_call, board_uuid)
  180. board = objects.Board.get_by_uuid(ctx, board_uuid)
  181. # session should be taken from board, not from the object
  182. session = objects.SessionWP.get_session_by_board_uuid(ctx, board_uuid)
  183. full_wamp_call = 'iotronic.' + \
  184. session.session_id + "." + \
  185. board.uuid + "." + wamp_rpc_call
  186. # check the session; it rise an excpetion if session miss
  187. if not board.is_online():
  188. raise exception.BoardNotConnected(board=board.uuid)
  189. cctx = self.wamp_agent_client.prepare(server=board.agent)
  190. res = cctx.call(ctx, 's4t_invoke_wamp',
  191. wamp_rpc_call=full_wamp_call,
  192. data=wamp_rpc_args)
  193. res = wm.deserialize(res)
  194. return res
  195. def destroy_plugin(self, ctx, plugin_id):
  196. LOG.info('Destroying plugin with id %s',
  197. plugin_id)
  198. plugin = objects.Plugin.get_by_uuid(ctx, plugin_id)
  199. plugin.destroy()
  200. return
  201. def update_plugin(self, ctx, plugin_obj):
  202. plugin = serializer.deserialize_entity(ctx, plugin_obj)
  203. LOG.debug('Updating plugin %s', plugin.name)
  204. plugin.save()
  205. return serializer.serialize_entity(ctx, plugin)
  206. def create_plugin(self, ctx, plugin_obj):
  207. new_plugin = serializer.deserialize_entity(ctx, plugin_obj)
  208. LOG.debug('Creating plugin %s',
  209. new_plugin.name)
  210. new_plugin.code = cpickle.dumps(new_plugin.code, 0).decode()
  211. new_plugin.create()
  212. return serializer.serialize_entity(ctx, new_plugin)
  213. def inject_plugin(self, ctx, plugin_uuid, board_uuid, onboot):
  214. LOG.info('Injecting plugin with id %s into the board %s',
  215. plugin_uuid, board_uuid)
  216. plugin = objects.Plugin.get(ctx, plugin_uuid)
  217. try:
  218. result = self.execute_on_board(ctx,
  219. board_uuid,
  220. 'PluginInject',
  221. (plugin, onboot))
  222. except exception:
  223. return exception
  224. injection = None
  225. try:
  226. injection = objects.InjectionPlugin.get(ctx,
  227. board_uuid,
  228. plugin_uuid)
  229. except Exception:
  230. pass
  231. if injection:
  232. injection.status = 'updated'
  233. injection.save()
  234. else:
  235. inj_data = {
  236. 'board_uuid': board_uuid,
  237. 'plugin_uuid': plugin_uuid,
  238. 'onboot': onboot,
  239. 'status': 'injected'
  240. }
  241. injection = objects.InjectionPlugin(ctx, **inj_data)
  242. injection.create()
  243. result = manage_result(result, 'PluginInject', board_uuid)
  244. LOG.debug(result)
  245. return result
  246. def remove_plugin(self, ctx, plugin_uuid, board_uuid):
  247. LOG.info('Removing plugin with id %s into the board %s',
  248. plugin_uuid, board_uuid)
  249. plugin = objects.Plugin.get_by_uuid(ctx, plugin_uuid)
  250. injection = objects.InjectionPlugin.get(ctx, board_uuid, plugin_uuid)
  251. try:
  252. result = self.execute_on_board(ctx, board_uuid, 'PluginRemove',
  253. (plugin.uuid,))
  254. except exception:
  255. return exception
  256. result = manage_result(result, 'PluginRemove', board_uuid)
  257. LOG.debug(result)
  258. injection.destroy()
  259. return result
  260. def action_plugin(self, ctx, plugin_uuid, board_uuid, action, params):
  261. LOG.info('Calling plugin with id %s into the board %s with params %s',
  262. plugin_uuid, board_uuid, params)
  263. plugin = objects.Plugin.get(ctx, plugin_uuid)
  264. objects.plugin.is_valid_action(action)
  265. try:
  266. if objects.plugin.want_params(action):
  267. result = self.execute_on_board(ctx, board_uuid, action,
  268. (plugin.uuid, params))
  269. else:
  270. result = self.execute_on_board(ctx, board_uuid, action,
  271. (plugin.uuid,))
  272. except exception:
  273. return exception
  274. result = manage_result(result, action, board_uuid)
  275. LOG.debug(result)
  276. return result
  277. def create_service(self, ctx, service_obj):
  278. new_service = serializer.deserialize_entity(ctx, service_obj)
  279. LOG.debug('Creating service %s',
  280. new_service.name)
  281. new_service.create()
  282. return serializer.serialize_entity(ctx, new_service)
  283. def destroy_service(self, ctx, service_id):
  284. LOG.info('Destroying service with id %s',
  285. service_id)
  286. service = objects.Service.get_by_uuid(ctx, service_id)
  287. service.destroy()
  288. return
  289. def update_service(self, ctx, service_obj):
  290. service = serializer.deserialize_entity(ctx, service_obj)
  291. LOG.debug('Updating service %s', service.name)
  292. service.save()
  293. return serializer.serialize_entity(ctx, service)
  294. def action_service(self, ctx, service_uuid, board_uuid, action):
  295. service = objects.Service.get(ctx, service_uuid)
  296. objects.service.is_valid_action(action)
  297. if action == "ServiceEnable":
  298. LOG.info('Enabling service with id %s into the board %s',
  299. service_uuid, board_uuid)
  300. try:
  301. objects.ExposedService.get(ctx,
  302. board_uuid,
  303. service_uuid)
  304. return exception.ServiceAlreadyExposed(uuid=service_uuid)
  305. except Exception:
  306. public_port = random_public_port()
  307. res = self.execute_on_board(ctx, board_uuid, action,
  308. (service, public_port))
  309. result = manage_result(res, action, board_uuid)
  310. exp_data = {
  311. 'board_uuid': board_uuid,
  312. 'service_uuid': service_uuid,
  313. 'public_port': public_port,
  314. }
  315. exposed = objects.ExposedService(ctx, **exp_data)
  316. exposed.create()
  317. LOG.debug(result)
  318. return result
  319. elif action == "ServiceDisable":
  320. LOG.info('Disabling service with id %s into the board %s',
  321. service_uuid, board_uuid)
  322. exposed = objects.ExposedService.get(ctx,
  323. board_uuid,
  324. service_uuid)
  325. res = self.execute_on_board(ctx, board_uuid, action,
  326. (service,))
  327. result = manage_result(res, action, board_uuid)
  328. LOG.debug(res.message)
  329. exposed.destroy()
  330. return result
  331. elif action == "ServiceRestore":
  332. LOG.info('Restoring service with id %s into the board %s',
  333. service_uuid, board_uuid)
  334. exposed = objects.ExposedService.get(ctx, board_uuid,
  335. service_uuid)
  336. res = self.execute_on_board(ctx, board_uuid, action,
  337. (service, exposed.public_port))
  338. result = manage_result(res, action, board_uuid)
  339. LOG.debug(result)
  340. return result
  341. def restore_services_on_board(self, ctx, board_uuid):
  342. LOG.info('Restoring the services into the board %s',
  343. board_uuid)
  344. exposed_list = objects.ExposedService.get_by_board_uuid(ctx,
  345. board_uuid)
  346. for exposed in exposed_list:
  347. service = objects.Service.get_by_uuid(ctx, exposed.service_uuid)
  348. self.execute_on_board(ctx, board_uuid, "ServiceRestore",
  349. (service, exposed.public_port))
  350. return 0
  351. def create_port_on_board(self, ctx, board_uuid, network_uuid,
  352. subnet_uuid, security_groups=None):
  353. LOG.info('Creation of a port on the board %s in the network',
  354. board_uuid)
  355. board = objects.Board.get_by_uuid(ctx, board_uuid)
  356. port_iotronic = objects.Port(ctx)
  357. subnet_info = neutron.subnet_info(subnet_uuid)
  358. cidr = str(subnet_info['subnet']['cidr'])
  359. slash = cidr.split("/", 1)[1]
  360. try:
  361. port = neutron.add_port_to_network(board, network_uuid,
  362. subnet_uuid, security_groups)
  363. p = str(port['port']['id'])
  364. port_socat = random.randint(10000, 20000)
  365. i = 0
  366. while i < len(Port):
  367. if Port[i] == port_socat:
  368. i = 0
  369. port_socat = random.randint(10000, 20000)
  370. i += 1
  371. global Port
  372. Port.insert(0, port_socat)
  373. r_tcp_port = str(port_socat)
  374. try:
  375. LOG.info('Creation of the VIF on the board')
  376. self.execute_on_board(ctx, board_uuid, "Create_VIF",
  377. (r_tcp_port,))
  378. try:
  379. LOG.debug('starting the wamp client')
  380. cctx = self.wamp_agent_client.prepare(server=board.agent)
  381. cctx.call(ctx, 'create_tap_interface',
  382. port_uuid=p,
  383. tcp_port=r_tcp_port)
  384. try:
  385. LOG.info('Updating the DB')
  386. VIF = str("iotronic" + str(r_tcp_port))
  387. port_iotronic.VIF_name = VIF
  388. port_iotronic.uuid = port['port']['id']
  389. port_iotronic.MAC_add = port['port']['mac_address']
  390. port_iotronic.board_uuid = str(board_uuid)
  391. port_iotronic.network = \
  392. port['port']['fixed_ips'][0]['subnet_id']
  393. port_iotronic.ip = \
  394. port['port']['fixed_ips'][0]['ip_address']
  395. port_iotronic.create()
  396. try:
  397. LOG.debug('Configuration of the VIF')
  398. self.execute_on_board(ctx, board_uuid,
  399. "Configure_VIF",
  400. (port_iotronic, slash,))
  401. return port_iotronic
  402. except Exception:
  403. LOG.error("Error while configuring the VIF")
  404. except Exception as e:
  405. LOG.error('Error while updating the DB :' + str(e))
  406. except Exception:
  407. LOG.error('wamp client error')
  408. except Exception:
  409. LOG.error('Error while creating the VIF')
  410. except Exception as e:
  411. LOG.error(str(e))
  412. def remove_VIF_from_board(self, ctx, board_uuid, port_uuid):
  413. LOG.info('removing the port %s from board %s',
  414. port_uuid, board_uuid)
  415. board = objects.Board.get_by_uuid(ctx, board_uuid)
  416. port = objects.Port.get_by_uuid(ctx, port_uuid)
  417. VIF_name = str(port.VIF_name)
  418. try:
  419. self.execute_on_board(ctx, board_uuid, "Remove_VIF", (VIF_name,))
  420. port_num = int(VIF_name[8:])
  421. global Port
  422. Port.remove(port_num)
  423. try:
  424. LOG.info("Removing the port from Neutron "
  425. "and Iotronic databases")
  426. neutron.delete_port(board.agent, port_uuid)
  427. LOG.info("Port removed from Neutron DB")
  428. port.destroy()
  429. LOG.info("Port removed from Iotronic DB")
  430. return port
  431. except Exception as e:
  432. LOG.error(str(e))
  433. except Exception as e:
  434. LOG.error(str(e))
  435. def create_fleet(self, ctx, fleet_obj):
  436. new_fleet = serializer.deserialize_entity(ctx, fleet_obj)
  437. LOG.debug('Creating fleet %s',
  438. new_fleet.name)
  439. new_fleet.create()
  440. return serializer.serialize_entity(ctx, new_fleet)
  441. def destroy_fleet(self, ctx, fleet_id):
  442. LOG.info('Destroying fleet with id %s',
  443. fleet_id)
  444. fleet = objects.Fleet.get_by_uuid(ctx, fleet_id)
  445. fleet.destroy()
  446. return
  447. def update_fleet(self, ctx, fleet_obj):
  448. fleet = serializer.deserialize_entity(ctx, fleet_obj)
  449. LOG.debug('Updating fleet %s', fleet.name)
  450. fleet.save()
  451. return serializer.serialize_entity(ctx, fleet)
  452. def create_webservice(self, ctx, webservice_obj):
  453. newwbs = serializer.deserialize_entity(ctx, webservice_obj)
  454. LOG.debug('Creating webservice %s',
  455. newwbs.name)
  456. en_webservice = objects.enabledwebservice. \
  457. EnabledWebservice.get_by_board_uuid(ctx,
  458. newwbs.board_uuid)
  459. full_zone_domain = en_webservice.dns + "." + en_webservice.zone
  460. dns_domain = newwbs.name + "." + full_zone_domain
  461. board = objects.Board.get_by_uuid(ctx, newwbs.board_uuid)
  462. create_record_dns_webservice(ctx, board, newwbs.name,
  463. en_webservice.dns,
  464. en_webservice.zone)
  465. LOG.debug('Creating webservice with full domain %s',
  466. dns_domain)
  467. list_webs = objects.Webservice.list(ctx,
  468. filters={
  469. 'board_uuid': newwbs.board_uuid
  470. })
  471. list_dns = full_zone_domain + ","
  472. for webs in list_webs:
  473. dname = webs.name + "." + full_zone_domain + ","
  474. list_dns = list_dns + dname
  475. list_dns = list_dns + dns_domain
  476. try:
  477. self.execute_on_board(ctx,
  478. newwbs.board_uuid,
  479. 'ExposeWebservice',
  480. (full_zone_domain,
  481. dns_domain,
  482. newwbs.port,
  483. list_dns,))
  484. except exception:
  485. return exception
  486. cctx = self.wamp_agent_client.prepare(server=board.agent)
  487. cctx.call(ctx, 'add_redirect', board_dns=en_webservice.dns,
  488. zone=en_webservice.zone, dns=newwbs.name)
  489. cctx.call(ctx, 'reload_proxy')
  490. newwbs.create()
  491. return serializer.serialize_entity(ctx, newwbs)
  492. def destroy_webservice(self, ctx, webservice_id):
  493. LOG.info('Destroying webservice with id %s',
  494. webservice_id)
  495. wbsrv = objects.Webservice.get_by_uuid(ctx, webservice_id)
  496. en_webservice = objects.enabledwebservice. \
  497. EnabledWebservice.get_by_board_uuid(ctx,
  498. wbsrv.board_uuid)
  499. full_zone_domain = en_webservice.dns + "." + en_webservice.zone
  500. dns_domain = wbsrv.name + "." + full_zone_domain
  501. list_webs = objects.Webservice.list(ctx,
  502. filters={
  503. 'board_uuid': wbsrv.board_uuid
  504. })
  505. list_dns = full_zone_domain + ","
  506. for webs in list_webs:
  507. if webs.name != wbsrv.name:
  508. dname = webs.name + "." + full_zone_domain + ","
  509. list_dns = list_dns + dname
  510. list_dns = list_dns[:-1]
  511. try:
  512. # result =
  513. self.execute_on_board(ctx,
  514. wbsrv.board_uuid,
  515. 'UnexposeWebservice',
  516. (dns_domain, list_dns,))
  517. except exception:
  518. return exception
  519. board = objects.Board.get_by_uuid(ctx, wbsrv.board_uuid)
  520. if board.agent == None:
  521. raise exception.BoardInvalidStatus(uuid=board.uuid,
  522. status=board.status)
  523. cctx = self.wamp_agent_client.prepare(server=board.agent)
  524. cctx.call(ctx, 'remove_redirect', board_dns=en_webservice.dns,
  525. zone=en_webservice.zone, dns=wbsrv.name)
  526. cctx.call(ctx, 'reload_proxy')
  527. wbsrv.destroy()
  528. designate.delete_record(wbsrv.name + "." + en_webservice.dns,
  529. en_webservice.zone)
  530. return
  531. def enable_webservice(self, ctx, dns, zone, email, board_uuid):
  532. board = objects.Board.get_by_uuid(ctx, board_uuid)
  533. if board.agent == None:
  534. raise exception.BoardInvalidStatus(uuid=board.uuid,
  535. status=board.status)
  536. try:
  537. create_record_dns(ctx, board, dns, zone)
  538. except exception:
  539. return exception
  540. try:
  541. en_webservice = objects.enabledwebservice. \
  542. EnabledWebservice.get_by_board_uuid(ctx,
  543. board.uuid)
  544. LOG.debug('Webservice data already exists for board %s',
  545. board.uuid)
  546. https_port = en_webservice.https_port
  547. http_port = en_webservice.http_port
  548. except Exception:
  549. # TO BE CHANGED
  550. https_port = random_public_port()
  551. http_port = random_public_port()
  552. en_webservice = {
  553. 'board_uuid': board.uuid,
  554. 'http_port': http_port,
  555. 'https_port': https_port,
  556. 'dns': dns,
  557. 'zone': zone
  558. }
  559. en_webservice = objects.enabledwebservice.EnabledWebservice(
  560. ctx, **en_webservice)
  561. LOG.debug('Save webservice data %s for board %s', dns + "." + zone,
  562. board.uuid)
  563. en_webservice.create()
  564. LOG.debug('Open ports on WampAgent %s for http and %s for https '
  565. 'on board %s', http_port, https_port, board.uuid)
  566. service = objects.Service.get_by_name(ctx, 'webservice')
  567. res = self.execute_on_board(ctx, board.uuid, "ServiceEnable",
  568. (service, http_port))
  569. result = manage_result(res, "ServiceEnable", board.uuid)
  570. exp_data = {
  571. 'board_uuid': board_uuid,
  572. 'service_uuid': service.uuid,
  573. 'public_port': http_port,
  574. }
  575. exposed = objects.ExposedService(ctx, **exp_data)
  576. exposed.create()
  577. LOG.debug(result)
  578. service = objects.Service.get_by_name(ctx, 'webservice_ssl')
  579. res = self.execute_on_board(ctx, board.uuid, "ServiceEnable",
  580. (service, https_port))
  581. result = manage_result(res, "ServiceEnable", board.uuid)
  582. exp_data = {
  583. 'board_uuid': board_uuid,
  584. 'service_uuid': service.uuid,
  585. 'public_port': https_port,
  586. }
  587. exposed = objects.ExposedService(ctx, **exp_data)
  588. exposed.create()
  589. LOG.debug(result)
  590. cctx = self.wamp_agent_client.prepare(server=board.agent)
  591. cctx.call(ctx, 'enable_webservice', board=dns,
  592. https_port=https_port, http_port=http_port, zone=zone)
  593. LOG.debug('Configure Web Proxy on Board %s with dns %s (email: %s) ',
  594. board.uuid, dns, email)
  595. try:
  596. # result =
  597. self.execute_on_board(ctx,
  598. board.uuid,
  599. 'EnableWebService',
  600. (dns + "." + zone, email,))
  601. except exception:
  602. return exception
  603. cctx.call(ctx, 'add_redirect', board_dns=dns, zone=zone)
  604. cctx.call(ctx, 'reload_proxy')
  605. return serializer.serialize_entity(ctx, en_webservice)
  606. def disable_webservice(self, ctx, board_uuid):
  607. LOG.info('Disabling webservice on board id %s',
  608. board_uuid)
  609. board = objects.Board.get_by_uuid(ctx, board_uuid)
  610. if board.agent == None:
  611. raise exception.BoardInvalidStatus(uuid=board.uuid,
  612. status=board.status)
  613. en_webservice = objects.enabledwebservice. \
  614. EnabledWebservice.get_by_board_uuid(ctx,
  615. board.uuid)
  616. https_port = en_webservice.https_port
  617. http_port = en_webservice.http_port
  618. LOG.debug('Disable Webservices ports %s for http and %s for https '
  619. 'on board %s', http_port, https_port, board.uuid)
  620. service = objects.Service.get_by_name(ctx, 'webservice')
  621. exposed = objects.ExposedService.get(ctx,
  622. board_uuid,
  623. service.uuid)
  624. res = self.execute_on_board(ctx, board.uuid, "ServiceDisable",
  625. (service,))
  626. LOG.debug(res.message)
  627. exposed.destroy()
  628. service = objects.Service.get_by_name(ctx, 'webservice_ssl')
  629. exposed = objects.ExposedService.get(ctx,
  630. board_uuid,
  631. service.uuid)
  632. res = self.execute_on_board(ctx, board.uuid, "ServiceDisable",
  633. (service,))
  634. LOG.debug(res.message)
  635. exposed.destroy()
  636. webservice = objects.EnabledWebservice.get_by_board_uuid(
  637. ctx, board_uuid)
  638. LOG.debug('Remove dns record %s for board %s',
  639. webservice.dns, board.uuid)
  640. designate.delete_record(webservice.dns, en_webservice.zone)
  641. cctx = self.wamp_agent_client.prepare(server=board.agent)
  642. cctx.call(ctx, 'disable_webservice', board=webservice.dns)
  643. # cctx.call(ctx, 'remove_redirect', board_dns=en_webservice.dns,
  644. # zone=en_webservice.zone)
  645. cctx.call(ctx, 'reload_proxy')
  646. webservice.destroy()
  647. return