Example #1
0
    def POST(self):
        try:
            data = json.loads(web.data())
            appname = data["application"]
            configs = data["configs"]

            # get app config if not exist will create it
            get_app_uid(appname)
        except:
            yield "Mirror error occured."
            return

        cmd = ["sudo", "-u", "sheep", "/usr/local/bin/farm-statics", appname, json.dumps(configs)]

        p = Popen(cmd, stdout=PIPE, stderr=STDOUT, stdin=open("/dev/null"))
        logs = []
        for line in p.stdout:
            try:
                levelno, line = line.split(":", 1)
                levelno = int(levelno)
            except ValueError:
                levelno = logging.DEBUG
            logs.append((time.time(), line))
            yield "%d:%s" % (levelno, line)

        if p.wait() == 0:
            yield "%d:Mirror succeeded.\n" % (logging.INFO)
        else:
            yield "%d:Mirror failed.  Logs followed.\n\n\n" % (logging.ERROR)
            for timestamp, line in logs:
                yield "%d:%s %s" % (logging.ERROR, time.strftime("%H:%M:%S", time.localtime(timestamp)), line)
Example #2
0
    def POST(self):
        try:
            data = json.loads(web.data())
            appname = data["application"]

            # get app config if not exist will create it
            get_app_uid(appname)
            is_exist = load_app_option(appname, "mysql")
            if data.get("passwd"):
                del data["passwd"]

            if is_exist:
                data["passwd"] = is_exist

            data = json.dumps(data)
            cmd = ["sudo", "-u", "sheep", "LD_LIBRARY_PATH=%s" % DEFALUT_LIB_DIR, "/usr/local/bin/farm-syncdb"]
            p = Popen(cmd, stdout=PIPE, stderr=STDOUT, stdin=PIPE)
            p.stdin.write(data)
            p.stdin.close()
            pre_log = ""
            traces = []
            for line in p.stdout:
                try:
                    levelno, line = line.split(":", 1)
                    levelno = int(levelno)
                except ValueError:
                    levelno = logging.DEBUG
                traces.append((time.time(), line))
                if not pre_log:
                    pre_log = "%d:%s" % (levelno, line)
                else:
                    yield pre_log
                    pre_log = "%d:%s" % (levelno, line)

            if p.wait() == 0:
                passwd = pre_log.split(":", 1)[1]
                if not is_exist:
                    save_app_option(appname, "mysql", passwd.strip())

                yield "%d:Syncdb succeeded.\n" % (logging.INFO)
            else:
                yield "%d:Syncdb failed.  traces followed.\n\n\n" % (logging.ERROR)
                for timestamp, line in traces:
                    yield "%d:%s %s" % (logging.ERROR, time.strftime("%H:%M:%S", time.localtime(timestamp)), line)
        except:
            logger.exception("error occured.")
            yield "Syncdb failed"
Example #3
0
    def POST(self):
        web.header('X-Accel-Buffering', 'no')
        i = web.input()
        app_uid = get_app_uid(i.app_name)

        yield "%d:%s is serving you\n" % (logging.DEBUG, socket.gethostname())

        cmd = ['sudo', '-u', 'sheep', 'LD_LIBRARY_PATH=%s' % DEFAULT_LIB_DIR, '/usr/local/bin/farm-deploy', i.app_name,
               i.app_url, str(app_uid),]

        extend_config = {}
        config_value = load_app_option(i.app_name, 'mysql')
        if config_value:
            extend_config['mysql'] = config_value

        if extend_config:
            cmd += ['--app-configs', json.dumps(extend_config)]

        p = Popen(cmd, stdout=PIPE, stderr=STDOUT, stdin=open('/dev/null'))
        for line in p.stdout:
            yield line