Esempio n. 1
0
 def query(self, query):
     inventoryd.logmessage(severity="DEBUG", message="Execute query %s" % query)
     res = self._dbo.query(query)
     if res is None:
         inventoryd.logmessage(severity="warn", message="The query encountered an error.")
     
     return res
Esempio n. 2
0
    def getHTTPfileContents(self, url):
        buffer = BytesIO()
        url = url.encode("utf8")
        con = pycurl.Curl()
        con.setopt(pycurl.URL, url)
        con.setopt(pycurl.WRITEFUNCTION, buffer.write)
        if self.getParameter("insecure") is True:
            con.setopt(pycurl.SSL_VERIFYPEER, 0)
            con.setopt(pycurl.SSL_VERIFYHOST, 0)
        else:
            con.setopt(pycurl.SSL_VERIFYPEER, 1)
            con.setopt(pycurl.SSL_VERIFYHOST, 2)

        con.perform()
        rc = con.getinfo(con.RESPONSE_CODE)
        con.close()
        if rc == 200:
            data = buffer.getvalue()
        else:
            message = "%s connector error: There was an error retrieving data from %s. RC: %s" % (
                self.connector_name, url, rc)
            self.message = message
            self.rc = 1
            inventoryd.logmessage(severity="err", message=message)
            data = None
        return data
Esempio n. 3
0
    def __init__(self, ip = "127.0.0.1", port = -1, certificate_path = None, keyfile_path = None):
        """Define a new REST server for inventoryd

parameters:
ip                ip address to listen on (default: 127.0.0.1)
port              port to listen on. specify -1 to not instantiate a REST server
certificate_path  path to the ssl certificate file  
keyfile_path      path to the ssl keyfile

To generate a new certifcate and keyfile pair, execute the following as root:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt
"""
        self._ip = ip
        self._port = port
        self._certificate_path = certificate_path
        self._keyfile_path = keyfile_path
        
        inventoryd.logmessage(severity="debug", message="Creating REST server.")
        inventoryd.logmessage(severity="debug", message="REST server address: %s " % ip)
        inventoryd.logmessage(severity="debug", message="REST server port: %d " % port)
        if certificate_path is not None and keyfile_path is not None:
            inventoryd.logmessage(severity="debug", message="REST server certificate: %s " % certificate_path)
            inventoryd.logmessage(severity="debug", message="REST server keyfile: %s " % keyfile_path)
        
        if self._port != -1:
            self.server = ThreadedRESTserver((ip,port), RESTRequestHandler)
            if certificate_path is not None and keyfile_path is not None:
                self.server.socket = ssl.wrap_socket(self.server.socket, server_side=True, certfile=certificate_path, keyfile=keyfile_path)
            
        return None
Esempio n. 4
0
    def disableConnector(self, connector_id):
        inventoryd.logmessage(severity="DEBUG", message="Disable connector %d" % connector_id)
        res = self._dbo.disableConnector(connector_id)
        if res is False:
            inventoryd.logmessage(severity="error", message="An error ocurred disabling the connector.")

        return res
Esempio n. 5
0
 def getUserPassword(self, username):
     inventoryd.logmessage(severity="DEBUG", message="Get user passhash for %s" % username)
     res = self._dbo.getUserPassword(username)
     if res is False:
         inventoryd.logmessage(severity="error", message="An error ocurred fetching the passhash.")
         res = [ '','' ]
     return res
Esempio n. 6
0
 def getUserACL(self, username):
     inventoryd.logmessage(severity="DEBUG", message="Get user ACL for %s" % username)
     res = self._dbo.getUserACL(username)
     if res is False:
         inventoryd.logmessage(severity="error", message="An error ocurred fetching the ACL.")
         res = list()
     return res
Esempio n. 7
0
 def getStaticGroupCache(self):
     inventoryd.logmessage(severity="DEBUG", message="Get static groupcache")
     res = self._dbo.getStaticGroupCache()
     if res is None:
         inventoryd.logmessage(severity="error", message="Something went wrong getting the static groupcache.")
         res = { "vars":[], 'membership': [] }
     return res
Esempio n. 8
0
 def getConnectorGroupCache(self, connector_id, timestamp = None):
     inventoryd.logmessage(severity="DEBUG", message="Get groupcache for connector %d" % connector_id)
     res = self._dbo.getConnectorGroupCache(connector_id, timestamp)
     if res is None:
         inventoryd.logmessage(severity="error", message="Something went wrong getting the connector groupcache.")
         res = { "vars":[], 'membership': [] }
     return res
Esempio n. 9
0
 def getStaticHostCache(self):
     inventoryd.logmessage(severity="DEBUG", message="Get static hostcache")
     res = self._dbo.getStaticHostCache()
     if res is False:
         inventoryd.logmessage(severity="error", message="Something went wrong getting the static hostcache.")
         res = { "vars":[] }
     return res
Esempio n. 10
0
 def getHosts(self):
     inventoryd.logmessage(severity="DEBUG", message="Get host info")
     res = self._dbo.readHost(host_id)
     if res is False:
         inventoryd.logmessage(severity="error", message="An error ocurred getting the hostinfo.")
         res = list()
     return res
Esempio n. 11
0
 def deleteStaticHostvar(self, hostname = None, fact = None):
     inventoryd.logmessage(severity="DEBUG", message="Delete static hostvar")
     res = self._dbo.deleteStaticHostvar(hostname, fact)
     if res is False:
         inventoryd.logmessage(severity="error", message="An error ocurred deleting a static hostvar.")
         res = list()
     return res
Esempio n. 12
0
    def _createInventoryCacheFile(self):
        inventoryd.logmessage(severity="info",
                              message="Create Inventory cache file")
        inventoryd.logmessage(severity="info",
                              message="Generating Ansible Inventory")
        db = inventoryd.db(self._cfg["db"])
        res = db.getAnsibleInventory()
        db.disconnect()
        timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
        filename = "%s/%s.json" % (self._cli.cachefilepath, timestamp)
        inventoryd.logmessage(severity="info",
                              message="Creating cache file %s" % filename)
        with open(filename, "w") as f:
            f.write(
                json.dumps(res,
                           sort_keys=True,
                           indent=4,
                           separators=(',', ': ')))

        inventoryd.logmessage(severity="info",
                              message="Creating link to %s" % filename)
        if os.path.isfile("%s/latest.json" % self._cli.cachefilepath) is True:
            os.unlink("%s/latest.json" % self._cli.cachefilepath)
        os.symlink(filename, "%s/latest.json" % self._cli.cachefilepath)

        inventoryd.logmessage(severity="info",
                              message="Done generating Ansible Inventory")
Esempio n. 13
0
 def commit(self, query):
     inventoryd.logmessage(severity="DEBUG", message="Committing query %s" % query)
     res = self._dbo.commit(query)
     
     if res is False:
         inventoryd.logmessage(severity="warn", message="The query encountered an error.")
     return res
Esempio n. 14
0
 def getHTTPfileContents(self, url):
     buffer = BytesIO()
     url = url.encode("utf8")
     con = pycurl.Curl()
     con.setopt(pycurl.URL, url)
     con.setopt(pycurl.WRITEFUNCTION, buffer.write)
     if self.getParameter("insecure") is True:
         con.setopt(pycurl.SSL_VERIFYPEER, 0)
         con.setopt(pycurl.SSL_VERIFYHOST, 0)
     else:
         con.setopt(pycurl.SSL_VERIFYPEER, 1)
         con.setopt(pycurl.SSL_VERIFYHOST, 2)
         
     con.perform()
     rc = con.getinfo(con.RESPONSE_CODE)
     con.close()
     if rc == 200:
         data = buffer.getvalue()
     else:
         message="%s connector error: There was an error retrieving data from %s. RC: %s" % (self.connector_name, url, rc)
         self.message = message
         self.rc = 1
         inventoryd.logmessage(severity="err", message=message)
         data = None
     return data
Esempio n. 15
0
    def _createPID(self):
        pid = str(os.getpid())

        if os.path.isfile(self._cli.pidfilepath) is True:
            inventoryd.logmessage(
                severity="crit",
                message=
                "A pidfile (%s) for inventoryd has been found. Aborting startup."
                % self._cli.pidfilepath)
            sys.exit(1)
        else:
            inventoryd.logmessage(
                severity="debug",
                message="No pidfile found. Starting inventoryd.")
            try:
                file(self._cli.pidfilepath, 'w').write(pid)
            except IOError as e:
                inventoryd.logmessage(severity="crit",
                                      message="Could not create pidfile.")
                inventoryd.logmessage(severity="crit",
                                      message="Error ({0}): {1}".format(
                                          e.errno, e.strerror))
                sys.exit(1)
            except:
                inventoryd.logmessage(severity="crit",
                                      message="Could not create pidfile.")
                sys.exit(1)
Esempio n. 16
0
 def updateArguments(self, arglist = list()):
     newargs = list()
     for arg in (self._args + arglist):
         try:
             arg["name"]
         except:
             inventoryd.logmessage(severity="crit", message="argument name is missing.")
             self.rc = 1
             self.message = "argument name is missing."
             #sys.exit(1)
             return False
         
         try:
             arg["mandatory"]
         except:
             arg["mandatory"] = False
         
         try:
             arg["default"]
         except:
             arg["default"] = None
         
         newargs.append(arg)
         self._args = newargs
     return True
Esempio n. 17
0
 def start(self):
     self._createPID()
     self._startRESTserver()
     if self._cli.run_scheduler is True:
         self._startScheduler()
     else:
         inventoryd.logmessage(severity="info", message="not running scheduler. --no-scheduler specified at startup.")
Esempio n. 18
0
 def getConnectorHostCache(self, connector_id, timestamp = None):
     inventoryd.logmessage(severity="DEBUG", message="Get hostcache for connector %d" % connector_id)
     res = self._dbo.getConnectorHostCache(connector_id, timestamp)
     if res is False:
         inventoryd.logmessage(severity="error", message="Something went wrong getting the connector hostcache.")
         res = { "vars":[] }
     return res
Esempio n. 19
0
 def getConnector(self, connector_id):
     inventoryd.logmessage(severity="DEBUG", message="Get connector with id %d" % connector_id)
     res = self._dbo.getConnector(connector_id)
     if res is not None:
         res["parameters"] = json.loads(res["parameters"])
     
     return res
Esempio n. 20
0
 def disconnect(self):
     inventoryd.logmessage(severity="DEBUG", message="Disconnecting from database.")
     if self._dbo.disconnect() is True:
         return True
     else:
         inventoryd.logmessage(severity="crit", message="Cannot disconnect from database. Aborting.")
         sys.exit(1)
Esempio n. 21
0
 def connect(self):
     inventoryd.logmessage(severity="DEBUG", message="Connecting to database.")
     if self._dbo.connect() is True:
         return True
     else:
         inventoryd.logmessage(severity="crit", message="Cannot connect to database. Aborting.")
         sys.exit(1)
Esempio n. 22
0
 def _stopRESTserver(self):
     inventoryd.logmessage(severity="info",
                           message="Stopping the REST server")
     if self._http_restserver is not None:
         self._http_restserver.stop()
     if self._https_restserver is not None:
         self._https_restserver.stop()
Esempio n. 23
0
 def modifyStaticHostvar(self, hostname = None, fact = None, value = None, priority = -1):
     inventoryd.logmessage(severity="DEBUG", message="Modify static hostvar")
     res = self._dbo.modifyStaticHostvar(hostname, fact, value, priority)
     if res is False:
         inventoryd.logmessage(severity="error", message="An error ocurred modifying a static hostvar.")
         res = list()
     return res
Esempio n. 24
0
 def createStaticHostvar(self, hostname = None, fact = None, value = None, priority = 0):
     inventoryd.logmessage(severity="DEBUG", message="Create static hostvar")
     res = self._dbo.createStaticHostvar(hostname, fact, value, priority)
     if res is False:
         inventoryd.logmessage(severity="error", message="An error ocurred creating a static hostvar.")
         res = list()
     return res
Esempio n. 25
0
    def do_POST(self):
        inventoryd.logmessage(severity="debug", message="POST request (%s)." % self.path)

        urlHandler = { '/inventory': inventoryd.restserver.showInventory,
                       '/admin/connector/list': inventoryd.restserver.listConnectors,
                       '/admin/connector/create': inventoryd.restserver.createConnector,
                       '/admin/connector/[0-9]+/modify': inventoryd.restserver.modifyConnector,
                       '/admin/connector/[0-9]+/delete': inventoryd.restserver.deleteConnector,
                       '/admin/connector/[0-9]+/show': inventoryd.restserver.readConnector,
                       '/admin/connector/[0-9]+/disable': inventoryd.restserver.disableConnector,
                       '/admin/connector/[0-9]+/enable': inventoryd.restserver.enableConnector,
                       '/admin/host/list': inventoryd.restserver.listHosts,
                       '/admin/hostvars/[a-zA-Z0-9\.-_]+/create': inventoryd.restserver.createHostvars,
                       '/admin/hostvars/[a-zA-Z0-9\.-_]+/modify': inventoryd.restserver.modifyHostvars,
                       '/admin/hostvars/[a-zA-Z0-9\.-_]+/delete': inventoryd.restserver.deleteHostvars,
                       '/admin/hostvars/[a-zA-Z0-9\.-_]+/show': inventoryd.restserver.readHostvars
                     }
                        
        content_len = int(self.headers.getheader('content-length', 0))
        post_data = self.rfile.read(content_len)
        try:
            json.loads(post_data)
        except:
            post_data = json.loads("{}")
        else:
            post_data = json.loads(post_data)
        
        try:
            post_data["username"]
        except:
            post_data["username"] = ""
        
        try:
            post_data["passphrase"]
        except:
            post_data["passphrase"] = None
            
        try:
            post_data["token"]
        except:
            post_data["token"] = None

        try:
            post_data["payload"]
        except:
            post_data["payload"] = {}
        
        user = inventoryd.user(post_data["username"])
        if post_data["passphrase"] is not None:
            user.authenticate(passphrase=post_data["passphrase"])
        elif post_data["token"] is not None:
            user.authenticate(token=post_data["token"])
        
        for url in urlHandler:
            if re.match('^%s$' % url, self.path) is not None:
                return urlHandler[url](user=user, payload=post_data["payload"], handler=self)

        self.send_response(404)
        return None
Esempio n. 26
0
 def getUserInfo(self, username = None):
     if username is None:
         inventoryd.logmessage(severity="error", message="Invalid username for user info")
         return None
     else:
         inventoryd.logmessage(severity="DEBUG", message="Get user info for %s" % username)
     res = self._dbo.getUserInfo(username)
     return res
Esempio n. 27
0
 def getRoleInfo(self, role_name = None):
     if role_name is None:
         inventoryd.logmessage(severity="error", message="Invalid role_name for role info")
         return None
     else:
         inventoryd.logmessage(severity="DEBUG", message="Get role info for %s" % role_name)
     res = self.getRoleInfo(role_name)
     return res
Esempio n. 28
0
 def readHost(self, host_id = None):
     inventoryd.logmessage(severity="DEBUG", message="Get host list")
     res = self._dbo.getHosts()
     if res is False:
         inventoryd.logmessage(severity="error", message="An error ocurred getting the hostlist.")
         res = dict()
     return res
     return self._dbo.readHost(host_id)
Esempio n. 29
0
 def stop(self):
     """Stop the REST server"""
     if self._port != -1:
         inventoryd.logmessage(severity="debug", message="Stopping REST server (%s:%d)." % (self._ip, self._port))
         self.server.shutdown()
         self.waitForThread()
         inventoryd.logmessage(severity="debug", message="REST server (%s:%d) Stopped." % (self._ip, self._port))
     return True
Esempio n. 30
0
 def getRoleACL(self, role_name = None):
     if role_name is None:
         inventoryd.logmessage(severity="error", message="Invalid role_name for role ACL")
         return []
     else:
         inventoryd.logmessage(severity="DEBUG", message="Get role ACLs for %s" % role_name)
     res = self._dbo.getRoleACL(role_name)
     return res
Esempio n. 31
0
 def start(self):
     """Start the REST server"""
     if self._port != -1:
         inventoryd.logmessage(severity="debug", message="Starting REST server (%s:%d)." % (self._ip, self._port))
         self.server_thread = threading.Thread(target=self.server.serve_forever)
         self.server_thread.start()
         inventoryd.logmessage(severity="debug", message="REST server (%s:%d) started." % (self._ip, self._port))
     return True
Esempio n. 32
0
 def createConnector(self, name, connector, connector_type, schedule, parameters, priority):
     inventoryd.logmessage(severity="DEBUG", message="Create a new connector")
     res = self._dbo.createConnector(name, connector, connector_type, schedule, parameters, priority)
     if res == -1:
         inventoryd.logmessage(severity="error", message="An error ocurred creating the connector.")
     
     self.modifyConnector(res, name, connector, connector_type, schedule, parameters, priority)
     self.disableConnector(res)
     return True
Esempio n. 33
0
 def connect(self):
     if os.path.isfile(self.db_location) is not True:
         inventoryd.logmessage(severity="crit", message="The database sould not be found")
         return False
     else:
         self.connection = sqlite3.connect(self.db_location)
         self.connection.row_factory = sqlite3.Row
         self.cursor = self.connection.cursor()
         return True
Esempio n. 34
0
 def getConnectors(self, enabled = True):
     if enabled is True:
         inventoryd.logmessage(severity="DEBUG", message="Get all connectors")
     else:
         inventoryd.logmessage(severity="DEBUG", message="Get all enabled connectors")
     res = self._dbo.getConnectors(enabled)
     
     for row in res:
         row["parameters"] = json.loads(row["parameters"])
     return res
Esempio n. 35
0
 def start(self):
     self._createPID()
     self._startRESTserver()
     if self._cli.run_scheduler is True:
         self._startScheduler()
     else:
         inventoryd.logmessage(
             severity="info",
             message=
             "not running scheduler. --no-scheduler specified at startup.")
Esempio n. 36
0
    def do_GET(self):
        inventoryd.logmessage(severity="debug",
                              message="GET request (%s)." % self.path)

        urlHandler = {'/inventory': inventoryd.restserver.showInventory}

        for url in urlHandler:
            if re.match('^%s$' % url, self.path) is not None:
                return urlHandler[url](handler=self)

        self.send_response(404)
        return None
Esempio n. 37
0
 def stop(self):
     """Stop the REST server"""
     if self._port != -1:
         inventoryd.logmessage(severity="debug",
                               message="Stopping REST server (%s:%d)." %
                               (self._ip, self._port))
         self.server.shutdown()
         self.waitForThread()
         inventoryd.logmessage(severity="debug",
                               message="REST server (%s:%d) Stopped." %
                               (self._ip, self._port))
     return True