-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathdaemon_base.py
191 lines (152 loc) · 6.4 KB
/
daemon_base.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python2
try:
import imp
import signal
import subprocess
import os
import sys
import syslog
except ImportError, e:
raise ImportError (str(e) + " - required module not found")
#
# Constants ====================================================================
#
# Redis DB information
REDIS_HOSTNAME = 'localhost'
REDIS_PORT = 6379
REDIS_TIMEOUT_MSECS = 0
# Platform root directory inside docker
PLATFORM_ROOT_DOCKER = '/usr/share/sonic/platform'
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
# Port config information
PORT_CONFIG = 'port_config.ini'
PORTMAP = 'portmap.ini'
EEPROM_MODULE_NAME = 'eeprom'
EEPROM_CLASS_NAME = 'board'
#
# Helper functions =============================================================
#
def db_connect(db):
from swsscommon import swsscommon
return swsscommon.DBConnector(db,
REDIS_HOSTNAME,
REDIS_PORT,
REDIS_TIMEOUT_MSECS)
#
# Helper classes ===============================================================
#
class Logger(object):
def __init__(self, syslog_identifier = None):
self.syslog = syslog
if syslog_identifier is None:
self.syslog.openlog()
else:
self.syslog.openlog(ident=syslog_identifier, logoption=self.syslog.LOG_NDELAY, facility=self.syslog.LOG_DAEMON)
def __del__(self):
self.syslog.closelog()
def log_error(self, msg, also_print_to_console=False):
self.syslog.syslog(self.syslog.LOG_ERR, msg)
if also_print_to_console:
print msg
def log_warning(self, msg, also_print_to_console=False):
self.syslog.syslog(self.syslog.LOG_WARNING, msg)
if also_print_to_console:
print msg
def log_notice(self, msg, also_print_to_console=False):
self.syslog.syslog(self.syslog.LOG_NOTICE, msg)
if also_print_to_console:
print msg
def log_info(self, msg, also_print_to_console=False):
self.syslog.syslog(self.syslog.LOG_INFO, msg)
if also_print_to_console:
print msg
def log_debug(self, msg, also_print_to_console=False):
self.syslog.syslog(self.syslog.LOG_DEBUG, msg)
if also_print_to_console:
print msg
#
# Daemon =======================================================================
#
class DaemonBase(object):
def __init__(self):
# Register our signal handlers
signal.signal(signal.SIGHUP, self.signal_handler)
signal.signal(signal.SIGINT, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)
# Signal handler
def signal_handler(self, sig, frame):
if sig == signal.SIGHUP:
syslog.syslog(syslog.LOG_INFO, "Caught SIGHUP - ignoring...")
elif sig == signal.SIGINT:
syslog.syslog(syslog.LOG_INFO, "Caught SIGINT - exiting...")
sys.exit(128 + sig)
elif sig == signal.SIGTERM:
syslog.syslog(syslog.LOG_INFO, "Caught SIGTERM - exiting...")
sys.exit(128 + sig)
else:
syslog.syslog(syslog.LOG_WARNING, "Caught unhandled signal '" + sig + "'")
# Returns platform and hwsku
def get_platform_and_hwsku(self):
try:
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
platform = stdout.rstrip('\n')
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT)
stdout = proc.communicate()[0]
proc.wait()
hwsku = stdout.rstrip('\n')
except OSError, e:
raise OSError("Failed to detect platform: %s" % (str(e)))
return (platform, hwsku)
# Returns path to platform and hwsku
def get_path_to_platform_and_hwsku(self):
# Get platform and hwsku
(platform, hwsku) = self.get_platform_and_hwsku()
# Load platform module from source
platform_path = PLATFORM_ROOT_DOCKER
hwsku_path = "/".join([platform_path, hwsku])
return (platform_path, hwsku_path)
# Returns path to port config file
def get_path_to_port_config_file(self):
# Get platform and hwsku path
(platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku()
# First check for the presence of the new 'port_config.ini' file
port_config_file_path = "/".join([hwsku_path, PORT_CONFIG])
if not os.path.isfile(port_config_file_path):
# port_config.ini doesn't exist. Try loading the legacy 'portmap.ini' file
port_config_file_path = "/".join([hwsku_path, PORTMAP])
if not os.path.isfile(port_config_file_path):
raise IOError("Failed to detect port config file: %s" % (port_config_file_path))
return port_config_file_path
# Loads platform specific psuutil module from source
def load_platform_util(self, module_name, class_name):
platform_util = None
# Get path to platform and hwsku
(platform_path, hwsku_path) = self.get_path_to_platform_and_hwsku()
try:
module_file = "/".join([platform_path, "plugins", module_name + ".py"])
module = imp.load_source(module_name, module_file)
except IOError, e:
raise IOError("Failed to load platform module '%s': %s" % (module_name, str(e)))
try:
platform_util_class = getattr(module, class_name)
# board class of eeprom requires 4 paramerters, need special treatment here.
if module_name == EEPROM_MODULE_NAME and class_name == EEPROM_CLASS_NAME:
platform_util = platform_util_class('','','','')
else:
platform_util = platform_util_class()
except AttributeError, e:
raise AttributeError("Failed to instantiate '%s' class: %s" % (class_name, str(e)))
return platform_util
# Runs daemon
def run(self):
raise NotImplementedError()