Created
May 9, 2018 14:25
-
-
Save evidex/520d7a096929bdda1779a51e380819be to your computer and use it in GitHub Desktop.
Lockable Resources From JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jenkins.plugins.lockableresources.* | |
import groovy.json.JsonSlurperClassic | |
def LOCK_FILE_PATH = 'locks/locks.json' | |
def manager = GlobalConfiguration.all().get(org.jenkins.plugins.lockableresources.LockableResourcesManager.class) | |
println "Found \${manager.resources.size()} locks" | |
println "Reading lock json file" | |
def locks = new JsonSlurperClassic().parseText(readFile(file: LOCK_FILE_PATH)) | |
println "Found \${locks.size()} in JSON" | |
println "Looking for locks to create" | |
locks.each { lock -> | |
exists = manager.fromName(lock.name) | |
if (exists == null) { | |
println "Creating lock \${lock.name} as it doesn't exist in the configuration" | |
manager.getResources().add(new LockableResource(lock.name, lock.description, lock.labels, lock.reservedBy)) | |
} | |
} | |
println "Looking for locks to remove" | |
def removeLocks = [] | |
manager.resources.each { lock -> | |
if (!(lock.name in locks.collect {it.name})) { | |
println "Lock [\${lock.name}] not found in manifest, adding to remove list" | |
removeLocks.add(lock) | |
} | |
} | |
println "Removing \${removeLocks.size()} locks" | |
removeLocks.each { lock -> | |
manager.resources.remove(lock) | |
} | |
println "Saving updated configuration" | |
manager.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment