Fix job templates and grain

Change-Id: I50f3cfc78a57d9f7277954247e57c32434a502d1
diff --git a/_grains/jenkins.py b/_grains/jenkins.py
deleted file mode 100644
index b8f85d7..0000000
--- a/_grains/jenkins.py
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env python
-
-import json
-
-def main():
-    output = { "jenkins_plugins" : {} }
-
-    list_plugin_groovy = """\
-        pluginList = []
-        Jenkins.instance.pluginManager.plugins.each{ pluginList << ("'${it.shortName}@${it.version}'")}
-        print pluginList
-    """
-    call_result = __salt__['jenkins_common.call_groovy_script'](list_plugin_groovy, [])
-
-    plugins = json.loads(call_result)
-
-    for plugin in plugins:
-        plugin_fields = plugin.split('@')
-        output["jenkins_plugins"][plugin_fields[0]] = {"version": plugin_fields[1]}
-
-    if output:
-        return output
-    else:
-        return None
diff --git a/_grains/jenkins_plugins.py b/_grains/jenkins_plugins.py
new file mode 100644
index 0000000..c13cb8b
--- /dev/null
+++ b/_grains/jenkins_plugins.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+
+import jenkins
+import salt.config
+
+def main():
+    output = { "jenkins_plugins" : {} }
+    opts = salt.config.minion_config('/etc/salt/minion')
+    user = opts['jenkins']['user']
+    password = opts['jenkins']['password']
+    url = opts['jenkins']['url']
+
+    server = jenkins.Jenkins(url, username=user, password=password)
+    plugins = server.get_plugins(depth=1)
+    for plugin_name, plugin_dict in plugins.iteritems():
+        output["jenkins_plugins"][plugin_name[0]] = {"version" : (plugin_dict["backupVersion"] or 0)}
+    return output
diff --git a/jenkins/files/jobs/_common.xml b/jenkins/files/jobs/_common.xml
index 5922fc1..e62d08b 100644
--- a/jenkins/files/jobs/_common.xml
+++ b/jenkins/files/jobs/_common.xml
@@ -12,7 +12,7 @@
     <org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty/>
     {%- endif %}
     {%- if job.get('scm', {}).github is defined %}
-    <com.coravy.hudson.plugins.github.GithubProjectProperty plugin="github@{% salt['grains.get']('jenkins_plugins:github:version') %}">
+    <com.coravy.hudson.plugins.github.GithubProjectProperty plugin="github@{{ salt['grains.get']('jenkins_plugins:github:version', '1.21.1') }}">
       <projectUrl>{{ job.scm.github.url }}</projectUrl>
       {%- if job.scm.github.name is defined %}
       <displayName>{{ job.scm.github.name }}</displayName>
@@ -37,7 +37,7 @@
           </threshold>
         </jenkins.triggers.ReverseBuildTrigger>
         {%- elif type == 'github' %}
-        <com.cloudbees.jenkins.GitHubPushTrigger plugin="github@{% salt['grains.get']('jenkins_plugins:github:version') %}">
+        <com.cloudbees.jenkins.GitHubPushTrigger plugin="github@{{ salt['grains.get']('jenkins_plugins:github:version', '1.21.1') }}">
           <spec/>
         </com.cloudbees.jenkins.GitHubPushTrigger>
         {%- elif type == 'pollscm' %}
@@ -46,8 +46,8 @@
           <ignorePostCommitHooks>false</ignorePostCommitHooks>
         </hudson.triggers.SCMTrigger>
         {%- elif type == 'gerrit' %}
-        <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger plugin="gerrit-trigger@{% salt['grains.get']('jenkins_plugins:gerrit-trigger:version') %}">
-          <spec></spec>
+        <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger plugin="gerrit-trigger@{{ salt['grains.get']('jenkins_plugins:gerrit-trigger:version', '2.23.0') }}">
+          <spec/>
           <gerritProjects>
             {%- for pname, project in trigger.project.iteritems() %}
             <com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.GerritProject>
@@ -76,7 +76,7 @@
             <onNotBuilt>{%- if "not_built" in trigger.get('skip_vote',[]) %}true{%- else %}false{%- endif %}</onNotBuilt>
           </skipVote>
           <silentMode>{{ trigger.get('silent_mode', False)|lower }}</silentMode>
-          <notificationLevel></notificationLevel>
+          <notificationLevel/>
           <silentStartMode>{{ trigger.get('silent_start_mode', False)|lower }}</silentStartMode>
           <escapeQuotes>true</escapeQuotes>
           <nameAndEmailParameterMode>PLAIN</nameAndEmailParameterMode>
@@ -159,12 +159,16 @@
             {%- endfor %}
           </triggerOnEvents>
           <dynamicTriggerConfiguration>false</dynamicTriggerConfiguration>
-          <triggerConfigURL></triggerConfigURL>
+          <triggerConfigURL/>
           <triggerInformationAction/>
         </com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger>
         {%- elif type == 'timer' and trigger.spec != '' %}
         <hudson.triggers.TimerTrigger>
+          {%- if trigger.spec is defined and trigger.spec != '' %}
           <spec>{{ trigger.spec }}</spec>
+          {%- else %}
+          <spec/>
+          {%- endif %}
         </hudson.triggers.TimerTrigger>
         {%- endif %}
         {%- endif %}
diff --git a/jenkins/files/jobs/_parameters.xml b/jenkins/files/jobs/_parameters.xml
index b42b19b..0f63ad5 100755
--- a/jenkins/files/jobs/_parameters.xml
+++ b/jenkins/files/jobs/_parameters.xml
@@ -5,7 +5,7 @@
         <hudson.model.{{ param.get('type', 'string')|capitalize }}ParameterDefinition>
           <name>{{ param_name }}</name>
           {%- if param.description is defined %}
-          <description>{{ description }}</description>
+          <description>{{ param.description }}</description>
           {%- else %}
           <description/>
           {%- endif %}
diff --git a/jenkins/files/jobs/workflow-scm.xml b/jenkins/files/jobs/workflow-scm.xml
index dba25d1..2aca88c 100644
--- a/jenkins/files/jobs/workflow-scm.xml
+++ b/jenkins/files/jobs/workflow-scm.xml
@@ -2,11 +2,11 @@
 {%- if job is not defined -%}
   {%- set job = salt['pillar.get']('jenkins:client:job:'+job_name) -%}
 {%- endif -%}
-<?xml version="1.0" encoding='UTF-8'?><flow-definition plugin="workflow-job@{% salt['grains.get']('jenkins_plugins:workflow-job:version') %}">
+<?xml version="1.0" encoding="UTF-8"?><flow-definition plugin="workflow-job@{{ salt['grains.get']('jenkins_plugins:workflow-job:version', '2.6') }}">
   {%- include "jenkins/files/jobs/_common.xml" %}
-  <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin=workflow-cps@"{% salt['grains.get']('jenkins_plugins:workflow-cps:version') %}">
+  <definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="workflow-cps@{{ salt['grains.get']('jenkins_plugins:workflow-cps:version', '2.13') }}">
     {%- if job.scm.get('type', 'git') == 'git' %}
-    <scm class="hudson.plugins.git.GitSCM" plugin="git@{% salt['grains.get']('jenkins_plugins:git:version') %}">
+    <scm class="hudson.plugins.git.GitSCM" plugin="git@{{ salt['grains.get']('jenkins_plugins:git:version', '2.5.3') }}">
       <configVersion>2</configVersion>
       <userRemoteConfigs>
         <hudson.plugins.git.UserRemoteConfig>
diff --git a/jenkins/files/jobs/workflow.xml b/jenkins/files/jobs/workflow.xml
index 7400d39..561d6f2 100644
--- a/jenkins/files/jobs/workflow.xml
+++ b/jenkins/files/jobs/workflow.xml
@@ -14,9 +14,9 @@
 {%- endmacro -%}
 
 <?xml version='1.0' encoding='UTF-8'?>
-<flow-definition plugin="workflow-job@2.5">
+<flow-definition ="workflow-job@{{ salt['grains.get']('jenkins_plugins:workflow-job:version', '2.5') }}">
   {%- include "jenkins/files/jobs/_common.xml" %}
-  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@{% salt['grains.get']('jenkins_plugins:workflow-cps:version') %}">
+  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@{{ salt['grains.get']('jenkins_plugins:workflow-cps:version', '2.12') }}">
     <script>// libraries
 {%- if job.libs is defined %}
 {%- for lib in job.libs %}