コード例 #1
0
    def post(self, request, command):
        if (command == 'bootstrap'):
            try:
                for share in Share.objects.all():
                    if (not is_share_mounted(share.name)):
                        mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                        pool_device = Disk.objects.filter(
                            pool=share.pool)[0].name
                        mount_share(share.subvol_name, pool_device, mnt_pt)
            except Exception, e:
                e_msg = ('Unable to mount a share(%s, %s) during bootstrap.' %
                         (pool_device, mnt_pt))
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)

            try:
                mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
                for sftpo in SFTP.objects.all():
                    sftp_mount(sftpo.share, settings.MNT_PT,
                               settings.SFTP_MNT_ROOT, mnt_map, sftpo.editable)
                    sftp_snap_toggle(sftpo.share)
            except Exception, e:
                e_msg = ('Unable to export all sftp shares due to a system'
                         ' error')
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)
コード例 #2
0
ファイル: sftp.py プロジェクト: kamal-gade/rockstor-core
    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)
                mnt_pt = ('%s%s' % (mnt_prefix, sftpo.share.name))
                umount_root(mnt_pt)
                if (os.path.isdir(mnt_pt)):
                    shutil.rmtree(mnt_pt)
            sftpo.delete()
            input_map = {}
            for so in SFTP.objects.all():
                if (so.id != sftpo.id):
                    input_map[so.share.owner] = (
                        '%s%s' % (settings.SFTP_MNT_ROOT, so.share.owner))
            update_sftp_config(input_map)
            return Response()
        except RockStorAPIException:
            raise
        except Exception, e:
            handle_exception(e, request)
コード例 #3
0
ファイル: command.py プロジェクト: kamal-gade/rockstor-core
    def post(self, request, command):
        if (command == 'bootstrap'):
            try:
                for share in Share.objects.all():
                    if (not is_share_mounted(share.name)):
                        mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                        pool_device = Disk.objects.filter(
                            pool=share.pool)[0].name
                        mount_share(share.subvol_name, pool_device, mnt_pt)
            except Exception, e:
                e_msg = ('Unable to mount a share(%s, %s) during bootstrap.' %
                         (pool_device, mnt_pt))
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)

            try:
                mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
                for sftpo in SFTP.objects.all():
                    sftp_mount(sftpo.share, settings.MNT_PT,
                               settings.SFTP_MNT_ROOT, mnt_map, sftpo.editable)
                    sftp_snap_toggle(sftpo.share)
            except Exception, e:
                e_msg = ('Unable to export all sftp shares due to a system'
                         ' error')
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)
コード例 #4
0
ファイル: sftp.py プロジェクト: rcgade/rockstor-core
    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)
コード例 #5
0
ファイル: sftp.py プロジェクト: kamal-gade/rockstor-core
    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)
                mnt_pt = ('%s%s' % (mnt_prefix, sftpo.share.name))
                umount_root(mnt_pt)
                if (os.path.isdir(mnt_pt)):
                    shutil.rmtree(mnt_pt)
            sftpo.delete()
            input_map = {}
            for so in SFTP.objects.all():
                if (so.id != sftpo.id):
                    input_map[so.share.owner] = (
                        '%s%s' % (settings.SFTP_MNT_ROOT, so.share.owner))
            update_sftp_config(input_map)
            return Response()
        except RockStorAPIException:
            raise
        except Exception, e:
            handle_exception(e, request)
コード例 #6
0
ファイル: sftp.py プロジェクト: rcgade/rockstor-core
    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)
コード例 #7
0
    def delete(self, request, id):
        with self._handle_exception(request):
            try:
                sftpo = SFTP.objects.get(id=id)
            except:
                e_msg = (
                    "SFTP config for the id ({}) does not exist.").format(id)
                handle_exception(Exception(e_msg), request)

            mnt_prefix = "{}{}/".format(settings.SFTP_MNT_ROOT,
                                        sftpo.share.owner)

            if is_share_mounted(sftpo.share.name, mnt_prefix):
                sftp_snap_toggle(sftpo.share, mount=False)
                mnt_pt = "{}{}".format(mnt_prefix, sftpo.share.name)
                umount_root(mnt_pt)
                if os.path.isdir(mnt_pt):
                    shutil.rmtree(mnt_pt)
            sftpo.delete()
            input_map = {}
            for so in SFTP.objects.all():
                if so.id != sftpo.id:
                    input_map[so.share.owner] = "{}{}".format(
                        settings.SFTP_MNT_ROOT,
                        so.share.owner,
                    )
            update_sftp_config(input_map)
            return Response()
コード例 #8
0
ファイル: sftp.py プロジェクト: rcgade/rockstor-core
    def post(self, request):
        if ('shares' not in request.DATA):
            e_msg = ('Must provide share names')
            handle_exception(Exception(e_msg), request)
        shares = [validate_share(s, request) for s in request.DATA['shares']]
        editable = 'rw'
        if ('read_only' in request.DATA and request.DATA['read_only'] is True):
            editable = 'ro'
        try:
            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            input_list = []
            for share in shares:
                if (SFTP.objects.filter(share=share).exists()):
                    e_msg = ('Share(%s) is already exported via SFTP' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
                if (share.owner == 'root'):
                    e_msg = ('Share(%s) is owned by root. It cannot be '
                             'exported via SFTP with root ownership' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
            for share in shares:
                sftpo = SFTP(share=share, editable=editable)
                sftpo.save()
                #  mount if not already mounted
                helper_mount_share(share)
                #  bindmount if not already
                sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT,
                           mnt_map, editable)
                sftp_snap_toggle(share)

                chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner))
                rsync_for_sftp(chroot_loc)
                input_list.append({
                    'user': share.owner,
                    'dir': chroot_loc,
                })
            for sftpo in SFTP.objects.all():
                if (sftpo.share not in shares):
                    input_list.append({
                        'user':
                        sftpo.share.owner,
                        'dir':
                        ('%s%s' % (settings.SFTP_MNT_ROOT, sftpo.share.owner)),
                    })
            update_sftp_config(input_list)
            return Response()
        except RockStorAPIException:
            raise
        except Exception, e:
            handle_exception(e, request)
コード例 #9
0
ファイル: sftp.py プロジェクト: rcgade/rockstor-core
    def post(self, request):
        if ('shares' not in request.DATA):
            e_msg = ('Must provide share names')
            handle_exception(Exception(e_msg), request)
        shares = [validate_share(s, request) for s in request.DATA['shares']]
        editable = 'rw'
        if ('read_only' in request.DATA and request.DATA['read_only'] is True):
            editable = 'ro'
        try:
            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            input_list = []
            for share in shares:
                if (SFTP.objects.filter(share=share).exists()):
                    e_msg = ('Share(%s) is already exported via SFTP' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
                if (share.owner == 'root'):
                    e_msg = ('Share(%s) is owned by root. It cannot be '
                             'exported via SFTP with root ownership' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
            for share in shares:
                sftpo = SFTP(share=share, editable=editable)
                sftpo.save()
                #  mount if not already mounted
                helper_mount_share(share)
                #  bindmount if not already
                sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT,
                           mnt_map, editable)
                sftp_snap_toggle(share)

                chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner))
                rsync_for_sftp(chroot_loc)
                input_list.append({'user': share.owner,
                                   'dir': chroot_loc, })
            for sftpo in SFTP.objects.all():
                if (sftpo.share not in shares):
                    input_list.append({'user': sftpo.share.owner,
                                       'dir': ('%s%s' %
                                               (settings.SFTP_MNT_ROOT,
                                                sftpo.share.owner)), })
            update_sftp_config(input_list)
            return Response()
        except RockStorAPIException:
            raise
        except Exception, e:
            handle_exception(e, request)
コード例 #10
0
    def post(self, request):
        with self._handle_exception(request):
            if "shares" not in request.data:
                e_msg = "Must provide share names."
                handle_exception(Exception(e_msg), request)
            shares = [
                validate_share(s, request) for s in request.data["shares"]
            ]
            editable = "rw"
            if "read_only" in request.data and request.data[
                    "read_only"] is True:
                editable = "ro"

            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            input_map = {}
            for share in shares:
                if SFTP.objects.filter(share=share).exists():
                    e_msg = (
                        "Share ({}) is already exported via SFTP.").format(
                            share.name)
                    handle_exception(Exception(e_msg), request)
                if share.owner == "root":
                    e_msg = ("Share ({}) is owned by root. It cannot be "
                             "exported via SFTP with "
                             "root ownership.").format(share.name)
                    handle_exception(Exception(e_msg), request)
            for share in shares:
                sftpo = SFTP(share=share, editable=editable)
                sftpo.save()
                #  mount if not already mounted
                helper_mount_share(share)
                #  bindmount if not already
                sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT,
                           mnt_map, editable)
                sftp_snap_toggle(share)

                chroot_loc = "{}{}".format(settings.SFTP_MNT_ROOT, share.owner)
                rsync_for_sftp(chroot_loc)
                input_map[share.owner] = chroot_loc
            for sftpo in SFTP.objects.all():
                if sftpo.share not in shares:
                    input_map[sftpo.share.owner] = "{}{}".format(
                        settings.SFTP_MNT_ROOT,
                        sftpo.share.owner,
                    )
            update_sftp_config(input_map)
            return Response()
コード例 #11
0
ファイル: sftp.py プロジェクト: sprintman/rockstor-core
    def post(self, request):
        with self._handle_exception(request):
            if ('shares' not in request.data):
                e_msg = ('Must provide share names')
                handle_exception(Exception(e_msg), request)
            shares = [
                validate_share(s, request) for s in request.data['shares']
            ]
            editable = 'rw'
            if ('read_only' in request.data
                    and request.data['read_only'] is True):
                editable = 'ro'

            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            input_map = {}
            for share in shares:
                if (SFTP.objects.filter(share=share).exists()):
                    e_msg = ('Share(%s) is already exported via SFTP' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
                if (share.owner == 'root'):
                    e_msg = ('Share(%s) is owned by root. It cannot be '
                             'exported via SFTP with root ownership' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
            for share in shares:
                sftpo = SFTP(share=share, editable=editable)
                sftpo.save()
                #  mount if not already mounted
                helper_mount_share(share)
                #  bindmount if not already
                sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT,
                           mnt_map, editable)
                sftp_snap_toggle(share)

                chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner))
                rsync_for_sftp(chroot_loc)
                input_map[share.owner] = chroot_loc
            for sftpo in SFTP.objects.all():
                if (sftpo.share not in shares):
                    input_map[sftpo.share.owner] = (
                        '%s%s' % (settings.SFTP_MNT_ROOT, sftpo.share.owner))
            update_sftp_config(input_map)
            return Response()
コード例 #12
0
ファイル: sftp.py プロジェクト: priyaganti/rockstor-core
    def post(self, request):
        with self._handle_exception(request):
            if ('shares' not in request.data):
                e_msg = ('Must provide share names')
                handle_exception(Exception(e_msg), request)
            shares = [validate_share(s, request) for s in
                      request.data['shares']]
            editable = 'rw'
            if ('read_only' in request.data and
                    request.data['read_only'] is True):
                editable = 'ro'

            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            input_map = {}
            for share in shares:
                if (SFTP.objects.filter(share=share).exists()):
                    e_msg = ('Share(%s) is already exported via SFTP' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
                if (share.owner == 'root'):
                    e_msg = ('Share(%s) is owned by root. It cannot be '
                             'exported via SFTP with root ownership' %
                             share.name)
                    handle_exception(Exception(e_msg), request)
            for share in shares:
                sftpo = SFTP(share=share, editable=editable)
                sftpo.save()
                #  mount if not already mounted
                helper_mount_share(share)
                #  bindmount if not already
                sftp_mount(share, settings.MNT_PT, settings.SFTP_MNT_ROOT,
                           mnt_map, editable)
                sftp_snap_toggle(share)

                chroot_loc = ('%s%s' % (settings.SFTP_MNT_ROOT, share.owner))
                rsync_for_sftp(chroot_loc)
                input_map[share.owner] = chroot_loc
            for sftpo in SFTP.objects.all():
                if (sftpo.share not in shares):
                    input_map[sftpo.share.owner] = (
                        '%s%s' % (settings.SFTP_MNT_ROOT, sftpo.share.owner))
            update_sftp_config(input_map)
            return Response()
コード例 #13
0
    def post(self, request, command, rtcepoch=None):
        if command == "bootstrap":
            self._update_disk_state()
            self._refresh_pool_state()
            for p in Pool.objects.all():
                if p.disk_set.attached().count() == 0:
                    continue
                if not p.is_mounted:
                    # Prior _refresh_pool_state() should have ensure a mount.
                    logger.error("Skipping import/update of prior known "
                                 "shares for pool ({}) as it is not mounted. "
                                 "(see previous errors)"
                                 ".".format(p.name))
                    continue
                # Import / update db shares counterpart for managed pool.
                import_shares(p, request)

            for share in Share.objects.all():
                if share.pool.disk_set.attached().count() == 0:
                    continue
                if not share.pool.is_mounted:
                    logger.error("Skipping mount of share ({}) as pool () is "
                                 "not mounted (see previous errors)"
                                 ".".format(share.name, share.pool.name))
                    continue
                try:
                    if not share.is_mounted:
                        # System mounted shares i.e. home will already be mounted.
                        mnt_pt = "{}{}".format(settings.MNT_PT, share.name)
                        mount_share(share, mnt_pt)
                except Exception as e:
                    e_msg = ("Exception while mounting a share ({}) during "
                             "bootstrap: ({}).").format(
                                 share.name, e.__str__())
                    logger.error(e_msg)
                    logger.exception(e)

                try:
                    import_snapshots(share)
                except Exception as e:
                    e_msg = ("Exception while importing snapshots of share "
                             "({}): ({}).").format(share.name, e.__str__())
                    logger.error(e_msg)
                    logger.exception(e)

            for snap in Snapshot.objects.all():
                if snap.uvisible:
                    try:
                        mount_snap(snap.share, snap.real_name, snap.qgroup)
                    except Exception as e:
                        e_msg = ("Failed to make the snapshot ({}) visible. "
                                 "Exception: ({}).").format(
                                     snap.real_name, e.__str__())
                        logger.error(e_msg)

            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            for sftpo in SFTP.objects.all():
                # The following may be buggy when used with system mounted (fstab) /home
                # but we currently don't allow /home to be exported.
                try:
                    sftp_mount(
                        sftpo.share,
                        settings.MNT_PT,
                        settings.SFTP_MNT_ROOT,
                        mnt_map,
                        sftpo.editable,
                    )
                    sftp_snap_toggle(sftpo.share)
                except Exception as e:
                    e_msg = ("Exception while exporting a SFTP share during "
                             "bootstrap: ({}).").format(e.__str__())
                    logger.error(e_msg)

            try:
                adv_entries = [
                    a.export_str for a in AdvancedNFSExport.objects.all()
                ]
                exports_d = self.create_adv_nfs_export_input(
                    adv_entries, request)
                exports = self.create_nfs_export_input(NFSExport.objects.all())
                exports.update(exports_d)
                self.refresh_wrapper(exports, request, logger)
            except Exception as e:
                e_msg = ("Exception while bootstrapping NFS: "
                         "({}).").format(e.__str__())
                logger.error(e_msg)

            #  bootstrap services
            try:
                systemctl("firewalld", "stop")
                systemctl("firewalld", "disable")
                systemctl("nginx", "stop")
                systemctl("nginx", "disable")
                systemctl("atd", "enable")
                systemctl("atd", "start")
            except Exception as e:
                e_msg = ("Exception while setting service statuses during "
                         "bootstrap: ({}).").format(e.__str__())
                logger.error(e_msg)
                handle_exception(Exception(e_msg), request)

            logger.debug("Bootstrap operations completed")
            return Response()

        if command == "utcnow":
            return Response(datetime.utcnow().replace(tzinfo=utc))

        if command == "uptime":
            return Response(uptime())

        if command == "kernel":
            try:
                return Response(kernel_info(settings.SUPPORTED_KERNEL_VERSION))
            except Exception as e:
                handle_exception(e, request)

        if command == "update-check":
            try:
                subo = None
                try:
                    subo = UpdateSubscription.objects.get(name="Stable",
                                                          status="active")
                except UpdateSubscription.DoesNotExist:
                    try:
                        subo = UpdateSubscription.objects.get(name="Testing",
                                                              status="active")
                    except UpdateSubscription.DoesNotExist:
                        pass
                return Response(rockstor_pkg_update_check(subscription=subo))
            except Exception as e:
                e_msg = ("Unable to check update due to a system error: "
                         "({}).").format(e.__str__())
                handle_exception(Exception(e_msg), request)

        if command == "update":
            try:
                # Once again, like on system shutdown/reboot, we filter
                # incoming requests with request.auth: every update from
                # WebUI misses request.auth, while yum update requests from
                # data_collector APIWrapper have it, so we can avoid
                # an additional command for yum updates
                if request.auth is None:
                    update_run()
                else:
                    update_run(update_all_other=True)
                return Response("Done")
            except Exception as e:
                e_msg = ("Update failed due to this exception: "
                         "({}).").format(e.__str__())
                handle_exception(Exception(e_msg), request)

        if command == "current-version":
            try:
                return Response(current_version())
            except Exception as e:
                e_msg = ("Unable to check current version due to this "
                         "exception: ({}).").format(e.__str__())
                handle_exception(Exception(e_msg), request)

        # default has shutdown and reboot with delay set to now
        # having normal sytem power off with now = 1 min
        # reboot and shutdown requests from WebUI don't have request.auth
        # while same requests over rest api (ex. scheduled tasks) have
        # an auth token, so if we detect a token we delay with 3 mins
        # to grant connected WebUI user to close it or cancel shutdown/reboot
        delay = "now"
        if request.auth is not None:
            delay = 3

        if command == "shutdown":
            msg = "The system will now be shutdown."
            try:
                # if shutdown request coming from a scheduled task
                # with rtc wake up time on we set it before
                # system shutdown starting
                if rtcepoch is not None:
                    set_system_rtc_wake(rtcepoch)
                request.session.flush()
                system_shutdown(delay)
            except Exception as e:
                msg = ("Failed to shutdown the system due to a low level "
                       "error: ({}).").format(e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if command == "reboot":
            msg = "The system will now reboot."
            try:
                request.session.flush()
                system_reboot(delay)
            except Exception as e:
                msg = ("Failed to reboot the system due to a low level "
                       "error: ({}).").format(e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if command == "suspend":
            msg = "The system will now be suspended to RAM."
            try:
                request.session.flush()
                set_system_rtc_wake(rtcepoch)
                system_suspend()
            except Exception as e:
                msg = ("Failed to suspend the system due to a low level "
                       "error: ({}).").format(e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if command == "current-user":
            return Response(request.user.username)

        if command == "auto-update-status":
            status = True
            try:
                status = auto_update_status()
            except:
                status = False
            finally:
                return Response({"enabled": status})

        if command == "enable-auto-update":
            try:
                auto_update(enable=True)
                return Response({"enabled": True})
            except Exception as e:
                msg = ("Failed to enable auto update due to this exception: "
                       "({}).").format(e.__str__())
                handle_exception(Exception(msg), request)

        if command == "disable-auto-update":
            try:
                auto_update(enable=False)
                return Response({"enabled": False})
            except Exception as e:
                msg = ("Failed to disable auto update due to this exception:  "
                       "({}).").format(e.__str__())
                handle_exception(Exception(msg), request)

        if command == "refresh-disk-state":
            self._update_disk_state()
            return Response()

        if command == "refresh-pool-state":
            self._refresh_pool_state()
            return Response()

        if command == "refresh-share-state":
            for p in Pool.objects.all():
                import_shares(p, request)
            return Response()

        if command == "refresh-snapshot-state":
            for share in Share.objects.all():
                import_snapshots(share)
            return Response()
コード例 #14
0
ファイル: command.py プロジェクト: drtyhbo/rockstor-core
                        pool_device = Disk.objects.filter(
                            pool=share.pool)[0].name
                        mount_share(share, pool_device, mnt_pt)
            except Exception, e:
                e_msg = ('Unable to mount a share(%s, %s) during bootstrap.' %
                         (pool_device, mnt_pt))
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)

            try:
                mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
                for sftpo in SFTP.objects.all():
                    sftp_mount(sftpo.share, settings.MNT_PT,
                               settings.SFTP_MNT_ROOT, mnt_map, sftpo.editable)
                    sftp_snap_toggle(sftpo.share)
            except Exception, e:
                e_msg = ('Unable to export all sftp shares due to a system'
                         ' error')
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)

            try:
                exports = create_nfs_export_input(NFSExport.objects.all())
                logger.info('export = %s' % exports)
                refresh_nfs_exports(exports)
            except Exception, e:
                e_msg = ('Unable to export all nfs shares due to a system'
                         'error')
                logger.error(e_msg)
コード例 #15
0
ファイル: command.py プロジェクト: MFlyer/rockstor-core
    def post(self, request, command, rtcepoch=None):
        if (command == 'bootstrap'):
            self._update_disk_state()
            self._refresh_pool_state()
            for p in Pool.objects.all():
                if p.disk_set.attached().count() == 0:
                    continue
                if not p.is_mounted:
                    # Prior _refresh_pool_state() should have ensure a mount.
                    logger.error('Skipping import/update of prior known '
                                 'shares for pool ({}) as it is not mounted. '
                                 '(see previous errors)'
                                 '.'.format(p.name))
                    continue
                # Import / update db shares counterpart for managed pool.
                import_shares(p, request)

            for share in Share.objects.all():
                if share.pool.disk_set.attached().count() == 0:
                    continue
                if not share.pool.is_mounted:
                    logger.error('Skipping mount of share ({}) as pool () is '
                                 'not mounted (see previous errors)'
                                 '.'.format(share.name, share.pool.name))
                    continue
                try:
                    if not share.is_mounted:
                        mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                        mount_share(share, mnt_pt)
                except Exception as e:
                    e_msg = ('Exception while mounting a share ({}) during '
                             'bootstrap: ({}).').format(share.name,
                                                        e.__str__())
                    logger.error(e_msg)
                    logger.exception(e)

                try:
                    import_snapshots(share)
                except Exception as e:
                    e_msg = ('Exception while importing snapshots of share '
                             '({}): ({}).').format(share.name, e.__str__())
                    logger.error(e_msg)
                    logger.exception(e)

            for snap in Snapshot.objects.all():
                if (snap.uvisible):
                    try:
                        mount_snap(snap.share, snap.real_name, snap.qgroup)
                    except Exception as e:
                        e_msg = ('Failed to make the snapshot ({}) visible. '
                                 'Exception: ({}).').format(snap.real_name,
                                                            e.__str__())
                        logger.error(e_msg)

            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            for sftpo in SFTP.objects.all():
                try:
                    sftp_mount(sftpo.share, settings.MNT_PT,
                               settings.SFTP_MNT_ROOT, mnt_map, sftpo.editable)
                    sftp_snap_toggle(sftpo.share)
                except Exception as e:
                    e_msg = ('Exception while exporting a SFTP share during '
                             'bootstrap: ({}).').format(e.__str__())
                    logger.error(e_msg)

            try:
                adv_entries = [a.export_str for a in
                               AdvancedNFSExport.objects.all()]
                exports_d = self.create_adv_nfs_export_input(adv_entries,
                                                             request)
                exports = self.create_nfs_export_input(NFSExport.objects.all())
                exports.update(exports_d)
                self.refresh_wrapper(exports, request, logger)
            except Exception as e:
                e_msg = ('Exception while bootstrapping NFS: '
                         '({}).').format(e.__str__())
                logger.error(e_msg)

            #  bootstrap services
            try:
                systemctl('firewalld', 'stop')
                systemctl('firewalld', 'disable')
                systemctl('nginx', 'stop')
                systemctl('nginx', 'disable')
                systemctl('atd', 'enable')
                systemctl('atd', 'start')
            except Exception as e:
                e_msg = ('Exception while setting service statuses during '
                         'bootstrap: ({}).').format(e.__str__())
                logger.error(e_msg)
                handle_exception(Exception(e_msg), request)

            logger.debug('Bootstrap operations completed')
            return Response()

        if (command == 'utcnow'):
            return Response(datetime.utcnow().replace(tzinfo=utc))

        if (command == 'uptime'):
            return Response(uptime())

        if (command == 'kernel'):
            try:
                return Response(kernel_info(settings.SUPPORTED_KERNEL_VERSION))
            except Exception as e:
                handle_exception(e, request)

        if (command == 'update-check'):
            try:
                subo = None
                try:
                    subo = UpdateSubscription.objects.get(name='Stable',
                                                          status='active')
                except UpdateSubscription.DoesNotExist:
                    try:
                        subo = UpdateSubscription.objects.get(name='Testing',
                                                              status='active')
                    except UpdateSubscription.DoesNotExist:
                        pass
                return Response(update_check(subscription=subo))
            except Exception as e:
                e_msg = ('Unable to check update due to a system error: '
                         '({}).').format(e.__str__())
                handle_exception(Exception(e_msg), request)

        if (command == 'update'):
            try:
                # Once again, like on system shutdown/reboot, we filter
                # incoming requests with request.auth: every update from
                # WebUI misses request.auth, while yum update requests from
                # data_collector APIWrapper have it, so we can avoid
                # an additional command for yum updates
                if request.auth is None:
                    update_run()
                else:
                    update_run(yum_update=True)
                return Response('Done')
            except Exception as e:
                e_msg = ('Update failed due to this exception: '
                         '({}).').format(e.__str__())
                handle_exception(Exception(e_msg), request)

        if (command == 'current-version'):
            try:
                return Response(current_version())
            except Exception as e:
                e_msg = ('Unable to check current version due to this '
                         'exception: ({}).').format(e.__str__())
                handle_exception(Exception(e_msg), request)

        # default has shutdown and reboot with delay set to now
        # having normal sytem power off with now = 1 min
        # reboot and shutdown requests from WebUI don't have request.auth
        # while same requests over rest api (ex. scheduled tasks) have
        # an auth token, so if we detect a token we delay with 3 mins
        # to grant connected WebUI user to close it or cancel shutdown/reboot
        delay = 'now'
        if request.auth is not None:
            delay = 3

        if (command == 'shutdown'):
            msg = 'The system will now be shutdown.'
            try:
                # if shutdown request coming from a scheduled task
                # with rtc wake up time on we set it before
                # system shutdown starting
                if rtcepoch is not None:
                    set_system_rtc_wake(rtcepoch)
                request.session.flush()
                system_shutdown(delay)
            except Exception as e:
                msg = ('Failed to shutdown the system due to a low level '
                       'error: ({}).').format(e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'reboot'):
            msg = 'The system will now reboot.'
            try:
                request.session.flush()
                system_reboot(delay)
            except Exception as e:
                msg = ('Failed to reboot the system due to a low level '
                       'error: ({}).').format(e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'suspend'):
            msg = 'The system will now be suspended to RAM.'
            try:
                request.session.flush()
                set_system_rtc_wake(rtcepoch)
                system_suspend()
            except Exception as e:
                msg = ('Failed to suspend the system due to a low level '
                       'error: ({}).').format(e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'current-user'):
            return Response(request.user.username)

        if (command == 'auto-update-status'):
            status = True
            try:
                status = auto_update_status()
            except:
                status = False
            finally:
                return Response({'enabled': status, })

        if (command == 'enable-auto-update'):
            try:
                auto_update(enable=True)
                return Response({'enabled': True, })
            except Exception as e:
                msg = ('Failed to enable auto update due to this exception: '
                       '({}).').format(e.__str__())
                handle_exception(Exception(msg), request)

        if (command == 'disable-auto-update'):
            try:
                auto_update(enable=False)
                return Response({'enabled': False, })
            except Exception as e:
                msg = ('Failed to disable auto update due to this exception:  '
                       '({}).').format(e.__str__())
                handle_exception(Exception(msg), request)

        if (command == 'refresh-disk-state'):
            self._update_disk_state()
            return Response()

        if (command == 'refresh-pool-state'):
            self._refresh_pool_state()
            return Response()

        if (command == 'refresh-share-state'):
            for p in Pool.objects.all():
                import_shares(p, request)
            return Response()

        if (command == 'refresh-snapshot-state'):
            for share in Share.objects.all():
                import_snapshots(share)
            return Response()
コード例 #16
0
    def post(self, request, command, rtcepoch=None):
        if (command == 'bootstrap'):
            self._update_disk_state()
            self._refresh_pool_state()
            for p in Pool.objects.all():
                if p.disk_set.attached().count() == 0:
                    continue
                import_shares(p, request)

            for share in Share.objects.all():
                if share.pool.disk_set.attached().count() == 0:
                    continue
                try:
                    if (share.pqgroup == settings.MODEL_DEFS['pqgroup']):
                        share.pqgroup = qgroup_create(share.pool)
                        share.save()
                    if not share.is_mounted:
                        mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                        mount_share(share, mnt_pt)
                except Exception as e:
                    e_msg = ('Exception while mounting a share(%s) during '
                             'bootstrap: %s' % (share.name, e.__str__()))
                    logger.error(e_msg)
                    logger.exception(e)

                try:
                    import_snapshots(share)
                except Exception as e:
                    e_msg = ('Exception while importing Snapshots of '
                             'Share(%s): %s' % (share.name, e.__str__()))
                    logger.error(e_msg)
                    logger.exception(e)

            for snap in Snapshot.objects.all():
                if (snap.uvisible):
                    try:
                        mount_snap(snap.share, snap.real_name)
                    except Exception as e:
                        e_msg = ('Failed to make the Snapshot(%s) visible. '
                                 'Exception: %s' %
                                 (snap.real_name, e.__str__()))
                        logger.error(e_msg)

            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            for sftpo in SFTP.objects.all():
                try:
                    sftp_mount(sftpo.share, settings.MNT_PT,
                               settings.SFTP_MNT_ROOT, mnt_map, sftpo.editable)
                    sftp_snap_toggle(sftpo.share)
                except Exception as e:
                    e_msg = ('Exception while exportin a sftp share during '
                             'bootstrap: %s' % e.__str__())
                    logger.error(e_msg)

            try:
                adv_entries = [a.export_str for a in
                               AdvancedNFSExport.objects.all()]
                exports_d = self.create_adv_nfs_export_input(adv_entries,
                                                             request)
                exports = self.create_nfs_export_input(NFSExport.objects.all())
                exports.update(exports_d)
                self.refresh_wrapper(exports, request, logger)
            except Exception as e:
                e_msg = ('Exception while bootstrapping NFS: %s' % e.__str__())
                logger.error(e_msg)

            #  bootstrap services
            try:
                systemctl('firewalld', 'stop')
                systemctl('firewalld', 'disable')
                systemctl('nginx', 'stop')
                systemctl('nginx', 'disable')
                systemctl('atd', 'enable')
                systemctl('atd', 'start')
            except Exception as e:
                e_msg = ('Exception while setting service statuses during '
                         'bootstrap: %s' % e.__str__())
                logger.error(e_msg)
                handle_exception(Exception(e_msg), request)

            logger.debug('Bootstrap operations completed')
            return Response()

        if (command == 'utcnow'):
            return Response(datetime.utcnow().replace(tzinfo=utc))

        if (command == 'uptime'):
            return Response(uptime())

        if (command == 'kernel'):
            try:
                return Response(kernel_info(settings.SUPPORTED_KERNEL_VERSION))
            except Exception as e:
                handle_exception(e, request)

        if (command == 'update-check'):
            try:
                subo = None
                try:
                    subo = UpdateSubscription.objects.get(name='Stable',
                                                          status='active')
                except UpdateSubscription.DoesNotExist:
                    try:
                        subo = UpdateSubscription.objects.get(name='Testing',
                                                              status='active')
                    except UpdateSubscription.DoesNotExist:
                        pass
                return Response(update_check(subscription=subo))
            except Exception as e:
                e_msg = ('Unable to check update due to a system error: %s'
                         % e.__str__())
                handle_exception(Exception(e_msg), request)

        if (command == 'update'):
            try:
                # Once again, like on system shutdown/reboot, we filter
                # incoming requests with request.auth: every update from
                # WebUI misses request.auth, while yum update requests from
                # data_collector APIWrapper have it, so we can avoid
                # an additional command for yum updates
                if request.auth is None:
                    update_run()
                else:
                    update_run(yum_update=True)
                return Response('Done')
            except Exception as e:
                e_msg = ('Update failed due to this exception: %s'
                         % e.__str__())
                handle_exception(Exception(e_msg), request)

        if (command == 'current-version'):
            try:
                return Response(current_version())
            except Exception as e:
                e_msg = ('Unable to check current version due to this '
                         'exception: ' % e.__str__())
                handle_exception(Exception(e_msg), request)

        # default has shutdown and reboot with delay set to now
        # having normal sytem power off with now = 1 min
        # reboot and shutdown requests from WebUI don't have request.auth
        # while same requests over rest api (ex. scheduled tasks) have
        # an auth token, so if we detect a token we delay with 3 mins
        # to grant connected WebUI user to close it or cancel shutdown/reboot
        delay = 'now'
        if request.auth is not None:
            delay = 3

        if (command == 'shutdown'):
            msg = ('The system will now be shutdown')
            try:
                # if shutdown request coming from a scheduled task
                # with rtc wake up time on we set it before
                # system shutdown starting
                if rtcepoch is not None:
                    set_system_rtc_wake(rtcepoch)
                request.session.flush()
                system_shutdown(delay)
            except Exception as e:
                msg = ('Failed to shutdown the system due to a low level '
                       'error: %s' % e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'reboot'):
            msg = ('The system will now reboot')
            try:
                request.session.flush()
                system_reboot(delay)
            except Exception as e:
                msg = ('Failed to reboot the system due to a low level error: '
                       '%s' % e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'suspend'):
            msg = ('The system will now be suspended to RAM')
            try:
                request.session.flush()
                set_system_rtc_wake(rtcepoch)
                system_suspend()
            except Exception as e:
                msg = ('Failed to suspend the system due to a low level '
                       'error: %s' % e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'current-user'):
            return Response(request.user.username)

        if (command == 'auto-update-status'):
            status = True
            try:
                status = auto_update_status()
            except:
                status = False
            finally:
                return Response({'enabled': status, })

        if (command == 'enable-auto-update'):
            try:
                auto_update(enable=True)
                return Response({'enabled': True, })
            except Exception as e:
                msg = ('Failed to enable auto update due to this exception: '
                       '%s' % e.__str__())
                handle_exception(Exception(msg), request)

        if (command == 'disable-auto-update'):
            try:
                auto_update(enable=False)
                return Response({'enabled': False, })
            except Exception as e:
                msg = ('Failed to disable auto update due to this exception:  '
                       '%s' % e.__str__())
                handle_exception(Exception(msg), request)

        if (command == 'refresh-disk-state'):
            self._update_disk_state()
            return Response()

        if (command == 'refresh-pool-state'):
            self._refresh_pool_state()
            return Response()

        if (command == 'refresh-share-state'):
            for p in Pool.objects.all():
                import_shares(p, request)
            return Response()

        if (command == 'refresh-snapshot-state'):
            for share in Share.objects.all():
                import_snapshots(share)
            return Response()
コード例 #17
0
ファイル: command.py プロジェクト: samajammin/rockstor-core
                        pool_device = Disk.objects.filter(
                            pool=share.pool)[0].name
                        mount_share(share, pool_device, mnt_pt)
            except Exception, e:
                e_msg = ('Unable to mount a share(%s, %s) during bootstrap.' %
                         (pool_device, mnt_pt))
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)

            try:
                mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
                for sftpo in SFTP.objects.all():
                    sftp_mount(sftpo.share, settings.MNT_PT,
                               settings.SFTP_MNT_ROOT, mnt_map, sftpo.editable)
                    sftp_snap_toggle(sftpo.share)
            except Exception, e:
                e_msg = ('Unable to export all sftp shares due to a system'
                         ' error')
                logger.error(e_msg)
                logger.exception(e)
                handle_exception(Exception(e_msg), request)

            try:
                exports = create_nfs_export_input(NFSExport.objects.all())
                refresh_nfs_exports(exports)
            except Exception, e:
                e_msg = ('Unable to export all nfs shares due to a system'
                         'error')
                logger.error(e_msg)
                logger.exception(e)
コード例 #18
0
    def post(self, request, command):
        if (command == 'bootstrap'):

            self._refresh_pool_state()
            for p in Pool.objects.all():
                import_shares(p, request)

            for share in Share.objects.all():
                try:
                    if (share.pqgroup == settings.MODEL_DEFS['pqgroup']):
                        share.pqgroup = qgroup_create(share.pool)
                        share.save()
                    if (not is_share_mounted(share.name)):
                        mnt_pt = ('%s%s' % (settings.MNT_PT, share.name))
                        mount_share(share, mnt_pt)
                except Exception as e:
                    e_msg = ('Exception while mounting a share(%s) during '
                             'bootstrap: %s' % (share.name, e.__str__()))
                    logger.error(e_msg)
                    logger.exception(e)

                try:
                    import_snapshots(share)
                except Exception as e:
                    e_msg = ('Exception while importing Snapshots of '
                             'Share(%s): %s' % (share.name, e.__str__()))
                    logger.error(e_msg)
                    logger.exception(e)

            for snap in Snapshot.objects.all():
                if (snap.uvisible):
                    try:
                        mount_snap(snap.share, snap.real_name)
                    except Exception as e:
                        e_msg = ('Failed to make the Snapshot(%s) visible. '
                                 'Exception: %s' %
                                 (snap.real_name, e.__str__()))
                        logger.error(e_msg)

            mnt_map = sftp_mount_map(settings.SFTP_MNT_ROOT)
            for sftpo in SFTP.objects.all():
                try:
                    sftp_mount(sftpo.share, settings.MNT_PT,
                               settings.SFTP_MNT_ROOT, mnt_map, sftpo.editable)
                    sftp_snap_toggle(sftpo.share)
                except Exception as e:
                    e_msg = ('Exception while exportin a sftp share during '
                             'bootstrap: %s' % e.__str__())
                    logger.error(e_msg)

            try:
                adv_entries = [
                    a.export_str for a in AdvancedNFSExport.objects.all()
                ]
                exports_d = self.create_adv_nfs_export_input(
                    adv_entries, request)
                exports = self.create_nfs_export_input(NFSExport.objects.all())
                exports.update(exports_d)
                self.refresh_wrapper(exports, request, logger)
            except Exception as e:
                e_msg = ('Exception while bootstrapping NFS: %s' % e.__str__())
                logger.error(e_msg)

            #  bootstrap services
            try:
                systemctl('firewalld', 'stop')
                systemctl('firewalld', 'disable')
                systemctl('nginx', 'stop')
                systemctl('nginx', 'disable')
                systemctl('atd', 'enable')
                systemctl('atd', 'start')
            except Exception as e:
                e_msg = ('Exception while setting service statuses during '
                         'bootstrap: %s' % e.__str__())
                logger.error(e_msg)
                handle_exception(Exception(e_msg), request)

            logger.debug('Bootstrap operations completed')
            return Response()

        if (command == 'utcnow'):
            return Response(datetime.utcnow().replace(tzinfo=utc))

        if (command == 'uptime'):
            return Response(uptime())

        if (command == 'kernel'):
            try:
                return Response(kernel_info(settings.SUPPORTED_KERNEL_VERSION))
            except Exception as e:
                handle_exception(e, request)

        if (command == 'update-check'):
            try:
                subo = None
                try:
                    subo = UpdateSubscription.objects.get(name='Stable',
                                                          status='active')
                except UpdateSubscription.DoesNotExist:
                    try:
                        subo = UpdateSubscription.objects.get(name='Testing',
                                                              status='active')
                    except UpdateSubscription.DoesNotExist:
                        pass
                return Response(update_check(subscription=subo))
            except Exception as e:
                e_msg = ('Unable to check update due to a system error: %s' %
                         e.__str__())
                handle_exception(Exception(e_msg), request)

        if (command == 'update'):
            try:
                update_run()
                return Response('Done')
            except Exception as e:
                e_msg = ('Update failed due to this exception: %s' %
                         e.__str__())
                handle_exception(Exception(e_msg), request)

        if (command == 'current-version'):
            try:
                return Response(current_version())
            except Exception as e:
                e_msg = ('Unable to check current version due to this '
                         'exception: ' % e.__str__())
                handle_exception(Exception(e_msg), request)

        if (command == 'shutdown'):
            msg = ('The system will now be shutdown')
            try:
                request.session.flush()
                system_shutdown()
            except Exception as e:
                msg = ('Failed to shutdown the system due to a low level '
                       'error: %s' % e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'reboot'):
            msg = ('The system will now reboot')
            try:
                request.session.flush()
                system_reboot()
            except Exception as e:
                msg = ('Failed to reboot the system due to a low level error: '
                       '%s' % e.__str__())
                handle_exception(Exception(msg), request)
            finally:
                return Response(msg)

        if (command == 'current-user'):
            return Response(request.user.username)

        if (command == 'auto-update-status'):
            status = True
            try:
                status = auto_update_status()
            except:
                status = False
            finally:
                return Response({
                    'enabled': status,
                })

        if (command == 'enable-auto-update'):
            try:
                auto_update(enable=True)
                return Response({
                    'enabled': True,
                })
            except Exception as e:
                msg = ('Failed to enable auto update due to this exception: '
                       '%s' % e.__str__())
                handle_exception(Exception(msg), request)

        if (command == 'disable-auto-update'):
            try:
                auto_update(enable=False)
                return Response({
                    'enabled': False,
                })
            except Exception as e:
                msg = ('Failed to disable auto update due to this exception:  '
                       '%s' % e.__str__())
                handle_exception(Exception(msg), request)

        if (command == 'refresh-pool-state'):
            self._refresh_pool_state()
            return Response()

        if (command == 'refresh-share-state'):
            for p in Pool.objects.all():
                import_shares(p, request)
            return Response()

        if (command == 'refresh-snapshot-state'):
            for share in Share.objects.all():
                import_snapshots(share)
            return Response()