class Chef::TidyCommon

Attributes

backup_path[RW]

Public Class Methods

new(backup_path = Dir.pwd) click to toggle source
# File lib/chef/tidy_common.rb, line 9
def initialize(backup_path = Dir.pwd)
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
  @backup_path = ::File.expand_path(backup_path)
end

Public Instance Methods

client_names(org) click to toggle source

The paths to each of the client json files in the backup

@param [String] org

@return [Array]

# File lib/chef/tidy_common.rb, line 66
def client_names(org)
  Dir[::File.join(clients_path(org), "*")].map { |dir| ::File.basename(dir, ".json") }
end
clients_path(org) click to toggle source

The path to the clients directory in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 56
def clients_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "clients"))
end
cookbook_name_from_path(path) click to toggle source

Determine the cookbook name from path

@param [String] path The path of the cookbook.

@return [String] The cookbook's name

@example cookbook_version_from_path('/data/chef_backup/snapshots/20191008040001/organizations/myorg/cookbooks/chef-sugar-5.0.4') => 'chef-sugar'

# File lib/chef/tidy_common.rb, line 179
def cookbook_name_from_path(path)
  ::File.basename(path, "-*")
end
cookbook_version_from_path(path) click to toggle source

Determine the cookbook version from a path.

@param [String] path The path of the cookbook.

@return [String] The version of the cookbook.

@example cookbook_version_from_path('/data/chef_backup/snapshots/20191008040001/organizations/myorg/cookbooks/chef-sugar-5.0.4') => '5.0.4' cookbook_version_from_path('/data/chef_backup/snapshots/20191008040001/organizations/myorg/cookbooks/chef-sugar-5.0.4/recipe/default.rb') => '5.0.4' cookbook_version_from_path('/data/chef_backup/snapshots/20191008040001/organizations/myorg/cookbooks/chef-sugar-5.0.4/files/cookbooks/default.rb') => '5.0.4'

# File lib/chef/tidy_common.rb, line 195
def cookbook_version_from_path(path)
  dirs = path.split(File::SEPARATOR)

  until dirs.empty?
    version_match = dirs[-1].match(/\d+\.\d+\.\d+/)
    if dirs[-2] == "cookbooks" && version_match # we found the cookbook version not something that looks like one inside a cookbook path
      return version_match.to_s
    else
      dirs.pop
    end
  end
end
cookbooks_path(org) click to toggle source

The path to cookbooks directory in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 104
def cookbooks_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "cookbooks"))
end
global_user_names() click to toggle source
# File lib/chef/tidy_common.rb, line 208
def global_user_names
  @global_user_names ||= Dir[::File.join(@backup_path, "users", "*")].map { |dir| ::File.basename(dir, ".json") }
end
groups_path(org) click to toggle source

The path to groups directory in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 76
def groups_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "groups"))
end
invitations_path(org) click to toggle source

The path to the invitations.json file in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 46
def invitations_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "invitations.json"))
end
json_file_to_hash(file_path, **options) click to toggle source

Read a json file and return a hash of parsed content with optional symbolized keys

@param [String] path to file @param [double splat] options to pass FFI_Yajl::Parser.parse()

@return [Hash] original json content as hash

@example json_file_to_hash('/path/to/file.json', symbolize_names: true) => { foo: “bar” }

# File lib/chef/tidy_common.rb, line 162
def json_file_to_hash(file_path, **options)
  FFI_Yajl::Parser.parse(File.read(file_path), options)
rescue Errno::ENOENT, Errno::EACCES, FFI_Yajl::ParseError
  puts "ERROR: unable to parse file: '#{file_path}'"
  raise
end
members_path(org) click to toggle source

The path to the members.json file in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 36
def members_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "members.json"))
end
org_acls_path(org) click to toggle source

The path to acls directory in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 86
def org_acls_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "acls"))
end
org_path(org) click to toggle source

The path to the org directory in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 124
def org_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org))
end
reports_dir() click to toggle source
# File lib/chef/tidy_common.rb, line 212
def reports_dir
  @reports_dir ||= ::File.join(Dir.pwd, "reports")
end
roles_path(org) click to toggle source

The path to roles directory in the backup

@param [String] org

@return [String]

# File lib/chef/tidy_common.rb, line 114
def roles_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "roles"))
end
save_user(user) click to toggle source
# File lib/chef/tidy_common.rb, line 137
def save_user(user)
  ::File.open(::File.join(users_path, "#{user["username"]}.json"), "w+") do |f|
    f.write(FFI_Yajl::Encoder.encode(user, pretty: true))
  end
end
ui() click to toggle source

@return [Chef::Knife::UI]

# File lib/chef/tidy_common.rb, line 18
def ui
  @ui ||= Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
end
unique_email() click to toggle source

generate a bogus, but valid email

@return [String]

# File lib/chef/tidy_common.rb, line 132
def unique_email
  (0...8).map { (65 + rand(26)).chr }.join.downcase +
    "@" + (0...8).map { (65 + rand(26)).chr }.join.downcase + ".com"
end
user_acls_path() click to toggle source

The path to user_acls directory in the backup

@return [String]

# File lib/chef/tidy_common.rb, line 94
def user_acls_path
  @user_acls_path ||= ::File.expand_path(::File.join(@backup_path, "user_acls"))
end
users_path() click to toggle source

The path to the users directory in the backup

@return [String]

# File lib/chef/tidy_common.rb, line 26
def users_path
  @users_path ||= ::File.expand_path(::File.join(@backup_path, "users"))
end
write_new_file(contents, path, backup = true) click to toggle source
# File lib/chef/tidy_common.rb, line 143
def write_new_file(contents, path, backup = true)
  if ::File.exist?(path) && backup
    FileUtils.cp(path, "#{path}.orig") unless ::File.exist?("#{path}.orig")
  end
  ::File.open(path, "w+") do |f|
    f.write(FFI_Yajl::Encoder.encode(contents, pretty: true))
  end
end