#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# 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.                                             #
#--------------------------------------------------------------------------- #

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
$: << RUBY_LIB_LOCATION+'/cli'

require 'OpenNebula'

require 'rubygems'
require 'sequel'
require 'quota'
require 'ssh_auth'
require 'yaml'

require 'command_parser'
require 'one_helper'

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

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

    set :option, CommandParser::OPTIONS

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

    # Helpers
    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, num_vms=nil)
        db=get_database
        quota=Quota.new(db, OpenNebula::Client.new)
        quota.set(uid.to_i, cpu.to_f, memory.to_i, num_vms)
    end

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

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

    login_desc = <<-EOT.unindent
        Generates authentication proxy. The last argument specifies 
        the expiration time in seconds
    EOT

    command 'login', login_desc, :userid, :text do
        user=args[0]
        time=args[1]
        pp args
        if time
            time=time.to_i
        else
            time=3600
        end

        ssh=SshAuth.new
        ssh.login(user, time)
        exit_with_code 0
    end

    command 'key', 'Gets public key' do
        ssh=SshAuth.new
        puts ssh.extract_public_key
        exit_with_code 0
    end
end