-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use template to generate copy_config_files.sh (#327)
- Create a Jinja template and Python driver to generate the copy_config_files.sh file. This gives us more flexibility than the cmake `configure_files()` command. - Add Jinja2 to requirements.txt file Signed-off-by: Derek G Foster <[email protected]>
- Loading branch information
Showing
8 changed files
with
206 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
p4runtime==1.3.0 | ||
Jinja2>=3.0.0 | ||
fcntl | ||
select |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{#- Jinja template to create copy_config_files.sh -#} | ||
#!/bin/bash | ||
# | ||
# Copyright 2021-2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Install {{ target|upper }} configuration files. | ||
# | ||
set -e | ||
|
||
if [ -z "$1" ] | ||
then | ||
echo "- Missing mandatory argument: P4CP_INSTALL" | ||
echo " - Usage: sudo copy_config_files.sh <P4CP_INSTALL> <SDE_INSTALL>" | ||
return 0 | ||
fi | ||
|
||
if [ -z "$2" ] | ||
then | ||
echo "- Missing mandatory argument: SDE_INSTALL" | ||
echo " - Usage: sudo copy_config_files.sh <P4CP_INSTALL> <SDE_INSTALL>" | ||
return 0 | ||
fi | ||
|
||
export P4CP_INSTALL=$1 | ||
export SDE_INSTALL=$2 | ||
|
||
# stratum directories | ||
mkdir -p /etc/stratum/ | ||
mkdir -p /var/log/stratum/ | ||
|
||
# stratum configuration | ||
SOURCE_DIR=${P4CP_INSTALL}/share/stratum/{{ target }} | ||
TARGET_DIR=/usr/share/stratum/{{ target }} | ||
mkdir -p ${TARGET_DIR} | ||
cp ${SOURCE_DIR}/{{ target }}_port_config.pb.txt ${TARGET_DIR}/ | ||
cp ${SOURCE_DIR}/{{ target }}_skip_p4.conf ${TARGET_DIR}/ | ||
|
||
# tls certificate generation | ||
TLS_CERTS_DIR=/usr/share/stratum | ||
cp ${SOURCE_DIR}/ca.conf ${TLS_CERTS_DIR}/ | ||
cp ${SOURCE_DIR}/grpc-client.conf ${TLS_CERTS_DIR}/ | ||
cp ${P4CP_INSTALL}/sbin/generate-certs.sh ${TLS_CERTS_DIR}/ | ||
{%- if target == 'es2k' -%} | ||
{{'\n\n'}}# packetio configuration | ||
mkdir -p /usr/share/bf_rt_shared | ||
cp $SDE_INSTALL/share/bf_rt_shared/tdi_pktio.json /usr/share/bf_rt_shared/ | ||
{%- endif %} | ||
|
||
# zlog configuration | ||
mkdir -p /usr/share/target_sys/ | ||
cp $SDE_INSTALL/share/target_sys/zlog-cfg /usr/share/target_sys/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2023 Intel Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Generates a script from a template file. | ||
# | ||
|
||
import argparse | ||
import logging | ||
import os | ||
|
||
from jinja2 import Environment | ||
from jinja2 import FileSystemLoader | ||
|
||
# Templates are in the same directory as this script. | ||
TEMPLATE_PATH = os.path.dirname(__file__) | ||
|
||
VALID_TARGETS = ['dpdk', 'es2k', 'tofino'] | ||
|
||
logger = logging.getLogger('genscript') | ||
|
||
errcount = 0 | ||
|
||
def error(msg, *args, **kwargs): | ||
"""Logs an error and increments the error count.""" | ||
global errcount | ||
logger.error(msg, *args, **kwargs) | ||
errcount += 1 | ||
return | ||
|
||
#----------------------------------------------------------------------- | ||
# generate_script() | ||
#----------------------------------------------------------------------- | ||
def generate_script(args): | ||
gendir = os.path.dirname(__file__) | ||
env = Environment(loader=FileSystemLoader(gendir)) | ||
template = env.get_template(args.template) | ||
rendered = template.render(target=args.target) | ||
with open(args.output, 'w') as fd: | ||
fd.write(rendered) | ||
return | ||
|
||
#----------------------------------------------------------------------- | ||
# parse_cmd_line() | ||
#----------------------------------------------------------------------- | ||
def parse_cmd_line(): | ||
parser = create_parser() | ||
args = parser.parse_args() | ||
process_args(args) | ||
return args | ||
|
||
def create_parser(): | ||
parser = argparse.ArgumentParser( | ||
prog='genfile.py', | ||
description='Generates a script from a template file.') | ||
|
||
parser.add_argument('--template', '-t', type=str, | ||
help='template file') | ||
parser.add_argument('--output', '-o', help='output file') | ||
parser.add_argument('--target', type=str, | ||
help='target to build {}'.format('|'.join(VALID_TARGETS))) | ||
|
||
return parser | ||
|
||
def process_args(args): | ||
process_output_arg(args) | ||
process_target_arg(args) | ||
process_template_arg(args) | ||
return | ||
|
||
def process_output_arg(args): | ||
if args.output is None: | ||
error("'--output' parameter not specified") | ||
return | ||
args.output = os.path.abspath(os.path.expanduser(args.output)) | ||
return | ||
|
||
def process_target_arg(args): | ||
if args.target is None: | ||
error("'--target' parameter not specified") | ||
return | ||
target = args.target.lower() | ||
if target in VALID_TARGETS: | ||
args.target = target | ||
else: | ||
error("Invalid target type: '%s'", args.target) | ||
return | ||
|
||
def process_template_arg(args): | ||
if args.template is None: | ||
error("'--template' parameter not specified") | ||
return | ||
# args.template = os.path.abspath(os.path.expanduser(args.template)) | ||
return | ||
|
||
#----------------------------------------------------------------------- | ||
# main - Main program | ||
#----------------------------------------------------------------------- | ||
if __name__ == '__main__': | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
args = parse_cmd_line() | ||
if errcount: | ||
exit(1) | ||
|
||
generate_script(args) | ||
# end __main__ |