Class: DTK::Client::Assembly

Inherits:
CommandBaseThor show all
Defined in:
lib/commands/thor/assembly.rb

Constant Summary

Constants inherited from CommandBaseThor

CommandBaseThor::ALT_IDENTIFIER_SEPARATOR, CommandBaseThor::EXTENDED_TIMEOUT, CommandBaseThor::HIDE_FROM_BASE_CONTEXT, CommandBaseThor::TIME_DIFF

Constants included from CommandHelperMixin

CommandHelperMixin::Loaded

Constants included from ReparseMixin

ReparseMixin::YamlDTKMetaFiles

Constants included from Poller

Poller::PERIOD_WAIT_TIME

Constants inherited from Thor

Thor::HIDE_FROM_BASE_CONTEXT_HELP

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBaseThor

action_on_revalidation_response, basename, create_context_arguments, execute_from_cli, generate_cached_id, get_cached_response, get_identifiers, get_usage_info, #help, #initialize, invalidate_entities, invisible_context_list, list_method_supported?, task_names, tiered_task_names, valid_id?

Methods included from CommandBase

#get, #get_connection, handle_argument_error, #post, #post_file, #rest_url, #rotate_args

Methods included from TaskStatusMixin

#list_task_info_aux, #task_status_aux, #task_status_stream

Methods included from Console

confirmation_prompt, confirmation_prompt_additional_options, confirmation_prompt_multiple_choice, confirmation_prompt_simple, unix_shell, wait_animation

Methods included from CommandHelperMixin

#Helper

Methods included from ReparseMixin

#reparse_aux

Methods included from PushCloneChangesMixin

#push_clone_changes_aux

Methods included from CommandBaseThor::CommonOptionDefs::ClassMixin

#version_method_option

Methods included from Poller

#poller_response, #print_response, #resolve_type

Methods inherited from Thor

get_alternative_identifiers, help, match_help_item_changes, overriden_help, printable_tasks, replace_if_matched!, set_context

Constructor Details

This class inherits a constructor from DTK::Client::CommandBaseThor

Class Method Details

.assembly_listObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/commands/thor/assembly.rb', line 90

def self.assembly_list()
  assembly_list = []
  response = get_cached_response(:service, "assembly/list", {})
  raise DTK::Client::DtkError, "Unable to retreive service list." unless (response.nil? || response.ok?)

  if assemblies = response.data
    assemblies.each do |assembly|
      assembly_list << assembly["display_name"]
    end
  end

  assembly_list
end

.get_assembly_template_id_for_service(assembly_template_name, service) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/commands/thor/assembly.rb', line 30

def self.get_assembly_template_id_for_service(assembly_template_name, service)
  assembly_template_id = nil
  # TODO: See with Rich if there is better way to resolve this
  response = DTK::Client::CommandBaseThor.get_cached_response(:assembly, "assembly/list", {:subtype => 'template' })
  # response = DTK::Client::CommandBaseThor.get_cached_response(:module, "service_module/list")

  service_namespace = service.split(":").first
  service_name = service.split(":").last

  if response.ok?
    unless response['data'].nil?
      response['data'].each do |module_item|
        if ("#{service_name.to_s}/#{assembly_template_name.to_s}" == (module_item['display_name']) && service_namespace == module_item['namespace'])
          assembly_template_id = module_item['id']
          break
        end
      end
    end
  end

  raise DTK::Client::DtkError, "Illegal name (#{assembly_template_name}) for assembly." if assembly_template_id.nil?

  return assembly_template_id
end

.get_assembly_template_name_for_service(assembly_template_id, service) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/commands/thor/assembly.rb', line 55

def self.get_assembly_template_name_for_service(assembly_template_id, service)
  assembly_template_name = nil
  # TODO: See with Rich if there is better way to resolve this
  response = DTK::Client::CommandBaseThor.get_cached_response(:assembly, "assembly/list", {:subtype => 'template' })

  if response.ok?
    unless response['data'].nil?
      response['data'].each do |module_item|
        if assembly_template_id.to_i == module_item['id']
          assembly_template_name = module_item['display_name'].gsub("#{service.to_s}::",'')
          break
        end
      end
    end
  end

  raise DTK::Client::DtkError, "Illegal name or id (#{assembly_template_id}) for assembly." if assembly_template_name.nil?
  return assembly_template_name
end

.pretty_print_colsObject



75
76
77
# File 'lib/commands/thor/assembly.rb', line 75

def self.pretty_print_cols()
  PPColumns.get(:assembly)
end

.validation_list(context_params) ⇒ Object

List assembly templates for specific module



81
82
83
84
85
86
87
88
# File 'lib/commands/thor/assembly.rb', line 81

def self.validation_list(context_params)
  if context_params.is_there_identifier?(:"service-module")
    service_module_id = context_params.retrieve_arguments([:service_module_id!])
    get_cached_response(:assembly, "service_module/list_assemblies", { :service_module_id => service_module_id })
  else
    get_cached_response(:assembly, "assembly/list", {:subtype => 'template' })
  end
end

.whoamiObject



26
27
28
# File 'lib/commands/thor/assembly.rb', line 26

def self.whoami()
  return :assembly, "assembly/list", {:subtype  => 'template'}
end

Instance Method Details

#delete(context_params) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/commands/thor/assembly.rb', line 334

def delete(context_params)
  assembly_template_id = context_params.retrieve_arguments([:option_1!],method_argument_names)
  unless options.force?
    # Ask user if really want to delete assembly-template, if not then return to dtk-shell without deleting
    return unless Console.confirmation_prompt("Are you sure you want to delete assembly '#{assembly_template_id}'"+"?")
  end

  post_body = {
    :assembly_id => assembly_template_id,
    :subtype => :template
  }
  response = post rest_url("assembly/delete"), post_body

  # when changing context send request for getting latest assemblies instead of getting from cache
  @@invalidate_map << :assembly
  return response unless response.ok?
  module_name,branch = response.data(:module_name,:workspace_branch)
  response = Helper(:git_repo).pull_changes?(:service_module,module_name,:local_branch => branch)
  return response unless response.ok?()
  Response::Ok.new()
end

#deploy(context_params) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/commands/thor/assembly.rb', line 285

def deploy(context_params)
  context_params.forward_options(options)
  assembly_template_id, service_module_id, name = context_params.retrieve_arguments([:assembly_id!, :service_module_id, :option_1],method_argument_names)
  post_body = {
    :assembly_id => assembly_template_id
  }

  # special case when we need service module id
  post_body[:service_module_id] = service_module_id if context_params.pure_cli_mode

  if commit_msg = options["commit_msg"]
    post_body.merge!(:commit_msg => commit_msg)
  end

  # using this to make sure cache will be invalidated after new assembly is created from other commands e.g.
  # 'assembly-create', 'install' etc.
  @@invalidate_map << :assembly

  assembly_template_name = get_assembly_name(assembly_template_id)
  if assembly_template_name
    assembly_template_name.gsub!(/(::)|(\/)/,'-')
  end

  # we check current options and forwarded options (from deploy method)
  instance_bindings = options["instance-bindings"]
  settings = parse_service_settings(options["settings"])
  assembly_list = Assembly.assembly_list()

  if name
    raise DTK::Client::DtkValidationError, "Unable to deploy service with name '#{name}'. Service with specified name exists already!" if assembly_list.include?(name)
  else
    name = get_assembly_stage_name(assembly_list,assembly_template_name)
  end

  post_body.merge!(:name => name) if name
  post_body.merge!(:instance_bindings => instance_bindings) if instance_bindings
  post_body.merge!(:settings_json_form => JSON.generate(settings)) if settings

  response = post rest_url("assembly/deploy"), post_body
  return response unless response.ok?
  # when changing context send request for getting latest assemblies instead of getting from cache
  @@invalidate_map << :service
  @@invalidate_map << :assembly
  response
end

#deploy_target(context_params) ⇒ Object

method_option :settings, :type => :string, :aliases => ā€˜-sā€™



275
276
# File 'lib/commands/thor/assembly.rb', line 275

def deploy_target(context_params)
end

#info(context_params) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/commands/thor/assembly.rb', line 106

def info(context_params)
  assembly_template_id = context_params.retrieve_arguments([:assembly_id!],method_argument_names)
  data_type = :assembly_template

  post_body = {
    :assembly_id => assembly_template_id,
    :subtype => 'template',
  }

  post rest_url("assembly/info"), post_body
end

#list(context_params) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/commands/thor/assembly.rb', line 143

def list(context_params)
  assembly_template_id, about, service_filter = context_params.retrieve_arguments([:assembly_id, :option_1, :option_1],method_argument_names)

  if assembly_template_id.nil?

    if options.service
      service_id = options.service
      context_params_for_service = DTK::Shell::ContextParams.new
      context_params_for_service.add_context_to_params("service_module", "service_module", service_id)
      context_params_for_service.method_arguments = ['assembly',"#{service_id}"]

      response = DTK::Client::ContextRouter.routeTask("service_module", "list", context_params_for_service, @conn)
    else
      response = post rest_url("assembly/list"), {:subtype => 'template', :detail_level => 'nodes'}
      data_type = :assembly_template
      response.render_table(data_type) unless options.list?
      return response
    end

  else

    post_body = {
      :subtype => 'template',
      :assembly_id => assembly_template_id,
      :about => about
    }

    case about
    when 'nodes'
      response = post rest_url("assembly/info_about"), post_body
      data_type = :assembly_node_template
    when 'components'
      response = post rest_url("assembly/info_about"), post_body
      data_type = :component
    # when 'attributes'
      # response = post rest_url("assembly/info_about"), post_body
      # data_type = :attribute
    else
      raise_validation_error_method_usage('list')
    end

    response.render_table(data_type) unless options.list?

    return response
  end
end

#list_components(context_params) ⇒ Object



135
136
137
138
# File 'lib/commands/thor/assembly.rb', line 135

def list_components(context_params)
  context_params.method_arguments = ["components"]
  list(context_params)
end

#list_nodes(context_params) ⇒ Object



124
125
126
127
# File 'lib/commands/thor/assembly.rb', line 124

def list_nodes(context_params)
  context_params.method_arguments = ["nodes"]
  list(context_params)
end

#list_settings(context_params) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/commands/thor/assembly.rb', line 191

def list_settings(context_params)
  assembly_template_id = context_params.retrieve_arguments([:assembly_id!],method_argument_names)

  post_body = {
    :assembly_id => assembly_template_id
  }

  response = post rest_url("assembly/list_settings"), post_body
  response.render_table(:service_setting) unless options.list?

  response
end

#stage(context_params) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/commands/thor/assembly.rb', line 222

def stage(context_params)
  assembly_template_id, service_module_id, name = context_params.retrieve_arguments([:assembly_id!, :service_module_id, :option_1],method_argument_names)
  post_body = {
    :assembly_id => assembly_template_id
  }

  # special case when we need service module id
  post_body[:service_module_id] = service_module_id if context_params.pure_cli_mode

  # using this to make sure cache will be invalidated after new assembly is created from other commands e.g.
  # 'assembly-create', 'install' etc.
  @@invalidate_map << :assembly

  assembly_template_name = get_assembly_name(assembly_template_id)
  if assembly_template_name
    assembly_template_name.gsub!(/(::)|(\/)/,'-')
  end

  instance_bindings = options["instance-bindings"]
  settings          = parse_service_settings(options["settings"])
  node_size         = options.node_size
  os_type           = options.os_type
  assembly_list     = Assembly.assembly_list()

  if name
    raise DTK::Client::DtkValidationError, "Unable to stage service with name '#{name}'. Service with specified name exists already!" if assembly_list.include?(name)
  else
    name = get_assembly_stage_name(assembly_list,assembly_template_name)
  end

  post_body.merge!(:name => name) if name
  post_body.merge!(:instance_bindings => instance_bindings) if instance_bindings
  post_body.merge!(:settings_json_form => JSON.generate(settings)) if settings
  post_body.merge!(:node_size => node_size) if node_size
  post_body.merge!(:os_type => os_type) if os_type

  response = post rest_url("assembly/stage"), post_body
  return response unless response.ok?
  # when changing context send request for getting latest assemblies instead of getting from cache
  @@invalidate_map << :service
  @@invalidate_map << :assembly

  return response
end

#stage_target(context_params) ⇒ Object



212
213
# File 'lib/commands/thor/assembly.rb', line 212

def stage_target(context_params)
end