Beispiel #1
0
def _update_application(client: MarathonClient,
                        app: MarathonApp,
                        definition_path: str,
                        do_backup: bool = False) -> Union[str, bool]:
    if do_backup:
        if not os.path.isdir('./backups'):
            os.mkdir('./backups/')
            print('Created backups directory')
        backup = client.get_app(app.id).to_json()
        backup_path = './backups/{}_{}.json'.format(
            mangling.appid_to_filename(app.id),
            time.strftime("%Y-%m-%d_%H:%M:%S"))
        with open(backup_path, 'w') as backup_file:
            backup_file.write(backup)
            print('\nBacked app into: {}'.format(backup_path))
    else:
        backup_path = ''
    print('Updating app: {} (from: {})'.format(app.id, definition_path))
    deployment = client.update_app(app.id, app, force=True)
    # TODO: Handle failure
    # Return the deployed backup file to build rollback order, if necessary
    # or False if a user-initiated rollback completed successfully
    if not wait_for_deployment(client, deployment):
        client.restart_app(app.id)
    return False if not wait_for_deployment(client,
                                            deployment) else backup_path
Beispiel #2
0
def send_to_marathon(request):
    try:
        if request.method == 'POST':
            action = request.POST.get('action', None)
            id = request.POST.get('id', None)
            mc = MarathonClient('http://{}:{}'.format(settings.MARATHON['host'], settings.MARATHON['port']))
            if action == 'stop':
                mc.scale_app(id, 0, force=True)
            elif action == 'start':
                mc.scale_app(id, 1)
            elif action == 'destroy':
                if request.user.has_perm("auth.can_init_app"):
                    mc.delete_app(id)
                else:
                    raise PermissionDenied
            elif action == 'restart':
                mc.restart_app(id)
            elif action == 'scale':
                mc.scale_app(id, int(request.POST.get('number_instance')))
            elif action == 'update':
                app = mc.get_app(id)
                app.cpus = float(request.POST.get('cpus'))
                app.mem = float(request.POST.get('mem'))
                app.container.docker.image = request.POST.get('version')
                mc.update_app(id, app)
            elif action  == "stop-deployment":
                mc.delete_deployment(id)
            result = '{"status":"success", "msg": "%(action)s success"}'%{"action":action}
    except Exception as e:
        result = '{"status":"error", "msg": "%(action)s fail: %(error)s" }'%{"action":action, "error": html.escape(str(e))}
    return HttpResponse(result)
Beispiel #3
0
def send_to_marathon(request):
    try:
        if request.method == 'POST':
            action = request.POST.get('action', None)
            id = request.POST.get('id', None)
            mc = MarathonClient('http://{}:{}'.format(
                settings.MARATHON['host'], settings.MARATHON['port']))
            if action == 'stop':
                mc.scale_app(id, 0, force=True)
            elif action == 'start':
                mc.scale_app(id, 1)
            elif action == 'destroy':
                if request.user.has_perm("auth.can_init_app"):
                    mc.delete_app(id)
                else:
                    raise PermissionDenied
            elif action == 'restart':
                mc.restart_app(id)
            elif action == 'scale':
                mc.scale_app(id, int(request.POST.get('number_instance')))
            elif action == 'update':
                app = mc.get_app(id)
                app.cpus = float(request.POST.get('cpus'))
                app.mem = float(request.POST.get('mem'))
                app.container.docker.image = request.POST.get('version')
                mc.update_app(id, app)
            elif action == "stop-deployment":
                mc.delete_deployment(id)
            result = '{"status":"success", "msg": "%(action)s success"}' % {
                "action": action
            }
    except Exception as e:
        result = '{"status":"error", "msg": "%(action)s fail: %(error)s" }' % {
            "action": action,
            "error": html.escape(str(e))
        }
    return HttpResponse(result)
Beispiel #4
0
def rolling_restart_app(client: MarathonClient, appid: str):
    deployment = client.restart_app(appid, force=True)
    wait_for_deployment(client, deployment)