-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease_descriptor.go
176 lines (155 loc) · 4.09 KB
/
release_descriptor.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
172
173
174
175
176
package main
import (
"encoding/xml"
"fmt"
"log"
"os"
"io/ioutil"
"github.com/blang/semver"
)
//Artifact map type to custom unmarshal it
type ArtifactMap map[string]Artifact
//evertime a artifact is found in the xml it gets decoded using this function, it
//unmarshals the artifact struct and stores it in the map
func (am ArtifactMap) UnmarshalXML(e *xml.Decoder, start xml.StartElement) error {
a := Artifact{}
err := e.DecodeElement(&a, &start)
if err != nil {
return err
}
am[a.Id] = a
return nil
}
func (am ArtifactMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
for _, a := range am {
err := e.Encode(a)
if err != nil {
return err
}
}
return nil
}
//Semver type to be able to custom unmarshal it
type Version struct {
semver.Version
}
//Get the version string an create a semver from it
func (v *Version) UnmarshalXMLAttr(attr xml.Attr) error {
str := attr.Value
parsed, err := semver.New(str)
if err != nil {
return err
}
v.Version = *parsed
return nil
}
//Get the version string an create a semver from it
func (v Version) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{Name: name, Value: v.String()}, nil
}
//Collection of artifacts
type ReleaseDescriptor struct {
XMLName xml.Name `xml:"releaseDescriptor"`
Href string `xml:"href,attr"` //href where to get this descriptor
Version Version `xml:"version,attr"` //version of the this release
Artifacts ArtifactMap `xml:"artifact"` //artifacts associated to this descriptor, the key is the artifact id
Time string `xml:"time"` //timestamp of the desciptor generation
}
//Create a new descriptor from all the info
func NewReleaseDescriptor(href string, version string, artifacts ...Artifact) (rd ReleaseDescriptor, err error) {
sver, err := semver.Parse(version)
if err != nil {
return
}
rd = ReleaseDescriptor{
Href: href,
Version: Version{sver},
Artifacts: map[string]Artifact{},
}
for _, a := range artifacts {
rd.Artifacts[a.Id] = a
}
return
}
//Create a new empty relase descriptor
func NewEmptyReleaseDescriptor() ReleaseDescriptor {
return ReleaseDescriptor{
Artifacts: map[string]Artifact{},
}
}
//Compares two indeces Returning a list of differences
func (i ReleaseDescriptor) IsDiff(old ReleaseDescriptor) (is bool, diffs DiffSet) {
//no changes
if i.Version.Compare(old.Version.Version) <= 0 {
return
}
news := i.Artifacts
olds := old.Artifacts
//range the new artifacts to find differences
for id, n := range news {
newArt := n
oldArt, ok := olds[id]
//there's no old version
if !ok {
diffs = append(diffs, Diff{New: &newArt, Old: nil})
} else if newArt.Version != oldArt.Version {
diffs = append(diffs, Diff{New: &newArt, Old: &oldArt})
}
}
//range the old artifacts to find deleted artifacts
for id, o := range olds {
if _, ok := news[id]; !ok {
oldArt := o
diffs = append(diffs, Diff{New: nil, Old: &oldArt})
}
}
return true, diffs
}
func (r ReleaseDescriptor) UpdateFrom(local ReleaseDescriptor, installationPath string) (success bool, err error) {
changes, diffSet := r.IsDiff(local)
if !changes {
Info("There are no new versions")
//nothing to do!
return false, nil
}
Info("Updating to version %s", r.Version)
tempDir, err := ioutil.TempDir("", "pipeline-updater")
if err != nil {
return false, err
}
toDeploy, err := Download(tempDir, diffSet.ToDownload()...)
if err != nil {
os.RemoveAll(tempDir)
return false, err
}
ok, errs := Remove(diffSet.ToRemove(installationPath))
if !ok {
//warn
log.Printf("errs %+v\n", errs)
}
ok, errs = Deploy(toDeploy, installationPath)
if !ok {
//warn
log.Printf("errs %+v\n", errs)
}
err = os.RemoveAll(tempDir)
if err != nil {
//warn
log.Printf("err %+v\n", err)
}
return true, nil
}
//String for logging
func (rd ReleaseDescriptor) String() string {
return fmt.Sprintf("ReleaseDescriptor %v", rd.Version.String())
}
func (rd ReleaseDescriptor) Save(path string) error {
data, err := xml.Marshal(rd)
f, err := os.Create(path)
defer f.Close()
if err != nil {
return err
}
_, err = f.Write(data)
return err
}