github.com/nilium/gitlab-runner@v12.5.0+incompatible/executors/executor_abstract.go (about)

     1  package executors
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"gitlab.com/gitlab-org/gitlab-runner/common"
     8  	"gitlab.com/gitlab-org/gitlab-runner/session/proxy"
     9  )
    10  
    11  type ExecutorOptions struct {
    12  	DefaultCustomBuildsDirEnabled bool
    13  	DefaultBuildsDir              string
    14  	DefaultCacheDir               string
    15  	SharedBuildsDir               bool
    16  	Shell                         common.ShellScriptInfo
    17  	ShowHostname                  bool
    18  	Metadata                      map[string]string
    19  }
    20  
    21  type AbstractExecutor struct {
    22  	ExecutorOptions
    23  	common.BuildLogger
    24  	Config       common.RunnerConfig
    25  	Build        *common.Build
    26  	Trace        common.JobTrace
    27  	BuildShell   *common.ShellConfiguration
    28  	currentStage common.ExecutorStage
    29  	Context      context.Context
    30  	ProxyPool    proxy.Pool
    31  }
    32  
    33  func (e *AbstractExecutor) updateShell() error {
    34  	script := e.Shell()
    35  	script.Build = e.Build
    36  	if e.Config.Shell != "" {
    37  		script.Shell = e.Config.Shell
    38  	}
    39  	return nil
    40  }
    41  
    42  func (e *AbstractExecutor) generateShellConfiguration() error {
    43  	info := e.Shell()
    44  	info.PreCloneScript = e.Config.PreCloneScript
    45  	info.PreBuildScript = e.Config.PreBuildScript
    46  	info.PostBuildScript = e.Config.PostBuildScript
    47  	shellConfiguration, err := common.GetShellConfiguration(*info)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	e.BuildShell = shellConfiguration
    52  	e.Debugln("Shell configuration:", shellConfiguration)
    53  	return nil
    54  }
    55  
    56  func (e *AbstractExecutor) startBuild() error {
    57  	// Save hostname
    58  	if e.ShowHostname && e.Build.Hostname == "" {
    59  		e.Build.Hostname, _ = os.Hostname()
    60  	}
    61  
    62  	return e.Build.StartBuild(
    63  		e.RootDir(),
    64  		e.CacheDir(),
    65  		e.CustomBuildEnabled(),
    66  		e.SharedBuildsDir,
    67  	)
    68  }
    69  
    70  func (e *AbstractExecutor) RootDir() string {
    71  	if e.Config.BuildsDir != "" {
    72  		return e.Config.BuildsDir
    73  	}
    74  
    75  	return e.DefaultBuildsDir
    76  }
    77  
    78  func (e *AbstractExecutor) CacheDir() string {
    79  	if e.Config.CacheDir != "" {
    80  		return e.Config.CacheDir
    81  	}
    82  
    83  	return e.DefaultCacheDir
    84  }
    85  
    86  func (e *AbstractExecutor) CustomBuildEnabled() bool {
    87  	if e.Config.CustomBuildDir != nil {
    88  		return e.Config.CustomBuildDir.Enabled
    89  	}
    90  
    91  	return e.DefaultCustomBuildsDirEnabled
    92  }
    93  
    94  func (e *AbstractExecutor) Shell() *common.ShellScriptInfo {
    95  	return &e.ExecutorOptions.Shell
    96  }
    97  
    98  func (e *AbstractExecutor) Prepare(options common.ExecutorPrepareOptions) error {
    99  	e.PrepareConfiguration(options)
   100  
   101  	return e.PrepareBuildAndShell()
   102  }
   103  
   104  func (e *AbstractExecutor) PrepareConfiguration(options common.ExecutorPrepareOptions) {
   105  	e.currentStage = common.ExecutorStagePrepare
   106  	e.Context = options.Context
   107  	e.Config = *options.Config
   108  	e.Build = options.Build
   109  	e.Trace = options.Trace
   110  	e.BuildLogger = common.NewBuildLogger(options.Trace, options.Build.Log())
   111  	e.ProxyPool = proxy.NewPool()
   112  }
   113  
   114  func (e *AbstractExecutor) PrepareBuildAndShell() error {
   115  	err := e.startBuild()
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	err = e.updateShell()
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	err = e.generateShellConfiguration()
   126  	if err != nil {
   127  		return err
   128  	}
   129  	return nil
   130  }
   131  
   132  func (e *AbstractExecutor) Finish(err error) {
   133  	e.currentStage = common.ExecutorStageFinish
   134  }
   135  
   136  func (e *AbstractExecutor) Cleanup() {
   137  	e.currentStage = common.ExecutorStageCleanup
   138  }
   139  
   140  func (e *AbstractExecutor) GetCurrentStage() common.ExecutorStage {
   141  	return e.currentStage
   142  }
   143  
   144  func (e *AbstractExecutor) SetCurrentStage(stage common.ExecutorStage) {
   145  	e.currentStage = stage
   146  }