1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

feature #661: Update oneuser command

This commit is contained in:
Daniel Molina 2011-06-10 18:25:17 +02:00
parent 2987ac861e
commit 0123216a8d
3 changed files with 182 additions and 239 deletions

24
src/cli/etc/oneuser.yaml Normal file
View File

@ -0,0 +1,24 @@
---
:ID:
:desc: ONE identifier for the Template
:size: 4
:NAME:
:desc: Name of the Template
:size: 15
:left: true
:GROUP:
:desc: Group of the Template
:size: 8
:left: true
:PASSWORD:
:desc: Password of the User
:size: 50
:default:
- :ID
- :GROUP
- :NAME
- :PASSWORD

View File

@ -0,0 +1,91 @@
# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
require 'one_helper'
class OneUserHelper < OpenNebulaHelper::OneHelper
TABLE_CONF_FILE="#{OpenNebulaHelper::TABLE_CONF_PATH}/oneuser.yaml"
def self.rname
"USER"
end
def self.password_to_str_desc
"TBD"
end
def self.password_to_str(arg, options)
if options[:read_file]
begin
password = File.read(arg).split("\n").first
rescue
return -1, "Can not read file: #{arg}"
end
else
if options[:plain]
password = arg.gsub(/\s/, '')
else
password = Digest::SHA1.hexdigest(arg)
end
end
return 0, password
end
private
def factory(id=nil)
if id
OpenNebula::User.new_with_id(id, @client)
else
xml=OpenNebula::User.build_xml
OpenNebula::User.new(xml, @client)
end
end
def factory_pool(user_flag=-2)
#TBD OpenNebula::UserPool.new(@client, user_flag)
OpenNebula::UserPool.new(@client)
end
def format_pool(pool, options, top=false)
table=CLIHelper::ShowTable.new(TABLE_CONF_FILE, self) do
column :ID, "ONE identifier for the User", :size=>4 do |d|
d["ID"]
end
column :NAME, "Name of the User", :left, :size=>15 do |d|
d["NAME"]
end
column :GROUP, "Group of the User", :left, :size=>8 do |d|
helper.uid_to_str(d["GID"], options)
end
column :PASSWORD, "Password of the User", :size=>50 do |d|
d['PASSWORD']
end
default :ID, :GROUP, :NAME, :PASSWORD
end
if top
table.top(pool, options)
else
table.show(pool, options)
end
end
end

View File

@ -25,255 +25,83 @@ else
end
$: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cli"
require 'command_parser'
require 'one_helper/oneuser_helper'
require 'OpenNebula'
require 'client_utilities'
require 'command_parse'
cmd=CommandParser::CmdParser.new(ARGV) do
usage "oneuser COMMAND [args..] [options..]"
version OpenNebulaHelper::ONE_VERSION
helper = OneUserHelper.new
ShowTableUP={
:id => {
:name => "ID",
:desc => "ONE identifier for user",
:size => 4,
:proc => lambda {|d,e| d.id }
},
:user => {
:name => "USER",
:desc => "name of the user",
:size => 15,
:left => true,
:proc => lambda {|d,e| d.name }
},
:password => {
:name => "PASSWORD",
:desc => "password of the user",
:size => 50,
:left => true,
:proc => lambda {|d,e| d['PASSWORD'] }
},
:default => [:id, :user, :password]
}
########################################################################
# Global Options
########################################################################
set :option, CommandParser::OPTIONS
list_options = CLIHelper::OPTIONS
list_options << OpenNebulaHelper::XML
list_options << OpenNebulaHelper::NUMERIC
READ_FILE={
:name => "read_file",
:short => "-r",
:large => "--read-file",
:description => "Read password from file"
}
class UPShow
def initialize
@userpool=OpenNebula::UserPool.new(get_one_client)
@table=ShowTable.new(ShowTableUP)
PLAIN={
:name => "plain",
:short => "-p",
:large => "--plain-password",
:description => "Store plain password"
}
create_options = [READ_FILE, PLAIN]
########################################################################
# Formatters for arguments
########################################################################
set :format, :userid, OneUserHelper.to_id_desc do |arg|
helper.to_id(arg)
end
set :format, :userid_list, OneUserHelper.list_to_id_desc do |arg|
helper.list_to_id(arg)
end
set :format, :filterflag, OneUserHelper.filterflag_to_i_desc do |arg|
helper.filterflag_to_i(arg)
end
def header_up_small
scr_bold
scr_underline
print @table.header_str
scr_restore
puts ""
set :format, :password, OneUserHelper.password_to_str_desc do |arg|
OneUserHelper.password_to_str(arg, options)
end
def list_short(options=nil)
res=@userpool.info
if options
@table.columns=options[:columns] if options[:columns]
########################################################################
# Commands
########################################################################
command :create, 'Creates a new User', :text, :password, :options=>create_options do
helper.create_resource(options) do |user|
user.allocate(args[0], args[1])
end
end
if OpenNebula.is_error?(res)
result=res
else
result=res
header_up_small
puts @table.data_str(@userpool, options)
result
command :list, 'Lists Templates in the pool', [:filterflag, nil], :options=>list_options do
helper.list_pool(options)
end
command :passwd, 'Change the given Users password', :userid, :password do
helper.perform_action(args[0],options,"password changed") do |user|
user.passwd(args[1])
end
end
command :delete, 'Removes a Template', [:range, :userid_list] do
helper.perform_actions(args[0],options,"deleted") do |user|
user.delete
end
end
end
class OneUPParse < CommandParse
COMMANDS_HELP=<<-EOT
Description:
This command enables the OpenNebula administrator to manage users, adding,
listing and deleting them.
The create and passwd commands accept the [-r, --read-file] option. Use this
option to store the contents of a file (without hashing it) as the password.
Commands:
* create (Creates a new user)
oneuser create username password
* delete (Removes a user)
oneuser delete <id>
* list (Lists all the users in the pool)
oneuser list
* passwd (Changes the given user's password)
oneuser passwd <id> password
Information Columns:
* UID User ID
* NAME Name of the user
* PASSWORD SHA1 encrypted password
* ENABLE Whether the user is enabled or not
EOT
def text_commands
COMMANDS_HELP
end
def text_command_name
"oneuser"
end
def list_options
table=ShowTable.new(ShowTableUP)
table.print_help
end
def special_options(opts, options)
opts.on_tail("-n", "--no-hash", "Store plain password "<<
"into the database") do |o|
options[:no_hash]=true
end
opts.on_tail("-r", "--read-file", "Read password from file") do |o|
options[:read_file]=true
end
end
end
oneup_opts=OneUPParse.new([:list, :xml])
oneup_opts.parse(ARGV)
ops=oneup_opts.options
result=[false, "Unknown error"]
command=ARGV.shift
case command
when "create"
check_parameters("create", 2)
user=OpenNebula::User.new(
OpenNebula::User.build_xml, get_one_client)
if ops[:read_file]
begin
password = File.read(ARGV[1]).split("\n").first
rescue
puts "Can not read file: #{ARGV[1]}"
exit -1
end
else
if ops[:no_hash]
password = ARGV[1].gsub(/\s/, '')
else
password = Digest::SHA1.hexdigest(ARGV[1])
end
end
result=user.allocate(ARGV[0], password)
if !OpenNebula.is_error?(result)
puts "ID: " + user.id.to_s if ops[:verbose]
exit 0
end
when "delete"
check_parameters("delete", 1)
args=expand_args(ARGV)
args.each do |param|
user_id=get_user_id(param)
# Check if the user has defined VM's
vms=false
vmpool=OpenNebula::VirtualMachinePool.new(
get_one_client, user_id.to_i)
vmpool.info
vmpool.each{ vms=true ; break }
if vms
puts "The user #{param} still has VMs defined, "+
"aborting user delete."
exit -1
end
# Check if the user has defined VN's
vns=false
vnpool=OpenNebula::VirtualNetworkPool.new(
get_one_client, user_id.to_i)
vnpool.info
vnpool.each{ vns=true ; break }
if vns
puts "The user #{param} still has Virtual Networks defined, "+
"aborting user delete."
exit -1
end
user=OpenNebula::User.new(
OpenNebula::User.build_xml(user_id), get_one_client)
result=user.delete
if !OpenNebula.is_error?(result)
puts "User deleted" if ops[:verbose]
end
end
when "passwd"
check_parameters("passwd", 2)
user_id=get_user_id(ARGV[0])
user=OpenNebula::User.new_with_id(user_id, get_one_client)
if ops[:read_file]
begin
password = File.read(ARGV[1]).split("\n").first
rescue
puts "Can not read file: #{ARGV[1]}"
exit -1
end
else
if ops[:no_hash]
password = ARGV[1].gsub(/\s/, '')
else
password = Digest::SHA1.hexdigest(ARGV[1])
end
end
result=user.passwd(password)
if !OpenNebula.is_error?(result)
puts "Password changed" if ops[:verbose]
else
puts
end
when "list"
if !ops[:xml]
uplist=UPShow.new
ops[:columns]=ops[:list] if ops[:list]
result=uplist.list_short(ops)
else
userpool=OpenNebula::UserPool.new(get_one_client)
userpool.info
puts userpool.to_xml(true)
end
else
oneup_opts.print_help
exit -1
end
if OpenNebula.is_error?(result)
puts "Error: " + result.message
exit -1
end