Module: RidleyExec::Configuration

Defined in:
lib/ridley-exec/configuration.rb

Class Method Summary collapse

Class Method Details

.api_from_knife(path) ⇒ Object



85
86
87
88
89
# File 'lib/ridley-exec/configuration.rb', line 85

def self.api_from_knife(path)
  args = []
  args << path unless path.nil?
  Ridley.from_chef_config(*args)
end

.api_from_options(options) ⇒ Object



91
92
93
# File 'lib/ridley-exec/configuration.rb', line 91

def self.api_from_options(options)
  Ridley.new(options)
end

.env_default(name, default, opt = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/ridley-exec/configuration.rb', line 7

def self.env_default(name, default, opt={})
  val = ENV[name.to_s]
  return default if val.nil? || (val.size == 0 && opt[:no_blank])
  if block_given?
    val = yield val
  end

  val
end

.parse_args(args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ridley-exec/configuration.rb', line 18

def self.parse_args(args)
  options = {}
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: ridley-exec [options]"

    opts.separator ""
    opts.separator "Specific options:"

    options[:server_url] = env_default(:CHEF_SERVER_URL, nil, :no_blank => true)
    opts.on('-u [URL]', '--server-url [URL]', String, "The URL of the chef server to query") do |url|
      options[:server_url] = url
    end


    options[:client_name] = env_default(:CHEF_CLIENT_NAME, nil, :no_blank => true)
    opts.on('-n [CLIENT_NAME]', '--client-name [CLIENT_NAME]', String, "The name of the client to use for chef API operations.") do |client|
      options[:client_name] = client
    end


    options[:client_key] = env_default(:CHEF_CLIENT_KEY, nil, :no_blank => true)
    opts.on('-k [CLIENT_KEY]', '--client-key [CLIENT_KEY]', String, "The path to the client key file used for chef API operations.") do |key|
      options[:client_key] = key
    end

    options[:encrypted_data_bag_secret] = env_default(:CHEF_ENCRYPTED_DATA_BAG_SECRET, nil, :no_blank => true)
    opts.on('-s [SECRET]', '--encrypted-data-bag-secret [SECRET]', String, "The secret string for encrypting data bags.") do |secret|
      options[:encrypted_data_bag_secret] = secret
    end

    options[:knife_path] = env_default(:CHEF_KNIFE_PATH, nil)
    opts.on('-p [KNIFE_PATH]', '--knife-path [KNIFE_PATH]', String, "Path to the knife.rb file to use for config (empty string does search)") do |path|
      path = '' if path.nil?
      options[:knife_path] = path
    end

    opts.on('-e [SCRIPT_STRING]', String, 'Execute the ruby in SCRIPT_STRING') do |script|
      options[:target] = prepare_script_string(script)
    end

    opts.on('-I', 'Execute the ruby from STDIN.') do
      options[:target] = prepare_stdin_script
    end

    opts.on('--pry', 'Run pry REPL with api in context') do
      options[:target] = prepare_pry_session
    end

    opts.separator ""
    opts.separator "Common options:"
    opts.on_tail("-h", "--help", "Show this help message.") do
      puts opts
      exit 0
    end

    opts.on_tail("-v", "--version", "Print the version.") do
      puts RidleyExec::VERSION
      exit 0
    end
  end

  # Stops on first non-option param
  non_param = []
  args = opt_parser.order(args) {|p| non_param << p; opt_parser.terminate}
  [options, non_param + args]
end

.prepare_pry_sessionObject



114
115
116
117
118
119
# File 'lib/ridley-exec/configuration.rb', line 114

def self.prepare_pry_session
  Proc.new do |scope|
    require "pry"
    scope.pry
  end
end

.prepare_script(path) ⇒ Object



96
97
98
99
100
# File 'lib/ridley-exec/configuration.rb', line 96

def self.prepare_script(path)
  Proc.new do |scope|
    eval File.read(path), scope, path
  end
end

.prepare_script_string(script) ⇒ Object



108
109
110
111
112
# File 'lib/ridley-exec/configuration.rb', line 108

def self.prepare_script_string(script)
  Proc.new do |scope|
    eval script, scope, '-'
  end
end

.prepare_stdin_scriptObject



102
103
104
105
106
# File 'lib/ridley-exec/configuration.rb', line 102

def self.prepare_stdin_script
  Proc.new do |scope|
    eval STDIN.read, scope, '-stdin-'
  end
end

.resolve_args(args) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ridley-exec/configuration.rb', line 121

def self.resolve_args(args)
  options, remaining = parse_args(args)
  options = options.inject({}) do |a, kv|
    a[kv[0]] = kv[1] unless kv[1].nil?
    a
  end

  if options.has_key? :target
    target = options.delete(:target)
  else
    target = prepare_script(remaining.shift)
  end

  if options.has_key? :knife_path
    api = api_from_knife(options[:knife_path])
  else
    api = api_from_options(options)
  end

  [api, target, remaining]
end