|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | | - "text/template" |
| 4 | + "encoding/xml" |
| 5 | + "fmt" |
6 | 6 |
|
7 | 7 | log "github.com/Sirupsen/logrus" |
8 | 8 | "github.com/codegangsta/cli" |
| 9 | + "io/ioutil" |
| 10 | + "strings" |
9 | 11 | ) |
10 | 12 |
|
| 13 | +type certificate struct { |
| 14 | + StoreLocation string `xml:"StoreLocation,omitempty"` |
| 15 | + StoreName string `xml:"StoreName,omitempty"` |
| 16 | + ThumbprintRequired bool `xml:"ThumbprintRequired,omitempty"` |
| 17 | + ThumbprintAlgorithm string `xml:"ThumbprintAlgorithm,omitempty"` |
| 18 | +} |
| 19 | + |
| 20 | +// NOTE(@boumenot): there is probably a better way to express this. If |
| 21 | +// you know please share... |
| 22 | +// |
| 23 | +// The only difference between ExtensionImage and ExtensionImageGlobal is the |
| 24 | +// Regions element. This element can be in three different states to my |
| 25 | +// knowledge. |
| 26 | +// |
| 27 | +// 1. not defined |
| 28 | +// 2. <Regions>Region1;Region2</Regions> |
| 29 | +// 3. <Regions></Regions> |
| 30 | +// |
| 31 | +// Case (1) occurs when an extension is first published. Case(2) occurs when |
| 32 | +// an extension is promoted to one or two regions. Case (3) occurs when an |
| 33 | +// extension is published to all regions. |
| 34 | +// |
| 35 | +// I do not know how to express all three cases using Go's XML serializer. |
| 36 | +// |
| 37 | +type extensionImage struct { |
| 38 | + XMLName string `xml:"ExtensionImage"` |
| 39 | + NS string `xml:"xmlns,attr"` |
| 40 | + ProviderNameSpace string `xml:"ProviderNameSpace"` |
| 41 | + Type string `xml:"Type"` |
| 42 | + Version string `xml:"Version"` |
| 43 | + Label string `xml:"Label"` |
| 44 | + HostingResources string `xml:"HostingResources"` |
| 45 | + MediaLink string `xml:"MediaLink"` |
| 46 | + Certificate *certificate `xml:"Certificate,omitempty"` |
| 47 | + PublicConfigurationSchema string `xml:"PublicConfigurationSchema,omitempty"` |
| 48 | + PrivateConfigurationSchema string `xml:"PrivateConfigurationSchema,omitempty"` |
| 49 | + Description string `xml:"Description"` |
| 50 | + BlockRoleUponFailure string `xml:"BlockRoleUponFailure,omitempty"` |
| 51 | + IsInternalExtension bool `xml:"IsInternalExtension"` |
| 52 | + Eula string `xml:"Eula,omitempty"` |
| 53 | + PrivacyURI string `xml:"PrivacyUri,omitempty"` |
| 54 | + HomepageURI string `xml:"HomepageUri,omitempty"` |
| 55 | + IsJSONExtension bool `xml:"IsJsonExtension,omitempty"` |
| 56 | + CompanyName string `xml:"CompanyName,omitempty"` |
| 57 | + SupportedOS string `xml:"SupportedOS,omitempty"` |
| 58 | + Regions string `xml:"Regions,omitempty"` |
| 59 | +} |
| 60 | + |
| 61 | +type extensionImageGlobal struct { |
| 62 | + XMLName string `xml:"ExtensionImage"` |
| 63 | + NS string `xml:"xmlns,attr"` |
| 64 | + ProviderNameSpace string `xml:"ProviderNameSpace"` |
| 65 | + Type string `xml:"Type"` |
| 66 | + Version string `xml:"Version"` |
| 67 | + Label string `xml:"Label"` |
| 68 | + HostingResources string `xml:"HostingResources"` |
| 69 | + MediaLink string `xml:"MediaLink"` |
| 70 | + Certificate *certificate `xml:"Certificate,omitempty"` |
| 71 | + PublicConfigurationSchema string `xml:"PublicConfigurationSchema,omitempty"` |
| 72 | + PrivateConfigurationSchema string `xml:"PrivateConfigurationSchema,omitempty"` |
| 73 | + Description string `xml:"Description"` |
| 74 | + BlockRoleUponFailure string `xml:"BlockRoleUponFailure,omitempty"` |
| 75 | + IsInternalExtension bool `xml:"IsInternalExtension"` |
| 76 | + Eula string `xml:"Eula,omitempty"` |
| 77 | + PrivacyURI string `xml:"PrivacyUri,omitempty"` |
| 78 | + HomepageURI string `xml:"HomepageUri,omitempty"` |
| 79 | + IsJSONExtension bool `xml:"IsJsonExtension,omitempty"` |
| 80 | + CompanyName string `xml:"CompanyName,omitempty"` |
| 81 | + SupportedOS string `xml:"SupportedOS,omitempty"` |
| 82 | + Regions string `xml:"Regions"` |
| 83 | +} |
| 84 | + |
| 85 | +type extensionManifest interface { |
| 86 | + Marshal() ([]byte, error) |
| 87 | +} |
| 88 | + |
| 89 | +func isGuestAgent(providerNameSpace string) bool { |
| 90 | + return "Microsoft.OSTCLinuxAgent" == providerNameSpace |
| 91 | +} |
| 92 | + |
| 93 | +func newExtensionImageManifest(filename string, regions []string) (extensionManifest, error) { |
| 94 | + b, err := ioutil.ReadFile(filename) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + var manifest extensionImage |
| 100 | + err = xml.Unmarshal(b, &manifest) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + manifest.Regions = strings.Join(regions, ";") |
| 106 | + |
| 107 | + if !isGuestAgent(manifest.ProviderNameSpace) { |
| 108 | + manifest.IsInternalExtension = false |
| 109 | + } else { |
| 110 | + log.Debug("VM agent namespace detected, IsInternalExtension ignored") |
| 111 | + } |
| 112 | + |
| 113 | + return &manifest, nil |
| 114 | +} |
| 115 | + |
| 116 | +func newExtensionImageGlobalManifest(filename string) (extensionManifest, error) { |
| 117 | + b, err := ioutil.ReadFile(filename) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + var manifest extensionImageGlobal |
| 123 | + err = xml.Unmarshal(b, &manifest) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + manifest.IsInternalExtension = !isGuestAgent(manifest.ProviderNameSpace) |
| 129 | + return &manifest, nil |
| 130 | +} |
| 131 | + |
| 132 | +func (ext *extensionImage) Marshal() ([]byte, error) { |
| 133 | + return xml.Marshal(*ext) |
| 134 | +} |
| 135 | + |
| 136 | +func (ext *extensionImageGlobal) Marshal() ([]byte, error) { |
| 137 | + return xml.Marshal(*ext) |
| 138 | +} |
| 139 | + |
11 | 140 | func newExtensionManifest(c *cli.Context) { |
12 | 141 | cl := mkClient(checkFlag(c, flMgtURL.Name), checkFlag(c, flSubsID.Name), checkFlag(c, flSubsCert.Name)) |
13 | 142 | storageRealm := checkFlag(c, flStorageRealm.Name) |
14 | 143 | storageAccount := checkFlag(c, flStorageAccount.Name) |
15 | 144 | extensionPkg := checkFlag(c, flPackage.Name) |
16 | 145 |
|
17 | | - var p struct { |
18 | | - Namespace, Name, Version, BlobURL, Label, Description, Eula, Privacy, Homepage, Company, OS string |
19 | | - } |
20 | | - flags := []struct { |
21 | | - ref *string |
22 | | - fl string |
23 | | - }{ |
24 | | - {&p.Namespace, flNamespace.Name}, |
25 | | - {&p.Name, flName.Name}, |
26 | | - {&p.Version, flVersion.Name}, |
27 | | - {&p.Label, "label"}, |
28 | | - {&p.Description, "description"}, |
29 | | - {&p.Eula, "eula-url"}, |
30 | | - {&p.Privacy, "privacy-url"}, |
31 | | - {&p.Homepage, "homepage-url"}, |
32 | | - {&p.Company, "company"}, |
33 | | - {&p.OS, "supported-os"}, |
34 | | - } |
35 | | - for _, f := range flags { |
36 | | - *f.ref = checkFlag(c, f.fl) |
37 | | - } |
38 | | - |
39 | 146 | // Upload extension blob |
40 | 147 | blobURL, err := uploadBlob(cl, storageRealm, storageAccount, extensionPkg) |
41 | 148 | if err != nil { |
42 | 149 | log.Fatal(err) |
43 | 150 | } |
44 | 151 | log.Debugf("Extension package uploaded to: %s", blobURL) |
45 | | - p.BlobURL = blobURL |
46 | | - |
47 | | - // doing a text template is easier and let us create comments (xml encoder can't) |
48 | | - // that are used as placeholders later on. |
49 | | - manifestXML := `<?xml version="1.0" encoding="utf-8" ?> |
50 | | -<ExtensionImage xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> |
51 | | - <!-- WARNING: Ordering of fields matter in this file. --> |
52 | | - <ProviderNameSpace>{{.Namespace}}</ProviderNameSpace> |
53 | | - <Type>{{.Name}}</Type> |
54 | | - <Version>{{.Version}}</Version> |
55 | | - <Label>{{.Label}}</Label> |
56 | | - <HostingResources>VmRole</HostingResources> |
57 | | - <MediaLink>{{.BlobURL}}</MediaLink> |
58 | | - <Description>{{.Description}}</Description> |
59 | | - <IsInternalExtension>true</IsInternalExtension> |
60 | | - <Eula>{{.Eula}}</Eula> |
61 | | - <PrivacyUri>{{.Privacy}}</PrivacyUri> |
62 | | - <HomepageUri>{{.Homepage}}</HomepageUri> |
63 | | - <IsJsonExtension>true</IsJsonExtension> |
64 | | - <CompanyName>{{.Company}}</CompanyName> |
65 | | - <SupportedOS>{{.OS}}</SupportedOS> |
66 | | - <!--%REGIONS%--> |
67 | | -</ExtensionImage> |
68 | | -` |
69 | | - tpl, err := template.New("manifest").Parse(manifestXML) |
70 | | - if err != nil { |
71 | | - log.Fatalf("template parse error: %v", err) |
| 152 | + |
| 153 | + manifest := extensionImage{ |
| 154 | + ProviderNameSpace: checkFlag(c, flNamespace.Name), |
| 155 | + Type: checkFlag(c, flName.Name), |
| 156 | + Version: checkFlag(c, flVersion.Name), |
| 157 | + Label: "label", |
| 158 | + Description: "description", |
| 159 | + IsInternalExtension: true, |
| 160 | + MediaLink: blobURL, |
| 161 | + Eula: "eula-url", |
| 162 | + PrivacyURI: "privacy-url", |
| 163 | + HomepageURI: "homepage-url", |
| 164 | + IsJSONExtension: true, |
| 165 | + CompanyName: "company", |
| 166 | + SupportedOS: "supported-os", |
72 | 167 | } |
73 | | - if err = tpl.Execute(os.Stdout, p); err != nil { |
74 | | - log.Fatalf("template execute error: %v", err) |
| 168 | + |
| 169 | + bs, err := xml.MarshalIndent(manifest, "", " ") |
| 170 | + if err != nil { |
| 171 | + log.Fatalf("xml marshall error: %v", err) |
75 | 172 | } |
| 173 | + |
| 174 | + fmt.Println(string(bs)) |
76 | 175 | } |
0 commit comments