forked from microsoft/azure-vhd-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vhdUploadCmdHandler.go
162 lines (144 loc) · 4.21 KB
/
vhdUploadCmdHandler.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"errors"
"fmt"
"log"
"net/url"
"runtime"
"strconv"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
"gopkg.in/urfave/cli.v1"
"github.com/flatcar/azure-vhd-utils/op"
)
func createServiceClient(c *cli.Context, account, key string) (*service.Client, error) {
var (
client *service.Client
err error
)
accountURL := fmt.Sprintf("https://%s.blob.core.windows.net", url.PathEscape(account))
if key != "" {
skc, err := service.NewSharedKeyCredential(account, key)
if err != nil {
return nil, fmt.Errorf("Failed to create shared key credential: %w", err)
}
client, err = service.NewClientWithSharedKeyCredential(accountURL, skc, nil)
} else {
opts := azidentity.DefaultAzureCredentialOptions{
DisableInstanceDiscovery: c.Bool("disableinstancediscovery"),
TenantID: c.String("tenantid"),
}
creds, err := azidentity.NewDefaultAzureCredential(&opts)
if err != nil {
return nil, fmt.Errorf("Failed to create default Azure credential: %w", err)
}
client, err = service.NewClient(accountURL, creds, nil)
}
if err != nil {
return nil, fmt.Errorf("Failed to create storage service client: %w", err)
}
return client, nil
}
func vhdUploadCmdHandler() cli.Command {
return cli.Command{
Name: "upload",
Usage: "Upload a local VHD to Azure storage as page blob",
Flags: []cli.Flag{
cli.StringFlag{
Name: "localvhdpath",
Usage: "Path to source VHD in the local machine.",
},
cli.StringFlag{
Name: "stgaccountname",
Usage: "Azure storage account name.",
},
cli.StringFlag{
Name: "stgaccountkey",
Usage: "Azure storage account key (optional).",
},
cli.StringFlag{
Name: "tenantid",
Usage: "Azure Tenant ID.",
},
cli.BoolFlag{
Name: "disableinstancediscovery",
Usage: "Skip the request to Microsoft Entra before authenticating.",
},
cli.StringFlag{
Name: "containername",
Usage: "Name of the container holding destination page blob. (Default: vhds)",
},
cli.StringFlag{
Name: "blobname",
Usage: "Name of the destination page blob.",
},
cli.StringFlag{
Name: "parallelism",
Usage: "Number of concurrent goroutines to be used for upload",
},
cli.BoolFlag{
Name: "overwrite",
Usage: "Overwrite the blob if already exists.",
},
},
Action: func(c *cli.Context) error {
const PageBlobPageSize int64 = 512
const PageBlobPageSetSize int64 = 4 * 1024 * 1024
localVHDPath := c.String("localvhdpath")
if localVHDPath == "" {
return errors.New("Missing required argument --localvhdpath")
}
stgAccountName := c.String("stgaccountname")
if stgAccountName == "" {
return errors.New("Missing required argument --stgaccountname")
}
// account key is optional, if not passed,
// then we expect that the required storage
// blob roles for storage account are already
// assigned to azure account
stgAccountKey := c.String("stgaccountkey")
containerName := c.String("containername")
if containerName == "" {
containerName = "vhds"
log.Println("Using default container 'vhds'")
}
blobName := c.String("blobname")
if blobName == "" {
return errors.New("Missing required argument --blobname")
}
if !strings.HasSuffix(strings.ToLower(blobName), ".vhd") {
blobName = blobName + ".vhd"
}
parallelism := int(0)
if c.IsSet("parallelism") {
p, err := strconv.ParseUint(c.String("parallelism"), 10, 32)
if err != nil {
return fmt.Errorf("invalid index value --parallelism: %s", err)
}
parallelism = int(p)
} else {
parallelism = 8 * runtime.NumCPU()
log.Printf("Using default parallelism [8*NumCPU] : %d\n", parallelism)
}
overwrite := c.IsSet("overwrite")
serviceClient, err := createServiceClient(c, stgAccountName, stgAccountKey)
if err != nil {
return err
}
uopts := op.UploadOptions{
Overwrite: overwrite,
Parallelism: parallelism,
Logger: func(s string) {
log.Println(s)
},
}
err = op.Upload(context.TODO(), serviceClient, containerName, blobName, localVHDPath, &uopts)
if err != nil {
log.Fatal(err)
}
return nil
},
}
}