本文整理汇总了Python中freenas.dispatcher.client.Client.on_error方法的典型用法代码示例。如果您正苦于以下问题:Python Client.on_error方法的具体用法?Python Client.on_error怎么用?Python Client.on_error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类freenas.dispatcher.client.Client
的用法示例。
在下文中一共展示了Client.on_error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Context
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Context(object):
def __init__(self, *args, **kwargs):
self.client = None
self.entity_subscribers = {}
def start_entity_subscribers(self):
for i in ENTITY_SUBSCRIBERS:
if i in self.entity_subscribers:
self.entity_subscribers[i].stop()
del self.entity_subscribers[i]
e = EntitySubscriber(self.client, i)
e.start()
self.entity_subscribers[i] = e
def wait_entity_subscribers(self):
for i in self.entity_subscribers.values():
i.wait_ready()
def connect(self):
while True:
try:
self.client.connect("unix:")
self.client.login_service("collectd_{0}".format(PLUGIN_NAME))
# enable streaming responses as they are needed but entitysubscriber for
# reliable performace and such
self.client.call_sync("management.enable_features", ["streaming_responses"])
self.start_entity_subscribers()
self.wait_entity_subscribers()
return
except (OSError, RpcException) as err:
collectd.warning(
"{0} collectd plugin could not connect to server retrying in 5 seconds".format(PLUGIN_NAME)
)
time.sleep(5)
def connection_error(self, event, **kwargs):
if event in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
collectd.info("{0} collectd plugin connection to dispatcher lost".format(PLUGIN_NAME))
self.connect()
def init_dispatcher(self):
self.client = Client()
self.client.on_error(self.connection_error)
self.connect()
def disk_temps(self):
for disk in self.entity_subscribers["disk"].query(("status.smart_info.temperature", "!=", None)):
yield (disk["name"], disk["status"]["smart_info"]["temperature"])
示例2: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Main(object):
def __init__(self):
self.logger = logging.getLogger('alertd')
self.config = None
self.datastore = None
self.configstore = None
self.client = None
self.plugin_dirs = []
self.emitters = {}
def init_datastore(self):
try:
self.datastore = datastore.get_datastore()
except datastore.DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.on_error(on_error)
self.connect()
def init_reminder(self):
t = threading.Thread(target=self.reminder_thread)
t.daemon = True
t.start()
def parse_config(self, filename):
try:
f = open(filename, 'r')
self.config = json.load(f)
f.close()
except IOError as err:
self.logger.error('Cannot read config file: %s', err.message)
sys.exit(1)
except ValueError:
self.logger.error('Config file has unreadable format (not valid JSON)')
sys.exit(1)
self.plugin_dirs = self.config['alertd']['plugin-dirs']
def connect(self):
while True:
try:
self.client.connect('unix:')
self.client.login_service('alertd')
self.client.enable_server()
self.client.register_service('alertd.management', ManagementService(self))
self.client.register_service('alertd.alert', AlertService(self))
self.client.register_service('alertd.debug', DebugService())
self.client.resume_service('alertd.management')
self.client.resume_service('alertd.alert')
self.client.resume_service('alertd.debug')
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def scan_plugins(self):
for i in self.plugin_dirs:
self.scan_plugin_dir(i)
def scan_plugin_dir(self, dir):
self.logger.debug('Scanning plugin directory %s', dir)
for f in os.listdir(dir):
name, ext = os.path.splitext(os.path.basename(f))
if ext != '.py':
continue
try:
plugin = imp.load_source(name, os.path.join(dir, f))
plugin._init(self)
except:
self.logger.error('Cannot initialize plugin {0}'.format(f), exc_info=True)
def emit_alert(self, alert):
self.logger.debug('Emitting alert <id:{0}> (class {1})'.format(alert['id'], alert['class']))
for i in self.datastore.query('alert.filters'):
for predicate in i.get('predicates', []):
if predicate['operator'] not in operators_table:
continue
if not operators_table[predicate['operator']](alert[predicate['property']], predicate['value']):
break
else:
try:
emitter = self.emitters.get(i['emitter'])
if not emitter:
self.logger.warning('Invalid emitter {0} for alert filter {1}'.format(i['emitter'], i['id']))
continue
self.logger.debug('Alert <id:{0}> matched filter {1}'.format(alert['id'], i['id']))
if alert['send_count'] > 0:
#.........这里部分代码省略.........
示例3: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
#.........这里部分代码省略.........
self.client.call_sync(
'alert.register_alert',
'stat.{0}.too_low'.format(name),
'{0} statistic value is too low'.format(name)
)
return alert_config
def get_data_source(self, name):
if name not in list(self.data_sources.keys()):
config = DataSourceConfig(self.datastore, name)
alert_config = self.init_alert_config(name)
ds = DataSource(self, name, config, alert_config)
self.data_sources[name] = ds
self.client.call_sync('plugin.register_event_type', 'statd.output', 'statd.{0}.pulse'.format(name))
return self.data_sources[name]
def register_schemas(self):
self.client.register_schema('stat', {
'type': 'object',
'additionalProperties': True,
'properties': {
'name': {'type': 'string'},
'last_value': {'type': ['integer', 'number', 'null']},
'alerts': {'$ref': 'stat-alert'},
}
})
self.client.register_schema('stat-alert', {
'type': 'object',
'additionalProperties': True,
'properties': {
'alert_high': {'type': ['integer', 'number', 'null']},
'alert_high_enabled': {'type': 'boolean'},
'alert_low': {'type': ['integer', 'number', 'null']},
'alert_low_enabled': {'type': 'boolean'}
}
})
def connect(self):
while True:
try:
self.client.connect('unix:')
self.client.login_service('statd')
self.client.enable_server()
self.client.register_service('statd.output', OutputService(self))
self.client.register_service('statd.alert', AlertService(self))
self.client.register_service('statd.debug', DebugService(gevent=True))
self.client.resume_service('statd.output')
self.client.resume_service('statd.alert')
self.client.resume_service('statd.debug')
for i in list(self.data_sources.keys()):
self.client.call_sync('plugin.register_event_type', 'statd.output', 'statd.{0}.pulse'.format(i))
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.use_bursts = True
self.client.on_error(on_error)
self.connect()
def die(self):
self.logger.warning('Exiting')
self.server.stop()
self.client.disconnect()
sys.exit(0)
def dispatcher_error(self, error):
self.die()
def main(self):
parser = argparse.ArgumentParser()
parser.add_argument('-c', metavar='CONFIG', default=DEFAULT_CONFIGFILE, help='Middleware config file')
args = parser.parse_args()
configure_logging('/var/log/fnstatd.log', 'DEBUG')
setproctitle.setproctitle('fnstatd')
# Signal handlers
gevent.signal(signal.SIGQUIT, self.die)
gevent.signal(signal.SIGTERM, self.die)
gevent.signal(signal.SIGINT, self.die)
self.server = InputServer(self)
self.config = args.c
self.init_datastore()
self.init_dispatcher()
self.init_database()
self.register_schemas()
self.server.start()
self.logger.info('Started')
self.client.wait_forever()
示例4: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
#.........这里部分代码省略.........
# Remove unplugged NICs from DB
for i in self.datastore.query('network.interfaces', ('id', 'nin', existing), ('cloned', '=', False)):
self.datastore.delete('network.interfaces', i['id'])
def init_datastore(self):
try:
self.datastore = get_datastore(self.config)
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def connect(self, resume=False):
while True:
try:
self.client.connect('unix:')
self.client.login_service('networkd')
self.client.enable_server()
self.register_schemas()
self.client.register_service('networkd.configuration', ConfigurationService(self))
self.client.register_service('networkd.debug', DebugService())
if resume:
self.client.resume_service('networkd.configuration')
self.client.resume_service('networkd.debug')
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect(resume=True)
self.client = Client()
self.client.on_error(on_error)
self.connect()
def init_routing_socket(self):
self.rtsock_thread = RoutingSocketEventSource(self)
self.rtsock_thread.start()
def register_schemas(self):
self.client.register_schema('NetworkAggregationProtocols', {
'type': 'string',
'enum': list(netif.AggregationProtocol.__members__.keys())
})
self.client.register_schema('NetworkLaggPortFlags', {
'type': 'array',
'items': {'$ref': 'NetworkLaggPortFlagsItems'}
})
self.client.register_schema('NetworkLaggPortFlagsItems', {
'type': 'string',
'enum': list(netif.LaggPortFlags.__members__.keys())
})
self.client.register_schema('NetworkInterfaceFlags', {
'type': 'array',
'items': {'$ref': 'NetworkInterfaceFlagsItems'}
})
示例5: Context
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Context(object):
def __init__(self):
self.server = None
self.client = None
self.jobs = {}
self.provides = set()
self.lock = RLock()
self.kq = select.kqueue()
self.devnull = os.open('/dev/null', os.O_RDWR)
self.logger = logging.getLogger('Context')
self.rpc = RpcContext()
self.rpc.register_service_instance('serviced.management', ManagementService(self))
self.rpc.register_service_instance('serviced.job', JobService(self))
def init_dispatcher(self):
if self.client and self.client.connected:
return
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.on_error(on_error)
self.connect()
def init_server(self, address):
self.server = Server(self)
self.server.rpc = self.rpc
self.server.streaming = True
self.server.start(address, transport_options={'permissions': 0o777})
thread = Thread(target=self.server.serve_forever)
thread.name = 'ServerThread'
thread.daemon = True
thread.start()
def provide(self, targets):
def doit():
self.logger.debug('Adding dependency targets: {0}'.format(', '.join(targets)))
with self.lock:
self.provides |= targets
for job in list(self.jobs.values()):
if job.state == JobState.STOPPED and job.requires <= self.provides:
job.start()
if targets:
Timer(2, doit).start()
def job_by_pid(self, pid):
job = first_or_default(lambda j: j.pid == pid, self.jobs.values())
return job
def event_loop(self):
while True:
with contextlib.suppress(InterruptedError):
for ev in self.kq.control(None, MAX_EVENTS):
self.logger.log(TRACE, 'New event: {0}'.format(ev))
if ev.filter == select.KQ_FILTER_PROC:
job = self.job_by_pid(ev.ident)
if job:
job.pid_event(ev)
continue
if ev.fflags & select.KQ_NOTE_CHILD:
if ev.fflags & select.KQ_NOTE_EXIT:
continue
pjob = self.job_by_pid(ev.data)
if not pjob:
self.untrack_pid(ev.ident)
continue
# Stop tracking at session ID boundary
try:
if pjob.pgid != os.getpgid(ev.ident):
self.untrack_pid(ev.ident)
continue
except ProcessLookupError:
continue
with self.lock:
job = Job(self)
job.load_anonymous(pjob, ev.ident)
self.jobs[job.id] = job
self.logger.info('Added job {0}'.format(job.label))
def track_pid(self, pid):
ev = select.kevent(
pid,
select.KQ_FILTER_PROC,
select.KQ_EV_ADD | select.KQ_EV_ENABLE,
select.KQ_NOTE_EXIT | select.KQ_NOTE_EXEC | select.KQ_NOTE_FORK | select.KQ_NOTE_TRACK,
0, 0
)
self.kq.control([ev], 0)
def untrack_pid(self, pid):
ev = select.kevent(
#.........这里部分代码省略.........
示例6: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Main(object):
def __init__(self):
self.client = None
self.server = None
self.datastore = None
self.hdf = None
self.hdf_group = None
self.config = None
self.logger = logging.getLogger('statd')
self.data_sources = {}
def parse_config(self, filename):
try:
f = open(filename, 'r')
self.config = json.load(f)
f.close()
except IOError as err:
self.logger.error('Cannot read config file: %s', err.message)
sys.exit(1)
except ValueError:
self.logger.error('Config file has unreadable format (not valid JSON)')
sys.exit(1)
def init_datastore(self):
try:
self.datastore = get_datastore(self.config['datastore']['driver'], self.config['datastore']['dsn'])
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
def init_database(self):
# adding this try/except till system-dataset plugin is added back in in full fidelity
# just a hack (since that directory's data will not persist)
# Please remove this when system-dataset plugin is added back in
try:
directory = self.client.call_sync('system_dataset.request_directory', 'statd')
except RpcException:
directory = '/var/tmp/statd'
if not os.path.exists(directory):
os.makedirs(directory)
self.hdf = tables.open_file(os.path.join(directory, DEFAULT_DBFILE), mode='a')
if not hasattr(self.hdf.root, 'stats'):
self.hdf.create_group('/', 'stats')
self.hdf_group = self.hdf.root.stats
def request_table(self, name):
try:
if hasattr(self.hdf_group, name):
return getattr(self.hdf_group, name)
return self.hdf.create_table(self.hdf_group, name, DataPoint, name)
except Exception as e:
self.logger.error(str(e))
def get_data_source(self, name):
if name not in list(self.data_sources.keys()):
config = DataSourceConfig(self.datastore, name)
ds = DataSource(self, name, config)
self.data_sources[name] = ds
self.client.call_sync('plugin.register_event_type', 'statd.output', 'statd.{0}.pulse'.format(name))
return self.data_sources[name]
def connect(self):
while True:
try:
self.client.connect('127.0.0.1')
self.client.login_service('statd')
self.client.enable_server()
self.client.register_service('statd.output', OutputService(self))
self.client.register_service('statd.debug', DebugService(gevent=True))
self.client.resume_service('statd.output')
self.client.resume_service('statd.debug')
for i in list(self.data_sources.keys()):
self.client.call_sync('plugin.register_event_type', 'statd.output', 'statd.{0}.pulse'.format(i))
return
except socket.error as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.use_bursts = True
self.client.on_error(on_error)
self.connect()
def die(self):
self.logger.warning('Exiting')
self.server.stop()
self.client.disconnect()
sys.exit(0)
def dispatcher_error(self, error):
#.........这里部分代码省略.........
示例7: RESTApi
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class RESTApi(object):
def __init__(self):
self.logger = logging.getLogger('restd')
self._cruds = []
self._threads = []
self._rpcs = {}
self._schemas = {}
self._used_schemas = set()
self._services = {}
self._tasks = {}
self.api = falcon.API(middleware=[
AuthMiddleware(),
JSONTranslator(),
])
self.api.add_route('/', SwaggerResource(self))
gevent.signal(signal.SIGINT, self.die)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.dispatcher = Client()
self.dispatcher.on_error(on_error)
self.connect()
def init_metadata(self):
self._tasks = self.dispatcher.call_sync('discovery.get_tasks')
self._schemas = self.dispatcher.call_sync('discovery.get_schema')
for service in self.dispatcher.call_sync('discovery.get_services'):
self._services[service] = self.dispatcher.call_sync('discovery.get_methods', service)
for method in self._services[service]:
self._rpcs['{0}.{1}'.format(service, method['name'])] = method
def load_plugins(self):
pluginsdir = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
for i in glob.glob1(pluginsdir, "*.py"):
try:
loader = importlib.machinery.SourceFileLoader(i.split('.')[0], os.path.join(pluginsdir, i))
mod = loader.load_module()
except:
self.logger.error('Failed to load plugin %s', i, exc_info=True)
raise
mod._init(self)
def connect(self):
while True:
try:
self.dispatcher.connect('unix:')
self.dispatcher.login_service('restd')
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def __call__(self, environ, start_response):
if 'HTTP_X_REAL_IP' in environ:
environ['PATH_INFO'] = environ.get('PATH_INFO', '').replace('/api/v2.0', '', 1)
return self.api.__call__(environ, start_response)
def register_crud(self, klass):
ins = klass(self, self.dispatcher)
self._cruds.append(ins)
def register_singleitem(self, klass):
klass(self, self.dispatcher)
def register_resource(self, klass):
klass(self)
def run(self):
self.init_dispatcher()
self.init_metadata()
self.load_plugins()
server4 = WSGIServer(('', 8889), self, handler_class=RESTWSGIHandler)
self._threads = [gevent.spawn(server4.serve_forever)]
checkin()
gevent.joinall(self._threads)
def die(self, *args):
gevent.killall(self._threads)
sys.exit(0)
示例8: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Main(object):
def __init__(self):
self.client = None
self.datastore = None
self.configstore = None
self.config = None
self.mgmt = None
self.vm_started = Event()
self.containers = {}
self.tokens = {}
self.logger = logging.getLogger('containerd')
self.bridge_interface = None
self.used_nmdms = []
def init_datastore(self):
try:
self.datastore = get_datastore(self.config)
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def allocate_nmdm(self):
for i in range(0, 255):
if i not in self.used_nmdms:
self.used_nmdms.append(i)
return i
def release_nmdm(self, index):
self.used_nmdms.remove(index)
def connect(self):
while True:
try:
self.client.connect('unix:')
self.client.login_service('containerd')
self.client.enable_server()
self.client.register_service('containerd.management', ManagementService(self))
self.client.register_service('containerd.debug', DebugService(gevent=True, builtins={"context": self}))
self.client.resume_service('containerd.management')
self.client.resume_service('containerd.debug')
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.use_bursts = True
self.client.on_error(on_error)
self.connect()
def init_mgmt(self):
self.mgmt = ManagementNetwork(self, MGMT_INTERFACE, MGMT_ADDR)
self.mgmt.up()
self.mgmt.bridge_if.add_address(netif.InterfaceAddress(
netif.AddressFamily.INET,
ipaddress.ip_interface('169.254.169.254/32')
))
def init_nat(self):
default_if = self.client.call_sync('networkd.configuration.get_default_interface')
if not default_if:
self.logger.warning('No default route interface; not configuring NAT')
return
p = pf.PF()
# Try to find and remove existing NAT rules for the same subnet
oldrule = first_or_default(
lambda r: r.src.address.address == MGMT_ADDR.network.network_address,
p.get_rules('nat')
)
if oldrule:
p.delete_rule('nat', oldrule.index)
rule = pf.Rule()
rule.src.address.address = MGMT_ADDR.network.network_address
rule.src.address.netmask = MGMT_ADDR.netmask
rule.action = pf.RuleAction.NAT
rule.af = socket.AF_INET
rule.ifname = default_if
rule.redirect_pool.append(pf.Address(ifname=default_if))
rule.proxy_ports = [50001, 65535]
p.append_rule('nat', rule)
try:
p.enable()
except OSError as err:
if err.errno != errno.EEXIST:
raise err
#.........这里部分代码省略.........
示例9: Context
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Context(object):
def __init__(self):
self.logger = logging.getLogger('schedulerd')
self.config = None
self.datastore = None
self.configstore = None
self.client = None
self.scheduler = None
self.active_tasks = {}
def init_datastore(self):
try:
self.datastore = get_datastore(self.config)
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.on_error(on_error)
self.connect()
def init_scheduler(self):
store = FreeNASJobStore()
self.scheduler = BackgroundScheduler(jobstores={'default': store, 'temp': MemoryJobStore()}, timezone=pytz.utc)
self.scheduler.start()
def connect(self):
while True:
try:
self.client.connect('unix:')
self.client.login_service('schedulerd')
self.client.enable_server()
self.client.register_service('scheduler.management', ManagementService(self))
self.client.register_service('scheduler.debug', DebugService())
self.client.resume_service('scheduler.management')
self.client.resume_service('scheduler.debug')
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def run_job(self, *args, **kwargs):
tid = self.client.call_sync('task.submit_with_env', args[0], args[1:], {
'RUN_AS_USER': 'root',
'CALENDAR_TASK_NAME': kwargs.get('name')
})
self.active_tasks[kwargs['id']] = tid
self.client.call_sync('task.wait', tid, timeout=None)
result = self.client.call_sync('task.status', tid)
if result['state'] != 'FINISHED':
try:
self.client.call_sync('alert.emit', {
'name': 'scheduler.task.failed',
'severity': 'CRITICAL',
'description': 'Task {0} has failed: {1}'.format(
kwargs.get('name', tid),
result['error']['message']
),
})
except RpcException as e:
self.logger.error('Failed to emit alert', exc_info=True)
del self.active_tasks[kwargs['id']]
self.datastore.insert('schedulerd.runs', {
'job_id': kwargs['id'],
'task_id': result['id']
})
def emit_event(self, name, params):
self.client.emit_event(name, params)
def checkin(self):
checkin()
def main(self):
parser = argparse.ArgumentParser()
parser.add_argument('-c', metavar='CONFIG', default=DEFAULT_CONFIGFILE, help='Middleware config file')
parser.add_argument('-f', action='store_true', default=False, help='Run in foreground')
args = parser.parse_args()
configure_logging('/var/log/schedulerd.log', 'DEBUG')
setproctitle('schedulerd')
self.config = args.c
self.init_datastore()
self.init_scheduler()
self.init_dispatcher()
self.checkin()
self.client.wait_forever()
示例10: __init__
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
#.........这里部分代码省略.........
# Remove unplugged NICs from DB
for i in self.datastore.query('network.interfaces', ('id', 'nin', existing), ('cloned', '=', False)):
self.datastore.delete('network.interfaces', i['id'])
def init_datastore(self):
try:
self.datastore = get_datastore(self.config)
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def connect(self, resume=False):
while True:
try:
self.client.connect('unix:')
self.client.login_service('networkd')
self.client.enable_server()
self.register_schemas()
self.client.register_service('networkd.configuration', ConfigurationService(self))
self.client.register_service('networkd.debug', DebugService())
if resume:
self.client.resume_service('networkd.configuration')
self.client.resume_service('networkd.debug')
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect(resume=True)
self.client = Client()
self.client.on_error(on_error)
self.connect()
def init_routing_socket(self):
self.rtsock_thread = RoutingSocketEventSource(self)
self.rtsock_thread.start()
def register_schemas(self):
self.client.register_schema('network-aggregation-protocols', {
'type': 'string',
'enum': list(netif.AggregationProtocol.__members__.keys())
})
self.client.register_schema('network-interface-flags', {
'type': 'array',
'items': {
'type': 'string',
'enum': list(netif.InterfaceFlags.__members__.keys())
}
})
self.client.register_schema('network-interface-capabilities', {
'type': 'array',
'items': {
'type': 'string',
'enum': list(netif.InterfaceCapability.__members__.keys())
}
示例11: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Main(object):
def __init__(self):
self.config = None
self.client = None
self.datastore = None
self.configstore = None
self.logger = logging.getLogger('dsd')
def parse_config(self, filename):
try:
f = open(filename, 'r')
self.config = json.load(f)
f.close()
except IOError as err:
self.logger.error('Cannot read config file: %s', err.message)
sys.exit(1)
except ValueError as err:
self.logger.error('Config file has unreadable format (not valid JSON)')
sys.exit(1)
def init_datastore(self, resume=False):
try:
self.datastore = get_datastore(self.config['datastore']['driver'],
self.config['datastore']['dsn'])
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def connect(self, resume=False):
while True:
try:
self.client.connect('127.0.0.1')
self.client.login_service('dsd')
self.client.enable_server()
self.register_schemas()
self.client.register_service('dsd.configuration', DSDConfigurationService(self))
self.client.register_service('dsd.debug', DebugService())
if resume:
self.client.resume_service('dsd.configuration')
self.client.resume_service('dsd.debug')
return
except socket.error as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect(resume=True)
self.client = Client()
self.client.on_error(on_error)
self.connect()
def register_schemas(self):
# XXX do stuff here? To be determined ...
pass
def report_error(self, message, exception):
if not os.path.isdir('/var/tmp/crash'):
try:
os.mkdir('/var/tmp/crash')
except:
return
report = {
'timestamp': str(datetime.datetime.now()),
'type': 'exception',
'application': 'dsd',
'message': message,
'exception': str(exception),
'traceback': traceback.format_exc()
}
try:
with tempfile.NamedTemporaryFile(dir='/var/tmp/crash', suffix='.json', prefix='report-', delete=False) as f:
json.dump(report, f, indent=4)
except:
pass
def main(self):
parser = argparse.ArgumentParser()
parser.add_argument('-c', metavar='CONFIG', default=DEFAULT_CONFIGFILE, help='Middleware config file')
args = parser.parse_args()
configure_logging('/var/log/dsd.log', 'DEBUG')
setproctitle.setproctitle('dsd')
self.parse_config(args.c)
self.init_datastore()
self.init_dispatcher()
self.client.resume_service('dsd.configuration')
self.logger.info('Started')
self.client.wait_forever()
示例12: __init__
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Main:
def __init__(self):
self.config = None
self.client = None
self.datastore = None
self.configstore = None
self.rtsock_thread = None
self.logger = logging.getLogger('networkd')
def configure_dhcp(self, interface):
# Check if dhclient is running
if os.path.exists(os.path.join('/var/run', 'dhclient.{0}.pid'.format(interface))):
self.logger.info('Interface {0} already configured by DHCP'.format(interface))
return True
# XXX: start dhclient through launchd in the future
ret = subprocess.call(['/sbin/dhclient', interface])
return ret == 0
def interface_detached(self, name):
self.logger.warn('Interface {0} detached from the system'.format(name))
def interface_attached(self, name):
self.logger.warn('Interface {0} attached to the system'.format(name))
def using_dhcp_for_gateway(self):
for i in self.datastore.query('network.interfaces'):
if i.get('dhcp') and self.configstore.get('network.dhcp.assign_gateway'):
return True
return False
def scan_interfaces(self):
self.logger.info('Scanning available network interfaces...')
existing = []
# Add newly plugged NICs to DB
for i in list(netif.list_interfaces().values()):
# We want only physical NICs
if i.cloned:
continue
existing.append(i.name)
if not self.datastore.exists('network.interfaces', ('id', '=', i.name)):
self.logger.info('Found new interface {0} ({1})'.format(i.name, i.type.name))
self.datastore.insert('network.interfaces', {
'enabled': False,
'id': i.name,
'type': i.type.name
})
# Remove unplugged NICs from DB
for i in self.datastore.query('network.interfaces', ('id', 'nin', existing)):
self.datastore.delete('network.interfaces', i['id'])
def parse_config(self, filename):
try:
f = open(filename, 'r')
self.config = json.load(f)
f.close()
except IOError as err:
self.logger.error('Cannot read config file: %s', err.message)
sys.exit(1)
except ValueError as err:
self.logger.error('Config file has unreadable format (not valid JSON)')
sys.exit(1)
def init_datastore(self):
try:
self.datastore = get_datastore(self.config['datastore']['driver'], self.config['datastore']['dsn'])
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def connect(self, resume=False):
while True:
try:
self.client.connect('127.0.0.1')
self.client.login_service('networkd')
self.client.enable_server()
self.register_schemas()
self.client.register_service('networkd.configuration', ConfigurationService(self))
self.client.register_service('networkd.debug', DebugService())
if resume:
self.client.resume_service('networkd.configuration')
self.client.resume_service('networkd.debug')
return
except socket.error as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect(resume=True)
#.........这里部分代码省略.........
示例13: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Main(object):
def __init__(self):
self.client = None
self.datastore = None
self.config = None
self.containers = {}
self.tokens = {}
self.logger = logging.getLogger('containerd')
def parse_config(self, filename):
try:
f = open(filename, 'r')
self.config = json.load(f)
f.close()
except IOError as err:
self.logger.error('Cannot read config file: %s', err.message)
sys.exit(1)
except ValueError:
self.logger.error('Config file has unreadable format (not valid JSON)')
sys.exit(1)
def init_datastore(self):
try:
self.datastore = get_datastore(self.config['datastore']['driver'], self.config['datastore']['dsn'])
except DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
def connect(self):
while True:
try:
self.client.connect('127.0.0.1')
self.client.login_service('containerd')
self.client.enable_server()
self.client.register_service('containerd.management', ManagementService(self))
self.client.register_service('containerd.debug', DebugService(gevent=True))
self.client.resume_service('containerd.management')
self.client.resume_service('containerd.debug')
return
except socket.error as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.use_bursts = True
self.client.on_error(on_error)
self.connect()
def die(self):
self.logger.warning('Exiting')
self.client.disconnect()
sys.exit(0)
def generate_id(self):
return ''.join([random.choice(string.ascii_letters + string.digits) for n in range(32)])
def dispatcher_error(self, error):
self.die()
def main(self):
parser = argparse.ArgumentParser()
parser.add_argument('-c', metavar='CONFIG', default=DEFAULT_CONFIGFILE, help='Middleware config file')
parser.add_argument('-p', type=int, metavar='PORT', default=5500, help="WebSockets server port")
args = parser.parse_args()
configure_logging('/var/log/containerd.log', 'DEBUG')
setproctitle.setproctitle('containerd')
self.parse_config(args.c)
self.init_datastore()
self.init_dispatcher()
self.logger.info('Started')
# WebSockets server
kwargs = {}
s4 = WebSocketServer(('', args.p), ServerResource({
'/console': ConsoleConnection,
}, context=self), **kwargs)
s6 = WebSocketServer(('::', args.p), ServerResource({
'/console': ConsoleConnection,
}, context=self), **kwargs)
serv_threads = [gevent.spawn(s4.serve_forever), gevent.spawn(s6.serve_forever)]
gevent.joinall(serv_threads)
示例14: Main
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Main(object):
def __init__(self):
self.logger = logging.getLogger('etcd')
self.root = None
self.configfile = None
self.config = None
self.datastore = None
self.configstore = None
self.client = None
self.plugin_dirs = []
self.renderers = {}
self.managed_files = {}
def init_datastore(self):
try:
self.datastore = datastore.get_datastore(self.configfile)
except datastore.DatastoreException as err:
self.logger.error('Cannot initialize datastore: %s', str(err))
sys.exit(1)
self.configstore = ConfigStore(self.datastore)
def init_dispatcher(self):
def on_error(reason, **kwargs):
if reason in (ClientError.CONNECTION_CLOSED, ClientError.LOGOUT):
self.logger.warning('Connection to dispatcher lost')
self.connect()
self.client = Client()
self.client.on_error(on_error)
self.connect()
def connect(self):
while True:
try:
self.client.connect('unix:')
self.client.login_service('etcd')
self.client.enable_server()
self.client.register_service('etcd.generation', FileGenerationService(self))
self.client.register_service('etcd.management', ManagementService(self))
self.client.register_service('etcd.debug', DebugService())
self.client.resume_service('etcd.generation')
self.client.resume_service('etcd.management')
self.client.resume_service('etcd.debug')
return
except (OSError, RpcException) as err:
self.logger.warning('Cannot connect to dispatcher: {0}, retrying in 1 second'.format(str(err)))
time.sleep(1)
def init_renderers(self):
for name, impl in TEMPLATE_RENDERERS.items():
self.renderers[name] = impl(self)
def parse_config(self, filename):
try:
f = open(filename, 'r')
self.config = json.load(f)
f.close()
except IOError as err:
self.logger.error('Cannot read config file: %s', err.message)
sys.exit(1)
except ValueError:
self.logger.error('Config file has unreadable format (not valid JSON)')
sys.exit(1)
self.plugin_dirs = self.config['etcd']['plugin-dirs']
def scan_plugins(self):
for i in self.plugin_dirs:
self.scan_plugin_dir(i)
def scan_plugin_dir(self, dir):
self.logger.debug('Scanning plugin directory %s', dir)
for root, dirs, files in os.walk(dir):
for name in files:
abspath = os.path.join(root, name)
path = os.path.relpath(abspath, dir)
name, ext = os.path.splitext(path)
if name in self.managed_files.keys():
continue
if ext in TEMPLATE_RENDERERS.keys():
self.managed_files[name] = abspath
self.logger.info('Adding managed file %s [%s]', name, ext)
def generate_file(self, file_path):
if file_path not in self.managed_files.keys():
raise RpcException(errno.ENOENT, 'No such file')
template_path = self.managed_files[file_path]
name, ext = os.path.splitext(template_path)
if ext not in self.renderers.keys():
raise RuntimeError("Can't find renderer for {0}".format(file_path))
renderer = self.renderers[ext]
try:
return renderer.render_template(template_path)
except Exception as e:
self.logger.warn('Cannot generate file {0}: {1}'.format(file_path, str(e)))
#.........这里部分代码省略.........
示例15: Context
# 需要导入模块: from freenas.dispatcher.client import Client [as 别名]
# 或者: from freenas.dispatcher.client.Client import on_error [as 别名]
class Context(object):
def __init__(self):
self.hostname = None
self.connection = Client()
self.ml = None
self.logger = logging.getLogger('cli')
self.plugin_dirs = []
self.task_callbacks = {}
self.plugins = {}
self.variables = VariableStore()
self.root_ns = RootNamespace('')
self.event_masks = ['*']
self.event_divert = False
self.event_queue = six.moves.queue.Queue()
self.keepalive_timer = None
self.argparse_parser = None
config.instance = self
@property
def is_interactive(self):
return os.isatty(sys.stdout.fileno())
def start(self):
self.discover_plugins()
self.connect()
def connect(self):
try:
self.connection.connect(self.hostname)
except socket_error as err:
output_msg(_(
"Could not connect to host: {0} due to error: {1}".format(self.hostname, err)
))
self.argparse_parser.print_help()
sys.exit(1)
def login(self, user, password):
try:
self.connection.login_user(user, password)
self.connection.subscribe_events(*EVENT_MASKS)
self.connection.on_event(self.handle_event)
self.connection.on_error(self.connection_error)
except RpcException as e:
if e.code == errno.EACCES:
self.connection.disconnect()
output_msg(_("Wrong username or password"))
sys.exit(1)
self.login_plugins()
def keepalive(self):
if self.connection.opened:
self.connection.call_sync('management.ping')
def read_middleware_config_file(self, file):
"""
If there is a cli['plugin-dirs'] in middleware.conf use that,
otherwise use the default plugins dir within cli namespace
"""
plug_dirs = None
if file:
with open(file, 'r') as f:
data = json.load(f)
if 'cli' in data and 'plugin-dirs' in data['cli']:
if type(data['cli']['plugin-dirs']) != list:
return
self.plugin_dirs += data['cli']['plugin-dirs']
if plug_dirs is None:
plug_dirs = os.path.dirname(os.path.realpath(__file__))
plug_dirs = os.path.join(plug_dirs, 'plugins')
self.plugin_dirs += [plug_dirs]
def discover_plugins(self):
for dir in self.plugin_dirs:
self.logger.debug(_("Searching for plugins in %s"), dir)
self.__discover_plugin_dir(dir)
def login_plugins(self):
for i in list(self.plugins.values()):
if hasattr(i, '_login'):
i._login(self)
def __discover_plugin_dir(self, dir):
for i in glob.glob1(dir, "*.py"):
self.__try_load_plugin(os.path.join(dir, i))
def __try_load_plugin(self, path):
if path in self.plugins:
return
self.logger.debug(_("Loading plugin from %s"), path)
name, ext = os.path.splitext(os.path.basename(path))
plugin = imp.load_source(name, path)
#.........这里部分代码省略.........