Method: Morpheus::Cli::LibraryContainerScriptsCommand#update

Defined in:
lib/morpheus/cli/library_container_scripts_command.rb

#update(args) ⇒ Object



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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
# File 'lib/morpheus/cli/library_container_scripts_command.rb', line 238

def update(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    opts.on('--name VALUE', String, "Name") do |val|
      params['name'] = val
    end
    opts.on('-t', '--type TYPE', "Script Type. i.e. bash, powershell. Default is bash.") do |val|
      params['scriptType'] = val
    end
    opts.on('--phase PHASE', String, "Script Phase. i.e. start, stop, preProvision, provision, postProvision, preDeploy, deploy, reconfigure, teardown. Default is provision.") do |val|
      params['scriptPhase'] = val
    end      
    opts.on('--script TEXT', String, "Contents of the script.") do |val|
      params['script'] = val
    end
    opts.on('--file FILE', "File containing the script. This can be used instead of --script" ) do |filename|
      full_filename = File.expand_path(filename)
      if File.exists?(full_filename)
        params['script'] = File.read(full_filename)
      else
        print_red_alert "File not found: #{full_filename}"
        exit 1
      end
    end
    opts.on("--sudo [on|off]", String, "Run with sudo") do |val|
      params['sudoUser'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    end
    opts.on("--run-as-user VALUE", String, "Run as user") do |val|
      params['runAsUser'] = val
    end
    # opts.on("--run-as-password VALUE", String, "Run as password") do |val|
    #   params['runAsPassword'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s == '1' || val.to_s == ''
    # end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote, :quiet])
    opts.footer = "Update a container script." + "\n" +
                  "[name] is required. This is the name or id of a container script."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "wrong number of arguments, expected 1 and got (#{args.count}) #{args.join(', ')}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    container_script = find_container_script_by_name_or_id(args[0])
    if container_script.nil?
      return 1
    end
    # construct payload
    payload = nil
    if options[:payload]
      payload = options[:payload]
      params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
      payload.deep_merge!({'containerScript' => params}) unless params.empty?
    else
      # update without prompting
      params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]
      script_payload = params
      if script_payload.empty?
        raise_command_error "Specify at least one option to update.\n#{optparse}"
      end
      payload = {'containerScript' => script_payload}
    end
    @container_scripts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @container_scripts_interface.dry.update(container_script["id"], payload)
      return
    end
    json_response = @container_scripts_interface.update(container_script["id"], payload)
    if options[:json]
      puts as_json(json_response, options)
    elsif !options[:quiet]
      print_green_success "Updated container script #{container_script['name']}"
      _get(container_script['id'], {})
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end