Commit 4dde84f6 authored by Sagar Arora's avatar Sagar Arora Committed by Raphael Defosseux

Jinja2 template for configuration files:

* feat: jinja2 based configuration file
* Current versions of tutorials are still working, but they can be simplified.
parent 4c73a0c2
# prompt has been removed for easier Ctrl+C Ctrl+V
# please update the following information according to your configuration
INSTANCE=1
PREFIX='/usr/local/etc/oai'
sudo mkdir -m 0777 -p $PREFIX
cp ../../etc/amf.conf $PREFIX
declare -A AMF_CONF
AMF_CONF[@INSTANCE@]=$INSTANCE
AMF_CONF[@PREFIX@]=$PREFIX
AMF_CONF[@PID_DIRECTORY@]='/var/run'
AMF_CONF[@MCC@]='208'
AMF_CONF[@MNC@]='95'
AMF_CONF[@REGION_ID@]='128'
AMF_CONF[@AMF_SET_ID@]='1'
AMF_CONF[@SERVED_GUAMI_MCC_0@]='208'
AMF_CONF[@SERVED_GUAMI_MNC_0@]='95'
AMF_CONF[@SERVED_GUAMI_REGION_ID_0@]='128'
AMF_CONF[@SERVED_GUAMI_AMF_SET_ID_0@]='1'
AMF_CONF[@SERVED_GUAMI_MCC_1@]='460'
AMF_CONF[@SERVED_GUAMI_MNC_1@]='11'
AMF_CONF[@SERVED_GUAMI_REGION_ID_1@]='10'
AMF_CONF[@SERVED_GUAMI_AMF_SET_ID_1@]='1'
AMF_CONF[@PLMN_SUPPORT_MCC@]='208'
AMF_CONF[@PLMN_SUPPORT_MNC@]='95'
AMF_CONF[@PLMN_SUPPORT_TAC@]='0xa000'
AMF_CONF[@SST_0@]='128'
AMF_CONF[@SD_0@]='128'
AMF_CONF[@SST_1@]='1'
AMF_CONF[@SD_1@]='1'
AMF_CONF[@AMF_INTERFACE_NAME_FOR_NGAP@]='eth0'
AMF_CONF[@AMF_INTERFACE_NAME_FOR_N11@]='eth0'
AMF_CONF[@EXTERNAL_AUSF@]='no'
AMF_CONF[@EXTERNAL_UDM@]='no'
AMF_CONF[@EXTERNAL_NRF@]='no'
AMF_CONF[@EXTERNAL_NSSF@]='no'
AMF_CONF[@USE_FQDN_DNS@]='no'
AMF_CONF[@USE_HTTP2@]='no'
AMF_CONF[@NF_REGISTRATION@]='no'
AMF_CONF[@SMF_SELECTION@]='no'
AMF_CONF[@SMF_INSTANCE_ID_0@]='1'
AMF_CONF[@SMF_IPV4_ADDR_0@]='192.168.22.208'
AMF_CONF[@SMF_HTTP_VERSION_0@]='v1'
AMF_CONF[@SMF_FQDN_0@]='oai-smf'
AMF_CONF[@SMF_INSTANCE_ID_1@]='2'
AMF_CONF[@SMF_IPV4_ADDR_1@]='192.168.22.207'
AMF_CONF[@SMF_HTTP_VERSION_1@]='v1'
AMF_CONF[@SMF_FQDN_1@]='oai-smf2'
AMF_CONF[@NRF_IPV4_ADDRESS@]='192.168.22.195'
AMF_CONF[@NRF_PORT@]='80'
AMF_CONF[@NRF_API_VERSION@]='v1'
AMF_CONF[@NRF_FQDN@]='oai-nrf'
AMF_CONF[@AUSF_IPV4_ADDRESS@]='192.168.22.199'
AMF_CONF[@AUSF_PORT@]='80'
AMF_CONF[@AUSF_API_VERSION@]='v1'
AMF_CONF[@AUSF_FQDN@]='oai-ausf'
AMF_CONF[@NSSF_IPV4_ADDRESS@]='192.168.22.214'
AMF_CONF[@NSSF_PORT@]='80'
AMF_CONF[@NSSF_API_VERSION@]='v1'
AMF_CONF[@NSSF_FQDN@]='oai-nssf'
AMF_CONF[@UDM_IPV4_ADDRESS@]='192.168.22.209'
AMF_CONF[@UDM_PORT@]='80'
AMF_CONF[@UDM_API_VERSION@]='v1'
AMF_CONF[@UDM_FQDN@]='oai-udm'
AMF_CONF[@NRF_SELECTION@]='no'
AMF_CONF[@INT_ALGO_LIST@]='[ "NIA0" , "NIA1" , "NIA2" ]'
AMF_CONF[@CIPH_ALGO_LIST@]='[ "NEA0" , "NEA1" , "NEA2" ]'
AMF_CONF[@MYSQL_SERVER@]='127.0.0.1'
AMF_CONF[@MYSQL_USER@]='root'
AMF_CONF[@MYSQL_PASS@]='linux'
AMF_CONF[@MYSQL_DB@]='oai_db'
for K in "${!AMF_CONF[@]}"; do
egrep -lRZ "$K" $PREFIX | xargs -0 -l sed -i -e "s|$K|${AMF_CONF[$K]}|g"
ret=$?;[[ ret -ne 0 ]] && echo "Tried to replace $K with ${AMF_CONF[$K]}"
done
#!/bin/sh
################################################################################
# Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The OpenAirInterface Software Alliance licenses this file to You under
# the OAI Public License, Version 1.1 (the "License"); you may not use this file
# except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.openairinterface.org/?page_id=698
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# For more information about the OpenAirInterface (OAI) Software Alliance:
# contact@openairinterface.org
################################################################################
## Sample values for configuration
export INSTANCE=0
export PID_DIRECTORY=/var/run
export MCC=208
export MNC=95
export REGION_ID=128
export AMF_SET_ID=1
export SERVED_GUAMI_MCC_0=208
export SERVED_GUAMI_MNC_0=95
export SERVED_GUAMI_REGION_ID_0=128
export SERVED_GUAMI_AMF_SET_ID_0=1
export PLMN_SUPPORT_MCC=208
export PLMN_SUPPORT_MNC=95
export PLMN_SUPPORT_TAC=0xa000
export SST_0=1
export SD_0=0xFFFFFF
export SST_1=1
export SD_1=1
export AMF_INTERFACE_NAME_FOR_NGAP=eth0
export AMF_INTERFACE_NAME_FOR_N11=eth0
export SMF_INSTANCE_ID_0=1
export SMF_FQDN_0=localhost
export SMF_IPV4_ADDR_0=127.0.0.1
export SMF_HTTP_VERSION_0=v1
export SELECTED_0=true
export MYSQL_SERVER=127.0.0.1
export MYSQL_USER=root
export MYSQL_PASS=linux
export MYSQL_DB=oai_db
export NRF_IPV4_ADDRESS=127.0.0.1
export NRF_PORT=80
export EXTERNAL_NRF=no
export NF_REGISTRATION=yes
export SMF_SELECTION=yes
export USE_FQDN_DNS=yes
export EXTERNAL_AUSF=yes
export EXTERNAL_UDM=no
export EXTERNAL_NSSF=no
export USE_HTTP2=no
export NRF_API_VERSION=v1
export NRF_FQDN=localhost
export AUSF_IPV4_ADDRESS=127.0.0.1
export AUSF_PORT=80
export AUSF_API_VERSION=v1
export AUSF_FQDN=localhost
export UDM_IPV4_ADDRESS=127.0.0.1
export UDM_PORT=80
export UDM_API_VERSION=v2
export UDM_FQDN=localhost
../../scripts/entrypoint.py
\ No newline at end of file
......@@ -64,6 +64,8 @@ RUN apt-get update && \
openssl \
libbrotli1 \
netbase \
python3\
python3-jinja2\
# Ubuntu 18 --> libasan4
# Ubuntu 20 --> libasan5
# Ubuntu 22 --> libasan6
......@@ -94,7 +96,7 @@ RUN apt-get update && \
WORKDIR /openair-amf/bin
COPY --from=oai-amf-builder \
/openair-amf/build/amf/build/oai_amf \
/openair-amf/scripts/entrypoint.sh \
/openair-amf/scripts/entrypoint.py \
/openair-amf/scripts/healthcheck.sh \
./
......@@ -127,5 +129,5 @@ HEALTHCHECK --interval=10s \
--retries=6 \
CMD /openair-amf/bin/healthcheck.sh
ENTRYPOINT ["/bin/bash","/openair-amf/bin/entrypoint.sh"]
ENTRYPOINT ["python3", "/openair-amf/bin/entrypoint.py"]
CMD ["/openair-amf/bin/oai_amf", "-c", "/openair-amf/etc/amf.conf", "-o"]
......@@ -81,7 +81,7 @@ def perform_flattening(tag):
cmd += ' --change "EXPOSE 38412/sctp" '
cmd += ' --change "HEALTHCHECK --interval=10s --timeout=15s --retries=6 CMD /openair-amf/bin/healthcheck.sh" '
cmd += ' --change "CMD [\\"/openair-amf/bin/oai_amf\\", \\"-c\\", \\"/openair-amf/etc/amf.conf\\", \\"-o\\"]" '
cmd += ' --change "ENTRYPOINT [\\"/bin/bash\\", \\"/openair-amf/bin/entrypoint.sh\\"]" '
cmd += ' --change "ENTRYPOINT [\\"python3\\", \\"/openair-amf/bin/entrypoint.py\\"]" '
cmd += ' - ' + image_prefix + tag
print (cmd)
subprocess.check_output(cmd, shell=True, universal_newlines=True)
......
......@@ -107,6 +107,8 @@ RUN rm -f /etc/rhsm-host && \
boost \
libevent \
findutils \
python3\
python3-jinja2\
mysql-libs \
libconfig && \
microdnf clean all -y && \
......@@ -119,7 +121,7 @@ RUN rm -f /etc/rhsm-host && \
WORKDIR /openair-amf/bin
COPY --from=oai-amf-builder \
/openair-amf/build/amf/build/oai_amf \
/openair-amf/scripts/entrypoint.sh \
/openair-amf/scripts/entrypoint.py \
/openair-amf/scripts/healthcheck.sh \
./
......@@ -146,5 +148,5 @@ HEALTHCHECK --interval=10s \
--retries=6 \
CMD /openair-amf/bin/healthcheck.sh
ENTRYPOINT ["/openair-amf/bin/entrypoint.sh"]
ENTRYPOINT ["python3", "/openair-amf/bin/entrypoint.py"]
CMD ["/openair-amf/bin/oai_amf", "-c", "/openair-amf/etc/amf.conf", "-o"]
......@@ -85,6 +85,8 @@ RUN apt-get update && \
openssl \
libbrotli1 \
netbase \
python3\
python3-jinja2\
# Ubuntu 18 --> libasan4
# Ubuntu 20 --> libasan5
# Ubuntu 22 --> libasan6
......@@ -115,7 +117,7 @@ RUN apt-get update && \
WORKDIR /openair-amf/bin
COPY --from=oai-amf-builder \
/openair-amf/build/amf/build/oai_amf \
/openair-amf/scripts/entrypoint.sh \
/openair-amf/scripts/entrypoint.py \
/openair-amf/scripts/healthcheck.sh \
./
......@@ -148,5 +150,5 @@ HEALTHCHECK --interval=10s \
--retries=6 \
CMD /openair-amf/bin/healthcheck.sh
ENTRYPOINT ["/bin/bash","/openair-amf/bin/entrypoint.sh"]
ENTRYPOINT ["python3", "/openair-amf/bin/entrypoint.py"]
CMD ["/openair-amf/bin/oai_amf", "-c", "/openair-amf/etc/amf.conf", "-o"]
This diff is collapsed.
#!/usr/bin/env python3
################################################################################
# Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The OpenAirInterface Software Alliance licenses this file to You under
# the OAI Public License, Version 1.1 (the "License"); you may not use this file
# except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.openairinterface.org/?page_id=698
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# For more information about the OpenAirInterface (OAI) Software Alliance:
# contact@openairinterface.org
################################################################################
from jinja2 import Environment, FileSystemLoader
import socket
import os
import sys
CONFIG_FILE = str(os.getenv('CONFIG_FILE','/openair-amf/etc/amf.conf'))
MOUNT_CONFIG = str(os.getenv('MOUNT_CONFIG','no')).lower()
def resolve(hostname):
try:
return socket.gethostbyname(hostname)
except socket.error:
print(f"Not able to resolve {hostname}")
def render(filepath,funcs,values):
env = Environment(loader=FileSystemLoader(os.path.dirname(filepath)))
jinja_template = env.get_template(os.path.basename(filepath))
jinja_template.globals.update(funcs)
template_string = jinja_template.render(env=values)
return template_string
env_variables = dict()
#list of all the environment variables
for name, value in os.environ.items():
env_variables.update({name:value})
if MOUNT_CONFIG != "yes":
output = render(CONFIG_FILE,{"resolve":resolve},env_variables)
with open(CONFIG_FILE, "w") as fh:
fh.write(output)
print(f"Configuration file {CONFIG_FILE} is ready")
if len(sys.argv) == 1:
sys.exit(0)
os.execvp(sys.argv[1], sys.argv[1:]) #important for running the network function it works like exec $@
else:
print("Configuration file is mounted to the network function")
\ No newline at end of file
#!/bin/bash
set -euo pipefail
CONFIG_DIR="/openair-amf/etc"
# Default values
EXTERNAL_NRF=${EXTERNAL_NRF:-no}
EXTERNAL_NSSF=${EXTERNAL_NSSF:-no}
EXTERNAL_AUSF=${EXTERNAL_AUSF:-no}
EXTERNAL_UDM=${EXTERNAL_UDM:-no}
UDM_IPV4_ADDRESS=${UDM_IPV4_ADDRESS:-0.0.0.0}
UDM_PORT=${UDM_PORT:-80}
UDM_API_VERSION=${UDM_API_VERSION:-v2}
UDM_FQDN=${UDM_FQDN:-oai-udm}
EXTERNAL_NRF=${EXTERNAL_NRF:-no}
NRF_SELECTION=${NRF_SELECTION:-no}
EXTERNAL_NSSF=${EXTERNAL_NSSF:-no}
NSSF_IPV4_ADDRESS=${NSSF_IPV4_ADDRESS:-0.0.0.0}
NSSF_PORT=${NSSF_PORT:-80}
NSSF_API_VERSION=${NSSF_API_VERSION:-v2}
NSSF_FQDN=${NSSF_FQDN:-oai-nssf}
INT_ALGO_LIST=${INT_ALGO_LIST:-'[ "NIA0" , "NIA1" , "NIA2" ]'}
CIPH_ALGO_LIST=${CIPH_ALGO_LIST:-'[ "NEA0" , "NEA1" , "NEA2" ]'}
USE_HTTP2=${USE_HTTP2:-no}
SST_0=${SST_0:-128}
SD_0=${SD_0:-128}
SST_1=${SST_1:-1}
SD_1=${SD_1:-0xFFFFFF}
SST_2=${SST_2:-130}
SD_2=${SD_2:-130}
OPERATOR_KEY=${OPERATOR_KEY:-'63bfa50ee6523365ff14c1f45f88737d'}
if [[ ${USE_FQDN_DNS} == "yes" ]];then
NSSF_IPV4_ADDR=${NSSF_IPV4_ADDR_0:-0.0.0.0}
SMF_IPV4_ADDR_0=${SMF_IPV4_ADDR_0:-0.0.0.0}
SMF_IPV4_ADDR_1=${SMF_IPV4_ADDR_1:-0.0.0.0}
NRF_IPV4_ADDRESS=${NRF_IPV4_ADDRESS:-0.0.0.0}
AUSF_IPV4_ADDRESS=${AUSF_IPV4_ADDRESS:-0.0.0.0}
UDM_IPV4_ADDRESS=${UDM_IPV4_ADDRESS:-0.0.0.0}
fi
for c in ${CONFIG_DIR}/*.conf; do
# grep variable names (format: ${VAR}) from template to be rendered
if ! grep -oP '@[a-zA-Z0-9_]+@' ${c}; then
echo "Configuration is already set"
exec "$@"
fi
VARS=$(grep -oP '@[a-zA-Z0-9_]+@' ${c} | sort | uniq | xargs)
echo "Now setting these variables '${VARS}'"
# create sed expressions for substituting each occurrence of ${VAR}
# with the value of the environment variable "VAR"
EXPRESSIONS=""
for v in ${VARS}; do
NEW_VAR=`echo $v | sed -e "s#@##g"`
if [[ -z ${!NEW_VAR+x} ]]; then
echo "Error: Environment variable '${NEW_VAR}' is not set." \
"Config file '$(basename $c)' requires all of $VARS."
exit 1
fi
EXPRESSIONS="${EXPRESSIONS};s|${v}|${!NEW_VAR}|g"
done
EXPRESSIONS="${EXPRESSIONS#';'}"
# render template and inline replace config file
sed -i "${EXPRESSIONS}" ${c}
done
echo "Done setting the configuration"
exec "$@"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment