def list_policies(): """ Get all polices. Returns: List of policies """ keys = PolicyModel.query().fetch(keys_only=True) policies_list = [] for key in keys: policies_list.append(key.id()) return json.dumps(policies_list)
def schedule(): """ Checks if it's time to run a schedule. Returns: """ logging.debug("From Cron start /tasks/schedule") keys = PolicyModel.query().fetch(keys_only=True) for key in keys: logging.debug("Creating deferred task for %s", key.id()) deferred.defer(policy_tasks.policy_checker, key.id()) return 'ok', 200
def del_policy(): """ Delete a policy Returns: """ name = request.args.get('policy') res = PolicyModel.query(PolicyModel.Name == name).get() if not res: return 'not found', 404 res.key.delete() return 'ok', 200
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
def del_policy(): """ Delete a policy Returns: """ name = request.args.get("policy") with client.context(): res = PolicyModel.query(PolicyModel.Name == name).get() if not res: return "Policy '{}' not found.".format(name), 404 res.key.delete() return "ok", 200
def list_policies(): """ Get all polices. Returns: List of policies """ policies_list = [] with client.context(): verbose = request.args.get("verbose") == "true" if verbose: policies = PolicyModel.query().fetch() for policy in policies: policies_list.append({ "name": policy.Name, "displayName": policy.DisplayName }) else: keys = PolicyModel.query().fetch(keys_only=True) for key in keys: policies_list.append(key.id()) return json.dumps(policies_list)
def list_policies(): """ Get all polices. Returns: List of policies """ policies_list = [] client = ndb.Client() with client.context(): keys = PolicyModel.query().fetch(keys_only=True) for key in keys: policies_list.append(key.id()) return json.dumps(policies_list)
def schedule(): """ Checks if it's time to run a schedule. Returns: """ logging.debug("From Cron start /tasks/schedule") client = ndb.Client() with client.context(): keys = PolicyModel.query().fetch(keys_only=True) for key in keys: logging.debug("Creating deferred task for %s", key.id()) policy_tasks.policy_checker(key.id()) return "ok", 200
def del_policy(): """ Delete a policy Returns: """ name = request.args.get("policy") client = ndb.Client() with client.context(): res = PolicyModel.query(PolicyModel.Name == name).get() if not res: return "not found", 404 res.key.delete() return "ok", 200
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
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
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
def get_policy(): """ Get policy. Returns: policy json """ name = request.args.get('policy') res = PolicyModel.query(PolicyModel.Name == name).get() logging.debug(res) if not res: return 'not found', 404 policy = {} policy.update({'name': res.Name}) policy.update({'schedulename': res.Schedule}) policy.update({'tags': res.Tags}) policy.update({'projects': res.Projects}) return json.dumps(policy)
def get_policy(): """ Get policy. Returns: policy json """ policy = {} name = request.args.get("policy") with client.context(): res = PolicyModel.query(PolicyModel.Name == name).get() logging.debug(res) if not res: return "not found", 404 policy.update({"name": res.Name}) policy.update({"schedulename": res.Schedule}) policy.update({"tags": res.Tags}) policy.update({"projects": res.Projects}) return json.dumps(policy)
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