def _do_http(opts, profile='default'): ''' Make the http request and return the data ''' ret = {} url = __salt__['config.get']('modjk:{0}:url'.format(profile), '') user = __salt__['config.get']('modjk:{0}:user'.format(profile), '') passwd = __salt__['config.get']('modjk:{0}:pass'.format(profile), '') realm = __salt__['config.get']('modjk:{0}:realm'.format(profile), '') timeout = __salt__['config.get']('modjk:{0}:timeout'.format(profile), '') if not url: raise Exception('missing url in profile {0}'.format(profile)) if user and passwd: auth = _auth(url=url, realm=realm, user=user, passwd=passwd) _install_opener(auth) url += '?{0}'.format(_urlencode(opts)) for line in _urlopen(url, timeout=timeout).read().splitlines(): splt = line.split('=', 1) if splt[0] in ret: ret[splt[0]] += ',{0}'.format(splt[1]) else: ret[splt[0]] = splt[1] return ret
def _do_http(opts, profile="default"): """ Make the http request and return the data """ ret = {} url = __salt__["config.get"]("modjk:{0}:url".format(profile), "") user = __salt__["config.get"]("modjk:{0}:user".format(profile), "") passwd = __salt__["config.get"]("modjk:{0}:pass".format(profile), "") realm = __salt__["config.get"]("modjk:{0}:realm".format(profile), "") timeout = __salt__["config.get"]("modjk:{0}:timeout".format(profile), "") if not url: raise Exception("missing url in profile {0}".format(profile)) if user and passwd: auth = _auth(url=url, realm=realm, user=user, passwd=passwd) _install_opener(auth) url += "?{0}".format(_urlencode(opts)) for line in _urlopen(url, timeout=timeout).read().splitlines(): splt = line.split("=", 1) if splt[0] in ret: ret[splt[0]] += ",{0}".format(splt[1]) else: ret[splt[0]] = splt[1] return ret
def _wget(cmd, opts=None, url='http://localhost:8080/manager', timeout=180): ''' A private function used to issue the command to tomcat via the manager webapp cmd the command to execute url the URL of the server manager webapp example: http://localhost:8080/manager opts a dict of arguments timeout timeout for HTTP request return value is a dict in the from of:: { res: [True|False] msg: list of lines we got back from the manager } ''' ret = {'res': True, 'msg': []} # prepare authentication auth = _auth(url) if auth is False: ret['res'] = False ret['msg'] = 'missing username and password settings (grain/pillar)' return ret # prepare URL if url[-1] != '/': url += '/' url6 = url url += 'text/{0}'.format(cmd) url6 += '{0}'.format(cmd) if opts: url += '?{0}'.format(_urlencode(opts)) url6 += '?{0}'.format(_urlencode(opts)) # Make the HTTP request _install_opener(auth) try: # Trying tomcat >= 7 url ret['msg'] = _urlopen(url, timeout=timeout).read().splitlines() except Exception: try: # Trying tomcat6 url ret['msg'] = _urlopen(url6, timeout=timeout).read().splitlines() except Exception: ret['msg'] = 'Failed to create HTTP request' if not ret['msg'][0].startswith('OK'): ret['res'] = False return ret
def _auth(url): """ Install an auth handler for urllib2 """ user = __salt__["config.get"]("solr.user", False) password = __salt__["config.get"]("solr.passwd", False) realm = __salt__["config.get"]("solr.auth_realm", "Solr") if user and password: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=password) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=password) _install_opener(_build_opener(basic, digest))
def _auth(url): ''' Install an auth handler for urllib2 ''' user = __salt__['config.get']('solr.user', False) password = __salt__['config.get']('solr.passwd', False) realm = __salt__['config.get']('solr.auth_realm', 'Solr') if user and password: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=password) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=password) _install_opener(_build_opener(basic, digest))
def _auth(url): ''' Install an auth handler for urllib2 ''' user = __salt__['config.get']('solr.user', False) password = __salt__['config.get']('solr.passwd', False) realm = __salt__['config.get']('solr.auth_realm', 'Solr') if user and password: basic = _HTTPBasicAuthHandler() basic.add_password( realm=realm, uri=url, user=user, passwd=password ) digest = _HTTPDigestAuthHandler() digest.add_password( realm=realm, uri=url, user=user, passwd=password ) _install_opener( _build_opener(basic, digest) )
def server_status(profile='default'): ''' Get Information from the Apache server-status handler .. note:: The server-status handler is disabled by default. In order for this function to work it needs to be enabled. See http://httpd.apache.org/docs/2.2/mod/mod_status.html The following configuration needs to exists in pillar/grains. Each entry nested in ``apache.server-status`` is a profile of a vhost/server. This would give support for multiple apache servers/vhosts. .. code-block:: yaml apache.server-status: default: url: http://localhost/server-status user: someuser pass: password realm: 'authentication realm for digest passwords' timeout: 5 CLI Examples: .. code-block:: bash salt '*' apache.server_status salt '*' apache.server_status other-profile ''' ret = { 'Scoreboard': { '_': 0, 'S': 0, 'R': 0, 'W': 0, 'K': 0, 'D': 0, 'C': 0, 'L': 0, 'G': 0, 'I': 0, '.': 0, }, } # Get configuration from pillar url = __salt__['config.get']( 'apache.server-status:{0}:url'.format(profile), 'http://localhost/server-status' ) user = __salt__['config.get']( 'apache.server-status:{0}:user'.format(profile), '' ) passwd = __salt__['config.get']( 'apache.server-status:{0}:pass'.format(profile), '' ) realm = __salt__['config.get']( 'apache.server-status:{0}:realm'.format(profile), '' ) timeout = __salt__['config.get']( 'apache.server-status:{0}:timeout'.format(profile), 5 ) # create authentication handler if configuration exists if user and passwd: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=passwd) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=passwd) _install_opener(_build_opener(basic, digest)) # get http data url += '?auto' try: response = _urlopen(url, timeout=timeout).read().splitlines() except URLError: return 'error' # parse the data for line in response: splt = line.split(':', 1) splt[0] = splt[0].strip() splt[1] = splt[1].strip() if splt[0] == 'Scoreboard': for c in splt[1]: ret['Scoreboard'][c] += 1 else: if splt[1].isdigit(): ret[splt[0]] = int(splt[1]) else: ret[splt[0]] = float(splt[1]) # return the good stuff return ret
def query(action=None, command=None, args=None, method="GET", data=None): """ Make a web call to a Parallels provider """ path = config.get_cloud_config_value( "url", get_configured_provider(), __opts__, search_global=False ) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm="Parallels Instance Manager", uri=path, user=config.get_cloud_config_value( "user", get_configured_provider(), __opts__, search_global=False ), passwd=config.get_cloud_config_value( "password", get_configured_provider(), __opts__, search_global=False ), ) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += "/{0}".format(command) if not type(args, dict): args = {} kwargs = {"data": data} if isinstance(data, six.string_types) and "<?xml" in data: kwargs["headers"] = { "Content-type": "application/xml", } if args: params = _urlencode(args) req = _Request(url="{0}?{1}".format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug("%s %s", method, req.get_full_url()) if data: log.debug(data) try: result = _urlopen(req) log.debug("PARALLELS Response Status Code: %s", result.getcode()) if "content-length" in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error("PARALLELS Response Status Code: %s %s", exc.code, exc.msg) root = ET.fromstring(exc.read()) log.error(root) return {"error": root}
def query(action=None, command=None, args=None, method='GET', data=None): ''' Make a web call to a Parallels provider ''' path = config.get_cloud_config_value('url', get_configured_provider(), __opts__, search_global=False) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm='Parallels Instance Manager', uri=path, user=config.get_cloud_config_value('user', get_configured_provider(), __opts__, search_global=False), passwd=config.get_cloud_config_value('password', get_configured_provider(), __opts__, search_global=False)) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += '/{0}'.format(command) if not type(args, dict): args = {} kwargs = {'data': data} if isinstance(data, six.string_types) and '<?xml' in data: kwargs['headers'] = { 'Content-type': 'application/xml', } if args: params = _urlencode(args) req = _Request(url='{0}?{1}'.format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug('{0} {1}'.format(method, req.get_full_url())) if data: log.debug(data) try: result = _urlopen(req) log.debug('PARALLELS Response Status Code: {0}'.format( result.getcode())) if 'content-length' in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error('PARALLELS Response Status Code: {0} {1}'.format( exc.code, exc.msg)) root = ET.fromstring(exc.read()) log.error(root) return {'error': root}
def _wget(cmd, opts=None, url="http://localhost:8080/manager", timeout=180): """ A private function used to issue the command to tomcat via the manager webapp cmd the command to execute url The URL of the server manager webapp (example: http://localhost:8080/manager) opts a dict of arguments timeout timeout for HTTP request Return value is a dict in the from of:: { res: [True|False] msg: list of lines we got back from the manager } """ ret = {"res": True, "msg": []} # prepare authentication auth = _auth(url) if auth is False: ret["res"] = False ret["msg"] = "missing username and password settings (grain/pillar)" return ret # prepare URL if url[-1] != "/": url += "/" url6 = url url += "text/{0}".format(cmd) url6 += "{0}".format(cmd) if opts: url += "?{0}".format(_urlencode(opts)) url6 += "?{0}".format(_urlencode(opts)) # Make the HTTP request _install_opener(auth) try: # Trying tomcat >= 7 url ret["msg"] = _urlopen(url, timeout=timeout).read().splitlines() except Exception: # pylint: disable=broad-except try: # Trying tomcat6 url ret["msg"] = _urlopen(url6, timeout=timeout).read().splitlines() except Exception: # pylint: disable=broad-except ret["msg"] = "Failed to create HTTP request" # Force all byte strings to utf-8 strings, for python >= 3.4 for key, value in enumerate(ret["msg"]): try: ret["msg"][key] = salt.utils.stringutils.to_unicode(value, "utf-8") except (UnicodeDecodeError, AttributeError): pass if not ret["msg"][0].startswith("OK"): ret["res"] = False return ret
def _wget(cmd, opts=None, url='http://localhost:8080/manager', timeout=180): ''' A private function used to issue the command to tomcat via the manager webapp cmd the command to execute url the URL of the server manager webapp example: http://localhost:8080/manager opts a dict of arguments timeout timeout for HTTP request return value is a dict in the from of:: { res: [True|False] msg: list of lines we got back from the manager } ''' ret = { 'res': True, 'msg': [] } # prepare authentication auth = _auth(url) if auth is False: ret['res'] = False ret['msg'] = 'missing username and password settings (grain/pillar)' return ret # prepare URL if url[-1] != '/': url += '/' url6 = url url += 'text/{0}'.format(cmd) url6 += '{0}'.format(cmd) if opts: url += '?{0}'.format(_urlencode(opts)) url6 += '?{0}'.format(_urlencode(opts)) # Make the HTTP request _install_opener(auth) try: # Trying tomcat >= 7 url ret['msg'] = _urlopen(url, timeout=timeout).read().splitlines() except Exception: try: # Trying tomcat6 url ret['msg'] = _urlopen(url6, timeout=timeout).read().splitlines() except Exception: ret['msg'] = 'Failed to create HTTP request' if not ret['msg'][0].startswith('OK'): ret['res'] = False return ret
def query(action=None, command=None, args=None, method='GET', data=None): ''' Make a web call to a Parallels provider ''' path = config.get_cloud_config_value( 'url', get_configured_provider(), __opts__, search_global=False ) auth_handler = _HTTPBasicAuthHandler() auth_handler.add_password( realm='Parallels Instance Manager', uri=path, user=config.get_cloud_config_value( 'user', get_configured_provider(), __opts__, search_global=False ), passwd=config.get_cloud_config_value( 'password', get_configured_provider(), __opts__, search_global=False ) ) opener = _build_opener(auth_handler) _install_opener(opener) if action: path += action if command: path += '/{0}'.format(command) if not type(args, dict): args = {} kwargs = {'data': data} if isinstance(data, str) and '<?xml' in data: kwargs['headers'] = { 'Content-type': 'application/xml', } if args: params = _urlencode(args) req = _Request(url='{0}?{1}'.format(path, params), **kwargs) else: req = _Request(url=path, **kwargs) req.get_method = lambda: method log.debug('{0} {1}'.format(method, req.get_full_url())) if data: log.debug(data) try: result = _urlopen(req) log.debug( 'PARALLELS Response Status Code: {0}'.format( result.getcode() ) ) if 'content-length' in result.headers: content = result.read() result.close() items = ET.fromstring(content) return items return {} except URLError as exc: log.error( 'PARALLELS Response Status Code: {0} {1}'.format( exc.code, exc.msg ) ) root = ET.fromstring(exc.read()) log.error(root) return {'error': root}
def server_status(profile="default"): """ Get Information from the Apache server-status handler .. note:: The server-status handler is disabled by default. In order for this function to work it needs to be enabled. See http://httpd.apache.org/docs/2.2/mod/mod_status.html The following configuration needs to exists in pillar/grains. Each entry nested in ``apache.server-status`` is a profile of a vhost/server. This would give support for multiple apache servers/vhosts. .. code-block:: yaml apache.server-status: default: url: http://localhost/server-status user: someuser pass: password realm: 'authentication realm for digest passwords' timeout: 5 CLI Examples: .. code-block:: bash salt '*' apache.server_status salt '*' apache.server_status other-profile """ ret = { "Scoreboard": { "_": 0, "S": 0, "R": 0, "W": 0, "K": 0, "D": 0, "C": 0, "L": 0, "G": 0, "I": 0, ".": 0, }, } # Get configuration from pillar url = __salt__["config.get"]( "apache.server-status:{0}:url".format(profile), "http://localhost/server-status") user = __salt__["config.get"]( "apache.server-status:{0}:user".format(profile), "") passwd = __salt__["config.get"]( "apache.server-status:{0}:pass".format(profile), "") realm = __salt__["config.get"]( "apache.server-status:{0}:realm".format(profile), "") timeout = __salt__["config.get"]( "apache.server-status:{0}:timeout".format(profile), 5) # create authentication handler if configuration exists if user and passwd: basic = _HTTPBasicAuthHandler() basic.add_password(realm=realm, uri=url, user=user, passwd=passwd) digest = _HTTPDigestAuthHandler() digest.add_password(realm=realm, uri=url, user=user, passwd=passwd) _install_opener(_build_opener(basic, digest)) # get http data url += "?auto" try: response = _urlopen(url, timeout=timeout).read().splitlines() except URLError: return "error" # parse the data for line in response: splt = line.split(":", 1) splt[0] = splt[0].strip() splt[1] = splt[1].strip() if splt[0] == "Scoreboard": for c in splt[1]: ret["Scoreboard"][c] += 1 else: if splt[1].isdigit(): ret[splt[0]] = int(splt[1]) else: ret[splt[0]] = float(splt[1]) # return the good stuff return ret