#!/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"
else
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end

$: << RUBY_LIB_LOCATION


require 'OpenNebula'
require 'client_utilities'
require 'command_parse'


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]
}

class UPShow
    def initialize
        @userpool=OpenNebula::UserPool.new(get_one_client)
        @table=ShowTable.new(ShowTableUP)
    end
    
    def header_up_small
        scr_bold
        scr_underline
        print @table.header_str
        scr_restore
        puts ""
    end
    
    def list_short(options=nil)
        res=@userpool.info
        if options
            @table.columns=options[:columns] if options[:columns]
        end

        if OpenNebula.is_error?(res)
            result=res
        else
            result=res
            header_up_small
            
            puts @table.data_str(@userpool, options)
            result
        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])
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
