Esempio n. 1
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:
                        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()
Esempio n. 2
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()
Esempio n. 3
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()