class Chef::Knife::EcKeyExport

Public Instance Methods

export(table, path) click to toggle source
# File lib/chef/knife/ec_key_export.rb, line 60
def export(table, path)
  data = db.select.from(table)
  File.open(path, 'w') { |file| file.write(data.all.to_json) }
end
export_keys(path) click to toggle source
# File lib/chef/knife/ec_key_export.rb, line 55
def export_keys(path)
  data = db.fetch('SELECT keys_by_name.*, orgs.name AS "org_name" FROM keys_by_name LEFT JOIN orgs ON keys_by_name.org_id=orgs.id')
  File.open(path, 'w') { |file| file.write(data.all.to_json) }
end
run() click to toggle source
# File lib/chef/knife/ec_key_export.rb, line 30
def run
  if config[:sql_user].nil? || config[:sql_password].nil?
    load_config_from_file!
  end

  # user_data_path defaults to key_dump.json to support
  # older knife-ec-backup exports
  user_data_path = @name_args[0] || "key_dump.json"
  key_data_path =  @name_args[1] || "key_table_dump.json"

  export(:users, user_data_path) unless config[:skip_users_table]

  begin
    export_keys(key_data_path) unless config[:skip_keys_table]
  rescue Sequel::DatabaseError => e
    if e.message =~ /^PG::UndefinedTable/
      ui.error "Keys table not found. The keys table only exists on Chef Server 12."
      ui.error "Chef Server 11 users should use the --skip-keys-table option to avoid this error."
      exit 1
    else
      raise
    end
  end
end