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

#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



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

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

#cookbooks_path(org) ⇒ Object



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

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

#global_user_namesObject



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

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

#groups_path(org) ⇒ Object



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

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



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

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

#org_path(org) ⇒ Object



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

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

#reports_dirObject



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

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

#roles_path(org) ⇒ Object



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

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

#save_user(user) ⇒ Object



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

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



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

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



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

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



70
71
72
73
74
75
76
77
# File 'lib/chef/tidy_common.rb', line 70

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