-
Notifications
You must be signed in to change notification settings - Fork 11
/
artifact.go
331 lines (291 loc) · 11.9 KB
/
artifact.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) 2020 Sorint.lab S.p.A.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package repo
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/1set/gut/yos"
"github.com/ercole-io/ercole/utils"
)
// ArtifactInfo contains info about all files in repository
type ArtifactInfo struct {
Repository string
Installed bool `json:"-"`
Version string
ReleaseDate string
Filename string
Name string
OperatingSystemFamily string
OperatingSystem string
Arch string
UpstreamType string
UpstreamInfo map[string]interface{}
}
//Regex for filenames
var (
agentRHEL5Regex *regexp.Regexp = regexp.MustCompile("^ercole-agent-(?P<version>.*)-1.(?P<arch>x86_64).rpm$")
agentRHELRegex *regexp.Regexp = regexp.MustCompile("^ercole-agent-(?P<version>.*)-1.el(?P<dist>\\d+).(?P<arch>x86_64).rpm$")
agentVirtualizationRHELRegex *regexp.Regexp = regexp.MustCompile("^ercole-agent-virtualization-(?P<version>.*)-1.el(?P<dist>\\d+).(?P<arch>x86_64).rpm$")
agentExadataRHELRegex *regexp.Regexp = regexp.MustCompile("^ercole-agent-exadata-(?P<version>.*)-1.el(?P<dist>\\d+).(?P<arch>x86_64).rpm$")
agentWinRegex *regexp.Regexp = regexp.MustCompile("^ercole-agent-setup-(?P<version>.*).exe$")
agentHpuxRegex *regexp.Regexp = regexp.MustCompile("^ercole-agent-hpux-(?P<version>.*).tar.gz")
agentAixRegexRpm *regexp.Regexp = regexp.MustCompile("^ercole-agent-aix-(?P<version>.*)-1.(?P<dist>.*).(?P<arch>noarch).rpm$")
agentAixRegexTarGz *regexp.Regexp = regexp.MustCompile("^ercole-agent-aix-(?P<version>.*).tar.gz$")
ercoleRHELRegex *regexp.Regexp = regexp.MustCompile("^ercole-(?P<version>.*)-1.el(?P<dist>\\d+).(?P<arch>x86_64).rpm$")
ercoleWebRHELRegex *regexp.Regexp = regexp.MustCompile("^ercole-web-(?P<version>.*)-1.el(?P<dist>\\d+).(?P<arch>noarch).rpm$")
ercoleAgentPerlRpmRegex *regexp.Regexp = regexp.MustCompile("^ercole-agent-perl-(?P<version>.*)-1.(?P<dist>[a-z0-9.]+).(?P<arch>noarch).rpm$")
ercoleAgentPerlTarGzRegex *regexp.Regexp = regexp.MustCompile("^ercole-agent-perl-(?P<version>.*)-1.(?P<dist>[a-z0-9.]+).(?P<arch>noarch).tar.gz$")
)
const (
// UpstreamTypeLocal repository upstream type
UpstreamTypeLocal = "local"
// UpstreamTypeDirectory repository upstream type
UpstreamTypeDirectory = "directory"
// UpstreamTypeGitHub repository upstream type
UpstreamTypeGitHub = "github-release"
// UpstreamTypeErcoleRepo repository upstream type
UpstreamTypeErcoleRepo = "ercole-reposervice"
)
// FullName return the fullname of the file
func (artifact *ArtifactInfo) FullName() string {
return fmt.Sprintf("%s/%s@%s", artifact.Repository, artifact.Name, artifact.Version)
}
// DirectoryPath get the path of the directory containing the artifact file
func (artifact *ArtifactInfo) DirectoryPath(distributedFiles string) string {
return filepath.Join(distributedFiles, artifact.OperatingSystemFamily, artifact.OperatingSystem, artifact.Arch)
}
//FilePath get the path of the artifact file
func (artifact *ArtifactInfo) FilePath(distributedFiles string) string {
return filepath.Join(artifact.DirectoryPath(distributedFiles), artifact.Filename)
}
// IsInstalled return true if file is detected in the distribution directory
func (artifact *ArtifactInfo) IsInstalled(distributedFiles string) bool {
_, err := os.Stat(filepath.Join(distributedFiles, "all", artifact.Filename))
return !os.IsNotExist(err)
}
// SetInfoFromFileName sets to fileInfo informations taken from filename
func (artifact *ArtifactInfo) SetInfoFromFileName(filename string) error {
switch {
case agentVirtualizationRHELRegex.MatchString(filename): //agent virtualization RHEL
data := utils.FindNamedMatches(agentVirtualizationRHELRegex, filename)
artifact.Name = "ercole-agent-virtualization-rhel" + data["dist"]
artifact.Version = data["version"]
artifact.Arch = data["arch"]
artifact.OperatingSystemFamily = "rhel"
artifact.OperatingSystem = "rhel" + data["dist"]
case agentExadataRHELRegex.MatchString(filename): //agent exadata RHEL
data := utils.FindNamedMatches(agentExadataRHELRegex, filename)
artifact.Name = "ercole-agent-exadata-rhel" + data["dist"]
artifact.Version = data["version"]
artifact.Arch = data["arch"]
artifact.OperatingSystemFamily = "rhel"
artifact.OperatingSystem = "rhel" + data["dist"]
case agentRHEL5Regex.MatchString(filename): //agent RHEL5
data := utils.FindNamedMatches(agentRHEL5Regex, filename)
artifact.Name = "ercole-agent-rhel5"
artifact.Version = data["version"]
artifact.Arch = data["arch"]
artifact.OperatingSystemFamily = "rhel"
artifact.OperatingSystem = "rhel5"
case agentRHELRegex.MatchString(filename): //agent RHEL
data := utils.FindNamedMatches(agentRHELRegex, filename)
artifact.Name = "ercole-agent-rhel" + data["dist"]
artifact.Version = data["version"]
artifact.Arch = data["arch"]
artifact.OperatingSystemFamily = "rhel"
artifact.OperatingSystem = "rhel" + data["dist"]
case ercoleRHELRegex.MatchString(filename): //ercole RHEL
data := utils.FindNamedMatches(ercoleRHELRegex, filename)
artifact.Name = "ercole-" + data["dist"]
artifact.Version = data["version"]
artifact.Arch = data["arch"]
artifact.OperatingSystemFamily = "rhel"
artifact.OperatingSystem = "rhel" + data["dist"]
case ercoleWebRHELRegex.MatchString(filename): //ercole-web RHEL
data := utils.FindNamedMatches(ercoleWebRHELRegex, filename)
artifact.Name = "ercole-web" + data["dist"]
artifact.Version = data["version"]
artifact.Arch = data["arch"]
artifact.OperatingSystemFamily = "rhel"
artifact.OperatingSystem = "rhel" + data["dist"]
case agentWinRegex.MatchString(filename): //agent WIN
data := utils.FindNamedMatches(agentWinRegex, filename)
artifact.Name = "ercole-agent-win"
artifact.Version = data["version"]
artifact.Arch = "x86_64"
artifact.OperatingSystemFamily = "win"
artifact.OperatingSystem = "win"
case agentHpuxRegex.MatchString(filename): //agent HPUX
data := utils.FindNamedMatches(agentHpuxRegex, filename)
artifact.Name = "ercole-agent-hpux"
artifact.Version = data["version"]
artifact.Arch = "noarch"
artifact.OperatingSystemFamily = "hpux"
artifact.OperatingSystem = "hpux"
case agentAixRegexRpm.MatchString(filename): //agent AIX
data := utils.FindNamedMatches(agentAixRegexRpm, filename)
artifact.Name = "ercole-agent-aix"
artifact.Version = data["version"]
artifact.Arch = "noarch"
artifact.OperatingSystemFamily = "aix"
artifact.OperatingSystem = data["dist"]
case agentAixRegexTarGz.MatchString(filename): //agent AIX
data := utils.FindNamedMatches(agentAixRegexTarGz, filename)
artifact.Name = "ercole-agent-aix-targz"
artifact.Version = data["version"]
artifact.Arch = "noarch"
artifact.OperatingSystemFamily = "aix-tar-gz"
artifact.OperatingSystem = "aix6.1"
case ercoleAgentPerlRpmRegex.MatchString(filename): //agent perl rpm
data := utils.FindNamedMatches(ercoleAgentPerlRpmRegex, filename)
artifact.Name = "ercole-agent-" + data["dist"] + "-rpm"
artifact.Version = data["version"]
artifact.Arch = data["arch"]
switch data["dist"] {
case "aix6.1":
artifact.OperatingSystemFamily = "aix"
artifact.OperatingSystem = data["dist"]
default:
return fmt.Errorf("Unknown distribution %s", data["dist"])
}
case ercoleAgentPerlTarGzRegex.MatchString(filename): //agent perl tar gz
data := utils.FindNamedMatches(ercoleAgentPerlTarGzRegex, filename)
artifact.Name = "ercole-agent-" + data["dist"] + "-tar-gz"
artifact.Version = data["version"]
artifact.Arch = data["arch"]
switch data["dist"] {
case "aix6.1":
artifact.OperatingSystemFamily = "aix"
artifact.OperatingSystem = data["dist"]
case "hpux":
artifact.OperatingSystemFamily = "hpux"
artifact.OperatingSystem = data["dist"]
case "solaris11":
artifact.OperatingSystemFamily = "solaris"
artifact.OperatingSystem = data["dist"]
default:
return fmt.Errorf("Unknown distribution %s", data["dist"])
}
default:
return fmt.Errorf("Filename %s is not supported. Please check that it is correct", filename)
}
return nil
}
// Download the artifact
func (artifact *ArtifactInfo) Download(verbose bool, dest string) {
switch artifact.UpstreamType {
case UpstreamTypeGitHub:
utils.DownloadFile(dest, artifact.UpstreamInfo["DownloadUrl"].(string))
case UpstreamTypeDirectory:
if verbose {
fmt.Printf("Copying file from %s to %s\n", artifact.UpstreamInfo["Filename"].(string), dest)
}
err := yos.CopyFile(artifact.UpstreamInfo["Filename"].(string), dest)
if err != nil {
panic(err)
}
err = os.Chmod(dest, 0755)
if err != nil {
panic(err)
}
case UpstreamTypeErcoleRepo:
utils.DownloadFile(dest, artifact.UpstreamInfo["DownloadUrl"].(string))
case UpstreamTypeLocal:
fmt.Println("Nothing to do, artifact already installed")
default:
panic(artifact)
}
}
// Install the artifact
func (artifact *ArtifactInfo) Install(verbose bool, distributedFiles string) {
//Create missing directories
if verbose {
fmt.Printf("Creating the directories (if missing) %s, %s\n",
artifact.DirectoryPath(distributedFiles),
filepath.Join(distributedFiles, "all"),
)
}
err := os.MkdirAll(artifact.DirectoryPath(distributedFiles), 0755)
if err != nil {
panic(err)
}
err = os.MkdirAll(filepath.Join(distributedFiles, "all"), 0755)
if err != nil {
panic(err)
}
//Download the file in the right location
if verbose {
fmt.Printf("Downloading the artifact %s to %s\n", artifact.Filename, artifact.FilePath(distributedFiles))
}
artifact.Download(verbose, artifact.FilePath(distributedFiles))
//Create a link to all
if verbose {
fmt.Printf("Linking the artifact to %s\n", filepath.Join(distributedFiles, "all", artifact.Filename))
}
err = os.Link(artifact.FilePath(distributedFiles), filepath.Join(distributedFiles, "all", artifact.Filename))
if err != nil {
panic(err)
}
if strings.HasSuffix(artifact.Filename, ".rpm") {
//Launch the createrepo command
if verbose {
fmt.Printf("Executing createrepo %s\n", artifact.DirectoryPath(distributedFiles))
}
cmd := exec.Command("createrepo", artifact.DirectoryPath(distributedFiles))
if verbose {
cmd.Stdout = os.Stdout
}
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Printf("Error running createrepo: %s\n", err.Error())
}
}
artifact.Installed = true
}
// Uninstall the artifact
func (artifact *ArtifactInfo) Uninstall(verbose bool, distributedFiles string) {
if verbose {
fmt.Printf("Removing the file %s\n", filepath.Join(distributedFiles, "all", artifact.Filename))
}
if err := os.Remove(filepath.Join(distributedFiles, "all", artifact.Filename)); err != nil {
panic(err)
}
if _, errStat := os.Stat(artifact.FilePath(distributedFiles)); errStat == nil {
if verbose {
fmt.Printf("Removing the file %s\n", artifact.FilePath(distributedFiles))
}
if err := os.Remove(artifact.FilePath(distributedFiles)); err != nil {
panic(err)
}
if strings.HasSuffix(artifact.Filename, ".rpm") {
if verbose {
fmt.Printf("Executing createrepo %s\n", artifact.DirectoryPath(distributedFiles))
}
cmd := exec.Command("createrepo", artifact.DirectoryPath(distributedFiles))
if verbose {
cmd.Stdout = os.Stdout
}
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic(err)
}
}
artifact.Installed = false
}
}