Merge "(FUEL-238) Quantum: custom providers(net/subnet/router) implementation"

This commit is contained in:
Victor Galkin 2013-02-15 02:08:27 +04:00 committed by Gerrit Code Review
commit f1002aa6d2
12 changed files with 710 additions and 368 deletions

View File

@ -0,0 +1,149 @@
#
# Quantum common functions
#
require 'puppet/util/inifile'
require 'tempfile'
class Puppet::Provider::Quantum < Puppet::Provider
def self.quantum_credentials
@quantum_credentials ||= get_quantum_credentials
end
def self.get_quantum_credentials
# if quantum_file and quantum_file['filter:authtoken'] and
# quantum_file['filter:authtoken']['auth_host'] and
# quantum_file['filter:authtoken']['auth_port'] and
# quantum_file['filter:authtoken']['auth_protocol'] and
# quantum_file['filter:authtoken']['admin_tenant_name'] and
# quantum_file['filter:authtoken']['admin_user'] and
# quantum_file['filter:authtoken']['admin_password']
if quantum_file and quantum_file['DEFAULT'] and
quantum_file['DEFAULT']['auth_url'] and
quantum_file['DEFAULT']['admin_tenant_name'] and
quantum_file['DEFAULT']['admin_user'] and
quantum_file['DEFAULT']['admin_password']
q = {}
# q['auth_host'] = quantum_file['filter:authtoken']['auth_host'].strip
# q['auth_port'] = quantum_file['filter:authtoken']['auth_port'].strip
# q['auth_protocol'] = quantum_file['filter:authtoken']['auth_protocol'].strip
q['auth_url'] = quantum_file['DEFAULT']['auth_url'].strip
q['admin_tenant_name'] = quantum_file['DEFAULT']['admin_tenant_name'].strip
q['admin_user'] = quantum_file['DEFAULT']['admin_user'].strip
q['admin_password'] = quantum_file['DEFAULT']['admin_password'].strip
return q
else
# raise(Puppet::Error, 'File: /etc/quantum/api-paste.ini does not contain all required sections.')
raise(Puppet::Error, 'File: /etc/quantum/l3_agent.ini does not contain all required sections.')
end
end
def quantum_credentials
self.class.quantum_credentials
end
def self.auth_endpoint
@auth_endpoint ||= get_auth_endpoint
end
def self.get_auth_endpoint
q = quantum_credentials
# "#{q['auth_protocol']}://#{q['auth_host']}:#{q['auth_port']}/v2.0/"
q['auth_url']
end
def self.quantum_file
return @quantum_file if @quantum_file
# quantum_apipaste = '/etc/quantum/api-paste.ini'
# tf_apipaste = Tempfile.new('api-paste-ini-')
#
# conf_opt = File.open(quantum_apipaste).read
# inside = false
# conf_opt.each do |line|
# if line.strip == '[filter:authtoken]'
# inside = true
# elsif inside and line.match(/^\s*\[/)
# inside = false
# end
# tf_apipaste.print line if inside
# end
# tf_apipaste.flush
# @quantum_file = Puppet::Util::IniConfig::File.new
# @quantum_file.read(tf_apipaste.path)
# tf_apipaste.close
@quantum_file = Puppet::Util::IniConfig::File.new
@quantum_file.read('/etc/quantum/l3_agent.ini')
@quantum_file
end
# def self.quantum_hash
# @quantum_hash ||= build_quantum_hash
# end
# def quantum_hash
# self.class.quantum_hash
# end
def self.auth_quantum(*args)
begin
q = quantum_credentials
# args_str = args.join '` '
# notice("ARGS: #{args_str}\n")
quantum('--os-tenant-name', q['admin_tenant_name'], '--os-username', q['admin_user'], '--os-password', q['admin_password'], '--os-auth-url', auth_endpoint, args)
rescue Exception => e
# Will probably add conditions later
raise(e)
end
end
def auth_quantum(*args)
self.class.auth_quantum(args)
end
private
# def self.list_quantum_objects
# ids = []
# (auth_quantum('index').split("\n")[2..-1] || []).collect do |line|
# ids << line.split[0]
# end
# return ids
# end
# def self.get_quantum_attr(id, attr)
# (auth_quantum('show', id).split("\n") || []).collect do |line|
# if line =~ /^#{attr}:/
# return line.split(': ')[1..-1]
# end
# end
# end
def self.list_keystone_tenants
q = quantum_credentials
tenants_id = {}
keystone(
'--os-tenant-name', q['admin_tenant_name'],
'--os-username', q['admin_user'],
'--os-password', q['admin_password'],
'--os-auth-url', auth_endpoint,
#'tenant-list').grep(/\|\s+#{tenant_name}\s+\|/) { |tenant| tenant.split[1] }.to_s
'tenant-list').split("\n")[3..-2].collect do |tenant|
tenants_id[tenant.split[3]] = tenant.split[1]
end
tenants_id
end
end

View File

@ -0,0 +1,83 @@
# Load the Quantum provider library to help
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/quantum')
Puppet::Type.type(:quantum_net).provide(
:quantum,
:parent => Puppet::Provider::Quantum
) do
desc "Manage quantum network"
optional_commands :quantum => 'quantum'
optional_commands :keystone => 'keystone'
# I need to setup caching and what-not to make this lookup performance not suck
def self.instances
network_list = auth_quantum("net-list")
return [] if network_list.chomp.empty?
network_list.split("\n")[3..-2].collect do |net|
new(:name => net.split[3])
end
end
def self.tenant_id
@tenant_id ||= get_tenants_id
end
def tenant_id
self.class.tenant_id
end
def create
# quantum net-create --tenant_id $tenant_id $tenant_network_name --provider:network_type vlan --provider:physical_network physnet2 --provider:segmentation_id 501)
# quantum net-create $ext_net_name -- --router:external=True --tenant_id $tenant_id --provider:network_type flat)
optional_opts = []
{
:router_ext => '--router:external',
:shared => '--shared',
:network_type => '--provider:network_type',
:physnet => '--provider:physical_network',
:segment_id => '--provider:segmentation_id'
}.each do |param, opt|
if @resource[param]
optional_opts.push(opt).push(@resource[param])
end
end
auth_quantum('net-create',
'--tenant_id', tenant_id[@resource[:tenant]],
@resource[:name],
optional_opts
)
end
def exists?
begin
network_list = auth_quantum("net-list")
return network_list.split("\n")[3..-2].detect do |net|
# n =~ /^(\S+)\s+(#{@resource[:network].split('/').first})/
net.split[3] == @resource[:name]
end
rescue
return false
end
end
def destroy
auth_quantum("net-delete", @resource[:name])
end
private
def self.get_id(net_info)
# ruby 1.8.x specific
net_info.grep(/ id /).to_s.split[3]
end
def self.get_tenants_id
# notice("*** GET_TENANT_ID")
list_keystone_tenants
end
end

View File

@ -0,0 +1,95 @@
# Load the Quantum provider library to help
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/quantum')
Puppet::Type.type(:quantum_router).provide(
:quantum,
:parent => Puppet::Provider::Quantum
) do
desc "Manage quantum router"
optional_commands :quantum => 'quantum'
optional_commands :keystone => 'keystone'
# I need to setup caching and what-not to make this lookup performance not suck
def self.instances
router_list = auth_quantum("router-list")
return [] if router_list.chomp.empty?
router_list.split("\n")[3..-2].collect do |net|
new(:name => net.split[3])
end
end
def self.tenant_id
@tenant_id ||= get_tenants_id
end
def tenant_id
self.class.tenant_id
end
def create
admin_state = []
if @resource[:admin_state] and @resource[:admin_state].downcase == 'down'
admin_state.push('--admin-state-down')
end
router_info = auth_quantum('router-create',
'--tenant_id', tenant_id[@resource[:tenant]],
admin_state,
@resource[:name]
)
# notice("ROUTER: #{router_info}")
# add an internal networks interfaces to a router
@resource[:int_subnets].each do |subnet|
auth_quantum('router-interface-add',
@resource[:name],
subnet
)
end
#Set an gateway interface to the specified external network
if @resource[:ext_net]
auth_quantum('router-gateway-set',
@resource[:name],
@resource[:ext_net]
)
# update router_id option
router_id = self.class.get_id(router_info)
ql3a_conf = Puppet::Type.type(:quantum_l3_agent_config).new(:name => "DEFAULT/router_id", :value => router_id)
ql3a_conf.provider.create
end
end
def exists?
begin
router_list = auth_quantum("router-list")
return router_list.split("\n")[3..-2].detect do |router|
router.split[3] == @resource[:name]
end
rescue
return false
end
end
def destroy
auth_quantum("router-delete", @resource[:name])
end
private
def self.get_id(router_info)
router_info.split("\n").grep(/\bid/).to_s.split[3]
end
def self.get_tenants_id
# notice("*** GET_TENANT_ID")
list_keystone_tenants
end
end

View File

@ -0,0 +1,98 @@
# Load the Quantum provider library to help
require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/quantum')
Puppet::Type.type(:quantum_subnet).provide(
:quantum,
:parent => Puppet::Provider::Quantum
) do
desc "Manage quantum subnet/networks"
optional_commands :quantum => 'quantum'
optional_commands :keystone => 'keystone'
# I need to setup caching and what-not to make this lookup performance not suck
def self.instances
network_list = auth_quantum("subnet-list")
return [] if network_list.chomp.empty?
network_list.split("\n")[3..-2].collect do |net|
new(:name => net.split[3])
end
end
def self.tenant_id
@tenant_id ||= get_tenants_id
end
def tenant_id
self.class.tenant_id
end
def create
# tenant_subnet_id=$(get_id quantum subnet-create --tenant_id $tenant_id --ip_version 4 $tenant_net_id $fixed_range --gateway $network_gateway)
# quantum subnet-create --tenant-id $tenant --name subnet01 net01 192.168.101.0/24
# quantum subnet-create --tenant-id $tenant --name pub_subnet01 --gateway 10.0.1.254 public01 10.0.1.0/24 --enable_dhcp False
# --allocation-pool start=$pool_floating_start,end=$pool_floating_end
# --dns_nameservers list=true 8.8.8.8
ip_opts = []
{
:ip_version => '--ip-version',
:gateway => '--gateway',
:alloc_pool => '--allocation-pool',
}.each do |param, opt|
if @resource[param]
ip_opts.push(opt).push(@resource[param])
end
end
proto_opts = []
{
:enable_dhcp => '--enable_dhcp',
:nameservers => ['--dns_nameservers', 'list=true']
}.each do |param, opt|
if @resource[param]
proto_opts.push(opt).push(@resource[param])
end
end
auth_quantum('subnet-create',
'--tenant-id', tenant_id[@resource[:tenant]],
'--name', @resource[:name],
ip_opts,
@resource[:network],
@resource[:cidr],
'--', proto_opts
)
end
def exists?
begin
network_list = auth_quantum("subnet-list")
return network_list.split("\n")[3..-2].detect do |net|
# n =~ /^(\S+)\s+(#{@resource[:network].split('/').first})/
net.split[3] == @resource[:name]
end
rescue
return false
end
end
def destroy
auth_quantum("subnet-delete", @resource[:name])
end
private
def self.get_id(subnet_info)
# ruby 1.8.x specific
subnet_info.grep(/ id /).to_s.split[3]
end
def self.get_tenants_id
# notice("*** GET_TENANT_ID")
list_keystone_tenants
end
end

View File

@ -0,0 +1,48 @@
Puppet::Type.newtype(:quantum_net) do
@doc = "Manage creation/deletion of quantum networks"
ensurable
newparam(:name, :namevar => true) do
desc 'The network name'
end
newparam(:tenant) do
desc "The tenant that the network is associated with"
defaultto "admin"
end
newparam(:network_type) do
desc 'Network type'
defaultto "gre"
end
newparam(:physnet) do
desc 'Private physical network name'
end
newparam(:router_ext) do
# defaultto "False"
end
newparam(:shared) do
# defaultto "False"
end
newparam(:segment_id) do
end
# validate do
# raise(Puppet::Error, 'Label must be set') unless self[:label]
# end
# Require the Quantum service to be running
# autorequire(:service) do
# ['quantum-server']
# end
autorequire(:package) do
['python-quantumclient']
end
end

View File

@ -0,0 +1,34 @@
Puppet::Type.newtype(:quantum_router) do
@doc = "Manage creation/deletion of quantum routers"
ensurable
newparam(:name, :namevar => true) do
desc 'The router name'
end
newparam(:tenant) do
desc "The tenant that the router is associated with"
defaultto "admin"
end
newparam(:admin_state) do
# defaultto "up"
end
newparam(:int_subnets) do
desc "Add an internal networks interfaces to a router"
defaultto ""
end
newparam(:ext_net) do
desc "Set an gateway interface to the specified external network"
end
# Require the Quantum service to be running
autorequire(:package) do
['python-quantumclient']
end
end

View File

@ -0,0 +1,56 @@
Puppet::Type.newtype(:quantum_subnet) do
@doc = "Manage creation/deletion of quantum subnet/networks"
ensurable
newparam(:name, :namevar => true) do
desc 'The subnet name'
end
newparam(:tenant) do
desc "The tenant that the network is associated with"
defaultto "admin"
end
newparam(:network) do
desc 'Network id or name this subnet belongs to'
end
newparam(:cidr) do
desc 'CIDR of subnet to create'
end
newparam(:ip_version) do
defaultto 4
end
newparam(:gateway) do
end
newparam(:enable_dhcp) do
defaultto "True"
end
newparam(:alloc_pool) do
desc 'Allocation pool IP addresses'
end
newparam(:nameservers) do
desc 'DNS name servers used by hosts'
end
# validate do
# raise(Puppet::Error, 'Label must be set') unless self[:label]
# end
# Require the Quantum service to be running
# autorequire(:service) do
# ['quantum-server']
# end
autorequire(:package) do
['python-quantumclient']
end
end

View File

@ -41,6 +41,11 @@ class quantum::agents::l3 (
$l3_agent_package = $::quantum::params::package_name
}
package { 'python-keystoneclient':
ensure => present,
before => Package[$l3_agent_package],
}
Package[$l3_agent_package] -> Quantum_l3_agent_config<||>
Quantum_config<||> ~> Service['quantum-l3']
Quantum_l3_agent_config<||> ~> Service['quantum-l3']
@ -65,36 +70,83 @@ class quantum::agents::l3 (
if $create_networks {
Vs_bridge<||> -> Exec['create-networks']
Vs_bridge<||> -> Quantum::Network::Setup<||>
package { 'python-keystoneclient':
ensure => present,
before => Exec['create-networks']
$segment_id = regsubst($segment_range, ':\d+', '')
if $tenant_network_type == 'gre' {
$internal_physical_network = undef
$external_physical_network = undef
$external_network_type = $tenant_network_type
$external_segment_id = $segment_id + 1
} else {
$internal_physical_network = 'physnet2'
$external_physical_network = 'physnet1'
$external_network_type = 'flat'
$external_segment_id = undef
}
# create external/internal networks
file { '/tmp/quantum-networking.sh':
mode => 740,
owner => root,
content => template("quantum/quantum-networking.sh.${::osfamily}.erb"),
#require => Service['quantum-l3'],
notify => Exec['create-networks'],
if empty($ext_ipinfo) {
$floating_net = regsubst($floating_range, '(.+\.)\d+/\d+', '\1')
$floating_host = regsubst($floating_range, '.+\.(\d+)/\d+', '\1') + 1
$external_gateway = "${floating_net}${floating_host}"
$external_alloc_pool = undef
} else {
$external_gateway = $ext_ipinfo['public_net_router']
$external_alloc_pool = [$ext_ipinfo['pool_start'], $ext_ipinfo['pool_end']]
}
package { 'cidr-package':
name => $::quantum::params::cidr_package,
ensure => $package_ensure,
before => Exec['create-networks']
}
exec { 'create-networks':
command => '/tmp/quantum-networking.sh',
# path => '/usr/bin',
quantum::network::setup { 'net04':
physnet => $internal_physical_network,
network_type => $tenant_network_type,
segment_id => $segment_id,
subnet_name => 'subnet04',
subnet_cidr => $fixed_range,
nameservers => '8.8.4.4',
}
quantum::network::setup { 'net04_ext':
tenant_name => 'services',
physnet => $external_physical_network,
network_type => $external_network_type,
segment_id => $external_segment_id, # undef,
router_external => 'True',
subnet_name => 'subnet04_ext',
subnet_cidr => $floating_range,
subnet_gw => $external_gateway, # undef,
alloc_pool => $external_alloc_pool, # undef,
enable_dhcp => 'False', # 'True',
}
quantum::network::provider_router { 'router04':
router_subnets => 'subnet04', # undef,
router_extnet => 'net04_ext', # undef,
notify => Service['quantum-l3'],
}
# turn down the current default route metric priority
$update_default_route_metric = "/sbin/route del default gw ${::defaultroute};\
/sbin/route add default gw ${::defaultroute} dev ${::defaultroute_interface} metric 100"
exec { 'update_default_route_metric':
command => $update_default_route_metric,
returns => [0, 7],
subscribe => Package[$l3_agent_package],
before => Service['quantum-l3'],
refreshonly => true,
logoutput => true,
require => Package[$l3_agent_package],
notify => Service['quantum-l3'],
}
exec { 'settle-down-default-route':
command => "/bin/ping -q -W2 -c1 ${external_gateway}",
subscribe => Exec['update_default_route_metric'],
require => Service['quantum-l3'],
logoutput => 'on_failure',
refreshonly => true,
try_sleep => 3,
tries => 5,
}
}
} else {
$ensure = 'stopped'
@ -121,26 +173,4 @@ class quantum::agents::l3 (
require => [Package[$l3_agent_package], Class['quantum'], Service['quantum-plugin-ovs-service']],
}
# turn down the current default route metric priority
$update_default_route_metric = "/sbin/route del default gw ${::defaultroute};\
/sbin/route add default gw ${::defaultroute} dev ${::defaultroute_interface} metric 100"
exec { 'update_default_route_metric':
command => $update_default_route_metric,
returns => [0, 7],
subscribe => Package[$l3_agent_package],
before => Service['quantum-l3'],
refreshonly => true,
}
exec { 'wait-for-default-route':
command => "/bin/ping -q -c1 ${::defaultroute}",
subscribe => Exec['update_default_route_metric'],
require => Service['quantum-l3'],
logoutput => 'on_failure',
refreshonly => true,
try_sleep => 3,
tries => 5,
}
}

View File

@ -0,0 +1,22 @@
#
# Use Case: Provider Router with Private Networks
#
define quantum::network::provider_router (
$tenant_name = 'admin',
$router_subnets = undef,
$router_extnet = undef,
$router_state = undef,
) {
Quantum_subnet<||> -> Quantum_router<||>
# create router
quantum_router { $title:
ensure => present,
tenant => $tenant_name,
int_subnets => $router_subnets,
ext_net => $router_extnet,
admin_state => $admin_state,
}
}

View File

@ -0,0 +1,49 @@
#
# Use Case: Provider Router with Private Networks
#
define quantum::network::setup (
$tenant_name = 'admin',
$physnet = undef,
$network_type = 'gre',
$segment_id = undef,
$router_external = 'False',
$subnet_name = 'subnet1',
$subnet_cidr = '10.47.27.0/24',
$subnet_gw = undef,
$alloc_pool = undef,
$enable_dhcp = 'True',
$nameservers = undef,
) {
Quantum_net<||> -> Quantum_subnet<||>
# create network
quantum_net { $title:
ensure => present,
tenant => $tenant_name,
physnet => $physnet,
network_type => $network_type,
segment_id => $segment_id,
router_ext => $router_external,
}
# validate allocation pool
if $alloc_pool and size($alloc_pool) == 2 {
$alloc_pool_str = "start=${alloc_pool[0]},end=${alloc_pool[1]}"
} else {
$alloc_pool_str = undef
}
# create subnet
quantum_subnet { $subnet_name:
ensure => present,
tenant => $tenant_name,
cidr => $subnet_cidr,
network => $title,
gateway => $subnet_gw,
alloc_pool => $alloc_pool_str,
enable_dhcp => $enable_dhcp,
nameservers => $nameservers,
}
}

View File

@ -1,161 +0,0 @@
#!/bin/bash
#
# Quantum Networking
#
# Description: Create Virtual Networking for Quantum
#
# Designed for "Provider Router with Private Networks" Use-Case (http://goo.gl/JTt5n)
#
# Authors :
# Emilien Macchi / StackOps
# Endre Karlson / Bouvet ASA
#
# Inspired by DevStack script
#
# Support: openstack@lists.launchpad.net
# License: Apache Software License (ASL) 2.0
Q_L3_CONF_FILE=/etc/quantum/l3_agent.ini
###########################
### Private Network #######
###########################
TENANT_NAME="admin"
TENANT_NETWORK_NAME="int_net"
FIXED_RANGE="<%= fixed_range %>"
NETWORK_GATEWAY=$(TERM=vt100 ipcalc -nb $FIXED_RANGE | awk '/^HostMin/ {print $2}')
###########################
##############################################################
### Public Network ###########################################
##############################################################
# Provider Router Informations
PROV_ROUTER_NAME="provider-router"
# Name of External Network (Don't change it)
EXT_NET_NAME="ext_net"
# External Network addressing
EXT_NET_CIDR="<%= floating_range %>"
EXT_NET_LEN=${EXT_NET_CIDR#*/}
# External bridge that we have configured into l3_agent.ini (Don't change it)
EXT_NET_BRIDGE=br-ex
<% if ext_ipinfo.empty? -%>
set $(TERM=vt100 ipcalc -nb $EXT_NET_CIDR | awk '/^(HostMin|HostMax)/ {print $2}')
LAST_OCTET=${1##*.}
# IP of external bridge (br-ex) :
EXT_GW_IP=${1/%$LAST_OCTET/$(($LAST_OCTET + 1))}
# IP of the Public Network Gateway (i.e.external router)
EXT_NET_GATEWAY=$1
# Floating IP range
POOL_FLOATING_START=${1/%$LAST_OCTET/$(($LAST_OCTET + 2))}
POOL_FLOATING_END=$2
<% else -%>
EXT_GW_IP=<%= ext_ipinfo['ext_bridge']%>
EXT_NET_GATEWAY=<%= ext_ipinfo['public_net_router']%>
POOL_FLOATING_START=<%= ext_ipinfo['pool_start']%>
POOL_FLOATING_END=<%= ext_ipinfo['pool_end']%>
<% end -%>
###############################################################
# Function to get ID :
get_id () {
echo `$@ | awk '/ id / { print $4 }'`
}
# Create the Tenant private network :
create_net() {
local tenant_name="$1"
local tenant_network_name="$2"
local prov_router_name="$3"
local fixed_range="$4"
local network_gateway="$5"
local tenant_id=$(keystone tenant-list | grep " $tenant_name " | awk '{print $2}')
tenant_net_id=$(get_id quantum net-create --tenant_id $tenant_id $tenant_network_name --provider:network_type <%= tenant_network_type %><% if tenant_network_type == 'vlan' -%> --provider:physical_network physnet2<% end -%> --provider:segmentation_id <%= segment_range.split(':')[0].to_i + 1 %>)
tenant_subnet_id=$(get_id quantum subnet-create --tenant_id $tenant_id --ip_version 4 $tenant_net_id $fixed_range --gateway $network_gateway --dns_nameservers list=true 8.8.8.8)
prov_router_id=$(get_id quantum router-create --tenant_id $tenant_id $prov_router_name)
quantum router-interface-add $prov_router_id $tenant_subnet_id
}
# Create External Network :
create_ext_net() {
local ext_net_name="$1"
local ext_net_cidr="$2"
local ext_net_gateway="$4"
local pool_floating_start="$5"
local pool_floating_end="$6"
local tenant_id=$(keystone tenant-list | grep " services " | awk '{print $2}')
ext_net_id=$(get_id quantum net-create $ext_net_name -- --router:external=True --tenant_id $tenant_id <% if tenant_network_type == 'vlan' -%>--provider:network_type flat --provider:physical_network physnet1<% else -%>--provider:network_type gre --provider:segmentation_id <%= segment_range.split(':')[0].to_i + 2 %><% end -%>)
quantum subnet-create --tenant_id $tenant_id --ip_version 4 --allocation-pool start=$pool_floating_start,end=$pool_floating_end \
--gateway $ext_net_gateway $ext_net_id $ext_net_cidr -- --enable_dhcp=False
}
# Connect the Tenant Virtual Router to External Network :
connect_providerrouter_to_externalnetwork() {
local prov_router_name="$1"
local ext_net_name="$2"
router_id=$(get_id quantum router-show $prov_router_name)
ext_net_id=$(get_id quantum net-show $ext_net_name)
quantum router-gateway-set $router_id $ext_net_id
}
# Get an option from an INI file
# iniget config-file section option
function iniget() {
local file=$1
local section=$2
local option=$3
local line
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
echo ${line#*=}
}
# Set an option in an INI file
# iniset config-file section option value
function iniset() {
local file=$1
local section=$2
local option=$3
local value=$4
if ! grep -q "^\[$section\]" $file; then
# Add section at the end
echo -e "\n[$section]" >>$file
fi
if [[ -z "$(iniget $file $section $option)" ]]; then
# Add it
sed -i -e "/^\[$section\]/ a\\
$option = $value
" $file
else
# Replace it
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
fi
}
source ~/openrc
# checking for the router/networks existence
quantum router-show $PROV_ROUTER_NAME &> /dev/null && exit 0
create_net $TENANT_NAME $TENANT_NETWORK_NAME $PROV_ROUTER_NAME $FIXED_RANGE $NETWORK_GATEWAY
create_ext_net $EXT_NET_NAME $EXT_NET_CIDR $EXT_NET_BRIDGE $EXT_NET_GATEWAY $POOL_FLOATING_START $POOL_FLOATING_END
connect_providerrouter_to_externalnetwork $PROV_ROUTER_NAME $EXT_NET_NAME
iniset $Q_L3_CONF_FILE DEFAULT router_id $(get_id quantum router-show $PROV_ROUTER_NAME)
## ovs-vsctl -- --may-exist add-br $EXT_NET_BRIDGE
# Configure br-ex to reach public network :
## ip addr flush dev $EXT_NET_BRIDGE
## ip addr add $EXT_GW_IP/$EXT_NET_LEN dev $EXT_NET_BRIDGE
## ip link set $EXT_NET_BRIDGE up

View File

@ -1,161 +0,0 @@
#!/bin/bash
#
# Quantum Networking
#
# Description: Create Virtual Networking for Quantum
#
# Designed for "Provider Router with Private Networks" Use-Case (http://goo.gl/JTt5n)
#
# Authors :
# Emilien Macchi / StackOps
# Endre Karlson / Bouvet ASA
#
# Inspired by DevStack script
#
# Support: openstack@lists.launchpad.net
# License: Apache Software License (ASL) 2.0
Q_L3_CONF_FILE=/etc/quantum/l3_agent.ini
###########################
### Private Network #######
###########################
TENANT_NAME="admin"
TENANT_NETWORK_NAME="int_net"
FIXED_RANGE="<%= fixed_range %>"
NETWORK_GATEWAY=$(whatmask $FIXED_RANGE | awk '/^First/ {print $7}')
###########################
##############################################################
### Public Network ###########################################
##############################################################
# Provider Router Informations
PROV_ROUTER_NAME="provider-router"
# Name of External Network (Don't change it)
EXT_NET_NAME="ext_net"
# External Network addressing
EXT_NET_CIDR="<%= floating_range %>"
EXT_NET_LEN=${EXT_NET_CIDR#*/}
# External bridge that we have configured into l3_agent.ini (Don't change it)
EXT_NET_BRIDGE=br-ex
<% if ext_ipinfo.empty? -%>
set $(whatmask $EXT_NET_CIDR | awk '/^(First|Last)/ {print $7}')
LAST_OCTET=${1##*.}
# IP of external bridge (br-ex) :
EXT_GW_IP=${1/%$LAST_OCTET/$(($LAST_OCTET + 1))}
# IP of the Public Network Gateway (i.e.external router)
EXT_NET_GATEWAY=$1
# Floating IP range
POOL_FLOATING_START=${1/%$LAST_OCTET/$(($LAST_OCTET + 2))}
POOL_FLOATING_END=$2
<% else -%>
EXT_GW_IP=<%= ext_ipinfo['ext_bridge']%>
EXT_NET_GATEWAY=<%= ext_ipinfo['public_net_router']%>
POOL_FLOATING_START=<%= ext_ipinfo['pool_start']%>
POOL_FLOATING_END=<%= ext_ipinfo['pool_end']%>
<% end -%>
###############################################################
# Function to get ID :
get_id () {
echo `$@ | awk '/ id / { print $4 }'`
}
# Create the Tenant private network :
create_net() {
local tenant_name="$1"
local tenant_network_name="$2"
local prov_router_name="$3"
local fixed_range="$4"
local network_gateway="$5"
local tenant_id=$(keystone tenant-list | grep " $tenant_name " | awk '{print $2}')
tenant_net_id=$(get_id quantum net-create --tenant_id $tenant_id $tenant_network_name --provider:network_type <%= tenant_network_type %><% if tenant_network_type == 'vlan' -%> --provider:physical_network physnet2<% end -%> --provider:segmentation_id <%= segment_range.split(':')[0].to_i + 1 %>)
tenant_subnet_id=$(get_id quantum subnet-create --tenant_id $tenant_id --ip_version 4 $tenant_net_id $fixed_range --gateway $network_gateway --dns_nameservers list=true 8.8.8.8)
prov_router_id=$(get_id quantum router-create --tenant_id $tenant_id $prov_router_name)
quantum router-interface-add $prov_router_id $tenant_subnet_id
}
# Create External Network :
create_ext_net() {
local ext_net_name="$1"
local ext_net_cidr="$2"
local ext_net_gateway="$4"
local pool_floating_start="$5"
local pool_floating_end="$6"
local tenant_id=$(keystone tenant-list | grep " services " | awk '{print $2}')
ext_net_id=$(get_id quantum net-create $ext_net_name -- --router:external=True --tenant_id $tenant_id <% if tenant_network_type == 'vlan' -%>--provider:network_type flat --provider:physical_network physnet1<% else -%>--provider:network_type gre --provider:segmentation_id <%= segment_range.split(':')[0].to_i + 2 %><% end -%>)
quantum subnet-create --tenant_id $tenant_id --ip_version 4 --allocation-pool start=$pool_floating_start,end=$pool_floating_end \
--gateway $ext_net_gateway $ext_net_id $ext_net_cidr -- --enable_dhcp=False
}
# Connect the Tenant Virtual Router to External Network :
connect_providerrouter_to_externalnetwork() {
local prov_router_name="$1"
local ext_net_name="$2"
router_id=$(get_id quantum router-show $prov_router_name)
ext_net_id=$(get_id quantum net-show $ext_net_name)
quantum router-gateway-set $router_id $ext_net_id
}
# Get an option from an INI file
# iniget config-file section option
function iniget() {
local file=$1
local section=$2
local option=$3
local line
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
echo ${line#*=}
}
# Set an option in an INI file
# iniset config-file section option value
function iniset() {
local file=$1
local section=$2
local option=$3
local value=$4
if ! grep -q "^\[$section\]" $file; then
# Add section at the end
echo -e "\n[$section]" >>$file
fi
if [[ -z "$(iniget $file $section $option)" ]]; then
# Add it
sed -i -e "/^\[$section\]/ a\\
$option = $value
" $file
else
# Replace it
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
fi
}
source ~/openrc
# checking for the router/networks existence
quantum router-show $PROV_ROUTER_NAME &> /dev/null && exit 0
create_net $TENANT_NAME $TENANT_NETWORK_NAME $PROV_ROUTER_NAME $FIXED_RANGE $NETWORK_GATEWAY
create_ext_net $EXT_NET_NAME $EXT_NET_CIDR $EXT_NET_BRIDGE $EXT_NET_GATEWAY $POOL_FLOATING_START $POOL_FLOATING_END
connect_providerrouter_to_externalnetwork $PROV_ROUTER_NAME $EXT_NET_NAME
iniset $Q_L3_CONF_FILE DEFAULT router_id $(get_id quantum router-show $PROV_ROUTER_NAME)
## ovs-vsctl -- --may-exist add-br $EXT_NET_BRIDGE
# Configure br-ex to reach public network :
## ip addr flush dev $EXT_NET_BRIDGE
## ip addr add $EXT_GW_IP/$EXT_NET_LEN dev $EXT_NET_BRIDGE
## ip link set $EXT_NET_BRIDGE up