forked from whamcloud/integrated-manager-for-lustre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchroma-host-discover
70 lines (53 loc) · 2.25 KB
/
chroma-host-discover
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# Copyright (c) 2018 DDN. All rights reserved.
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file.
import dbus
import gobject
import avahi
from dbus.mainloop.glib import DBusGMainLoop
import daemon
import daemon.pidlockfile
import sys
import os
bin_dir = os.path.abspath(os.path.dirname(sys.modules['__main__'].__file__))
project_dir = "/" + os.path.join(*(bin_dir.split(os.sep)[0:-2]))
sys.path.append(project_dir)
from django.core.management import setup_environ
import settings
setup_environ(settings)
from chroma_core.models.host import ManagedHost
TYPE = "_chroma-agent._tcp"
def service_resolved(interface, protocol, name, stype, domain, host,
aprotocol, address, port, txt, flags):
hostname = host[0:host.rfind(".")]
try:
ManagedHost.create_from_string(hostname)
except:
# what, oh what to do, really?
print "adding host %s failed" % hostname
def print_error(err):
print err
def myhandler(interface, protocol, name, stype, domain, flags):
if flags & avahi.LOOKUP_RESULT_LOCAL:
# local service, skip
pass
server.ResolveService(interface, protocol, name, stype, domain,
avahi.PROTO_UNSPEC, dbus.UInt32(0),
reply_handler=service_resolved,
error_handler=print_error)
with daemon.DaemonContext(pidfile = \
daemon.pidlockfile.PIDLockFile('/var/run/chroma-host-discover.pid')):
loop = DBusGMainLoop()
bus = dbus.SystemBus(mainloop=loop)
server = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
avahi.DBUS_PATH_SERVER),
avahi.DBUS_INTERFACE_SERVER)
b = dbus.Interface(bus.get_object(avahi.DBUS_NAME,
server.ServiceBrowserNew(avahi.IF_UNSPEC,
avahi.PROTO_UNSPEC,
TYPE, 'local',
dbus.UInt32(0))),
avahi.DBUS_INTERFACE_SERVICE_BROWSER)
b.connect_to_signal('ItemNew', myhandler)
gobject.MainLoop().run()