-
Notifications
You must be signed in to change notification settings - Fork 2
/
jplugins-app.go
291 lines (232 loc) · 8.07 KB
/
jplugins-app.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
package main
import (
"bufio"
"errors"
"fmt"
"jplugins/simplefile"
"os"
"path"
"strings"
core "jplugins/coremgt"
"jplugins/utils"
git "github.com/forj-oss/go-git"
"github.com/alecthomas/kingpin"
"github.com/forj-oss/forjj-modules/trace"
)
type jPluginsApp struct {
app *kingpin.Application
listInstalled cmdListInstalled
checkVersions cmdCheckVersions
initCmd cmdInit
installCmd cmdInstall
installedElements *core.Plugins
repository *core.Repository
jenkinsHome *core.JenkinsHome
}
const (
defaultFeaturesRepoName = "jenkins-install-inits"
defaultFeaturesRepoPath = ".jplugins/repo-cache/" + defaultFeaturesRepoName
defaultFeaturesRepoURL = "https://github.com/forj-oss/" + defaultFeaturesRepoName
defaultJenkinsHome = "/var/jenkins_home"
lockFileName = "jplugins.lock"
lockBakFileName = "jplugins.lock.bak"
featureFileName = "jplugins.lst"
preInstalledFileName = "jplugins-preinstalled.lst"
)
var build_branch, build_commit, build_date, build_tag string
func (a *jPluginsApp) init() {
gotrace.SetInfo()
a.app = kingpin.New("jplugins", "Jenkins plugins as Code management tool.")
a.setVersion()
a.listInstalled.cmd = a.app.Command("list-installed", "Display Jenkins plugins list of current Jenkins installation.")
a.listInstalled.jenkinsHomePath = a.listInstalled.cmd.Flag("jenkins-home", "Where Jenkins is installed.").Default(defaultJenkinsHome).String()
a.listInstalled.preInstalled = a.listInstalled.cmd.Flag("save-pre-installed", "To create the jplugins-preinstalled.lst instead displaying.").Bool()
a.checkVersions.init()
a.initCmd.init()
a.installCmd.cmd = a.app.Command("install", "Install plugins and groovies defined by the 'jplugins.lock'.")
a.installCmd.lockFile = a.installCmd.cmd.Flag("lock-file", "Full path to the lock file.").Default(lockFileName).String()
a.installCmd.featureRepoPath = a.installCmd.cmd.Flag("features-repo-path", "Path to a feature repository. "+
"By default, jplugins store the repo clone in jplugins cache directory.").Default(defaultFeaturesRepoPath).String()
a.installCmd.featureRepoURL = a.installCmd.cmd.Flag("features-repo-url", "URL to the feature repository. NOT IMPLEMENTED").Default(defaultFeaturesRepoURL).String()
a.installCmd.jenkinsHomePath = a.installCmd.cmd.Flag("jenkins-home", "Where Jenkins is installed.").Default(defaultJenkinsHome).String()
// Do not use default git wrapper logOut function.
git.SetLogFunc(func(msg string) {
gotrace.Trace(msg)
})
}
func (a *jPluginsApp) setJenkinsHome(jenkinsHomePath string) {
a.jenkinsHome = core.NewJenkinsHome(jenkinsHomePath)
}
func (a *jPluginsApp) writeLockFile(lockFileName string, lockData *core.PluginsStatus) (_ bool) {
err := lockData.WriteSimple(lockFileName)
if err != nil {
gotrace.Error("Unable to save the lockfile. %s", err)
return
}
gotrace.Info("%s written\n", lockFileName)
return true
}
func (a *jPluginsApp) readFeatures(featurePath, featureFile, featureURL string, lockData *core.PluginsStatus) (_ bool) {
gotrace.Trace("Loading constraints...")
if gotrace.IsDebugMode() {
fmt.Printf("Reading %s\n--------\n", featureFileName)
}
gotrace.Info("Reading %s", featureFileName)
fd, err := os.Open(featureFile)
if err != nil {
gotrace.Error("Unable to read '%s'. %s", featureFileName, err)
return
}
if featurePath != defaultFeaturesRepoPath {
lockData.SetLocal()
}
lockData.SetFeaturesPath(featurePath)
lockData.SetFeaturesRepoURL(featureURL)
bError := false
fileScan := bufio.NewScanner(fd)
for fileScan.Scan() {
line := strings.Trim(fileScan.Text(), " \n")
if gotrace.IsDebugMode() {
fmt.Printf("== %s ==\n", line)
}
lockData.CheckElementLine(line, func(ftype, name, version string) {
switch ftype {
case "feature":
if err = lockData.CheckFeature(name); err != nil {
gotrace.Error("%s", err)
bError = true
}
//case "groovy":
case "plugin":
if err := lockData.CheckPlugin(name, version, nil); err != nil {
gotrace.Error("%s", err)
bError = true
}
default:
gotrace.Warning("feature type '%s' is currently not supported. Ignored.", ftype)
return
}
})
}
if bError {
gotrace.Error("Errors detected. Exiting.")
return false
}
if gotrace.IsDebugMode() {
fmt.Println("--------")
}
gotrace.Trace("Identifying version from constraints...")
lockData.DefinePluginsVersion()
if !lockData.CheckMinDep() {
return
}
return true
}
// readFeaturesFromSimpleFormat will load a feature file and expand them to get a list of plugins/groovies/... (elements)
func (a *jPluginsApp) readFeaturesFromSimpleFormat(featurePath, featureFile, featureURL string) (elements *core.ElementsType, err error) {
if gotrace.IsDebugMode() {
fmt.Println("******** Loading features and build constraints ********")
}
elements = core.NewElementsType()
if featurePath != defaultFeaturesRepoPath {
elements.SetLocal()
}
elements.SetFeaturesPath(featurePath)
elements.SetFeaturesRepoURL(featureURL)
elements.SetRepository(a.repository)
feature := simplefile.NewSimpleFile(featureFile, 3)
bError := false
feature.Read(":", func(fields []string) (_ error) {
_, err := elements.Add(fields...)
if err != nil {
gotrace.Error("%s", err)
bError = true
}
return
})
if bError {
err = errors.New("Errors detected. Please review")
return
}
return
}
// checkJenkinsHome verify if the path given exist or not
func (a *jPluginsApp) checkJenkinsHome() (_ bool) {
if a.jenkinsHome == nil {
return
}
return a.jenkinsHome.IsValid()
}
// readFromJenkins read manifest of each plugins and store information in a.installedPlugins
func (a *jPluginsApp) readFromJenkins() (elements *core.ElementsType, _ error) {
if a.jenkinsHome == nil {
return
}
return a.jenkinsHome.GetPlugins()
}
// checkSimpleFormatFile simply verify if the file exist.
func (a *jPluginsApp) checkSimpleFormatFile(filepath, file string) (_ bool) {
return utils.CheckFile(filepath, file)
}
// readFromSimpleFormat read a simple description file for plugins or groovies.
func (a *jPluginsApp) readFromSimpleFormat(filepath, fileName string) (elements *core.ElementsType, _ error) {
file := path.Join(filepath, fileName)
elements = core.NewElementsType()
elements.AddSupport("plugin", "groovy")
elements.AddSupportContext("groovy", "noMoreContext", "true")
elements.SetRepository(a.repository)
err := elements.Read(file, 3)
if err != nil {
return nil, fmt.Errorf("Unable to open file simple file format'%s'. %s", file, err)
}
return
}
// printOutVersion display the list of plugins given
func (a *jPluginsApp) printOutVersion(plugins *core.ElementsType) (_ bool) {
if plugins == nil {
return
}
plugins.PrintOut(func(element core.Element) {
version, _ := element.GetVersion()
fmt.Printf("%s: %s\n", element.Name(), version)
})
fmt.Printf("\n%d plugin(s)\n", plugins.Length())
return true
}
// saveVersionAsPreInstalled store the list of plugins in the jenkinsHomePath
func (a *jPluginsApp) saveVersionAsPreInstalled(jenkinsHomePath string, plugins *core.ElementsType) (_ bool) {
if plugins == nil {
return
}
preInstalledFile := path.Join(jenkinsHomePath, preInstalledFileName)
piDescriptor, err := os.OpenFile(preInstalledFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
gotrace.Error("Unable to create '%s'. %s", preInstalledFile, err)
return
}
defer piDescriptor.Close()
plugins.PrintOut(func(element core.Element) {
version, _ := element.GetVersion()
fmt.Fprintf(piDescriptor, "plugin:%s:%s\n", element.Name(), version)
})
fmt.Printf("%d plugin(s) saved in '%s'\n", plugins.Length(), preInstalledFile)
return true
}
// setVersion define the current jplugins version.
func (a *jPluginsApp) setVersion() {
version := "jplugins"
if PRERELEASE {
version += " pre-release V" + VERSION
} else if build_tag == "false" {
version += " pre-version V" + VERSION
} else {
version += " V" + VERSION
}
if build_branch != "master" && build_branch != "HEAD" {
version += fmt.Sprintf(" branch %s", build_branch)
}
if build_tag == "false" {
version += fmt.Sprintf(" - %s - %s", build_date, build_commit)
}
a.app.Version(version).Author(AUTHOR)
}