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

     1  package machine
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"strings"
     7  	"time"
     8  
     9  	"gitlab.com/gitlab-org/gitlab-runner/common"
    10  	"gitlab.com/gitlab-org/gitlab-runner/helpers/dns"
    11  )
    12  
    13  func machineFormat(runner string, template string) string {
    14  	if runner != "" {
    15  		return "runner-" + strings.ToLower(runner) + "-" + template
    16  	}
    17  	return template
    18  }
    19  
    20  func machineFilter(config *common.RunnerConfig) string {
    21  	return machineFormat(dns.MakeRFC1123Compatible(config.ShortDescription()), config.Machine.MachineName)
    22  }
    23  
    24  func matchesMachineFilter(name, filter string) bool {
    25  	var query string
    26  	if n, _ := fmt.Sscanf(name, filter, &query); n == 1 {
    27  		return true
    28  	}
    29  	return false
    30  }
    31  
    32  func filterMachineList(machines []string, filter string) (newMachines []string) {
    33  	newMachines = make([]string, 0, len(machines))
    34  	for _, machine := range machines {
    35  		if matchesMachineFilter(machine, filter) {
    36  			newMachines = append(newMachines, machine)
    37  		}
    38  	}
    39  	return
    40  }
    41  
    42  func newMachineName(config *common.RunnerConfig) string {
    43  	r := make([]byte, 4)
    44  	rand.Read(r)
    45  	t := time.Now().Unix()
    46  	return fmt.Sprintf(machineFilter(config), fmt.Sprintf("%d-%x", t, r))
    47  }