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

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  
     8  	"gitlab.com/gitlab-org/gitlab-runner/helpers/url"
     9  )
    10  
    11  type UpdateState int
    12  type UploadState int
    13  type DownloadState int
    14  type JobState string
    15  type JobFailureReason string
    16  
    17  const (
    18  	Pending JobState = "pending"
    19  	Running JobState = "running"
    20  	Failed  JobState = "failed"
    21  	Success JobState = "success"
    22  )
    23  
    24  const (
    25  	NoneFailure         JobFailureReason = ""
    26  	ScriptFailure       JobFailureReason = "script_failure"
    27  	RunnerSystemFailure JobFailureReason = "runner_system_failure"
    28  	JobExecutionTimeout JobFailureReason = "job_execution_timeout"
    29  )
    30  
    31  const (
    32  	UpdateSucceeded UpdateState = iota
    33  	UpdateNotFound
    34  	UpdateAbort
    35  	UpdateFailed
    36  	UpdateRangeMismatch
    37  )
    38  
    39  const (
    40  	UploadSucceeded UploadState = iota
    41  	UploadTooLarge
    42  	UploadForbidden
    43  	UploadFailed
    44  )
    45  
    46  const (
    47  	DownloadSucceeded DownloadState = iota
    48  	DownloadForbidden
    49  	DownloadFailed
    50  	DownloadNotFound
    51  )
    52  
    53  type FeaturesInfo struct {
    54  	Variables               bool `json:"variables"`
    55  	Image                   bool `json:"image"`
    56  	Services                bool `json:"services"`
    57  	Artifacts               bool `json:"artifacts"`
    58  	Cache                   bool `json:"cache"`
    59  	Shared                  bool `json:"shared"`
    60  	UploadMultipleArtifacts bool `json:"upload_multiple_artifacts"`
    61  	UploadRawArtifacts      bool `json:"upload_raw_artifacts"`
    62  	Session                 bool `json:"session"`
    63  	Terminal                bool `json:"terminal"`
    64  	Refspecs                bool `json:"refspecs"`
    65  	Masking                 bool `json:"masking"`
    66  }
    67  
    68  type RegisterRunnerParameters struct {
    69  	Description    string `json:"description,omitempty"`
    70  	Tags           string `json:"tag_list,omitempty"`
    71  	RunUntagged    bool   `json:"run_untagged"`
    72  	Locked         bool   `json:"locked"`
    73  	MaximumTimeout int    `json:"maximum_timeout,omitempty"`
    74  	Active         bool   `json:"active"`
    75  }
    76  
    77  type RegisterRunnerRequest struct {
    78  	RegisterRunnerParameters
    79  	Info  VersionInfo `json:"info,omitempty"`
    80  	Token string      `json:"token,omitempty"`
    81  }
    82  
    83  type RegisterRunnerResponse struct {
    84  	Token string `json:"token,omitempty"`
    85  }
    86  
    87  type VerifyRunnerRequest struct {
    88  	Token string `json:"token,omitempty"`
    89  }
    90  
    91  type UnregisterRunnerRequest struct {
    92  	Token string `json:"token,omitempty"`
    93  }
    94  
    95  type VersionInfo struct {
    96  	Name         string       `json:"name,omitempty"`
    97  	Version      string       `json:"version,omitempty"`
    98  	Revision     string       `json:"revision,omitempty"`
    99  	Platform     string       `json:"platform,omitempty"`
   100  	Architecture string       `json:"architecture,omitempty"`
   101  	Executor     string       `json:"executor,omitempty"`
   102  	Shell        string       `json:"shell,omitempty"`
   103  	Features     FeaturesInfo `json:"features"`
   104  }
   105  
   106  type JobRequest struct {
   107  	Info       VersionInfo  `json:"info,omitempty"`
   108  	Token      string       `json:"token,omitempty"`
   109  	LastUpdate string       `json:"last_update,omitempty"`
   110  	Session    *SessionInfo `json:"session,omitempty"`
   111  }
   112  
   113  type SessionInfo struct {
   114  	URL           string `json:"url,omitempty"`
   115  	Certificate   string `json:"certificate,omitempty"`
   116  	Authorization string `json:"authorization,omitempty"`
   117  }
   118  
   119  type JobInfo struct {
   120  	Name        string `json:"name"`
   121  	Stage       string `json:"stage"`
   122  	ProjectID   int    `json:"project_id"`
   123  	ProjectName string `json:"project_name"`
   124  }
   125  
   126  type GitInfoRefType string
   127  
   128  const (
   129  	RefTypeBranch GitInfoRefType = "branch"
   130  	RefTypeTag    GitInfoRefType = "tag"
   131  )
   132  
   133  type GitInfo struct {
   134  	RepoURL   string         `json:"repo_url"`
   135  	Ref       string         `json:"ref"`
   136  	Sha       string         `json:"sha"`
   137  	BeforeSha string         `json:"before_sha"`
   138  	RefType   GitInfoRefType `json:"ref_type"`
   139  	Refspecs  []string       `json:"refspecs"`
   140  	Depth     int            `json:"depth"`
   141  }
   142  
   143  type RunnerInfo struct {
   144  	Timeout int `json:"timeout"`
   145  }
   146  
   147  type StepScript []string
   148  
   149  type StepName string
   150  
   151  const (
   152  	StepNameScript      StepName = "script"
   153  	StepNameAfterScript StepName = "after_script"
   154  )
   155  
   156  type StepWhen string
   157  
   158  const (
   159  	StepWhenOnFailure StepWhen = "on_failure"
   160  	StepWhenOnSuccess StepWhen = "on_success"
   161  	StepWhenAlways    StepWhen = "always"
   162  )
   163  
   164  type CachePolicy string
   165  
   166  const (
   167  	CachePolicyUndefined CachePolicy = ""
   168  	CachePolicyPullPush  CachePolicy = "pull-push"
   169  	CachePolicyPull      CachePolicy = "pull"
   170  	CachePolicyPush      CachePolicy = "push"
   171  )
   172  
   173  type Step struct {
   174  	Name         StepName   `json:"name"`
   175  	Script       StepScript `json:"script"`
   176  	Timeout      int        `json:"timeout"`
   177  	When         StepWhen   `json:"when"`
   178  	AllowFailure bool       `json:"allow_failure"`
   179  }
   180  
   181  type Steps []Step
   182  
   183  type Image struct {
   184  	Name       string   `json:"name"`
   185  	Alias      string   `json:"alias,omitempty"`
   186  	Command    []string `json:"command,omitempty"`
   187  	Entrypoint []string `json:"entrypoint,omitempty"`
   188  }
   189  
   190  type Services []Image
   191  
   192  type ArtifactPaths []string
   193  
   194  type ArtifactWhen string
   195  
   196  const (
   197  	ArtifactWhenOnFailure ArtifactWhen = "on_failure"
   198  	ArtifactWhenOnSuccess ArtifactWhen = "on_success"
   199  	ArtifactWhenAlways    ArtifactWhen = "always"
   200  )
   201  
   202  func (when ArtifactWhen) OnSuccess() bool {
   203  	return when == "" || when == ArtifactWhenOnSuccess || when == ArtifactWhenAlways
   204  }
   205  
   206  func (when ArtifactWhen) OnFailure() bool {
   207  	return when == ArtifactWhenOnFailure || when == ArtifactWhenAlways
   208  }
   209  
   210  type ArtifactFormat string
   211  
   212  const (
   213  	ArtifactFormatDefault ArtifactFormat = ""
   214  	ArtifactFormatZip     ArtifactFormat = "zip"
   215  	ArtifactFormatGzip    ArtifactFormat = "gzip"
   216  	ArtifactFormatRaw     ArtifactFormat = "raw"
   217  )
   218  
   219  type Artifact struct {
   220  	Name      string         `json:"name"`
   221  	Untracked bool           `json:"untracked"`
   222  	Paths     ArtifactPaths  `json:"paths"`
   223  	When      ArtifactWhen   `json:"when"`
   224  	Type      string         `json:"artifact_type"`
   225  	Format    ArtifactFormat `json:"artifact_format"`
   226  	ExpireIn  string         `json:"expire_in"`
   227  }
   228  
   229  type Artifacts []Artifact
   230  
   231  type Cache struct {
   232  	Key       string        `json:"key"`
   233  	Untracked bool          `json:"untracked"`
   234  	Policy    CachePolicy   `json:"policy"`
   235  	Paths     ArtifactPaths `json:"paths"`
   236  }
   237  
   238  func (c Cache) CheckPolicy(wanted CachePolicy) (bool, error) {
   239  	switch c.Policy {
   240  	case CachePolicyUndefined, CachePolicyPullPush:
   241  		return true, nil
   242  	case CachePolicyPull, CachePolicyPush:
   243  		return wanted == c.Policy, nil
   244  	}
   245  
   246  	return false, fmt.Errorf("Unknown cache policy %s", c.Policy)
   247  }
   248  
   249  type Caches []Cache
   250  
   251  type Credentials struct {
   252  	Type     string `json:"type"`
   253  	URL      string `json:"url"`
   254  	Username string `json:"username"`
   255  	Password string `json:"password"`
   256  }
   257  
   258  type DependencyArtifactsFile struct {
   259  	Filename string `json:"filename"`
   260  	Size     int64  `json:"size"`
   261  }
   262  
   263  type Dependency struct {
   264  	ID            int                     `json:"id"`
   265  	Token         string                  `json:"token"`
   266  	Name          string                  `json:"name"`
   267  	ArtifactsFile DependencyArtifactsFile `json:"artifacts_file"`
   268  }
   269  
   270  type Dependencies []Dependency
   271  
   272  type GitlabFeatures struct {
   273  	TraceSections bool `json:"trace_sections"`
   274  }
   275  
   276  type JobResponse struct {
   277  	ID            int            `json:"id"`
   278  	Token         string         `json:"token"`
   279  	AllowGitFetch bool           `json:"allow_git_fetch"`
   280  	JobInfo       JobInfo        `json:"job_info"`
   281  	GitInfo       GitInfo        `json:"git_info"`
   282  	RunnerInfo    RunnerInfo     `json:"runner_info"`
   283  	Variables     JobVariables   `json:"variables"`
   284  	Steps         Steps          `json:"steps"`
   285  	Image         Image          `json:"image"`
   286  	Services      Services       `json:"services"`
   287  	Artifacts     Artifacts      `json:"artifacts"`
   288  	Cache         Caches         `json:"cache"`
   289  	Credentials   []Credentials  `json:"credentials"`
   290  	Dependencies  Dependencies   `json:"dependencies"`
   291  	Features      GitlabFeatures `json:"features"`
   292  
   293  	TLSCAChain  string `json:"-"`
   294  	TLSAuthCert string `json:"-"`
   295  	TLSAuthKey  string `json:"-"`
   296  }
   297  
   298  func (j *JobResponse) RepoCleanURL() string {
   299  	return url_helpers.CleanURL(j.GitInfo.RepoURL)
   300  }
   301  
   302  type UpdateJobRequest struct {
   303  	Info          VersionInfo      `json:"info,omitempty"`
   304  	Token         string           `json:"token,omitempty"`
   305  	State         JobState         `json:"state,omitempty"`
   306  	FailureReason JobFailureReason `json:"failure_reason,omitempty"`
   307  }
   308  
   309  type JobCredentials struct {
   310  	ID          int    `long:"id" env:"CI_JOB_ID" description:"The build ID to upload artifacts for"`
   311  	Token       string `long:"token" env:"CI_JOB_TOKEN" required:"true" description:"Build token"`
   312  	URL         string `long:"url" env:"CI_SERVER_URL" required:"true" description:"GitLab CI URL"`
   313  	TLSCAFile   string `long:"tls-ca-file" env:"CI_SERVER_TLS_CA_FILE" description:"File containing the certificates to verify the peer when using HTTPS"`
   314  	TLSCertFile string `long:"tls-cert-file" env:"CI_SERVER_TLS_CERT_FILE" description:"File containing certificate for TLS client auth with runner when using HTTPS"`
   315  	TLSKeyFile  string `long:"tls-key-file" env:"CI_SERVER_TLS_KEY_FILE" description:"File containing private key for TLS client auth with runner when using HTTPS"`
   316  }
   317  
   318  func (j *JobCredentials) GetURL() string {
   319  	return j.URL
   320  }
   321  
   322  func (j *JobCredentials) GetTLSCAFile() string {
   323  	return j.TLSCAFile
   324  }
   325  
   326  func (j *JobCredentials) GetTLSCertFile() string {
   327  	return j.TLSCertFile
   328  }
   329  
   330  func (j *JobCredentials) GetTLSKeyFile() string {
   331  	return j.TLSKeyFile
   332  }
   333  
   334  func (j *JobCredentials) GetToken() string {
   335  	return j.Token
   336  }
   337  
   338  type UpdateJobInfo struct {
   339  	ID            int
   340  	State         JobState
   341  	FailureReason JobFailureReason
   342  }
   343  
   344  type ArtifactsOptions struct {
   345  	BaseName string
   346  	ExpireIn string
   347  	Format   ArtifactFormat
   348  	Type     string
   349  }
   350  
   351  type FailuresCollector interface {
   352  	RecordFailure(reason JobFailureReason, runnerDescription string)
   353  }
   354  
   355  type JobTrace interface {
   356  	io.Writer
   357  	Success()
   358  	Fail(err error, failureReason JobFailureReason)
   359  	SetCancelFunc(cancelFunc context.CancelFunc)
   360  	SetFailuresCollector(fc FailuresCollector)
   361  	SetMasked(values []string)
   362  	IsStdout() bool
   363  }
   364  
   365  type Network interface {
   366  	RegisterRunner(config RunnerCredentials, parameters RegisterRunnerParameters) *RegisterRunnerResponse
   367  	VerifyRunner(config RunnerCredentials) bool
   368  	UnregisterRunner(config RunnerCredentials) bool
   369  	RequestJob(config RunnerConfig, sessionInfo *SessionInfo) (*JobResponse, bool)
   370  	UpdateJob(config RunnerConfig, jobCredentials *JobCredentials, jobInfo UpdateJobInfo) UpdateState
   371  	PatchTrace(config RunnerConfig, jobCredentials *JobCredentials, content []byte, startOffset int) (int, UpdateState)
   372  	DownloadArtifacts(config JobCredentials, artifactsFile string) DownloadState
   373  	UploadRawArtifacts(config JobCredentials, reader io.Reader, options ArtifactsOptions) UploadState
   374  	ProcessJob(config RunnerConfig, buildCredentials *JobCredentials) JobTrace
   375  }