Пример #1
0
def env_allocation_time_exceeded(_cloud):
    now = datetime.now()
    schedule = Schedule.objects(cloud=_cloud, start__lt=now).first()
    time_delta = now - schedule.start
    if time_delta.seconds > TOLERANCE:
        return True
    return False
Пример #2
0
 def env_allocation_time_exceeded(self):
     now = datetime.now()
     schedule = Schedule.objects(
         cloud=self.cloud, start__lt=now, end__gt=now
     ).first()
     time_delta = now - schedule.start
     if time_delta.seconds // 60 > Config["validation_grace_period"]:
         return True
     logger.warning(
         "You're still within the configurable validation grace period. Skipping validation for %s."
         % self.cloud.name
     )
     return False
Пример #3
0
def report_detailed(_logger, _start, _end):
    start = _start.replace(hour=21, minute=59, second=0)
    start_defer = start - timedelta(weeks=1)
    start_defer_id = date_to_object_id(start_defer)
    end = _end.replace(hour=22, minute=1, second=0)
    end_id = date_to_object_id(end)
    cloud_history = CloudHistory.objects(__raw__={
        "_id": {
            "$lt": end_id,
            "$gt": start_defer_id,
        },
    }).order_by("-_id")

    headers = [
        "Owner",
        "Ticket",
        "Cloud",
        "Description",
        "Systems",
        "Scheduled",
        "Duration",
    ]
    _logger.info(f"{headers[0]:<9}| "
                 f"{headers[1]:>9}| "
                 f"{headers[2]:>8}| "
                 f"{headers[3]:>10}| "
                 f"{headers[4]:>5}| "
                 f"{headers[5]:>10}| "
                 f"{headers[6]:>5}| ")

    for cloud in cloud_history:
        cloud_ref = Cloud.objects(name=cloud.name).first()
        schedule = Schedule.objects(
            Q(end__lt=end) & Q(start__gt=start)
            & Q(cloud=cloud_ref)).order_by("-_id")
        if schedule:
            delta = schedule[0].end - schedule[0].start
            description = cloud.description[:len(headers[3])]
            _logger.info(f"{cloud.owner:<9}| "
                         f"{cloud.ticket:>9}| "
                         f"{cloud.name:>8}| "
                         f"{description:>11}| "
                         f"{schedule.count():>7}| "
                         f"{str(schedule[0].start)[:10]:>9}| "
                         f"{delta.days:>8}| ")
Пример #4
0
    def POST(self, **data):
        # handle force

        force = data.get("force", False) == "True"
        if "force" in data:
            del data["force"]

        # make sure post data passed in is ready to pass to mongo engine
        result, obj_data = self.model.prep_data(data)

        # Check if there were data validation errors
        if result:
            result = ["Data validation failed: %s" % ", ".join(result)]
            cherrypy.response.status = "400 Bad Request"
        else:
            # check if object already exists
            obj_name = data["name"]
            obj = self._get_obj(obj_name)
            if obj and not force:
                result.append("%s %s already exists." %
                              (self.name.capitalize(), obj_name))
                cherrypy.response.status = "409 Conflict"
            else:
                # Create/update Operation
                try:
                    # if force and found object do an update
                    if force and obj:
                        schedule_count = 0
                        if self.name == "cloud":
                            if obj.last_redefined:
                                cloud_reservation_lock = int(
                                    conf["cloud_reservation_lock"])
                                lock_release = obj.last_redefined + datetime.timedelta(
                                    hours=cloud_reservation_lock)
                                if lock_release > datetime.datetime.now():
                                    time_left = lock_release - datetime.datetime.now(
                                    )
                                    hours = time_left.total_seconds() // 3600
                                    minutes = (time_left.total_seconds() %
                                               3600) // 60
                                    cloud_string = "%s still has %dhr %dmin remaining on a pre-schedule reservation lock" % (
                                        obj.name,
                                        hours,
                                        minutes,
                                    )
                                    result.append(cloud_string)
                                    cherrypy.response.status = "400 Bad Request"
                                    return json.dumps({"result": result})

                            schedule_count = Schedule.objects(
                                cloud=obj,
                                start__gte=datetime.datetime.now()).count()
                            notification_obj = Notification.objects(
                                cloud=obj, ticket=data["ticket"]).first()
                            if not notification_obj:
                                Notification(cloud=obj,
                                             ticket=data["ticket"]).save()

                            copy_data = data.copy()
                            history_result, history_data = CloudHistory.prep_data(
                                copy_data)
                            if history_result:
                                result.append("Data validation failed: %s" %
                                              ", ".join(history_result))
                                cherrypy.response.status = "400 Bad Request"
                            else:
                                CloudHistory(**history_data).save()

                            current_schedule = Schedule.current_schedule(
                                cloud=obj).count()
                            if current_schedule:
                                if data.get("wipe", False):
                                    if data["wipe"]:
                                        data.pop("wipe")

                        if schedule_count > 0:
                            result.append(
                                "Can't redefine cloud due to future use.")
                            cherrypy.response.status = "400 Bad Request"
                        else:
                            obj.update(**obj_data)
                            result.append("Updated %s %s" %
                                          (self.name, obj_name))
                    # otherwise create it
                    else:
                        self.model(**obj_data).save()
                        obj = self._get_obj(obj_name)
                        if self.name == "cloud":
                            notification_obj = Notification.objects(
                                cloud=obj, ticket=data["ticket"]).first()
                            if not notification_obj:
                                Notification(cloud=obj,
                                             ticket=data["ticket"]).save()
                        cherrypy.response.status = "201 Resource Created"
                        result.append("Created %s %s" % (self.name, obj_name))
                except Exception as e:
                    # TODO: make sure when this is thrown the output
                    #       points back to here and gives the end user
                    #       enough information to fix the issue
                    cherrypy.response.status = "500 Internal Server Error"
                    result.append("Error: %s" % e)
        return json.dumps({"result": result})
Пример #5
0
def report_available(_logger, _start, _end):
    start = _start.replace(hour=22, minute=0, second=0)
    end = _end.replace(hour=22, minute=0, second=0)
    next_sunday = start + timedelta(days=(6 - start.weekday()))

    hosts = Host.objects()

    _logger.info(f"QUADS report for {start.date()} to {end.date()}:")

    days = 0
    total_allocated_month = 0
    total_hosts = len(hosts)
    for _date in date_span(start, end):
        total_allocated_month += Schedule.current_schedule(date=_date).count()
        days += 1
    utilized = total_allocated_month * 100 // (total_hosts * days)
    _logger.info(f"Percentage Utilized: {utilized}%")

    schedules = Schedule.objects(build_start__ne=None, build_end__ne=None)
    total = timedelta()
    for schedule in schedules:
        total += schedule.build_end - schedule.build_start
    if schedules:
        average_build = total / len(schedules)
        _logger.info(f"Average build delta: {average_build}")

    hosts_summary = {}
    for host in hosts:
        host_type = host.name.split(".")[0].split("-")[-1]
        if not hosts_summary.get(host_type):
            hosts_summary[host_type] = []
        hosts_summary[host_type].append(host)

    headers = [
        "Server Type", "Total", "Free", "Scheduled", "2 weeks", "4 weeks"
    ]
    _logger.info(f"{headers[0]:<12}| "
                 f"{headers[1]:>5}| "
                 f"{headers[2]:>5}| "
                 f"{headers[3]:>9}| "
                 f"{headers[4]:>7}| "
                 f"{headers[5]:>7}")
    for host_type, _hosts in hosts_summary.items():
        scheduled_count = 0
        two_weeks_availability_count = 0
        four_weeks_availability_count = 0
        for host in _hosts:
            schedule = Schedule.current_schedule(host=host)
            if schedule:
                scheduled_count += 1

            two_weeks_availability = Schedule.is_host_available(
                host=host.name,
                start=next_sunday,
                end=next_sunday + timedelta(weeks=2))
            if two_weeks_availability:
                two_weeks_availability_count += 1

            four_weeks_availability = Schedule.is_host_available(
                host=host.name,
                start=next_sunday,
                end=next_sunday + timedelta(weeks=4))
            if four_weeks_availability:
                four_weeks_availability_count += 1

        free = len(_hosts) - scheduled_count
        schedule_percent = scheduled_count * 100 // len(_hosts)
        _logger.info(f"{host_type:<12}| "
                     f"{len(_hosts):>5}| "
                     f"{free:>5}| "
                     f"{schedule_percent:>8}%| "
                     f"{two_weeks_availability_count:>7}| "
                     f"{four_weeks_availability_count:>7}")