<?xml version='1.0' encoding='UTF-8'?>
<matrix-project>
  <actions/>
  <description>&lt;p&gt;&#xd;
Dynamically Created Packaging Job for a packaging repo-compatible project.&#xd;
Intended to be triggered from within a project with the packaging repo&#xd;
bootstrapped.&#xd;
&lt;/p&gt;&#xd;
&lt;p&gt;&#xd;
Three parameters are expected:&#xd;
&lt;/p&gt;&#xd;
1) File: A build parameters file that describes information about the build state&#xd;
&lt;br&gt;&#xd;
2) File: A tar.gz of a git bundle, created by the packaging repo, of the&#xd;
current state of the project to be packaged&#xd;
&lt;/br&gt;&#xd;
&lt;br&gt;&#xd;
3) String: The project, e.g. facter, puppet&#xd;
&lt;/br&gt;&#xd;
&lt;p&gt;&#xd;
This job will be triggered automatically after generation by the packaging&#xd;
repo.&#xd;
&lt;/p&gt;&#xd;
&lt;br&gt;&#xd;
To trigger another downstream job after completion of this and the downstream repo creation job, pass DOWNSTREAM_JOB=$URI with the invocation, e.g.&#xd;
&lt;/br&gt;&#xd;
&lt;p&gt;&#xd;
pl:jenkins:uber_build DOWNSTREAM_JOB=http://jenkins-release.delivery.puppetlabs.net/job/puppetlabs-packaging-downstream-test/build&#xd;
&lt;/p&gt;&#xd;
&lt;p&gt;&#xd;
To get notified of the failure of this packaging job, pass NOTIFY= with the invocation, e.g.&#xd;
&lt;/p&gt;&#xd;
&lt;p&gt;&#xd;
pl:jenkins:uber_build NOTIFY=foo@puppetlabs.com&#xd;
&lt;/p&gt;&#xd;
&lt;p&gt;&#xd;
  Successful completion of packaging will result in the automatic creation of&#xd;
  apt/yum repositories on the distribution server with the built packages.  To&#xd;
  generate client apt/yum repository config files for accessing these packages,&#xd;
  check out the ref of the git sha that was packaged, and run `bundle exec rake&#xd;
  pl:jenkins:(deb|rpm)_repo_configs`. Configuration files will be generated and&#xd;
  deposited locally in pkg/repo_configs.&#xd;
&lt;/p&gt;&#xd;
&#xd;
&#xd;
  &#xd;
</description>
  <logRotator class="hudson.tasks.LogRotator">
    <daysToKeep>3</daysToKeep>
    <numToKeep>-1</numToKeep>
    <artifactDaysToKeep>-1</artifactDaysToKeep>
    <artifactNumToKeep>-1</artifactNumToKeep>
  </logRotator>
  <keepDependencies>false</keepDependencies>
  <properties>
    <jp.ikedam.jenkins.plugins.groovy__label__assignment.GroovyLabelAssignmentProperty plugin="groovy-label-assignment@1.0.0">
      <groovyScript>def labelMap = [
        <% if Pkg::Config.cows %>
          <% Pkg::Config.cows.split(' ').each do |cow| %>
            <% if cow =~ /cumulus/ %>
          &quot;pl_deb COW=<%= cow %>&quot;: &quot;cumulus&quot;,
            <% else %>
          &quot;pl_deb COW=<%= cow %>&quot;: &quot;deb&quot;,
            <% end %>
          <% end %>
        <% end %>
        <% if Pkg::Config.final_mocks %>
          <% Pkg::Config.final_mocks.split(' ').each do |mock| %>&quot;pl_mock MOCK=<%= mock %>&quot;: &quot;rpm&quot;,<% end %>
        <% end %>
        <% if Pkg::Config.build_tar then %>&quot;package_tar&quot;: &quot;tar&quot;,<% end %>
        <% if Pkg::Config.build_gem then %>&quot;package_gem&quot;: &quot;gem&quot;,<% end %>
        <% if Pkg::Config.build_dmg then %>&quot;package_apple&quot;: &quot;dmg&quot;,<% end %>
];
return labelMap.get(binding.getVariables().get(&quot;command&quot;));</groovyScript>
    </jp.ikedam.jenkins.plugins.groovy__label__assignment.GroovyLabelAssignmentProperty>
    <jenkins.plugins.hipchat.HipChatNotifier_-HipChatJobProperty plugin="hipchat-plugin@0.1.0">
      <room></room>
      <startNotification>false</startNotification>
    </jenkins.plugins.hipchat.HipChatNotifier_-HipChatJobProperty>
    <hudson.model.ParametersDefinitionProperty>
      <parameterDefinitions>
        <hudson.model.FileParameterDefinition>
          <name>BUILD_PROPERTIES</name>
          <description></description>
        </hudson.model.FileParameterDefinition>
        <hudson.model.FileParameterDefinition>
          <name>PROJECT_BUNDLE</name>
          <description></description>
        </hudson.model.FileParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>PROJECT</name>
          <description></description>
          <defaultValue></defaultValue>
        </hudson.model.StringParameterDefinition>
        <hudson.model.StringParameterDefinition>
          <name>METRICS</name>
          <description></description>
          <defaultValue></defaultValue>
        </hudson.model.StringParameterDefinition>
      </parameterDefinitions>
    </hudson.model.ParametersDefinitionProperty>
  </properties>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers class="vector"/>
  <concurrentBuild>false</concurrentBuild>
  <axes>
    <hudson.matrix.TextAxis>
      <name>command</name>
      <values>
        <% if Pkg::Config.cows %>
          <% Pkg::Config.cows.split(' ').each do |cow| %><string>pl_deb COW=<%= cow %></string><% end %>
        <% end %>
        <% if Pkg::Config.final_mocks %>
          <% Pkg::Config.final_mocks.split(' ').each do |mock| %><string>pl_mock MOCK=<%= mock %></string><% end %>
        <% end %>
        <% if Pkg::Config.build_tar then %><string>package_tar</string><% end %>
        <% if Pkg::Config.build_gem then %><string>package_gem</string><% end %>
        <% if Pkg::Config.build_dmg then %><string>package_apple</string><% end %>
      </values>
    </hudson.matrix.TextAxis>
  </axes>
  <builders>
    <hudson.tasks.Shell>
      <command>#!/bin/bash
set -e
### Create a local clone of the git-bundle that was passed
# The bundle is wrapped in a tarball. We don't need to know the project that is
# in the git bundle.

# Swap out _ for : in the command - lein cannot handle : in paths, and jenkins
# automatically makes directories with the names of the matrix cell (in this
# case $command) if https://github.com/technomancy/leiningen/issues/891 is ever
# resolved, this dirty hack can be removed.
command=$(echo $command | sed 's/_/:/1')

[ -f &quot;PROJECT_BUNDLE&quot; ] || exit 1
mkdir project &amp;&amp; tar -xzf PROJECT_BUNDLE -C project/

pushd project
  git clone --recursive $(ls) git_repo

  pushd git_repo

    ### Clone the packaging repo
    bundle install --path .bundle/gems --binstubs .bundle/bin --retry 3

    ### Perform the build
    bundle exec rake $command PARAMS_FILE=&quot;$WORKSPACE/BUILD_PROPERTIES&quot; --trace

    ### Send the results
    bundle exec rake pl:jenkins:ship[&quot;artifacts&quot;] PARAMS_FILE=&quot;$WORKSPACE/BUILD_PROPERTIES&quot; --trace

  popd
popd</command>
    </hudson.tasks.Shell>
  </builders>
  <publishers>
    <org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder plugin="groovy-postbuild@1.8">
      <groovyScript>
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.net.HttpURLConnection
import java.util.Date;

def get_jenkins_build_time() {
    start_time  = manager.build.getStartTimeInMillis()
    end_time    = new Date().getTime()
    return String.valueOf((end_time - start_time)/1000)
}

// Assemble metrics to post to build metrics server

app_server    = &quot;<%= Pkg::Config.metrics_url %>&quot;
task_metrics  = manager.build.getEnvironment(manager.listener)[&apos;METRICS&apos;]
charset       = &quot;UTF-8&quot;

// Maintain backwards compatibility
if ( task_metrics == null) {
    build_user  = &quot;N/A&quot;
    version     = &quot;N/A&quot;
    pe_version  = &quot;N/A&quot;
    build_team  = &quot;N/A&quot;
} else {
    build_user  = task_metrics.split(&quot;~&quot;)[0]
    version     = task_metrics.split(&quot;~&quot;)[1]
    pe_version  = task_metrics.split(&quot;~&quot;)[2]
    build_team  = task_metrics.split(&quot;~&quot;)[3]
}

matcher = manager.getLogMatcher(/(?:Finished building in:) ([\d]+\.?[\d]*)/)
if (matcher != null) {
  package_build_time = matcher[0][1]
} else {
  package_build_time = &quot;N/A&quot;
}

cmd_string  = manager.build.getEnvironment(manager.listener)[&apos;command&apos;]
if(cmd_string =~ /deb/) {
  package_type = 'deb'
} else if(cmd_string =~ /rpm|mock/) {
  package_type = 'rpm'
} else if(cmd_string =~ /gem/) {
  package_type = 'gem'
} else if(cmd_string =~ /apple/) {
  package_type = 'dmg'
} else if(cmd_string =~ /tar/) {
  package_type = 'tar'
}  else {
  package_type = 'N/A'
}

switch (package_type) {
  case 'deb':
    dist = cmd_string.split('-')[1]
    break
  case 'rpm':
    if(pe_version != 'N/A') {
      dist = cmd_string.split('-')[2]
    } else {
      dist = cmd_string.split('-')[1] + cmd_string.split('-')[2]
    }
    break
  case 'gem':
    dist = 'gem'
    break
  case 'dmg':
    dist = 'apple'
    break
  case 'tar':
    dist = 'tar'
    break
  default:
    dist = 'N/A'
}

jenkins_build_time  = get_jenkins_build_time()
package_name        = manager.build.getEnvironment(manager.listener)[&apos;PROJECT&apos;]
build_loc           = manager.build.getEnvironment(manager.listener)[&apos;NODE_NAME&apos;]
build_log           = &quot;${manager.build.getEnvironment(manager.listener)[&apos;BUILD_URL&apos;]}&quot; + &quot;consoleText&quot;
success             = String.valueOf(manager.build.result)

String query = String.format(&quot;package_name=%s&amp;dist=%s&amp;package_type=%s&amp;build_user=%s&amp;build_team=%s&amp;build_loc=%s&amp;version=%s&amp;pe_version=%s&amp;success=%s&amp;build_log=%s&amp;jenkins_build_time=%s&amp;package_build_time=%s&quot;,
     URLEncoder.encode(package_name, charset),
     URLEncoder.encode(dist, charset),
     URLEncoder.encode(package_type, charset),
     URLEncoder.encode(build_user, charset),
     URLEncoder.encode(build_team, charset),
     URLEncoder.encode(build_loc, charset),
     URLEncoder.encode(version, charset),
     URLEncoder.encode(pe_version, charset),
     URLEncoder.encode(success, charset),
     URLEncoder.encode(build_log, charset),
     URLEncoder.encode(jenkins_build_time, charset),
     URLEncoder.encode(package_build_time, charset))

// Make sure the server is listening before attempting to post data

URLConnection connection = null
serverAlive = false
try {
    URL u       = new URL(app_server);
    connection  = (HttpURLConnection) u.openConnection();
    connection.setRequestMethod(&quot;GET&quot;);
    int code    = connection.getResponseCode();
    serverAlive = true
    connection.disconnect();

} catch (MalformedURLException e) {
    serverAlive = false
    e.printStackTrace()

} catch (IOException e) {
    serverAlive = false
    e.printStackTrace()

} finally {
    if (serverAlive == true) {
        connection = new URL(app_server).openConnection()
        connection.setDoOutput(true) // Triggers POST.
        connection.setRequestProperty(&quot;Accept-Charset&quot;, charset);
        connection.setRequestProperty(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded;charset=&quot; + charset);
        OutputStream output = null;

        try {
             output = connection.getOutputStream()
             output.write(query.getBytes(charset))
             InputStream response = connection.getInputStream()
        } finally {
             if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
        }
    }
}     </groovyScript>
      <behavior>0</behavior>
    </org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder>
    <hudson.tasks.ArtifactArchiver>
      <artifacts>**/PROJECT_BUNDLE, **/BUILD_PROPERTIES</artifacts>
      <latestOnly>false</latestOnly>
      <allowEmptyArchive>false</allowEmptyArchive>
    </hudson.tasks.ArtifactArchiver><% if Pkg::Config.notify %>
    <hudson.tasks.Mailer plugin="mailer@1.4">
      <recipients><%= "#{Pkg::Config.notify}" %></recipients>
      <dontNotifyEveryUnstableBuild>false</dontNotifyEveryUnstableBuild>
      <sendToIndividuals>false</sendToIndividuals>
    </hudson.tasks.Mailer><% end %>
    <hudson.plugins.parameterizedtrigger.BuildTrigger plugin="parameterized-trigger@2.16">
      <configs>
        <hudson.plugins.parameterizedtrigger.BuildTriggerConfig>
          <configs>
            <hudson.plugins.parameterizedtrigger.CurrentBuildParameters/>
          </configs>
          <% if Pkg::Config.build_msi
            downstream = "#{Pkg::Config.project}-msi-#{Pkg::Config.build_date}-#{Pkg::Config.short_ref}"
          else
            downstream = "#{Pkg::Config.project}-repo-#{Pkg::Config.build_date}-#{Pkg::Config.ref}"
          end -%>
          <projects><%= downstream %></projects>
          <condition>ALWAYS</condition>
          <triggerWithNoParameters>false</triggerWithNoParameters>
        </hudson.plugins.parameterizedtrigger.BuildTriggerConfig>
      </configs>
    </hudson.plugins.parameterizedtrigger.BuildTrigger>
  </publishers>
  <buildWrappers>
    <hudson.plugins.build__timeout.BuildTimeoutWrapper plugin="build-timeout@1.11">
      <timeoutMinutes>90</timeoutMinutes>
      <failBuild>false</failBuild>
      <writingDescription>false</writingDescription>
      <timeoutPercentage>0</timeoutPercentage>
      <timeoutType>absolute</timeoutType>
      <timeoutMinutesElasticDefault>3</timeoutMinutesElasticDefault>
    </hudson.plugins.build__timeout.BuildTimeoutWrapper>
    <hudson.plugins.ansicolor.AnsiColorBuildWrapper plugin="ansicolor@0.3.1">
      <colorMapName>xterm</colorMapName>
    </hudson.plugins.ansicolor.AnsiColorBuildWrapper>
    <hudson.plugins.timestamper.TimestamperBuildWrapper plugin="timestamper@1.8.4"/>
  </buildWrappers>
  <executionStrategy class="hudson.matrix.DefaultMatrixExecutionStrategyImpl">
    <runSequentially>false</runSequentially>
  </executionStrategy>
</matrix-project>