oswaldlabs/chef-rundeck

View on GitHub
bin/chef-rundeck

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
#
# Copyright 2010, Opscode, Inc.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))

require 'rubygems'
require 'chef-rundeck'
require 'mixlib/cli'

class ChefRundeckCLI
  include Mixlib::CLI

   option :api_url,
     :short => "-a API_URL",
     :long => "--api-url API_URL",
     :description => "The base URL of the Chef REST API"

   option :client_key,
     :short => "-k KEYFILE",
     :long => "--key-file KEYFILE",
     :description => "The client.pem to sign requests"
 
   option :config_file,
      :short => "-c CONFIG",
      :long  => "--config CONFIG",
      :default => '/etc/chef/client.rb',
      :description => "The Chef configuration file to use, default /etc/chef/client.rb"

   option :username,
     :short => "-u USERNAME",
     :long => "--username USERNAME",
     :default => ENV['USER'],
     :description => "The Username for Rundeck to SSH as"

   option :web_ui_url,
     :short => "-w WEB_UI_URL",
     :long => "--web-ui-url WEB_UI_URL",
     :description => "The base URL of the Chef Web UI",
     :default => "https://manage.opscode.com"

   option :project_config,
     :short => "-f PROJECT_CONFIG",
     :long => "--project-config PROJECT_CONFIG",
     :description => "JSON project configuration file, default /etc/chef/rundeck.json",
     :default => "/etc/chef/rundeck.json"

   option :host,
     :short => "-o HOST",
     :long => "--host HOST",
     :description => "Listen on HOST (default: localhost)",
     :default => "localhost"

   option :port,
     :short => "-p PORT",
     :long => "--port PORT",
     :description => "The port to run on, default 9980",
     :default => 9980

   option :pidfile,
     :short => "-P FILE",
     :long => "--pidfile FILE",
     :description => "Prefix for our PID file, default: /var/run/chef-rundeck- ('PORT.pid' will be added)",
     :default => "/var/run/chef-rundeck"
   
   option :env,
     :short => "-e ENV",
     :long => "--env ENV",
     :description => "Sets the environment for chef-rundeck to execute under. Use 'development' for more logging.",
     :default => "production"

   option :cache_timeout,
     :short => "-t CACHE_TIMEOUT",
     :long => "--timeout CACHE_TIMEOUT",
     :description => "Sets the response timeout in seconds for the query data.",
     :default => "30"

   option :partial_search,
     :long => "--partial-search TRUE/FALSE",
     :description => "Enables partial searching, Chef 11 or greater.",
     :default => "false"
end

cli = ChefRundeckCLI.new
cli.parse_options

ChefRundeck.config_file = cli.config[:config_file]
ChefRundeck.username = cli.config[:username]
ChefRundeck.web_ui_url = cli.config[:web_ui_url]
ChefRundeck.api_url = cli.config[:api_url]
ChefRundeck.client_key = cli.config[:client_key]
ChefRundeck.project_config = cli.config[:project_config]
ChefRundeck.environment = cli.config[:env].to_sym
ChefRundeck.cache_timeout = cli.config[:cache_timeout].to_i
ChefRundeck.partial_search = cli.config[:partial_search] == "true"
ChefRundeck.configure

begin
   pid = "#{cli.config[:pidfile]}-#{cli.config[:port]}.pid"
   puts "Writing to #{pid}"
   File.open(pid, 'w'){ |f| f.write(Process.pid) }
   at_exit { File.delete(pid) if File.exist?(pid) }
rescue Exception => e
   puts "== Error writing pid file #{pid}!"
end

ChefRundeck.run! :bind => cli.config[:host], :port => cli.config[:port]