github.com/lablabs/operator-sdk@v0.8.2/pkg/ansible/runner/eventapi/types.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package eventapi
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  )
    22  
    23  const (
    24  	// Ansible Events
    25  
    26  	// EventPlaybookOnTaskStart - playbook is starting to run a task.
    27  	EventPlaybookOnTaskStart = "playbook_on_task_start"
    28  	// EventRunnerOnOk - task finished with ok status.
    29  	EventRunnerOnOk = "runner_on_ok"
    30  	// EventRunnerOnFailed - task finished with failed status.
    31  	EventRunnerOnFailed = "runner_on_failed"
    32  	// EventPlaybookOnStats - playbook has finished running.
    33  	EventPlaybookOnStats = "playbook_on_stats"
    34  
    35  	// Ansible Task Actions
    36  
    37  	// TaskActionSetFact - task action of setting a fact.
    38  	TaskActionSetFact = "set_fact"
    39  	// TaskActionDebug - task action of printing a debug message.
    40  	TaskActionDebug = "debug"
    41  
    42  	// defaultFailedMessage - Default failed playbook message
    43  	defaultFailedMessage = "unknown playbook failure"
    44  )
    45  
    46  // EventTime - time to unmarshal nano time.
    47  type EventTime struct {
    48  	time.Time
    49  }
    50  
    51  // UnmarshalJSON - override unmarshal json.
    52  func (e *EventTime) UnmarshalJSON(b []byte) (err error) {
    53  	e.Time, err = time.Parse("2006-01-02T15:04:05.999999999", strings.Trim(string(b[:]), "\"\\"))
    54  	return
    55  }
    56  
    57  // MarshalJSON - override the marshal json.
    58  func (e EventTime) MarshalJSON() ([]byte, error) {
    59  	return []byte(fmt.Sprintf("\"%s\"", e.Time.Format("2006-01-02T15:04:05.99999999"))), nil
    60  }
    61  
    62  // JobEvent - event of an ansible run.
    63  type JobEvent struct {
    64  	UUID      string                 `json:"uuid"`
    65  	Counter   int                    `json:"counter"`
    66  	StdOut    string                 `json:"stdout"`
    67  	StartLine int                    `json:"start_line"`
    68  	EndLine   int                    `json:"EndLine"`
    69  	Event     string                 `json:"event"`
    70  	EventData map[string]interface{} `json:"event_data"`
    71  	PID       int                    `json:"pid"`
    72  	Created   EventTime              `json:"created"`
    73  }
    74  
    75  // StatusJobEvent - event of an ansible run.
    76  type StatusJobEvent struct {
    77  	UUID      string         `json:"uuid"`
    78  	Counter   int            `json:"counter"`
    79  	StdOut    string         `json:"stdout"`
    80  	StartLine int            `json:"start_line"`
    81  	EndLine   int            `json:"EndLine"`
    82  	Event     string         `json:"event"`
    83  	EventData StatsEventData `json:"event_data"`
    84  	PID       int            `json:"pid"`
    85  	Created   EventTime      `json:"created"`
    86  }
    87  
    88  // StatsEventData - data for a the status event.
    89  type StatsEventData struct {
    90  	Playbook     string         `json:"playbook"`
    91  	PlaybookUUID string         `json:"playbook_uuid"`
    92  	Changed      map[string]int `json:"changed"`
    93  	Ok           map[string]int `json:"ok"`
    94  	Failures     map[string]int `json:"failures"`
    95  	Skipped      map[string]int `json:"skipped"`
    96  }
    97  
    98  // FailureMessages - failure messages from the event api
    99  type FailureMessages []string
   100  
   101  // GetFailedPlaybookMessage - get the failure message from res.msg
   102  func (je JobEvent) GetFailedPlaybookMessage() string {
   103  	message := defaultFailedMessage
   104  	result, ok := je.EventData["res"].(map[string]interface{})
   105  	if !ok {
   106  		return message
   107  	}
   108  	if m, ok := result["msg"].(string); ok {
   109  		message = m
   110  	}
   111  	return message
   112  }
   113  
   114  // IgnoreError - Does the job event contain the ignore_error ansible flag
   115  func (je JobEvent) IgnoreError() bool {
   116  	ignoreErrors, ok := je.EventData["ignore_errors"]
   117  	if !ok {
   118  		return false
   119  	}
   120  	if b, ok := ignoreErrors.(bool); ok && b {
   121  		return b
   122  	}
   123  	return false
   124  }