1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
package shells
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/featureflags"
)
const (
dockerWindowsExecutor = "docker-windows"
SNPwsh = "pwsh"
SNPowershell = "powershell"
// Before executing a script, powershell parses it.
// A `ParserError` can then be thrown if a parsing error is found.
// Those errors are not catched by the powershell_trap_script thus causing the job to hang
// To avoid this problem, the PwshValidationScript is used to validate the given script and eventually to cause
// the job to fail if a `ParserError` is thrown
pwshJSONTerminationScript = `
param (
[Parameter(Mandatory=$true,Position=1)]
[string]$Path
)
%s -File $Path
$out_json= '{"command_exit_code": ' + $LASTEXITCODE + ', "script": "' + $MyInvocation.MyCommand.Name + '"}'
echo ""
echo "$out_json"
Exit 0
`
)
type powershellChangeUserError struct {
shell string
executor string
}
func (p *powershellChangeUserError) Error() string {
return fmt.Sprintf("%s doesn't support changing user with the %s executor", p.shell, p.executor)
}
type PowerShell struct {
AbstractShell
Shell string
EOL string
}
type PsWriter struct {
bytes.Buffer
TemporaryPath string
indent int
Shell string
EOL string
resolvePaths bool
}
func stdinCmdArgs() []string {
return []string{
"-NoProfile",
"-NoLogo",
"-InputFormat",
"text",
"-OutputFormat",
"text",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-Command",
"-",
}
}
func fileCmdArgs() []string {
return []string{"-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command"}
}
func PwshJSONTerminationScript(shell string) string {
return fmt.Sprintf(pwshJSONTerminationScript, shell)
}
func PowershellDockerCmd(shell string) []string {
return append([]string{shell}, stdinCmdArgs()...)
}
func psQuote(text string) string {
// taken from: http://www.robvanderwoude.com/escapechars.php
text = strings.ReplaceAll(text, "`", "``")
// text = strings.ReplaceAll(text, "\0", "`0")
text = strings.ReplaceAll(text, "\a", "`a")
text = strings.ReplaceAll(text, "\b", "`b")
text = strings.ReplaceAll(text, "\f", "^f")
text = strings.ReplaceAll(text, "\r", "`r")
text = strings.ReplaceAll(text, "\n", "`n")
text = strings.ReplaceAll(text, "\t", "^t")
text = strings.ReplaceAll(text, "\v", "^v")
text = strings.ReplaceAll(text, "#", "`#")
text = strings.ReplaceAll(text, "'", "`'")
text = strings.ReplaceAll(text, "\"", "`\"")
return `"` + text + `"`
}
func psQuoteVariable(text string) string {
text = psQuote(text)
text = strings.ReplaceAll(text, "$", "`$")
text = strings.ReplaceAll(text, "``e", "`e")
return text
}
func (p *PsWriter) GetTemporaryPath() string {
return p.TemporaryPath
}
func (p *PsWriter) Line(text string) {
p.WriteString(strings.Repeat(" ", p.indent) + text + p.EOL)
}
func (p *PsWriter) Linef(format string, arguments ...interface{}) {
p.Line(fmt.Sprintf(format, arguments...))
}
func (p *PsWriter) CheckForErrors() {
p.checkErrorLevel()
}
func (p *PsWriter) Indent() {
p.indent++
}
func (p *PsWriter) Unindent() {
p.indent--
}
func (p *PsWriter) checkErrorLevel() {
p.Line("if(!$?) { Exit &{if($LASTEXITCODE) {$LASTEXITCODE} else {1}} }")
p.Line("")
}
func (p *PsWriter) Command(command string, arguments ...string) {
p.Line(p.buildCommand(command, arguments...))
p.checkErrorLevel()
}
func (p *PsWriter) SectionStart(id, command string) {}
func (p *PsWriter) SectionEnd(id string) {}
func (p *PsWriter) buildCommand(command string, arguments ...string) string {
list := []string{
psQuote(command),
}
for _, argument := range arguments {
list = append(list, psQuote(argument))
}
return "& " + strings.Join(list, " ")
}
func (p *PsWriter) resolvePath(path string) string {
if p.resolvePaths {
return fmt.Sprintf("$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(%s)", psQuote(path))
}
return psQuote(p.fromSlash(path))
}
func (p *PsWriter) TmpFile(name string) string {
if p.resolvePaths {
return p.Join(p.TemporaryPath, name)
}
filePath := p.Absolute(p.Join(p.TemporaryPath, name))
return p.fromSlash(filePath)
}
func (p *PsWriter) fromSlash(path string) string {
if p.resolvePaths {
return path
}
if p.Shell == SNPwsh {
// pwsh wants OS slash style, not necessarily backslashes
return filepath.FromSlash(path)
}
return helpers.ToBackslash(path)
}
func (p *PsWriter) EnvVariableKey(name string) string {
return fmt.Sprintf("$%s", name)
}
func (p *PsWriter) Variable(variable common.JobVariable) {
if variable.File {
variableFile := p.TmpFile(variable.Key)
p.MkDir(p.TemporaryPath)
p.Linef(
"[System.IO.File]::WriteAllText(%s, %s)",
p.resolvePath(variableFile),
psQuoteVariable(variable.Value),
)
p.Linef("$%s=%s", variable.Key, p.resolvePath(variableFile))
} else {
p.Linef("$%s=%s", variable.Key, psQuoteVariable(variable.Value))
}
p.Linef("$env:%s=$%s", variable.Key, variable.Key)
}
func (p *PsWriter) IfDirectory(path string) {
p.Linef("if(Test-Path %s -PathType Container) {", p.resolvePath(path))
p.Indent()
}
func (p *PsWriter) IfFile(path string) {
p.Linef("if(Test-Path %s -PathType Leaf) {", p.resolvePath(path))
p.Indent()
}
func (p *PsWriter) IfCmd(cmd string, arguments ...string) {
p.ifInTryCatch(p.buildCommand(cmd, arguments...) + " 2>$null")
}
func (p *PsWriter) IfCmdWithOutput(cmd string, arguments ...string) {
p.ifInTryCatch(p.buildCommand(cmd, arguments...))
}
func (p *PsWriter) ifInTryCatch(cmd string) {
p.Line("Set-Variable -Name cmdErr -Value $false")
p.Line("Try {")
p.Indent()
p.Line(cmd)
p.Line("if(!$?) { throw &{if($LASTEXITCODE) {$LASTEXITCODE} else {1}} }")
p.Unindent()
p.Line("} Catch {")
p.Indent()
p.Line("Set-Variable -Name cmdErr -Value $true")
p.Unindent()
p.Line("}")
p.Line("if(!$cmdErr) {")
p.Indent()
}
func (p *PsWriter) Else() {
p.Unindent()
p.Line("} else {")
p.Indent()
}
func (p *PsWriter) EndIf() {
p.Unindent()
p.Line("}")
}
func (p *PsWriter) Cd(path string) {
p.Line("cd " + p.resolvePath(path))
p.checkErrorLevel()
}
func (p *PsWriter) MkDir(path string) {
p.Linef("New-Item -ItemType directory -Force -Path %s | out-null", p.resolvePath(path))
}
func (p *PsWriter) MkTmpDir(name string) string {
dirPath := p.Join(p.TemporaryPath, name)
p.MkDir(dirPath)
return dirPath
}
func (p *PsWriter) RmDir(path string) {
path = p.resolvePath(path)
p.Linef(
"if( (Get-Command -Name Remove-Item2 -Module NTFSSecurity -ErrorAction SilentlyContinue) "+
"-and (Test-Path %s -PathType Container) ) {",
path,
)
p.Indent()
p.Line("Remove-Item2 -Force -Recurse " + path)
p.Unindent()
p.Linef("} elseif(Test-Path %s) {", path)
p.Indent()
p.Line("Remove-Item -Force -Recurse " + path)
p.Unindent()
p.Line("}")
p.Line("")
}
func (p *PsWriter) RmFile(path string) {
path = p.resolvePath(path)
p.Line(
"if( (Get-Command -Name Remove-Item2 -Module NTFSSecurity -ErrorAction SilentlyContinue) " +
"-and (Test-Path " + path + " -PathType Leaf) ) {")
p.Indent()
p.Line("Remove-Item2 -Force " + path)
p.Unindent()
p.Linef("} elseif(Test-Path %s) {", path)
p.Indent()
p.Line("Remove-Item -Force " + path)
p.Unindent()
p.Line("}")
p.Line("")
}
func (p *PsWriter) RmFilesRecursive(path string, name string) {
resolvedPath := p.resolvePath(path)
p.IfDirectory(path)
p.Linef(
// `Remove-Item -Recurse` has a known issue (see Example 4 in
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item)
"Get-ChildItem -Path %s -Filter %s -Recurse | ForEach-Object { Remove-Item -Force $_.FullName }",
resolvedPath, psQuote(name),
)
p.EndIf()
}
func (p *PsWriter) Printf(format string, arguments ...interface{}) {
coloredText := helpers.ANSI_RESET + fmt.Sprintf(format, arguments...)
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) Noticef(format string, arguments ...interface{}) {
coloredText := helpers.ANSI_BOLD_GREEN + fmt.Sprintf(format, arguments...) + helpers.ANSI_RESET
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) Warningf(format string, arguments ...interface{}) {
coloredText := helpers.ANSI_YELLOW + fmt.Sprintf(format, arguments...) + helpers.ANSI_RESET
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) Errorf(format string, arguments ...interface{}) {
coloredText := helpers.ANSI_BOLD_RED + fmt.Sprintf(format, arguments...) + helpers.ANSI_RESET
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) EmptyLine() {
p.Line(`echo ""`)
}
func (p *PsWriter) Absolute(dir string) string {
if p.resolvePaths {
return dir
}
if filepath.IsAbs(dir) {
return dir
}
p.Linef("$CurrentDirectory = (Resolve-Path .%s).Path", string(os.PathSeparator))
return p.Join("$CurrentDirectory", dir)
}
func (p *PsWriter) Join(elem ...string) string {
if p.resolvePaths {
// We rely on the resolve function and always use forward slashes
// when joining paths.
return path.Join(elem...)
}
return filepath.Join(elem...)
}
func (p *PsWriter) Finish(trace bool) string {
var buf strings.Builder
if p.Shell == SNPwsh {
p.finishPwsh(&buf, trace)
} else {
p.finishPowerShell(&buf, trace)
}
return buf.String()
}
func (p *PsWriter) finishPwsh(buf *strings.Builder, trace bool) {
if p.EOL == "\n" {
buf.WriteString("#!/usr/bin/env " + SNPwsh + p.EOL)
}
// All pwsh scripts can and should be wrapped in a script block. Regardless whether they are passed
// as files or through stdin, this way the whole script will be executed as a block,
// this was suggested at https://github.com/PowerShell/PowerShell/issues/15331#issuecomment-1016942586.
// This also fixes things like https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/2715 and
// allows us to bypass file permissions when changing the current user.
buf.WriteString("& {" + p.EOL + p.EOL)
if trace {
buf.WriteString("Set-PSDebug -Trace 2" + p.EOL)
}
buf.WriteString(`$ErrorActionPreference = "Stop"` + p.EOL)
buf.WriteString(p.String() + p.EOL)
buf.WriteString("}" + p.EOL + p.EOL)
}
func (p *PsWriter) finishPowerShell(buf *strings.Builder, trace bool) {
// write UTF-8 BOM (Powershell Core doesn't use a BOM as mentioned in
// https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3896#note_157830131)
buf.WriteString("\xef\xbb\xbf")
if trace {
buf.WriteString("Set-PSDebug -Trace 2" + p.EOL)
}
buf.WriteString(p.String() + p.EOL)
}
func (b *PowerShell) GetName() string {
return b.Shell
}
func (b *PowerShell) GetConfiguration(info common.ShellScriptInfo) (*common.ShellConfiguration, error) {
script := &common.ShellConfiguration{
Command: b.Shell,
PassFile: b.Shell != SNPwsh && info.Build.Runner.Executor != dockerWindowsExecutor,
Extension: "ps1",
DockerCommand: PowershellDockerCmd(b.Shell),
}
if info.User != "" {
if script.PassFile {
return nil, &powershellChangeUserError{
shell: b.Shell,
executor: info.Build.Runner.Executor,
}
}
script.Command = "su"
if runtime.GOOS == OSLinux {
script.Arguments = append(script.Arguments, "-s", "/usr/bin/"+b.Shell)
}
script.Arguments = append(
script.Arguments,
info.User,
"-c",
b.Shell+" "+strings.Join(stdinCmdArgs(), " "),
)
} else {
script.Arguments = b.scriptArgs(script)
}
script.CmdLine = strings.Join(append([]string{script.Command}, script.Arguments...), " ")
return script, nil
}
func (b *PowerShell) scriptArgs(script *common.ShellConfiguration) []string {
if script.PassFile {
return fileCmdArgs()
}
return stdinCmdArgs()
}
func (b *PowerShell) GenerateScript(buildStage common.BuildStage, info common.ShellScriptInfo) (string, error) {
w := &PsWriter{
Shell: b.Shell,
EOL: b.EOL,
TemporaryPath: info.Build.TmpProjectDir(),
resolvePaths: info.Build.IsFeatureFlagOn(featureflags.UsePowershellPathResolver),
}
return b.generateScript(w, buildStage, info)
}
func (b *PowerShell) generateScript(
w ShellWriter,
buildStage common.BuildStage,
info common.ShellScriptInfo,
) (string, error) {
b.ensurePrepareStageHostnameMessage(w, buildStage, info)
err := b.writeScript(w, buildStage, info)
if err != nil {
return "", err
}
script := w.Finish(info.Build.IsDebugTraceEnabled())
return script, nil
}
func (b *PowerShell) ensurePrepareStageHostnameMessage(
w ShellWriter,
buildStage common.BuildStage,
info common.ShellScriptInfo,
) {
if buildStage == common.BuildStagePrepare {
if info.Build.Hostname != "" {
w.Line(
fmt.Sprintf(
`echo "Running on $([Environment]::MachineName) via %s..."`,
psQuoteVariable(info.Build.Hostname),
),
)
} else {
w.Line(`echo "Running on $([Environment]::MachineName)..."`)
}
}
}
func (b *PowerShell) IsDefault() bool {
return false
}
func init() {
eol := "\r\n"
if runtime.GOOS != OSWindows {
eol = "\n"
}
common.RegisterShell(&PowerShell{Shell: SNPwsh, EOL: eol})
common.RegisterShell(&PowerShell{Shell: SNPowershell, EOL: "\r\n"})
}
|