Vagrant Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 50: Line 50:


<syntaxhighlight lang='ruby'>
<syntaxhighlight lang='ruby'>
# frozen_string_literal: true


require_relative 'helpers'
#
# Configuration
#


# AWS authentication is assumed to be setup in shell environment
#
BASTION_HOST = '52.43.31.217'
# It is assumed that the environment was already configured with AWS authentication (.aws/credentials or AWS
# Change the default AMI ID when a new one is built, see ./README.md##bringing-up-a-single-node-k8s-cluster
# environment variables). The VM will be created in the configured account/region.
AMI_ID = ENV['AMI_ID'] || 'ami-00ae6b0dc1d1c7404'
#
AWS_KEYPAIR = 'sbox-ml-kp-01'
 
SUBNET_ID = 'subnet-0fe2965e8bf239c09' # private subnet
ENVIRONMENT_NAME = "infra-playground"
SECURITY_GROUP_ID = 'sg-041f9d51704199e98' # should only require ssh access
BASTION_HOST = '34.221.207.255'
AWS_KEYPAIR = 'infra-playground'
SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem"
SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem"
SSH_CONFIG_FILE = "#{ENV['HOME']}/.ssh/dev/minikube"
SSH_CONFIG_FILE = "#{ENV['HOME']}/.ssh/dev/infra-worker"
 
#
# This is required by AWS CLI as part of instance provisioning call. It must the ID of the private subnet
# the instance will be attached to. If using environments, this subnet should be the default private subnet
# of the environment.
#
SUBNET_ID = 'subnet-0c4f76387ae204f74'
 
#
# This is required by AWS CLI as part of instance provisioning call. It must the ID of the security
# group that will protect the access to the instance. Access must be configuring depending on the instance
# requirements. At minimum, it should allow inbound ssh access.
#
SECURITY_GROUP_ID = 'sg-0d7ee8cfa2817e422'
 
#
# The AMI of the infra-worker image created by Packer
#
AMI_ID = ENV['AMI_ID'] || 'ami-047125c4b16e4577d'
 
INSTANCE_TYPE = 'm5.4xlarge'
VOLUME_SIZE_GB = 50
 
#
# Install vagrant plugin
#
# @param: plugin type: Array[String] desc: The desired plugin to install
def ensure_plugins(plugins)
  logger = Vagrant::UI::Colored.new
  result = false
  plugins.each do |p|
    pm = Vagrant::Plugin::Manager.new(
      Vagrant::Plugin::Manager.user_plugins_file
    )
    plugin_hash = pm.installed_plugins
    next if plugin_hash.key?(p)


    result = true
    logger.warn("Installing plugin #{p}")
    pm.install_plugin(p)
  end
  if result
    logger.warn('Re-run vagrant up now that plugins are installed')
    exit
  end
end
def aws_config(name)
  value = `aws configure get #{name}`.strip
  raise ArgumentError, "aws #{name} config must be set; use 'aws configure set #{name} <value>'" if value.empty?
  value
end
def username
  ENV['USER'] || 'anonymous'
end
def hostname
  "infra-worker-#{username}"
end
#
# Create and configure the AWS instance(s)
# Create and configure the AWS instance(s)
#
Vagrant.configure('2') do |config|
Vagrant.configure('2') do |config|
   ensure_plugins(%w[vagrant-aws])
   ensure_plugins(%w[vagrant-aws])


   config.vm.define :minikube do |t|
   config.vm.define :infra_worker do |t|
   end
   end
   config.vm.hostname = hostname
   config.vm.hostname = hostname
   config.vm.box = 'dummy' # Use dummy AWS box
  #
  # Use dummy AWS box
  #
   config.vm.box = 'dummy'
   config.vm.synced_folder '.', '/vagrant', disabled: true
   config.vm.synced_folder '.', '/vagrant', disabled: true


  #
   # Specify AWS provider configuration
   # Specify AWS provider configuration
  #
   config.vm.provider 'aws' do |aws, override|
   config.vm.provider 'aws' do |aws, override|
    #
     # Specify SSH keypair to use, which should match SSH_PRIVATE_KEY
     # Specify SSH keypair to use, which should match SSH_PRIVATE_KEY
    #
     aws.keypair_name = AWS_KEYPAIR
     aws.keypair_name = AWS_KEYPAIR
     aws.instance_type = 'm5.4xlarge'
     aws.instance_type = INSTANCE_TYPE
     aws.associate_public_ip = false
     aws.associate_public_ip = false
     aws.elastic_ip = false
     aws.elastic_ip = false


    #
     # Launch configuration
     # Launch configuration
    #
     aws.ami = AMI_ID
     aws.ami = AMI_ID
     aws.subnet_id = SUBNET_ID
     aws.subnet_id = SUBNET_ID
     aws.security_groups = [SECURITY_GROUP_ID]
     aws.security_groups = [SECURITY_GROUP_ID]
     aws.block_device_mapping = [{ 'DeviceName' => '/dev/xvda', 'Ebs.VolumeSize' => 50 }]
     aws.block_device_mapping = [{ 'DeviceName' => '/dev/xvda', 'Ebs.VolumeSize' => VOLUME_SIZE_GB }]
     aws.tags = {
     aws.tags = {
       'Name' => hostname,
       'Name' => hostname,
       'Created by' => username,
       'Created by' => username,
       'Environment' => 'k8s'
       'Environment' => ENVIRONMENT_NAME
     }
     }


    #
     # Specify username and private key path
     # Specify username and private key path
    #
     config.ssh.forward_agent = true
     config.ssh.forward_agent = true
     override.ssh.username = 'ec2-user'
     override.ssh.username = 'ec2-user'
Line 101: Line 180:
   config.trigger.after [:up] do |t|
   config.trigger.after [:up] do |t|
     t.info = "Writing ssh config to #{SSH_CONFIG_FILE}"
     t.info = "Writing ssh config to #{SSH_CONFIG_FILE}"
     t.run = { path: './utils/set-ssh-config.sh', args: [SSH_CONFIG_FILE.to_s] }
     t.run = { path: '../_common_tools_and_config/bin/configure-ssh-access', args: [SSH_CONFIG_FILE.to_s] }
   end
   end
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 23:59, 16 November 2019

External

Internal

Box Operations

List

vagrant box list

Instance Operations

Start and provision the vagrant environment:

vagrant up

If property configured, bringing the machine up may create a ~/.ssh/dev/vagranthostname configuration that can be used by ssh to connect directly.

Connect to machine via SSH:

vagrant ssh

How does it know to connect to the instance it was started? More about jump box.

Status of the VM:

vagrant global-status
vagrant status [name|id]

Stops and deletes all traces of the vagrant machine:

vagrant destroy
vagrant destroy [name|id]

If the corresponding VM runs in AWS EC2, this terminates the instance.

Managing a VM in an AWS Environment

Describe the VM in a Vagrantfile

This is an example. More details about Vagrantfile syntax in:

Vagrantfile
# frozen_string_literal: true

#
# Configuration
#

#
# It is assumed that the environment was already configured with AWS authentication (.aws/credentials or AWS
# environment variables). The VM will be created in the configured account/region.
#

ENVIRONMENT_NAME = "infra-playground"
BASTION_HOST = '34.221.207.255'
AWS_KEYPAIR = 'infra-playground'
SSH_PRIVATE_KEY = "~/.ssh/#{AWS_KEYPAIR}.pem"
SSH_CONFIG_FILE = "#{ENV['HOME']}/.ssh/dev/infra-worker"

#
# This is required by AWS CLI as part of instance provisioning call. It must the ID of the private subnet
# the instance will be attached to. If using environments, this subnet should be the default private subnet
# of the environment.
#
SUBNET_ID = 'subnet-0c4f76387ae204f74'

#
# This is required by AWS CLI as part of instance provisioning call. It must the ID of the security
# group that will protect the access to the instance. Access must be configuring depending on the instance
# requirements. At minimum, it should allow inbound ssh access.
#
SECURITY_GROUP_ID = 'sg-0d7ee8cfa2817e422'

#
# The AMI of the infra-worker image created by Packer
#
AMI_ID = ENV['AMI_ID'] || 'ami-047125c4b16e4577d'

INSTANCE_TYPE = 'm5.4xlarge'
VOLUME_SIZE_GB = 50

#
# Install vagrant plugin
#
# @param: plugin type: Array[String] desc: The desired plugin to install
def ensure_plugins(plugins)
  logger = Vagrant::UI::Colored.new
  result = false
  plugins.each do |p|
    pm = Vagrant::Plugin::Manager.new(
      Vagrant::Plugin::Manager.user_plugins_file
    )
    plugin_hash = pm.installed_plugins
    next if plugin_hash.key?(p)

    result = true
    logger.warn("Installing plugin #{p}")
    pm.install_plugin(p)
  end
  if result
    logger.warn('Re-run vagrant up now that plugins are installed')
    exit
  end
end

def aws_config(name)
  value = `aws configure get #{name}`.strip
  raise ArgumentError, "aws #{name} config must be set; use 'aws configure set #{name} <value>'" if value.empty?
  value
end

def username
  ENV['USER'] || 'anonymous'
end

def hostname
  "infra-worker-#{username}"
end

#
# Create and configure the AWS instance(s)
#
Vagrant.configure('2') do |config|

  ensure_plugins(%w[vagrant-aws])

  config.vm.define :infra_worker do |t|
  end

  config.vm.hostname = hostname
  #
  # Use dummy AWS box
  #
  config.vm.box = 'dummy'
  config.vm.synced_folder '.', '/vagrant', disabled: true

  #
  # Specify AWS provider configuration
  #
  config.vm.provider 'aws' do |aws, override|
    #
    # Specify SSH keypair to use, which should match SSH_PRIVATE_KEY
    #
    aws.keypair_name = AWS_KEYPAIR
    aws.instance_type = INSTANCE_TYPE
    aws.associate_public_ip = false
    aws.elastic_ip = false

    #
    # Launch configuration
    #
    aws.ami = AMI_ID
    aws.subnet_id = SUBNET_ID
    aws.security_groups = [SECURITY_GROUP_ID]
    aws.block_device_mapping = [{ 'DeviceName' => '/dev/xvda', 'Ebs.VolumeSize' => VOLUME_SIZE_GB }]
    aws.tags = {
      'Name' => hostname,
      'Created by' => username,
      'Environment' => ENVIRONMENT_NAME
    }

    #
    # Specify username and private key path
    # 
    config.ssh.forward_agent = true
    override.ssh.username = 'ec2-user'
    override.ssh.private_key_path = SSH_PRIVATE_KEY
    override.ssh.proxy_command = "ssh -o ExitOnForwardFailure=yes -W %h:%p -i #{override.ssh.private_key_path} %r@#{BASTION_HOST}"
  end

  config.trigger.after [:up] do |t|
    t.info = "Writing ssh config to #{SSH_CONFIG_FILE}"
    t.run = { path: '../_common_tools_and_config/bin/configure-ssh-access', args: [SSH_CONFIG_FILE.to_s] }
  end
end