aws_command.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 ops 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. # Make sure we're completely up to date before launching
  37. clear_cache()
  38. ah.run_playbook("playbooks/aws/#{options[:type]}/launch.yml")
  39. ensure
  40. # This is so that if we a config right after a launch, the newly launched instances will be
  41. # in the list.
  42. clear_cache()
  43. end
  44. desc "clear-cache", 'Clear the inventory cache'
  45. def clear_cache()
  46. print "Clearing inventory cache... "
  47. AwsHelper.clear_inventory_cache()
  48. puts "Done."
  49. end
  50. option :name, :required => false, :type => :string,
  51. :desc => 'The name of the instance to configure.'
  52. option :env, :required => false, :aliases => '-e', :enum => SUPPORTED_ENVS,
  53. :desc => 'The environment of the new instances.'
  54. option :type, :required => false, :enum => LaunchHelper.get_aws_host_types,
  55. :desc => 'The type of the instances to configure.'
  56. desc "config", 'Configures instances.'
  57. def config()
  58. ah = AnsibleHelper.for_aws()
  59. abort 'Error: you can\'t specify both --name and --type' unless options[:type].nil? || options[:name].nil?
  60. abort 'Error: you can\'t specify both --name and --env' unless options[:env].nil? || options[:name].nil?
  61. host_type = nil
  62. if options[:name]
  63. details = AwsHelper.get_host_details(options[:name])
  64. #- ah.extra_vars['oo_host_group_exp'] = options[:name]
  65. #- ah.extra_vars['oo_env'] = details['env']
  66. #- host_type = details['host-type']
  67. #+ ah.extra_vars['oo_host_group_exp'] = details['ec2_public_dns_name']
  68. #+ ah.extra_vars['oo_env'] = details['ec2_tag_environment']
  69. #+ host_type = details['ec2_tag_host-type']
  70. # ah.extra_vars['oo_host_group_exp'] = details['ec2_public_dns_name']
  71. ah.extra_vars['oo_host_group_exp'] = options[:name]
  72. # ah.extra_vars['oo_env'] = details['env']
  73. ah.extra_vars['oo_env'] = details['ec2_tag_environment']
  74. host_type = details['ec2_tag_host-type']
  75. elsif options[:type] && options[:env]
  76. oo_env_host_type_tag = AwsHelper.generate_env_host_type_tag_name(options[:env], options[:type])
  77. ah.extra_vars['oo_host_group_exp'] = "groups['#{oo_env_host_type_tag}']"
  78. ah.extra_vars['oo_env'] = options[:env]
  79. host_type = options[:type]
  80. else
  81. abort 'Error: you need to specify either --name or (--type and --env)'
  82. end
  83. puts
  84. puts "Configuring #{options[:type]} instance(s) in AWS..."
  85. ah.run_playbook("playbooks/aws/#{host_type}/config.yml")
  86. end
  87. option :env, :required => false, :aliases => '-e', :enum => SUPPORTED_ENVS,
  88. :desc => 'The environment to list.'
  89. desc "list", "Lists instances."
  90. def list()
  91. AwsHelper.check_creds()
  92. hosts = AwsHelper.get_hosts()
  93. hosts.delete_if { |h| h.env != options[:env] } unless options[:env].nil?
  94. fmt_str = "%34s %5s %8s %17s %7s"
  95. puts
  96. puts fmt_str % ['Name','Env', 'State', 'IP Address', 'Created By']
  97. puts fmt_str % ['----','---', '-----', '----------', '----------']
  98. hosts.each { |h| puts fmt_str % [h.name, h.env, h.state, h.public_ip, h.created_by ] }
  99. puts
  100. end
  101. desc "ssh", "Ssh to an instance"
  102. def ssh(*ssh_ops, host)
  103. if host =~ /^([\w\d_.-]+)@([\w\d-_.]+)/
  104. user = $1
  105. host = $2
  106. end
  107. details = AwsHelper.get_host_details(host)
  108. abort "\nError: Instance [#{host}] is not RUNNING\n\n" unless details['ec2_state'] == 'running'
  109. cmd = "ssh #{ssh_ops.join(' ')}"
  110. if user.nil?
  111. cmd += " "
  112. else
  113. cmd += " #{user}@"
  114. end
  115. cmd += "#{details['ec2_ip_address']}"
  116. exec(cmd)
  117. end
  118. desc 'types', 'Displays instance types'
  119. def types()
  120. puts
  121. puts "Available Host Types"
  122. puts "--------------------"
  123. LaunchHelper.get_aws_host_types.each { |t| puts " #{t}" }
  124. puts
  125. end
  126. end
  127. end
  128. end