diff --git a/iotronic/conductor/endpoints.py b/iotronic/conductor/endpoints.py index 396a4cf..b30258d 100644 --- a/iotronic/conductor/endpoints.py +++ b/iotronic/conductor/endpoints.py @@ -596,7 +596,13 @@ class ConductorEndpoint(object): except exception: return exception + cctx = self.wamp_agent_client.prepare(server=board.agent) + cctx.call(ctx, 'add_redirect', board_dns=en_webservice.dns, + zone=en_webservice.zone, dns=newwbs.name) + cctx.call(ctx, 'reload_proxy') + newwbs.create() + return serializer.serialize_entity(ctx, newwbs) def destroy_webservice(self, ctx, webservice_id): @@ -632,6 +638,16 @@ class ConductorEndpoint(object): except exception: return exception + board = objects.Board.get_by_uuid(ctx, wbsrv.board_uuid) + if board.agent == None: + raise exception.BoardInvalidStatus(uuid=board.uuid, + status=board.status) + + cctx = self.wamp_agent_client.prepare(server=board.agent) + cctx.call(ctx, 'remove_redirect', board_dns=en_webservice.dns, + zone=en_webservice.zone, dns=wbsrv.name) + cctx.call(ctx, 'reload_proxy') + wbsrv.destroy() designate.delete_record(wbsrv.name + "." + en_webservice.dns, en_webservice.zone) @@ -717,7 +733,6 @@ class ConductorEndpoint(object): cctx = self.wamp_agent_client.prepare(server=board.agent) cctx.call(ctx, 'enable_webservice', board=dns, https_port=https_port, http_port=http_port, zone=zone) - cctx.call(ctx, 'reload_proxy') LOG.debug('Configure Web Proxy on Board %s with dns %s (email: %s) ', board.uuid, dns, email) @@ -731,6 +746,9 @@ class ConductorEndpoint(object): except exception: return exception + cctx.call(ctx, 'add_redirect', board_dns=dns, zone=zone) + cctx.call(ctx, 'reload_proxy') + return serializer.serialize_entity(ctx, en_webservice) def disable_webservice(self, ctx, board_uuid): @@ -784,6 +802,10 @@ class ConductorEndpoint(object): cctx = self.wamp_agent_client.prepare(server=board.agent) cctx.call(ctx, 'disable_webservice', board=webservice.dns) + + # cctx.call(ctx, 'remove_redirect', board_dns=en_webservice.dns, + # zone=en_webservice.zone) + cctx.call(ctx, 'reload_proxy') webservice.destroy() diff --git a/iotronic/wamp/proxies/nginx.py b/iotronic/wamp/proxies/nginx.py index b8ac651..017b902 100644 --- a/iotronic/wamp/proxies/nginx.py +++ b/iotronic/wamp/proxies/nginx.py @@ -71,6 +71,39 @@ def remove(board): ]) +def string_redirect(board, zone, dns=None): + if not dns: + host = "%s.%s" % (board, zone) + else: + host = "%s.%s.%s" % (dns, board, zone) + string = "if ($host = %s) { return 301 https://$host$request_uri; }\n" % ( + host) + return string + + +def insert_entry(line, lines): + try: + lines.index(line) + except Exception: + lines.insert(4, line) + return lines + + +def remove_entry(line, lines): + try: + lines.remove(line) + except Exception: + pass + return lines + + +def save_conf(f_conf, lines): + f = open(f_conf, "w") + lines = "".join(lines) + f.write(lines) + f.close() + + class ProxyManager(Proxy): def __init__(self): @@ -91,3 +124,25 @@ class ProxyManager(Proxy): LOG.debug('Disabling WebService on board %s', board) remove(board) + + def add_redirect(self, ctx, board_dns, zone, dns=None): + line = string_redirect(board_dns, zone, dns) + path = CONF.nginx.nginx_path + "/servers/" + board_dns + LOG.debug('Adding redirect %s on %s', line, path) + + f = open(str(CONF.nginx.nginx_path + "/servers/" + board_dns), "r") + lines = f.readlines() + f.close() + lines = insert_entry(line, lines) + save_conf(path, lines) + + def remove_redirect(self, ctx, board_dns, zone, dns=None): + path = CONF.nginx.nginx_path + "/servers/" + board_dns + line = string_redirect(board_dns, zone, dns) + LOG.debug('Removing redirect %s on %s', line, path) + + f = open(path, "r") + lines = f.readlines() + f.close() + lines = remove_entry(line, lines) + save_conf(path, lines)