Class: Chef::Handler::Users

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef-handler-users.rb

Defined Under Namespace

Classes: ConfigurationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Users

Returns a new instance of Users.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chef-handler-users.rb', line 27

def initialize(config={})
  @config = config

  %w[to from].each do |key|
    unless @config.has_key? key
      raise ConfigurationError.new("Required configuration #{key} not passed to handler")
    end
  end

  if @config[:via] == 'smtp' && !@config[:via_options]
    raise ConfigurationError.new("via_options is required for smtp")
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



24
25
26
# File 'lib/chef-handler-users.rb', line 24

def config
  @config
end

Instance Method Details

#ensure_hash(hash_thing) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/chef-handler-users.rb', line 85

def ensure_hash(hash_thing)
  new_h = {}
  hash_thing.each do |k,v|
    new_h[k.to_sym] = v.kind_of?(Hash) || v.kind_of?(Node::Attribute) ? to_hash(v) : v
  end
  new_h
end

#generate_email_body(users) ⇒ Object



65
66
67
68
69
70
# File 'lib/chef-handler-users.rb', line 65

def generate_email_body users
  users.inject([]) do |body, user|
    body << summary_for_user(user)
    body
  end.join("\n")
end

#reportObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef-handler-users.rb', line 41

def report
  updated_users = run_status.updated_resources.select do |resource|
    resource.resource_name == :user
  end

  if updated_users.empty?
    Chef::Log.info "Users handler detected no user changes"
    return
  else
    Chef::Log.info "Users handler detected #{updated_users.length} user changes. Generating summary email for #{@config[:to]}"
  end

  subject = "Chef run on #{node.name} at #{Time.now} resulted in change of #{updated_users.length} users"

  message = generate_email_body(updated_users)

  Pony.mail(
    ensure_hash(@config).update(
      :subject => subject,
      :body => message
    )
  )
end

#summary_for_user(user) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef-handler-users.rb', line 72

def summary_for_user user
  <<-EOH
User #{user.name} updated:
- resource action: #{user.action}
- comment (GECOS): #{user.comment}
- system username: #{user.username}
- system uid: #{user.uid}
- system gid: #{user.gid}
- system shell: #{user.shell}
- system user: #{user.system}
  EOH
end