Commits

Ville Suoranta authored c354caa2b8e Merge
Merge branch 'master' into CAS-13857
No tags

bambooscripts/agentcleanup_newcli.groovy

Added
1 +import groovy.json.*
2 +import groovy.xml.MarkupBuilder
3 +import groovy.cli.commons.CliBuilder
4 +
5 +def cli = new CliBuilder(usage: 'groovy agentcleanup.groovy [--delete] [--all] [--workdir <value>]')
6 +cli.h(longOpt: 'help', 'Show help')
7 +cli.w(args:1, argName:'w', longOpt: 'workdir', 'Working directory')
8 +cli.d(argName:'d', longOpt: 'delete', 'Set the option to true when you want to enable deletion (true). By default only report the directories')
9 +cli.a(argName:'a', longOpt: 'all', 'Delete working directories even if there is an existing Bamboo branch')
10 +
11 +def options = cli.parse(args)
12 +assert options
13 +if (!options || options.h || !options.w) {
14 + cli.usage();
15 + return // Exit script
16 +}
17 +def workingDirectory = options.w
18 +def deleteflag = options.d
19 +def all = options.a
20 +
21 +println "All flag: " + all
22 +println "Delete flag: " + deleteflag
23 +
24 +println "Current working directory: " + workingDirectory
25 +
26 +def env = System.getenv()
27 +ArrayList envarray = env.collect { k, v -> "$k=$v" }
28 +String user = env['DOCUSER']
29 +String password = env['PASSWD']
30 +String userpass = user + ":" + password;
31 +String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
32 +
33 +String branchUrl = "https://open-bamboo.nrao.edu/rest/api/latest/plan.json?expand=plans.plan.branches.branch&max-result=100000"
34 +
35 +//System.setProperty("deployment.security.TLSv1.2", "true");
36 +System.setProperty("https.protocols", "TLSv1.2");
37 +
38 +URLConnection connection = branchUrl.toURL().openConnection();
39 +connection.setRequestProperty("Authorization", basicAuth);
40 +InputStream inputStream = connection.getInputStream();
41 +def result = new groovy.json.JsonSlurper().parseText(inputStream.text);
42 +connection.disconnect();
43 +
44 +ArrayList<String> activeplans = new ArrayList<String>()
45 +ArrayList<String> activebranches = new ArrayList<String>()
46 +
47 +println "Collecting plan and branch information"
48 +for (def plan : result.plans.plan) {
49 + println plan.planKey.key
50 + activeplans.add(plan.planKey.key)
51 + for (def branch: plan.branches.branch) {
52 + println branch.key
53 + activebranches.add(branch.key)
54 + }
55 +}
56 +
57 +// Sanity check to make sure we got reasonable looking set of branches and plans
58 +if (activebranches.size() <6 || activeplans.size()<6) {
59 + println "Found fewer than 5 active branches or plans. Won't continue cleanup."
60 + return // Exit script
61 +}
62 +
63 +println "Getting current working directories"
64 +Set<String> directories = new TreeSet<String>()
65 +File dir = new File(workingDirectory)
66 +dir.eachFile { f ->
67 + if (f.isDirectory()) {
68 + println f.getName()
69 + directories.add(f.getName())
70 + }
71 +}
72 +
73 + Set<String> directoriesToKeep = new TreeSet<String>()
74 + if (!all) {
75 + println "Resolving directories to keep"
76 + for ( def candidate: directories) {
77 + for (def activeBranch: activebranches) {
78 + if (candidate.contains(activeBranch + "-") && ! activeBranch.startsWith("CASA-PPRT")) {
79 + println "Found a matching branch " + activeBranch +" for " + candidate
80 + directoriesToKeep.add(candidate)
81 + }
82 + else {
83 + for (def activePlan: activeplans) {
84 + if (candidate.contains(activePlan + "-")) {
85 + println "Found a matching plan " + activePlan +" for " + candidate
86 + directoriesToKeep.add(candidate)
87 + }
88 + }
89 + }
90 + }
91 + }
92 +}
93 +// Always keep "repositoryData" directory
94 +directoriesToKeep.add("repositoryData")
95 +// Keep ccache
96 +directoriesToKeep.add("ccache")
97 +
98 +
99 +// Figure out directories to delete
100 +Set<File> directoriesToDelete = new TreeSet<File>()
101 +
102 +for (def candidate: directories) {
103 + def match = false
104 + for (def keeper: directoriesToKeep) {
105 + println "Comparing " + candidate + " to " + keeper
106 + if (keeper.matches(candidate)) {
107 + match = true
108 + }
109 + }
110 + if (!match) {
111 + directoriesToDelete.add(new File (workingDirectory + "/" + candidate))
112 + }
113 +}
114 +println "Plans: " + activeplans
115 +println "Branches: " + activebranches
116 +println "Directories: " + directories
117 +println "Keeping these directories: " + directoriesToKeep
118 +println "Deleting these directories" + directoriesToDelete
119 +println "Directory count " + directories.size()
120 +println "Kept Directory count " + directoriesToKeep.size()
121 +println "Directories to delete count " + directoriesToDelete.size()
122 +
123 +if (directoriesToDelete.size()>0) {
124 + if (deleteflag) {
125 + // Actually delete directories
126 + println "Deleting directories"
127 + for (File f : directoriesToDelete) {
128 + println "Deleting " + f
129 + //f.deleteDir()
130 + String cmd = "rm -rf " + f
131 + def outputStream = new StringBuffer();
132 + def proc = cmd.execute(envarray, new File(workingDirectory))
133 + proc.waitForProcessOutput(System.out, System.err)
134 + println outputStream.toString()
135 + println "Deleted " + f
136 + }
137 + }
138 + else {
139 + println "Delete flag not set. Only reporting results."
140 + }
141 +}

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut