github.com/ssube/gitlab-ci-multi-runner@v1.2.1-0.20160607142738-b8d1285632e6/executors/executor_abstract.go (about)

     1  package executors
     2  
     3  import (
     4  	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
     5  	"os"
     6  )
     7  
     8  type ExecutorOptions struct {
     9  	DefaultBuildsDir string
    10  	DefaultCacheDir  string
    11  	SharedBuildsDir  bool
    12  	Shell            common.ShellScriptInfo
    13  	ShowHostname     bool
    14  	SupportedOptions []string
    15  }
    16  
    17  type AbstractExecutor struct {
    18  	ExecutorOptions
    19  	Config      common.RunnerConfig
    20  	Build       *common.Build
    21  	BuildLog    common.BuildTrace
    22  	BuildScript *common.ShellScript
    23  }
    24  
    25  func (e *AbstractExecutor) updateShell() error {
    26  	script := &e.Shell
    27  	script.Build = e.Build
    28  	if e.Config.Shell != "" {
    29  		script.Shell = e.Config.Shell
    30  	}
    31  	return nil
    32  }
    33  
    34  func (e *AbstractExecutor) generateShellScript() error {
    35  	shellScript, err := common.GenerateShellScript(e.Shell)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	e.BuildScript = shellScript
    40  	e.Debugln("Shell script:", shellScript)
    41  	return nil
    42  }
    43  
    44  func (e *AbstractExecutor) startBuild() error {
    45  	// Save hostname
    46  	if e.ShowHostname && e.Build.Hostname == "" {
    47  		e.Build.Hostname, _ = os.Hostname()
    48  	}
    49  
    50  	// Start actual build
    51  	rootDir := e.Config.BuildsDir
    52  	if rootDir == "" {
    53  		rootDir = e.DefaultBuildsDir
    54  	}
    55  	cacheDir := e.Config.CacheDir
    56  	if cacheDir == "" {
    57  		cacheDir = e.DefaultCacheDir
    58  	}
    59  	e.Build.StartBuild(rootDir, cacheDir, e.SharedBuildsDir)
    60  	return nil
    61  }
    62  
    63  func (e *AbstractExecutor) verifyOptions() error {
    64  	supportedOptions := e.SupportedOptions
    65  	if shell := common.GetShell(e.Shell.Shell); shell != nil {
    66  		supportedOptions = append(supportedOptions, shell.GetSupportedOptions()...)
    67  	}
    68  
    69  	for key, value := range e.Build.Options {
    70  		if value == nil {
    71  			continue
    72  		}
    73  		found := false
    74  		for _, option := range supportedOptions {
    75  			if option == key {
    76  				found = true
    77  				break
    78  			}
    79  		}
    80  
    81  		if !found {
    82  			e.Warningln(key, "is not supported by selected executor and shell")
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  func (e *AbstractExecutor) Prepare(globalConfig *common.Config, config *common.RunnerConfig, build *common.Build) error {
    89  	e.Config = *config
    90  	e.Build = build
    91  	e.BuildLog = build.Trace
    92  
    93  	err := e.startBuild()
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	e.Infoln(common.VersionLine())
    99  
   100  	err = e.updateShell()
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	err = e.verifyOptions()
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	err = e.generateShellScript()
   111  	if err != nil {
   112  		return err
   113  	}
   114  	return nil
   115  }
   116  
   117  func (e *AbstractExecutor) ShellScript() *common.ShellScript {
   118  	return e.BuildScript
   119  }
   120  
   121  func (e *AbstractExecutor) Finish(err error) {
   122  	if err != nil {
   123  		e.Println()
   124  		e.Errorln("Build failed:", err)
   125  	} else {
   126  		e.Println()
   127  		e.Infoln("Build succeeded")
   128  	}
   129  }
   130  
   131  func (e *AbstractExecutor) Cleanup() {
   132  }