-
Notifications
You must be signed in to change notification settings - Fork 12
/
publish.go
120 lines (107 loc) · 3.81 KB
/
publish.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/Azure/azure-sdk-for-go/management"
"github.com/Azure/azure-sdk-for-go/management/storageservice"
"github.com/Azure/azure-sdk-for-go/storage"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
const (
containerName = "extension-packages"
)
func publishExtension(c *cli.Context, operationName string, manifest []byte, op func([]byte) (management.OperationID, error)) error {
log.Infof("%s operation starting...", operationName)
mPath, err := saveManifestForDebugging(manifest)
if err != nil {
return fmt.Errorf("Error saving manifest for debugging: %v", err)
}
log.Debugf("Saving used manifest for debugging: %s", mPath)
cl := mkClient(checkFlag(c, flMgtURL.Name), checkFlag(c, flSubsID.Name), checkFlag(c, flSubsCert.Name))
opID, err := op(manifest)
if err != nil {
return fmt.Errorf("Error: %v", err)
}
log.Debugf("%s operation started.", operationName)
if err := cl.WaitForOperation(opID); err != nil {
return fmt.Errorf("%s failed: %v", operationName, err)
}
log.Infof("%s operation finished.", operationName)
return nil
}
func saveManifestForDebugging(contents []byte) (string, error) {
dir, err := ioutil.TempDir("", "extension-manifests")
if err != nil {
return "", err
}
f, err := ioutil.TempFile(dir, "manifest")
if err != nil {
return "", err
}
if _, err := f.Write(contents); err != nil {
return "", err
}
fi, err := f.Stat()
if err != nil {
return "", err
}
return filepath.Join(dir, fi.Name()), nil
}
func publishExtensionFromManifestFile(c *cli.Context, operationName, manifestPath string, op func([]byte) (management.OperationID, error)) error {
b, err := ioutil.ReadFile(manifestPath)
if err != nil {
return fmt.Errorf("Error reading manifest: %v", err)
}
return publishExtension(c, operationName, b, op)
}
func createExtension(c *cli.Context) {
cl := mkClient(checkFlag(c, flMgtURL.Name), checkFlag(c, flSubsID.Name), checkFlag(c, flSubsCert.Name))
if err := publishExtensionFromManifestFile(c, "CreateExtension",
checkFlag(c, flManifest.Name), cl.CreateExtension); err != nil {
log.Fatal(err)
}
}
func updateExtension(c *cli.Context) {
cl := mkClient(checkFlag(c, flMgtURL.Name), checkFlag(c, flSubsID.Name), checkFlag(c, flSubsCert.Name))
if err := publishExtensionFromManifestFile(c, "UpdateExtension", checkFlag(c, flManifest.Name),
cl.UpdateExtension); err != nil {
log.Fatal(err)
}
}
func uploadBlob(cl ExtensionsClient, storageRealm, storageAccount, packagePath string) (string, error) {
// Fetch keys for storage account
svc := storageservice.NewClient(cl.client)
keys, err := svc.GetStorageServiceKeys(storageAccount)
if err != nil {
return "", fmt.Errorf("Could not fetch keys for storage account. Make sure it is in publisher subscription. Error: %v", err)
}
log.Debug("Retrieved storage account keys.")
// Read package
pkg, err := os.OpenFile(packagePath, os.O_RDONLY, 0777)
if err != nil {
return "", fmt.Errorf("Could not reach package file: %v", err)
}
stat, err := pkg.Stat()
if err != nil {
return "", fmt.Errorf("Could not stat the package file: %v", err)
}
defer pkg.Close()
// Upload blob
sc, err := storage.NewClient(storageAccount, keys.PrimaryKey, storageRealm, storage.DefaultAPIVersion, true)
if err != nil {
return "", fmt.Errorf("Could not create storage client: %v", err)
}
bs := sc.GetBlobService()
if _, err := bs.CreateContainerIfNotExists(containerName, storage.ContainerAccessTypeBlob); err != nil {
return "", fmt.Errorf("Error creating blob container: %v", err)
}
blobName := fmt.Sprintf("%d.zip", time.Now().Unix())
if err := bs.CreateBlockBlobFromReader(containerName, blobName, uint64(stat.Size()), pkg, nil); err != nil {
return "", fmt.Errorf("Error uploading blob: %v", err)
}
return bs.GetBlobURL(containerName, blobName), nil
}