本文整理汇总了Python中storageadmin.util.handle_exception函数的典型用法代码示例。如果您正苦于以下问题:Python handle_exception函数的具体用法?Python handle_exception怎么用?Python handle_exception使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了handle_exception函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _refresh_and_reload
def _refresh_and_reload(request):
try:
refresh_afp_config(list(NetatalkShare.objects.all()))
return systemctl('netatalk', 'reload-or-restart')
except Exception as e:
e_msg = ('Failed to reload Netatalk server. Exception: %s' % e.__str__())
handle_exception(Exception(e_msg), request)
示例2: post
def post(self, request, command):
"""
execute a command on the service
"""
e_msg = ('Failed to %s SNMP service due to system error.' %
command)
with self._handle_exception(request, e_msg):
if (command == 'config'):
service = Service.objects.get(name=self.service_name)
config = request.data.get('config', {})
if (type(config) != dict):
e_msg = ('config dictionary is required input')
handle_exception(Exception(e_msg), request)
for option in ('syslocation', 'syscontact', 'rocommunity',):
if (option not in config):
e_msg = ('%s is missing in config' % option)
handle_exception(Exception(e_msg), request)
if (config[option] is None or config[option] == ''):
e_msg = ('%s cannot be empty' % option)
handle_exception(Exception(e_msg), request)
if ('aux' not in config):
e_msg = ('aux is missing in config: %s' % config)
handle_exception(Exception(e_msg), request)
if (type(config['aux']) != list):
e_msg = ('aux must be a list in config: %s' % config)
handle_exception(Exception(e_msg), request)
configure_snmp(config)
self._save_config(service, config)
else:
self._switch_snmp(command)
return Response()
示例3: _ntp_check
def _ntp_check(self, request):
ntpo = Service.objects.get(name='ntpd')
if (not self._get_status(ntpo)):
e_msg = ('NTP must be configured and running first before Rockstor'
' can join AD. Configure NTP service first '
'and try again.')
handle_exception(Exception(e_msg), request)
示例4: post
def post(self, request, sname, command):
with self._handle_exception(request):
share = self._validate_share(request, sname)
if (command == 'clone'):
new_name = request.data.get('name', '')
return create_clone(share, new_name, request, logger)
if (command == 'rollback'):
snap = self._validate_snapshot(request, share)
if (NFSExport.objects.filter(share=share).exists()):
e_msg = ('Share(%s) cannot be rolled back as it is '
'exported via nfs. Delete nfs exports and '
'try again' % sname)
handle_exception(Exception(e_msg), request)
if (SambaShare.objects.filter(share=share).exists()):
e_msg = ('Share(%s) cannot be rolled back as it is shared'
' via Samba. Unshare and try again' % sname)
handle_exception(Exception(e_msg), request)
rollback_snap(snap.real_name, share.name, share.subvol_name,
share.pool)
update_quota(share.pool, snap.qgroup, share.size * 1024)
share.qgroup = snap.qgroup
share.save()
snap.delete()
return Response()
示例5: _validate_enabled
def _validate_enabled(request):
enabled = request.data.get('enabled', True)
if (type(enabled) != bool):
e_msg = ('enabled flag must be a boolean and not %s' %
type(enabled))
handle_exception(Exception(e_msg), request)
return enabled
示例6: put
def put(self, request, username):
user = self._get_user_object(request, username)
try:
# if password is present in input data, change password
if ('password' in request.DATA):
# change password
password = request.DATA['password']
usermod(username, password)
smbpasswd(username, password)
user.set_password(password)
user.save()
# check if admin attribute has changed
if ('is_active' in request.DATA):
is_active = request.DATA['is_active']
# put is through bacbone model save so is_active comes in
# as a boolean
if is_active != user.is_active:
if request.user.username == username:
e_msg = ('Cannot modify admin attribute of the '
'currently logged in user')
handle_exception(Exception(e_msg), request)
user.is_active = is_active
shell = settings.DEFAULT_SHELL
if (is_active is True):
shell = settings.ADMIN_SHELL
update_shell(username, shell)
user.save()
return Response(UserSerializer(user).data)
except RockStorAPIException:
raise
except Exception, e:
handle_exception(e, request)
示例7: _validate_input
def _validate_input(backup_id, request):
try:
return ConfigBackup.objects.get(id=backup_id)
except ConfigBackup.DoesNotExist:
e_msg = ('Config backup for the id ({}) '
'does not exist.').format(backup_id)
handle_exception(Exception(e_msg), request)
示例8: delete
def delete(self, request, id):
try:
sftpo = SFTP.objects.get(id=id)
except:
e_msg = ('SFTP config for the id(%s) does not exist' % id)
handle_exception(Exception(e_msg), request)
try:
mnt_prefix = ('%s%s/' % (settings.SFTP_MNT_ROOT,
sftpo.share.owner))
if (is_share_mounted(sftpo.share.name, mnt_prefix)):
sftp_snap_toggle(sftpo.share, mount=False)
umount_root(('%s%s' % (mnt_prefix, sftpo.share.name)))
import shutil
shutil.rmtree(mnt_prefix)
sftpo.delete()
input_list = []
for so in SFTP.objects.all():
if (so.id != sftpo.id):
input_list.append({'user': so.share.owner,
'dir': ('%s%s' %
(settings.SFTP_MNT_ROOT,
so.share.name)), })
update_sftp_config(input_list)
return Response()
except RockStorAPIException:
raise
except Exception, e:
handle_exception(e, request)
示例9: post
def post(self, request, command):
"""
execute a command on the service
"""
service = Service.objects.get(name=self.name)
if (command == 'config'):
try:
config = request.data.get('config')
try:
listener_port = int(config['listener_port'])
except ValueError:
raise Exception('Listener Port must be a valid port number between 0-65535')
if (listener_port < 0 or listener_port > 65535):
raise Exception('Invalid listener port(%d)' % listener_port)
ni = config['network_interface']
try:
nco = NetworkConnection.objects.get(name=ni)
except NetworkConnection.DoesNotExist:
raise Exception('Network Connection(%s) does not exist.' % ni)
#@todo: we should make restart transparent to the user.
ztask_helpers.restart_rockstor.async(nco.ipaddr, listener_port)
self._save_config(service, config)
return Response()
except Exception, e:
e_msg = ('Failed to configure Rockstor service. Try again. Exception: %s' % e.__str__())
handle_exception(Exception(e_msg), request)
示例10: _btrfs_disk_import
def _btrfs_disk_import(self, dname, request):
try:
disk = self._validate_disk(dname, request)
p_info = get_pool_info(dname)
# get some options from saved config?
po = Pool(name=p_info['label'], raid="unknown")
# need to save it so disk objects get updated properly in the for
# loop below.
po.save()
for d in p_info['disks']:
do = Disk.objects.get(name=d)
do.pool = po
do.parted = False
do.save()
mount_root(po)
po.raid = pool_raid('%s%s' % (settings.MNT_PT, po.name))['data']
po.size = pool_usage('%s%s' % (settings.MNT_PT, po.name))[0]
po.save()
enable_quota(po)
import_shares(po, request)
for share in Share.objects.filter(pool=po):
import_snapshots(share)
return Response(DiskInfoSerializer(disk).data)
except Exception, e:
e_msg = ('Failed to import any pool on this device(%s). Error: %s'
% (dname, e.__str__()))
handle_exception(Exception(e_msg), request)
示例11: _validate_input
def _validate_input(request):
meta = {}
crontab = request.data.get('crontab')
crontabwindow = request.data.get('crontabwindow')
meta = request.data.get('meta', {})
if (type(meta) != dict):
e_msg = ('meta must be a dictionary, not %s' % type(meta))
handle_exception(Exception(e_msg), request)
if 'pool' in meta:
if not Pool.objects.filter(id=meta['pool']).exists():
raise Exception('Non-existent Pool(%s) in meta. %s' %
(meta['pool'], meta))
# Add pool_name to task meta dictionary
pool = Pool.objects.get(id=meta['pool'])
meta['pool_name'] = pool.name
if 'share' in meta:
if not meta['share'].isdigit():
raise Exception('Non-digit share element ({}) in meta {}'
.format(meta['share'], meta))
if not Share.objects.filter(id=meta['share']).exists():
raise Exception('Non-existent Share id (%s) in meta. %s' %
(meta['pool'], meta))
if 'rtc_hour' in meta:
meta['rtc_hour'] = int(meta['rtc_hour'])
meta['rtc_minute'] = int(meta['rtc_minute'])
return crontab, crontabwindow, meta
示例12: post
def post(self, request, command, dname=None):
with self._handle_exception(request):
if (command == 'scan'):
return self._update_disk_state()
e_msg = ('Unsupported command(%s).' % command)
handle_exception(Exception(e_msg), request)
示例13: post
def post(self, request, pname, command=None):
pool = self._validate_pool(pname, request)
if (command is not None and command != 'status'):
e_msg = ('Unknown scrub command: %s' % command)
handle_exception(Exception(e_msg), request)
with self._handle_exception(request):
ps = self._scrub_status(pool)
if (command == 'status'):
return Response(PoolScrubSerializer(ps).data)
force = request.data.get('force', False)
if ((PoolScrub.objects.filter(pool=pool,
status__regex=r'(started|running)')
.exists())):
if (force):
p = PoolScrub.objects.filter(
pool=pool,
status__regex=r'(started|running)').order_by('-id')[0]
p.status = 'terminated'
p.save()
else:
e_msg = ('A Scrub process is already running for '
'pool(%s). If you really want to kill it '
'and start a new scrub, use force option' % pname)
handle_exception(Exception(e_msg), request)
scrub_pid = scrub_start(pool, force=force)
ps = PoolScrub(pool=pool, pid=scrub_pid)
ps.save()
return Response(PoolScrubSerializer(ps).data)
示例14: _handle_exception
def _handle_exception(request, msg):
try:
yield
except RockStorAPIException:
raise
except Exception, e:
handle_exception(e, request, msg)
示例15: _delete_snapshot
def _delete_snapshot(self, request, sname, id=None, snap_name=None):
share = self._validate_share(sname, request)
try:
snapshot = None
if (id is not None):
snapshot = Snapshot.objects.get(id=id)
elif (snap_name is not None):
snapshot = Snapshot.objects.get(share=share, name=snap_name)
else:
return True
except:
e_msg = ''
if (id is not None):
e_msg = ('Snapshot(%s) does not exist.' % id)
else:
e_msg = ('Snapshot(%s) does not exist.' % snap_name)
handle_exception(Exception(e_msg), request)
if (snapshot.uvisible):
self._toggle_visibility(share, snapshot.real_name, on=False)
toggle_sftp_visibility(share, snapshot.real_name, on=False)
remove_snap(share.pool, sname, snapshot.name)
snapshot.delete()
return Response()