Ejemplo n.º 1
0
def add_policy():
    """
    Add policy.
    Returns:

    """
    logging.debug(json.dumps(request.json))
    name = request.json["name"]
    display_name = request.json.get("displayname", name)
    tags = request.json["tags"]
    projects = request.json["projects"]
    schedule_name = request.json["schedulename"]
    with client.context():
        res = SchedulesModel.query(SchedulesModel.Name == schedule_name).get()
        if not res:
            return "Schedule '{}' not found.".format(schedule_name), 404

        policy_model = PolicyModel()
        policy_model.Name = name
        policy_model.DisplayName = display_name
        policy_model.Tags = tags
        policy_model.Projects = projects
        policy_model.Schedule = schedule_name
        policy_model.key = ndb.Key("PolicyModel", name)
        policy_model.put()
    return "ok", 200
Ejemplo n.º 2
0
def add_policy():
    """
    Add policy.
    Returns:

    """
    logging.debug(json.dumps(request.json))
    name = request.json["name"]
    tags = request.json["tags"]
    projects = request.json["projects"]
    schedule_name = request.json["schedulename"]
    client = ndb.Client()
    with client.context():
        res = SchedulesModel.query(SchedulesModel.Name == schedule_name).get()
        if not res:
            return "not found", 404

        policy_model = PolicyModel()
        policy_model.Name = name
        policy_model.Tags = tags
        policy_model.Projects = projects
        policy_model.Schedule = schedule_name
        policy_model.key = ndb.Key("PolicyModel", name)
        policy_model.put()
    return "ok", 200
Ejemplo n.º 3
0
def list_schedules():
    """
    Get all schedules.
    Returns: A list of schedules

    """
    keys = SchedulesModel.query().fetch(keys_only=True)
    schedules_list = []
    for key in keys:
        schedules_list.append(key.id())
    return json.dumps(schedules_list)
Ejemplo n.º 4
0
def list_schedules():
    """
    Get all schedules.
    Returns: A list of schedules

    """
    schedules_list = []
    with client.context():
        verbose = request.args.get("verbose") == "true"
        if verbose:
            schedules = SchedulesModel.query().fetch()
            for schedule in schedules:
                schedules_list.append({
                    "name": schedule.Name,
                    "displayName": schedule.DisplayName
                })
        else:
            keys = SchedulesModel.query().fetch(keys_only=True)
            for key in keys:
                schedules_list.append(key.id())
    return json.dumps(schedules_list)
Ejemplo n.º 5
0
def policy_checker(name):
    """
    Check if there is a need to take an action for a policy.
    Args:
        name: policy name

    Returns:

    """
    policy = PolicyModel.query(PolicyModel.Name == name).get()
    if not policy:
        logging.error('Policy %s not found!', name)
        return 'not found', 404
    schedule = SchedulesModel.query(
        SchedulesModel.Name == policy.Schedule).get()
    if not schedule:
        logging.error('Schedule %s not found!', policy.Schedule)
        return 'not found', 404
    logging.debug("Time at Timezone %s is %s", schedule.Timezone,
                  tz.get_time_at_timezone(schedule.Timezone))
    day, hour = tz.convert_time_to_index(
        tz.get_time_at_timezone(schedule.Timezone))
    logging.debug("Working on day  %s hour  %s", day, hour)
    arr = np.asarray(schedule.Schedule['__ndarray__'], dtype=np.int).flatten()
    matrix_size = schedule.Schedule['Shape'][0] * schedule.Schedule['Shape'][
        1]
    prev = utils.get_prev_idx(day * 24 + hour, matrix_size)
    now = arr[day * 24 + hour]
    prev = arr[prev]
    logging.error("Previous state %s current %s", prev, now)
    if now == prev:
        # do nothing
        logging.info("Nothing should be done for %s", name)
        return 'ok', 200
    else:
        # stop/start
        logging.info("State is changing for %s to %s", name, now)
        # for each tag lets do it
        for tag in policy.Tags:
            for project in policy.Projects:
                task = taskqueue.add(queue_name='zorya-tasks',
                                     url="/tasks/change_state",
                                     method='GET',
                                     params={
                                         'project': project,
                                         'tagkey': tag.keys(),
                                         'tagvalue': tag.values(),
                                         'action': now
                                     })
                logging.debug('Task %s enqueued, ETA %s.', task.name,
                              task.eta)
        return 'ok', 200
Ejemplo n.º 6
0
def list_schedules():
    """
    Get all schedules.
    Returns: A list of schedules

    """
    schedules_list = []
    client = ndb.Client()
    with client.context():
        keys = SchedulesModel.query().fetch(keys_only=True)
        for key in keys:
            schedules_list.append(key.id())
    return json.dumps(schedules_list)
Ejemplo n.º 7
0
def get_schedule():
    """
    Get a schedule.
    Returns: schedule json

    """
    name = request.args.get('schedule')
    res = SchedulesModel.query(SchedulesModel.Name == name).get()
    if not res:
        return 'not found', 404
    schedule = {}
    schedule.update({'name': res.Name})
    schedule.update(res.Schedule)
    schedule.update({'timezone': res.Timezone})
    logging.debug(json.dumps(res.Schedule))
    return json.dumps(schedule)
Ejemplo n.º 8
0
def del_schedule():
    """
    Delete a schedule.
    Returns:

    """
    name = request.args.get('schedule')
    res = SchedulesModel.query(SchedulesModel.Name == name).get()
    if not res:
        return 'not found', 404
    policy = PolicyModel.query(PolicyModel.Schedule == name).get()
    if policy:
        return 'Forbidden policy {} is using the schedule'.format(
            policy.Name), 403
    res.key.delete()
    return 'ok', 200
Ejemplo n.º 9
0
def add_schedule():
    """
    Add a schedule.
    Returns:

    """
    with client.context():
        schedules_model = SchedulesModel()
        schedules_model.Schedule = {
            "dtype": request.json["dtype"],
            "Corder": request.json["Corder"],
            "Shape": request.json["Shape"],
            "__ndarray__": request.json["__ndarray__"],
        }

        schedules_model.Name = request.json["name"]
        schedules_model.DisplayName = request.json.get(
            "displayname", request.json.get("name"))
        schedules_model.Timezone = request.json["timezone"]
        schedules_model.key = ndb.Key("SchedulesModel", request.json["name"])
        schedules_model.put()
    return "ok", 200
Ejemplo n.º 10
0
def del_schedule():
    """
    Delete a schedule.
    Returns:

    """
    name = request.args.get("schedule")
    with client.context():
        res = SchedulesModel.query(SchedulesModel.Name == name).get()
        if not res:
            return "Schedule '{}' not found.".format(name), 404
        policy = PolicyModel.query(PolicyModel.Schedule == name).get()
        if policy:
            return "Forbidden. Schedule '{}' is in use by policy '{}'.".format(
                name, policy.Name), 403
        res.key.delete()
    return "ok", 200
Ejemplo n.º 11
0
def get_schedule():
    """
    Get a schedule.
    Returns: schedule json

    """
    name = request.args.get("schedule")
    schedule = {}
    with client.context():
        res = SchedulesModel.query(SchedulesModel.Name == name).get()
        if not res:
            return "not found", 404
        schedule.update({"name": res.Name})
        schedule.update(res.Schedule)
        schedule.update({"timezone": res.Timezone})
        logging.debug(json.dumps(res.Schedule))
    return json.dumps(schedule)
Ejemplo n.º 12
0
def del_schedule():
    """
    Delete a schedule.
    Returns:

    """
    name = request.args.get("schedule")
    with client.context():
        res = SchedulesModel.query(SchedulesModel.Name == name).get()
        if not res:
            return "not found", 404
        policy = PolicyModel.query(PolicyModel.Schedule == name).get()
        if policy:
            return "Forbidden policy {} is using the schedule".format(
                policy.Name), 403
        res.key.delete()
    return "ok", 200
Ejemplo n.º 13
0
def add_schedule():
    """
    Add a schedule.
    Returns:

    """
    schedules_model = SchedulesModel()
    schedules_model.Schedule = {
        'dtype': request.json['dtype'],
        'Corder': request.json['Corder'],
        'Shape': request.json['Shape'],
        '__ndarray__': request.json['__ndarray__']
    }

    schedules_model.Name = request.json['name']
    schedules_model.Timezone = request.json['timezone']
    schedules_model.key = ndb.Key('SchedulesModel', request.json['name'])
    schedules_model.put()
    return 'ok', 200
Ejemplo n.º 14
0
def add_policy():
    """
    Add policy.
    Returns:

    """
    logging.debug(json.dumps(request.json))
    name = request.json['name']
    tags = request.json['tags']
    projects = request.json['projects']
    schedule_name = request.json['schedulename']

    res = SchedulesModel.query(SchedulesModel.Name == schedule_name).get()
    if not res:
        return 'not found', 404

    policy_model = PolicyModel()
    policy_model.Name = name
    policy_model.Tags = tags
    policy_model.Projects = projects
    policy_model.Schedule = schedule_name
    policy_model.key = ndb.Key('PolicyModel', name)
    policy_model.put()
    return 'ok', 200
Ejemplo n.º 15
0
def policy_checker(name):
    """
    Check if there is a need to take an action for a policy.
    Args:
        name: policy name

    Returns:

    """
    client = ndb.Client()
    with client.context():
        policy = PolicyModel.query(PolicyModel.Name == name).get()
        if not policy:
            logging.error("Policy %s not found!", name)
            return "not found", 404
        schedule = SchedulesModel.query(
            SchedulesModel.Name == policy.Schedule).get()
        if not schedule:
            logging.error("Schedule %s not found!", policy.Schedule)
            return "not found", 404
        logging.debug(
            "Time at Timezone %s is %s",
            schedule.Timezone,
            tz.get_time_at_timezone(schedule.Timezone),
        )
        day, hour = tz.convert_time_to_index(
            tz.get_time_at_timezone(schedule.Timezone))
        logging.debug("Working on day  %s hour  %s", day, hour)
        arr = np.asarray(schedule.Schedule["__ndarray__"],
                         dtype=np.int).flatten()
        matrix_size = schedule.Schedule["Shape"][0] * schedule.Schedule[
            "Shape"][1]
        prev = utils.get_prev_idx(day * 24 + hour, matrix_size)
        now = arr[day * 24 + hour]
        prev = arr[prev]
        logging.debug("Previous state %s current %s", prev, now)
        if now == prev:
            # do nothing
            logging.info("Nothing should be done for %s", name)
            return "ok", 200
        else:
            # stop/start
            logging.info("State is changing for %s to %s", name, now)
            # for each tag lets do it

            task_client = tasks_v2.CloudTasksClient()
            task = {
                "app_engine_http_request": {  # Specify the type of request.
                    "http_method": "POST",
                    "relative_uri": "/tasks/change_state",
                }
            }
            parent = task_client.queue_path(
                queue="zorya-tasks",
                project=utils.get_project_id(),
                location=location.get_location(),
            )
            for tag in policy.Tags:
                for project in policy.Projects:
                    payload = {
                        "project": project,
                        "tagkey": next(iter(tag)),
                        "tagvalue": tag[next(iter(tag))],
                        "action": str(now),
                    }
                    task["app_engine_http_request"]["body"] = (
                        json.dumps(payload)).encode()
                    response = task_client.create_task(parent, task)
                    logging.debug("Task %s enqueued", response.name)
        return "ok", 200