/
chef-sudo.rb
67 lines (58 loc) · 1.45 KB
/
chef-sudo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'chef/resource'
class Chef
class Resource
class Sudo < Chef::Resource
def initialize(name, collection=nil, node=nil)
super(name, collection, node)
@resource_name = :sudo
@action = :execute
@allowed_actions.push(:execute)
end
def command(arg=nil)
set_or_return(
:command,
arg,
:kind_of => [ String ]
)
end
def user(arg=nil)
set_or_return(
:user,
arg,
:kind_of => [ String ]
)
end
def group(arg=nil)
set_or_return(
:group,
arg,
:kind_of => [ String ]
)
end
def simulate_initial_login(arg=false)
set_or_return(
:simulate_initial_login,
arg,
:kind_of => [ TrueClass, FalseClass ]
)
end
end
end
end
class Chef
class Provider
class Sudo < Chef::Provider
def load_current_resource
end
def action_execute
command = ["sudo"]
command << "-u #{@new_resource.user}" if @new_resource.user
command << "-g #{@new_resource.group}" if @new_resource.group
command << "-i" if @new_resource.simulate_initial_login
command << @new_resource.command
Chef::Mixin::Command.run_command(:command => command.join(' '))
Chef::Log.info "Ran sudo [#{@new_resource.name}]"
end
end
end
end