def _prompt_instance_type():
  """Prompts for an instance type."""
  instance_types = [(instance_type.name, instance_type)
                    for instance_type in _INSTANCE_TYPES]

  return common.prompt_choice("Instance type", instance_types,
                              _DEFAULT_INSTANCE_TYPE)
def main():
  connection = common.connect()

  prefix = raw_input(
    "Do you want the section of {} to have a prefix? (default '')".format(
      _HOSTS_PATH))
  if prefix:
    prefix = prefix + " "
  region = common.prompt_region(connection)
  type_ = common.prompt_choice("Type", ["private", "public"], "public")

  begin_marker = "# {}{} begin".format(prefix, region.name)
  end_marker = "# {}{} end".format(prefix, region.name)

  with open(_HOSTS_PATH) as hosts_file:
    content = hosts_file.read()

  content = re.sub("\s*{}.*{}".format(begin_marker, end_marker), "", content,
                   flags=re.DOTALL)

  connection = common.connect(region)
  mapping = sorted(_get_mapping(connection, type_ == "public"))
  content += _generate_mapping_content(begin_marker, end_marker, mapping)

  with tempfile.NamedTemporaryFile(delete=False) as temporary_file:
    temporary_file.write(content)

  subprocess.check_call([
    "sudo",
    "cp",
    temporary_file.name,
    _HOSTS_PATH
  ])
def main():
  ec2_connection = common.connect()
  region = common.prompt_region(ec2_connection)
  elb_connection = common.connect_elb_region(region.name)
  elb = common.prompt_elb(elb_connection, _DEFAULT_ELB_NAME)

  if not elb:
    print("No ELBs exist for region {}".format(region.name))
    return

  choice = common.prompt_choice("Choice", _ELB_ACTIONS, _DEFAULT_ELB_ACTION)
  while (choice != "Exit"):
    elb_info = _get_elb_info(ec2_connection, elb)
    _handle_user_choice(choice, elb, elb_info.zones,
                        elb_info.registered_instances,
                        elb_info.unregistered_instances)
    choice = common.prompt_choice("Choice", _ELB_ACTIONS, _DEFAULT_ELB_ACTION)
  return
def _add_instance(elb, elb_zones, unregistered_instances):
  """Prompts the user for an instance to add, then adds that instance
  to an ELB."""
  _pretty_print_elb_zones(elb, elb_zones)
  _pretty_print_elb_instances(elb, unregistered_instances, False)

  unreg_instances = [(inst.name, inst) for inst in unregistered_instances]
  instance_to_add = common.prompt_choice("Add", unreg_instances)
  if common.prompt_confirmation("Are you sure you want to add {}".format(
    instance_to_add.name)):
    elb.register_instances([instance_to_add.id])
    print("Instance added to ELB.")
  else:
    print("Instance was NOT added to ELB.")
def _remove_instance(elb, registered_instances):
  """Prompts the user for an instance to remove from the ELB, then removes
  it. Asks for a confirmation before actually removing it."""
  _pretty_print_elb_instances(elb, registered_instances, True)

  reg_instances = [(inst.name, inst) for inst in registered_instances]
  if len(reg_instances) == 0:
    print("Cannot remove an instance since none are registered to this ELB.")
    return

  instance_to_remove = common.prompt_choice("Remove", reg_instances)
  if common.prompt_confirmation("Are you sure you want to remove {}".format(
    instance_to_remove.name)):
    elb.deregister_instances([instance_to_remove.id])
    print("Instance removed from ELB.")
  else:
    print("Instance was NOT removed from ELB.")
def main():
  connection = common.connect()

  instance = common.prompt_instance(connection,
    "Which instance to create a RAID for")
  if not instance:
    sys.exit("Instance not found.")

  key_path = common.get_pem(instance)
  print("Using key: {}".format(key_path))

  env.host_string = instance.public_dns_name
  env.key_filename = key_path
  env.user = _USERNAME

  number_of_disks = common.prompt_choice(
    "How many EBS volumes should be in the RAID array", range(1, 11), 4)
  size_of_disks = _prompt_size()
  level = common.prompt_choice("Level", _LEVELS, _DEFAULT_LEVEL)

  common.wait_until_remote_reachable()

  possible_device_paths = ["/dev/sd" + chr(letter)
    for letter in range(ord("f"), ord("z") + 1)]

  # Remove used device paths from the list
  used_devices = _get_existing_device_paths()

  for used_device in used_devices:
    used_device_no_trailing_digits = re.sub("\d+$", "", used_device)
    if used_device_no_trailing_digits in possible_device_paths:
      possible_device_paths.remove(used_device_no_trailing_digits)

  # Offer the user a prompt of which device path to beginning with
  prefix_device_path = common.prompt_choice(
    "Volumes should be attached with numbers appended to which device path",
    possible_device_paths[:9], 1)

  # Create the disks
  new_volume_device_strings = []
  for index in range(number_of_disks):
    device_string = prefix_device_path + str(index + 1)  # example: /dev/sdf2
    new_volume_device_strings.append(device_string)
    _create_ebs_volume(connection, instance, size_of_disks, device_string)

  # Figure out where to attach the new RAID
  possible_devices_for_raid = ["/dev/md" + str(index) for index in range(10)]
  for used_device in used_devices:
    if used_device in possible_devices_for_raid:
      possible_devices_for_raid.remove(used_device)
  raid_device_path = common.prompt_choice(
      "The RAID should be built at which device path",
        possible_devices_for_raid[:5], 1)

  print("Where do you want the RAID directory to be? [Default: /mnt/raid]")
  raid_directory_path = raw_input()
  if raid_directory_path == "":
    raid_directory_path = "/mnt/raid"

  _create_raid(new_volume_device_strings, level, raid_device_path,
               raid_directory_path)