Statistics
| Branch: | Tag: | Revision:

one / share / sudoers / sudo_commands.rb @ ff285c99

History | View | Annotate | Download (2.38 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs        #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
require "erb"
20

    
21
KEYS = [:MISC , :NET , :LVM , :ISCSI , :OVS , :XEN]
22

    
23
CMDS = {
24
    :MISC  => %w(dd mkfs sync),
25
    :NET   => %w(brctl ebtables iptables ip),
26
    :LVM   => %w(lvcreate lvremove lvrename lvs vgdisplay),
27
    :ISCSI => %w(iscsiadm tgt-admin tgtadm),
28
    :OVS   => %w(ovs-ofctl ovs-vsctl),
29
    :XEN   => %w(xentop xl xm)
30
}
31

    
32
abs_cmds = {}
33
not_found_cmds = []
34

    
35
KEYS.each do |label|
36
    cmds = CMDS[label]
37

    
38
    _abs_cmds = []
39
    cmds.each do |cmd|
40
        abs_cmd = `which #{cmd} 2>/dev/null`
41

    
42
        if !abs_cmd.empty?
43
            _abs_cmds << abs_cmd.strip
44
        else
45
            not_found_cmds << cmd
46
        end
47
    end
48

    
49
    abs_cmds["ONE_#{label}"] = _abs_cmds
50
end
51

    
52
abs_cmds.reject!{|k,v| v.empty?}
53

    
54
puts ERB.new(DATA.read,nil, "<>").result(binding)
55

    
56
if !not_found_cmds.empty?
57
    STDERR.puts "\n---\n\nNot found:"
58
    not_found_cmds.each{|cmd| STDERR.puts("- #{cmd}")}
59
end
60

    
61
__END__
62
Defaults !requiretty
63
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
64

65
<% KEYS.each do |k|; l = "ONE_#{k}"; v = abs_cmds[l]  %>
66
<% if !v.nil? %>
67
Cmnd_Alias <%= l %> = <%= v.join(", ") %>
68
<% end %>
69
<% end %>
70

71
oneadmin ALL=(ALL) NOPASSWD: <%= KEYS.select{|k| !abs_cmds["ONE_#{k}"].nil?}.collect{|k| "ONE_#{k}"}.join(", ") %>