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.



127
128
129
130
131
132
133
134
135
# File 'lib/chef/knife/uploader_base.rb', line 127

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



145
146
147
148
149
# File 'lib/chef/knife/uploader_base.rb', line 145

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

#diff(a, b) ⇒ Object



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

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

#diff_color(a, b) ⇒ Object



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

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

#get_chef_repo_pathObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/chef/knife/uploader_base.rb', line 173

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



161
162
163
# File 'lib/chef/knife/uploader_base.rb', line 161

def get_knife_config_path
  locate_config_value(:config_file, :required)
end

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



151
152
153
154
155
156
157
158
159
# File 'lib/chef/knife/uploader_base.rb', line 151

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



165
166
167
168
169
170
171
# File 'lib/chef/knife/uploader_base.rb', line 165

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



226
227
228
229
230
231
232
233
# File 'lib/chef/knife/uploader_base.rb', line 226

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

#ridleyObject



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
220
221
222
223
224
# File 'lib/chef/knife/uploader_base.rb', line 195

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



235
236
237
238
239
240
241
# File 'lib/chef/knife/uploader_base.rb', line 235

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