Statistics
| Branch: | Tag: | Revision:

one / src / cli / oneuser @ 6329d8b3

History | View | Annotate | Download (7.02 KB)

1
#!/usr/bin/env ruby
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
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
ONE_LOCATION=ENV["ONE_LOCATION"]
20

    
21
if !ONE_LOCATION
22
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
23
else
24
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
25
end
26

    
27
$: << RUBY_LIB_LOCATION
28

    
29

    
30
require 'OpenNebula'
31
require 'client_utilities'
32
require 'command_parse'
33

    
34

    
35
ShowTableUP={
36
    :id => {
37
        :name => "ID",
38
        :desc => "ONE identifier for user",
39
        :size => 4,
40
        :proc => lambda {|d,e| d.id }
41
    },
42
    :gid=> {
43
        :name => "GID",
44
        :desc => "Id of the group",
45
        :size => 4,
46
        :proc => lambda {|d,e|
47
            d.gid
48
        }
49
    },
50
    :user => {
51
        :name => "USER",
52
        :desc => "name of the user",
53
        :size => 15,
54
        :left => true,
55
        :proc => lambda {|d,e| d.name }
56
    },
57
    :password => {
58
        :name => "PASSWORD",
59
        :desc => "password of the user",
60
        :size => 50,
61
        :left => true,
62
        :proc => lambda {|d,e| d['PASSWORD'] }
63
    },
64
   
65
    :default => [:id, :gid, :user, :password]
66
}
67

    
68
class UPShow
69
    def initialize
70
        @userpool=OpenNebula::UserPool.new(get_one_client)
71
        @table=ShowTable.new(ShowTableUP)
72
    end
73
    
74
    def header_up_small
75
        scr_bold
76
        scr_underline
77
        print @table.header_str
78
        scr_restore
79
        puts ""
80
    end
81
    
82
    def list_short(options=nil)
83
        res=@userpool.info
84
        if options
85
            @table.columns=options[:columns] if options[:columns]
86
        end
87

    
88
        if OpenNebula.is_error?(res)
89
            result=res
90
        else
91
            result=res
92
            header_up_small
93
            
94
            puts @table.data_str(@userpool, options)
95
            result
96
        end
97
    end
98
end
99

    
100
class OneUPParse < CommandParse
101
    
102
    COMMANDS_HELP=<<-EOT
103

    
104
Description:
105

    
106
This command enables the OpenNebula administrator to manage users, adding,
107
listing and deleting them.
108

    
109
The create and passwd commands accept the [-r, --read-file] option. Use this
110
option to store the contents of a file (without hashing it) as the password.
111

    
112

    
113
Commands:
114

    
115
* create (Creates a new user)
116
    oneuser create username password
117
    
118
* delete (Removes a user)
119
    oneuser delete <id>
120
    
121
* list (Lists all the users in the pool)
122
    oneuser list
123

    
124
* passwd (Changes the given user's password)
125
    oneuser passwd <id> password
126

    
127

    
128
Information Columns:
129

    
130
* UID       User ID
131
* NAME      Name of the user
132
* PASSWORD  SHA1 encrypted password
133
* ENABLE    Whether the user is enabled or not
134

    
135

    
136
EOT
137

    
138
    def text_commands
139
        COMMANDS_HELP
140
    end
141

    
142
    def text_command_name
143
        "oneuser"
144
    end
145

    
146
    def list_options
147
        table=ShowTable.new(ShowTableUP)
148
        table.print_help
149
    end
150
    
151
    def special_options(opts, options)
152
        opts.on_tail("-n", "--no-hash", "Store plain password "<<
153
                "into the database") do |o|
154
            options[:no_hash]=true
155
        end
156
        opts.on_tail("-r", "--read-file", "Read password from file") do |o|
157
            options[:read_file]=true
158
        end
159
    end
160
end
161

    
162
oneup_opts=OneUPParse.new([:list, :xml])
163
oneup_opts.parse(ARGV)
164
ops=oneup_opts.options
165

    
166
result=[false, "Unknown error"]
167

    
168
command=ARGV.shift
169

    
170
case command
171
when "create"
172
    check_parameters("create", 2)
173
    user=OpenNebula::User.new(
174
        OpenNebula::User.build_xml, get_one_client)
175

    
176
    if ops[:read_file]
177
        begin
178
            password = File.read(ARGV[1]).split("\n").first
179
        rescue
180
            puts "Can not read file: #{ARGV[1]}"
181
            exit -1
182
        end
183
    else
184
        if ops[:no_hash]
185
            password = ARGV[1].gsub(/\s/, '')
186
        else
187
            password = Digest::SHA1.hexdigest(ARGV[1])
188
        end
189
    end
190

    
191
    result=user.allocate(ARGV[0], password)
192
    if !OpenNebula.is_error?(result)
193
        puts "ID: " + user.id.to_s if ops[:verbose]
194
        exit 0
195
    end
196
    
197
when "delete"
198
    check_parameters("delete", 1)
199
    args=expand_args(ARGV)
200

    
201
    args.each do |param|
202
        user_id=get_user_id(param)
203

    
204
        # Check if the user has defined VM's
205
        vms=false
206
        vmpool=OpenNebula::VirtualMachinePool.new(
207
            get_one_client, user_id.to_i)
208
        vmpool.info
209
        vmpool.each{ vms=true ; break }
210

    
211
        if vms
212
            puts "The user #{param} still has VMs defined, "+
213
                "aborting user delete."
214
            exit -1 
215
        end
216
        
217
        # Check if the user has defined VN's
218
        vns=false
219
        vnpool=OpenNebula::VirtualNetworkPool.new(
220
            get_one_client, user_id.to_i)
221

    
222
        vnpool.info
223
        vnpool.each{ vns=true ; break }
224

    
225
        if vns
226
            puts "The user #{param} still has Virtual Networks defined, "+
227
                "aborting user delete."
228
            exit -1 
229
        end
230

    
231
        user=OpenNebula::User.new(
232
            OpenNebula::User.build_xml(user_id), get_one_client)
233
        result=user.delete
234
        if !OpenNebula.is_error?(result)
235
            puts "User deleted" if ops[:verbose]
236
        end
237
    end
238

    
239
when "passwd"
240
    check_parameters("passwd", 2)
241

    
242
    user_id=get_user_id(ARGV[0])
243

    
244
    user=OpenNebula::User.new_with_id(user_id, get_one_client)
245

    
246
    if ops[:read_file]
247
        begin
248
            password = File.read(ARGV[1]).split("\n").first
249
        rescue
250
            puts "Can not read file: #{ARGV[1]}"
251
            exit -1
252
        end
253
    else
254
        if ops[:no_hash]
255
            password = ARGV[1].gsub(/\s/, '')
256
        else
257
            password = Digest::SHA1.hexdigest(ARGV[1])
258
        end
259
    end
260
    result=user.passwd(password)
261

    
262
    if !OpenNebula.is_error?(result)
263
        puts "Password changed" if ops[:verbose]
264
    else
265
        puts
266
    end
267

    
268
when "list"
269
    if !ops[:xml]
270
        uplist=UPShow.new
271
        ops[:columns]=ops[:list] if ops[:list]
272
        result=uplist.list_short(ops)
273
    else
274
        userpool=OpenNebula::UserPool.new(get_one_client)
275
        userpool.info
276
        puts userpool.to_xml(true)
277
    end
278
    
279
else
280
    oneup_opts.print_help
281
    exit -1
282
end
283

    
284
if OpenNebula.is_error?(result)
285
    puts "Error: " + result.message
286
    exit -1
287
end