@@ -26,7 +26,7 @@ func RunNetlaunch(config *NetLaunchConfig) error {
2626 // Read the ISO
2727 iso , err := os .ReadFile (config .IsoPath )
2828 if err != nil {
29- log . WithError ( err ). Fatalf ( "failed to find iso for testing" )
29+ return fmt . Errorf ( "failed to read ISO file '%s': %w" , config . IsoPath , err )
3030 }
3131
3232 localListenAddress := fmt .Sprintf (":%d" , config .ListenPort )
@@ -58,13 +58,20 @@ func RunNetlaunch(config *NetLaunchConfig) error {
5858 if config .Netlaunch .AnnounceIp != nil {
5959 // If an IP is specified, use it.
6060 announceIp = * config .Netlaunch .AnnounceIp
61- } else {
61+ } else if config . Netlaunch . Bmc != nil && config . Netlaunch . Bmc . Ip != "" {
6262 // Otherwise, try to be clever...
6363 // We need to find the IP of the local interface that can reach the BMC.
6464 log .Warn ("No announce IP specified. Attempting to find local IP to announce based on BMC IP." )
6565 announceIp , err = netfinder .FindLocalIpForTargetIp (config .Netlaunch .Bmc .Ip )
6666 if err != nil {
67- return fmt .Errorf ("failed to find local IP for BMC: %v" , err )
67+ return fmt .Errorf ("failed to find local IP for BMC: %w" , err )
68+ }
69+ } else {
70+ // If we have no BMC, find the default outbound IP.
71+ log .Warn ("No announce IP specified. Attempting to find default outbound IP to announce." )
72+ announceIp , err = netfinder .FindDefaultOutboundIp ()
73+ if err != nil {
74+ return fmt .Errorf ("failed to find default outbound IP: %w" , err )
6875 }
6976 }
7077
@@ -85,7 +92,7 @@ func RunNetlaunch(config *NetLaunchConfig) error {
8592 log .Info ("Using Trident config file: " , config .HostConfigFile )
8693 tridentConfigContents , err := os .ReadFile (config .HostConfigFile )
8794 if err != nil {
88- return fmt .Errorf ("failed to read Trident config: %v " , err )
95+ return fmt .Errorf ("failed to read Trident config: %w " , err )
8996 }
9097
9198 // Replace NETLAUNCH_HOST_ADDRESS with the address of the netlaunch server
@@ -94,7 +101,7 @@ func RunNetlaunch(config *NetLaunchConfig) error {
94101 trident := make (map [string ]interface {})
95102 err = yaml .UnmarshalStrict ([]byte (tridentConfigContentsStr ), & trident )
96103 if err != nil {
97- return fmt .Errorf ("failed to unmarshal Trident config: %v " , err )
104+ return fmt .Errorf ("failed to unmarshal Trident config: %w " , err )
98105 }
99106
100107 if _ , ok := trident ["trident" ]; ! ok {
@@ -105,27 +112,27 @@ func RunNetlaunch(config *NetLaunchConfig) error {
105112
106113 tridentConfig , err := yaml .Marshal (trident )
107114 if err != nil {
108- return fmt .Errorf ("failed to marshal Trident config: %v " , err )
115+ return fmt .Errorf ("failed to marshal Trident config: %w " , err )
109116 }
110117
111118 err = isopatcher .PatchFile (iso , "/etc/trident/config.yaml" , tridentConfig )
112119 if err != nil {
113- return fmt .Errorf ("failed to patch Trident config into ISO: %v " , err )
120+ return fmt .Errorf ("failed to patch Trident config into ISO: %w " , err )
114121 }
115122
116123 if config .Iso .PreTridentScript != nil {
117124 log .Info ("Patching in pre-trident script!" )
118125 err = isopatcher .PatchFile (iso , "/trident_cdrom/pre-trident-script.sh" , []byte (* config .Iso .PreTridentScript ))
119126 if err != nil {
120- return fmt .Errorf ("failed to patch pre-trident script into ISO: %v " , err )
127+ return fmt .Errorf ("failed to patch pre-trident script into ISO: %w " , err )
121128 }
122129 }
123130
124131 if config .Iso .ServiceOverride != nil {
125132 log .Info ("Patching Trident service override!" )
126133 err = isopatcher .PatchFile (iso , "/trident_cdrom/trident-override.conf" , []byte (* config .Iso .ServiceOverride ))
127134 if err != nil {
128- return fmt .Errorf ("failed to patch service override into ISO: %v " , err )
135+ return fmt .Errorf ("failed to patch service override into ISO: %w " , err )
129136 }
130137 }
131138
@@ -153,14 +160,14 @@ func RunNetlaunch(config *NetLaunchConfig) error {
153160 // Set up listening for logstream
154161 logstreamFull , err := phonehome .SetupLogstream (config .LogstreamFile )
155162 if err != nil {
156- return fmt .Errorf ("failed to setup logstream: %v " , err )
163+ return fmt .Errorf ("failed to setup logstream: %w " , err )
157164 }
158165 defer logstreamFull .Close ()
159166
160167 // Set up listening for tracestream
161168 traceFile , err := phonehome .SetupTraceStream (config .TracestreamFile )
162169 if err != nil {
163- return fmt .Errorf ("failed to setup tracestream: %v " , err )
170+ return fmt .Errorf ("failed to setup tracestream: %w " , err )
164171 }
165172 defer traceFile .Close ()
166173
@@ -193,7 +200,7 @@ func RunNetlaunch(config *NetLaunchConfig) error {
193200 if config .Netlaunch .Bmc != nil && config .Netlaunch .Bmc .SerialOverSsh != nil {
194201 serial , err := config .Netlaunch .Bmc .ListenForSerialOutput ()
195202 if err != nil {
196- return fmt .Errorf ("failed to open serial over SSH session: %v " , err )
203+ return fmt .Errorf ("failed to open serial over SSH session: %w " , err )
197204 }
198205 defer serial .Close ()
199206 }
@@ -218,27 +225,27 @@ func RunNetlaunch(config *NetLaunchConfig) error {
218225 log .Info ("Connecting to BMC" )
219226 client .Registry .Drivers = client .Registry .For ("gofish" )
220227 if err := client .Open (context .Background ()); err != nil {
221- return fmt .Errorf ("failed to open connection to BMC: %v " , err )
228+ return fmt .Errorf ("failed to open connection to BMC: %w " , err )
222229 }
223230
224231 log .Info ("Shutting down machine" )
225232 if _ , err = client .SetPowerState (ctx , "off" ); err != nil {
226- return fmt .Errorf ("failed to turn off machine: %v " , err )
233+ return fmt .Errorf ("failed to turn off machine: %w " , err )
227234 }
228235
229236 log .WithField ("url" , iso_location ).Info ("Setting virtual media to ISO" )
230237 if _ , err = client .SetVirtualMedia (ctx , string (redfish .CDMediaType ), iso_location ); err != nil {
231- return fmt .Errorf ("failed to set virtual media: %v " , err )
238+ return fmt .Errorf ("failed to set virtual media: %w " , err )
232239 }
233240
234241 log .Info ("Setting boot media" )
235242 if _ , err = client .SetBootDevice (ctx , "cdrom" , false , true ); err != nil {
236- return fmt .Errorf ("failed to set boot media: %v " , err )
243+ return fmt .Errorf ("failed to set boot media: %w " , err )
237244 }
238245
239246 log .Info ("Turning on machine" )
240247 if _ , err = client .SetPowerState (ctx , "on" ); err != nil {
241- return fmt .Errorf ("failed to turn on machine: %v " , err )
248+ return fmt .Errorf ("failed to turn on machine: %w " , err )
242249 }
243250 }
244251
@@ -270,21 +277,21 @@ func startLocalVm(localVmUuidStr string, isoLocation string, secureBoot bool, si
270277 // TODO: Parse the UUID directly when reading the config file
271278 vmUuid , err := uuid .Parse (localVmUuidStr )
272279 if err != nil {
273- return fmt .Errorf ("failed to parse LocalVmUuid as UUID: %v " , err )
280+ return fmt .Errorf ("failed to parse LocalVmUuid as UUID: %w " , err )
274281 }
275282
276283 vm , err := stormutils .InitializeVm (vmUuid )
277284 if err != nil {
278- return fmt .Errorf ("failed to initialize VM: %v " , err )
285+ return fmt .Errorf ("failed to initialize VM: %w " , err )
279286 }
280287 defer vm .Disconnect ()
281288
282289 if err = vm .SetFirmwareVars (isoLocation , secureBoot , signingCert ); err != nil {
283- return fmt .Errorf ("failed to set UEFI variables: %v " , err )
290+ return fmt .Errorf ("failed to set UEFI variables: %w " , err )
284291 }
285292
286293 if err = vm .Start (); err != nil {
287- return fmt .Errorf ("failed to start VM: %v " , err )
294+ return fmt .Errorf ("failed to start VM: %w " , err )
288295 }
289296
290297 return nil
0 commit comments