Class: KnifeUploader::BaseCommand

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/uploader_base.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ BaseCommand

Returns a new instance of BaseCommand.



122
123
124
125
126
127
128
129
130
# File 'lib/chef/knife/uploader_base.rb', line 122

def initialize(args)
  super
  @pattern = locate_config_value(:pattern)
  if @pattern
    @pattern = Regexp.new(@pattern)
  else
    @pattern = //  # matches anything
  end
end

Instance Method Details

#debug(msg) ⇒ Object



140
141
142
143
144
# File 'lib/chef/knife/uploader_base.rb', line 140

def debug(msg)
  if locate_config_value(:debug)
    ui.info("DEBUG: #{msg}")
  end
end

#diff(a, b) ⇒ Object



132
133
134
# File 'lib/chef/knife/uploader_base.rb', line 132

def diff(a, b)
  ::Diffy::Diff.new(a, b, :context => 2)
end

#diff_color(a, b) ⇒ Object



136
137
138
# File 'lib/chef/knife/uploader_base.rb', line 136

def diff_color(a, b)
  diff(a, b).to_s(ui.color? ? :color : :text)
end

#get_chef_repo_pathObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/chef/knife/uploader_base.rb', line 168

def get_chef_repo_path
  unless @chef_repo_path
    path_list = parsed_knife_config.get_cookbook_path_list
    path_list.each do |cookbooks_path|
      [cookbooks_path, File.expand_path('..', cookbooks_path)].each do |path|
        if ['.git', 'data_bags', 'environments', 'roles'].map do |subdir_name|
          File.directory?(File.join(path, subdir_name))
        end.all?
          @chef_repo_path = path
        end
      end
    end

    raise "No chef repository checkout path could be determined using " +
          "cookbook paths #{path_list}" unless @chef_repo_path

    debug("Identified Chef repo path: #{@chef_repo_path}")
  end

  @chef_repo_path
end

#get_knife_config_pathObject



156
157
158
# File 'lib/chef/knife/uploader_base.rb', line 156

def get_knife_config_path
  locate_config_value(:config_file, :required)
end

#locate_config_value(key, kind = :optional) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/chef/knife/uploader_base.rb', line 146

def locate_config_value(key, kind = :optional)
  raise unless [:required, :optional].include?(kind)
  key = key.to_sym
  value = config[key] || Chef::Config[:knife][key]
  if kind == :required && value.nil?
    raise "#{key} not specified"
  end
  value
end

#parsed_knife_configObject



160
161
162
163
164
165
166
# File 'lib/chef/knife/uploader_base.rb', line 160

def parsed_knife_config
  unless @parsed_knife_config
    @parsed_knife_config = KnifeConfigParser.new(get_knife_config_path)
  end

  @parsed_knife_config
end

#report_errors(&block) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/chef/knife/uploader_base.rb', line 221

def report_errors(&block)
  begin
    yield
  rescue => exception
    ui.fatal("#{exception}: #{exception.backtrace.join("\n")}")
    raise exception
  end
end

#ridleyObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/chef/knife/uploader_base.rb', line 190

def ridley
  unless @ridley
    knife_conf_path = get_knife_config_path

    # Check file existence (Ridley will throw a confusing error).
    raise "File #{knife_conf_path} does not exist" unless File.file?(knife_conf_path)

    @ridley = Ridley.from_chef_config(knife_conf_path, :ssl => { :verify => false })
    data_bag_secret_file_path = @ridley.options[:encrypted_data_bag_secret]
    unless data_bag_secret_file_path
      raise "No encrypted data bag secret location specified in #{knife_conf_path}"
    end

    unless File.file?(data_bag_secret_file_path)
      raise "File #{data_bag_secret_file_path} does not exist"
    end

    # The encrypted data bag secret has to be the value, even though the readme in Ridley 1.5.2
    # says it can also be a file name, so we have to re-create the Ridley object.
    @ridley = Ridley.new(
      server_url: @ridley.server_url,
      client_name: @ridley.client_name,
      client_key: @ridley.client_key,
      encrypted_data_bag_secret: IO.read(data_bag_secret_file_path),
      ssl: { verify: false }
    )
  end

  @ridley
end

#runObject



230
231
232
233
234
235
236
# File 'lib/chef/knife/uploader_base.rb', line 230

def run
  begin
    run_internal
  ensure
    # Cleanup code can be added here.
  end
end