def register_handlers(self):
     """
     This method will be called by the plugin user when it will be
     necessary to register module for listening to stanza.
     """
     TNArchipelEntity.register_handlers(self)
     self.xmppclient.RegisterHandler('iq', self.process_iq_for_centralagent, ns=ARCHIPEL_NS_CENTRALAGENT)
    def init_vocabulary(self):
        """
        Initialize the base vocabulary.
        """
        TNArchipelEntity.init_vocabulary(self)
        registrar_items = [
            {
                "commands": ["capabilities"],
                "parameters": [],
                "method": self.message_capabilities,
                "permissions": ["capabilities"],
                "description": "Get my libvirt capabilities",
            },
            {
                "commands": ["libvirt uri"],
                "parameters": [],
                "method": self.message_libvirt_uri,
                "permissions": ["uri"],
                "description": "Get my libvirt URI",
            },
            {
                "commands": ["ip"],
                "parameters": [],
                "method": self.message_ip,
                "permissions": ["ip"],
                "description": "Get my IP address",
            },
            {
                "commands": ["roster", "vms", "virtual machines", "domains"],
                "parameters": [],
                "method": self.message_roster,
                "permissions": ["rostervm"],
                "description": "Get the content of my roster",
            },
            {
                "commands": ["alloc"],
                "parameters": [
                    {"name": "name", "description": "The name of the vm. If not given, it will be generated"}
                ],
                "method": self.message_alloc,
                "permissions": ["alloc"],
                "description": "Allocate a new virtual machine",
            },
            {
                "commands": ["free"],
                "parameters": [{"name": "identifier", "description": "The name or the UUID of the vm to free"}],
                "method": self.message_free,
                "permissions": ["free"],
                "description": "Free a virtual machine",
            },
            {
                "commands": ["clone"],
                "parameters": [{"name": "identifier", "description": "The name or the UUID of the vm to clone"}],
                "method": self.message_clone,
                "permissions": ["clone"],
                "description": "Clone a virtual machine",
            },
        ]

        self.add_message_registrar_items(registrar_items)
 def init_permissions(self):
     """
     Initialize the permissions.
     """
     TNArchipelEntity.init_permissions(self)
     self.permission_center.create_permission("alloc", "Authorizes users to allocate new virtual machines", False)
     self.permission_center.create_permission("free", "Authorizes users to free allocated virtual machines", False)
     self.permission_center.create_permission("rostervm", "Authorizes users to access the hypervisor's roster", False)
     self.permission_center.create_permission("clone", "Authorizes users to clone virtual machines", False)
     self.permission_center.create_permission("ip", "Authorizes users to get hypervisor's IP address", False)
     self.permission_center.create_permission("uri", "Authorizes users to get the hypervisor's libvirt URI", False)
     self.permission_center.create_permission("capabilities", "Authorizes users to access the hypervisor capabilities", False)
 def init_permissions(self):
     """
     Initialize the permissions.
     """
     TNArchipelEntity.init_permissions(self)
     self.permission_center.create_permission("alloc", "Authorizes users to allocate new virtual machines", False)
     self.permission_center.create_permission("free", "Authorizes users to free allocated virtual machines", False)
     self.permission_center.create_permission("rostervm", "Authorizes users to access the hypervisor's roster", False)
     self.permission_center.create_permission("clone", "Authorizes users to clone virtual machines", False)
     self.permission_center.create_permission("ip", "Authorizes users to get hypervisor's IP address", False)
     self.permission_center.create_permission("migrationinfo", "Authorizes users to get the migration informations", False)
     self.permission_center.create_permission("capabilities", "Authorizes users to access the hypervisor capabilities", False)
     self.permission_center.create_permission("manage", "Authorizes users make Archipel able to manage external virtual machines", False)
     self.permission_center.create_permission("unmanage", "Authorizes users to make Archipel able to unmanage virtual machines", False)
 def unregister_handlers(self):
     """
     Unregister the handlers.
     """
     TNArchipelEntity.unregister_handlers(self)
     self.xmppclient.UnregisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_HYPERVISOR_CONTROL)
 def register_handlers(self):
     """
     This method overrides the defaut register_handler of the super class.
     """
     TNArchipelEntity.register_handlers(self)
     self.xmppclient.RegisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_HYPERVISOR_CONTROL)
    def __init__(self, jid, password, configuration, name, database_file="./database.sqlite3"):
        """
        This is the constructor of the class.
        @type jid: string
        @param jid: the jid of the hypervisor
        @type password: string
        @param password: the password associated to the JID
        @type name: string
        @param name: the name of the hypervisor
        @type database_file: string
        @param database_file: the sqlite3 file to store existing VM for persistance
        """
        TNArchipelEntity.__init__(self, jid, password, configuration, name)
        self.log.info("starting archipel-agent")
        archipelLibvirtEntity.TNArchipelLibvirtEntity.__init__(self, configuration)

        self.virtualmachines            = {}
        self.database_file              = database_file
        self.xmppserveraddr             = self.jid.getDomain()
        self.entity_type                = "hypervisor"
        self.default_avatar             = self.configuration.get("HYPERVISOR", "hypervisor_default_avatar")
        self.libvirt_event_callback_id  = None
        self.vcard_infos                = {}

        # start the permission center
        self.permission_db_file = self.configuration.get("HYPERVISOR", "hypervisor_permissions_database_path")
        self.permission_center.start(database_file=self.permission_db_file)
        self.init_permissions()

        # libvirt connection
        self.connect_libvirt()

        if (self.configuration.has_section("VCARD")):
            for key in ("orgname", "orgunit", "userid", "locality", "url", "categories"):
                if self.configuration.has_option("VCARD", key):
                    self.vcard_infos[key.upper()] = self.configuration.get("VCARD", key)
        self.vcard_infos["TITLE"] = "Hypervisor (%s)" % self.current_hypervisor()

        names_file = open(self.configuration.get("HYPERVISOR", "name_generation_file"), 'r')
        self.generated_names = names_file.readlines()
        names_file.close()
        self.number_of_names = len(self.generated_names) - 1

        self.log.info("Server address defined as %s" % self.xmppserveraddr)

        # hooks
        self.create_hook("HOOK_HYPERVISOR_ALLOC")
        self.create_hook("HOOK_HYPERVISOR_SOFT_ALLOC")
        self.create_hook("HOOK_HYPERVISOR_FREE")
        self.create_hook("HOOK_HYPERVISOR_MIGRATEDVM_LEAVE")
        self.create_hook("HOOK_HYPERVISOR_MIGRATEDVM_ARRIVE")
        self.create_hook("HOOK_HYPERVISOR_CLONE")
        self.create_hook("HOOK_HYPERVISOR_VM_WOKE_UP")
        self.create_hook("HOOK_HYPERVISOR_WOKE_UP")

        # vocabulary
        self.init_vocabulary()

        # module inits
        self.initialize_modules('archipel.plugin.core')
        self.initialize_modules('archipel.plugin.hypervisor')

        if self.is_hypervisor((archipelLibvirtEntity.ARCHIPEL_HYPERVISOR_TYPE_QEMU, archipelLibvirtEntity.ARCHIPEL_HYPERVISOR_TYPE_XEN)):
            try:
                self.libvirt_event_callback_id = self.libvirt_connection.domainEventRegisterAny(None, libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE, self.hypervisor_on_domain_event, None)
            except libvirt.libvirtError:
                self.log.error("We are sorry. But your hypervisor doesn't support libvirt virConnectDomainEventRegisterAny. And this really bad. I'm sooo sorry.")
        else:
            self.log.warning("Your hypervisor doesn't support libvirt eventing. Using fake event loop.")
        self.capabilities = self.get_capabilities()

        # action on auth
        self.register_hook("HOOK_ARCHIPELENTITY_XMPP_AUTHENTICATED", method=self.manage_vcard_hook)
        self.register_hook("HOOK_ARCHIPELENTITY_XMPP_AUTHENTICATED", method=self.wake_up_virtual_machines_hook, oneshot=True)
        self.register_hook("HOOK_ARCHIPELENTITY_XMPP_AUTHENTICATED", method=self.update_presence)
    def __init__(self, jid, password, configuration):
        """
        This is the constructor of the class.
        @type jid: string
        @param jid: the jid of the hypervisor
        @type password: string
        @param password: the password associated to the JID
        """
        TNArchipelEntity.__init__(self, jid, password, configuration, "central-agent")
        self.log.info("Starting Archipel central agent")

        self.xmppserveraddr            = self.jid.getDomain()
        self.entity_type               = "central-agent"
        self.default_avatar            = self.configuration.get("CENTRALAGENT", "central_agent_default_avatar")
        self.libvirt_event_callback_id = None
        self.vcard_infos               = {}

        self.vcard_infos["TITLE"]      = "Central agent"

        self.log.info("Server address defined as %s" % self.xmppserveraddr)

        # start the permission center
        self.permission_db_file = self.configuration.get("CENTRALAGENT", "centralagent_permissions_database_path")
        self.permission_center.start(database_file=self.permission_db_file)
        self.init_permissions()

        # action on auth
        self.register_hook("HOOK_ARCHIPELENTITY_XMPP_AUTHENTICATED", method=self.hook_xmpp_authenticated)
        self.register_hook("HOOK_ARCHIPELENTITY_XMPP_AUTHENTICATED", method=self.manage_vcard_hook)

        # create hooks
        self.create_hook("HOOK_CENTRALAGENT_VM_REGISTERED")
        self.create_hook("HOOK_CENTRALAGENT_VM_UNREGISTERED")
        self.create_hook("HOOK_CENTRALAGENT_HYP_REGISTERED")
        self.create_hook("HOOK_CENTRALAGENT_HYP_UNREGISTERED")


        self.central_agent_jid_val = None

        self.xmpp_authenticated   = False
        self.is_central_agent     = False
        self.salt                 = random.random()
        self.random_wait          = random.random()
        self.database             = sqlite3.connect(self.configuration.get("CENTRALAGENT", "database"), check_same_thread=False)
        self.database.row_factory = sqlite3.Row

        # defining the structure of the keepalive pubsub event
        self.keepalive_event      = xmpp.Node("event",attrs={"type":"keepalive","jid":self.jid})
        self.last_keepalive_heard = datetime.datetime.now()
        self.last_hyp_check       = datetime.datetime.now()
        self.required_stats_xml   = None

        # module inits
        self.initialize_modules('archipel.plugin.core')
        self.initialize_modules('archipel.plugin.centralagent')

        module_platformrequest    = self.configuration.get("MODULES", "platformrequest")

        if module_platformrequest:

            required_stats = self.get_plugin("platformrequest").computing_unit.required_stats
            self.required_stats_xml = xmpp.Node("required_stats")

            for stat in required_stats:

                self.log.debug("CENTRALAGENT: stat : %s" % stat)
                self.required_stats_xml.addChild("stat", attrs=stat)

            self.keepalive_event.addChild(node = self.required_stats_xml)
 def unregister_handlers(self):
     """
     Unregister the handlers.
     """
     TNArchipelEntity.unregister_handlers(self)
     self.xmppclient.UnregisterHandler('iq', self.process_iq_for_centralagent, ns=ARCHIPEL_NS_CENTRALAGENT)
 def init_permissions(self):
     """
     Initialize the permissions.
     """
     TNArchipelEntity.init_permissions(self)
    def __init__(self, jid, password, configuration, name, database_file="./database.sqlite3"):
        """
        this is the constructor of the class.
        @type jid: string
        @param jid: the jid of the hypervisor
        @type password: string
        @param password: the password associated to the JID
        @type name: string
        @param name: the name of the hypervisor
        @type database_file: string
        @param database_file: the sqlite3 file to store existing VM for persistance
        """
        TNArchipelEntity.__init__(self, jid, password, configuration, name)
        archipelLibvirtEntity.TNArchipelLibvirtEntity.__init__(self, configuration)

        self.virtualmachines = {}
        self.database_file = database_file
        self.xmppserveraddr = self.jid.getDomain()
        self.entity_type = "hypervisor"
        self.default_avatar = self.configuration.get("HYPERVISOR", "hypervisor_default_avatar")
        self.libvirt_event_callback_id = None

        # permissions
        permission_db_file = self.configuration.get("HYPERVISOR", "hypervisor_permissions_database_path")
        permission_admin_name = self.configuration.get("GLOBAL", "archipel_root_admin")
        self.permission_center = TNArchipelPermissionCenter(permission_db_file, permission_admin_name)
        self.init_permissions()

        names_file = open(self.configuration.get("HYPERVISOR", "name_generation_file"), "r")
        self.generated_names = names_file.readlines()
        names_file.close()
        self.number_of_names = len(self.generated_names) - 1

        self.log.info("server address defined as %s" % self.xmppserveraddr)

        # hooks
        self.create_hook("HOOK_HYPERVISOR_ALLOC")
        self.create_hook("HOOK_HYPERVISOR_FREE")
        self.create_hook("HOOK_HYPERVISOR_MIGRATEDVM_LEAVE")
        self.create_hook("HOOK_HYPERVISOR_MIGRATEDVM_ARRIVE")
        self.create_hook("HOOK_HYPERVISOR_CLONE")

        # vocabulary
        self.init_vocabulary()

        # module inits
        self.initialize_modules("archipel.plugin.core")
        self.initialize_modules("archipel.plugin.hypervisor")

        # # libvirt connection
        self.connect_libvirt()

        if self.is_hypervisor(
            (archipelLibvirtEntity.ARCHIPEL_HYPERVISOR_TYPE_QEMU, archipelLibvirtEntity.ARCHIPEL_HYPERVISOR_TYPE_XEN)
        ):
            try:
                self.libvirt_event_callback_id = self.libvirt_connection.domainEventRegisterAny(
                    None, libvirt.VIR_DOMAIN_EVENT_ID_LIFECYCLE, self.hypervisor_on_domain_event, None
                )
            except libvirt.libvirtError:
                self.log.error(
                    "we are sorry. but your hypervisor doesn't support libvirt virConnectDomainEventRegisterAny. And this really bad. I'm sooo sorry"
                )
        else:
            self.log.warning("your hypervisor doesn't support libvirt eventing. using fake event loop.")
        self.capabilities = self.get_capabilities()

        # persistance
        self.manage_persistance()

        # action on auth
        self.register_hook("HOOK_ARCHIPELENTITY_XMPP_AUTHENTICATED", method=self.manage_vcard_hook)
        self.register_hook("HOOK_ARCHIPELENTITY_XMPP_AUTHENTICATED", method=self.update_presence)