Make spotter-appmgr update proxy target IP directly in nginx

This commit is contained in:
Disassembler 2018-03-26 21:12:48 +02:00
parent e52fbfd1f2
commit dd2a936cc7
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
18 changed files with 30 additions and 37 deletions

View File

@ -7,7 +7,6 @@ import os
import subprocess import subprocess
CONF_FILE = '/etc/spotter/apps.json' CONF_FILE = '/etc/spotter/apps.json'
HOSTS_FILE = '/etc/hosts'
ISSUE_FILE = '/etc/issue' ISSUE_FILE = '/etc/issue'
NGINX_DIR = '/etc/nginx/conf.d' NGINX_DIR = '/etc/nginx/conf.d'
@ -19,7 +18,7 @@ NGINX_TEMPLATE = '''server {{
error_log /var/log/nginx/{app}.error.log; error_log /var/log/nginx/{app}.error.log;
location / {{ location / {{
proxy_pass http://{app}:8080; proxy_pass http://{ip}:8080;
}} }}
}} }}
''' '''
@ -103,8 +102,6 @@ class SpotterManager:
self.add_app_to_conf(app, args) self.add_app_to_conf(app, args)
if args.url: if args.url:
self.update_app_conf(app) self.update_app_conf(app)
self.add_app_to_nginx(app)
self.reload_nginx()
def add_app_to_conf(self, app, args): def add_app_to_conf(self, app, args):
self.conf[app] = {} self.conf[app] = {}
@ -123,22 +120,18 @@ class SpotterManager:
host = '{}.{}'.format(app, self.domain) host = '{}.{}'.format(app, self.domain)
subprocess.call([script_path, host, self.port]) subprocess.call([script_path, host, self.port])
def update_proxy(self, app):
self.add_app_to_nginx(app)
self.reload_nginx()
def add_app_to_nginx(self, app): def add_app_to_nginx(self, app):
ip = get_container_ip(app)
with open(os.path.join(NGINX_DIR, '{}.conf'.format(app)), 'w') as f: with open(os.path.join(NGINX_DIR, '{}.conf'.format(app)), 'w') as f:
f.write(NGINX_TEMPLATE.format(app=app, domain=self.domain, port=self.port)) f.write(NGINX_TEMPLATE.format(app=app, ip=ip, domain=self.domain, port=self.port))
def reload_nginx(self): def reload_nginx(self):
subprocess.call(['service', 'nginx', 'reload']) subprocess.call(['service', 'nginx', 'reload'])
def update_hosts(self, app):
with open(HOSTS_FILE, 'r') as f:
lines = f.readlines()
with open(HOSTS_FILE, 'w') as f:
for line in lines:
if not line.strip().endswith(' {}'.format(app)):
f.write(line)
f.write('{} {}\n'.format(get_container_ip(app), app))
def update_domain(self, domain, port): def update_domain(self, domain, port):
self.domain = self.conf["_"]["domain"] = domain self.domain = self.conf["_"]["domain"] = domain
self.port = self.conf["_"]["port"] = port self.port = self.conf["_"]["port"] = port
@ -179,7 +172,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Spotter VM application manager') parser = argparse.ArgumentParser(description='Spotter VM application manager')
subparsers = parser.add_subparsers() subparsers = parser.add_subparsers()
parser_add_app = subparsers.add_parser('add-app', help='Registers a new application and creates hosts and nginx definition for it') parser_add_app = subparsers.add_parser('add-app', help='Registers a new application')
parser_add_app.set_defaults(action='add-app') parser_add_app.set_defaults(action='add-app')
parser_add_app.add_argument('app', help='Application name') parser_add_app.add_argument('app', help='Application name')
parser_add_app.add_argument('url', nargs='?', help='URL to the application. Use "{host}" as a host placeholder') parser_add_app.add_argument('url', nargs='?', help='URL to the application. Use "{host}" as a host placeholder')
@ -187,9 +180,9 @@ if __name__ == '__main__':
parser_add_app.add_argument('password', nargs='?', help='Administrative password') parser_add_app.add_argument('password', nargs='?', help='Administrative password')
parser_add_app.add_argument('-p', '--property', nargs=2, action='append', help='Add arbitrary key-value to the application properties') parser_add_app.add_argument('-p', '--property', nargs=2, action='append', help='Add arbitrary key-value to the application properties')
parser_update_app = subparsers.add_parser('update-hosts', help='Updates hosts definition for application container') parser_update_proxy = subparsers.add_parser('update-proxy', help='Updates nginx proxy target for an application container')
parser_update_app.set_defaults(action='update-hosts') parser_update_proxy.set_defaults(action='update-proxy')
parser_update_app.add_argument('app', help='Application name') parser_update_proxy.add_argument('app', help='Application name')
parser_update_domain = subparsers.add_parser('update-domain', help='Rebuilds domain structure of VM with new domain name and new HTTPS port') parser_update_domain = subparsers.add_parser('update-domain', help='Rebuilds domain structure of VM with new domain name and new HTTPS port')
parser_update_domain.set_defaults(action='update-domain') parser_update_domain.set_defaults(action='update-domain')
@ -200,7 +193,7 @@ if __name__ == '__main__':
sm = SpotterManager() sm = SpotterManager()
if args.action == 'add-app': if args.action == 'add-app':
sm.add_app(args.app, args) sm.add_app(args.app, args)
elif args.action == 'update-hosts': elif args.action == 'update-proxy':
sm.update_hosts(args.app) sm.update_proxy(args.app)
elif args.action == 'update-domain': elif args.action == 'update-domain':
sm.update_domain(args.domain, args.port) sm.update_domain(args.domain, args.port)

View File

@ -18,7 +18,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts ccleanup /usr/local/bin/spotter-appmgr update-proxy ccleanup
} }
stop() { stop() {

View File

@ -23,7 +23,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts ckan /usr/local/bin/spotter-appmgr update-proxy ckan
} }
stop() { stop() {

View File

@ -17,7 +17,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts cts /usr/local/bin/spotter-appmgr update-proxy cts
} }
stop() { stop() {

View File

@ -18,7 +18,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts gnuhealth /usr/local/bin/spotter-appmgr update-proxy gnuhealth
} }
stop() { stop() {

View File

@ -19,7 +19,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts kanboard /usr/local/bin/spotter-appmgr update-proxy kanboard
} }
stop() { stop() {

View File

@ -19,7 +19,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts mifosx /usr/local/bin/spotter-appmgr update-proxy mifosx
} }
stop() { stop() {

View File

@ -19,7 +19,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts motech /usr/local/bin/spotter-appmgr update-proxy motech
} }
stop() { stop() {

View File

@ -20,7 +20,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts odk /usr/local/bin/spotter-appmgr update-proxy odk
} }
stop() { stop() {

View File

@ -17,7 +17,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts odkbuild /usr/local/bin/spotter-appmgr update-proxy odkbuild
} }
stop() { stop() {

View File

@ -17,7 +17,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts omk /usr/local/bin/spotter-appmgr update-proxy omk
} }
stop() { stop() {

View File

@ -20,7 +20,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts pandora /usr/local/bin/spotter-appmgr update-proxy pandora
} }
stop() { stop() {

View File

@ -21,7 +21,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts sahana-demo /usr/local/bin/spotter-appmgr update-proxy sahana-demo
} }
stop() { stop() {

View File

@ -21,7 +21,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts sahana /usr/local/bin/spotter-appmgr update-proxy sahana
} }
stop() { stop() {

View File

@ -21,7 +21,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts sambro /usr/local/bin/spotter-appmgr update-proxy sambro
} }
stop() { stop() {

View File

@ -19,7 +19,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts seeddms /usr/local/bin/spotter-appmgr update-proxy seeddms
} }
stop() { stop() {

View File

@ -20,7 +20,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts sigmah /usr/local/bin/spotter-appmgr update-proxy sigmah
} }
stop() { stop() {

View File

@ -20,7 +20,7 @@ start() {
} }
start_post() { start_post() {
/usr/local/bin/spotter-appmgr update-hosts ushahidi /usr/local/bin/spotter-appmgr update-proxy ushahidi
} }
stop() { stop() {