Skip to content

Commit

Permalink
Use template to generate copy_config_files.sh (#327)
Browse files Browse the repository at this point in the history
- 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
ffoulkes authored Nov 9, 2023
1 parent d4c0d5c commit 67e2c4f
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 97 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
p4runtime==1.3.0
Jinja2>=3.0.0
fcntl
select
4 changes: 4 additions & 0 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
# SPDX-License-Identifier: Apache 2.0
#

set(GENSCRIPT_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/gen/genscript.py CACHE PATH
"Command to generate a script file using a template")
mark_as_advanced(GENSCRIPT_COMMAND)

add_subdirectory(common)

if(DPDK_TARGET)
Expand Down
46 changes: 0 additions & 46 deletions scripts/common/copy_config_files.sh.in

This file was deleted.

28 changes: 20 additions & 8 deletions scripts/dpdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@
# SPDX-License-Identifier: Apache 2.0
#

set(TARGET_NAME "dpdk")
configure_file("../common/copy_config_files.sh.in"
${CMAKE_CURRENT_BINARY_DIR}/copy_config_files.sh @ONLY)
unset(TARGET_NAME)
set(_filename copy_config_files.sh)
set(_template ${_filename}.jinja)
set(_outfile ${CMAKE_CURRENT_BINARY_DIR}/${_filename})

add_custom_target(dpdk-copy-script ALL
COMMAND
${GENSCRIPT_COMMAND}
--template=${_template}
--output=${_outfile}
--target=dpdk
BYPRODUCTS
${_outfile}
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
COMMENT
"Generating ${_filename}"
VERBATIM
)

# Sets execute permissions
install(
PROGRAMS
${CMAKE_CURRENT_BINARY_DIR}/copy_config_files.sh
TYPE
SBIN
PROGRAMS ${_outfile}
TYPE SBIN
)
31 changes: 20 additions & 11 deletions scripts/es2k/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
# SPDX-License-Identifier: Apache 2.0
#

# Customize copy_config_files.sh script
set(_filename copy_config_files.sh)
set(_template ${_filename}.jinja)
set(_outfile ${CMAKE_CURRENT_BINARY_DIR}/${_filename})

set(TARGET_NAME "es2k")

configure_file("../common/copy_config_files.sh.in"
${CMAKE_CURRENT_BINARY_DIR}/copy_config_files.sh @ONLY)

unset(TARGET_NAME)
add_custom_target(es2k-copy-script ALL
COMMAND
${GENSCRIPT_COMMAND}
--template=${_template}
--output=${_outfile}
--target=es2k
BYPRODUCTS
${_outfile}
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
COMMENT
"Generating ${_filename}"
VERBATIM
)

# Sets execute permissions
install(
PROGRAMS
${CMAKE_CURRENT_BINARY_DIR}/copy_config_files.sh
TYPE
SBIN
PROGRAMS ${_outfile}
TYPE SBIN
)
32 changes: 0 additions & 32 deletions scripts/es2k/copy_config_files.sh

This file was deleted.

53 changes: 53 additions & 0 deletions scripts/gen/copy_config_files.sh.jinja
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/

108 changes: 108 additions & 0 deletions scripts/gen/genscript.py
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__

0 comments on commit 67e2c4f

Please sign in to comment.