summaryrefslogtreecommitdiff
path: root/src/edu/stsci/DataConfig.groovy
blob: c9bf83bd5db58f11f17a6948750f861404a907e2 (plain) (blame)
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
package edu.stsci
import groovy.json.JsonOutput
import org.apache.commons.io.FileUtils

class DataConfig implements Serializable {
    String root = '.'
    String server_ident = ''
    String match_prefix = '(.*)'
    Boolean keep_data = false
    int keep_builds = 20
    int keep_days = 10
    def steps
    def server_conn
    String scan_path
    def data = [:]
    def buildInfo

    DataConfig(steps, connection) {
        this.steps = steps
        this.server_ident = server_ident
        this.server_conn = connection // steps.Artifactory.server(this.server_ident)
        this.buildInfo = this.server_conn.newBuildInfo()
        this.buildInfo.env.capture = true
        this.buildInfo.env.collect()
        // Construct absolute path to data
        this.scan_path = FilenameUtils.getFullPath(
                    "${this.steps.env.WORKSPACE}/${this.root}"
        )
    }

    def insert(String name, String block) {
        /* Store JSON directly as string */
        this.data[name] = block
    }

    def insert(String name, block=[:]) {
        /* Convert a Groovy Map to JSON and store it */
        this.data[name] = JsonOutput.toJson(block)
    }

    def populate() {
        // Record listing of all files starting at ${path}
        // (Native Java and Groovy approaches will not
        // work here)
        this.steps.sh(script: "find ${this.scan_path} -type f",
           returnStdout: true).trim().tokenize('\n').each {

            // Semi-wildcard matching of JSON input files
            // ex:
            //      it = "test_1234_result.json"
            //      artifact.match_prefix = "(.*)_result"
            //
            //      pattern becomes: (.*)_result(.*)\\.json
            if (it.matches(
                    this.match_prefix + '(.*)\\.json')) {
                def basename = FilenameUtils.getBaseName(it)
                def data = readFile(it)

                // Store JSON in a logical map
                // i.e. ["basename": [data]]
                this.insert(basename, data)
            }
        } // end find.each
    }

    def run() {
        this.populate()

        // Submit each request to the Artifactory server
        this.data.each { blob ->
            def spec = this.server_conn.upload(spec: blob.value)
            this.buildInfo.append(spec)
        }

        // Define retention scheme
        // Defaults: see DataConfig.groovy
        this.buildInfo.retention \
            maxBuilds: this.keep_builds, \
            maxDays: this.keep_days, \
            deleteBuildArtifacts: !this.keep_data

        this.server_conn.publishBuildInfo(this.buildInfo)

    } // end stage Artifactory
}