Properly check installed plugins in offline case

Check provided plugin from Plugin Manager (it may be already
installed) in case of unavailability of Update Center in offline mode.

Change-Id: If332f33b7fa6531d05cf88bcd46f25a29c255304
Related-bug: PROD-25079
diff --git a/jenkins/files/groovy/plugin.template b/jenkins/files/groovy/plugin.template
index 24591c4..1febba1 100644
--- a/jenkins/files/groovy/plugin.template
+++ b/jenkins/files/groovy/plugin.template
@@ -20,6 +20,7 @@
 
 ArrayList pluginsToInstall = []
 ArrayList pluginsToRemove = []
+ArrayList pluginsUnavailable = []
 
 LinkedHashMap allPluginDeps = [:]
 
@@ -47,11 +48,19 @@
 }
 
 // Return all dependency plugins for the specified one (recursiverly)
-LinkedHashMap getPluginDeps(String pluginName, String pluginVersion = null){
+LinkedHashMap getPluginDeps(String pluginName, String pluginVersion = null, boolean updateCenterAvailable){
   LinkedHashMap pluginDeps = [:]
-  def pluginToProbe = Jenkins.instance.updateCenter.getPlugin(pluginName)
-  pluginToProbe.dependencies.each { p, v ->
-    pluginDeps << getPluginDeps(p, v)
+  // if plugin is available from UC get deps from it, otherwise get deps from installed
+  if (updateCenterAvailable) {
+    def pluginToProbe = Jenkins.instance.updateCenter.getPlugin(pluginName)
+    pluginToProbe.dependencies.each { p, v ->
+      pluginDeps << getPluginDeps(p, v, true)
+    }
+  } else {
+    def pluginToProbe = Jenkins.instance.pluginManager.getPlugin(pluginName)
+    pluginToProbe.getDependencies().each { dep ->
+      pluginDeps << getPluginDeps(dep.shortName, dep.version, false)
+    }
   }
   // FIXME: need to get minimal version because Jenkins senses specified version as 'at least'
   pluginDeps[pluginName] = pluginVersion
@@ -79,6 +88,14 @@
   return removedPlugins
 }
 
+// this function is needed to detect plugins install/upgrade in Offline mode
+// and raise exception if plugins from Update Center is unavailable
+def detectPluginsUnavailability(ArrayList pluginsUnavailable) {
+  if (pluginsUnavailable) {
+    throw new NullPointerException("Next plugins ${pluginsUnavailable} are not available from Update Center, but required to install/upgrade.");
+  }
+}
+
 String pluginName
 def pluginInfo
 def pluginAvailable
@@ -104,7 +121,8 @@
   }
 
   pluginAvailable = uc.getPlugin(pluginName)
-  pluginInstalled = pluginAvailable.getInstalled()
+  pluginAvailableFromUC = pluginAvailable ? true : false
+  pluginInstalled = pluginAvailable ? pluginAvailable.getInstalled() : pm.getPlugin(pluginName)
   // If plugin installed
   if (pluginInstalled){
     // ... and pluginInfo contains version
@@ -112,7 +130,11 @@
       // ... and installed plugin version is lower than version from pluginInfo
       if (versionCmp(pluginInstalled.getVersion(), pluginInfo.get('version')) < 0){
         // upgrade plugin
-        pluginsToInstall << pluginAvailable
+        if (pluginAvailableFromUC) {
+          pluginsToInstall << pluginAvailable
+        } else {
+          pluginsUnavailable << pluginName
+        }
       }
     }
     // ... plugin is active and pluginInfo has enable=false
@@ -129,13 +151,18 @@
     }
   } else {
     // install plugin
-    pluginsToInstall << pluginAvailable
+    if (pluginAvailableFromUC) {
+      pluginsToInstall << pluginAvailable
+    } else {
+      pluginsUnavailable << pluginName
+    }
   }
 
   // Collect all plugin dependencies to decide about unwanted installed plugins
-  allPluginDeps << getPluginDeps(pluginName, pluginInfo ? pluginInfo.get('version') : null)
+  allPluginDeps << getPluginDeps(pluginName, pluginInfo ? pluginInfo.get('version') : null, pluginAvailableFromUC)
 }
 
+detectPluginsUnavailability(pluginsUnavailable)
 // Deploy plugins by list if any
 pluginsToInstall.each {
   it.deploy(false).get()