-
Notifications
You must be signed in to change notification settings - Fork 6
/
post-processor.go
171 lines (148 loc) · 4.67 KB
/
post-processor.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
163
164
165
166
167
168
169
170
171
//go:generate packer-sdc mapstructure-to-hcl2 -type Config
package main
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/common"
"github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/plugin"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"io"
"net/http"
"os"
"strings"
)
var AmazonBuilderIds = []string{
"mitchellh.amazonebs",
"mitchellh.amazon.ebssurrogate",
"mitchellh.amazon.instance",
"mitchellh.amazon.chroot",
}
func main() {
server, err := plugin.Server()
if err != nil {
panic(err)
}
_ = server.RegisterPostProcessor(new(PostProcessor))
server.Serve()
}
type PostProcessor struct {
config Config
}
type Config struct {
common.PackerConfig `mapstructure:",squash"`
TeamCityUrl string `mapstructure:"teamcity_url"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Token string `mapstructure:"token"`
ProjectId string `mapstructure:"project_id"`
CloudImage string `mapstructure:"cloud_image"`
}
func (p *PostProcessor) ConfigSpec() hcldec.ObjectSpec {
return p.config.FlatMapstructure().HCL2Spec()
}
func (p *PostProcessor) Configure(raws ...interface{}) error {
err := config.Decode(&p.config, nil, raws...)
if err != nil {
return err
}
errs := new(packer.MultiError)
if p.config.TeamCityUrl != "" {
if p.config.Token == "" {
if p.config.Username == "" || p.config.Password == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("(`username` and `password`) or `token` is required"))
}
} else {
if p.config.Username != "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("`username` conflicts with `token`"))
}
if p.config.Password != "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("`password` conflicts with `token`"))
}
}
if p.config.ProjectId == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("project_id is required"))
}
if p.config.CloudImage == "" {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("cloud_image is required"))
}
}
if len(errs.Errors) > 0 {
return errs
}
return nil
}
func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact packer.Artifact) (a packer.Artifact, keep bool, forceOverride bool, err error) {
isAmazonArtifact := contains(AmazonBuilderIds, artifact.BuilderId())
var image string
if isAmazonArtifact {
s := strings.Split(artifact.Id(), ":")
image = s[1]
} else {
image = artifact.Id()
}
if os.Getenv("TEAMCITY_VERSION") != "" {
ui.Message(fmt.Sprintf("##teamcity[setParameter name='packer.artifact.%v.id' value='%v']", p.config.PackerBuildName, image))
if isAmazonArtifact {
s := strings.Split(artifact.Id(), ":")
region, ami := s[0], s[1]
ui.Message(fmt.Sprintf("##teamcity[setParameter name='packer.artifact.%v.aws.region' value='%v']", p.config.PackerBuildName, region))
ui.Message(fmt.Sprintf("##teamcity[setParameter name='packer.artifact.%v.aws.ami' value='%v']", p.config.PackerBuildName, ami))
} else {
ui.Message(fmt.Sprintf("##teamcity[setParameter name='packer.artifact.last.id' value='%v']", image))
}
}
if p.config.TeamCityUrl != "" {
var url string
if isAmazonArtifact {
url = fmt.Sprintf(
"%v/app/rest/projects/id:%v/projectFeatures/type:CloudImage,property(name:image-name-prefix,value:%v)/properties/amazon-id",
strings.TrimRight(p.config.TeamCityUrl, "/"),
p.config.ProjectId,
p.config.CloudImage,
)
} else {
url = fmt.Sprintf(
"%v/app/rest/projects/id:%v/projectFeatures/type:CloudImage,property(name:source-id,value:%v)/properties/sourceVmName",
strings.TrimRight(p.config.TeamCityUrl, "/"),
p.config.ProjectId,
p.config.CloudImage,
)
}
body := bytes.NewBufferString(image)
c := &http.Client{}
req, err := http.NewRequestWithContext(ctx, "PUT", url, body)
if err != nil {
return artifact, true, false, err
}
req.Header.Add("Content-Type", "text/plain")
if p.config.Token != "" {
req.Header.Set("Authorization", "Bearer "+p.config.Token)
} else {
req.SetBasicAuth(p.config.Username, p.config.Password)
}
resp, err := c.Do(req)
if err != nil {
return artifact, true, false, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != 200 {
return artifact, true, false, errors.New(fmt.Sprintf("Error updating a cloud profile: %v", resp.Status))
}
ui.Message(fmt.Sprintf("Cloud agent image '%v' is switched to image '%v'", p.config.CloudImage, image))
}
return artifact, true, false, nil
}
func contains(slice []string, value string) bool {
for _, element := range slice {
if element == value {
return true
}
}
return false
}