docs: replace spec.html with docbook D-Bus API reference
[NetworkManager.git] / examples / python / dbus / create-bond.py
1 #!/usr/bin/env python
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17 # Copyright (C) 2015 Red Hat, Inc.
18 #
19
20 #
21 # This example configures a Bond from ethernet devices and activates it
22 #
23 # NetworkManager D-Bus API:
24 # https://developer.gnome.org/NetworkManager/stable/spec.html
25 #
26
27 import dbus, sys, uuid
28 from dbus.mainloop.glib import DBusGMainLoop
29 import gobject
30
31 DBusGMainLoop(set_as_default=True)
32
33 def add_connection(con):
34     bus = dbus.SystemBus()
35     proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
36     settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
37     return settings.AddConnection(con)
38
39 def create_bond(bond_name):
40     bond_opts = dbus.Dictionary({'mode': '4'})
41     s_bond = dbus.Dictionary({'options': bond_opts})
42     s_con = dbus.Dictionary({
43         'type': 'bond',
44         'uuid': str(uuid.uuid4()),
45         'id': bond_name,
46         'interface-name': bond_name,
47         'autoconnect': False,
48         'autoconnect-slaves': 1})
49     s_ip4 = dbus.Dictionary({'method': 'auto'})
50     s_ip6 = dbus.Dictionary({'method': 'ignore'})
51
52     con = dbus.Dictionary({
53         'bond': s_bond,
54         'connection': s_con,
55         'ipv4': s_ip4,
56         'ipv6': s_ip6})
57     print "Creating bond connection: %s" % bond_name
58     return add_connection(con)
59
60 def create_slave(device, master):
61     slave_name = 'bond-' + master + '-slave-' + device
62     s_wired = dbus.Dictionary({'duplex': 'full'})
63     s_con = dbus.Dictionary({
64         'type': '802-3-ethernet',
65         'uuid': str(uuid.uuid4()),
66         'id': slave_name,
67         'interface-name': device,
68         'autoconnect': False,
69         'master': master,
70         'slave-type': 'bond'})
71
72     con = dbus.Dictionary({
73         '802-3-ethernet': s_wired,
74         'connection': s_con})
75     print "Creating slave connection: %s" % slave_name
76     add_connection(con)
77
78 def usage():
79     print "Usage: %s <bond_name> <ifname1> ..." % sys.argv[0]
80     sys.exit(0)
81
82
83 if len(sys.argv) < 3:
84     usage()
85
86 # Create bond master and slave connections
87 bond_name = sys.argv[1]
88 bond_path = create_bond(bond_name)
89 for ifname in sys.argv[2:]:
90     create_slave(ifname, bond_name)
91
92 # Activate the bond
93 bus = dbus.SystemBus()
94 proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
95 manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
96 ac = manager.ActivateConnection(bond_path, "/", "/")
97 print "Activating bond: %s (%s)" % (bond_name, ac)
98
99 # Monitor the active bond connection
100 loop = gobject.MainLoop()
101 def properties_changed(props):
102     if 'State' in props:
103         if props['State'] == 2:
104             print "Succesfully connected"
105             loop.quit()
106         if props['State'] == 3 or props['State'] == 4:
107             print "Bond activation failed"
108             loop.quit()
109
110 obj = bus.get_object("org.freedesktop.NetworkManager", ac)
111 iface = dbus.Interface(obj, "org.freedesktop.NetworkManager.Connection.Active")
112 iface.connect_to_signal("PropertiesChanged", properties_changed)
113
114 loop.run()