#!/bin/bash
# Copyright (c) 2015 SUSE LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# 
#======================================
# Functions...
#--------------------------------------
test -f /.kconfig && . /.kconfig
test -f /.profile && . /.profile

# mkdir /var/lib/named
# mkdir /var/lib/pgsql
# mkdir /var/lib/mailman
# mkdir /boot/grub2/i386-pc

mkdir /var/lib/misc/reconfig_system

#======================================
# Greeting...
#--------------------------------------
echo "Configure image: [$name]..."

#======================================
# add missing fonts
#--------------------------------------
CONSOLE_FONT="lat9w-16.psfu"

#======================================
# prepare for setting root pw, timezone
#--------------------------------------
echo ** "reset machine settings"
sed -i 's/^root:[^:]*:/root:*:/' /etc/shadow
rm /etc/machine-id
rm /etc/localtime
rm /var/lib/zypp/AnonymousUniqueId
rm /var/lib/systemd/random-seed

#======================================
# SuSEconfig
#--------------------------------------
echo "** Running suseConfig..."
suseConfig

echo "** Running ldconfig..."
/sbin/ldconfig

#======================================
# Setup baseproduct link
#--------------------------------------
suseSetupProduct

#======================================
# Specify default runlevel
#--------------------------------------
baseSetRunlevel 3

#======================================
# Add missing gpg keys to rpm
#--------------------------------------
suseImportBuildKey

#======================================
# Enable DHCP on eth0
#--------------------------------------
cat >/etc/sysconfig/network/ifcfg-eth0 <<EOF
BOOTPROTO='dhcp'
MTU=''
REMOTE_IPADDR=''
STARTMODE='auto'
ETHTOOL_OPTIONS=''
USERCONTROL='no'
EOF

#======================================
# Firewall Configuration
#--------------------------------------
echo '** Configuring firewall...'
chkconfig SuSEfirewall2_init on
chkconfig SuSEfirewall2_setup on

#======================================
# Enable sshd
#--------------------------------------
chkconfig sshd on

#======================================
# Remove doc files
#--------------------------------------
baseStripDocs

#======================================
# remove rpms defined in config.xml in the image type=delete section 
#--------------------------------------
baseStripRPM

#======================================
# Sysconfig Update
#--------------------------------------
echo '** Update sysconfig entries...'
baseUpdateSysConfig /etc/sysconfig/SuSEfirewall2 FW_CONFIGURATIONS_EXT sshd
baseUpdateSysConfig /etc/sysconfig/console CONSOLE_FONT "$CONSOLE_FONT"
# baseUpdateSysConfig /etc/sysconfig/snapper SNAPPER_CONFIGS root
if [ "${kiwi_iname##*-for-}" != "OpenStack-Cloud" ]; then
	baseUpdateSysConfig /etc/sysconfig/network/dhcp DHCLIENT_SET_HOSTNAME yes
fi

# true
#======================================
# SSL Certificates Configuration
#--------------------------------------
echo '** Rehashing SSL Certificates...'
update-ca-certificates

if [ ! -s /var/log/zypper.log ]; then
	> /var/log/zypper.log
fi

test -f /.kconfig && . /.kconfig
test -f /.profile && . /.profile

if [[ "${kiwi_iname}" == *"openstack"* ]]; then
	# not useful for cloud
	systemctl mask jeos-firstboot.service
	systemctl mask systemd-firstboot.service

	suseInsertService cloud-init-local
	suseInsertService cloud-init
	suseInsertService cloud-config
	suseInsertService cloud-final

	echo '*** adjusting cloud.cfg for openstack'
	sed -i -e '/mount_default_fields/{adatasource_list: [ NoCloud, OpenStack, None ]
	}' /etc/cloud/cloud.cfg
else
	# use jeos-firstboot.service instead of systemd-firstboot.service
	systemctl mask systemd-firstboot.service
	systemctl enable jeos-firstboot.service
fi

# Configure Amphora agent
suseInsertService openstack-octavia-amphora-agent

# Configure HAProxy
sysctl -w net.ipv4.tcp_max_tw_buckets=5800000
sysctl -w net.ipv4.tcp_max_orphans=5800000
sysctl -w net.ipv4.tcp_max_syn_backlog=100000
sysctl -w net.ipv4.tcp_keepalive_time=300
sysctl -w net.ipv4.tcp_tw_recycle=0
sysctl -w net.ipv4.tcp_tw_reuse=1
sysctl -w net.core.somaxconn=65534
sysctl -w net.ipv4.tcp_synack_retries=3
sysctl -w net.core.netdev_max_backlog=100000
sysctl -w fs.file-max=1048576

# It's ok for these to fail if conntrack module isn't loaded
# sysctl -w net.netfilter.nf_conntrack_max=131072 || true
# sysctl -w net.ipv4.netfilter.ip_conntrack_max=1524288 || true
# sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=5 || true
# sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=5 || true

sysctl -w net.ipv4.tcp_fin_timeout=5
sysctl -w net.ipv4.ip_nonlocal_bind=1
sysctl -w net.ipv4.tcp_rmem="16384 65536 524288"
sysctl -w net.ipv4.tcp_wmem="16384 349520 699040"
sysctl -w net.ipv4.ip_local_port_range="1025 65534"

cat >> /var/lib/octavia/haproxy-default-user-group.conf <<EOF
global
    group haproxy
EOF

suseInsertService haproxy

# Enable the certs-ramfs
# https://github.com/openstack/octavia/blob/master/elements/certs-ramfs/init-scripts/systemd/certs-ramfs.service
suseInsertService certs-ramfs

# only for debugging
#systemctl enable debug-shell.service

baseCleanMount

exit 0
