Class: Chef::TidyCommon

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/tidy_common.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backup_path = Dir.pwd) ⇒ TidyCommon

Returns a new instance of TidyCommon.



9
10
11
12
13
# 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

Instance Attribute Details

#backup_pathObject

Returns the value of attribute backup_path.



7
8
9
# File 'lib/chef/tidy_common.rb', line 7

def backup_path
  @backup_path
end

Instance Method Details

#client_names(org) ⇒ Array

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

Parameters:

  • org (String)

Returns:

  • (Array)


66
67
68
# 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) ⇒ String

The path to the clients directory in the backup

Parameters:

  • org (String)

Returns:

  • (String)


56
57
58
# 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) ⇒ String

Determine the cookbook name from path

cookbook_version_from_path(‘/data/chef_backup/snapshots/20191008040001/organizations/myorg/cookbooks/chef-sugar-5.0.4’) => ‘chef-sugar’

Parameters:

  • path (String)

    The path of the cookbook.

Returns:

  • (String)

    The cookbook’s name



179
180
181
# File 'lib/chef/tidy_common.rb', line 179

def cookbook_name_from_path(path)
  ::File.basename(path, "-*")
end

#cookbook_version_from_path(path) ⇒ String

Determine the cookbook version from a path.

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’

Parameters:

  • path (String)

    The path of the cookbook.

Returns:

  • (String)

    The version of the cookbook.



195
196
197
198
199
200
201
202
203
204
205
206
# 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) ⇒ String

The path to cookbooks directory in the backup

Parameters:

  • org (String)

Returns:

  • (String)


104
105
106
# 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_namesObject



208
209
210
# 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) ⇒ String

The path to groups directory in the backup

Parameters:

  • org (String)

Returns:

  • (String)


76
77
78
# 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) ⇒ String

The path to the invitations.json file in the backup

Parameters:

  • org (String)

Returns:

  • (String)


46
47
48
# 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) ⇒ Hash

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

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

Parameters:

  • path (String)

    to file

  • options (double splat)

    to pass FFI_Yajl::Parser.parse()

Returns:

  • (Hash)

    original json content as hash



162
163
164
165
166
167
# 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) ⇒ String

The path to the members.json file in the backup

Parameters:

  • org (String)

Returns:

  • (String)


36
37
38
# 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) ⇒ String

The path to acls directory in the backup

Parameters:

  • org (String)

Returns:

  • (String)


86
87
88
# 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) ⇒ String

The path to the org directory in the backup

Parameters:

  • org (String)

Returns:

  • (String)


124
125
126
# File 'lib/chef/tidy_common.rb', line 124

def org_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org))
end

#reports_dirObject



212
213
214
# File 'lib/chef/tidy_common.rb', line 212

def reports_dir
  @reports_dir ||= ::File.join(Dir.pwd, "reports")
end

#roles_path(org) ⇒ String

The path to roles directory in the backup

Parameters:

  • org (String)

Returns:

  • (String)


114
115
116
# 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) ⇒ Object



137
138
139
140
141
# 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

#uiChef::Knife::UI

Returns:

  • (Chef::Knife::UI)


18
19
20
# File 'lib/chef/tidy_common.rb', line 18

def ui
  @ui ||= Chef::Knife::UI.new(STDOUT, STDERR, STDIN, {})
end

#unique_emailString

generate a bogus, but valid email

Returns:

  • (String)


132
133
134
135
# 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_pathString

The path to user_acls directory in the backup

Returns:

  • (String)


94
95
96
# 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_pathString

The path to the users directory in the backup

Returns:

  • (String)


26
27
28
# 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) ⇒ Object



143
144
145
146
147
148
149
150
# 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