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

Changed to use template files #13

Merged
merged 2 commits into from
Nov 6, 2019
Merged

Conversation

mikemrm
Copy link
Contributor

@mikemrm mikemrm commented Oct 31, 2019

Here I'm pulling the hard coded """{template}""" into actual
template files. In doing so, I also brought common files that
are shared between network configs such has hostname configuration
and hosts files into the distro builder.

By doing so, I also had to move the render and execution functions
into the distro builder. This I think makes more sense, and allows
for a better hierarchical structure to tasks.

To better assist with adding tasks and using template files, a new
parent class Tasks was created which allows a task to be set by
calling self.task(task, content, write_mode, mode, fmt) which
will build the structure accordingly. For a template file you can
call self.task_template(task, rel_path, write_mode, mode, fmt).
rel_path is relative to the templates_base variable defined
in the distro builder file.

if not isinstance(builder.builders[j], DebianBondedNetwork):
del builder.builders[j]
j -= 1
return builder
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't like the way this looked and went looking for some better looking code, but I only came up with:

builder.builders = next(builder for builders in builder.builders if isinstance(builder, DebianBondedNetwork))
return builder

and I'm not so sure its better. It does have the benefit of not needing to iterate through the whole list only to keep 1 item though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hah, good catch, much cleaner

Copy link
Contributor

Choose a reason for hiding this comment

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

minus the typo. It should be

builder.builders = next(builder for builder in builder.builders if isinstance(builder, DebianBondedNetwork))
return builder

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

{% endif %}
bond-master bond0
{% endfor %}
{% endif %}
Copy link
Contributor

Choose a reason for hiding this comment

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

newline

post-up route add -net {{ subnet }} gw {{ ip4priv.gateway }}
post-down route del -net {{ subnet }} gw {{ ip4priv.gateway }}
{% endfor %}
{% endif %}
Copy link
Contributor

Choose a reason for hiding this comment

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

newline

import logging
from textwrap import dedent
from jinja2 import Template, StrictUndefined
from ..utils import Tasks
Copy link
Contributor

Choose a reason for hiding this comment

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

split imports with a blank line between stdlib, 3party, this-repo

packetnetworking/distros/distro_builder.py Outdated Show resolved Hide resolved
self.task_template(
"etc/sysconfig/network-scripts/ifcfg-bond0:0",
"bonded/etc_sysconfig_network-scripts_ifcfg-bond0_0.j2",
)

# If no ip4pub is specified, the ip4priv is configured on the bond0 interface
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment isn't really very useful here, maybe instead at the top of inside the if and expanded to say bond0 will already by the private ip so we don't need bond0:0 and we don't need any extra route either?

@@ -11,3 +11,13 @@ class RedhatBuilder(DistroBuilder):
"scientificcernslc",
]
network_builders = [RedhatBondedNetwork, RedhatIndividualNetwork]
templates_base = "distros/redhat/templates"
Copy link
Contributor

Choose a reason for hiding this comment

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

it would kind of be better of templates was assumed to exist or that this os's templates are in distro/$distro/templates and handle existence/non-existence of templates in the base class machinery

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought of that, but went with this to be lazy. Will go ahead and implement that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

def build_tasks(self):
self.tasks = {}

self.task_template("etc/hostname", "etc_hostname.j2")
Copy link
Contributor

Choose a reason for hiding this comment

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

evil me want to find some utf-8 char that looks like / and use that in the template name and replace utf8-esq-/ -> / and have the base class be able to handle these common cases :D Less duplicated code.

https://www.fileformat.info/info/unicode/char/2215/index.htm :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I personally like having all the files in the same folder like this. But I could build out the actual structure. There's nothing keeping us from doing that.

Copy link
Contributor

Choose a reason for hiding this comment

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

so with the unicode char hack it would still all be in one dir, its just that we would have a character that we could use to encode filepaths in the filename. Hence the evil part :p

@@ -1,4 +1,7 @@
from .distro_builder import DistroBuilder, get_distro_builder
from ..builder import NetworkData
from .. import utils
Copy link
Contributor

Choose a reason for hiding this comment

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

these imports are in the wrong order right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

heh, not sure, flipped

@@ -1,7 +1,25 @@
#!/usr/bin/env python3

import os
from setuptools import setup, find_packages
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't this a nono?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

import os? github has been selecting wrong lines. Or are you referring to package_dir = ?

Copy link
Contributor

Choose a reason for hiding this comment

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

nope to the importing multiple things in one line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have never heard multiple imports under a package on the same line being a problem. Quick google search hasn't brought anything up about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

right you are. PEP8 explicitly allows this. Its the multiple different modules in 1 line that is a nono. Also google python style guide says

Do not use relative names in imports. Even if the module is in the same package, use the full package name. This helps prevent unintentionally importing a package twice.

But who cares.

@mikemrm mikemrm force-pushed the change-to-user-template-files branch 2 times, most recently from 9643767 to bd4275d Compare October 31, 2019 19:40
@mikemrm mikemrm requested a review from mmlb October 31, 2019 19:41

def build(self):
"""
Build executes the `DistroBuilder.build_tasks` and each `NetworkBuilder.build`
function which builds the `tasks` dictionary of files to be processed
Copy link
Contributor

Choose a reason for hiding this comment

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

but what does this mean? LIke how is build different than render?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've given this another shot.

def build_tasks(self):
self.tasks = {}

self.task_template("etc/hostname", "etc_hostname.j2")
Copy link
Contributor

Choose a reason for hiding this comment

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

so with the unicode char hack it would still all be in one dir, its just that we would have a character that we could use to encode filepaths in the filename. Hence the evil part :p

@@ -1,9 +1,12 @@
import os
import re
import sys
import json
Copy link
Contributor

Choose a reason for hiding this comment

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

click should be on its own line, and please2sort

@@ -1,7 +1,25 @@
#!/usr/bin/env python3

import os
from setuptools import setup, find_packages
Copy link
Contributor

Choose a reason for hiding this comment

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

nope to the importing multiple things in one line

Here I'm pulling the hard coded """{template}""" into actual
template files. In doing so, I also brought common files that
are shared between network configs such has hostname configuration
and hosts files into the distro builder.

By doing so, I also had to move the render and execution functions
into the distro builder. This I think makes more sense, and allows
for a better hierarchical structure to tasks.

To better assist with adding tasks and using template files, a new
parent class `Tasks` was created which allows a task to be set by
calling `self.task(task, content, write_mode, mode, fmt)` which
will build the structure accordingly. For a template file you can
call `self.task_template(task, rel_path, write_mode, mode, fmt)`.
`rel_path` is relative to the `templates_base` variable defined
in the distro builder file.
@mikemrm mikemrm force-pushed the change-to-user-template-files branch from bd4275d to c8665b7 Compare October 31, 2019 20:09
@mikemrm mikemrm requested a review from mmlb October 31, 2019 20:10
@mikemrm mikemrm merged commit 07f1a19 into master Nov 6, 2019
@mikemrm mikemrm deleted the change-to-user-template-files branch November 6, 2019 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants