Ejemplo n.º 1
0
def on(crontab_id):
    crontab = Crontab.get(crontab_id)
    if not crontab:
        return jsonify({'r': 1, 'msg': 'not found'})
        
    if get_crontab(crontab):
        return jsonify({'r': 1, 'msg': 'already on'})

    add_crontab(crontab)
    return jsonify({'r': 0, 'msg': 'ok'})
Ejemplo n.º 2
0
def off(crontab_id):
    crontab = Crontab.get(crontab_id)
    if not crontab:
        return jsonify({'r': 1, 'msg': 'not found'})
        
    if not get_crontab(crontab):
        return jsonify({'r': 1, 'msg': 'already off'})

    remove_crontab(crontab)
    return jsonify({'r': 0, 'msg': 'ok'})
Ejemplo n.º 3
0
def off(crontab_id):
    crontab = Crontab.get(crontab_id)
    if not crontab:
        return jsonify({'r': 1, 'msg': 'not found'})

    if not get_crontab(crontab):
        return jsonify({'r': 1, 'msg': 'already off'})

    remove_crontab(crontab)
    return jsonify({'r': 0, 'msg': 'ok'})
Ejemplo n.º 4
0
def on(crontab_id):
    crontab = Crontab.get(crontab_id)
    if not crontab:
        return jsonify({'r': 1, 'msg': 'not found'})

    if get_crontab(crontab):
        return jsonify({'r': 1, 'msg': 'already on'})

    add_crontab(crontab)
    return jsonify({'r': 0, 'msg': 'ok'})
Ejemplo n.º 5
0
def run_crontab(crontab_id):
    crontab = Crontab.get(crontab_id)
    if not crontab:
        return

    # debug 打开并不真正执行任务.
    if CRONTAB_DEBUG:
        return

    props = crontab.props
    callback_url = url_for('crontab.callback',
                           crontab_id=crontab.id,
                           _external=True)
    try:
        r = eru.deploy_private(
            props.get('group', ''),
            props.get('pod', ''),
            props.get('appname', ''),
            1,
            1,
            props.get('version', ''),
            props.get('entrypoint', ''),
            props.get('env', ''),
            props.get('network_ids', []),
            callback_url=callback_url,
        )
    except EruException as e:
        print e
        return

    task_id = r['tasks'][0]
    while True:
        time.sleep(1)
        try:
            task = eru.get_task(task_id)
            if not task['finished']:
                continue

            container_id = task['props']['container_ids'][0]
            cronjob = CronJob.get_by_container_id(container_id)
            if not cronjob:
                crontab.add_job(container_id)

            break
        except EruException as e:
            print e
            break
Ejemplo n.º 6
0
def run_crontab(crontab_id):
    crontab = Crontab.get(crontab_id)
    if not crontab:
        return

    # debug 打开并不真正执行任务.
    if CRONTAB_DEBUG:
        return

    props = crontab.props
    callback_url = url_for('crontab.callback', crontab_id=crontab.id, _external=True)
    try:
        r = eru.deploy_private(
            props.get('group', ''),
            props.get('pod', ''),
            props.get('appname', ''),
            1,
            1,
            props.get('version', ''),
            props.get('entrypoint', ''),
            props.get('env', ''),
            props.get('network_ids', []),
            callback_url=callback_url,
        )
    except EruException as e:
        print e
        return

    task_id = r['tasks'][0]
    while True:
        time.sleep(1)
        try:
            task = eru.get_task(task_id)
            if not task['finished']:
                continue

            container_id = task['props']['container_ids'][0]
            cronjob = CronJob.get_by_container_id(container_id)
            if not cronjob:
                crontab.add_job(container_id)

            break
        except EruException as e:
            print e
            break
Ejemplo n.º 7
0
def create():
    if request.method == "GET":
        try:
            networks = eru.list_network(g.start, g.limit)
            groups = eru.list_groups(g.start, g.limit)
            group = groups and groups[0] and groups[0]["name"] or None
            pods = group and eru.list_group_pods(group) or []
        except EruException:
            groups = []
            networks = []
            pods = []
        return rt("/create.html", groups=groups, networks=networks, pods=pods)

    name = request.form.get("name", "")
    if not name:
        flash(u"给cron取个名字吧", "error")
        return redirect(url_for("crontab.create"))

    crontab_kwargs = parse_crontab(request.form.get("cron", ""))
    if not crontab_kwargs:
        flash(u"cron 参数不对", "error")
        return redirect(url_for("crontab.create"))

    props = {key: request.form.get(key, "") for key in property_keys}
    kw = props.copy()
    kw.pop("network_ids", None)
    kw.pop("env", "")
    if not all(kw.values()):
        flash(u"输入参数不对", "error")
        return redirect(url_for("crontab.create"))

    network_ids = request.form.getlist("network_ids") or []
    props["network_ids"] = network_ids
    props["env"] = request.form.get("env", "prod")

    c = Crontab.create(name, crontab_kwargs, props)
    if not c:
        flash(u"创建出错", "error")
        return redirect(url_for("crontab.create"))

    return redirect(url_for("crontab.crontab", crontab_id=c.id))
Ejemplo n.º 8
0
def create():
    if request.method == 'GET':
        try:
            networks = eru.list_network(g.start, g.limit)
            groups = eru.list_groups(g.start, g.limit)
            group = groups and groups[0] and groups[0]['name'] or None
            pods = group and eru.list_group_pods(group) or []
        except EruException:
            groups = []
            networks = []
            pods = []
        return rt('/create.html', groups=groups, networks=networks, pods=pods)

    name = request.form.get('name', '')
    if not name:
        flash(u'给cron取个名字吧', 'error')
        return redirect(url_for('crontab.create'))

    crontab_kwargs = parse_crontab(request.form.get('cron', ''))
    if not crontab_kwargs:
        flash(u'cron 参数不对', 'error')
        return redirect(url_for('crontab.create'))

    props = {key: request.form.get(key, '') for key in property_keys}
    kw = props.copy()
    kw.pop('network_ids', None)
    kw.pop('env', '')
    if not all(kw.values()):
        flash(u'输入参数不对', 'error')
        return redirect(url_for('crontab.create'))

    network_ids = request.form.getlist('network_ids') or []
    props['network_ids'] = network_ids
    props['env'] = request.form.get('env', 'prod')

    c = Crontab.create(name, crontab_kwargs, props)
    if not c:
        flash(u'创建出错', 'error')
        return redirect(url_for('crontab.create'))

    return redirect(url_for('crontab.crontab', crontab_id=c.id))
Ejemplo n.º 9
0
def list_crons():
    total, crontabs = Crontab.list_all(g.start, g.limit)
    return rt("/list.html", crontabs=crontabs, total=total, endpoint="crontab.list_crons")
Ejemplo n.º 10
0
def _get_crontab(crontab_id):
    c = Crontab.get(crontab_id)
    if not c:
        abort(404)
    return c
Ejemplo n.º 11
0
def list_crons():
    total, crontabs = Crontab.list_all(g.start, g.limit)
    return rt('/list.html',
              crontabs=crontabs,
              total=total,
              endpoint='crontab.list_crons')
Ejemplo n.º 12
0
def _get_crontab(crontab_id):
    c = Crontab.get(crontab_id)
    if not c:
        abort(404)
    return c