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.



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

def initialize(backup_path = Dir.pwd)
  @backup_path = ::File.expand_path(backup_path)
end

Instance Attribute Details

#backup_pathObject

Returns the value of attribute backup_path.



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

def backup_path
  @backup_path
end

Instance Method Details

#clients_path(org) ⇒ Object



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

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

#cookbook_name_from_path(path) ⇒ Object



72
73
74
# File 'lib/chef/tidy_common.rb', line 72

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

#cookbooks_path(org) ⇒ Object



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

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

#global_user_namesObject



76
77
78
# File 'lib/chef/tidy_common.rb', line 76

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

#groups_path(org) ⇒ Object



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

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

#invitations_path(org) ⇒ Object



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

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

#members_path(org) ⇒ Object



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

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

#org_acls_path(org) ⇒ Object



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

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

#org_path(org) ⇒ Object



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

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

#reports_dirObject



80
81
82
# File 'lib/chef/tidy_common.rb', line 80

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

#roles_path(org) ⇒ Object



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

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

#save_user(user) ⇒ Object



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

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

#unique_emailObject



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

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



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

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

#users_pathObject



12
13
14
# File 'lib/chef/tidy_common.rb', line 12

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

#write_new_file(contents, path) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/chef/tidy_common.rb', line 63

def write_new_file(contents, path)
  if ::File.exist?(path)
    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