gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/common/version.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"github.com/urfave/cli"
     9  )
    10  
    11  var NAME = "gitlab-runner"
    12  var VERSION = "development version"
    13  var REVISION = "HEAD"
    14  var BRANCH = "HEAD"
    15  var BUILT = "unknown"
    16  
    17  var AppVersion AppVersionInfo
    18  
    19  type AppVersionInfo struct {
    20  	Name         string `json:"name"`
    21  	Version      string `json:"version"`
    22  	Revision     string `json:"revision"`
    23  	Branch       string `json:"branch"`
    24  	GOVersion    string `json:"go_version"`
    25  	BuiltAt      string `json:"built_at"`
    26  	OS           string `json:"os"`
    27  	Architecture string `json:"architecture"`
    28  }
    29  
    30  func (v *AppVersionInfo) Printer(c *cli.Context) {
    31  	fmt.Print(v.Extended())
    32  }
    33  
    34  func (v *AppVersionInfo) Line() string {
    35  	return fmt.Sprintf("%s %s (%s)", v.Name, v.Version, v.Revision)
    36  }
    37  
    38  func (v *AppVersionInfo) ShortLine() string {
    39  	return fmt.Sprintf("%s (%s)", v.Version, v.Revision)
    40  }
    41  
    42  func (v *AppVersionInfo) UserAgent() string {
    43  	return fmt.Sprintf("%s %s (%s; %s; %s/%s)", v.Name, v.Version, v.Branch, v.GOVersion, v.OS, v.Architecture)
    44  }
    45  
    46  func (v *AppVersionInfo) Variables() JobVariables {
    47  	return JobVariables{
    48  		{Key: "CI_RUNNER_VERSION", Value: v.Version, Public: true, Internal: true, File: false},
    49  		{Key: "CI_RUNNER_REVISION", Value: v.Revision, Public: true, Internal: true, File: false},
    50  		{Key: "CI_RUNNER_EXECUTABLE_ARCH", Value: fmt.Sprintf("%s/%s", v.OS, v.Architecture), Public: true, Internal: true, File: false},
    51  	}
    52  }
    53  
    54  func (v *AppVersionInfo) Extended() string {
    55  	version := fmt.Sprintf("Version:      %s\n", v.Version)
    56  	version += fmt.Sprintf("Git revision: %s\n", v.Revision)
    57  	version += fmt.Sprintf("Git branch:   %s\n", v.Branch)
    58  	version += fmt.Sprintf("GO version:   %s\n", v.GOVersion)
    59  	version += fmt.Sprintf("Built:        %s\n", v.BuiltAt)
    60  	version += fmt.Sprintf("OS/Arch:      %s/%s\n", v.OS, v.Architecture)
    61  
    62  	return version
    63  }
    64  
    65  // NewMetricsCollector returns a prometheus.Collector which represents current build information.
    66  func (v *AppVersionInfo) NewMetricsCollector() *prometheus.GaugeVec {
    67  	labels := map[string]string{
    68  		"name":         v.Name,
    69  		"version":      v.Version,
    70  		"revision":     v.Revision,
    71  		"branch":       v.Branch,
    72  		"go_version":   v.GOVersion,
    73  		"built_at":     v.BuiltAt,
    74  		"os":           v.OS,
    75  		"architecture": v.Architecture,
    76  	}
    77  	labelNames := make([]string, 0, len(labels))
    78  	for n := range labels {
    79  		labelNames = append(labelNames, n)
    80  	}
    81  
    82  	buildInfo := prometheus.NewGaugeVec(
    83  		prometheus.GaugeOpts{
    84  			Name: "gitlab_runner_version_info",
    85  			Help: "A metric with a constant '1' value labeled by different build stats fields.",
    86  		},
    87  		labelNames,
    88  	)
    89  	buildInfo.With(labels).Set(1)
    90  	return buildInfo
    91  }
    92  
    93  func init() {
    94  	AppVersion = AppVersionInfo{
    95  		Name:         NAME,
    96  		Version:      VERSION,
    97  		Revision:     REVISION,
    98  		Branch:       BRANCH,
    99  		GOVersion:    runtime.Version(),
   100  		BuiltAt:      BUILT,
   101  		OS:           runtime.GOOS,
   102  		Architecture: runtime.GOARCH,
   103  	}
   104  }