mitchellh / vagrant-aws

Use Vagrant to manage your EC2 and VPC instances.
MIT License
2.61k stars 572 forks source link

Fetch the public IP address to pass it to capistrano #215

Open amrnt opened 10 years ago

amrnt commented 10 years ago

I'm trying to deploy my application on a machine and the database on another machine.

I set up both machines with config like this:

    machine.vm.provider :aws do |aws, override|
      override.vm.box = 'dummy'
      override.vm.box_url = 'https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box'

      aws.access_key_id = ENV['AWS_ACCESS_KEY']
      aws.secret_access_key = ENV['AWS_SECRET_KEY']
      aws.region =  ENV['AWS_REGION']

      aws.private_ip_address = '10.0.0.101'
      aws.subnet_id = "subnet-xxx"
      aws.elastic_ip = true
      aws.security_groups = ['sg-xyx']

      aws.ami = 'ami-709ba735'
      aws.instance_type = 't1.micro'
      aws.keypair_name = ENV['AWS_SSH_KEY_ID']

      override.ssh.username = 'ubuntu'
      override.ssh.private_key_path = ENV['AWS_KEY_PAIR']
    end

How can I tell Capistrano the IP address of the machines?

OR, can I setup both machines through an amazon machine and assign private IPs to setup the other machines without public IPs?

markrebec commented 10 years ago

I currently use something like this in a config/deploy/vagrant.rb to deploy to an EC2 instance I've brought up with vagrant (I have some slightly more complicated configs for multi-machine apps).

require File.expand_path('../../../lib/cockpit/version', __FILE__)

vagrant_status = `sudo -u \`whoami\` vagrant status 2>&1`
if vagrant_status.match(/cockpit-web-precise64-#{Cockpit::BOX_VERSION}\s*running/)

  ssh_config = `sudo -u \`whoami\` vagrant ssh-config 2>&1`
  raise ssh_config unless ssh_config.match(/^Host/)

  vagrant_ip = ssh_config.match(/HostName\s+([a-zA-Z0-9\.]*)$/m)[1]
  vagrant_port = ssh_config.match(/Port\s+(\d*)$/m)[1]
  vagrant_user = ssh_config.match(/User\s+([a-zA-Z0-9_]*)$/m)[1]

  server "#{vagrant_ip}:#{vagrant_port}", user: vagrant_user, roles: %w{web app}
end

It's dirty, I haven't had a chance to clean it up much yet, but it gets the job done. The use of sudo is to execute vagrant within a clean shell, because vagrant will complain about bundler if it's executed from within a rake/capistrano process (I assume because that's all already running within a bundled environment).

Just for some context: I then take the provisioned (with vagrant) and deployed (with capistrano) instance and create an AMI, then use that AMI to spin up instances in my auto-scaling groups and/or load balancers. My staging, production, etc. capistrano stage configs then build their list of servers by querying all the instances running in those groups (or our load balancers in some cases, for apps that aren't auto-scaled).

Edit: You'll want to pass the machine name to the vagrant ssh-config MACHINE_NAME command if you're spinning up multiple machines. I'm not doing it in the example above because it's the only configured machine for that app, so vagrant ssh-config just returns it's config.

rtyler commented 10 years ago

@amrnt What I'm considering adding is a JSON file in .vagrant that lists information about provisioned machines, e.g.:

{
  "hosts":
    [
      {
        "provisioned": true,
        "public_hostname": "foo.com",
        "internal_hostname": "foo.internal",
        "elastic_ip" : "123",
        "tags" : {}
      }
    ]
}

Would this meet your needs?

akutz commented 8 years ago

Hi @rtyler,

What became of this idea? I would sure like to be able to grab the public and private IPs as well.

avkonst commented 7 years ago

+1