aws_command.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. require 'thor'
  2. require_relative 'aws_helper'
  3. require_relative 'launch_helper'
  4. module OpenShift
  5. module Ops
  6. class AwsCommand < Thor
  7. # WARNING: we do not currently support environments with hyphens in the name
  8. SUPPORTED_ENVS = %w(prod stg int tint kint test jint amint tdint lint)
  9. option :type, :required => true, :enum => LaunchHelper.get_aws_host_types,
  10. :desc => 'The host type of the new instances.'
  11. option :env, :required => true, :aliases => '-e', :enum => SUPPORTED_ENVS,
  12. :desc => 'The environment of the new instances.'
  13. option :count, :default => 1, :aliases => '-c', :type => :numeric,
  14. :desc => 'The number of instances to create'
  15. option :tag, :type => :array,
  16. :desc => 'The tag(s) to add to the new instances. Allowed characters are letters, numbers, and hyphens.'
  17. desc "launch", "Launches instances."
  18. def launch()
  19. AwsHelper.check_creds()
  20. # Expand all of the instance names so that we have a complete array
  21. names = []
  22. options[:count].times { names << "#{options[:env]}-#{options[:type]}-#{SecureRandom.hex(5)}" }
  23. ah = AnsibleHelper.for_aws()
  24. # AWS specific configs
  25. ah.extra_vars['oo_new_inst_names'] = names
  26. ah.extra_vars['oo_new_inst_tags'] = options[:tag]
  27. ah.extra_vars['oo_env'] = options[:env]
  28. # Add a created by tag
  29. ah.extra_vars['oo_new_inst_tags'] = {} if ah.extra_vars['oo_new_inst_tags'].nil?
  30. ah.extra_vars['oo_new_inst_tags']['created-by'] = ENV['USER']
  31. ah.extra_vars['oo_new_inst_tags'].merge!(AwsHelper.generate_env_tag(options[:env]))
  32. ah.extra_vars['oo_new_inst_tags'].merge!(AwsHelper.generate_host_type_tag(options[:type]))
  33. ah.extra_vars['oo_new_inst_tags'].merge!(AwsHelper.generate_env_host_type_tag(options[:env], options[:type]))
  34. puts
  35. puts "Creating #{options[:count]} #{options[:type]} instance(s) in AWS..."
  36. ah.ignore_bug_6407
  37. # Make sure we're completely up to date before launching
  38. clear_cache()
  39. ah.run_playbook("playbooks/aws/#{options[:type]}/launch.yml")
  40. ensure
  41. # This is so that if we a config right after a launch, the newly launched instances will be
  42. # in the list.
  43. clear_cache()
  44. end
  45. desc "clear-cache", 'Clear the inventory cache'
  46. def clear_cache()
  47. print "Clearing inventory cache... "
  48. AwsHelper.clear_inventory_cache()
  49. puts "Done."
  50. end
  51. option :name, :required => false, :type => :string,
  52. :desc => 'The name of the instance to configure.'
  53. option :env, :required => false, :aliases => '-e', :enum => SUPPORTED_ENVS,
  54. :desc => 'The environment of the new instances.'
  55. option :type, :required => false, :enum => LaunchHelper.get_aws_host_types,
  56. :desc => 'The type of the instances to configure.'
  57. desc "config", 'Configures instances.'
  58. def config()
  59. ah = AnsibleHelper.for_aws()
  60. abort 'Error: you can\'t specify both --name and --type' unless options[:type].nil? || options[:name].nil?
  61. abort 'Error: you can\'t specify both --name and --env' unless options[:env].nil? || options[:name].nil?
  62. host_type = nil
  63. if options[:name]
  64. details = AwsHelper.get_host_details(options[:name])
  65. ah.extra_vars['oo_host_group_exp'] = options[:name]
  66. ah.extra_vars['oo_env'] = details['env']
  67. host_type = details['host-type']
  68. elsif options[:type] && options[:env]
  69. oo_env_host_type_tag = AwsHelper.generate_env_host_type_tag_name(options[:env], options[:type])
  70. ah.extra_vars['oo_host_group_exp'] = "groups['#{oo_env_host_type_tag}']"
  71. ah.extra_vars['oo_env'] = options[:env]
  72. host_type = options[:type]
  73. else
  74. abort 'Error: you need to specify either --name or (--type and --env)'
  75. end
  76. puts
  77. puts "Configuring #{options[:type]} instance(s) in AWS..."
  78. ah.ignore_bug_6407
  79. ah.run_playbook("playbooks/aws/#{host_type}/config.yml")
  80. end
  81. option :env, :required => false, :aliases => '-e', :enum => SUPPORTED_ENVS,
  82. :desc => 'The environment to list.'
  83. desc "list", "Lists instances."
  84. def list()
  85. AwsHelper.check_creds()
  86. hosts = AwsHelper.get_hosts()
  87. hosts.delete_if { |h| h.env != options[:env] } unless options[:env].nil?
  88. fmt_str = "%34s %5s %8s %17s %7s"
  89. puts
  90. puts fmt_str % ['Name','Env', 'State', 'IP Address', 'Created By']
  91. puts fmt_str % ['----','---', '-----', '----------', '----------']
  92. hosts.each { |h| puts fmt_str % [h.name, h.env, h.state, h.public_ip, h.created_by ] }
  93. puts
  94. end
  95. desc "ssh", "Ssh to an instance"
  96. def ssh(*ssh_ops, host)
  97. if host =~ /^([\w\d_.-]+)@([\w\d-_.]+)/
  98. user = $1
  99. host = $2
  100. end
  101. details = AwsHelper.get_host_details(host)
  102. abort "\nError: Instance [#{host}] is not RUNNING\n\n" unless details['ec2_state'] == 'running'
  103. cmd = "ssh #{ssh_ops.join(' ')}"
  104. if user.nil?
  105. cmd += " "
  106. else
  107. cmd += " #{user}@"
  108. end
  109. cmd += "#{details['ec2_ip_address']}"
  110. exec(cmd)
  111. end
  112. desc 'types', 'Displays instance types'
  113. def types()
  114. puts
  115. puts "Available Host Types"
  116. puts "--------------------"
  117. LaunchHelper.get_aws_host_types.each { |t| puts " #{t}" }
  118. puts
  119. end
  120. end
  121. end
  122. end