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) ⇒ Object



35
36
37
# File 'lib/chef/tidy_common.rb', line 35

def client_names(org)
  Dir[::File.join(clients_path(org), "*")].map { |dir| ::File.basename(dir, ".json") }
end

#clients_path(org) ⇒ Object



31
32
33
# File 'lib/chef/tidy_common.rb', line 31

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

#cookbook_name_from_path(path) ⇒ Object



83
84
85
# File 'lib/chef/tidy_common.rb', line 83

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

#cookbooks_path(org) ⇒ Object



51
52
53
# File 'lib/chef/tidy_common.rb', line 51

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

#global_user_namesObject



87
88
89
# File 'lib/chef/tidy_common.rb', line 87

def global_user_names
  @global_user_names ||= Dir[::File.join(@backup_path, "users", "*")].map { |dir| ::File.basename(dir, ".json") }
end

#groups_path(org) ⇒ Object



39
40
41
# File 'lib/chef/tidy_common.rb', line 39

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

#invitations_path(org) ⇒ Object



27
28
29
# File 'lib/chef/tidy_common.rb', line 27

def invitations_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "invitations.json"))
end

#members_path(org) ⇒ Object



23
24
25
# File 'lib/chef/tidy_common.rb', line 23

def members_path(org)
  ::File.expand_path(::File.join(@backup_path, "organizations", org, "members.json"))
end

#org_acls_path(org) ⇒ Object



43
44
45
# File 'lib/chef/tidy_common.rb', line 43

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

#org_path(org) ⇒ Object



59
60
61
# File 'lib/chef/tidy_common.rb', line 59

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

#reports_dirObject



91
92
93
# File 'lib/chef/tidy_common.rb', line 91

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

#roles_path(org) ⇒ Object



55
56
57
# File 'lib/chef/tidy_common.rb', line 55

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

#save_user(user) ⇒ Object



68
69
70
71
72
# File 'lib/chef/tidy_common.rb', line 68

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

#uiObject



15
16
17
# File 'lib/chef/tidy_common.rb', line 15

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

#unique_emailObject



63
64
65
66
# File 'lib/chef/tidy_common.rb', line 63

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_pathObject



47
48
49
# File 'lib/chef/tidy_common.rb', line 47

def user_acls_path
  @user_acls_path ||= ::File.expand_path(::File.join(@backup_path, "user_acls"))
end

#users_pathObject



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

def users_path
  @users_path ||= ::File.expand_path(::File.join(@backup_path, "users"))
end

#write_new_file(contents, path, backup = true) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/chef/tidy_common.rb', line 74

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