当前位置: 代码迷 >> 综合 >> OpenStack Neutron服务安装脚本
  详细解决方案

OpenStack Neutron服务安装脚本

热度:38   发布时间:2023-12-09 03:47:22.0

Controller

#!/bin/bashreadonly NEUTRON_DBPASS=openstack
readonly NEUTRON_PASS=openstack # 在keystone服务中的密码
readonly METADATA_SECRET=openstackreadonly RABBIT_PASS=openstack
readonly NOVA_PASS=openstack
#Replace PROVIDER_INTERFACE_NAME with the name of the underlying provider physical network interface
readonly PROVIDER_INTERFACE_NAME=eth1
#Replace OVERLAY_INTERFACE_IP_ADDRESS with the IP address of the underlying physical network interface that handles overlay networks. The example architecture uses the management interface to tunnel traffic to the other nodes. Therefore, replace OVERLAY_INTERFACE_IP_ADDRESS with the management IP address of the controller node. See Host networking for more information.
readonly OVERLAY_INTERFACE_IP_ADDRESS=172.16.199.11#获取参数范围的头部行数
#$1 匹配正则
#$2 文件名
function get_range_start_line() {
    
grep -n $1 $2 | cut -d ":" -f 1 
}#获取参数范围的尾部行数
# $1 文件名
# $2 头部行数
function get_range_end_line() {
    
grep -n "^\[" $1 | cut -d ":" -f 1 | grep $2 -A1 | tail -n1 
}#在OpenStack中创建域、角色、服务和端点
function create_object(){
    
. ~/admin-openrc
openstack user create --domain default --password $NEUTRON_PASS neutron
openstack role add --project service --user neutron admin
openstack service create --name neutron --description "OpenStack Networking" network
openstack endpoint create --region RegionOne network public http://controller:9696
openstack endpoint create --region RegionOne network internal http://controller:9696
openstack endpoint create --region RegionOne network admin http://controller:9696
}function config_network() {
    
#echo "-- option1"
#source ./install_neutron_controller_network_option1.sh
echo "-- option2"
source ./install_neutron_controller_network_option2.sh
}# in /etc/neutron/metadata_agent.ini
function init_config_metadata_agent() {
    
filepath='/etc/neutron/metadata_agent.ini'
cp $filepath ${filepath}.bak
default_start=$(get_range_start_line "^\[DEFAULT\]" $filepath)
default_end=$(get_range_end_line $filepath $default_start)
sed -i "${default_start},$default_end s/^#nova_metadata_host =.*/nova_metadata_host = controller/g" $filepath
sed -i "${default_start},$default_end s/^#metadata_proxy_shared_secret =.*/metadata_proxy_shared_secret = "$METADATA_SECRET"/g" $filepath
}# in /etc/nova/nova.conf
function init_config_nova() {
    
filepath='/etc/nova/nova.conf'
# 备份原有配置
cp $filepath ${filepath}.bakneutron_start=$(get_range_start_line "^\[neutron\]" $filepath)
neutron_end=$(get_range_end_line $filepath $neutron_start)sed -i "${neutron_start},$neutron_end s#^\#url =.*#url = http://controller:9696#g" $filepath
sed -i "${neutron_start},$neutron_end s#^\#auth_url =.*#auth_url = http://controller:5000#g" $filepath
sed -i "${neutron_start},$neutron_end s/^#auth_type =.*/auth_type = password/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#project_domain_name =.*/project_domain_name = default/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#user_domain_name =.*/user_domain_name = default/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#region_name =.*/region_name = RegionOne/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#project_name =.*/project_name = service/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#username =.*/username = neutron/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#password =.*/password = "$NEUTRON_PASS"/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#service_metadata_proxy =.*/service_metadata_proxy = true/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#metadata_proxy_shared_secret =.*/metadata_proxy_shared_secret = "$METADATA_SECRET"/g" $filepath
}function check_neutron() {
    
. ~/admin-openrc
openstack extension list --network
echo "--------------网络配置opt1 和 opt2的区别在于,opt2会多一个在controller上的L3 agent"
openstack network agent list
}install_neutron(){
    
#报错即刻退出
set -o errexit
set -x#Prerequisites
echo "--------------创建neutron数据库"
source ./sql_scripts/neutron.sqlecho "--------------在OpenStack中创建域、角色、服务和端点"
create_objectecho "--------------设置网络选项"
config_networkecho "--------------设置元数据代理"
init_config_metadata_agentecho "--------------设置nova.conf,使compute服务可以使用网络服务"
init_config_novaecho "--------------生成neutron数据库的数据"
su -s /bin/sh -c "neutron-db-manage --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini upgrade head" neutron# 完成安装
echo "--------------完成安装"
service nova-api restart#重启网络服务
service neutron-server restart
service neutron-linuxbridge-agent restart
service neutron-dhcp-agent restart
service neutron-metadata-agent restart
# option2时,额外操作
service neutron-l3-agent restartecho "-----------验证操作及常用命令--------"
check_neutron}install_neutron

network option1

#!/bin/bash# in /etc/neutron/neutron.conf
function init_config_neutron() {
    filepath='/etc/neutron/neutron.conf'# 备份原有配置cp $filepath ${filepath}.baksed -i '/^\[database\]/,/^\[/ {s#^connection =.*#connection = mysql+pymysql://neutron:'$NEUTRON_DBPASS'@controller/neutron#g} ' $filepath default_start=$(get_range_start_line "^\[DEFAULT\]" $filepath)default_end=$(get_range_end_line $filepath $default_start)sed -i "${default_start},$default_end s/^core_plugin =.*/core_plugin = ml2/g" $filepathsed -i "${default_start},$default_end s/^#service_plugins =.*/service_plugins = /g" $filepathsed -i "${default_start},$default_end s#^\#transport_url =.*#transport_url = rabbit://openstack:"$RABBIT_PASS"@controller#g" $filepathsed -i "${default_start},$default_end s/^#auth_strategy =.*/auth_strategy = keystone/g" $filepathsed -i "${default_start},$default_end s/^#notify_nova_on_port_status_changes =.*/notify_nova_on_port_status_changes = true/g" $filepathsed -i "${default_start},$default_end s/^#notify_nova_on_port_data_changes =.*/notify_nova_on_port_data_changes = true/g" $filepathkeystone_start=$(get_range_start_line "^\[keystone_authtoken\]" $filepath)keystone_end=$(get_range_end_line $filepath $keystone_start)sed -i "${keystone_start},$keystone_end {/^\w/d}" $filepathsed -i $keystone_start"a\www_authenticate_uri = http://controller:5000" $filepath sed -i $((keystone_start + 1))"a\auth_url = http://controller:5000" $filepath sed -i $((keystone_start + 2))"a\memcached_servers = controller:11211" $filepath  sed -i $((keystone_start + 3))"a\auth_type = password" $filepath  sed -i $((keystone_start + 4))"a\project_domain_name = default" $filepath  sed -i $((keystone_start + 5))"a\user_domain_name = default" $filepath  sed -i $((keystone_start + 6))"a\project_name = service" $filepath  sed -i $((keystone_start + 7))"a\username = neutron" $filepath  sed -i $((keystone_start + 8))"a\password = "$NEUTRON_PASS $filepath  nova_start=$(get_range_start_line "^\[nova\]" $filepath)nova_end=$(get_range_end_line $filepath $nova_start)sed -i "${nova_start},$nova_end s#^\#auth_url =.*#auth_url = http://controller:5000#g" $filepathsed -i "${nova_start},$nova_end s/^#auth_type =.*/auth_type = password/g" $filepathsed -i "${nova_start},$nova_end s/^#project_domain_name =.*/project_domain_name = default/g" $filepathsed -i "${nova_start},$nova_end s/^#user_domain_name =.*/user_domain_name = default/g" $filepathsed -i "${nova_start},$nova_end s/^#region_name =.*/region_name = RegionOne/g" $filepathsed -i "${nova_start},$nova_end s/^#project_name =.*/project_name = service/g" $filepathsed -i "${nova_start},$nova_end s/^#username =.*/username = nova/g" $filepathsed -i "${nova_start},$nova_end s/^#password =.*/password = "$NOVA_PASS"/g" $filepathoslo_start=$(get_range_start_line "^\[oslo_concurrency\]" $filepath)oslo_end=$(get_range_end_line $filepath $oslo_start)sed -i "${oslo_start},$oslo_end s#^\#lock_path =.*#lock_path = /var/lib/neutron/tmp#g" $filepath
}# in /etc/neutron/plugins/ml2/ml2_conf.ini
function init_config_ml2() {
    filepath='/etc/neutron/plugins/ml2/ml2_conf.ini'# 备份原有配置cp $filepath ${filepath}.bakml2_start=$(get_range_start_line "^\[ml2\]" $filepath)ml2_end=$(get_range_end_line $filepath $ml2_start)
# After you configure the ML2 plug-in, removing values in the type_drivers option can lead to database inconsistency.sed -i "${ml2_start},$ml2_end s/^#type_drivers =.*/type_drivers = flat,vlan/g" $filepathsed -i "${ml2_start},$ml2_end s/^#tenant_network_types =.*/tenant_network_types = /g" $filepathsed -i "${ml2_start},$ml2_end s/^#mechanism_drivers =.*/mechanism_drivers = linuxbridge/g" $filepathsed -i "${ml2_start},$ml2_end s/^#extension_drivers =.*/extension_drivers = port_security/g" $filepathml2_type_flat_start=$(get_range_start_line "^\[ml2_type_flat\]" $filepath)ml2_type_flat_end=$(get_range_end_line $filepath $ml2_type_flat_start)sed -i "${ml2_type_flat_start},$ml2_type_flat_end s/^#flat_networks =.*/flat_networks = provider/g" $filepathsecuritygroup_start=$(get_range_start_line "^\[securitygroup\]" $filepath)#securitygroup是最后一个,end找不到securitygroup_end=$(get_range_end_line $filepath $securitygroup_start)if [ "$securitygroup_start"=="$securitygroup_end" ]; thensed -i "${securitygroup_start},$ {s/^#enable_ipset =.*/enable_ipset = true/g}" $filepathelsesed -i "${securitygroup_start},$securitygroup_end s/^#enable_ipset =.*/enable_ipset = true/g" $filepathfi
}# /etc/neutron/plugins/ml2/linuxbridge_agent.ini
function init_config_ml2_linuxbridge_agent() {
    filepath='/etc/neutron/plugins/ml2/linuxbridge_agent.ini'# 备份原有配置cp $filepath ${filepath}.baklinux_bridge_start=$(get_range_start_line "^\[linux_bridge\]" $filepath)linux_bridge_end=$(get_range_end_line $filepath $linux_bridge_start)sed -i "${linux_bridge_start},$linux_bridge_end s/^#physical_interface_mappings =.*/physical_interface_mappings = provider:"$PROVIDER_INTERFACE_NAME"/g" $filepath#vxlan是最后一个,end找不到vxlan_start=$(get_range_start_line "^\[vxlan\]" $filepath)vxlan_end=$(get_range_end_line $filepath $vxlan_start)if [ "$vxlan_start"=="$vxlan_end" ]; thensed -i "${vxlan_start},$ s/^#enable_vxlan =.*/enable_vxlan = false/g" $filepathelsesed -i "${vxlan_start},$vxlan_end s/^#enable_vxlan =.*/enable_vxlan = false/g" $filepathfisecuritygroup_start=$(get_range_start_line "^\[securitygroup\]" $filepath)securitygroup_end=$(get_range_end_line $filepath $securitygroup_start)sed -i "${securitygroup_start},$securitygroup_end s/^#enable_security_group =.*/enable_security_group = true/g" $filepathsed -i "${securitygroup_start},$securitygroup_end s/^#firewall_driver =.*/firewall_driver = neutron.agent.linux.iptables_firewall.IptablesFirewallDriver/g" $filepath#net.bridge.bridge-nf-call-iptables#net.bridge.bridge-nf-call-ip6tables
}# /etc/neutron/dhcp_agent.ini
function init_config_dhcp_agent() {
    filepath='/etc/neutron/dhcp_agent.ini'# 备份原有配置cp $filepath ${filepath}.bakdefault_start=$(get_range_start_line "^\[DEFAULT\]" $filepath)default_end=$(get_range_end_line $filepath $default_start)sed -i "${default_start},$default_end s/^#interface_driver =.*/interface_driver = linuxbridge/g" $filepathsed -i "${default_start},$default_end s/^#dhcp_driver =.*/dhcp_driver = neutron.agent.linux.dhcp.Dnsmasq/g" $filepathsed -i "${default_start},$default_end s/^#enable_isolated_metadata =.*/enable_isolated_metadata = true/g" $filepath
}install_neutron_controller_network_option2(){
    echo "--------------安装网络服务option2"apt install neutron-server neutron-plugin-ml2 neutron-linuxbridge-agent neutron-dhcp-agent neutron-metadata-agent -yecho "--------------设置服务组件"init_config_neutronecho "--------------设置ML2插件"init_config_ml2echo "--------------设置网桥代理"init_config_ml2_linuxbridge_agent	echo "--------------设置DHCP代理"init_config_dhcp_agent
}install_neutron_controller_network_option2

network option2

#!/bin/bash# in /etc/neutron/neutron.conf
function init_config_neutron() {
    
filepath='/etc/neutron/neutron.conf'
# 备份原有配置
cp $filepath ${filepath}.baksed -i '/^\[database\]/,/^\[/ {s#^connection =.*#connection = mysql+pymysql://neutron:'$NEUTRON_DBPASS'@controller/neutron#g} ' $filepath default_start=$(get_range_start_line "^\[DEFAULT\]" $filepath)
default_end=$(get_range_end_line $filepath $default_start)
sed -i "${default_start},$default_end s/^core_plugin =.*/core_plugin = ml2/g" $filepath
sed -i "${default_start},$default_end s/^#service_plugins =.*/service_plugins = router/g" $filepath
sed -i "${default_start},$default_end s/^#allow_overlapping_ips =.*/allow_overlapping_ips = true/g" $filepath
sed -i "${default_start},$default_end s#^\#transport_url =.*#transport_url = rabbit://openstack:"$RABBIT_PASS"@controller#g" $filepath
sed -i "${default_start},$default_end s/^#auth_strategy =.*/auth_strategy = keystone/g" $filepath
sed -i "${default_start},$default_end s/^#notify_nova_on_port_status_changes =.*/notify_nova_on_port_status_changes = true/g" $filepath
sed -i "${default_start},$default_end s/^#notify_nova_on_port_data_changes =.*/notify_nova_on_port_data_changes = true/g" $filepathkeystone_start=$(get_range_start_line "^\[keystone_authtoken\]" $filepath)
keystone_end=$(get_range_end_line $filepath $keystone_start)
sed -i "${keystone_start},$keystone_end {/^\w/d}" $filepath
sed -i $keystone_start"a\www_authenticate_uri = http://controller:5000" $filepath 
sed -i $((keystone_start + 1))"a\auth_url = http://controller:5000" $filepath 
sed -i $((keystone_start + 2))"a\memcached_servers = controller:11211" $filepath  
sed -i $((keystone_start + 3))"a\auth_type = password" $filepath  
sed -i $((keystone_start + 4))"a\project_domain_name = default" $filepath  
sed -i $((keystone_start + 5))"a\user_domain_name = default" $filepath  
sed -i $((keystone_start + 6))"a\project_name = service" $filepath  
sed -i $((keystone_start + 7))"a\username = neutron" $filepath  
sed -i $((keystone_start + 8))"a\password = "$NEUTRON_PASS $filepath  nova_start=$(get_range_start_line "^\[nova\]" $filepath)
nova_end=$(get_range_end_line $filepath $nova_start)
sed -i "${nova_start},$nova_end s#^\#auth_url =.*#auth_url = http://controller:5000#g" $filepath
sed -i "${nova_start},$nova_end s/^#auth_type =.*/auth_type = password/g" $filepath
sed -i "${nova_start},$nova_end s/^#project_domain_name =.*/project_domain_name = default/g" $filepath
sed -i "${nova_start},$nova_end s/^#user_domain_name =.*/user_domain_name = default/g" $filepath
sed -i "${nova_start},$nova_end s/^#region_name =.*/region_name = RegionOne/g" $filepath
sed -i "${nova_start},$nova_end s/^#project_name =.*/project_name = service/g" $filepath
sed -i "${nova_start},$nova_end s/^#username =.*/username = nova/g" $filepath
sed -i "${nova_start},$nova_end s/^#password =.*/password = "$NOVA_PASS"/g" $filepathoslo_start=$(get_range_start_line "^\[oslo_concurrency\]" $filepath)
oslo_end=$(get_range_end_line $filepath $oslo_start)
sed -i "${oslo_start},$oslo_end s#^\#lock_path =.*#lock_path = /var/lib/neutron/tmp#g" $filepath
}# in /etc/neutron/plugins/ml2/ml2_conf.ini
function init_config_ml2() {
    
filepath='/etc/neutron/plugins/ml2/ml2_conf.ini'
# 备份原有配置
cp $filepath ${filepath}.bak
ml2_start=$(get_range_start_line "^\[ml2\]" $filepath)
ml2_end=$(get_range_end_line $filepath $ml2_start)
# After you configure the ML2 plug-in, removing values in the type_drivers option can lead to database inconsistency.
sed -i "${ml2_start},$ml2_end s/^#type_drivers =.*/type_drivers = flat,vlan,vxlan/g" $filepath
sed -i "${ml2_start},$ml2_end s/^#tenant_network_types =.*/tenant_network_types = vxlan/g" $filepath
sed -i "${ml2_start},$ml2_end s/^#mechanism_drivers =.*/mechanism_drivers = linuxbridge,l2population/g" $filepath
sed -i "${ml2_start},$ml2_end s/^#extension_drivers =.*/extension_drivers = port_security/g" $filepathml2_type_flat_start=$(get_range_start_line "^\[ml2_type_flat\]" $filepath)
ml2_type_flat_end=$(get_range_end_line $filepath $ml2_type_flat_start)
sed -i "${ml2_type_flat_start},$ml2_type_flat_end s/^#flat_networks =.*/flat_networks = provider/g" $filepathml2_type_vxlan_start=$(get_range_start_line "^\[ml2_type_vxlan\]" $filepath)
ml2_type_vxlan_end=$(get_range_end_line $filepath $ml2_type_vxlan_start)
sed -i "${ml2_type_vxlan_start},$ml2_type_vxlan_end s/^#vni_ranges =.*/vni_ranges = 1:1000/g" $filepathsecuritygroup_start=$(get_range_start_line "^\[securitygroup\]" $filepath)
#securitygroup是最后一个,end找不到
securitygroup_end=$(get_range_end_line $filepath $securitygroup_start)
if [ "$securitygroup_start"=="$securitygroup_end" ]; then
sed -i "${securitygroup_start},$ {s/^#enable_ipset =.*/enable_ipset = true/g}" $filepath
else
sed -i "${securitygroup_start},$securitygroup_end s/^#enable_ipset =.*/enable_ipset = true/g" $filepath
fi
}# /etc/neutron/plugins/ml2/linuxbridge_agent.ini
function init_config_ml2_linuxbridge_agent() {
    
filepath='/etc/neutron/plugins/ml2/linuxbridge_agent.ini'
# 备份原有配置
cp $filepath ${filepath}.baklinux_bridge_start=$(get_range_start_line "^\[linux_bridge\]" $filepath)
linux_bridge_end=$(get_range_end_line $filepath $linux_bridge_start)
sed -i "${linux_bridge_start},$linux_bridge_end s/^#physical_interface_mappings =.*/physical_interface_mappings = provider:"$PROVIDER_INTERFACE_NAME"/g" $filepath#vxlan是最后一个,end找不到
vxlan_start=$(get_range_start_line "^\[vxlan\]" $filepath)
vxlan_end=$(get_range_end_line $filepath $vxlan_start)
if [ "$vxlan_start"=="$vxlan_end" ]; then
sed -i "${vxlan_start},$ s/^#enable_vxlan =.*/enable_vxlan = true/g" $filepath
sed -i "${vxlan_start},$ s/^#local_ip =.*/local_ip = "$OVERLAY_INTERFACE_IP_ADDRESS"/g" $filepath
sed -i "${vxlan_start},$ s/^#l2_population =.*/l2_population = true/g" $filepath
else
sed -i "${vxlan_start},$vxlan_end s/^#enable_vxlan =.*/enable_vxlan = true/g" $filepath
sed -i "${vxlan_start},$vxlan_end s/^#local_ip =.*/local_ip = "$OVERLAY_INTERFACE_IP_ADDRESS"/g" $filepath
sed -i "${vxlan_start},$vxlan_end s/^#l2_population =.*/l2_population = true/g" $filepath
fisecuritygroup_start=$(get_range_start_line "^\[securitygroup\]" $filepath)
securitygroup_end=$(get_range_end_line $filepath $securitygroup_start)
sed -i "${securitygroup_start},$securitygroup_end s/^#enable_security_group =.*/enable_security_group = true/g" $filepath
sed -i "${securitygroup_start},$securitygroup_end s/^#firewall_driver =.*/firewall_driver = neutron.agent.linux.iptables_firewall.IptablesFirewallDriver/g" $filepath#net.bridge.bridge-nf-call-iptables
#net.bridge.bridge-nf-call-ip6tables
}# /etc/neutron/l3_agent.ini
function init_config_l3_agent() {
    
filepath='/etc/neutron/l3_agent.ini'
# 备份原有配置
cp $filepath ${filepath}.bakdefault_start=$(get_range_start_line "^\[DEFAULT\]" $filepath)
default_end=$(get_range_end_line $filepath $default_start)
sed -i "${default_start},$default_end s/^#interface_driver =.*/interface_driver = linuxbridge/g" $filepath
}# /etc/neutron/dhcp_agent.ini
function init_config_dhcp_agent() {
    
filepath='/etc/neutron/dhcp_agent.ini'
# 备份原有配置
cp $filepath ${filepath}.bakdefault_start=$(get_range_start_line "^\[DEFAULT\]" $filepath)
default_end=$(get_range_end_line $filepath $default_start)
sed -i "${default_start},$default_end s/^#interface_driver =.*/interface_driver = linuxbridge/g" $filepath
sed -i "${default_start},$default_end s/^#dhcp_driver =.*/dhcp_driver = neutron.agent.linux.dhcp.Dnsmasq/g" $filepath
sed -i "${default_start},$default_end s/^#enable_isolated_metadata =.*/enable_isolated_metadata = true/g" $filepath
}install_neutron_controller_network_option2(){
    
echo "--------------安装网络服务option2"
apt install neutron-server neutron-plugin-ml2 neutron-linuxbridge-agent neutron-l3-agent neutron-dhcp-agent neutron-metadata-agent -yecho "--------------设置服务组件"
init_config_neutron
echo "--------------设置ML2插件"
init_config_ml2
echo "--------------设置网桥代理"
init_config_ml2_linuxbridge_agent
echo "--------------设置三层代理"
init_config_l3_agent
echo "--------------设置DHCP代理"
init_config_dhcp_agent
}install_neutron_controller_network_option2

SQL

mysql << EOF
CREATE DATABASE neutron;
GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'localhost' IDENTIFIED BY '$NEUTRON_DBPASS'; GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@'%' IDENTIFIED BY '$NEUTRON_DBPASS';
EOF

Compute

#!/bin/bashreadonly NEUTRON_DBPASS=openstack
readonly NEUTRON_PASS=openstack # 在keystone服务中的密码readonly RABBIT_PASS=openstack
#Replace PROVIDER_INTERFACE_NAME with the name of the underlying provider physical network interface
readonly PROVIDER_INTERFACE_NAME=eth1
#Replace OVERLAY_INTERFACE_IP_ADDRESS with the IP address of the underlying physical network interface that handles overlay networks. The example architecture uses the management interface to tunnel traffic to the other nodes. Therefore, replace OVERLAY_INTERFACE_IP_ADDRESS with the management IP address of the compute node. See Host networking for more information.
readonly OVERLAY_INTERFACE_IP_ADDRESS=172.16.199.31#获取参数范围的头部行数
#$1 匹配正则
#$2 文件名
function get_range_start_line() {
    
grep -n $1 $2 | cut -d ":" -f 1 
}#获取参数范围的尾部行数
# $1 文件名
# $2 头部行数
function get_range_end_line() {
    
grep -n "^\[" $1 | cut -d ":" -f 1 | grep $2 -A1 | tail -n1 
}# in /etc/neutron/neutron.conf
function init_config_neutron() {
    
filepath='/etc/neutron/neutron.conf'
# 备份原有配置
cp $filepath ${filepath}.bakdatabase_start=$(get_range_start_line "^\[database\]" $filepath)
database_end=$(get_range_end_line $filepath $database_start)
sed -i "${database_start},$database_end {/^\w/d}" $filepathdefault_start=$(get_range_start_line "^\[DEFAULT\]" $filepath)
default_end=$(get_range_end_line $filepath $default_start)
sed -i "${default_start},$default_end s#^\#transport_url =.*#transport_url = rabbit://openstack:"$RABBIT_PASS"@controller#g" $filepath
sed -i "${default_start},$default_end s/^#auth_strategy =.*/auth_strategy = keystone/g" $filepathkeystone_start=$(get_range_start_line "^\[keystone_authtoken\]" $filepath)
keystone_end=$(get_range_end_line $filepath $keystone_start)
sed -i "${keystone_start},$keystone_end {/^\w/d}" $filepath
sed -i $keystone_start"a\www_authenticate_uri = http://controller:5000" $filepath 
sed -i $((keystone_start + 1))"a\auth_url = http://controller:5000" $filepath 
sed -i $((keystone_start + 2))"a\memcached_servers = controller:11211" $filepath  
sed -i $((keystone_start + 3))"a\auth_type = password" $filepath  
sed -i $((keystone_start + 4))"a\project_domain_name = default" $filepath  
sed -i $((keystone_start + 5))"a\user_domain_name = default" $filepath  
sed -i $((keystone_start + 6))"a\project_name = service" $filepath  
sed -i $((keystone_start + 7))"a\username = neutron" $filepath  
sed -i $((keystone_start + 8))"a\password = "$NEUTRON_PASS $filepath  oslo_start=$(get_range_start_line "^\[oslo_concurrency\]" $filepath)
oslo_end=$(get_range_end_line $filepath $oslo_start)
sed -i "${oslo_start},$oslo_end s#^\#lock_path =.*#lock_path = /var/lib/neutron/tmp#g" $filepath
}function config_network() {
    
#echo "-- option1"
#source ./install_neutron_compute_network_option1.sh
echo "-- option2"
source ./install_neutron_compute_network_option2.sh
}# in /etc/nova/nova.conf
function init_config_nova() {
    
filepath='/etc/nova/nova.conf'
# 备份原有配置
cp $filepath ${filepath}.bakneutron_start=$(get_range_start_line "^\[neutron\]" $filepath)
neutron_end=$(get_range_end_line $filepath $neutron_start)sed -i "${neutron_start},$neutron_end s#^\#url =.*#url = http://controller:9696#g" $filepath
sed -i "${neutron_start},$neutron_end s#^\#auth_url =.*#auth_url = http://controller:5000#g" $filepath
sed -i "${neutron_start},$neutron_end s/^#auth_type =.*/auth_type = password/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#project_domain_name =.*/project_domain_name = default/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#user_domain_name =.*/user_domain_name = default/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#region_name =.*/region_name = RegionOne/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#project_name =.*/project_name = service/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#username =.*/username = neutron/g" $filepath
sed -i "${neutron_start},$neutron_end s/^#password =.*/password = "$NEUTRON_PASS"/g" $filepath}install_neutron_compute(){
    
#报错即刻退出
set -o errexit
set -xecho "--------------安装组件"
apt install neutron-linuxbridge-agent -yecho "--------------设置通用组件"
init_config_neutronecho "--------------设置网络选项"
config_networkecho "--------------设置nova.conf,使compute服务可以使用网络服务"
init_config_nova# 完成安装
echo "--------------完成安装"
service nova-compute restart
service neutron-linuxbridge-agent restart
}install_neutron_compute

network option1

#!/bin/bash# /etc/neutron/plugins/ml2/linuxbridge_agent.ini
function init_config_ml2_linuxbridge_agent() {
    filepath='/etc/neutron/plugins/ml2/linuxbridge_agent.ini'# 备份原有配置cp $filepath ${filepath}.baklinux_bridge_start=$(get_range_start_line "^\[linux_bridge\]" $filepath)linux_bridge_end=$(get_range_end_line $filepath $linux_bridge_start)sed -i "${linux_bridge_start},$linux_bridge_end s/^#physical_interface_mappings =.*/physical_interface_mappings = provider:"$PROVIDER_INTERFACE_NAME"/g" $filepath#vxlan是最后一个,end找不到vxlan_start=$(get_range_start_line "^\[vxlan\]" $filepath)vxlan_end=$(get_range_end_line $filepath $vxlan_start)if [ "$vxlan_start"=="$vxlan_end" ]; thensed -i "${vxlan_start},$ s/^#enable_vxlan =.*/enable_vxlan = false/g" $filepathelsesed -i "${vxlan_start},$vxlan_end s/^#enable_vxlan =.*/enable_vxlan = false/g" $filepathfisecuritygroup_start=$(get_range_start_line "^\[securitygroup\]" $filepath)securitygroup_end=$(get_range_end_line $filepath $securitygroup_start)sed -i "${securitygroup_start},$securitygroup_end s/^#enable_security_group =.*/enable_security_group = true/g" $filepathsed -i "${securitygroup_start},$securitygroup_end s/^#firewall_driver =.*/firewall_driver = neutron.agent.linux.iptables_firewall.IptablesFirewallDriver/g" $filepath
}install_neutron_compute_network_option2(){
    echo "--------------设置网桥代理"init_config_ml2_linuxbridge_agent
}install_neutron_compute_network_option2

network option2

#!/bin/bash# /etc/neutron/plugins/ml2/linuxbridge_agent.ini
function init_config_ml2_linuxbridge_agent() {
    
filepath='/etc/neutron/plugins/ml2/linuxbridge_agent.ini'
# 备份原有配置
cp $filepath ${filepath}.baklinux_bridge_start=$(get_range_start_line "^\[linux_bridge\]" $filepath)
linux_bridge_end=$(get_range_end_line $filepath $linux_bridge_start)
sed -i "${linux_bridge_start},$linux_bridge_end s/^#physical_interface_mappings =.*/physical_interface_mappings = provider:"$PROVIDER_INTERFACE_NAME"/g" $filepath#vxlan是最后一个,end找不到
vxlan_start=$(get_range_start_line "^\[vxlan\]" $filepath)
vxlan_end=$(get_range_end_line $filepath $vxlan_start)
if [ "$vxlan_start"=="$vxlan_end" ]; then
sed -i "${vxlan_start},$ s/^#enable_vxlan =.*/enable_vxlan = true/g" $filepath
sed -i "${vxlan_start},$ s/^#local_ip =.*/local_ip = "$OVERLAY_INTERFACE_IP_ADDRESS"/g" $filepath
sed -i "${vxlan_start},$ s/^#l2_population =.*/l2_population = true/g" $filepath
else
sed -i "${vxlan_start},$vxlan_end s/^#enable_vxlan =.*/enable_vxlan = true/g" $filepath
sed -i "${vxlan_start},$vxlan_end s/^#local_ip =.*/local_ip = "$OVERLAY_INTERFACE_IP_ADDRESS"/g" $filepath
sed -i "${vxlan_start},$vxlan_end s/^#l2_population =.*/l2_population = true/g" $filepath
fisecuritygroup_start=$(get_range_start_line "^\[securitygroup\]" $filepath)
securitygroup_end=$(get_range_end_line $filepath $securitygroup_start)
sed -i "${securitygroup_start},$securitygroup_end s/^#enable_security_group =.*/enable_security_group = true/g" $filepath
sed -i "${securitygroup_start},$securitygroup_end s/^#firewall_driver =.*/firewall_driver = neutron.agent.linux.iptables_firewall.IptablesFirewallDriver/g" $filepath#net.bridge.bridge-nf-call-iptables
#net.bridge.bridge-nf-call-ip6tables
}install_neutron_compute_network_option2(){
    
echo "--------------设置网桥代理"
init_config_ml2_linuxbridge_agent
}install_neutron_compute_network_option2

参考

Networking service