Python datetime.datetime.utcnow() Examples

The following are 30 code examples for showing how to use datetime.datetime.utcnow(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module datetime.datetime , or try the search function .

Example 1
Project: drydock   Author: airshipit   File: state.py    License: Apache License 2.0 6 votes vote down vote up
def maintain_leadership(self, leader_id):
        """The active leader reaffirms its existence.

        :param leader_id: uuid.UUID ID of the leader
        """
        try:
            with self.db_engine.connect() as conn:
                query = self.active_instance_tbl.update().where(
                    self.active_instance_tbl.c.identity == leader_id.
                    bytes).values(last_ping=datetime.utcnow())
                rs = conn.execute(query)
                rc = rs.rowcount

                if rc == 1:
                    return True
                else:
                    return False
        except Exception as ex:
            self.logger.error("Error maintaining leadership: %s" % str(ex)) 
Example 2
Project: drydock   Author: airshipit   File: test_postgres_builddata.py    License: Apache License 2.0 6 votes vote down vote up
def test_build_data_insert_iwth_collected_date(self, blank_state):
        """Test that build data can be inserted specifying collection date."""
        build_data_fields = {
            'node_name': 'foo',
            'generator': 'hello_world',
            'data_format': 'text/plain',
            'data_element': 'Hello World!',
            'task_id': uuid.uuid4(),
            'collected_date': datetime.utcnow(),
        }

        build_data = objects.BuildData(**build_data_fields)

        result = blank_state.post_build_data(build_data)

        assert result 
Example 3
Project: drydock   Author: airshipit   File: test_postgres_builddata.py    License: Apache License 2.0 6 votes vote down vote up
def test_build_data_select(self, blank_state):
        """Test that build data can be deserialized from the database."""
        build_data_fields = {
            'node_name': 'foo',
            'generator': 'hello_world',
            'data_format': 'text/plain',
            'data_element': 'Hello World!',
            'task_id': uuid.uuid4(),
            'collected_date': datetime.utcnow(),
        }

        build_data = objects.BuildData(**build_data_fields)

        result = blank_state.post_build_data(build_data)

        assert result

        bd_list = blank_state.get_build_data()

        assert len(bd_list) == 1

        assert bd_list[0].to_dict() == build_data.to_dict() 
Example 4
Project: aegea   Author: kislyuk   File: s3.py    License: Apache License 2.0 6 votes vote down vote up
def describe_bucket_worker(bucket):
    bucket.LocationConstraint = clients.s3.get_bucket_location(Bucket=bucket.name)["LocationConstraint"]
    cloudwatch = resources.cloudwatch
    bucket_region = bucket.LocationConstraint or "us-east-1"
    if bucket_region != cloudwatch.meta.client.meta.region_name:
        cloudwatch = boto3.Session(region_name=bucket_region).resource("cloudwatch")
    data = get_cloudwatch_metric_stats("AWS/S3", "NumberOfObjects",
                                       start_time=datetime.utcnow() - timedelta(days=2),
                                       end_time=datetime.utcnow(), period=3600, BucketName=bucket.name,
                                       StorageType="AllStorageTypes", resource=cloudwatch)
    bucket.NumberOfObjects = int(data["Datapoints"][-1]["Average"]) if data["Datapoints"] else None
    data = get_cloudwatch_metric_stats("AWS/S3", "BucketSizeBytes",
                                       start_time=datetime.utcnow() - timedelta(days=2),
                                       end_time=datetime.utcnow(), period=3600, BucketName=bucket.name,
                                       StorageType="StandardStorage", resource=cloudwatch)
    bucket.BucketSizeBytes = format_number(data["Datapoints"][-1]["Average"]) if data["Datapoints"] else None
    return bucket 
Example 5
Project: aegea   Author: kislyuk   File: billing.py    License: Apache License 2.0 6 votes vote down vote up
def ls(args):
    bucket = resources.s3.Bucket(args.billing_reports_bucket.format(account_id=ARN.get_account_id()))
    now = datetime.utcnow()
    year = args.year or now.year
    month = str(args.month or now.month).zfill(2)
    next_year = year + ((args.month or now.month) + 1) // 12
    next_month = str(((args.month or now.month) + 1) % 12).zfill(2)
    manifest_name = "aegea/{report}/{yr}{mo}01-{next_yr}{next_mo}01/{report}-Manifest.json"
    manifest_name = manifest_name.format(report=__name__, yr=year, mo=month, next_yr=next_year, next_mo=next_month)
    try:
        manifest = json.loads(bucket.Object(manifest_name).get().get("Body").read())
        for report_key in manifest["reportKeys"]:
            report = BytesIO(bucket.Object(report_key).get().get("Body").read())
            with gzip.GzipFile(fileobj=report) as fh:
                reader = csv.DictReader(fh)
                for line in reader:
                    page_output(tabulate(filter_line_items(reader, args), args))
    except ClientError as e:
        msg = 'Unable to get report {} from {}: {}. Run "aegea billing configure" to enable reports.'
        raise AegeaException(msg.format(manifest_name, bucket, e)) 
Example 6
Project: aegea   Author: kislyuk   File: logs.py    License: Apache License 2.0 6 votes vote down vote up
def logs(args):
    if args.log_group and (args.log_stream or args.start_time or args.end_time):
        if args.export and args.print_s3_urls:
            return ["s3://{}/{}".format(f.bucket_name, f.key) for f in export_log_files(args)]
        elif args.export:
            return export_and_print_log_events(args)
        else:
            return print_log_events(args)
    table = []
    group_cols = ["logGroupName"]
    stream_cols = ["logStreamName", "lastIngestionTime", "storedBytes"]
    args.columns = group_cols + stream_cols
    for group in paginate(clients.logs.get_paginator("describe_log_groups")):
        if args.log_group and group["logGroupName"] != args.log_group:
            continue
        n = 0
        for stream in paginate(clients.logs.get_paginator("describe_log_streams"),
                               logGroupName=group["logGroupName"], orderBy="LastEventTime", descending=True):
            now = datetime.utcnow().replace(microsecond=0)
            stream["lastIngestionTime"] = now - datetime.utcfromtimestamp(stream.get("lastIngestionTime", 0) // 1000)
            table.append(dict(group, **stream))
            n += 1
            if n >= args.max_streams_per_group:
                break
    page_output(tabulate(table, args)) 
Example 7
Project: premeStock   Author: supthunder   File: monitor.py    License: MIT License 6 votes vote down vote up
def slackMsg(item,color,link, size):
    # line 101

    if slack_token == "":
        return
    sc = SlackClient(slack_token)

    text = item+"\n"
    text += color+'\n'
    text += size.title()+'\n'
    text += link+'\n'
    text += "Restock!"+'\n'
    text += str(datetime.utcnow().strftime('%H:%M:%S.%f')[:-3])

    sc.api_call(
      "chat.postMessage",
      channel="#test",
      text=text
    ) 
Example 8
Project: premeStock   Author: supthunder   File: monitor.py    License: MIT License 6 votes vote down vote up
def sendTweet(item,color,link, size):
    # line 102
    auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
    auth.set_access_token(A_TOKEN, A_TOKEN_SECRET)
    api = tweepy.API(auth)

    tweet = item+"\n"
    tweet += color+'\n'
    tweet += size.title()+'\n'
    tweet += link+'\n'
    tweet += "Restock!"+'\n'
    tweet += str(datetime.utcnow().strftime('%H:%M:%S.%f')[:-3])

    try:
        api.update_status(tweet)
        print(tweet)
    except:
        print("Error sending tweet!") 
Example 9
Project: ssm-cache-python   Author: alexcasalboni   File: cache_test.py    License: MIT License 6 votes vote down vote up
def test_should_refresh(self):
        """ Unit test _should_refresh private method """
        # without max age
        ref = Refreshable(None)
        self.assertFalse(ref._should_refresh())

        # with max age and no data
        ref = Refreshable(max_age=10)
        self.assertTrue(ref._should_refresh())

        # manually force refresh time
        ref._last_refresh_time = datetime.utcnow()
        # with max age and last refreshed date OK
        self.assertFalse(ref._should_refresh())

        # freeze_time will pretend 10 seconds have passed!
        with freeze_time(lambda: datetime.utcnow() + timedelta(seconds=10)):
            # with max age and last refreshed date KO
            self.assertTrue(ref._should_refresh()) 
Example 10
Project: ssm-cache-python   Author: alexcasalboni   File: cache_test.py    License: MIT License 6 votes vote down vote up
def test_main_with_expiration_group(self):
        """ Test group case with expiration """
        group = SSMParameterGroup(max_age=300)
        param_1 = group.parameter("my_param_1")
        param_2 = group.parameter("my_param_2")
        param_3 = group.parameter("my_param_3")

        # individual params don't share max_age internally (for now)
        for param in (param_1, param_2, param_3):
            self.assertEqual(param._max_age, None)

        # force fetch
        group.refresh()

        # pretend time has passed (for the group)
        group._last_refresh_time = datetime.utcnow() - timedelta(seconds=301)
        self.assertTrue(group._should_refresh())
        self.assertTrue(param_1._should_refresh())
        self.assertTrue(param_2._should_refresh())
        self.assertTrue(param_3._should_refresh()) 
Example 11
Project: controller   Author: deis   File: pod.py    License: MIT License 6 votes vote down vote up
def _handle_long_image_pulling(self, reason, pod):
        """
        If pulling an image is taking long (1 minute) then return how many seconds
        the pod ready state timeout should be extended by

        Return value is an int that represents seconds
        """
        # only apply once
        if getattr(self, '_handle_long_image_pulling_applied', False):
            return 0

        if reason is not 'Pulling':
            return 0

        # last event should be Pulling in this case
        event = self.events(pod).pop()
        # see if pull operation has been happening for over 1 minute
        seconds = 60  # time threshold before padding timeout
        start = self.parse_date(event['firstTimestamp'])
        if (start + timedelta(seconds=seconds)) < datetime.utcnow():
            # make it so function doesn't do processing again
            setattr(self, '_handle_long_image_pulling_applied', True)
            return 600

        return 0 
Example 12
Project: aws-ops-automator   Author: awslabs   File: task_tracking_table.py    License: Apache License 2.0 6 votes vote down vote up
def _run_local_stream_event(table, table_action, new_item, old_item=None, context=None):

        # if not running in lambda environment create event that normally results from dynamodb inserts and pass directly
        # to the main lambda handler to simulate an event triggered by the dynamodb stream

        if old_item is None:
            old_item = {}
        account = os.getenv(handlers.ENV_OPS_AUTOMATOR_ACCOUNT)
        region = services.get_session().region_name
        event = {
            "Records": [
                {
                    "eventName": table_action,
                    "eventSourceARN": "arn:aws:dynamodb:{}:{}:table/{}/stream/{}".format(region, account, table,
                                                                                         datetime.utcnow().isoformat()),
                    "eventSource": "aws:dynamodb",
                    "dynamodb": {
                        "NewImage": build_record(new_item),
                        "OldImage": build_record(old_item)
                    }
                }]
        }

        handler = handlers.get_class_for_handler("TaskTrackingHandler")(event, context)
        handler.handle_request() 
Example 13
Project: aws-ops-automator   Author: awslabs   File: schedule_handler.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, event, context):
        """
        Initializes the instance.
        :param event: event to handle
        :param context: CLambda context
        """
        self._context = context
        self._event = event
        self._table = None

        # Setup logging
        classname = self.__class__.__name__
        dt = datetime.utcnow()
        logstream = LOG_STREAM.format(classname, dt.year, dt.month, dt.day)
        self._logger = QueuedLogger(logstream=logstream, buffersize=50, context=context)

        self.configuration_update = ScheduleHandler.is_config_update(self._event)
        if self.configuration_update:
            if "OldImage" in self._event["Records"][0]["dynamodb"]:
                self.updated_task = self._event["Records"][0]["dynamodb"]["OldImage"][configuration.CONFIG_TASK_NAME]["S"]
            else:
                self.updated_task = self._event["Records"][0]["dynamodb"]["NewImage"][configuration.CONFIG_TASK_NAME]["S"]

        self.execute_task_request = self.is_execute_event(self._event)
        self.executed_task_name = event.get(handlers.HANDLER_EVENT_TASK_NAME, "") if self.execute_task_request else None 
Example 14
Project: botbuilder-python   Author: microsoft   File: test_adapter.py    License: MIT License 6 votes vote down vote up
def process_activity(
        self, activity: Activity, logic: Callable[[TurnContext], Awaitable]
    ):
        self._conversation_lock.acquire()
        try:
            # ready for next reply
            if activity.type is None:
                activity.type = ActivityTypes.message

            activity.channel_id = self.template.channel_id
            activity.from_property = self.template.from_property
            activity.recipient = self.template.recipient
            activity.conversation = self.template.conversation
            activity.service_url = self.template.service_url

            activity.id = str((self._next_id))
            self._next_id += 1
        finally:
            self._conversation_lock.release()

        activity.timestamp = activity.timestamp or datetime.utcnow()
        await self.run_pipeline(TurnContext(self, activity), logic) 
Example 15
Project: botbuilder-python   Author: microsoft   File: activity_helper.py    License: MIT License 6 votes vote down vote up
def create_activity_reply(activity: Activity, text: str = None, locale: str = None):
    """Helper to create reply object."""
    return Activity(
        type=ActivityTypes.message,
        timestamp=datetime.utcnow(),
        from_property=ChannelAccount(
            id=getattr(activity.recipient, "id", None),
            name=getattr(activity.recipient, "name", None),
        ),
        recipient=ChannelAccount(
            id=activity.from_property.id, name=activity.from_property.name
        ),
        reply_to_id=activity.id,
        service_url=activity.service_url,
        channel_id=activity.channel_id,
        conversation=ConversationAccount(
            is_group=activity.conversation.is_group,
            id=activity.conversation.id,
            name=activity.conversation.name,
        ),
        text=text or "",
        locale=locale or "",
        attachments=[],
        entities=[],
    ) 
Example 16
Project: botbuilder-python   Author: microsoft   File: parent_bot.py    License: MIT License 6 votes vote down vote up
def _create_reply(self, activity) -> Activity:
        return Activity(
            type=ActivityTypes.message,
            timestamp=datetime.utcnow(),
            from_property=ChannelAccount(
                id=activity.recipient.id, name=activity.recipient.name
            ),
            recipient=ChannelAccount(
                id=activity.from_property.id, name=activity.from_property.name
            ),
            reply_to_id=activity.id,
            service_url=activity.service_url,
            channel_id=activity.channel_id,
            conversation=ConversationAccount(
                is_group=activity.conversation.is_group,
                id=activity.conversation.id,
                name=activity.conversation.name,
            ),
            text="",
            locale=activity.locale,
        ) 
Example 17
Project: botbuilder-python   Author: microsoft   File: app.py    License: MIT License 6 votes vote down vote up
def on_error(context: TurnContext, error: Exception):
    # This check writes out errors to console log .vs. app insights.
    # NOTE: In production environment, you should consider logging this to Azure
    #       application insights.
    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
    traceback.print_exc()

    # Send a message to the user
    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )
    # Send a trace activity if we're talking to the Bot Framework Emulator
    if context.activity.channel_id == "emulator":
        # Create a trace activity that contains the error object
        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )
        # Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.send_activity(trace_activity) 
Example 18
Project: botbuilder-python   Author: microsoft   File: app.py    License: MIT License 6 votes vote down vote up
def on_error(context: TurnContext, error: Exception):
    # This check writes out errors to console log .vs. app insights.
    # NOTE: In production environment, you should consider logging this to Azure
    #       application insights.
    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)

    # Send a message to the user
    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )
    # Send a trace activity if we're talking to the Bot Framework Emulator
    if context.activity.channel_id == "emulator":
        # Create a trace activity that contains the error object
        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )
        # Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.send_activity(trace_activity) 
Example 19
Project: botbuilder-python   Author: microsoft   File: app.py    License: MIT License 6 votes vote down vote up
def on_error(context: TurnContext, error: Exception):
    # This check writes out errors to console log .vs. app insights.
    # NOTE: In production environment, you should consider logging this to Azure
    #       application insights.
    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)

    # Send a message to the user
    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )
    # Send a trace activity if we're talking to the Bot Framework Emulator
    if context.activity.channel_id == "emulator":
        # Create a trace activity that contains the error object
        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )
        # Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.send_activity(trace_activity) 
Example 20
Project: botbuilder-python   Author: microsoft   File: app.py    License: MIT License 6 votes vote down vote up
def on_error(context: TurnContext, error: Exception):
    # This check writes out errors to console log .vs. app insights.
    # NOTE: In production environment, you should consider logging this to Azure
    #       application insights.
    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
    print(traceback.format_exc())

    # Send a message to the user
    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )
    # Send a trace activity if we're talking to the Bot Framework Emulator
    if context.activity.channel_id == "emulator":
        # Create a trace activity that contains the error object
        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )
        # Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.send_activity(trace_activity) 
Example 21
Project: botbuilder-python   Author: microsoft   File: teams_file_bot.py    License: MIT License 6 votes vote down vote up
def _create_reply(self, activity, text=None, text_format=None):
        return Activity(
            type=ActivityTypes.message,
            timestamp=datetime.utcnow(),
            from_property=ChannelAccount(
                id=activity.recipient.id, name=activity.recipient.name
            ),
            recipient=ChannelAccount(
                id=activity.from_property.id, name=activity.from_property.name
            ),
            reply_to_id=activity.id,
            service_url=activity.service_url,
            channel_id=activity.channel_id,
            conversation=ConversationAccount(
                is_group=activity.conversation.is_group,
                id=activity.conversation.id,
                name=activity.conversation.name,
            ),
            text=text or "",
            text_format=text_format or None,
            locale=activity.locale,
        ) 
Example 22
Project: botbuilder-python   Author: microsoft   File: app.py    License: MIT License 6 votes vote down vote up
def on_error(context: TurnContext, error: Exception):
    # This check writes out errors to console log .vs. app insights.
    # NOTE: In production environment, you should consider logging this to Azure
    #       application insights.
    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)

    # Send a message to the user
    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )
    # Send a trace activity if we're talking to the Bot Framework Emulator
    if context.activity.channel_id == "emulator":
        # Create a trace activity that contains the error object
        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )

        # Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.send_activity(trace_activity) 
Example 23
Project: botbuilder-python   Author: microsoft   File: app.py    License: MIT License 6 votes vote down vote up
def on_error(context: TurnContext, error: Exception):
    # This check writes out errors to console log .vs. app insights.
    # NOTE: In production environment, you should consider logging this to Azure
    #       application insights.
    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
    traceback.print_exc()

    # Send a message to the user
    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )
    # Send a trace activity if we're talking to the Bot Framework Emulator
    if context.activity.channel_id == "emulator":
        # Create a trace activity that contains the error object
        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )
        # Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.send_activity(trace_activity) 
Example 24
Project: drydock   Author: airshipit   File: builddata.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self,
                 node_name=None,
                 task_id=None,
                 collected_date=None,
                 generator=None,
                 data_format=None,
                 data_element=None):
        """Initiator for BuildData."""
        if not all((node_name, task_id, generator, data_format, data_element)):
            raise ValueError("Required field missing.")

        try:
            if isinstance(data_element, bytes):
                data_element = data_element.decode('utf-8')
            elif not isinstance(data_element, str):
                data_element = str(data_element)
        except Exception:
            raise errors.BuildDataError(
                "Error saving build data - data_element type %s could"
                "not be cast to string." % str(type(data_element)))

        self.node_name = node_name
        self.task_id = task_id
        self.collected_date = collected_date or datetime.utcnow()
        self.generator = generator
        self.data_format = data_format
        self.data_element = data_element 
Example 25
Project: drydock   Author: airshipit   File: task.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self,
                 action=None,
                 design_ref=None,
                 parent_task_id=None,
                 node_filter=None,
                 context=None,
                 statemgr=None,
                 retry=0):
        self.statemgr = statemgr

        self.task_id = uuid.uuid4()
        self.status = hd_fields.TaskStatus.Requested
        self.subtask_id_list = []
        self.result = TaskStatus()
        self.action = action or hd_fields.OrchestratorAction.Noop
        self.design_ref = design_ref
        self.retry = retry
        self.parent_task_id = parent_task_id
        self.created = datetime.utcnow()
        self.node_filter = copy.deepcopy(node_filter)
        self.created_by = None
        self.updated = None
        self.terminated = None
        self.terminated_by = None
        self.request_context = context
        self.terminate = False
        self.logger = logging.getLogger("drydock")

        if context is not None:
            self.created_by = context.user 
Example 26
Project: drydock   Author: airshipit   File: task.py    License: Apache License 2.0 5 votes vote down vote up
def terminate_task(self, terminated_by=None):
        """Terminate this task.

        If the task is queued, just mark it terminated. Otherwise mark it as
        terminating and let the orchestrator manage completing termination.
        """
        self.terminate = True
        self.terminated = datetime.utcnow()
        self.terminated_by = terminated_by
        self.save() 
Example 27
Project: drydock   Author: airshipit   File: task.py    License: Apache License 2.0 5 votes vote down vote up
def save(self):
        """Save this task's current state to the database."""
        chk_task = self.statemgr.get_task(self.get_id())

        if chk_task in [
                hd_fields.TaskStatus.Terminating,
                hd_fields.TaskStatus.Terminated
        ]:
            self.set_status(chk_task.status)

        self.updated = datetime.utcnow()
        if not self.statemgr.put_task(self):
            raise errors.OrchestratorError("Error saving task.") 
Example 28
Project: drydock   Author: airshipit   File: task.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, msg, error, ctx_type, ctx, **kwargs):
        self.message = msg
        self.error = error
        self.ctx_type = ctx_type
        self.ctx = ctx
        if 'ts' not in kwargs:
            self.ts = datetime.utcnow()
        else:
            self.ts = kwargs.pop('ts')
        self.extra = kwargs 
Example 29
Project: drydock   Author: airshipit   File: node.py    License: Apache License 2.0 5 votes vote down vote up
def collect_build_data(self, machine):
        """Collect MaaS build data after commissioning."""
        self.logger.debug("Collecting build data for %s" % machine.hostname)
        try:
            data = machine.get_details()
            if data:
                for t, d in data.items():
                    if t in ('lshw', 'lldp'):
                        df = 'text/xml'
                    else:
                        df = 'text/plain'
                    bd = objects.BuildData(
                        node_name=machine.hostname,
                        task_id=self.task.get_id(),
                        generator=t,
                        collected_date=datetime.utcnow(),
                        data_format=df,
                        data_element=d.decode())
                    self.logger.debug(
                        "Saving build data from generator %s" % t)
                    self.state_manager.post_build_data(bd)
                    self.task.add_status_msg(
                        msg="Saving build data element.",
                        error=False,
                        ctx=machine.hostname,
                        ctx_type='node')
        except Exception as ex:
            self.logger.error(
                "Error collecting node build data for %s" % machine.hostname,
                exc_info=ex) 
Example 30
Project: drydock   Author: airshipit   File: test_postgres_builddata.py    License: Apache License 2.0 5 votes vote down vote up
def test_build_data_select_latest(self, blank_state):
        """Test that build data can be selected for only latest instance."""
        build_data_latest = {
            'node_name': 'foo',
            'generator': 'hello_world',
            'data_format': 'text/plain',
            'data_element': 'Hello World!',
            'task_id': uuid.uuid4(),
            'collected_date': datetime.utcnow(),
        }

        build_data_old = copy.deepcopy(build_data_latest)
        build_data_old['collected_date'] = build_data_latest[
            'collected_date'] - timedelta(days=1)
        build_data_old['task_id'] = uuid.uuid4()

        build_data1 = objects.BuildData(**build_data_latest)
        build_data2 = objects.BuildData(**build_data_old)

        result = blank_state.post_build_data(build_data1)

        assert result

        result = blank_state.post_build_data(build_data2)

        assert result

        bd_list = blank_state.get_build_data(node_name='foo', latest=True)

        assert len(bd_list) == 1

        assert bd_list[0].to_dict() == build_data1.to_dict()