def sys_info(query_type=None): """ Provide system information based on the query_type Valid query types are: ipv4_addresses, checkconf and checkversions **RESTRICTED** """ if query_type == 'ipv4_addresses': return jsonify(data=ipv4_addresses()), 200 elif query_type == 'checkconf': local_hash = gen_file_hash('/etc/ceph/iscsi-gateway.cfg') return jsonify(data=local_hash), 200 elif query_type == 'checkversions': config_errors = pre_reqs_errors() if config_errors: return jsonify(data=config_errors), 500 else: return jsonify(data='checks passed'), 200 else: # Request Unknown return jsonify(message="Unknown /sysinfo query"), 404
def valid_gateway(gw_name, gw_ip, config): """ validate the request for a new gateway :param gw_name: (str) host (shortname) of the gateway :param gw_ip: (str) ip address on the gw that will be used for iSCSI :param config: (dict) current config :return: (str) "ok" or error description """ http_mode = 'https' if settings.config.api_secure else "http" # if the gateway request already exists in the config, computer says "no" if gw_name in config['gateways']: return "Gateway name {} already defined".format(gw_name) if gw_ip in config['gateways'].get('ip_list', []): return "IP address already defined to the configuration" # validate the gateway name is resolvable if not resolve_ip_addresses(gw_name): return ("Gateway '{}' is not resolvable to an IP address".format(gw_name)) # validate the ip_address is valid ip if not resolve_ip_addresses(gw_ip): return ("IP address provided is not usable (name doesn't" " resolve, or not a valid IPv4/IPv6 address)") # At this point the request seems reasonable, so lets check a bit deeper gw_api = '{}://{}:{}/api'.format(http_mode, gw_name, settings.config.api_port) # check the intended host actually has the requested IP available api = APIRequest(gw_api + '/sysinfo/ip_addresses') api.get() if api.response.status_code != 200: return ("ip_addresses query to {} failed - check " "rbd-target-api log. Is the API server " "running and in the right mode (http/https)?".format(gw_name)) try: target_ips = api.response.json()['data'] except Exception: return "Malformed REST API response" if gw_ip not in target_ips: return ("IP address of {} is not available on {}. Valid " "IPs are :{}".format(gw_ip, gw_name, ','.join(target_ips))) # check that config file on the new gateway matches the local machine api = APIRequest(gw_api + '/sysinfo/checkconf') api.get() if api.response.status_code != 200: return ("checkconf API call to {} failed with " "code".format(gw_name, api.response.status_code)) # compare the hash of the new gateways conf file with the local one local_hash = gen_file_hash('/etc/ceph/iscsi-gateway.cfg') try: remote_hash = str(api.response.json()['data']) except Exception: remote_hash = None if local_hash != remote_hash: return ("/etc/ceph/iscsi-gateway.cfg on {} does " "not match the local version. Correct and " "retry request".format(gw_name)) # Check for package version dependencies api = APIRequest(gw_api + '/sysinfo/checkversions') api.get() if api.response.status_code != 200: try: errors = api.response.json()['data'] except Exception: return "Malformed REST API response" return ("{} failed package validation checks - " "{}".format(gw_name, ','.join(errors))) # At this point the gateway seems valid return "ok"