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
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 .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pipeline:
fmt_and_lint:
image: python:3.6-alpine
commands:
- pip install black pylama
- pip install black==19.3b0 pylama
- black --check --diff .
- pylama packetnetworking setup.py

Expand Down
4 changes: 2 additions & 2 deletions packetnetworking/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def cli(
if resolvers:
builder.network.resolvers = resolvers

complete = builder.run(rootfs)
if complete is None:
tasks = builder.run(rootfs)
if not tasks:
if not quiet:
click.echo("No tasks processed", file=sys.stderr)
sys.exit(30)
Expand Down
91 changes: 2 additions & 89 deletions packetnetworking/distros/debian/bonded.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,96 +10,9 @@ def build(self):

def build_tasks(self):
self.tasks = {}
self.tasks[
"etc/network/interfaces"
] = """\
auto lo
iface lo inet loopback

auto bond0
iface bond0 inet static
{% if ip4pub %}
address {{ ip4pub.address }}
netmask {{ ip4pub.netmask }}
gateway {{ ip4pub.gateway }}
{% else %}
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
gateway {{ ip4priv.gateway }}
{% endif %}
bond-downdelay 200
bond-miimon 100
bond-mode {{ net.bonding.mode }}
bond-updelay 200
bond-xmit_hash_policy layer3+4
{% if osinfo.distro == 'ubuntu' and net.bonding.mode == 4 %}
bond-lacp-rate 1
{% endif %}
bond-slaves{% for iface in interfaces %} {{ iface.name }}{% endfor %}

dns-nameservers{% for dns in resolvers %} {{ dns }}{% endfor %}

{% if ip6pub %}
iface bond0 inet6 static
address {{ ip6pub.address }}
netmask {{ ip6pub.cidr }}
gateway {{ ip6pub.gateway }}
{% endif %}

{% if ip4pub %}
auto bond0:0
iface bond0:0 inet static
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
{% for subnet in private_subnets %}
post-up route add -net {{ subnet }} gw {{ ip4priv.gateway }}
post-down route del -net {{ subnet }} gw {{ ip4priv.gateway }}
{% endfor %}
{% endif %}
{% if osinfo.distro == 'ubuntu' %}
{% for iface in interfaces %}

auto {{ iface.name }}
iface {{ iface.name }} inet manual
{% if iface.name != interfaces[0].name %}
pre-up sleep 4
{% endif %}
bond-master bond0
{% endfor %}
{% endif %}
"""

self.tasks["etc/modules"] = {
"file_mode": "a",
"template": """\
bonding
""",
}

self.tasks[
"etc/resolv.conf"
] = """\
{% for server in resolvers %}
nameserver {{ server }}
{% endfor %}
"""

self.tasks[
"etc/hostname"
] = """\
{{ hostname }}
"""

self.tasks[
"etc/hosts"
] = """\
127.0.0.1 localhost {{ hostname }}

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
"""
self.task_template("etc/network/interfaces", "bonded/etc_network_interfaces.j2")
self.task_template("etc/modules", "bonded/etc_modules.j2", write_mode="a")

if self.metadata.operating_system.version == "14.04":
self.tasks.update(generate_persistent_names())
Expand Down
9 changes: 9 additions & 0 deletions packetnetworking/distros/debian/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@
class DebianBuilder(DistroBuilder):
distros = ["debian", "ubuntu"]
network_builders = [DebianBondedNetwork, DebianIndividualNetwork]

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

self.task_template("etc/hostname", "etc_hostname.j2")
self.task_template("etc/resolv.conf", "etc_resolv.conf.j2")
self.task_template("etc/hosts", "etc_hosts.j2")

return self.tasks
18 changes: 12 additions & 6 deletions packetnetworking/distros/debian/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ def _builder(distro, version, public=True, metadata=None):
)
builder = debianbuilder(metadata, public=public)
builder.build()
for builder in builder.builders:
if isinstance(builder, DebianBondedNetwork):
return builder
builder.builders = [
builder
for builder in builder.builders
if isinstance(builder, DebianBondedNetwork)
]
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


return _builder

Expand All @@ -69,8 +72,11 @@ def _builder(distro, version, public=True, metadata=None):
)
builder = debianbuilder(metadata, public=public)
builder.build()
for builder in builder.builders:
if isinstance(builder, DebianIndividualNetwork):
return builder
builder.builders = [
builder
for builder in builder.builders
if isinstance(builder, DebianIndividualNetwork)
]
return builder

return _builder
65 changes: 3 additions & 62 deletions packetnetworking/distros/debian/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,10 @@ def build(self):

def build_tasks(self):
self.tasks = {}
self.tasks[
"etc/network/interfaces"
] = """\
auto lo
iface lo inet loopback

auto {{ iface0.name }}
iface {{ iface0.name }} inet static
{% if ip4pub %}
address {{ ip4pub.address }}
netmask {{ ip4pub.netmask }}
gateway {{ ip4pub.gateway }}
{% else %}
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
gateway {{ ip4priv.gateway }}
{% endif %}

dns-nameservers{% for dns in resolvers %} {{ dns }}{% endfor %}

{% if ip6pub %}
iface {{ iface0.name }} inet6 static
address {{ ip6pub.address }}
netmask {{ ip6pub.cidr }}
gateway {{ ip6pub.gateway }}
{% endif %}

{% if ip4pub %}
auto {{ iface0.name }}:0
iface {{ iface0.name }}:0 inet static
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
{% for subnet in private_subnets %}
post-up route add -net {{ subnet }} gw {{ ip4priv.gateway }}
post-down route del -net {{ subnet }} gw {{ ip4priv.gateway }}
{% endfor %}
{% endif %}
"""

self.tasks[
"etc/resolv.conf"
] = """\
{% for server in resolvers %}
nameserver {{ server }}
{% endfor %}
"""

self.tasks[
"etc/hostname"
] = """\
{{ hostname }}
"""

self.tasks[
"etc/hosts"
] = """\
127.0.0.1 localhost {{ hostname }}

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
"""
self.task_template(
"etc/network/interfaces", "individual/etc_network_interfaces.j2"
)

if self.metadata.operating_system.version == "14.04":
self.tasks.update(generate_persistent_names())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bonding
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
auto lo
iface lo inet loopback

auto bond0
iface bond0 inet static
{% if ip4pub %}
address {{ ip4pub.address }}
netmask {{ ip4pub.netmask }}
gateway {{ ip4pub.gateway }}
{% else %}
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
gateway {{ ip4priv.gateway }}
{% endif %}
bond-downdelay 200
bond-miimon 100
bond-mode {{ net.bonding.mode }}
bond-updelay 200
bond-xmit_hash_policy layer3+4
{% if osinfo.distro == 'ubuntu' and net.bonding.mode == 4 %}
bond-lacp-rate 1
{% endif %}
bond-slaves{% for iface in interfaces %} {{ iface.name }}{% endfor %}

dns-nameservers{% for dns in resolvers %} {{ dns }}{% endfor %}

{% if ip6pub %}
iface bond0 inet6 static
address {{ ip6pub.address }}
netmask {{ ip6pub.cidr }}
gateway {{ ip6pub.gateway }}
{% endif %}

{% if ip4pub %}
auto bond0:0
iface bond0:0 inet static
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
{% for subnet in private_subnets %}
post-up route add -net {{ subnet }} gw {{ ip4priv.gateway }}
post-down route del -net {{ subnet }} gw {{ ip4priv.gateway }}
{% endfor %}
{% endif %}
{% if osinfo.distro == 'ubuntu' %}
{% for iface in interfaces %}

auto {{ iface.name }}
iface {{ iface.name }} inet manual
{% if iface.name != interfaces[0].name %}
pre-up sleep 4
{% endif %}
bond-master bond0
{% endfor %}
{% endif %}
1 change: 1 addition & 0 deletions packetnetworking/distros/debian/templates/etc_hostname.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ hostname }}
6 changes: 6 additions & 0 deletions packetnetworking/distros/debian/templates/etc_hosts.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
127.0.0.1 localhost {{ hostname }}

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
3 changes: 3 additions & 0 deletions packetnetworking/distros/debian/templates/etc_resolv.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% for server in resolvers %}
nameserver {{ server }}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
auto lo
iface lo inet loopback

auto {{ iface0.name }}
iface {{ iface0.name }} inet static
{% if ip4pub %}
address {{ ip4pub.address }}
netmask {{ ip4pub.netmask }}
gateway {{ ip4pub.gateway }}
{% else %}
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
gateway {{ ip4priv.gateway }}
{% endif %}

dns-nameservers{% for dns in resolvers %} {{ dns }}{% endfor %}

{% if ip6pub %}
iface {{ iface0.name }} inet6 static
address {{ ip6pub.address }}
netmask {{ ip6pub.cidr }}
gateway {{ ip6pub.gateway }}
{% endif %}

{% if ip4pub %}
auto {{ iface0.name }}:0
iface {{ iface0.name }}:0 inet static
address {{ ip4priv.address }}
netmask {{ ip4priv.netmask }}
{% for subnet in private_subnets %}
post-up route add -net {{ subnet }} gw {{ ip4priv.gateway }}
post-down route del -net {{ subnet }} gw {{ ip4priv.gateway }}
{% endfor %}
{% endif %}
Loading