def make_it_so(args): parent_dir = None args.pathName if not os.path.exists(args.pathName): """ This is experimental and doesn't get called yet. Samba bails out early if the path doesn't exist. I can probably modify samba to allow this in this case. """ parent_dir = os.path.dirname(args.pathName) bn = os.path.basename(args.pathName) if not os.path.exists(parent_dir): return 1 pds = Client().call('pool.dataset.query', [('mountpoint', '=', parent_dir)])['id'] Client.call('pool.dataset.create', { 'name': f'{pds}/{bn}', 'share_type': 'SMB', 'atime': 'OFF', }) Client().call('sharing.smb.create', { 'path': args.pathName, 'name': args.shareName, 'comment': args.comment, }) return 0
def run(self): while True: if not self.initialized: try: with Client() as c: self.disks = c.call("disk.disks_for_temperature_monitoring") self.powermode = c.call("smart.config")["powermode"] except Exception as e: print(f"Failed to query disks for temperature monitoring: {e!r}") else: self.initialized = True if not self.initialized: time.sleep(self.interval) continue if not self.disks: return try: with Client() as c: self.temperatures = { disk: temperature for disk, temperature in c.call("disk.temperatures", self.disks, self.powermode).items() if temperature is not None } except Exception as e: print(f"Failed to collect disks temperatures: {e!r}") self.temperatures = {} time.sleep(self.interval)
def _observer(self, message): self.observer_queue.put(message) logger = logging.getLogger("middlewared.plugins.zettarepl") try: if isinstance(message, (PeriodicSnapshotTaskStart, PeriodicSnapshotTaskSuccess, PeriodicSnapshotTaskError)): task_id = int(message.task_id.split("_")[-1]) if isinstance(message, PeriodicSnapshotTaskStart): with Client() as c: context = c.call("vmware.periodic_snapshot_task_begin", task_id, job=True) self.vmware_contexts[task_id] = context if context and context["vmsynced"]: # If there were no failures and we successfully took some VMWare snapshots # set the ZFS property to show the snapshot has consistent VM snapshots # inside it. return message.response(properties={"freenas:vmsynced": "Y"}) if isinstance(message, (PeriodicSnapshotTaskSuccess, PeriodicSnapshotTaskError)): context = self.vmware_contexts.pop(task_id, None) if context: with Client() as c: c.call("vmware.periodic_snapshot_task_end", context, job=True) except ClientException as e: if e.error: logger.error("Unhandled exception in ZettareplProcess._observer: %r", e.error) if e.trace: logger.error("Unhandled exception in ZettareplProcess._observer:\n%s", e.trace["formatted"]) except Exception: logger.error("Unhandled exception in ZettareplProcess._observer", exc_info=True)
def stop(): db = get_db_values() Client().call('datastore.update', 'directoryservice.ActiveDirectory', db['ad']['id'], {'ad_enable': 'False'}) Client().call('datastore.update', 'services.services', db['cifs_srv']['id'], {'srv_enable': 'False'}) service_launcher("samba_server", "stop")
def get_db_values(): conf = {} conf['ad'] = Client().call('datastore.query', 'directoryservice.ActiveDirectory', None, {'get': True}) conf['ldap'] = Client().call('datastore.query', 'directoryservice.LDAP', None, {'get': True}) conf['cifs'] = Client().call('datastore.query', 'services.cifs', None, {'get': True}) conf['cifs_srv'] = Client().call('datastore.query', 'services.services', [['srv_service', '=', 'cifs']], {'get': True}) return conf
def enumerate_host_interfaces(): """Get all addresses of all installed interfaces except loopbacks""" libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True) addr = ctypes.POINTER(if_addrs)() retval = libc.getifaddrs(ctypes.byref(addr)) if retval: raise OSError(ctypes.get_errno()) IFF_LOOPBACK = 0x8 # common value for Linux, Free/OpenBSD addrs = [] bind_ips = Client().call('smb.config')['bindip'] if not bind_ips: bind_ips = [x['address'] for x in Client().call('interface.ip_in_use')] ptr = addr while ptr: deref = ptr[0] family = deref.addr[0].family if deref.addr else None dev_name = deref.name.decode() if deref.flags & IFF_LOOPBACK != 0: logger.debug('ignoring loop-back interface {}'.format(dev_name)) elif family == socket.AF_INET: addrs.append((dev_name, family, socket.inet_ntop(family, bytes(deref.addr[0].data[2:6])))) elif family == socket.AF_INET6: if bytes(deref.addr[0].data[6:8]) == b'\xfe\x80': addrs.append( (dev_name, family, socket.inet_ntop(family, bytes(deref.addr[0].data[6:22])))) ptr = deref.next libc.freeifaddrs(addr) # filter detected addresses by command line arguments, if args.ipv4only: addrs = [x for x in addrs if x[1] == socket.AF_INET] if args.ipv6only: addrs = [x for x in addrs if x[1] == socket.AF_INET6] addrs = [x for x in addrs if x[2] in bind_ips] addrs = [x for x in addrs if not x[0].startswith('lo')] return addrs
def connect_and_wait(self): try: with Client(f'ws://{self.remote_ip}:6000/websocket', reserved_ports=True) as c: self.client = c self.connected.set() # Subscribe to all events on connection with self._subscribe_lock: for name in self._subscriptions: self.client.subscribe( name, partial(self._sub_callback, name)) self._on_connect() c._closed.wait() except OSError as e: if e.errno in ( errno. EPIPE, # Happens when failover is configured on cxl device that has no link errno.ENETDOWN, errno.EHOSTDOWN, errno.ENETUNREACH, errno.EHOSTUNREACH, errno.ECONNREFUSED, ) or isinstance(e, socket.timeout): raise ConnectionRefusedError() raise finally: if self.connected.is_set(): # Only happens if we have successfully connected once self._on_disconnect() self.client = None self.connected.clear()
def create_test_group(): with Client() as c: c.call("group.create", { "name": "test", }) return c.call("group.query", [["group", "=", "test"]], {"get": True})["gid"]
def read(self): if not self.initialized: self.init() if not self.initialized: return if not self.disks: return try: with Client() as c: temperatures = c.call('disk.temperatures', self.disks, self.powermode) for disk, temp in temperatures.items(): if temp is not None: self.dispatch_value(disk, 'temperature', temp, data_type='temperature') except CallTimeout: collectd.error("Timeout collecting disk temperatures") except Exception: collectd.error(traceback.format_exc())
def main(): client = Client() sssd_conf = None if client.call('notifier.common', 'system', 'ldap_enabled') and client.call( 'notifier.common', 'system', 'ldap_anonymous_bind'): sys.exit(1) sssd_setup() if os.path.exists(SSSD_CONFIGFILE): sssd_conf = SSSD_CONFIGFILE cookie = get_directoryservice_cookie(client) if not cookie: sys.exit(1) def nullfunc(): pass sc = SSSDConf(client=client, path=sssd_conf, parse=nullfunc, cookie=cookie) sc.add_sssd_section() sc.add_nss_section() sc.add_pam_section() if client.call('notifier.common', 'system', 'activedirectory_enabled' ) and activedirectory_has_unix_extensions(client): add_activedirectory_section(client, sc) if client.call('notifier.common', 'system', 'ldap_enabled'): add_ldap_section(client, sc) sc.save(SSSD_CONFIGFILE)
def main(): parse_args() permitted_clockskew = datetime.timedelta(minutes=3) ad = Client().call('datastore.query', 'directoryservice.activedirectory', [], { 'get': True, 'prefix': 'ad_' }) output = {} with Pool(processes=8) as pool: if args.online: output['server_data'] = check_servers_exist(ad, pool) if args.dump: output['connectable_servers'] = check_servers_exist(ad, pool, -1) if args.records: output['srv_records'] = get_srv_records(ad, pool) if args.time: output['time'] = check_clockskew(ad) if args.ssl: output['ssl'] = check_supports_ssl(ad) if args.json: print(json.dumps(output, sort_keys=True, indent=2)) else: outputtotext(output)
def main(): client = Client() cifs_config = Struct( client.call('datastore.query', 'services.cifs', None, {'get': True})) krb_config = Struct( client.call('datastore.query', 'services.cifs', None, {'get': True})) if not validate_hosts(cifs_config): print("restarting ix-hostname service") service_launcher("ix-hostname", "quietstart") # validate_klist("krb_config.krb_realm") service_launcher("ix-kerberos", "quietstart") service_launcher("ix-nsswitch", "quietstart") if not service_launcher("ix-kinit", "status"): if not service_launcher("ix-kinit", "quietstart"): print("ix-kinit failed") service_launcher("ix-pre-samba", "quietstart") service_launcher("ix-activedirectory", "quietstart") service_launcher("samba_server", "restart") service_launcher("ix-pam", "quietstart") service_launcher("ix-cache", "quietstart")
def main(): parser = argparse.ArgumentParser() parser.add_argument('job', type=int) args = parser.parse_args() with Client() as c: middleware = FakeMiddleware(c) return middleware._call_job(args.job)
def main(): client = Client() ldap_conf = "/usr/local/etc/openldap/ldap.conf" if client.call('notifier.common', 'system', 'ldap_enabled'): ldap_conf_ldap(client, ldap_conf) elif client.call('notifier.common', 'system', 'activedirectory_enabled'): ldap_conf_activedirectory(client, ldap_conf)
def main(): client = Client() certs = client.call('certificate.query') write_certificates(certs) certs = client.call('certificateauthority.query') write_certificates(certs)
def receive_events(): c = Client('ws+unix:///var/run/middlewared-internal.sock', py_exceptions=True) c.subscribe('core.environ', lambda *args, **kwargs: environ_update(kwargs['fields'])) c.subscribe('core.reconfigure_logging', reconfigure_logging) environ_update(c.call('core.environ'))
def init(self): collectd.info('Initializing "disktemp" plugin') with Client() as c: self.disks = [ disk['devname'] for disk in c.call('disk.query', [['togglesmart', '=', True]]) ] self.powermode = c.call('smart.config')['powermode'].lower()
def get_client(self): try: c = Client(self.websocket) except (FileNotFoundError, ConnectionRefusedError): exit('middlewared is not running.') if self.user and self.password: c.call('auth.login', self.user, self.password) return c
def client(py_exceptions=True): if "NODE_A_IP" in os.environ: password = os.environ["APIPASS"] else: password = os.environ["MIDDLEWARE_TEST_PASSWORD"] with Client(f"ws://{host()}/websocket", py_exceptions=py_exceptions) as c: c.call("auth.login", "root", password) yield c
def receive_environ(): def callback(*args, **kwargs): environ_update(kwargs['fields']) c = Client('ws+unix:///var/run/middlewared-internal.sock', py_exceptions=True) c.subscribe('core.environ', callback) environ_update(c.call('core.environ'))
def read(self): try: with Client() as c: temperatures = c.call('disk.temperatures', self.disks, self.powermode, self.smartctl_args) for disk, temp in temperatures.items(): if temp is not None: self.dispatch_value(disk, 'temperature', temp, data_type='temperature') except Exception: collectd.error(traceback.format_exc())
def init(self): collectd.info('Initializing "disktemp" plugin') try: with Client() as c: self.disks = c.call('disk.disks_for_temperature_monitoring') self.powermode = c.call('smart.config')['powermode'] except Exception: collectd.error(traceback.format_exc()) else: self.initialized = True
def main(): client = Client() smb_conf_path = "/usr/local/etc/smb4.conf" smb4_tdb = [] smb4_conf = [] smb4_shares = [] backup_secrets_database() smb4_setup(client) old_samba4_datasets = get_old_samba4_datasets(client) if migration_available(old_samba4_datasets): do_migration(client, old_samba4_datasets) role = get_server_role(client) generate_smbusers(client) generate_smb4_tdb(client, smb4_tdb) generate_smb4_conf(client, smb4_conf, role) generate_smb4_system_shares(client, smb4_shares) generate_smb4_shares(client, smb4_shares) if role == 'dc' and not client.call('notifier.samba4', 'domain_provisioned'): provision_smb4(client) with open(smb_conf_path, "w") as f: for line in smb4_conf: f.write(line + '\n') for line in smb4_shares: f.write(line + '\n') smb4_set_SID(client) if role == 'member' and smb4_ldap_enabled(client): set_ldap_password(client) backup_secrets_database() if role != 'dc': if not client.call('notifier.samba4', 'users_imported'): smb4_import_users( client, smb_conf_path, smb4_tdb, "/var/db/samba4/private/passdb.tdb" ) smb4_grant_rights() client.call('notifier.samba4', 'user_import_sentinel_file_create') smb4_map_groups(client) if role == 'member' and client.call('notifier.common', 'system', 'activedirectory_enabled') and idmap_backend_rfc2307(client): set_idmap_rfc2307_secret(client) restore_secrets_database()
def main(): device = os.environ.get("SMARTD_DEVICE") if device is None: return message = os.environ.get("SMARTD_MESSAGE") if message is None: return with Client() as c: c.call("alert.oneshot_create", "SMART", {"device": device, "message": message})
def unlock(): with Client() as c: try: c.call('disk.sed_unlock_all') except ClientException as e: if e.errno == errno.EACCES: print('SED disks failed to unlocked') else: raise else: print('All SED disks unlocked')
def init(self): collectd.info('Initializing "disktemp" plugin') with Client() as c: self.disks = [disk['devname'] for disk in c.call('disk.query', [['devname', '!=', None], ['togglesmart', '=', True], # Polling for disk temperature does # not allow them to go to sleep # automatically ['hddstandby', '=', 'ALWAYS ON']])] self.smartctl_args = c.call('disk.smartctl_args_for_devices', self.disks) self.powermode = c.call('smart.config')['powermode']
def _call(self, name, serviceobj, methodobj, params=None, app=None, pipes=None, io_thread=False, job=None): try: with Client('ws+unix:///var/run/middlewared-internal.sock', py_exceptions=True) as c: self.client = c job_options = getattr(methodobj, '_job', None) if job and job_options: params = list(params) if params else [] params.insert(0, FakeJob(job['id'], self.client)) return methodobj(*params) finally: self.client = None
async def _call(self, name, serviceobj, methodobj, params=None, app=None, pipes=None, spawn_thread=True, job=None): with Client() as c: self.client = c job_options = getattr(methodobj, '_job', None) if job and job_options: params = list(params) if params else [] params.insert(0, FakeJob(job['id'], self.client)) if asyncio.iscoroutinefunction(methodobj): return await methodobj(*params) else: return methodobj(*params) self.client = None
def __enter__(self): """ Original intent of that class was to use a single connection to middleware for all django threads, however turned out a bit difficult regarding management of that connection (e.g. reconnect) and due to priorities that feature has been delayed. As a stop-gap solution to keep the same API we are using local thread data to keep track of the client object. """ local = self.locals[threading.get_ident()] = threading.local() local.client = Client() return local.client
def create_test_user(): with Client() as c: g = c.call("group.query", [["group", "=", "users"]], {"get": True}) c.call( "user.create", { "username": "******", "full_name": "test", "group": g["id"], "password_disabled": True, }) return c.call("user.query", [["username", "=", "test"]], {"get": True})["uid"]