Esempio n. 1
0
def custom_distros(request, id=None):
    customer = request.user.customer
    if not id:
        j = [{'id': d.pk, 'name': d.name, 'container': d.container.uid} for d in CustomDistro.objects.filter(container__customer=customer)]
        return spit_json(request, j)
    try:
        container = customer.container_set.get(pk=(int(id) - UWSGI_IT_BASE_UID))
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
    if request.method == 'POST':
        if not container.custom_distros_storage:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        distro = CustomDistro(container=container) 
        allowed_fields = ('name', 'path', 'note')
        for field in allowed_fields:
            if field in j:
                setattr(distro, field, j[field])
        try:
            distro.full_clean()
            distro.save()
        except:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        response = HttpResponse(json.dumps({'message': 'Created'}), content_type="application/json")
        response.status_code = 201
        return response
    j = [{'id': d.pk, 'name': d.name} for d in CustomDistro.objects.filter(container__server=container.server,container__customer=customer).exclude(container=container)]
    return spit_json(request, j)
Esempio n. 2
0
def tag(request, id):
    customer = request.user.customer
    try:
        t = Tag.objects.get(customer=customer, pk=id)
    except:
        return HttpResponseNotFound(json.dumps({"error": "Not found"}), content_type="application/json")

    allowed_keys = ("name", "note")
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        for key in allowed_keys:
            if key in j:
                setattr(t, key, j[key])
        try:
            t.save()
            j = {"id": t.pk, "name": t.name, "note": t.note}
            return spit_json(request, j)
        except:
            response = HttpResponse(json.dumps({"error": "Conflict"}), content_type="application/json")
            response.status_code = 409
        return response
    elif request.method == "GET":
        j = {"id": t.pk, "name": t.name, "note": t.note}
        return spit_json(request, j)
    elif request.method == "DELETE":
        t.delete()
        return HttpResponse(json.dumps({"message": "Ok"}), content_type="application/json")
    allowed_keys = ("name", "note")
    response = HttpResponse(json.dumps({"error": "Method not allowed"}), content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 3
0
def tags(request):
    customer = request.user.customer
    allowed_keys = ('name', 'note')
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        tag = Tag(customer=customer)
        for key in allowed_keys:
            if key in j:
                setattr(tag, key, j[key])
        try:
            tag.save()
            j = {'id': tag.pk, 'name': tag.name, 'note': tag.note}
            response = spit_json(request, j)
            response.status_code = 201
            response.reason_phrase = 'Created'
        except:
            response = HttpResponse(json.dumps({'error': 'Conflict'}), content_type="application/json")
            response.status_code = 409
        return response

    elif request.method == 'GET':
        j = [{'id': t.pk, 'name': t.name} for t in Tag.objects.filter(customer=customer)]
        return spit_json(request, j)
    response = HttpResponse(json.dumps({'error': 'Method not allowed'}), content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 4
0
def tags(request):
    customer = request.user.customer
    allowed_keys = ('name', 'note')
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        tag = Tag(customer=customer)
        for key in allowed_keys:
            if key in j:
                setattr(tag, key, j[key])
        try:
            tag.save()
            j = {'id': tag.pk, 'name': tag.name, 'note': tag.note}
            response = spit_json(request, j)
            response.status_code = 201
            response.reason_phrase = 'Created'
        except:
            response = HttpResponse(json.dumps({'error': 'Conflict'}),
                                    content_type="application/json")
            response.status_code = 409
        return response

    elif request.method == 'GET':
        j = [{
            'id': t.pk,
            'name': t.name
        } for t in Tag.objects.filter(customer=customer)]
        return spit_json(request, j)
    response = HttpResponse(json.dumps({'error': 'Method not allowed'}),
                            content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 5
0
def tag(request, id):
    customer = request.user.customer
    try:
        t = Tag.objects.get(customer=customer, pk=id)
    except:
        return HttpResponseNotFound(json.dumps({'error': 'Not found'}), content_type="application/json")

    allowed_keys = ('name', 'note')
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        for key in allowed_keys:
            if key in j:
                setattr(t, key, j[key])
        try:
            t.save()
            j = {'id': t.pk, 'name': t.name, 'note': t.note}
            return spit_json(request, j)
        except:
            response = HttpResponse(json.dumps({'error': 'Conflict'}), content_type="application/json")
            response.status_code = 409
        return response
    elif request.method == 'GET':
        j = {'id': t.pk, 'name': t.name, 'note': t.note}
        return spit_json(request, j)
    elif request.method == 'DELETE':
        t.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}), content_type="application/json")
    allowed_keys = ('name', 'note')
    response = HttpResponse(json.dumps({'error': 'Method not allowed'}), content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 6
0
def custom_distros(request, id=None):
    customer = request.user.customer
    if not id:
        j = [{'id': d.pk, 'name': d.name, 'container': d.container.uid} for d in CustomDistro.objects.filter(container__customer=customer)]
        return spit_json(request, j)
    try:
        container = customer.container_set.get(pk=(int(id) - UWSGI_IT_BASE_UID))
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
    if request.method == 'POST':
        if not container.custom_distros_storage:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        distro = CustomDistro(container=container) 
        allowed_fields = ('name', 'path', 'note')
        for field in allowed_fields:
            if field in j:
                setattr(distro, field, j[field])
        try:
            distro.full_clean()
            distro.save()
        except:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        response = HttpResponse(json.dumps({'message': 'Created'}), content_type="application/json")
        response.status_code = 201
        return response
    j = [{'id': d.pk, 'name': d.name} for d in CustomDistro.objects.filter(container__server=container.server,container__customer=customer).exclude(container=container)]
    return spit_json(request, j)
Esempio n. 7
0
def tags(request):
    customer = request.user.customer
    allowed_keys = ("name", "note")
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        tag = Tag(customer=customer)
        for key in allowed_keys:
            if key in j:
                setattr(tag, key, j[key])
        try:
            tag.save()
            j = {"id": tag.pk, "name": tag.name, "note": tag.note}
            response = spit_json(request, j)
            response.status_code = 201
            response.reason_phrase = "Created"
        except:
            response = HttpResponse(json.dumps({"error": "Conflict"}), content_type="application/json")
            response.status_code = 409
        return response

    elif request.method == "GET":
        j = [{"id": t.pk, "name": t.name} for t in Tag.objects.filter(customer=customer)]
        return spit_json(request, j)
    response = HttpResponse(json.dumps({"error": "Method not allowed"}), content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 8
0
def domain(request, id):
    customer = request.user.customer
    try:
        domain = customer.domain_set.get(pk=id)
    except:
        return HttpResponseNotFound(json.dumps({'error': 'Not found'}),
                                    content_type="application/json")
    allowed_keys = ('note', )
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        for key in allowed_keys:
            if key in j:
                setattr(domain, key, j[key])
        if 'tags' in j:
            new_tags = []
            for tag in j['tags']:
                try:
                    new_tags.append(
                        Tag.objects.get(customer=customer, name=tag))
                except:
                    pass
            domain.tags = new_tags
        try:
            domain.save()
            j = {
                'id': domain.pk,
                'name': domain.name,
                'uuid': domain.uuid,
                'tags': [t.name for t in domain.tags.all()],
                'note': domain.note
            }
            return spit_json(request, j)
        except:
            response = HttpResponse(json.dumps({'error': 'Conflict'}),
                                    content_type="application/json")
            response.status_code = 409
        return response
    elif request.method == 'DELETE':
        domain.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}),
                            content_type="application/json")

    elif request.method == 'GET':
        j = {
            'id': domain.pk,
            'name': domain.name,
            'uuid': domain.uuid,
            'tags': [t.name for t in domain.tags.all()],
            'note': domain.note
        }
        return spit_json(request, j)

    response = HttpResponse(json.dumps({'error': 'Method not allowed'}),
                            content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 9
0
def domain(request, id):
    customer = request.user.customer
    try:
        domain = customer.domain_set.get(pk=id)
    except:
        return HttpResponseNotFound(json.dumps({"error": "Not found"}), content_type="application/json")
    allowed_keys = ("note",)
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        for key in allowed_keys:
            if key in j:
                setattr(domain, key, j[key])
        if "tags" in j:
            new_tags = []
            for tag in j["tags"]:
                try:
                    new_tags.append(Tag.objects.get(customer=customer, name=tag))
                except:
                    pass
            domain.tags = new_tags
        try:
            domain.save()
            j = {
                "id": domain.pk,
                "name": domain.name,
                "uuid": domain.uuid,
                "tags": [t.name for t in domain.tags.all()],
                "note": domain.note,
            }
            return spit_json(request, j)
        except:
            response = HttpResponse(json.dumps({"error": "Conflict"}), content_type="application/json")
            response.status_code = 409
        return response
    elif request.method == "DELETE":
        domain.delete()
        return HttpResponse(json.dumps({"message": "Ok"}), content_type="application/json")

    elif request.method == "GET":
        j = {
            "id": domain.pk,
            "name": domain.name,
            "uuid": domain.uuid,
            "tags": [t.name for t in domain.tags.all()],
            "note": domain.note,
        }
        return spit_json(request, j)

    response = HttpResponse(json.dumps({"error": "Method not allowed"}), content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 10
0
def domain(request, id):
    customer = request.user.customer
    try:
        domain = customer.domain_set.get(pk=id)
    except:
        return HttpResponseNotFound(json.dumps({'error': 'Not found'}),
                                    content_type="application/json")
    allowed_keys = ('note',)
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        for key in allowed_keys:
            if key in j:
                setattr(domain, key, j[key])
        if 'tags' in j:
            new_tags = []
            for tag in j['tags']:
                try:
                    new_tags.append(
                        Tag.objects.get(customer=customer, name=tag))
                except:
                    pass
            domain.tags = new_tags
        try:
            domain.save()
            j = {'id': domain.pk, 'name': domain.name, 'uuid': domain.uuid,
                 'tags': [t.name for t in domain.tags.all()],
                 'note': domain.note}
            return spit_json(request, j)
        except:
            response = HttpResponse(json.dumps({'error': 'Conflict'}),
                                    content_type="application/json")
            response.status_code = 409
        return response
    elif request.method == 'DELETE':
        domain.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}),
                            content_type="application/json")

    elif request.method == 'GET':
        j = {'id': domain.pk, 'name': domain.name, 'uuid': domain.uuid,
             'tags': [t.name for t in domain.tags.all()],
             'note': domain.note}
        return spit_json(request, j)

    response = HttpResponse(json.dumps({'error': 'Method not allowed'}),
                            content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 11
0
def custom_distro(request, id):
    customer = request.user.customer
    try:
        distro = CustomDistro.objects.get(pk=id, container__customer=customer)
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}),
                                     content_type="application/json")
    if request.method == 'DELETE':
        distro.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}),
                            content_type="application/json")
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        allowd_fields = ('name', 'path', 'note')
        for field in allowed_fields:
            if field in j:
                setattr(distro, field, j[field])
        distro.full_clean()
        distro.save()
    d = {
        'id': distro.pk,
        'container': distro.container.uid,
        'name': distro.name,
        'path': distro.path,
        'note': distro.note,
        'uuid': distro.uuid,
    }
    return spit_json(request, d)
Esempio n. 12
0
def alarm(request, id):
    customer = request.user.customer
    try:
        alarm = Alarm.objects.get(pk=id,
                                  container__in=customer.container_set.all())
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}),
                                     content_type="application/json")
    if request.method == 'DELETE':
        alarm.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}),
                            content_type="application/json")
    a = {
        'id': alarm.pk,
        'container': alarm.container.uid,
        'level': alarm.level,
        'color': alarm.color,
        'class': alarm._class,
        'line': alarm.line,
        'filename': alarm.filename,
        'func': alarm.func,
        'vassal': alarm.vassal,
        'unix': int(alarm.unix.strftime('%s')),
        'msg': alarm.msg
    }
    return spit_json(request, a)
Esempio n. 13
0
def private_loopboxes(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        j = [{'id': loopbox.pk, 'uid':loopbox.container.uid, 'filename': loopbox.filename, 'mountpoint': loopbox.mountpoint, 'ro': loopbox.ro } for loopbox in Loopbox.objects.filter(container__server=server)]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 14
0
def private_custom_services(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        j = [{'customer':service.customer.pk, 'config': service.config, 'mtime': service.munix, 'id': service.pk } for service in server.customservice_set.all()]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 15
0
def domains_in_container(request, id):
    if request.method == 'GET':
        customer = request.user.customer
        try:
            container_obj = customer.container_set.get(pk=(int(id) - UWSGI_IT_BASE_UID))
        except:
            return HttpResponseNotFound(json.dumps({'error': 'Not found'}),
                                        content_type="application/json")

        today = datetime.datetime.today()
        domain_list = [{'id': d.pk, 'uuid': d.uuid, 'name': d.name} for d in Domain.objects.filter(
            pk__in=HitsDomainMetric.objects.values_list(
                'domain', flat=True).filter(
                container=container_obj,
                year=today.year,
                month=today.month,
                day=today.day
            ).order_by('-year', '-month', '-day')
        )]

        return spit_json(request, domain_list)

    response = HttpResponse(json.dumps({'error': 'Method not allowed'}),
                            content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 16
0
def containers_per_domain(request, id):
    if request.method == 'GET':
        customer = request.user.customer
        try:
            domain = customer.domain_set.get(pk=id)
        except:
            return HttpResponseNotFound(json.dumps({'error': 'Not found'}),
                                        content_type="application/json")

        today = datetime.datetime.today()
        container_list = [{'id': c.pk, 'uuid': c.uuid, 'name': c.name, 'uid': c.uid} for c in Container.objects.filter(
            pk__in=HitsDomainMetric.objects.values_list(
                'container', flat=True).filter(
                domain=domain,
                year=today.year,
                month=today.month,
                day=today.day
            ).order_by('-year', '-month', '-day')
        )]
        return spit_json(request, container_list)

    response = HttpResponse(json.dumps({'error': 'Method not allowed'}),
                            content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 17
0
def custom_distro(request, id):
    customer = request.user.customer
    try:
        distro = CustomDistro.objects.get(pk=id, container__customer=customer)
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
    if request.method == 'DELETE':
        distro.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}), content_type="application/json")
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        allowd_fields = ('name', 'path', 'note')
        for field in allowed_fields:
            if field in j:
                setattr(distro, field, j[field])
        distro.full_clean()
        distro.save()
    d = {
        'id': distro.pk,
        'container': distro.container.uid,
        'name': distro.name,
        'path': distro.path,
        'note': distro.note,
        'uuid': distro.uuid,
    }
    return spit_json(request, d)
Esempio n. 18
0
def private_containers(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        j = [{'uid':container.uid, 'mtime': container.munix, 'ssh_keys_mtime': container.ssh_keys_munix } for container in server.container_set.exclude(distro__isnull=True).exclude(ssh_keys_raw__exact='').exclude(ssh_keys_raw__isnull=True)]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 19
0
def custom_distro(request, id):
    customer = request.user.customer
    try:
        distro = CustomDistro.objects.get(pk=id, container__customer=customer)
    except:
        return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
    if request.method == "DELETE":
        distro.delete()
        return HttpResponse(json.dumps({"message": "Ok"}), content_type="application/json")
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        allowd_fields = ("name", "path", "note")
        for field in allowed_fields:
            if field in j:
                setattr(distro, field, j[field])
        distro.full_clean()
        distro.save()
    d = {
        "id": distro.pk,
        "container": distro.container.uid,
        "name": distro.name,
        "path": distro.path,
        "note": distro.note,
        "uuid": distro.uuid,
    }
    return spit_json(request, d)
Esempio n. 20
0
def private_containers(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        j = [{'uid':container.uid, 'mtime': container.munix, 'ssh_keys_mtime': container.ssh_keys_munix } for container in server.container_set.exclude(distro__isnull=True).exclude(ssh_keys_raw__exact='').exclude(ssh_keys_raw__isnull=True)]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 21
0
def metrics_container_do(request, container, qs, prefix):
    """
    you can ask metrics for a single day of the year (288 metrics is the worst/general case)
    if the day is today, the response is cached for 5 minutes, otherwise it is cached indefinitely
    """
    today = datetime.datetime.today()
    year = today.year
    month = today.month
    day = today.day
    if 'year' in request.GET:year = int(request.GET['year'])
    if 'month' in request.GET: month = int(request.GET['month'])
    if 'day' in request.GET: day = int(request.GET['day'])
    expires = 86400
    if day != today.day or month != today.month or year != today.year: expires = 300
    try:
        # this will trigger the db query
        if not UWSGI_IT_METRICS_CACHE: raise
        cache = get_cache(UWSGI_IT_METRICS_CACHE)
        j = cache.get("%s_%d_%d_%d_%d" % (prefix, container.uid, year, month, day))
        if not j:
            j = qs.get(year=year,month=month,day=day).json
            cache.set("%s_%d_%d_%d_%d" % (prefix, container.uid, year, month, day ), j, expires)
    except: 
        import sys
        print sys.exc_info()
        try:
            j = qs.get(year=year,month=month,day=day).json
        except:
            j = "[]"
    return spit_json(request, j, expires, True)
Esempio n. 22
0
def me(request):
    customer = request.user.customer
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        allowed_keys = ('vat', 'company')
        j = json.loads(request.read())
        for key in j:
            if key in allowed_keys:
                setattr(customer, key, j[key])
        if 'password' in j:
            customer.user.set_password(j['password'])
            customer.user.save()
        if 'email' in j:
            customer.user.email = j['email']
            customer.user.save()
        customer.save()
    c = {
        'email': customer.user.email,
        'vat': customer.vat,
        'company': customer.company,
        'uuid': customer.uuid,
        'containers': [cc.uid for cc in customer.container_set.all()],
        'servers': [s.address for s in customer.server_set.all()],
    }
    return spit_json(request, c)
Esempio n. 23
0
def private_privileged_secret_uuids(request):
    try:
        privileged_client = PrivilegedClient.objects.get(address=request.META['REMOTE_ADDR'])
        j = [{'uid':container.uid, 'mtime': container.munix, 'secret_uuid': container.secret_uuid, 'address': container.server.address } for container in Container.objects.all().exclude(distro__isnull=True).exclude(ssh_keys_raw__exact='').exclude(ssh_keys_raw__isnull=True)]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 24
0
def private_custom_services(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        j = [{'customer':service.customer.pk, 'config': service.config, 'mtime': service.munix, 'id': service.pk } for service in server.customservice_set.all()]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 25
0
def metrics_container_do(request, container, qs, prefix):
    """
    you can ask metrics for a single day of the year (288 metrics is the worst/general case)
    if the day is today, the response is cached for 5 minutes, otherwise it is cached indefinitely
    """
    today = datetime.datetime.today()
    year = today.year
    month = today.month
    day = today.day
    if 'year' in request.GET: year = int(request.GET['year'])
    if 'month' in request.GET: month = int(request.GET['month'])
    if 'day' in request.GET: day = int(request.GET['day'])
    expires = 86400
    if day != today.day or month != today.month or year != today.year:
        expires = 300
    try:
        # this will trigger the db query
        if not UWSGI_IT_METRICS_CACHE: raise
        cache = get_cache(UWSGI_IT_METRICS_CACHE)
        j = cache.get("%s_%d_%d_%d_%d" %
                      (prefix, container.uid, year, month, day))
        if not j:
            j = qs.get(year=year, month=month, day=day).json
            cache.set(
                "%s_%d_%d_%d_%d" % (prefix, container.uid, year, month, day),
                j, expires)
    except:
        import sys
        print sys.exc_info()
        try:
            j = qs.get(year=year, month=month, day=day).json
        except:
            j = "[]"
    return spit_json(request, j, expires, True)
Esempio n. 26
0
def me(request):
    customer = request.user.customer
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        allowed_keys = ('vat', 'company')
        j = json.loads(request.read())
        for key in j:
            if key in allowed_keys:
                setattr(customer, key, j[key])
        if 'password' in j:
            customer.user.set_password(j['password'])
            customer.user.save()
        if 'email' in j:
            customer.user.email = j['email']
            customer.user.save()
        customer.save()
    c = {
        'email': customer.user.email,
        'vat': customer.vat,
        'company': customer.company,
        'uuid': customer.uuid,
        'containers': [cc.uid for cc in customer.container_set.all()],
        'servers': [s.address for s in customer.server_set.all()],
    }
    return spit_json(request, c)
Esempio n. 27
0
def loopbox(request, id):
    customer = request.user.customer
    try:
        loopbox = Loopbox.objects.get(pk=id, container__in=customer.container_set.all())
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        if 'tags' in j:
            new_tags = []
            for tag in j['tags']:
                try:
                    new_tags.append(Tag.objects.get(customer=customer, name=tag))
                except:
                    pass
            loopbox.tags = new_tags
        loopbox.save()
    elif request.method == 'DELETE':
        loopbox.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}), content_type="application/json")
    l = {
        'id': loopbox.pk,
        'container': loopbox.container.uid,
        'filename': loopbox.filename,
        'mountpoint': loopbox.mountpoint,
        'ro': loopbox.ro,
        'tags': [t.name for t in loopbox.tags.all()]
    }
    return spit_json(request, l)
Esempio n. 28
0
def alarm(request, id):
    customer = request.user.customer
    try:
        alarm = Alarm.objects.get(pk=id,
                                  container__in=customer.container_set.all())
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}),
                                     content_type="application/json")
    if request.method == 'DELETE':
        alarm.delete()
        return HttpResponse(json.dumps({'message': 'Ok'}),
                            content_type="application/json")
    a = {
        'id': alarm.pk,
        'container': alarm.container.uid,
        'level': alarm.level,
        'color': alarm.color,
        'class': alarm._class,
        'line': alarm.line,
        'filename': alarm.filename,
        'func': alarm.func,
        'vassal': alarm.vassal,
        'unix': int(alarm.unix.strftime('%s')),
        'msg': alarm.msg
    }
    return spit_json(request, a)
Esempio n. 29
0
def loopbox(request, id):
    customer = request.user.customer
    try:
        loopbox = Loopbox.objects.get(pk=id, container__in=customer.container_set.all())
    except:
        return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        if "tags" in j:
            new_tags = []
            for tag in j["tags"]:
                try:
                    new_tags.append(Tag.objects.get(customer=customer, name=tag))
                except:
                    pass
            loopbox.tags = new_tags
        loopbox.save()
    elif request.method == "DELETE":
        loopbox.delete()
        return HttpResponse(json.dumps({"message": "Ok"}), content_type="application/json")
    l = {
        "id": loopbox.pk,
        "container": loopbox.container.uid,
        "filename": loopbox.filename,
        "mountpoint": loopbox.mountpoint,
        "ro": loopbox.ro,
        "tags": [t.name for t in loopbox.tags.all()],
    }
    return spit_json(request, l)
Esempio n. 30
0
def me(request):
    customer = request.user.customer
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        allowed_keys = ("vat", "company")
        j = json.loads(request.read())
        for key in j:
            if key in allowed_keys:
                setattr(customer, key, j[key])
        if "password" in j:
            customer.user.set_password(j["password"])
            customer.user.save()
        if "email" in j:
            customer.user.email = j["email"]
            customer.user.save()
        customer.save()
    c = {
        "email": customer.user.email,
        "vat": customer.vat,
        "company": customer.company,
        "uuid": customer.uuid,
        "containers": [cc.uid for cc in customer.container_set.all()],
        "servers": [s.address for s in customer.server_set.all()],
    }
    return spit_json(request, c)
Esempio n. 31
0
def private_loopboxes(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        j = [{'id': loopbox.pk, 'uid':loopbox.container.uid, 'filename': loopbox.filename, 'mountpoint': loopbox.mountpoint, 'ro': loopbox.ro } for loopbox in Loopbox.objects.filter(container__server=server)]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 32
0
def containers(request):
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        needed_keys = ('server', 'name', 'memory', 'storage')
        for k in needed_keys:
            if not k in j.keys():
                return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        try:
            server = Server.objects.get(address=j['server'])
            if server.owner != request.user.customer:
                return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        except:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        try:
            container = Container(customer=request.user.customer, server=server)
            container.name = j['name']
            container.memory = int(j['memory'])
            container.storage = int(j['storage'])
            container.save()
            response = HttpResponse(json.dumps({'message': 'Created'}), content_type="application/json")
            response.status_code = 201
            return response
        except:
            response = HttpResponse(json.dumps({'error': 'Conflict'}), content_type="application/json")
            response.status_code = 409
            return response
    elif (request.method == 'GET' and
         'tags' in request.GET):
            containers = request.user.customer.container_set.filter(tags__name__in=request.GET['tags'].split(','))
    else:
        containers = request.user.customer.container_set.all()

    c = []
    for container in containers:
        cc = {
            'uid': container.uid,
            'name': container.name,
            'hostname': container.hostname,
            'ip': str(container.ip),
            'memory': container.memory,
            'storage': container.storage,
            'uuid': container.uuid,
            'distro': None,
            'distro_name': None,
            'server': container.server.name,
            'server_address': container.server.address,
            'tags': [t.name for t in container.tags.all()]
        }
        if container.distro:
            cc['distro'] = container.distro.pk
            cc['distro_name'] = container.distro.name
        c.append(cc)

    return spit_json(request, c)
Esempio n. 33
0
def domains(request):
    customer = request.user.customer

    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if Domain.objects.filter(name=j['name']):
            response = HttpResponse(json.dumps({'error': 'Conflict'}),
                                    content_type="application/json")
            response.status_code = 409
            return response
        if dns_check(j['name'], customer.uuid):
            try:
                customer.domain_set.create(name=j['name'])
                response = HttpResponse(json.dumps({'message': 'Created'}),
                                        content_type="application/json")
                response.status_code = 201
            except:
                response = HttpResponse(json.dumps({'error': 'Conflict'}),
                                        content_type="application/json")
                response.status_code = 409
            return response
        else:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}),
                                         content_type="application/json")

    elif request.method == 'DELETE':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        try:
            customer.domain_set.get(name=j['name']).delete()
        except Domain.DoesNotExist:
            return HttpResponseNotFound(json.dumps({'error': 'Not found'}),
                                        content_type="application/json")
        return HttpResponse(json.dumps({'message': 'Ok'}),
                            content_type="application/json")

    elif request.method == 'GET':
        if 'tags' in request.GET:
            j = [{'id': d.pk, 'name': d.name, 'uuid': d.uuid,
                  'tags': [t.name for t in d.tags.all()]} for d in
                 customer.domain_set.filter(
                     tags__name__in=request.GET['tags'].split(','))]
        else:
            j = [{'id': d.pk, 'name': d.name, 'uuid': d.uuid,
                  'tags': [t.name for t in d.tags.all()]} for d in
                 customer.domain_set.all()]
        return spit_json(request, j)

    response = HttpResponse(json.dumps({'error': 'Method not allowed'}),
                            content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 34
0
def loopboxes(request):
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        needed_keys = ("container", "filename", "mountpoint")
        for k in needed_keys:
            if not k in j.keys():
                return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        try:
            container = request.user.customer.container_set.get(pk=(int(j["container"]) - UWSGI_IT_BASE_UID))
        except:
            return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        try:
            loopbox = Loopbox(container=container)
            loopbox.filename = j["filename"]
            loopbox.mountpoint = j["mountpoint"]
            if "ro" in j:
                loopbox.ro = j["ro"]
            loopbox.save()
            response = HttpResponse(json.dumps({"message": "Created"}), content_type="application/json")
            response.status_code = 201
            return response
        except:
            response = HttpResponse(json.dumps({"error": "Conflict"}), content_type="application/json")
            response.status_code = 409
            return response
    elif request.method == "GET":
        query = {}
        if "tags" in request.GET:
            query["tags__name__in"] = request.GET["tags"].split(",")
        if "container" in request.GET:
            try:
                query["container"] = request.user.customer.container_set.get(
                    pk=(int(request.GET["container"]) - UWSGI_IT_BASE_UID)
                )
            except:
                return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        else:
            query["container__in"] = request.user.customer.container_set.all()
        loopboxes = Loopbox.objects.filter(**query)
    else:
        loopboxes = Loopbox.objects.filter(container__in=request.user.customer.container_set.all())

    l = []
    for loopbox in loopboxes:
        ll = {
            "id": loopbox.pk,
            "container": loopbox.container.uid,
            "filename": loopbox.filename,
            "mountpoint": loopbox.mountpoint,
            "ro": loopbox.ro,
            "tags": [t.name for t in loopbox.tags.all()],
        }
        l.append(ll)
    return spit_json(request, l)
Esempio n. 35
0
def private_domains_rsa(request):
    server = Server.objects.get(address=request.META['REMOTE_ADDR'])
    server_customers = Customer.objects.filter(container__server=server)
    j = []
    for customer in server_customers:
        domains = []
        for domain in customer.domain_set.all():
            domains.append({'name': domain.name, 'mtime': domain.munix})
        j.append({'rsa': customer.rsa_pubkey, 'domains': domains })
    return spit_json(request, j)
Esempio n. 36
0
def news(request):
    news_list = []
    user = api_auth(request)
    if user:
        for n in News.objects.all()[0:10]:
            news_list.append({"content": n.content, "date": int(time.mktime(n.ctime.timetuple()))})
    else:
        for n in News.objects.filter(public=True)[0:10]:
            news_list.append({"content": n.content, "date": int(time.mktime(n.ctime.timetuple()))})
    return spit_json(request, news_list)
Esempio n. 37
0
def private_domains_rsa(request):
    server = Server.objects.get(address=request.META['REMOTE_ADDR'])
    server_customers = Customer.objects.filter(container__server=server)
    j = []
    for customer in server_customers:
        domains = []
        for domain in customer.domain_set.all():
            domains.append({'name': domain.name, 'mtime': domain.munix})
        j.append({'rsa': customer.rsa_pubkey, 'domains': domains })
    return spit_json(request, j)
Esempio n. 38
0
def loopboxes(request):
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        needed_keys = ('container', 'filename', 'mountpoint')
        for k in needed_keys:
            if not k in j.keys():
                return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        try:
            container = request.user.customer.container_set.get(pk=(int(j['container']) - UWSGI_IT_BASE_UID))
        except:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        try:
            loopbox = Loopbox(container=container)
            loopbox.filename = j['filename']
            loopbox.mountpoint = j['mountpoint']
            if 'ro' in j:
                loopbox.ro = j['ro']
            loopbox.save()
            response = HttpResponse(json.dumps({'message': 'Created'}), content_type="application/json")
            response.status_code = 201
            return response
        except:
            response = HttpResponse(json.dumps({'error': 'Conflict'}), content_type="application/json")
            response.status_code = 409
            return response
    elif request.method == 'GET':
        query = {}
        if 'tags' in request.GET:
            query['tags__name__in'] = request.GET['tags'].split(',')
        if 'container' in request.GET:
            try:
                query['container'] = request.user.customer.container_set.get(pk=(int(request.GET['container']) - UWSGI_IT_BASE_UID))
            except:
                return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        else:
            query['container__in'] = request.user.customer.container_set.all()
        loopboxes = Loopbox.objects.filter(**query)
    else:
        loopboxes = Loopbox.objects.filter(container__in=request.user.customer.container_set.all())

    l = []
    for loopbox in loopboxes:
        ll = {
            'id': loopbox.pk,
            'container': loopbox.container.uid,
            'filename': loopbox.filename,
            'mountpoint': loopbox.mountpoint,
            'ro': loopbox.ro,
            'tags': [t.name for t in loopbox.tags.all()]
        }
        l.append(ll)
    return spit_json(request, l)
Esempio n. 39
0
def portmappings(request, ip):
    customer = request.user.customer
    try:
        server = Server.objects.get(address=ip, owner=customer)
    except:
        return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        pm = Portmap()
        try:
            pm.proto = j["proto"]
            pm.public_port = int(j["public_port"])
            pm.private_port = int(j["private_port"])
            pm.container = server.container_set.get(pk=(int(j["container"]) - UWSGI_IT_BASE_UID), customer=customer)
            pm.full_clean()
            pm.save()
        except:
            import sys

            print sys.exc_info()
            return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        response = HttpResponse(json.dumps({"message": "Created"}), content_type="application/json")
        response.status_code = 201
        return response
    elif request.method == "DELETE":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        try:
            pm = Portmap.objects.get(pk=j["id"], container__server=server)
            pm.delete()
        except:
            return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")
        return HttpResponse(json.dumps({"message": "Ok"}), content_type="application/json")
    mappings = []
    for portmap in Portmap.objects.filter(container__server=server):
        mappings.append(
            {
                "id": portmap.pk,
                "proto": portmap.proto,
                "public_port": portmap.public_port,
                "container": portmap.container.uid,
                "container_ip": str(portmap.container.ip),
                "private_port": portmap.private_port,
            }
        )
    return spit_json(request, mappings)
Esempio n. 40
0
def news(request):
    news_list = []
    user = api_auth(request)
    if user:
        for n in News.objects.all()[0:10]:
            news_list.append({'content': n.content,
                              'date': int(time.mktime(n.ctime.timetuple()))})
    else:
        for n in News.objects.filter(public=True)[0:10]:
            news_list.append({'content': n.content,
                              'date': int(time.mktime(n.ctime.timetuple()))})
    return spit_json(request, news_list)
Esempio n. 41
0
def portmappings(request, ip):
    customer = request.user.customer
    try:
        server = Server.objects.get(address=ip,owner=customer)
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        pm = Portmap()
        try:
            pm.proto = j['proto']
            pm.public_port = int(j['public_port'])
            pm.private_port = int(j['private_port'])
            pm.container = server.container_set.get(pk=(int(j['container']) - UWSGI_IT_BASE_UID), customer=customer)
            pm.full_clean()
            pm.save()
        except:
            import sys
            print sys.exc_info()
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        response = HttpResponse(json.dumps({'message': 'Created'}), content_type="application/json")
        response.status_code = 201
        return response
    elif request.method == 'DELETE':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        try:
            pm = Portmap.objects.get(pk=j['id'], container__server=server)
            pm.delete()
        except:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        return HttpResponse(json.dumps({'message': 'Ok'}), content_type="application/json")
    mappings = []
    for portmap in Portmap.objects.filter(container__server=server):
        mappings.append({'id': portmap.pk,
                         'proto': portmap.proto,
                         'public_port': portmap.public_port,
                         'container': portmap.container.uid,
                         'container_ip': str(portmap.container.ip),
                         'private_port': portmap.private_port,
                       })
    return spit_json(request, mappings)
Esempio n. 42
0
def domains(request):
    customer = request.user.customer

    if request.method == "POST":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if Domain.objects.filter(name=j["name"]):
            response = HttpResponse(json.dumps({"error": "Conflict"}), content_type="application/json")
            response.status_code = 409
            return response
        if dns_check(j["name"], customer.uuid):
            try:
                customer.domain_set.create(name=j["name"])
                response = HttpResponse(json.dumps({"message": "Created"}), content_type="application/json")
                response.status_code = 201
            except:
                response = HttpResponse(json.dumps({"error": "Conflict"}), content_type="application/json")
                response.status_code = 409
            return response
        else:
            return HttpResponseForbidden(json.dumps({"error": "Forbidden"}), content_type="application/json")

    elif request.method == "DELETE":
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        try:
            customer.domain_set.get(name=j["name"]).delete()
        except Domain.DoesNotExist:
            return HttpResponseNotFound(json.dumps({"error": "Not found"}), content_type="application/json")
        return HttpResponse(json.dumps({"message": "Ok"}), content_type="application/json")

    elif request.method == "GET":
        if "tags" in request.GET:
            j = [
                {"id": d.pk, "name": d.name, "uuid": d.uuid, "tags": [t.name for t in d.tags.all()]}
                for d in customer.domain_set.filter(tags__name__in=request.GET["tags"].split(","))
            ]
        else:
            j = [
                {"id": d.pk, "name": d.name, "uuid": d.uuid, "tags": [t.name for t in d.tags.all()]}
                for d in customer.domain_set.all()
            ]
        return spit_json(request, j)

    response = HttpResponse(json.dumps({"error": "Method not allowed"}), content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 43
0
def portmappings(request, ip):
    customer = request.user.customer
    try:
        server = Server.objects.get(address=ip,owner=customer)
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        pm = Portmap()
        try:
            pm.proto = j['proto']
            pm.public_port = int(j['public_port'])
            pm.private_port = int(j['private_port'])
            pm.container = server.container_set.get(pk=(int(j['container']) - UWSGI_IT_BASE_UID), customer=customer)
            pm.full_clean()
            pm.save()
        except:
            import sys
            print sys.exc_info()
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        response = HttpResponse(json.dumps({'message': 'Created'}), content_type="application/json")
        response.status_code = 201
        return response
    elif request.method == 'DELETE':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        try:
            pm = Portmap.objects.get(pk=j['id'], container__server=server)
            pm.delete()
        except:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")
        return HttpResponse(json.dumps({'message': 'Ok'}), content_type="application/json")
    mappings = []
    for portmap in Portmap.objects.filter(container__server=server):
        mappings.append({'id': portmap.pk,
                         'proto': portmap.proto,
                         'public_port': portmap.public_port,
                         'container': portmap.container.uid,
                         'container_ip': str(portmap.container.ip),
                         'private_port': portmap.private_port,
                       })
    return spit_json(request, mappings)
Esempio n. 44
0
def private_privileged_secret_uuids(request):
    try:
        privileged_client = PrivilegedClient.objects.get(
            address=request.META['REMOTE_ADDR'])
        j = [{
            'uid': container.uid,
            'mtime': container.munix,
            'secret_uuid': container.secret_uuid,
            'address': container.server.address
        } for container in Container.objects.all().exclude(
            distro__isnull=True).exclude(ssh_keys_raw__exact='').exclude(
                ssh_keys_raw__isnull=True)]
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 45
0
def domains(request):
    customer = request.user.customer

    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        if Domain.objects.filter(name=j['name']):
            response = HttpResponse(json.dumps({'error': 'Conflict'}), content_type="application/json")
            response.status_code = 409
            return response
        if dns_check(j['name'], customer.uuid):
            try:
                customer.domain_set.create(name=j['name'])
                response = HttpResponse(json.dumps({'message': 'Created'}), content_type="application/json")
                response.status_code = 201
            except:
                response = HttpResponse(json.dumps({'error': 'Conflict'}), content_type="application/json")
                response.status_code = 409
            return response
        else:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}), content_type="application/json")

    elif request.method == 'DELETE':
        response = check_body(request)
        if response:
            return response
        j = json.loads(request.read())
        try:
            customer.domain_set.get(name=j['name']).delete()
        except Domain.DoesNotExist:
            return HttpResponseNotFound(json.dumps({'error': 'Not found'}), content_type="application/json")
        return HttpResponse(json.dumps({'message': 'Ok'}), content_type="application/json")

    elif request.method == 'GET':
        if 'tags' in request.GET:
            j = [{'id': d.pk, 'name': d.name, 'uuid': d.uuid, 'tags': [t.name for t in d.tags.all()]} for d in
                 customer.domain_set.filter(tags__name__in=request.GET['tags'].split(','))]
        else:
            j = [{'id': d.pk, 'name': d.name, 'uuid': d.uuid, 'tags': [t.name for t in d.tags.all()]} for d in
                 customer.domain_set.all()]
        return spit_json(request, j)

    response = HttpResponse(json.dumps({'error': 'Method not allowed'}), content_type="application/json")
    response.status_code = 405
    return response
Esempio n. 46
0
def private_portmappings(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        unix = server.portmappings_munix
        pmappings = []
        for portmap in Portmap.objects.filter(container__server=server):
            pmappings.append({
                             'proto': portmap.proto,
                             'public_ip': str(portmap.container.server.address),
                             'public_port': portmap.public_port,
                             'private_ip': str(portmap.container.ip),
                             'private_port': portmap.private_port,
                            })
            if portmap.munix > unix:
                unix = portmap.munix
        j = {'unix': unix, 'mappings':pmappings}
        return spit_json(request, j)
    except:
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 47
0
def private_server_file_metadata(request):
    try:
        server = Server.objects.get(address=request.META['REMOTE_ADDR'])
        if request.method == 'POST':
            response = check_body(request)
            if response: return response
            j = json.loads(request.read())
            metadata = ServerFileMetadata.objects.get(filename=j['file'])
            sm, created = ServerMetadata.objects.get_or_create(server=server, metadata=metadata)
            sm.value = j['value']
            sm.save()
            response = HttpResponse('Created\n')
            response.status_code = 201
            return response
        files = []
        for _file in ServerFileMetadata.objects.all():
            files.append(_file.filename)
        return spit_json(request, files)
    except:
        import sys
        print sys.exc_info()
        return HttpResponseForbidden('Forbidden\n')
Esempio n. 48
0
def distros(request):
    j = [{'id': d.pk, 'name': d.name} for d in Distro.objects.all()]
    return spit_json(request, j)
Esempio n. 49
0
def alarms(request):
    query = {}
    if 'container' in request.GET:
        try:
            query['container'] = request.user.customer.container_set.get(
                pk=(int(request.GET['container']) - UWSGI_IT_BASE_UID))
        except:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}),
                                         content_type="application/json")
    else:
        query['container__in'] = request.user.customer.container_set.all()

    if 'vassal' in request.GET:
        query['vassal'] = request.GET['vassal']

    if 'class' in request.GET:
        query['_class'] = request.GET['class']

    if 'color' in request.GET:
        query['color'] = request.GET['color']

    if 'level' in request.GET:
        query['level'] = int(request.GET['level'])

    if 'line' in request.GET:
        query['line'] = int(request.GET['line'])

    if 'filename' in request.GET:
        query['filename'] = request.GET['filename']

    if 'func' in request.GET:
        query['func'] = request.GET['func']

    alarms = Alarm.objects.filter(**query)

    a = []

    if 'with_total' in request.GET:
        response = {'total': alarms.count(), 'alarms': a}
    else:
        response = a

    if 'range' in request.GET:
        to = request.GET['range']
        try:
            if '-' in to:
                _from, to = to.split('-')
            else:
                _from = 0
            alarms = alarms[int(min(_from, to)):int(max(_from, to))]

        except:
            response = HttpResponse(json.dumps(
                {'error': 'Requested Range Not Satisfiable'}),
                                    content_type="application/json")
            response.status_code = 416
            return response
        if _from > to:
            alarms = alarms.reverse()

    for alarm in alarms:
        aa = {
            'id': alarm.pk,
            'container': alarm.container.uid,
            'level': alarm.level,
            'color': alarm.color,
            'class': alarm._class,
            'vassal': alarm.vassal,
            'line': alarm.line,
            'filename': alarm.filename,
            'func': alarm.func,
            'unix': int(alarm.unix.strftime('%s')),
            'msg': alarm.msg
        }
        a.append(aa)

    return spit_json(request, response)
Esempio n. 50
0
def container(request, id):
    customer = request.user.customer
    try:
        container = customer.container_set.get(pk=(int(id) -
                                                   UWSGI_IT_BASE_UID))
    except:
        return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}),
                                     content_type="application/json")
    if request.method == 'POST':
        response = check_body(request)
        if response:
            return response
        allowed_keys = (
            'name',
            'note',
            'quota_threshold',
            'jid',
            'jid_secret',
            'jid_destinations',
            'nofollow',
            'pushover_user',
            'pushover_token',
            'pushover_sound',
            'alarm_freq',
        )

        j = json.loads(request.read())
        if not j:
            return HttpResponseForbidden(json.dumps({'error': 'Forbidden'}),
                                         content_type="application/json")
        for key in j:
            if key in allowed_keys:
                setattr(container, key, j[key])
        if 'ssh_keys' in j:
            container.ssh_keys_raw = '\n'.join(j['ssh_keys'])
            container.ssh_keys_mtime = datetime.datetime.now()
        if 'distro' in j:
            container.distro = Distro.objects.get(pk=j['distro'])
        if 'memory' in j:
            if container.server.owner == customer:
                container.memory = int(j['memory'])
        if 'storage' in j:
            if container.server.owner == customer:
                container.storage = int(j['storage'])
        if 'tags' in j:
            new_tags = []
            for tag in j['tags']:
                try:
                    new_tags.append(
                        Tag.objects.get(customer=customer, name=tag))
                except:
                    pass
            container.tags = new_tags
        # linking and unlinking requires reboot
        if 'link' in j:
            try:
                link = ContainerLink()
                link.container = container
                link.to = Container.objects.get(pk=(int(j['link']) -
                                                    UWSGI_IT_BASE_UID))
                link.full_clean()
                link.save()
                container.last_reboot = datetime.datetime.now()
            except:
                response = HttpResponse(json.dumps({'error': 'Conflict'}),
                                        content_type="application/json")
                response.status_code = 409
                return response
        if 'unlink' in j:
            try:
                link = container.containerlink_set.get(to=(int(j['unlink']) -
                                                           UWSGI_IT_BASE_UID))
                link.delete()
                container.last_reboot = datetime.datetime.now()
            except:
                response = HttpResponse(json.dumps({'error': 'Conflict'}),
                                        content_type="application/json")
                response.status_code = 409
                return response
        if 'reboot' in j:
            container.last_reboot = datetime.datetime.now()
        container.full_clean()
        container.save()
    c = {
        'uid': container.uid,
        'name': container.name,
        'hostname': container.hostname,
        'ip': str(container.ip),
        'memory': container.memory,
        'storage': container.storage,
        'uuid': container.uuid,
        'distro': None,
        'distro_name': None,
        'server': container.server.name,
        'server_address': container.server.address,
        'jid': container.jid,
        'jid_destinations': container.jid_destinations,
        'pushover_user': container.pushover_user,
        'pushover_token': container.pushover_token,
        'pushover_sound': container.pushover_sound,
        'alarm_freq': container.alarm_freq,
        'quota_threshold': container.quota_threshold,
        'nofollow': container.nofollow,
        'note': container.note,
        'linked_to': container.linked_to,
        'ssh_keys': container.ssh_keys,
        'tags': [t.name for t in container.tags.all()],
        'legion_address':
        [l.address for l in container.server.legion_set.all()]
    }
    if container.distro:
        c['distro'] = container.distro.pk
        c['distro_name'] = container.distro.name
    return spit_json(request, c)