Skip to content

Commit bf40961

Browse files
author
Jeremi Piotrowski
committed
TMP: azure: Save screenshot on failure
1 parent 476fd15 commit bf40961

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

platform/api/azure/instance.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@ import (
3030
"github.com/flatcar/mantle/util"
3131
)
3232

33+
type MachineState int
34+
35+
const (
36+
READY MachineState = iota
37+
PROVISIONING
38+
)
39+
3340
type Machine struct {
3441
ID string
3542
PublicIPAddress string
3643
PrivateIPAddress string
3744
InterfaceName string
3845
PublicIPName string
46+
State MachineState
3947
}
4048

4149
func (a *API) getAvset() string {
@@ -239,7 +247,7 @@ func (a *API) CreateInstance(name, sshkey, resourceGroup, storageAccount string,
239247
defer cancel()
240248
_, err = poller.PollUntilDone(ctx, nil)
241249
if err != nil {
242-
return &Machine{ID: name}, fmt.Errorf("PollUntilDone: %w", err)
250+
return &Machine{ID: name, State: PROVISIONING}, fmt.Errorf("PollUntilDone: %w", err)
243251
}
244252
plog.Infof("Instance %s created", name)
245253

@@ -300,6 +308,38 @@ func (a *API) TerminateInstance(machine *Machine, resourceGroup string) error {
300308
return err
301309
}
302310

311+
func (a *API) GetScreenshot(name, resourceGroup string, output io.Writer) error {
312+
vmResourceGroup := a.getVMRG(resourceGroup)
313+
param := &armcompute.VirtualMachinesClientRetrieveBootDiagnosticsDataOptions{
314+
SasURIExpirationTimeInMinutes: to.Ptr[int32](5),
315+
}
316+
resp, err := a.compClient.RetrieveBootDiagnosticsData(context.TODO(), vmResourceGroup, name, param)
317+
if err != nil {
318+
return fmt.Errorf("could not get VM: %v", err)
319+
}
320+
if resp.ConsoleScreenshotBlobURI == nil {
321+
return fmt.Errorf("console screenshot URI is nil")
322+
}
323+
324+
var data io.ReadCloser
325+
err = util.Retry(6, 10*time.Second, func() error {
326+
reply, err := http.Get(*resp.ConsoleScreenshotBlobURI)
327+
if err != nil {
328+
return fmt.Errorf("could not GET console screenshot: %v", err)
329+
}
330+
data = reply.Body
331+
return nil
332+
})
333+
if err != nil {
334+
return err
335+
}
336+
written, err := io.Copy(output, data)
337+
if err == nil {
338+
plog.Debugf("wrote %d bytes to screenshot", written)
339+
}
340+
return err
341+
}
342+
303343
func (a *API) GetConsoleOutput(name, resourceGroup, storageAccount string) ([]byte, error) {
304344
vmResourceGroup := a.getVMRG(resourceGroup)
305345
param := &armcompute.VirtualMachinesClientRetrieveBootDiagnosticsDataOptions{

platform/machine/azure/machine.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ func (am *machine) saveConsole() error {
126126
return fmt.Errorf("failed writing console to file: %v", err)
127127
}
128128

129+
if am.mach.State == azure.PROVISIONING {
130+
path := filepath.Join(am.dir, "screenshot.bmp")
131+
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
132+
if err != nil {
133+
return err
134+
}
135+
defer f.Close()
136+
return am.cluster.flight.Api.GetScreenshot(am.ID(), am.ResourceGroup(), f)
137+
}
138+
129139
return nil
130140
}
131141

0 commit comments

Comments
 (0)