forked from Azure/azure-extensions-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
promote.go
74 lines (64 loc) · 2.3 KB
/
promote.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
func promoteToFirstSlice(c *cli.Context) {
if err := promoteExtension(c, mkRegionElement(
checkFlag(c, flRegion1.Name))); err != nil {
log.Fatal(err)
}
log.Info("Extension is promoted to PROD in one region. See replication-status.")
}
func promoteToSecondSlice(c *cli.Context) {
if err := promoteExtension(c, mkRegionElement(
checkFlag(c, flRegion1.Name),
checkFlag(c, flRegion2.Name))); err != nil {
log.Fatal(err)
}
log.Info("Extension is promoted to PROD in two regions. See replication-status.")
}
func promoteToAllRegions(c *cli.Context) {
regions := `` // replace placeholder with empty string to omit the element.
if err := promoteExtension(c, regions); err != nil {
log.Fatal(err)
}
log.Info("Extension is promoted to all regions. See replication-status.")
}
func promoteExtension(c *cli.Context, regionsXML string) error {
b, err := updateManifestRegions(checkFlag(c, flManifest.Name), regionsXML)
if err != nil {
return err
}
if err := publishExtension(c, "UpdateExtension", b,
mkClient(checkFlag(c, flMgtURL.Name), checkFlag(c, flSubsID.Name), checkFlag(c, flSubsCert.Name)).UpdateExtension); err != nil {
return err
}
return nil
}
func mkRegionElement(regions ...string) string {
return fmt.Sprintf(`<Regions>%s</Regions>`, strings.Join(regions, ";"))
}
// updateManifestRegions makes an in-memory update to the <!--%REGIONS%-->
// placeholder string in the manifest XML for further usage, and
// sets <IsInternalExtension> according to whether the package
// should remain internal (vm agent) or not (extensions)
func updateManifestRegions(manifestPath string, regionsXMLElement string) ([]byte, error) {
b, err := ioutil.ReadFile(manifestPath)
if err != nil {
return nil, fmt.Errorf("Error reading manifest: %v", err)
}
// todo: improve this
b = bytes.Replace(b, []byte(`<!--%REGIONS%-->`), []byte(regionsXMLElement), 1)
updateInternal := !bytes.Contains(b, []byte(`<ProviderNameSpace>Microsoft.OSTCLinuxAgent</ProviderNameSpace>`))
if updateInternal {
b = bytes.Replace(b, []byte(`<IsInternalExtension>true`), []byte(`<IsInternalExtension>false`), 1)
} else {
log.Debug("VM agent namespace detected, IsInternalExtension ignored")
}
return b, err
}