~cloud-init-dev/cloud-init/trunk

« back to all changes in this revision

Viewing changes to cloudinit/net/udev.py

  • Committer: Scott Moser
  • Date: 2016-08-10 15:06:15 UTC
  • Revision ID: smoser@ubuntu.com-20160810150615-ma2fv107w3suy1ma
README: Mention move of revision control to git.

cloud-init development has moved its revision control to git.
It is available at 
  https://code.launchpad.net/cloud-init

Clone with 
  git clone https://git.launchpad.net/cloud-init
or
  git clone git+ssh://git.launchpad.net/cloud-init

For more information see
  https://git.launchpad.net/cloud-init/tree/HACKING.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#   Copyright (C) 2015 Canonical Ltd.
2
 
#
3
 
#   Author: Ryan Harper <ryan.harper@canonical.com>
4
 
#
5
 
#   Curtin is free software: you can redistribute it and/or modify it under
6
 
#   the terms of the GNU Affero General Public License as published by the
7
 
#   Free Software Foundation, either version 3 of the License, or (at your
8
 
#   option) any later version.
9
 
#
10
 
#   Curtin is distributed in the hope that it will be useful, but WITHOUT ANY
11
 
#   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
 
#   FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
13
 
#   more details.
14
 
#
15
 
#   You should have received a copy of the GNU Affero General Public License
16
 
#   along with Curtin.  If not, see <http://www.gnu.org/licenses/>.
17
 
 
18
 
 
19
 
def compose_udev_equality(key, value):
20
 
    """Return a udev comparison clause, like `ACTION=="add"`."""
21
 
    assert key == key.upper()
22
 
    return '%s=="%s"' % (key, value)
23
 
 
24
 
 
25
 
def compose_udev_attr_equality(attribute, value):
26
 
    """Return a udev attribute comparison clause, like `ATTR{type}=="1"`."""
27
 
    assert attribute == attribute.lower()
28
 
    return 'ATTR{%s}=="%s"' % (attribute, value)
29
 
 
30
 
 
31
 
def compose_udev_setting(key, value):
32
 
    """Return a udev assignment clause, like `NAME="eth0"`."""
33
 
    assert key == key.upper()
34
 
    return '%s="%s"' % (key, value)
35
 
 
36
 
 
37
 
def generate_udev_rule(interface, mac):
38
 
    """Return a udev rule to set the name of network interface with `mac`.
39
 
 
40
 
    The rule ends up as a single line looking something like:
41
 
 
42
 
    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
43
 
    ATTR{address}="ff:ee:dd:cc:bb:aa", NAME="eth0"
44
 
    """
45
 
    rule = ', '.join([
46
 
        compose_udev_equality('SUBSYSTEM', 'net'),
47
 
        compose_udev_equality('ACTION', 'add'),
48
 
        compose_udev_equality('DRIVERS', '?*'),
49
 
        compose_udev_attr_equality('address', mac),
50
 
        compose_udev_setting('NAME', interface),
51
 
    ])
52
 
    return '%s\n' % rule
53
 
 
54
 
# vi: ts=4 expandtab syntax=python