Files

135 lines
4.2 KiB
Go

// Package main implements the IntegraCI windows-deploy Dagger module.
//
// Deploys to Windows Server over WinRM by running a PowerShell script on the
// target host — the Windows analogue of vm-deploy (which uses Pyinfra/SSH for
// Linux). Covers the "Deploy Windows Server (WinRM/PowerShell -> IIS / Windows
// Service)" branch of the delivery flow. Uses pywinrm inside a container; no
// vendor SDK is embedded in the platform.
//
// Usage:
//
// dagger call deploy --address 10.0.0.9 --script ./deploy.ps1 --username deployer --password env://WINRM_PASS
// dagger call deploy --address host --script ./deploy.ps1 --username u --password env://P --port 5985 --use-https=false
package main
import (
"context"
"dagger/windows-deploy/internal/dagger"
"fmt"
"strings"
)
// WindowsDeploy is the IntegraCI Windows (WinRM/PowerShell) deploy module.
type WindowsDeploy struct{}
// WinRMResult summarises a PowerShell-over-WinRM run.
type WinRMResult struct {
Host string `json:"host"`
StatusCode int `json:"status_code"`
Output string `json:"output"`
Success bool `json:"success"`
}
// runner returns a python:3.12-slim container with pywinrm installed.
func (m *WindowsDeploy) runner() *dagger.Container {
return dag.Container().
From("python:3.12-slim").
WithExec([]string{"pip", "install", "--no-cache-dir", "pywinrm==0.5.0"})
}
// The wrapper connects to the host over WinRM and runs the mounted PowerShell
// script, echoing stdout/stderr and exiting with the remote status code.
const winrmWrapper = `
import os, sys, winrm
host = os.environ["WINRM_HOST"]
scheme = "https" if os.environ.get("WINRM_HTTPS", "true").lower() != "false" else "http"
port = os.environ.get("WINRM_PORT", "5986" if scheme == "https" else "5985")
endpoint = f"{scheme}://{host}:{port}/wsman"
with open("/work/pass") as f:
password = f.read().strip()
with open("/work/deploy.ps1") as f:
script = f.read()
session = winrm.Session(
endpoint,
auth=(os.environ["WINRM_USER"], password),
transport=os.environ.get("WINRM_TRANSPORT", "ntlm"),
server_cert_validation="ignore",
)
r = session.run_ps(script)
sys.stdout.write(r.std_out.decode("utf-8", "replace"))
sys.stderr.write(r.std_err.decode("utf-8", "replace"))
sys.exit(r.status_code)
`
// Deploy runs a PowerShell deploy script on a Windows host over WinRM.
func (m *WindowsDeploy) Deploy(
ctx context.Context,
// Target Windows host (IP or DNS name).
address string,
// PowerShell deploy script to run on the host (copy artifact, deploy to
// IIS / register a Windows Service, etc.).
script *dagger.File,
// WinRM username.
username string,
// WinRM password as a Dagger secret.
password *dagger.Secret,
// WinRM port (default 5986 for HTTPS, 5985 for HTTP).
// +optional
port string,
// Use HTTPS WinRM (recommended). Set false for plain HTTP (5985).
// +optional
// +default=true
useHTTPS bool,
// WinRM transport: ntlm (default), kerberos, basic, credssp.
// +optional
// +default="ntlm"
transport string,
) (*WinRMResult, error) {
if address == "" {
return nil, fmt.Errorf("address is required")
}
scheme := "true"
if !useHTTPS {
scheme = "false"
}
c := m.runner().
WithMountedFile("/work/deploy.ps1", script).
WithMountedSecret("/work/pass", password).
WithEnvVariable("WINRM_HOST", address).
WithEnvVariable("WINRM_USER", username).
WithEnvVariable("WINRM_HTTPS", scheme).
WithEnvVariable("WINRM_TRANSPORT", transport).
WithNewFile("/work/run.py", winrmWrapper)
if port != "" {
c = c.WithEnvVariable("WINRM_PORT", port)
}
// Swallow the non-zero exit so we can surface status/Output ourselves.
c = c.WithExec([]string{"sh", "-c", "python /work/run.py; echo \"__RC__=$?\""})
out, _ := c.Stdout(ctx)
result := &WinRMResult{Host: address, Output: out}
// The wrapper's exit code is the remote PowerShell status; 0 = success.
if marker := lastRC(out); marker == 0 {
result.Success = true
} else {
result.StatusCode = marker
}
return result, nil
}
// lastRC parses the trailing "__RC__=N" marker the deploy step appends.
func lastRC(out string) int {
idx := strings.LastIndex(out, "__RC__=")
if idx < 0 {
return -1
}
rc := 0
for _, r := range out[idx+len("__RC__="):] {
if r < '0' || r > '9' {
break
}
rc = rc*10 + int(r-'0')
}
return rc
}