Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,26 @@ Uninstall Cabbie service:

### Wsus

Initializes the wsus server configuration and restarts the windows update
service. If `--wsus_servers` flag is provided, Cabbie will check for
Initializes the WSUS server configuration and restarts the Windows Update
service. If the `--wsus_servers` flag is provided, Cabbie checks for
connectivity to Windows Update endpoints. If it fails to connect, or if
`--force` is true, it will write the provided list of servers to the
`WsusServers` value in `HKLM:\SOFTWARE\Google\Cabbie` before initializing WSUS
configuration and restarting the Windows Update service.
`--force` is true, it writes the provided list of servers to the
`WSUSServers` value in `HKLM:\SOFTWARE\Google\Cabbie`.

By default, Cabbie pings the WSUS servers to find the fastest one. If `--force`
is true, it skips this verification and unconditionally sets the first server.
If no WSUS servers are reachable and `--force` is not active, it reverts to
Windows Update and displays a warning.

If no servers are provided and none are configured in the registry, a helpful
message is displayed and the command will fail.

`cabbie wsus [--wsus_servers=<server1>,<server2>] [--force]`

Example:

`cabbie wsus --wsus_servers=server1.google.com,server2.google.com`
`"C:\Program Files\Google\Cabbie\cabbie.exe" wsus
--wsus_servers="wsus1.example.com,wsus2.example.com"`

`cabbie wsus`

Expand Down
26 changes: 25 additions & 1 deletion wsus.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,37 @@ func (c *wsusCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...any) sub
}
}

if _, err := wsus.Init(config.WSUSServers); err != nil {
if len(config.WSUSServers) == 0 {
fmt.Println("No WSUS servers configured. Please provide servers using the --wsus_servers flag.")
return subcommands.ExitFailure
}

var w *wsus.WSUS
var err error

if c.force {
deck.Info("Forcing WSUS configuration, skipping verification.")
w = &wsus.WSUS{Servers: config.WSUSServers}
err = w.Set(0)
if err == nil {
w.ServerSelection = wsus.ManagedServer
}
} else {
w, err = wsus.Init(config.WSUSServers)
}

if err != nil {
msg := fmt.Sprintf("Failed to initialize WSUS: %v\n", err)
deck.ErrorfA("%s", msg).With(eventID(cablib.EvtErrMisc)).Go()
fmt.Print(msg)
return subcommands.ExitFailure
}

if w.ServerSelection == wsus.WindowsUpdate && !c.force {
deck.Warning("No WSUS servers were reachable. Reverted to Windows Update.")
fmt.Println("Warning: No WSUS servers were reachable. Reverted to Windows Update.")
}

deck.InfoA("WSUS configuration refreshed, restarting wuauserv to apply settings.").With(eventID(cablib.EvtMisc)).Go()
if err := helpers.RestartService("wuauserv"); err != nil {
msg := fmt.Sprintf("Failed to restart wuauserv: %v\n", err)
Expand Down
7 changes: 7 additions & 0 deletions wsus/wsus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ func TestSet(t *testing.T) {
}
}
}

func TestInit_EmptyServers(t *testing.T) {
_, err := Init([]string{})
if err == nil {
t.Error("Init([]) expected error for empty servers list, got nil")
}
}
3 changes: 1 addition & 2 deletions wsus/wsus_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ func Init(servers []string) (*WSUS, error) {
var err error

if len(servers) == 0 {
w.ServerSelection = WindowsUpdate
return &w, w.Clear()
return nil, fmt.Errorf("servers list cannot be empty")
}

wlog, err = eventlog.Open("Cabbie WSUS")
Expand Down
Loading