github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/docker/machine/details.go (about)

     1  package machine
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus"
     9  
    10  	"gitlab.com/gitlab-org/gitlab-runner/helpers"
    11  )
    12  
    13  type machineDetails struct {
    14  	Name       string
    15  	Created    time.Time `yaml:"-"`
    16  	Used       time.Time `yaml:"-"`
    17  	UsedCount  int
    18  	State      machineState
    19  	Reason     string
    20  	RetryCount int
    21  	LastSeen   time.Time
    22  }
    23  
    24  func (m *machineDetails) isPersistedOnDisk() bool {
    25  	// Machines in creating phase might or might not be persisted on disk
    26  	// this is due to async nature of machine creation process
    27  	// where to `docker-machine create` is the one that is creating relevant files
    28  	// and it is being executed with undefined delay
    29  	return m.State != machineStateCreating
    30  }
    31  
    32  func (m *machineDetails) isUsed() bool {
    33  	return m.State != machineStateIdle
    34  }
    35  
    36  func (m *machineDetails) isStuckOnRemove() bool {
    37  	return m.State == machineStateRemoving && m.RetryCount >= removeRetryTries
    38  }
    39  
    40  func (m *machineDetails) isDead() bool {
    41  	return m.State == machineStateIdle &&
    42  		time.Since(m.LastSeen) > machineDeadInterval
    43  }
    44  
    45  func (m *machineDetails) canBeUsed() bool {
    46  	return m.State == machineStateAcquired
    47  }
    48  
    49  func (m *machineDetails) match(machineFilter string) bool {
    50  	var query string
    51  	if n, _ := fmt.Sscanf(m.Name, machineFilter, &query); n != 1 {
    52  		return false
    53  	}
    54  	return true
    55  }
    56  
    57  func (m *machineDetails) writeDebugInformation() {
    58  	if logrus.GetLevel() < logrus.DebugLevel {
    59  		return
    60  	}
    61  
    62  	var details struct {
    63  		Details    machineDetails
    64  		Time       string
    65  		CreatedAgo time.Duration
    66  	}
    67  	details.Details = *m
    68  	details.Time = time.Now().String()
    69  	details.CreatedAgo = time.Since(m.Created)
    70  	data := helpers.ToYAML(&details)
    71  	ioutil.WriteFile("machines/"+details.Details.Name+".yml", []byte(data), 0600)
    72  }
    73  
    74  func (m *machineDetails) logger() *logrus.Entry {
    75  	return logrus.WithFields(logrus.Fields{
    76  		"name":      m.Name,
    77  		"lifetime":  time.Since(m.Created),
    78  		"used":      time.Since(m.Used),
    79  		"usedCount": m.UsedCount,
    80  		"reason":    m.Reason,
    81  	})
    82  }
    83  
    84  type machinesDetails map[string]*machineDetails