Files
one/src/authm_mad/oneauth
T

148 lines
3.4 KiB
Ruby
Executable File

#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2010, 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. #
#--------------------------------------------------------------------------- #
ONE_LOCATION=ENV["ONE_LOCATION"]
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby"
ETC_LOCATION="/etc/one/"
VAR_LOCATION="/var/lib/one"
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
ETC_LOCATION=ONE_LOCATION+"/etc/"
VAR_LOCATION="#{ONE_LOCATION}/var"
end
$: << RUBY_LIB_LOCATION
require 'OpenNebula'
require 'rubygems'
require 'sequel'
require 'quota'
require 'ssh_auth'
COMMANDS_HELP=<<-EOT
Usage:
oneauth <command> [<parameters>]
Commands:
* create (Creates a new user)
oneauth create <username> <ssh public key>
* quota set (sets quota for a user)
oneauth quota set <id> <cpu> <memory>
* login (generates authentication proxy)
oneauth login <username> [<expire time in seconds>]
* key (gets public key)
oneauth key
* help (prints help)
oneauth help
EOT
def print_help
puts COMMANDS_HELP
end
def get_database
config_data=File.read(ETC_LOCATION+'/auth/auth.conf')
config=YAML::load(config_data)
database_url=config[:database]
db=Sequel.connect(database_url)
end
def add_quota(uid, cpu, memory)
db=get_database
quota=Quota.new(db, OpenNebula::Client.new)
quota.set(uid.to_i, cpu.to_f, memory.to_i, nil)
end
result=[false, "Unknown error"]
command=ARGV.shift
case command
when "quota"
check_parameters("quota", 1)
case ARGV[0].downcase
when 'set'
check_parameters("quota set", 3)
Dir.chdir VAR_LOCATION
add_quota(*ARGV[1..3])
else
#default
end
exit 0
when "login"
check_parameters("login", 1)
user=ARGV[0]
time=ARGV[1]
if time
time=time.to_i
else
time=3600
end
ssh=SshAuth.new
ssh.login(user, time)
when "create"
user=OpenNebula::User.new(
OpenNebula::User.build_xml, OpenNebula::Client.new)
password = ARGV[1]
result=user.allocate(ARGV[0], password)
if !OpenNebula.is_error?(result)
puts "ID: " + user.id.to_s if ops[:verbose]
exit 0
end
when "key"
ssh=SshAuth.new
puts ssh.extract_public_key
exit 0
when "help"
print_help
exit 0
else
print_help
exit -1
end
if OpenNebula.is_error?(result)
puts "Error: " + result.message
exit -1
end