Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding a chef-jenkins application

  • Loading branch information...
commit 2697929ecbd889c69af7da57b26b9d329439cabc 1 parent 3f1d743
Adam Jacob authored
25  bin/chef-jenkins
... ...
@@ -0,0 +1,25 @@
  1
+#!/usr/bin/env ruby
  2
+#
  3
+# Author:: Adam Jacob (<adam@opscode.com>)
  4
+# Copyright:: Copyright (c) 2011 Opscode, Inc.
  5
+# License:: Apache License, Version 2.0
  6
+#
  7
+# Licensed under the Apache License, Version 2.0 (the "License");
  8
+# you may not use this file except in compliance with the License.
  9
+# You may obtain a copy of the License at
  10
+# 
  11
+#     http://www.apache.org/licenses/LICENSE-2.0
  12
+# 
  13
+# Unless required by applicable law or agreed to in writing, software
  14
+# distributed under the License is distributed on an "AS IS" BASIS,
  15
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16
+# See the License for the specific language governing permissions and
  17
+# limitations under the License.
  18
+
  19
+require 'rubygems'
  20
+$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
  21
+require 'chef/application/jenkins'
  22
+
  23
+Chef::Application::Jenkins.new.run
  24
+
  25
+
145  lib/chef/application/jenkins.rb
... ...
@@ -0,0 +1,145 @@
  1
+#
  2
+# Author:: Adam Jacob (<adam@opscode.com)
  3
+# Copyright:: Copyright (c) 2011 Opscode, Inc.
  4
+# License:: Apache License, Version 2.0
  5
+#
  6
+# Licensed under the Apache License, Version 2.0 (the "License");
  7
+# you may not use this file except in compliance with the License.
  8
+# You may obtain a copy of the License at
  9
+#
  10
+#     http://www.apache.org/licenses/LICENSE-2.0
  11
+#
  12
+# Unless required by applicable law or agreed to in writing, software
  13
+# distributed under the License is distributed on an "AS IS" BASIS,
  14
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15
+# See the License for the specific language governing permissions and
  16
+# limitations under the License.
  17
+
  18
+require 'chef/application'
  19
+require 'chef/jenkins'
  20
+require 'mixlib/log'
  21
+
  22
+class Chef::Application::Jenkins < Chef::Application
  23
+
  24
+  banner "Usage: jenkins (sync|prop) (options)"
  25
+
  26
+  NO_COMMAND_GIVEN = "You need to pass either sync or prop as the first argument\n"
  27
+
  28
+  option :config_file,
  29
+    :short => "-c CONFIG",
  30
+    :long  => "--config CONFIG",
  31
+    :description => "The configuration file to use",
  32
+    :proc => lambda { |path| File.expand_path(path, Dir.pwd) }
  33
+
  34
+  option :env_to,
  35
+    :short        => "-t ENVIRONMENT",
  36
+    :long         => "--env-to ENVIRONMENT",
  37
+    :description  => "Set the Chef environment to sync/prop to"
  38
+
  39
+  option :env_from,
  40
+    :short        => "-f ENVIRONMENT",
  41
+    :long         => "--env-from ENVIRONMENT",
  42
+    :description  => "Set the Chef environment to prop from"
  43
+
  44
+  option :help,
  45
+    :short        => "-h",
  46
+    :long         => "--help",
  47
+    :description  => "Show this message",
  48
+    :on           => :tail,
  49
+    :boolean      => true
  50
+
  51
+  option :node_name,
  52
+    :short => "-u USER",
  53
+    :long => "--user USER",
  54
+    :description => "API Client Username"
  55
+
  56
+  option :client_key,
  57
+    :short => "-k KEY",
  58
+    :long => "--key KEY",
  59
+    :description => "API Client Key",
  60
+    :proc => lambda { |path| File.expand_path(path, Dir.pwd) }
  61
+
  62
+  option :chef_server_url,
  63
+    :short => "-s URL",
  64
+    :long => "--server-url URL",
  65
+    :description => "Chef Server URL"
  66
+
  67
+  option :version,
  68
+    :short        => "-v",
  69
+    :long         => "--version",
  70
+    :description  => "Show chef version",
  71
+    :boolean      => true,
  72
+    :proc         => lambda {|v| puts "chef-jenkins: #{::Chef::Jenkins::VERSION}"},
  73
+    :exit         => 0
  74
+
  75
+  # Run knife
  76
+  def run
  77
+    Mixlib::Log::Formatter.show_time = false
  78
+    validate_and_parse_options
  79
+    quiet_traps
  80
+    jenkins = Chef::Jenkins.new
  81
+    if ARGV[0] == "sync"
  82
+      jenkins.sync
  83
+    elsif ARGV[0] == "prop"
  84
+      jenkins.prop
  85
+    end
  86
+
  87
+    exit 0
  88
+  end
  89
+
  90
+  private
  91
+
  92
+  def quiet_traps
  93
+    trap("TERM") do
  94
+      exit 1
  95
+    end
  96
+
  97
+    trap("INT") do
  98
+      exit 2
  99
+    end
  100
+  end
  101
+
  102
+  def validate_and_parse_options
  103
+    # Checking ARGV validity *before* parse_options because parse_options
  104
+    # mangles ARGV in some situations
  105
+    if no_command_given?
  106
+      print_help_and_exit(1, NO_COMMAND_GIVEN)
  107
+    elsif no_subcommand_given?
  108
+      if (want_help? || want_version?)
  109
+        print_help_and_exit
  110
+      else
  111
+        print_help_and_exit(2, NO_COMMAND_GIVEN)
  112
+      end
  113
+    end
  114
+  end
  115
+
  116
+  def no_subcommand_given?
  117
+    ARGV[0] =~ /^-/
  118
+  end
  119
+
  120
+  def no_command_given?
  121
+    ARGV.empty?
  122
+  end
  123
+
  124
+  def want_help?
  125
+    ARGV[0] =~ /^(--help|-h)$/
  126
+  end
  127
+
  128
+  def want_version?
  129
+    ARGV[0] =~ /^(--version|-v)$/
  130
+  end
  131
+
  132
+  def print_help_and_exit(exitcode=1, fatal_message=nil)
  133
+    Chef::Log.error(fatal_message) if fatal_message
  134
+
  135
+    begin
  136
+      self.parse_options
  137
+    rescue OptionParser::InvalidOption => e
  138
+      puts "#{e}\n"
  139
+    end
  140
+    puts self.opt_parser
  141
+    exit exitcode
  142
+  end
  143
+
  144
+end
  145
+
2  lib/chef/jenkins.rb
@@ -21,6 +21,8 @@
21 21
 
22 22
 class Chef
23 23
   class Jenkins
  24
+    VERSION = "0.1.0"
  25
+
24 26
     # if we have never run, upload everything, then store the ENV[GIT_COMMIT]
25 27
     # from jenkins
26 28
     #

0 notes on commit 2697929

Please sign in to comment.
Something went wrong with that request. Please try again.