<commit_before>#!/bin/sh
#
# Quantum Networking
#
# Description: Create Virtual Networking for Quantum
# 
# Designed for "Provider Router with Private Networks" Use-Case (http://goo.gl/JTt5n)
#
# Authors : 
# <NAME> / StackOps
# <NAME> / Bouvet ASA
#
# Inspired by DevStack script
#
# Support: <EMAIL>
# License: Apache Software License (ASL) 2.0

Q_L3_CONF_FILE=/etc/quantum/l3_agent.ini

###########################
### Private Network #######
###########################
TENANT_NAME="admin"
TENANT_NETWORK_NAME="internal_net"
FIXED_RANGE="<%= fixed_range %>"
NETWORK_GATEWAY=$(ipcalc $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

set $(whatmask $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

###############################################################

# 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)
    tenant_subnet_id=$(get_id quantum subnet-create --tenant_id $tenant_id --ip_version 4 $tenant_net_id $fixed_range --gateway $network_gateway)
    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"

    ext_net_id=$(get_id quantum net-create $ext_net_name -- --router:external=True)
    quantum subnet-create --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
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)

# 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
<commit_msg>Ubuntu/quantum: fix IP calculator name<commit_after>#!/bin/sh
#
# Quantum Networking
#
# Description: Create Virtual Networking for Quantum
# 
# Designed for "Provider Router with Private Networks" Use-Case (http://goo.gl/JTt5n)
#
# Authors : 
# <NAME> / StackOps
# <NAME> / Bouvet ASA
#
# Inspired by DevStack script
#
# Support: <EMAIL>
# License: Apache Software License (ASL) 2.0

Q_L3_CONF_FILE=/etc/quantum/l3_agent.ini

###########################
### Private Network #######
###########################
TENANT_NAME="admin"
TENANT_NETWORK_NAME="internal_net"
FIXED_RANGE="<%= fixed_range %>"
NETWORK_GATEWAY=$(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

set $(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

###############################################################

# 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)
    tenant_subnet_id=$(get_id quantum subnet-create --tenant_id $tenant_id --ip_version 4 $tenant_net_id $fixed_range --gateway $network_gateway)
    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"

    ext_net_id=$(get_id quantum net-create $ext_net_name -- --router:external=True)
    quantum subnet-create --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
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)

# 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

{'fraction_non_alphanumeric': 0.19525409036732042, 'fraction_numerical': 0.008668328096218442, 'mean_word_length': 4.600728155339806, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6440652', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3241, 'n_words': 750}