Module: Aspera::Cli::SyncActions

Included in:
Plugins::Node, Plugins::Server
Defined in:
lib/aspera/cli/sync_actions.rb

Overview

Module for sync actions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.declare_options(options) ⇒ Object



36
37
38
# File 'lib/aspera/cli/sync_actions.rb', line 36

def declare_options(options)
  options.declare(:sync_info, 'Information for sync instance and sessions', types: Hash)
end

Instance Method Details

#execute_sync_action(&block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/aspera/cli/sync_actions.rb', line 86

def execute_sync_action(&block)
  Aspera.assert(block){'No block given'}
  command = options.get_next_command(%i[start admin])
  # try to get 3 arguments as simple arguments
  case command
  when :start
    # possibilities are:
    async_params = options.get_option(:sync_info, default: {})
    sync_args_to_params(async_params)
    Transfer::Sync.start(async_params, &block)
    return Main.result_success
  when :admin
    command2 = options.get_next_command([:status])
    case command2
    when :status
      sync_session_name = options.get_next_argument('name of sync session', mandatory: false, validation: String)
      async_params = options.get_option(:sync_info, mandatory: true)
      return {type: :single_object, data: Transfer::Sync.admin_status(async_params, sync_session_name)}
    end
  end
end

#sync_args_to_params(async_params) ⇒ Object

Read command line arguments (3) and converts to sync_info format

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/aspera/cli/sync_actions.rb', line 42

def sync_args_to_params(async_params)
  # sync session parameters can be provided on command line instead of sync_info
  arguments = {}
  SYNC_ARGUMENTS_INFO.each do |info|
    value = options.get_next_argument(
      info[:conf],
      mandatory: false,
      validation: info[:type],
      accept_list: info[:values])
    break if value.nil?
    arguments[info[:conf]] = value.to_s
  end
  Log.log.debug{Log.dump('arguments', arguments)}
  raise Cli::BadArgument, "Provide 0 or 3 arguments, not #{arguments.keys.length} for: #{SYNC_SIMPLE_ARGS.join(', ')}" unless
    [0, 3].include?(arguments.keys.length)
  if !arguments.empty?
    session_info = async_params
    param_path = :conf
    if async_params.key?('sessions') || async_params.key?('instance')
      async_params['sessions'] ||= [{}]
      Aspera.assert(async_params['sessions'].length == 1){'Only one session is supported with arguments'}
      session_info = async_params['sessions'][0]
      param_path = :args
    end
    SYNC_ARGUMENTS_INFO.each do |info|
      key_path = info[param_path].split('.')
      hash_for_key = session_info
      if key_path.length > 1
        first = key_path.shift
        async_params[first] ||= {}
        hash_for_key = async_params[first]
      end
      raise "Parameter #{info[:conf]} is also set in sync_info, remove from sync_info" if hash_for_key.key?(key_path.last)
      hash_for_key[key_path.last] = arguments[info[:conf]]
    end
    if !session_info.key?('name')
      # if no name is specified, generate one from simple arguments
      session_info['name'] = SYNC_SIMPLE_ARGS.map do |arg_name|
        arguments[arg_name]&.gsub(/[^a-zA-Z0-9]/, '')
      end.compact.reject(&:empty?).join('_')
    end
  end
end