Add files synchronization for user/base configuration
This commit is contained in:
parent
704cbb3ea2
commit
2912212142
|
|
@ -14,6 +14,10 @@ import (
|
||||||
k8s "sigs.k8s.io/controller-runtime/pkg/client"
|
k8s "sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
jobHashParameterName = "hash"
|
||||||
|
)
|
||||||
|
|
||||||
// Groovy defines API for groovy scripts execution via jenkins job
|
// Groovy defines API for groovy scripts execution via jenkins job
|
||||||
type Groovy struct {
|
type Groovy struct {
|
||||||
jenkinsClient jenkinsclient.Jenkins
|
jenkinsClient jenkinsclient.Jenkins
|
||||||
|
|
@ -36,7 +40,7 @@ func New(jenkinsClient jenkinsclient.Jenkins, k8sClient k8s.Client, logger logr.
|
||||||
|
|
||||||
// ConfigureGroovyJob configures jenkins job for executing groovy scripts
|
// ConfigureGroovyJob configures jenkins job for executing groovy scripts
|
||||||
func (g *Groovy) ConfigureGroovyJob() error {
|
func (g *Groovy) ConfigureGroovyJob() error {
|
||||||
_, err := g.jenkinsClient.CreateOrUpdateJob(fmt.Sprintf(configurationJobXMLFmt, g.scriptsPath, g.scriptsPath), g.jobName)
|
_, err := g.jenkinsClient.CreateOrUpdateJob(fmt.Sprintf(configurationJobXMLFmt, g.scriptsPath), g.jobName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -47,7 +51,8 @@ func (g *Groovy) ConfigureGroovyJob() error {
|
||||||
func (g *Groovy) EnsureGroovyJob(secretOrConfigMapData map[string]string, jenkins *virtuslabv1alpha1.Jenkins) (bool, error) {
|
func (g *Groovy) EnsureGroovyJob(secretOrConfigMapData map[string]string, jenkins *virtuslabv1alpha1.Jenkins) (bool, error) {
|
||||||
jobsClient := jobs.New(g.jenkinsClient, g.k8sClient, g.logger)
|
jobsClient := jobs.New(g.jenkinsClient, g.k8sClient, g.logger)
|
||||||
|
|
||||||
done, err := jobsClient.EnsureBuildJob(g.jobName, g.calculateHash(secretOrConfigMapData), map[string]string{}, jenkins, true)
|
hash := g.calculateHash(secretOrConfigMapData)
|
||||||
|
done, err := jobsClient.EnsureBuildJob(g.jobName, hash, map[string]string{jobHashParameterName: hash}, jenkins, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
@ -66,27 +71,69 @@ func (g *Groovy) calculateHash(secretOrConfigMapData map[string]string) string {
|
||||||
hash.Write([]byte(key))
|
hash.Write([]byte(key))
|
||||||
hash.Write([]byte(secretOrConfigMapData[key]))
|
hash.Write([]byte(secretOrConfigMapData[key]))
|
||||||
}
|
}
|
||||||
return base64.URLEncoding.EncodeToString(hash.Sum(nil))
|
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
const configurationJobXMLFmt = `<?xml version='1.1' encoding='UTF-8'?>
|
const configurationJobXMLFmt = `<?xml version='1.1' encoding='UTF-8'?>
|
||||||
<flow-definition plugin="workflow-job@2.25">
|
<flow-definition plugin="workflow-job@2.31">
|
||||||
<actions/>
|
<actions/>
|
||||||
<description></description>
|
<description></description>
|
||||||
<keepDependencies>false</keepDependencies>
|
<keepDependencies>false</keepDependencies>
|
||||||
<properties/>
|
<properties>
|
||||||
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.31">
|
<org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty/>
|
||||||
<script>import groovy.io.FileType
|
<hudson.model.ParametersDefinitionProperty>
|
||||||
|
<parameterDefinitions>
|
||||||
|
<hudson.model.StringParameterDefinition>
|
||||||
|
<name>` + jobHashParameterName + `</name>
|
||||||
|
<description></description>
|
||||||
|
<defaultValue></defaultValue>
|
||||||
|
<trim>false</trim>
|
||||||
|
</hudson.model.StringParameterDefinition>
|
||||||
|
</parameterDefinitions>
|
||||||
|
</hudson.model.ParametersDefinitionProperty>
|
||||||
|
</properties>
|
||||||
|
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.61">
|
||||||
|
<script>def scriptsPath = '%s'
|
||||||
|
def expectedHash = params.hash
|
||||||
|
|
||||||
node('master') {
|
node('master') {
|
||||||
def scriptsText = sh(script: 'ls %s', returnStdout: true).trim()
|
def scriptsText = sh(script: "ls ${scriptsPath} | sort", returnStdout: true).trim()
|
||||||
def scripts = []
|
def scripts = []
|
||||||
scripts.addAll(scriptsText.tokenize('\n'))
|
scripts.addAll(scriptsText.tokenize('\n'))
|
||||||
for(script in scripts) {
|
|
||||||
stage(script) {
|
stage('Synchronizing files') {
|
||||||
load "%s/${script}"
|
def complete = false
|
||||||
|
for(int i = 1; i <= 10; i++) {
|
||||||
|
def actualHash = calculateHash((String[])scripts, scriptsPath)
|
||||||
|
println "Expected hash '${expectedHash}', actual hash '${actualHash}'"
|
||||||
|
if(expectedHash == actualHash) {
|
||||||
|
complete = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
sleep 2
|
||||||
|
}
|
||||||
|
if(!complete) {
|
||||||
|
error("Timeout while synchronizing files")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(script in scripts) {
|
||||||
|
stage(script) {
|
||||||
|
load "${scriptsPath}/${script}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonCPS
|
||||||
|
def calculateHash(String[] scripts, String scriptsPath) {
|
||||||
|
def hash = java.security.MessageDigest.getInstance("SHA-256")
|
||||||
|
for(script in scripts) {
|
||||||
|
hash.update(script.getBytes())
|
||||||
|
def fileLocation = java.nio.file.Paths.get("${scriptsPath}/${script}")
|
||||||
|
def fileData = java.nio.file.Files.readAllBytes(fileLocation)
|
||||||
|
hash.update(fileData)
|
||||||
|
}
|
||||||
|
return Base64.getEncoder().encodeToString(hash.digest())
|
||||||
}</script>
|
}</script>
|
||||||
<sandbox>false</sandbox>
|
<sandbox>false</sandbox>
|
||||||
</definition>
|
</definition>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue