Skip to content

Instantly share code, notes, and snippets.

@mauromorales
Created December 8, 2017 08:58
Show Gist options
  • Save mauromorales/e6910d7626d8ebfb72a30828961e819f to your computer and use it in GitHub Desktop.
Save mauromorales/e6910d7626d8ebfb72a30828961e819f to your computer and use it in GitHub Desktop.
require 'fog/openstack'
require 'pry'
auth_url = ENV['OS_AUTH_URL'].gsub('v3', 'v2.0')
@connection_params = {
openstack_auth_url: "#{auth_url}/tokens",
openstack_username: ENV['OS_USERNAME'],
openstack_api_key: ENV['OS_PASSWORD'],
openstack_tenant: ENV['OS_PROJECT_NAME'],
openstack_domain_id: ENV['OS_USER_DOMAIN_NAME'],
connection_options: {
ssl_verify_peer: false,
ssl_ca_file: ENV['OS_CACERT']
}
}
compute = Fog::Compute::OpenStack.new(@connection_params)
begin
#using the bosh network since we cleanup afterwards
network = Fog::Network.new(@connection_params.merge(provider: :openstack))
network_id = network.list_networks.body['networks'].select {|n| n['name'] =~ /bosh/}.first['id']
#create a security group that enables incoming traffic on VRRP, SSH and ICMP protocols
security_group = compute.security_groups.create(name: 'fog-vrrp',
description: 'Allows traffic on VRRP, SSH and ICMP')
network.create_security_group_rule(security_group.id, 'ingress', { protocol: 112 })
network.create_security_group_rule(security_group.id, 'ingress', { protocol: 'icmp' })
network.create_security_group_rule(security_group.id, 'ingress', {
protocol: 'tcp',
from_port: 22,
to_port: 22
}
)
#create port to save for vrrp
vrrp_port = network.create_port(network_id, name: 'fog-vrrp', security_group: security_group.id)
vrrp_ip_address = vrrp_port[:body]['port']['fixed_ips'][0]['ip_address']
p "vrrp_ip_address: #{vrrp_ip_address}"
#create ports that will be assigned to two other vms
vm1_port = network.create_port(network_id, name: 'fog-vm1', allowed_address_pairs: [{ip_address: vrrp_ip_address}], security_group: security_group.id)
vm2_port = network.create_port(network_id, name: 'fog-vm2', allowed_address_pairs: [{ip_address: vrrp_ip_address}], security_group: security_group.id)
p "vm1_port: #{vm1_port.body['port']}"
p "vm2_port: #{vm2_port.body['port']}"
p 'starting cleanup'
#start pry for manual testing
binding.pry
# cleanup
[ vrrp_port, vm1_port, vm2_port ].map { |port| network.delete_port(port.body['port']['id']) }
security_group.destroy
rescue => e
binding.pry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment