Example #1
0
 def post(self):
     head = request.headers
     if head.get('Content-Type').startswith("application/json"):
         data = request.get_json()["certificate"]
         if data.get("is_acme"):
             certs = certificates.get()
             for x in certs:
                 if x.domain == data["domain"] and x.is_acme:
                     emsg = ("You can only have one ACME certificate at a "
                             "time for this domain.")
                     return jsonify(errors={"msg": emsg}), 422
             id = as_job(self._request_acme, data)
             return job_response(
                 id, data={"certificate": {"id": data["id"]}})
         else:
             id = as_job(self._generate, data)
             return job_response(
                 id, data={"certificate": {"id": data["id"]}})
     elif head.get('Content-Type').startswith("multipart/form-data"):
         name = request.form.get("id")
         files = [
             request.files.get("file[0]").read(),
             request.files.get("file[1]").read(),
             request.files.get("file[2]").read()
             if request.files.get("file[2]") else None]
         id = as_job(self._upload, name, files)
         return job_response(id)
     else:
         abort(400)
Example #2
0
def firstrun():
    data = request.get_json()
    resize_boards = [
        "Raspberry Pi", "Raspberry Pi 2", "Raspberry Pi 3", "Cubieboard2",
        "Cubietruck", "BeagleBone Black", "ODROID-U"
    ]
    if data.get("resize_sd_card", None)\
            and config.get("enviro", "board") in resize_boards:
        part = 1 if config.get("enviro", "board").startswith("Cubie") else 2
        p1str = 'd\nn\np\n1\n\n\nw\n'
        p2str = 'd\n2\nn\np\n2\n\n\nw\n'
        shell('fdisk /dev/mmcblk0', stdin=(p1str if part == 1 else p2str))
        if not os.path.exists('/etc/cron.d'):
            os.mkdir('/etc/cron.d')
        with open('/etc/cron.d/resize', 'w') as f:
            f.write('@reboot root e2fsck -fy /dev/mmcblk0p{0}\n'.format(part))
            f.write('@reboot root resize2fs /dev/mmcblk0p{0}\n'.format(part))
            f.write('@reboot root rm /etc/cron.d/resize\n')
            f.close()
    if data.get("use_gpu_mem", None) \
            and config.get("enviro", "board").startswith("Raspberry"):
        f = filesystems.get("mmcblk0p1")
        if not f.is_mounted:
            f.mountpoint = "/boot"
            f.mount()
        cfgdata = []
        if os.path.exists('/boot/config.txt'):
            with open("/boot/config.txt", "r") as f:
                for x in f.readlines():
                    if x.startswith("gpu_mem"):
                        x = "gpu_mem=16\n"
                    cfgdata.append(x)
                if "gpu_mem=16\n" not in cfgdata:
                    cfgdata.append("gpu_mem=16\n")
            with open("/boot/config.txt", "w") as f:
                f.writelines(cfgdata)
        else:
            with open("/boot/config.txt", "w") as f:
                f.write("gpu_mem=16\n")
    if data.get("cubie_mac", None) \
            and config.get("enviro", "board").startswith("Cubie"):
        if config.get("enviro", "board") == "Cubieboard2":
            with open('/boot/uEnv.txt', 'w') as f:
                opt_str = 'extraargs=mac_addr={0}\n'
                f.write(opt_str.format(data.get("cubie_mac")))
        elif config.get("enviro", "board") == "Cubietruck":
            with open('/etc/modprobe.d/gmac.conf', 'w') as f:
                opt_str = 'options sunxi_gmac mac_str="{0}"\n'
                f.write(opt_str.format(data.get("cubie_mac")))
    if data.get("install"):
        as_job(install, data["install"])
    rootpwd = ""
    if data.get("protectRoot"):
        rootpwd = random_string(16)
        shell("passwd root", stdin="{0}\n{0}\n".format(rootpwd))
    security.initialize_firewall()
    return jsonify(rootpwd=rootpwd)
Example #3
0
 def put(self, id):
     data = request.get_json()["website"]
     site = websites.get(id)
     if not site:
         abort(404)
     if data.get("operation") == "enable":
         site.nginx_enable()
     elif data.get("operation") == "disable":
         site.nginx_disable()
     elif data.get("operation") == "enable_ssl":
         cert = certificates.get(data["cert"])
         cert.assign("website", site.id)
     elif data.get("operation") == "disable_ssl":
         site.cert.unassign("website", site.id)
     elif data.get("operation") == "update":
         id = as_job(self._put, site)
         return job_response(
             id,
             data={"website": site.serialized.update({"is_ready": False})})
     else:
         site.domain = data["domain"]
         site.port = data["port"]
         site.edit(data.get("new_name"))
     if data.get("new_name"):
         remove_record("website", id)
         push_record("website", site.serialized)
     return jsonify(website=site.serialized)
Example #4
0
 def put(self, id, time):
     data = request.get_json().get("backup")
     data["id"] = id + "/" + time
     if id and time and data:
         id = as_job(self._put, data)
         return job_response(id, data={"backup": data})
     else:
         abort(404)
Example #5
0
 def post(self):
     install, remove = [], []
     data = request.get_json()["packages"]
     for x in data:
         if x["operation"] == "install":
             install.append(x["id"])
         elif x["operation"] == "remove":
             remove.append(x["id"])
     id = as_job(self._operation, install, remove)
     return job_response(id)
Example #6
0
 def put(self, id):
     operation = request.get_json()["app"]["operation"]
     app = applications.get(id)
     if not app:
         abort(404)
     if operation == "install":
         if app.installed and not getattr(app, "upgradable", None):
             return jsonify(app=app.serialized)
         id = as_job(self._install, app)
     elif operation == "uninstall":
         if not app.installed:
             resp = jsonify(message="Application isn't yet installed")
             resp.status_code = 422
             return resp
         id = as_job(self._uninstall, app)
     else:
         return jsonify(errors={"msg": "Unknown operation"}), 422
     data = app.serialized
     data["is_ready"] = False
     return job_response(id, {"app": data})
Example #7
0
 def post(self, id, time):
     data = request.get_json()["backup"]
     id = as_job(self._post, data)
     return job_response(id)
Example #8
0
 def delete(self, id):
     id = as_job(self._delete, id, success_code=204)
     return job_response(id)
Example #9
0
 def post(self):
     data = request.get_json()["website"]
     id = as_job(self._post, data)
     return job_response(id)
Example #10
0
 def post(self):
     data = request.get_json()["filesystem"]
     id = as_job(self._post, data)
     return job_response(id, data={"filesystem": data})