how to wait plugin installation completion from a groovy script?

527 views
Skip to first unread message

christoph...@inria.fr

unread,
Nov 8, 2016, 5:23:48 PM11/8/16
to Jenkins Developers
Hi,

I use a small groovy script to install automatically a set of plugins (including their dependencies):
def pluginParameter="ldap matrix-auth ssh-credentials"
def plugins = pluginParameter.split()
def instance = Jenkins.getInstance()
def pluginManager = instance.getPluginManager()
def updateCenter = instance.getUpdateCenter()
def installed = false

plugins.each {
  if (!pluginManager.getPlugin(it)) {
    def plugin = updateCenter.getPlugin(it)
    if (plugin) {
      println("Installing " + it)
      def installationJob = plugin.deploy()
      installed = true
    }
  }
}

instance.save()
if (installed)
  instance.doSafeRestart()

I want to wait that plugins are effectively installed before proceeding with next instruction of the script. Plugin installation is an async task. I tried to use plugin.deploy.get() as deploy() returns a Future but the script ends before all plugins and dependencies are effectively installed. Is there a way to wait until plugins and dependencies are effectively installed?

Regards,
Christophe

Kristian Thornley

unread,
Apr 24, 2017, 8:52:15 PM4/24/17
to Jenkins Developers
Hi Christophe

I hope this is not too late and perhaps it might help others.  The Futures job returned from the deploy operation contains isDone() operation you can use like so by registering in a map and checking

plugins.each {
logger.info("> checking " + it)
if (!pm.getPlugin(it)) {
logger.info("> looking updatecenter for " + it)
if (!initialized) {
uc.updateAllSites()
initialized = true
}
def plugin = uc.getPlugin(it)
if (plugin) {
logger.info("> installing " + it)
futures[it] = plugin.deploy()
installed = true;
}
}
}
def allInstalled = false
while(!allInstalled){
def countOfDone = 0
futures.each{ k, v ->
if(v.isDone()){
countOfDone++
}
}
if(futures.size() == countOfDone){
allInstalled = true;
}else {
logger.info("> count of done " + countOfDone)
sleep(1000)
}
}
inputFile.write("initialized")
logger.info("> plugins installed user created, initializing a restart!")

christoph...@inria.fr

unread,
Apr 25, 2017, 8:17:38 AM4/25/17
to Jenkins Developers
Hi Kristian,

Thank you for your reply. It is good to know.
I worked around the problem with the following script that could also be useful for other:
installed = [] as Set

updateCenter.updateAllSites()

def install(id) {
  plugin = updateCenter.getPlugin(id)

  plugin.dependencies.each {
    install(it.key)
  }

  if (!pluginManager.getPlugin(id) && !installed.contains(id)) {
    println("installing plugin ${id}...")
    pluginManager.install([id], false).each { it.get() }
    installed.add(id)
  }
}

plugins.each {
  install(it)
}
Reply all
Reply to author
Forward
0 new messages