Esempio n. 1
0
class UriGuestBy1Status(Rest):
    @auth
    def _GET(self, *param, **params):
        """<comment-ja>
        virDomainState
         - VIR_DOMAIN_NOSTATE = 0
         - VIR_DOMAIN_RUNNING = 1
         - VIR_DOMAIN_BLOCKED = 2
         - VIR_DOMAIN_PAUSED = 3
         - VIR_DOMAIN_SHUTDOWN = 4
         - VIR_DOMAIN_SHUTOFF = 5
         - VIR_DOMAIN_CRASHED = 6
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """

        host_id = param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id = param[1]
        if uri_id is None:
            return web.notfound()

        model = findbyhost1(self.orm, host_id)

        if self.is_mode_input() is False:
            if model.attribute == 2:
                info = {}
                segs = uri_split(model.hostname)
                uri = uri_join(segs, without_auth=True)
                creds = ""
                if segs["user"] is not None:
                    creds += segs["user"]
                    if segs["passwd"] is not None:
                        creds += ":" + segs["passwd"]
                self.kvc = KaresansuiVirtConnectionAuth(uri, creds)

                try:
                    host = MergeHost(self.kvc, model)
                    for guest in host.guests:
                        _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                        if 0 < len(_virt):
                            for _v in _virt:
                                info = _v.get_info()
                                if info["uuid"] == uri_id or (uri[0:5] == "test:"):
                                    __guest = MergeGuest(guest.info["model"], _v)
                                    status = _v.status()
                                    break

                    if self.is_json() is True:
                        self.view.status = json_dumps(status)
                    else:
                        self.view.status = status
                finally:
                    self.kvc.close()

        return True

    @auth
    def _PUT(self, *param, **params):
        """<comment-ja>
        ステータス更新
         - param
           - create = 0
           - shutdown = 1
           - destroy = 2
           - suspend = 3
           - resume = 4
           - reboot = 5
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """

        host_id = param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id = param[1]
        if uri_id is None:
            return web.notfound()

        if not validates_uriguest_status(self):
            return web.badrequest(self.view.alert)

        status = int(self.input.status)

        model = findbyhost1(self.orm, host_id)

        if model.attribute == 2:
            info = {}
            segs = uri_split(model.hostname)
            uri = uri_join(segs, without_auth=True)
            creds = ""
            if segs["user"] is not None:
                creds += segs["user"]
                if segs["passwd"] is not None:
                    creds += ":" + segs["passwd"]
            self.kvc = KaresansuiVirtConnectionAuth(uri, creds)

            try:
                host = MergeHost(self.kvc, model)
                for guest in host.guests:
                    _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                    if 0 < len(_virt):
                        for _v in _virt:
                            info = _v.get_info()
                            # uri = _v._conn.getURI()
                            if info["uuid"] == uri_id or (uri[0:5] == "test:"):

                                esc_name = "'%s'" % guest.info["model"].name
                                opts = {"name": esc_name, "connection": uri}

                                if creds != "":
                                    passwd_file = KARESANSUI_TMP_DIR + "/" + segs["host"] + ".auth"
                                    open(passwd_file, "w").write(creds)
                                    os.chmod(passwd_file, 0600)
                                    opts["passwd-file"] = passwd_file

                                if status == GUEST_ACTION_CREATE:
                                    # -- Create
                                    cmdname = ["Start Guest", "start guest"]
                                    if _v.is_creatable() is True:
                                        _cmd = dict2command(
                                            "%s/%s"
                                            % (karesansui.config["application.bin.dir"], VIRT_COMMAND_START_GUEST),
                                            opts,
                                        )

                                        self.view.status = VIRT_COMMAND_START_GUEST
                                    else:
                                        self.logger.error("Create Action:The state can not run. - %d" % _v.status())

                                elif status == GUEST_ACTION_SHUTDOWN:
                                    cmdname = ["Shutdown Guest", "shutdown guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Shutdown
                                        _cmd = dict2command(
                                            "%s/%s"
                                            % (karesansui.config["application.bin.dir"], VIRT_COMMAND_SHUTDOWN_GUEST),
                                            opts,
                                        )

                                        self.view.status = VIRT_COMMAND_SHUTDOWN_GUEST
                                    else:
                                        self.logger.error("Shutdown Action:The state can not run. - %d" % _v.status())

                                elif status == GUEST_ACTION_DESTROY:
                                    cmdname = ["Destroy Guest", "Destroy guest"]
                                    if _v.is_destroyable() is True:
                                        # -- destroy
                                        _cmd = dict2command(
                                            "%s/%s"
                                            % (karesansui.config["application.bin.dir"], VIRT_COMMAND_DESTROY_GUEST),
                                            opts,
                                        )

                                        self.view.status = VIRT_COMMAND_DESTROY_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())

                                elif status == GUEST_ACTION_SUSPEND:
                                    cmdname = ["Suspend Guest", "suspend guest"]
                                    if _v.is_suspendable() is True:
                                        # -- Suspend
                                        _cmd = dict2command(
                                            "%s/%s"
                                            % (karesansui.config["application.bin.dir"], VIRT_COMMAND_SUSPEND_GUEST),
                                            opts,
                                        )

                                        self.view.status = VIRT_COMMAND_SUSPEND_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())

                                elif status == GUEST_ACTION_RESUME:
                                    cmdname = ["Resume Guest", "resume guest"]
                                    if _v.is_resumable() is True:
                                        # -- Resume
                                        _cmd = dict2command(
                                            "%s/%s"
                                            % (karesansui.config["application.bin.dir"], VIRT_COMMAND_RESUME_GUEST),
                                            opts,
                                        )

                                        self.view.status = VIRT_COMMAND_RESUME_GUEST
                                    else:
                                        self.logger.error("Resume Action:The state can not run. - %d" % _v.status())

                                elif status == GUEST_ACTION_REBOOT:
                                    cmdname = ["Reboot Guest", "reboot guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Reboot
                                        _cmd = dict2command(
                                            "%s/%s"
                                            % (karesansui.config["application.bin.dir"], VIRT_COMMAND_REBOOT_GUEST),
                                            opts,
                                        )

                                        self.view.status = VIRT_COMMAND_REBOOT_GUEST
                                    else:
                                        self.logger.error("Reboot Action:The state can not run. - %d" % _v.status())

                                elif status == GUEST_ACTION_ENABLE_AUTOSTART:
                                    opts["enable"] = None
                                    cmdname = ["Enable Autostart Guest", "enable autostart guest"]
                                    # -- Enable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s"
                                        % (karesansui.config["application.bin.dir"], VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts,
                                    )

                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST

                                elif status == GUEST_ACTION_DISABLE_AUTOSTART:
                                    opts["disable"] = None
                                    cmdname = ["Disable Autostart Guest", "disable autostart guest"]
                                    # -- Disable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s"
                                        % (karesansui.config["application.bin.dir"], VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts,
                                    )

                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST

                                else:
                                    self.logger.error("Action:Bad Request. - request status=%d" % status)
                                    return web.badrequest()

                                break

            finally:
                self.kvc.close()

            # Job Register
            _jobgroup = JobGroup(cmdname[0], karesansui.sheconf["env.uniqkey"])
            _jobgroup.jobs.append(Job("%s command" % cmdname[1], 0, _cmd))

            _machine2jobgroup = m2j_new(
                machine=model,
                jobgroup_id=-1,
                uniq_key=karesansui.sheconf["env.uniqkey"],
                created_user=self.me,
                modified_user=self.me,
            )

            # INSERT
            save_job_collaboration(self.orm, self.pysilhouette.orm, _machine2jobgroup, _jobgroup)

            return web.accepted(url="/host/%d/uriguest/%s.part" % (host_id, uri_id))
Esempio n. 2
0
class UriGuestBy1Status(Rest):
    
    @auth
    def _GET(self, *param, **params):
        """<comment-ja>
        virDomainState
         - VIR_DOMAIN_NOSTATE = 0
         - VIR_DOMAIN_RUNNING = 1
         - VIR_DOMAIN_BLOCKED = 2
         - VIR_DOMAIN_PAUSED = 3
         - VIR_DOMAIN_SHUTDOWN = 4
         - VIR_DOMAIN_SHUTOFF = 5
         - VIR_DOMAIN_CRASHED = 6
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """

        host_id =  param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id =  param[1]
        if uri_id is None:
            return web.notfound()

        model = findbyhost1(self.orm, host_id)

        if self.is_mode_input() is False:
            if model.attribute == 2:
                info = {}
                segs = uri_split(model.hostname)
                uri = uri_join(segs, without_auth=True)
                creds = ''
                if segs["user"] is not None:
                    creds += segs["user"]
                    if segs["passwd"] is not None:
                        creds += ':' + segs["passwd"]
                self.kvc = KaresansuiVirtConnectionAuth(uri,creds)

                try:
                    host = MergeHost(self.kvc, model)
                    for guest in host.guests:
                        _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                        if 0 < len(_virt):
                            for _v in _virt:
                                info = _v.get_info()
                                if info["uuid"] == uri_id or (uri[0:5] == "test:"):
                                    __guest = MergeGuest(guest.info["model"],_v)
                                    status          = _v.status()
                                    break

                    if self.is_json() is True:
                        self.view.status = json_dumps(status)
                    else:
                        self.view.status = status
                finally:
                    self.kvc.close()

        return True

    @auth
    def _PUT(self, *param, **params):
        """<comment-ja>
        ステータス更新
         - param
           - create = 0
           - shutdown = 1
           - destroy = 2
           - suspend = 3
           - resume = 4
           - reboot = 5
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """

        host_id =  param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id =  param[1]
        if uri_id is None:
            return web.notfound()

        if not validates_uriguest_status(self):
            return web.badrequest(self.view.alert)

        status = int(self.input.status)

        model = findbyhost1(self.orm, host_id)

        if model.attribute == 2:
            info = {}
            segs = uri_split(model.hostname)
            uri = uri_join(segs, without_auth=True)
            creds = ''
            if segs["user"] is not None:
                creds += segs["user"]
                if segs["passwd"] is not None:
                    creds += ':' + segs["passwd"]
            self.kvc = KaresansuiVirtConnectionAuth(uri,creds)

            try:
                host = MergeHost(self.kvc, model)
                for guest in host.guests:
                    _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                    if 0 < len(_virt):
                        for _v in _virt:
                            info = _v.get_info()
                            #uri = _v._conn.getURI()
                            if info["uuid"] == uri_id or (uri[0:5] == "test:"):

                                esc_name = "'%s'" % guest.info["model"].name
                                opts = {"name":esc_name,"connection":uri}

                                if creds != '':
                                    passwd_file = KARESANSUI_TMP_DIR + "/" + segs['host'] + ".auth"
                                    open(passwd_file, "w").write(creds)
                                    os.chmod(passwd_file, 0600)
                                    opts["passwd-file"] = passwd_file

                                if status == GUEST_ACTION_CREATE:
                                    # -- Create
                                    cmdname = ["Start Guest", "start guest"]
                                    if _v.is_creatable() is True:
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_START_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_START_GUEST
                                    else:
                                        self.logger.error("Create Action:The state can not run. - %d" % _v.status())
                                    
                                elif status == GUEST_ACTION_SHUTDOWN:
                                    cmdname = ["Shutdown Guest", "shutdown guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Shutdown
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_SHUTDOWN_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_SHUTDOWN_GUEST
                                    else:
                                        self.logger.error("Shutdown Action:The state can not run. - %d" % _v.status())
                                
                                elif status == GUEST_ACTION_DESTROY:
                                    cmdname = ["Destroy Guest", "Destroy guest"]
                                    if _v.is_destroyable() is True:
                                        # -- destroy
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_DESTROY_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_DESTROY_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())
                                        
                                elif status == GUEST_ACTION_SUSPEND:
                                    cmdname = ["Suspend Guest", "suspend guest"]
                                    if _v.is_suspendable() is True:
                                        # -- Suspend
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_SUSPEND_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_SUSPEND_GUEST
                                    else:
                                        self.logger.error("Destroy Action:The state can not run. - %d" % _v.status())
                                        
                                elif status == GUEST_ACTION_RESUME:
                                    cmdname = ["Resume Guest", "resume guest"]
                                    if _v.is_resumable() is True:
                                        # -- Resume
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_RESUME_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_RESUME_GUEST
                                    else:
                                        self.logger.error("Resume Action:The state can not run. - %d" % _v.status())
                    
                                elif status == GUEST_ACTION_REBOOT:
                                    cmdname = ["Reboot Guest", "reboot guest"]
                                    if _v.is_shutdownable() is True:
                                        # -- Reboot
                                        _cmd = dict2command(
                                            "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_REBOOT_GUEST),
                                            opts)
                                        
                                        self.view.status = VIRT_COMMAND_REBOOT_GUEST
                                    else:
                                        self.logger.error("Reboot Action:The state can not run. - %d" % _v.status())
                    
                                elif status == GUEST_ACTION_ENABLE_AUTOSTART:
                                    opts["enable"] = None
                                    cmdname = ["Enable Autostart Guest", "enable autostart guest"]
                                    # -- Enable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts)
                                        
                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST
                    
                                elif status == GUEST_ACTION_DISABLE_AUTOSTART:
                                    opts["disable"] = None
                                    cmdname = ["Disable Autostart Guest", "disable autostart guest"]
                                    # -- Disable autostart guest
                                    _cmd = dict2command(
                                        "%s/%s" % (karesansui.config['application.bin.dir'],VIRT_COMMAND_AUTOSTART_GUEST),
                                        opts)
                                        
                                    self.view.status = VIRT_COMMAND_AUTOSTART_GUEST

                                else:
                                    self.logger.error("Action:Bad Request. - request status=%d" % status)
                                    return web.badrequest()

                                break

            finally:
                self.kvc.close()


            # Job Register
            _jobgroup = JobGroup(cmdname[0], karesansui.sheconf['env.uniqkey'])
            _jobgroup.jobs.append(Job('%s command' % cmdname[1], 0, _cmd))
        
            _machine2jobgroup = m2j_new(machine=model,
                                        jobgroup_id=-1,
                                        uniq_key=karesansui.sheconf['env.uniqkey'],
                                        created_user=self.me,
                                        modified_user=self.me,
                                        )

            # INSERT
            save_job_collaboration(self.orm,
                                   self.pysilhouette.orm,
                                   _machine2jobgroup,
                                   _jobgroup,
                                   )

            return web.accepted(url="/host/%d/uriguest/%s.part" % (host_id, uri_id))
Esempio n. 3
0
class UriGuestBy1(Rest):

    def _post(self, f):
        ret = Rest._post(self, f)
        if hasattr(self, "kvc") is True:
            self.kvc.close()
        return ret

    @auth
    def _GET(self, *param, **params):

        if self.input.has_key('job_id') is True:
            self.view.job_id = self.input.job_id
        else:
            self.view.job_id = None

        host_id =  param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id =  param[1]
        if uri_id is None:
            return web.notfound()

        model = findbyhost1(self.orm, host_id)

        if self.is_mode_input() is False:
            if model.attribute == 2:
                info = {}
                segs = uri_split(model.hostname)
                uri = uri_join(segs, without_auth=True)
                creds = ''
                if segs["user"] is not None:
                    creds += segs["user"]
                    if segs["passwd"] is not None:
                        creds += ':' + segs["passwd"]
                self.kvc = KaresansuiVirtConnectionAuth(uri,creds)

                try:
                    host = MergeHost(self.kvc, model)
                    for guest in host.guests:
                        if not '__guest' in locals():
                            _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                            if 0 < len(_virt):
                                for _v in _virt:
                                    info = _v.get_info()
                                    if info["uuid"] == uri_id or (uri[0:5] == "test:"):
                                        __guest = MergeGuest(guest.info["model"],_v)
                                        autostart       = _v.autostart()
                                        status          = _v.status()
                                        is_creatable    = _v.is_creatable()
                                        is_shutdownable = _v.is_shutdownable()
                                        is_suspendable  = _v.is_suspendable()
                                        is_resumable    = _v.is_resumable()
                                        is_destroyable  = _v.is_destroyable()
                                        is_active       = _v.is_active()
                                        break

                    if self.is_json() is True:
                        json_host  = host.get_json(self.me.languages)
                        json_guest = __guest.get_json(self.me.languages)
                        self.view.data = json_dumps(
                            {
                                "parent_model": json_host["model"],
                                "parent_virt": json_host["virt"],
                                "model": json_guest["model"],
                                "virt": json_guest["virt"],
                                "info": info,
                                "autostart": autostart,
                                "status": status,
                                "is_creatable": is_creatable,
                                "is_shutdownable": is_shutdownable,
                                "is_suspendable": is_suspendable,
                                "is_resumable": is_resumable,
                                "is_destroyable": is_destroyable,
                                "is_active": is_active,
                            }
                        )
                    else:
                        self.view.parent_model = host.info["model"]
                        self.view.parent_virt = host.info["virt"]
                        self.view.model = __guest.info["model"]
                        self.view.virt = __guest.info["virt"]
                        self.view.info = info
                        self.view.autostart = autostart
                        self.view.status = status
                        self.view.is_creatable = is_creatable
                        self.view.is_shutdownable = is_shutdownable
                        self.view.is_suspendable = is_suspendable
                        self.view.is_resumable = is_resumable
                        self.view.is_destroyable = is_destroyable
                        self.view.is_active = is_active
                finally:
                    self.kvc.close()

        return True