Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cfggen] Add support in -v for jinja2 expression #345

Merged
merged 2 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dockers/docker-teamd/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

mkdir -p /etc/teamd

for pc in `sonic-cfggen -m /etc/sonic/minigraph.xml --var-keys minigraph_portchannel_interfaces`; do
for pc in `sonic-cfggen -m /etc/sonic/minigraph.xml -v "minigraph_portchannel_interfaces.keys()"`; do
sonic-cfggen -m /etc/sonic/minigraph.xml -a '{"pc":"'$pc'"}' -t /usr/share/sonic/templates/teamd.j2 >/etc/teamd/$pc.conf
done

Expand Down
12 changes: 7 additions & 5 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import jinja2
import netaddr
import json
from minigraph import parse_xml
from minigraph import minigraph_encoder


def is_ipv4(value):
Expand Down Expand Up @@ -55,9 +56,9 @@ def main():
group = parser.add_mutually_exclusive_group()
group.add_argument("-t", "--template", help="render the data with the template file")
group.add_argument("-s", "--alias-mapping", help="print alias mapping json if available", action='store_true')
group.add_argument("-v", "--var", help="print the value of a variable")
group.add_argument("-v", "--var", help="print the value of a variable, support jinja2 expression")
group.add_argument("--var-json", help="print the value of a variable, in json format")
group.add_argument("--var-keys", help="print all keys of a map variable")
group.add_argument("--var-keys", help="print all keys of a map variable - to be deprecated, use -v and keys()")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we still need this, anywhere else is using this option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No other script is using, just to keep it there for a while in case someone is using it offline. To be removed in later version.

group.add_argument("--print-data", help="print all data", action='store_true')
args = parser.parse_args()

Expand Down Expand Up @@ -94,10 +95,11 @@ def main():
print template.render(data)

if args.var != None:
print data[args.var]
template = jinja2.Template('{{' + args.var + '}}')
print template.render(data)

if args.var_json != None:
print json.dumps(data[args.var_json])
print json.dumps(data[args.var_json], indent=4, cls=minigraph_encoder)

if args.var_keys != None:
for key in data[args.var_keys].keys():
Expand All @@ -111,7 +113,7 @@ def main():
print json.dumps(mapping)

if args.print_data:
print data
print json.dumps(data, indent=4, cls=minigraph_encoder)

if __name__ == "__main__":
main()
Expand Down