/
BasePackager.groovy
194 lines (151 loc) · 5.64 KB
/
BasePackager.groovy
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
package org.pongasoft.glu.packaging.setup
import org.linkedin.glu.groovy.utils.io.GluGroovyIOUtils
import org.linkedin.glu.groovy.utils.shell.Shell
import org.linkedin.groovy.util.io.GroovyIOUtils
import org.linkedin.util.io.resource.Resource
import org.pongasoft.glu.provisioner.core.metamodel.MetaModel
import org.pongasoft.glu.provisioner.core.metamodel.StateMachineMetaModel
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* @author yan@pongasoft.com */
public abstract class BasePackager
{
public static final String MODULE = BasePackager.class.getName();
public static final Logger log = LoggerFactory.getLogger(MODULE);
public static final String CONFIG_TOKENS_KEY = 'configTokens'
public static final String PACKAGER_CONTEXT_KEY = 'packagerContext'
PackagerContext packagerContext
Resource outputFolder
Resource inputPackage
Collection<Resource> configsRoots
boolean dryMode = false
protected def _sha1s = [:].withDefault { Resource r -> shell.sha1(r) }
/**
* Make sure the package name ends with the proper version
*
* @return the package name (without the version)
*/
String ensureVersion(String version)
{
String fullPackageName = inputPackage.filename
if(!fullPackageName.endsWith("-${version}"))
throw new IllegalArgumentException("input package [${inputPackage.filename}] mismatch version [${version}]")
fullPackageName - "-${version}"
}
abstract Map<MetaModel, PackagedArtifact> createPackages()
Shell getShell()
{
packagerContext.shell
}
Resource copyInputPackage(Resource destination)
{
Resource parent = shell.mkdirs(destination.parentResource)
// make sure do not exist...
shell.delete(destination)
shell.delete(parent.createRelative(inputPackage.filename))
Resource location = shell.cp(inputPackage, parent)
if(location != destination)
shell.mv(location, destination)
resolveLinks(destination)
return destination
}
void processConfigs(String fromFolder, Map tokens, Resource toFolder)
{
configsRoots.each { configsRoot ->
processConfigs(configsRoot.createRelative(fromFolder), tokens, toFolder)
}
}
void resolveLinks(Resource destination)
{
def out = []
GroovyIOUtils.eachChildRecurse(inputPackage) { Resource file ->
if(file.filename.endsWith('.jar.lnk'))
resolveLink(destination, file, out)
}
if(out)
{
// optimization => cp forks and makes the process slow => forking once...
def content = """#!/bin/bash
${out.join('\n')}
"""
shell.withTempFile { Resource f ->
shell.saveContent(f, content)
shell.exec(f)
}
}
}
void resolveLink(Resource destination, Resource link, def out)
{
Resource linkFolder = link.parentResource
def (linkSourceRelativePath, sha1) = link.file.readLines()
def linkSource = linkFolder.createRelative(linkSourceRelativePath)
if(sha1 && sha1 != _sha1s[linkSource])
throw new IllegalArgumentException("mismatch sha1 for ${linkSource} (expecting ${sha1}, got ${_sha1s[linkSource]})")
String destinationPath =
inputPackage.file.toPath().relativize(linkFolder.file.toPath()).toString()
//shell.cp(linkSource, destination.createRelative(destinationPath))
out << "cp -R ${linkSource.file.canonicalPath} ${destination.createRelative(destinationPath).file.canonicalPath}"
//shell.rm(destination.createRelative(destinationPath).createRelative(link.filename))
out << "rm ${destination.createRelative(destinationPath).createRelative(link.filename).file.canonicalPath}"
}
void processConfigs(Resource fromFolder, Map tokens, Resource toFolder)
{
if(fromFolder?.exists())
{
GroovyIOUtils.eachChildRecurse(fromFolder.chroot('.')) { Resource templateOrFile ->
if(!templateOrFile.isDirectory())
{
Resource to =
(toFolder ?: outputFolder).createRelative(templateOrFile.path)
if(to.path.contains('@'))
to = shell.toResource(shell.replaceTokens(to.path, tokens))
switch(GluGroovyIOUtils.getFileExtension(templateOrFile))
{
case 'gtmpl':
case 'xtmpl':
case 'ctmpl':
to = to.parentResource.createRelative(to.filename[0..-7])
log.debug("processing config templateOrFile: ${templateOrFile}")
// process templateOrFile (token replacement)
shell.processTemplate(templateOrFile, to, tokens)
break
default:
// not a templateOrFile => simply copy
log.debug("copying config file: ${templateOrFile}")
shell.cp(templateOrFile, to)
break
}
}
}
}
}
protected Resource generateStateMachineJarFile(StateMachineMetaModel stateMachineMetaModel,
Resource toFolder)
{
shell.withTempFile { Resource r ->
def lines = []
stateMachineMetaModel.defaultTransitions.transitions.each { state, transitions ->
transitions = transitions
.collect { transition -> "[to: '${transition.to}', action: '${transition.action}']"}
.join(', ')
lines << " '${state}': [${transitions}]"
}
def content = """
defaultTransitions =
[
${lines.join(',\n')}
]
defaultEntryState = '${stateMachineMetaModel.defaultEntryState}'
"""
shell.saveContent(r.createRelative('glu/DefaultStateMachine.groovy'), content)
shell.mkdirs(toFolder)
Resource jarFile = toFolder.createRelative('glu-state-machine.jar')
shell.ant { ant ->
ant.jar(destfile: jarFile.file,
basedir: r.file)
}
return jarFile
}
}
}