/
vmserialconsole.go
82 lines (66 loc) · 2.28 KB
/
vmserialconsole.go
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
package adminactions
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
mgmtcompute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
mgmtstorage "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
azstorage "github.com/Azure/azure-sdk-for-go/storage"
"github.com/Azure/go-autorest/autorest/date"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/util/stringutils"
)
func (a *azureActions) VMSerialConsole(ctx context.Context, w http.ResponseWriter,
log *logrus.Entry, vmName string) error {
clusterRGName := stringutils.LastTokenByte(a.oc.Properties.ClusterProfile.ResourceGroupID, '/')
vm, err := a.virtualMachines.Get(ctx, clusterRGName, vmName, mgmtcompute.InstanceView)
if err != nil {
return err
}
if vm.InstanceView == nil || vm.InstanceView.BootDiagnostics == nil {
return fmt.Errorf("BootDiagnostics not enabled on %s, serial log is not available", vmName)
}
u, err := url.Parse(*vm.InstanceView.BootDiagnostics.SerialConsoleLogBlobURI)
if err != nil {
return err
}
parts := strings.Split(u.Path, "/")
if len(parts) != 3 {
return fmt.Errorf("serialConsoleLogBlobURI has %d parts, expected 3", len(parts))
}
t := time.Now().UTC().Truncate(time.Second)
res, err := a.storageAccounts.ListAccountSAS(
ctx, clusterRGName, "cluster"+a.oc.Properties.StorageSuffix, mgmtstorage.AccountSasParameters{
Services: mgmtstorage.B,
ResourceTypes: mgmtstorage.SignedResourceTypesO,
Permissions: mgmtstorage.R,
Protocols: mgmtstorage.HTTPS,
SharedAccessStartTime: &date.Time{Time: t},
SharedAccessExpiryTime: &date.Time{Time: t.Add(24 * time.Hour)},
})
if err != nil {
return err
}
v, err := url.ParseQuery(*res.AccountSasToken)
if err != nil {
return err
}
blobService := azstorage.NewAccountSASClient(
"cluster"+a.oc.Properties.StorageSuffix, v, (*a.env.Environment()).Environment).GetBlobService()
c := blobService.GetContainerReference(parts[1])
b := c.GetBlobReference(parts[2])
rc, err := b.Get(nil)
if err != nil {
return err
}
defer rc.Close()
w.Header().Add("Content-Type", "text/plain")
_, err = io.Copy(w, rc)
return err
}