Statistics
| Branch: | Tag: | Revision:

one / src / authm_mad / oneauth @ 13f69b5a

History | View | Annotate | Download (3.4 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
    ETC_LOCATION="/etc/one/"
24
    VAR_LOCATION="/var/lib/one"
25
else
26
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
27
    ETC_LOCATION=ONE_LOCATION+"/etc/"
28
    VAR_LOCATION="#{ONE_LOCATION}/var"
29
end
30

    
31
$: << RUBY_LIB_LOCATION
32
$: << RUBY_LIB_LOCATION+'/cli'
33

    
34
require 'OpenNebula'
35

    
36
require 'rubygems'
37
require 'sequel'
38
require 'quota'
39
require 'ssh_auth'
40
require 'yaml'
41

    
42
require 'command_parser'
43
require 'one_helper'
44

    
45
cmd=CommandParser::CmdParser.new(ARGV) do
46
    usage "oneauth COMMAND [args..]"
47

    
48
    description "This command contains a set of utilities to " <<
49
                "manage authorization module."
50

    
51
    set :option, CommandParser::OPTIONS
52

    
53
    set :format, :userid, OpenNebulaHelper.name_to_id_desc("USER") do |arg|
54
        OpenNebulaHelper.name_to_id(arg, "USER")
55
    end
56

    
57
    # Helpers
58
    def get_database
59
        config_data=File.read(ETC_LOCATION+'/auth/auth.conf')
60
        config=YAML::load(config_data)
61

    
62
        database_url=config[:database]
63
        db=Sequel.connect(database_url)
64
    end
65

    
66
    def add_quota(uid, cpu, memory, num_vms=nil)
67
        db=get_database
68
        quota=Quota.new(db, OpenNebula::Client.new)
69
        quota.set(uid.to_i, cpu.to_f, memory.to_i, num_vms)
70
    end
71

    
72
    # Commands
73
    quotaset_desc = <<-EOT.unindent
74
        Sets CPU, MEMORY and NUM_VMs quota for a given user
75
    EOT
76

    
77
    command 'quota-set', quotaset_desc , :userid, :cpu, :memory, :num_vms do
78
        Dir.chdir VAR_LOCATION
79
        begin
80
            add_quota(*args[1..4])
81
        rescue Exception => e
82
            exit_with_code -1, "Error starting server: #{e}"
83
        end
84
        exit_with_code 0
85
    end
86

    
87
    login_desc = <<-EOT.unindent
88
        Generates authentication proxy. The last argument specifies 
89
        the expiration time in seconds
90
    EOT
91

    
92
    command 'login', login_desc, :userid, :text do
93
        user=args[0]
94
        time=args[1]
95
        pp args
96
        if time
97
            time=time.to_i
98
        else
99
            time=3600
100
        end
101

    
102
        ssh=SshAuth.new
103
        ssh.login(user, time)
104
        exit_with_code 0
105
    end
106

    
107
    command 'key', 'Gets public key' do
108
        ssh=SshAuth.new
109
        puts ssh.extract_public_key
110
        exit_with_code 0
111
    end
112
end