def get_queryset(self, *args, **kwargs): for cbo in ConfigBackup.objects.all(): fp = os.path.join(ConfigBackup.cb_dir(), cbo.filename) if not os.path.isfile(fp): cbo.delete() try: with gzip.open(fp, "rb") as f: f.read() except IOError as e: logger.exception(e) logger.info( "The file {} is not gzipped, so compress it now.".format( cbo.filename ) ) try: o, err, rc = run_command(["/usr/bin/gzip", fp], log=True) except Exception as e: # gzip returns rc == 2 if the destination file already exists # so let's return an explicit error message to the user for this case if e.rc == 2: e_msg = ( "A destination file for the config backup file with the same " "name ({}) already exists. Please remove it and try again.".format( fp ) ) # Delete file from system run_command(["/bin/rm", "-f", fp], log=True) else: e_msg = ( "The backup config file ({}) couldn't be gzipped.\n" "Reload the page to refresh the list of backups".format(fp) ) cbo.delete() handle_exception(Exception(e_msg), self.request) gz_name = "{}.gz".format(cbo.filename) cbo.filename = gz_name fp = os.path.join(ConfigBackup.cb_dir(), cbo.filename) cbo.md5sum = md5sum(fp) cbo.size = os.stat(fp).st_size cbo.save() fp_md5sum = md5sum(fp) if fp_md5sum != cbo.md5sum: logger.error( "md5sum mismatch for {}. cbo: {} file: {}. " "Deleting dbo.".format(cbo.filename, cbo.md5sum, fp_md5sum) ) cbo.delete() return ConfigBackup.objects.filter().order_by("-id")
def enable_rockstor_service(logging): rs_dest = '/etc/systemd/system/rockstor.service' rs_src = '%s/conf/rockstor.service' % BASE_DIR sum1 = md5sum(rs_dest) sum2 = md5sum(rs_src) if (sum1 != sum2): logging.info('updating rockstor systemd service') shutil.copy(rs_src, rs_dest) run_command([SYSCTL, 'enable', 'rockstor']) logging.info('Done.') logging.info('rockstor service looks correct. Not updating.')
def enable_rockstor_service(logging): rs_dest = "/etc/systemd/system/rockstor.service" rs_src = "{}/conf/rockstor.service".format(BASE_DIR) sum1 = md5sum(rs_dest) sum2 = md5sum(rs_src) if sum1 != sum2: logging.info("updating rockstor systemd service") shutil.copy(rs_src, rs_dest) run_command([SYSCTL, "enable", "rockstor"]) logging.info("Done.") logging.info("rockstor service looks correct. Not updating.")
def enable_rockstor_service(logging): rs_dest = "/etc/systemd/system/rockstor.service" rs_src = "%s/conf/rockstor.service" % BASE_DIR sum1 = md5sum(rs_dest) sum2 = md5sum(rs_src) if sum1 != sum2: logging.info("updating rockstor systemd service") shutil.copy(rs_src, rs_dest) run_command([SYSCTL, "enable", "rockstor"]) logging.info("Done.") logging.info("rockstor service looks correct. Not updating.")
def enable_bootstrap_service(logging): name = 'rockstor-bootstrap.service' bs_dest = '/etc/systemd/system/%s' % name bs_src = ('%s/conf/%s' % (BASE_DIR, name)) sum1 = "na" if (os.path.isfile(bs_dest)): sum1 = md5sum(bs_dest) sum2 = md5sum(bs_src) if (sum1 != sum2): logging.info('updating rockstor-bootstrap systemd service') shutil.copy(bs_src, bs_dest) run_command([SYSCTL, 'enable', name]) return logging.info('Done.') return logging.info('%s looks correct. Not updating.' % name)
def update_smb_service(logging): name = 'smb.service' ss_dest = '/etc/systemd/system/%s' % name if (not os.path.isfile(ss_dest)): return logging.info('%s is not enabled. Not updating.') ss_src = '%s/conf/%s' % (BASE_DIR, name) sum1 = md5sum(ss_dest) sum2 = md5sum(ss_src) if (sum1 != sum2): logging.info('Updating %s' % name) shutil.copy(ss_src, ss_dest) run_command([SYSCTL, 'daemon-reload']) return logging.info('Done.') return logging.info('%s looks correct. Not updating.' % name)
def update_smb_service(logging): name = "smb.service" ss_dest = "/etc/systemd/system/{}".format(name) if not os.path.isfile(ss_dest): return logging.info("{} is not enabled. Not updating.".format(name)) ss_src = "{}/conf/{}".format(BASE_DIR, name) sum1 = md5sum(ss_dest) sum2 = md5sum(ss_src) if sum1 != sum2: logging.info("Updating {}".format(name)) shutil.copy(ss_src, ss_dest) run_command([SYSCTL, "daemon-reload"]) return logging.info("Done.") return logging.info("{} looks correct. Not updating.".format(name))
def update_smb_service(logging): name = "smb.service" ss_dest = "/etc/systemd/system/%s" % name if not os.path.isfile(ss_dest): return logging.info("%s is not enabled. Not updating.") ss_src = "%s/conf/%s" % (BASE_DIR, name) sum1 = md5sum(ss_dest) sum2 = md5sum(ss_src) if sum1 != sum2: logging.info("Updating %s" % name) shutil.copy(ss_src, ss_dest) run_command([SYSCTL, "daemon-reload"]) return logging.info("Done.") return logging.info("%s looks correct. Not updating." % name)
def enable_bootstrap_service(logging): name = "rockstor-bootstrap.service" bs_dest = "/etc/systemd/system/%s" % name bs_src = "%s/conf/%s" % (BASE_DIR, name) sum1 = "na" if os.path.isfile(bs_dest): sum1 = md5sum(bs_dest) sum2 = md5sum(bs_src) if sum1 != sum2: logging.info("updating rockstor-bootstrap systemd service") shutil.copy(bs_src, bs_dest) run_command([SYSCTL, "enable", name]) return logging.info("Done.") return logging.info("%s looks correct. Not updating." % name)
def enable_bootstrap_service(logging): name = "rockstor-bootstrap.service" bs_dest = "/etc/systemd/system/{}".format(name) bs_src = "{}/conf/{}".format(BASE_DIR, name) sum1 = "na" if os.path.isfile(bs_dest): sum1 = md5sum(bs_dest) sum2 = md5sum(bs_src) if sum1 != sum2: logging.info("updating rockstor-bootstrap systemd service") shutil.copy(bs_src, bs_dest) run_command([SYSCTL, "enable", name]) run_command([SYSCTL, "daemon-reload"]) return logging.info("Done.") return logging.info("{} looks correct. Not updating.".format(name))
def backup_config(): models = {'storageadmin': ['user', 'group', 'sambashare', 'sambacustomconfig', 'netatalkshare', 'nfsexport', 'nfsexportgroup', 'advancednfsexport', ], 'smart_manager': ['service', ], } model_list = [] for a in models: for m in models[a]: model_list.append('%s.%s' % (a, m)) logger.debug('model list = %s' % model_list) filename = ('backup-%s.json' % datetime.now().strftime('%Y-%m-%d-%H%M%S')) cb_dir = ConfigBackup.cb_dir() if (not os.path.isdir(cb_dir)): os.mkdir(cb_dir) fp = os.path.join(cb_dir, filename) with open(fp, 'w') as dfo: call_command('dumpdata', *model_list, stdout=dfo) dfo.write('\n') call_command('dumpdata', database='smart_manager', *model_list, stdout=dfo) run_command(['/usr/bin/gzip', fp]) gz_name = ('%s.gz' % filename) fp = os.path.join(cb_dir, gz_name) size = os.stat(fp).st_size cbo = ConfigBackup(filename=gz_name, md5sum=md5sum(fp), size=size) cbo.save() return cbo
def cleanup_rclocal(logging): #this could potentially be problematic if users want to have a custom #rc.local file, which is not really needed or recommended due to better #systemd alternative method. #This cleanup method can be safely removed when we know there are no #<3.8-9 versions out there any more. rc_dest = '/etc/rc.d/rc.local' rc_src = '%s/conf/rc.local' % BASE_DIR sum1 = md5sum(rc_dest) sum2 = md5sum(rc_src) if (sum1 != sum2): logging.info('updating %s' % rc_dest) shutil.copy(rc_src, rc_dest) logging.info('Done.') return os.chmod(rc_dest, 0755) logging.info('%s looks correct. Not updating.' % rc_dest)
def get_queryset(self, *args, **kwargs): for cbo in ConfigBackup.objects.all(): fp = os.path.join(ConfigBackup.cb_dir(), cbo.filename) if not os.path.isfile(fp): cbo.delete() fp_md5sum = md5sum(fp) if fp_md5sum != cbo.md5sum: logger.error("md5sum mismatch for {}. cbo: {} file: {}. " "Deleting dbo".format(cbo.filename, cbo.md5sum, fp_md5sum)) cbo.delete() return ConfigBackup.objects.filter().order_by("-id")
def backup_config(): models = { "storageadmin": [ "user", "group", "sambashare", "sambacustomconfig", "netatalkshare", "nfsexport", "nfsexportgroup", "advancednfsexport", 'rockon', 'dcontainer', 'dcustomconfig', 'dimage', 'dcontainerenv', 'dcontainerlabel', 'dvolume', 'dport', 'containeroption', 'dcontainerlink', 'dcontainerargs', 'dcontainerdevice', "share", ], "smart_manager": ["service", "servicestatus", "taskdefinition"], } model_list = [] for a in models: for m in models[a]: model_list.append("{}.{}".format(a, m)) filename = "backup-{}.json".format( datetime.now().strftime("%Y-%m-%d-%H%M%S")) cb_dir = ConfigBackup.cb_dir() if not os.path.isdir(cb_dir): os.mkdir(cb_dir) fp = os.path.join(cb_dir, filename) with open(fp, "w") as dfo: call_command("dumpdata", *model_list, stdout=dfo) dfo.write("\n") call_command("dumpdata", database="smart_manager", *model_list, stdout=dfo) run_command(["/usr/bin/gzip", fp]) gz_name = "{}.gz".format(filename) fp = os.path.join(cb_dir, gz_name) size = os.stat(fp).st_size cbo = ConfigBackup(filename=gz_name, md5sum=md5sum(fp), size=size) cbo.save() return cbo
def get_queryset(self, *args, **kwargs): for cbo in ConfigBackup.objects.all(): fp = os.path.join(ConfigBackup.cb_dir(), cbo.filename) if (not os.path.isfile(fp)): cbo.delete() fp_md5sum = md5sum(fp) if (fp_md5sum != cbo.md5sum): logger.error('md5sum mismatch for {}. cbo: {} file: {}. ' 'Deleting dbo'.format(cbo.filename, cbo.md5sum, fp_md5sum)) cbo.delete() return ConfigBackup.objects.filter().order_by('-id')
def get_queryset(self, *args, **kwargs): for cbo in ConfigBackup.objects.all(): fp = os.path.join(ConfigBackup.cb_dir(), cbo.filename) if (not os.path.isfile(fp)): cbo.delete() fp_md5sum = md5sum(fp) if (fp_md5sum != cbo.md5sum): logger.error('md5sum mismatch for %s. cbo: %s file: %s. ' 'Deleting dbo' % (cbo.filename, cbo.md5sum, fp_md5sum)) cbo.delete() return ConfigBackup.objects.filter().order_by('-id')
def _write_smb_service(self, systemd_name, distro_id): """ Customize smb.service file in a distro-dependent manner. In rockstor-based systems, source from settings.CONFROOT. In opensuse-based systems, source from package default. Check for differences before final copy. :param systemd_name: :param distro_id: :return: """ ss_dest = "/etc/systemd/system/{}".format(systemd_name) # BEGIN CentOS section if distro_id == "rockstor": ss_src = "{}/{}".format(settings.CONFROOT, systemd_name) sum1 = md5sum(ss_dest) sum2 = md5sum(ss_src) if sum1 != sum2: shutil.copy(ss_src, ss_dest) # END CentOS section else: ss_src = "/usr/lib/systemd/system/smb.service" # Customize package's default fo, npath = mkstemp() with open(ss_src) as ino, open(npath, "w") as tfo: for l in ino.readlines(): if re.match("After=", l) is not None: tfo.write("{} {}\n".format( l.strip(), "rockstor-bootstrap.service")) else: tfo.write(l) # Check for diff and then move file from temp to final location sum1 = md5sum(ss_dest) sum2 = md5sum(npath) if sum1 != sum2: shutil.move(npath, ss_dest)
def post(self, request, format=None): with self._handle_exception(request): filename = request.data['file-name'] file_obj = request.data['file'] if (ConfigBackup.objects.filter(filename=filename).exists()): msg = ('Config backup(%s) already exists. Uploading a ' 'duplicate is not allowed.' % filename) handle_exception(Exception(msg), request) cbo = ConfigBackup.objects.create(filename=filename, config_backup=file_obj) cb_dir = ConfigBackup.cb_dir() if not os.path.isdir(cb_dir): os.mkdir(cb_dir) fp = os.path.join(cb_dir, filename) cbo.md5sum = md5sum(fp) cbo.size = os.stat(fp).st_size cbo.save() return Response(ConfigBackupSerializer(cbo).data)
def backup_config(): models = { 'storageadmin': [ 'user', 'group', 'sambashare', 'sambacustomconfig', 'netatalkshare', 'nfsexport', 'nfsexportgroup', 'advancednfsexport', ], 'smart_manager': [ 'service', ], } model_list = [] for a in models: for m in models[a]: model_list.append('%s.%s' % (a, m)) logger.debug('model list = %s' % model_list) filename = ('backup-%s.json' % datetime.now().strftime('%Y-%m-%d-%H%M%S')) cb_dir = ConfigBackup.cb_dir() if (not os.path.isdir(cb_dir)): os.mkdir(cb_dir) fp = os.path.join(cb_dir, filename) with open(fp, 'w') as dfo: call_command('dumpdata', *model_list, stdout=dfo) dfo.write('\n') call_command('dumpdata', database='smart_manager', *model_list, stdout=dfo) run_command(['/usr/bin/gzip', fp]) gz_name = ('%s.gz' % filename) fp = os.path.join(cb_dir, gz_name) size = os.stat(fp).st_size cbo = ConfigBackup(filename=gz_name, md5sum=md5sum(fp), size=size) cbo.save() return cbo
def post(self, request, format=None): with self._handle_exception(request): filename = request.data['file-name'] file_obj = request.data['file'] if (ConfigBackup.objects.filter(filename=filename).exists()): msg = ('Config backup ({}) already exists. Uploading a ' 'duplicate is not allowed.').format(filename) handle_exception(Exception(msg), request) cbo = ConfigBackup.objects.create( filename=filename, config_backup=file_obj ) cb_dir = ConfigBackup.cb_dir() if not os.path.isdir(cb_dir): os.mkdir(cb_dir) fp = os.path.join(cb_dir, filename) cbo.md5sum = md5sum(fp) cbo.size = os.stat(fp).st_size cbo.save() return Response(ConfigBackupSerializer(cbo).data)
config = request.data.get('config', {'workgroup': 'MYGROUP',}) workgroup = config['workgroup'] self._save_config(service, config) update_global_config(workgroup) restart_samba(hard=True) except Exception, e: e_msg = ('Samba could not be configured. Try again. ' 'Exception: %s' % e.__str__()) handle_exception(Exception(e_msg), request) else: try: if (command == 'stop'): systemctl('smb', 'disable') systemctl('nmb', 'disable') else: systemd_name = '%s.service' % service_name ss_dest = ('/etc/systemd/system/%s' % systemd_name) ss_src = ('%s/%s' % (settings.CONFROOT, systemd_name)) sum1 = md5sum(ss_dest) sum2 = md5sum(ss_src) if (sum1 != sum2): shutil.copy(ss_src, ss_dest) systemctl('smb', 'enable') systemctl('nmb', 'enable') systemctl('smb', command) systemctl('nmb', command) except Exception, e: e_msg = ('Failed to %s samba due to a system error: %s' % (command, e.__str__())) handle_exception(Exception(e_msg), request) return Response()
def post(self, request, command): """ execute a command on the service """ service = Service.objects.get(name=self.service_name) if (command == 'config'): try: config = request.data.get('config', {}) global_config = {} if 'global_config' in config: gc_lines = config['global_config'].split('\n') for l in gc_lines: gc_param = l.strip().split(' = ') if (len(gc_param) == 2): if '=' in gc_param[0]: raise Exception( 'Syntax error, one param has wrong ' 'spaces around equal signs, ' 'please check syntax of ' '\'%s\'' % ''.join(gc_param)) global_config[gc_param[0].strip().lower( )] = gc_param[1].strip() # noqa # #E501 Default set current workgroup to one got via samba # config page global_config['workgroup'] = config['workgroup'] else: global_config = config # Check Active Directory config and status if AD configured and # ON set workgroup to AD retrieved workgroup else AD not # running and leave workgroup to one choosen by user adso = Service.objects.get(name='active-directory') adconfig = None adso_status = 1 if (adso.config is not None): adconfig = self._get_config(adso) adso_out, adso_err, adso_status = service_status( 'active-directory', adconfig) if adso_status == 0: global_config['workgroup'] = adconfig['workgroup'] else: adconfig = None self._save_config(service, global_config) update_global_config(global_config, adconfig) restart_samba(hard=True) except Exception as e: e_msg = ('Samba could not be configured. Try again. ' 'Exception: %s' % e.__str__()) handle_exception(Exception(e_msg), request) else: try: if (command == 'stop'): systemctl('smb', 'disable') systemctl('nmb', 'disable') else: systemd_name = '%s.service' % self.service_name ss_dest = ('/etc/systemd/system/%s' % systemd_name) ss_src = ('%s/%s' % (settings.CONFROOT, systemd_name)) sum1 = md5sum(ss_dest) sum2 = md5sum(ss_src) if (sum1 != sum2): shutil.copy(ss_src, ss_dest) systemctl('smb', 'enable') systemctl('nmb', 'enable') systemctl('nmb', command) systemctl('smb', command) except Exception as e: e_msg = ('Failed to %s samba due to a system error: %s' % (command, e.__str__())) handle_exception(Exception(e_msg), request) return Response()
def post(self, request, command): """ execute a command on the service """ service = Service.objects.get(name=self.service_name) if (command == 'config'): try: config = request.data.get('config', {}) global_config = {} if 'global_config' in config: gc_lines = config['global_config'].split('\n') for l in gc_lines: gc_param = l.strip().split(' = ') if (len(gc_param) == 2): if '=' in gc_param[0]: raise Exception( 'Syntax error, one param has wrong ' 'spaces around equal signs, ' 'please check syntax of ' '\'%s\'' % ''.join(gc_param)) global_config[gc_param[0].strip().lower()] = gc_param[1].strip() # noqa # #E501 Default set current workgroup to one got via samba # config page global_config['workgroup'] = config['workgroup'] else: global_config = config # Check Active Directory config and status if AD configured and # ON set workgroup to AD retrieved workgroup else AD not # running and leave workgroup to one choosen by user adso = Service.objects.get(name='active-directory') adconfig = None adso_status = 1 if (adso.config is not None): adconfig = self._get_config(adso) adso_out, adso_err, adso_status = service_status( 'active-directory', adconfig) if adso_status == 0: global_config['workgroup'] = adconfig['workgroup'] else: adconfig = None self._save_config(service, global_config) update_global_config(global_config, adconfig) restart_samba(hard=True) except Exception as e: e_msg = ('Samba could not be configured. Try again. ' 'Exception: %s' % e.__str__()) handle_exception(Exception(e_msg), request) else: try: if (command == 'stop'): systemctl('smb', 'disable') systemctl('nmb', 'disable') else: systemd_name = '%s.service' % self.service_name ss_dest = ('/etc/systemd/system/%s' % systemd_name) ss_src = ('%s/%s' % (settings.CONFROOT, systemd_name)) sum1 = md5sum(ss_dest) sum2 = md5sum(ss_src) if (sum1 != sum2): shutil.copy(ss_src, ss_dest) systemctl('smb', 'enable') systemctl('nmb', 'enable') systemctl('nmb', command) systemctl('smb', command) except Exception as e: e_msg = ('Failed to %s samba due to a system error: %s' % (command, e.__str__())) handle_exception(Exception(e_msg), request) return Response()