class Chef::TidyCommon
Attributes
Public Class Methods
# 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
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
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
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
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
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
# 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
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
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
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
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
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
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
# File lib/chef/tidy_common.rb, line 212 def reports_dir @reports_dir ||= ::File.join(Dir.pwd, "reports") end
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
# 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
@return [Chef::Knife::UI]
# File lib/chef/tidy_common.rb, line 18 def ui @ui ||= Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {}) end
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
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
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
# 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