Package config_client :: Module rpc_cli_repository
[hide private]
[frames] | no frames]

Source Code for Module config_client.rpc_cli_repository

  1  # 
  2  # Copyright (c) 2008--2017 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  import os 
 17  import sys 
 18   
 19  try: # python2 
 20      import xmlrpclib 
 21  except ImportError: # python3 
 22      import xmlrpc.client as xmlrpclib 
 23   
 24  from config_common import local_config, cfg_exceptions, file_utils, \ 
 25      repository 
 26  from config_common.rhn_log import log_debug 
 27  from spacewalk.common.usix import raise_with_tb 
 28   
 29  import traceback 
 30   
31 -class ClientRepository(repository.RPC_Repository):
32 33 default_systemid = "/etc/sysconfig/rhn/systemid" 34 35 # bug #170825,169203: reusing the base class's default value for setup_network
36 - def __init__(self, setup_network=1):
37 repository.RPC_Repository.__init__(self, setup_network) 38 39 systemid_file = local_config.get("systemid") or self.default_systemid 40 try: 41 f = open(systemid_file, "r") 42 except IOError: 43 e = sys.exc_info()[1] 44 sys.stderr.write("Cannot open %s: %s\n" % (systemid_file, e)) 45 sys.exit(1) 46 47 self.system_id = f.read() 48 f.close() 49 50 log_debug(4, 'system id', self.system_id) 51 52 self.files_to_delete = []
53
54 - def rpc_call(self, method_name, *params):
55 try: 56 result = repository.RPC_Repository.rpc_call(self, method_name, *params) 57 except xmlrpclib.Fault: 58 e = sys.exc_info()[1] 59 if e.faultCode == -9: 60 # System not subscribed 61 raise_with_tb(cfg_exceptions.AuthenticationError( 62 "Invalid digital server certificate%s" % e.faultString), sys.exc_info()[2]) 63 raise 64 return result
65
66 - def load_config_channels(self):
67 log_debug(4) 68 self.config_channels = self.rpc_call( 69 'config.client.list_config_channels', self.system_id) 70 return self.config_channels
71
72 - def list_files(self):
73 log_debug(4) 74 return self.rpc_call('config.client.list_files', self.system_id)
75
76 - def get_file_info(self, file, auto_delete=1, dest_directory=None):
77 log_debug(4, file) 78 result = self.rpc_call('config.client.get_file', self.system_id, file) 79 80 if 'missing' in result: 81 return None 82 83 dirs_created = None 84 85 # Older servers will not return directories; if filetype is missing, 86 # assume file 87 if result.get('filetype') == 'directory': 88 if dest_directory: 89 result['path'] = dest_directory.rstrip(os.path.sep) + result['path'] 90 if os.path.isfile(result['path']): 91 raise cfg_exceptions.DirectoryEntryIsFile(result['path']) 92 else: 93 auto_delete = 0 94 temp_file = result['path'] 95 else: 96 f = file_utils.FileProcessor() 97 temp_file, dirs_created = f.process(result, directory=dest_directory) 98 99 if auto_delete: 100 self.files_to_delete.append(temp_file) 101 102 return temp_file, result, dirs_created
103
104 - def put_files(self, action_id, files, upload_contents=1):
105 """Inserts a set of files into the repo, as a result of a scheduled 106 action""" 107 log_debug(4) 108 missing_files = [] 109 files_too_large = [] 110 failed_due_to_quota = [] 111 112 max_file_size = self.get_maximum_file_size() 113 114 for file in files: 115 try: 116 params = self._make_file_info(file, local_path=None, 117 load_contents=upload_contents) 118 except cfg_exceptions.RepositoryLocalFileError: 119 missing_files.append(file) 120 continue 121 122 if upload_contents and (params['size'] > max_file_size): 123 files_too_large.append(file) 124 continue 125 126 try: 127 self.rpc_call('config.client.upload_file', 128 self.system_id, action_id, params) 129 except xmlrpclib.Fault: 130 e = sys.exc_info()[1] 131 fault_code, fault_string = e.faultCode, e.faultString 132 # deal with particular faults 133 if fault_code == -4003: 134 # File too large 135 files_too_large.append(file) 136 elif fault_code == -4014: 137 # Ran out of org quota space 138 failed_due_to_quota.append(file) 139 else: 140 raise_with_tb(cfg_exceptions.RepositoryFilePushError(fault_code, 141 fault_string), sys.exc_info()[2]) 142 except Exception: 143 traceback.print_exc() 144 raise 145 146 result = {} 147 # If there are files too large to be pushed, result will have a key 148 # `file_too_large' 149 if len(files_too_large) > 0: 150 result['files_too_large'] = files_too_large 151 152 if len(failed_due_to_quota) > 0: 153 result['failed_due_to_quota'] = failed_due_to_quota 154 155 if len(missing_files) > 0: 156 result['missing_files'] = missing_files 157 158 return result
159 160
161 - def list_config_channels(self):
162 log_debug(4) 163 return self.config_channels
164
165 - def _get_default_delimiters(self):
166 "retrieves the default delimiters from the server" 167 log_debug(4) 168 result = self.rpc_call('config.client.get_default_delimiters', 169 self.system_id) 170 return result.get('delim_start'), result.get('delim_end')
171
172 - def _get_maximum_file_size(self):
173 log_debug(4) 174 result = self.rpc_call('config.client.get_maximum_file_size', 175 self.system_id) 176 return result
177
178 - def cleanup(self):
179 log_debug(4) 180 for file in self.files_to_delete: 181 os.unlink(file)
182