#!/usr/bin/ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# 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'
    GEMS_LOCATION     = '/usr/share/one/gems'
else
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
end

# %%RUBYGEMS_SETUP_BEGIN%%
require 'load_opennebula_paths'
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION + '/cli'

require 'command_parser'
require 'one_helper'
require 'one_helper/onehook_helper'

CommandParser::CmdParser.new(ARGV) do
    usage '`onehook` <command> [<args>] [<options>]'
    version OpenNebulaHelper::ONE_VERSION

    helper = OneHookHelper.new

    before_proc do
        helper.set_client(options)
    end

    USE = {
        :name => 'use',
        :large => '--use',
        :description => 'lock use actions'
    }

    MANAGE = {
        :name => 'manage',
        :large => '--manage',
        :description => 'lock manage actions'
    }

    ADMIN = {
        :name => 'admin',
        :large => '--admin',
        :description => 'lock admin actions'
    }

    ALL = {
        :name => 'all',
        :large => '--all',
        :description => 'lock all actions'
    }

    EXECUTION = {
        :name => 'execution',
        :large => '--execution exeid',
        :short => '-e exeid',
        :format => String,
        :description => 'execution ID'
    }

    set :format, :hookid, OneHookHelper.to_id_desc do |arg|
        helper.to_id(arg)
    end

    set :format, :hookid_list, OneHookHelper.list_to_id_desc do |arg|
        helper.list_to_id(arg)
    end

    set :format, :execid, 'Hook execution id' do |arg|
        format_int(arg)
    end

    ########################################################################
    # Hook log Options
    ########################################################################

    ERROR = {
        :name => 'error',
        :large => '--error',
        :description => 'Show only fail executions'
    }

    SUCCESS = {
        :name => 'success',
        :large => '--success',
        :description => 'Show only success executions'
    }

    HOOK_ID = {
        :name => 'hook_id',
        :large => '--hook-id id',
        :description => 'Hook ID to check logs',
        :format => Integer
    }

    SINCE = {
        :name => 'since',
        :large => '--since date',
        :description => 'First date to take logs',
        :format => String
    }

    UNTIL = {
        :name => 'until',
        :large => '--until date',
        :description => 'Last date to take logs',
        :format => String
    }

    LOG_OPTIONS = [ERROR, SUCCESS, HOOK_ID, SINCE, UNTIL]

    ########################################################################
    # Global Options
    ########################################################################
    set :option, CommandParser::OPTIONS + OpenNebulaHelper::CLIENT_OPTIONS

    list_options  = CLIHelper::OPTIONS
    list_options += OpenNebulaHelper::FORMAT
    list_options << OpenNebulaHelper::NUMERIC
    list_options << OpenNebulaHelper::DESCRIBE

    ########################################################################
    # Commands
    ########################################################################

    create_desc = <<-EOT.unindent
        Creates a new Hook from the given description

        Examples:
          - using a Hook description file:

            onehook create hook.tmpl

            - using a Hook description file via stdin:

            cat $hook_template | onehook create

    EOT

    command :create, create_desc, :file, :options =>
            [OpenNebulaHelper::DRY] do
        helper.create_resource(options) do |tmpl|
            begin
                if args[0]
                    template = File.read(args[0])
                elsif !(stdin = OpenNebulaHelper.read_stdin).empty?
                    template = stdin
                else
                    STDERR.puts 'No hook template provided'
                    exit(-1)
                end

                if options[:dry]
                    puts template
                    exit 0
                else
                    tmpl.allocate(template)
                end
            rescue StandardError => e
                STDERR.puts e.messsage
                exit(-1)
            end
        end
    end

    delete_desc = <<-EOT.unindent
        Deletes the given Hook
    EOT

    command :delete, delete_desc, [:range, :hookid_list] do
        helper.perform_actions(args[0], options, 'deleted') do |hook|
            hook.delete
        end
    end

    update_desc = <<-EOT.unindent
        Update the Hook contents. If a path is not provided the editor will
        be launched to modify the current content.
    EOT

    command :update, update_desc, :hookid, [:file, nil],
            :options => OpenNebulaHelper::APPEND do
        helper.perform_action(args[0], options, 'modified') do |obj|
            if options[:append]
                str = OpenNebulaHelper.append_template(args[0], obj, args[1])
            else
                str = OpenNebulaHelper.update_template(args[0], obj, args[1])
            end

            helper.set_client(options)
            obj = helper.retrieve_resource(obj.id)

            obj.update(str, options[:append])
        end
    end

    rename_desc = <<-EOT.unindent
        Renames the Hook
    EOT

    command :rename, rename_desc, :hookid, :name do
        helper.perform_action(args[0], options, 'renamed') do |o|
            o.rename(args[1])
        end
    end

    list_desc = <<-EOT.unindent
        Lists Hooks in the pool. #{OneHookHelper.list_layout_help}
    EOT

    command :list, list_desc, [:filterflag, nil], :options => list_options do
        helper.list_pool(options, false, args[0])
    end

    show_desc = <<-EOT.unindent
        Shows information for the given Hook. An execution ID can be given to
        obtain detailed information of a given hook execution
    EOT

    command :show, show_desc, :hookid,
            :options => [OpenNebulaHelper::FORMAT, OpenNebulaHelper::EXTENDED,
                         EXECUTION] do
        helper.show_resource(args[0], options)
    end

    top_desc = <<-EOT.unindent
        Lists Hooks continuously
    EOT

    command :top, top_desc, [:filterflag, nil], :options => list_options do
        helper.list_pool(options, true, args[0])
    end

    lock_desc = <<-EOT.unindent
        Locks a Hook to prevent certain actions defined by different levels.
        The show action will never be locked.
        Valid states are: All.
        Levels:
        [Use]: locks Admin, Manage and Use actions.
        [Manage]: locks Manage and Use actions.
        [Admin]: locks only Admin actions.
    EOT

    command :lock, lock_desc, [:range, :hookid_list],
            :options => [USE, MANAGE, ADMIN, ALL] do
        helper.perform_actions(args[0], options, 'Hook locked') do |t|
            if !options[:use].nil?
                level = 1
            elsif !options[:manage].nil?
                level = 2
            elsif !options[:admin].nil?
                level = 3
            elsif !options[:all].nil?
                level = 4
            else
                level = 1
            end
            t.lock(level)
        end
    end

    unlock_desc = <<-EOT.unindent
        Unlocks a Hook for unlock any actions with this Hook.
        Valid states are: All.
    EOT

    command :unlock, unlock_desc, [:range, :hookid_list] do
        helper.perform_actions(args[0], options, 'Hook unlocked') do |t|
            t.unlock
        end
    end

    retry_desc = <<-EOT.unindent
        Retry a previous hook execution.
    EOT

    command :retry, retry_desc, :hookid, :execid do
        helper.perform_action(args[0], options, 'Retry executed') do |t|
            t.retry args[1].to_i
        end
    end

    log_desc = <<-EOT.unindent
    Get logs about hook executions

    Examples:

        ~ $ onehook log --since 09/19/19 # returns all logs since that date
        ~ $ onehook log --error          # returns all failing execs logs
        ~ $ onehook log --hook-id 0      # returns all logs from hook 0

    EOT

    command :log, log_desc, :options => [LOG_OPTIONS, OpenNebulaHelper::XML] do
        helper.hook_logs(options)

        0
    end
end
