def setPermsPath(path, user=None, group='root', chmod=int('0750', 8)): """chown user.group and set permissions to chmod""" if isSUSE() and user is None: user = '******' elif user is None: user = '******' if not os.path.exists(path): raise OSError( "*** ERROR: Path doesn't exist (can't set permissions): %s" % path) # If non-root, don't bother to change owners if os.getuid() != 0: return gc = GecosCache() uid = gc.getuid(user) if uid is None: raise OSError( "*** ERROR: user '%s' doesn't exist. Cannot set permissions properly." % user) gid = gc.getgid(group) if gid is None: raise OSError( "*** ERROR: group '%s' doesn't exist. Cannot set permissions properly." % group) uid_, gid_ = os.stat(path)[4:6] if uid_ != uid or gid_ != gid: os.chown(path, uid, gid) os.chmod(path, chmod)
def createPath(path, user=None, group=None, chmod=int('0755', 8)): """advanced makedirs Will create the path if necessary. Will chmod, and chown that path properly. Defaults for user/group to the apache user Uses the above makedirs() function. """ if isSUSE(): if user is None: user = '******' if group is None: group = 'www' else: if user is None: user = '******' if group is None: group = 'apache' path = cleanupAbsPath(path) if not os.path.exists(path): makedirs(path, mode=chmod, user=user, group=group) elif not os.path.isdir(path): raise ValueError("ERROR: createPath('%s'): path doesn't lead to a directory" % str(path)) else: os.chmod(path, chmod) uid, gid = getUidGid(user, group) try: os.chown(path, uid, gid) except OSError: # Changing permissions failed; ignore the error sys.stderr.write("Changing owner for %s failed\n" % path)
def createPath(path, user=None, group=None, chmod=int('0755', 8)): """advanced makedirs Will create the path if necessary. Will chmod, and chown that path properly. Defaults for user/group to the apache user Uses the above makedirs() function. """ if isSUSE(): if user is None: user = '******' if group is None: group = 'www' else: if user is None: user = '******' if group is None: group = 'apache' path = cleanupAbsPath(path) if not os.path.exists(path): makedirs(path, mode=chmod, user=user, group=group) elif not os.path.isdir(path): raise ValueError( "ERROR: createPath('%s'): path doesn't lead to a directory" % str(path)) else: os.chmod(path, chmod) uid, gid = getUidGid(user, group) try: os.chown(path, uid, gid) except OSError: # Changing permissions failed; ignore the error sys.stderr.write("Changing owner for %s failed\n" % path)
def setPermsPath(path, user=None, group='root', chmod=int('0750', 8)): """chown user.group and set permissions to chmod""" if isSUSE() and user is None: user = '******' elif user is None: user = '******' if not os.path.exists(path): raise OSError("*** ERROR: Path doesn't exist (can't set permissions): %s" % path) # If non-root, don't bother to change owners if os.getuid() != 0: return gc = GecosCache() uid = gc.getuid(user) if uid is None: raise OSError("*** ERROR: user '%s' doesn't exist. Cannot set permissions properly." % user) gid = gc.getgid(group) if gid is None: raise OSError("*** ERROR: group '%s' doesn't exist. Cannot set permissions properly." % group) uid_, gid_ = os.stat(path)[4:6] if uid_ != uid or gid_ != gid: os.chown(path, uid, gid) os.chmod(path, chmod)
def __init__(self, channel_label, repo_type, url=None, fail=False, quiet=False, filters=None, no_errata=False, sync_kickstart=False, latest=False, strict=0): self.regen = False self.fail = fail self.quiet = quiet self.filters = filters or [] self.no_errata = no_errata self.sync_kickstart = sync_kickstart self.latest = latest initCFG('server.satellite') rhnSQL.initDB() # setup logging log_filename = channel_label + '.log' if CFG.DEBUG is not None: log_level = CFG.DEBUG rhnLog.initLOG(default_log_location + log_filename, log_level) # os.fchown isn't in 2.4 :/ if isSUSE(): os.system("chgrp www " + default_log_location + log_filename) else: os.system("chgrp apache " + default_log_location + log_filename) self.log_msg("\nSync started: %s" % (time.asctime(time.localtime()))) self.log_msg(str(sys.argv)) self.channel_label = channel_label self.channel = self.load_channel() if not self.channel: self.print_msg("Channel does not exist.") sys.exit(1) if not url: # TODO:need to look at user security across orgs h = rhnSQL.prepare("""select s.id, s.source_url, s.label, fm.channel_family_id from rhnContentSource s, rhnChannelContentSource cs, rhnChannelFamilyMembers fm where s.id = cs.source_id and cs.channel_id = fm.channel_id and cs.channel_id = :channel_id""") h.execute(channel_id=int(self.channel['id'])) source_data = h.fetchall_dict() if source_data: self.urls = [(row['id'], row['source_url'], row['label'], row['channel_family_id']) for row in source_data] else: self.error_msg("Channel has no URL associated") sys.exit(1) else: self.urls = [(None, u, None, None) for u in url] self.repo_plugin = self.load_plugin(repo_type) self.strict = strict
def cache_set(self, params, value): log_debug(4, params) last_modified = self._get_last_modified(params) key = self._get_key(params) user = '******' group = 'apache' if rhnLib.isSUSE(): user = '******' group = 'www' return rhnCache.set(key, value, modified=last_modified, raw=1, user=user, group=group, mode=int('0755', 8))
def initLOG(log_file="stderr", level=0): global LOG # check if it already setup if LOG is not None: # We already have a logging object if log_file is None or LOG.file == log_file: # Keep the same logging object, change only the log level LOG.level = level return # We need a different type, so destroy the old one LOG = None elif log_file is None: log_file = "/dev/null" # attempt to create the path to the log file if neccessary log_path = os.path.dirname(log_file) if log_file not in ('stderr', 'stdout') \ and log_path and not os.path.exists(os.path.dirname(log_file)): log_stderr( "WARNING: log path not found; attempting to create %s" % log_path, sys.exc_info()[:2]) # fetch uid, gid so we can do a "chown ..." if isSUSE(): apache_uid, apache_gid = getUidGid('wwwrun', 'www') else: apache_uid, apache_gid = getUidGid('apache', 'apache') try: os.makedirs(log_path) if os.getuid() == 0: os.chown(log_path, apache_uid, 0) else: os.chown(log_path, apache_uid, apache_gid) except: log_stderr("ERROR: unable to create log file path %s" % log_path, sys.exc_info()[:2]) return # At this point, LOG is None and log_file is not None # Get a new LOG LOG = rhnLog(log_file, level) return 0
def initLOG(log_file="stderr", level=0): global LOG # check if it already setup if LOG is not None: # We already have a logging object if log_file is None or LOG.file == log_file: # Keep the same logging object, change only the log level LOG.level = level return # We need a different type, so destroy the old one LOG = None elif log_file is None: log_file = "/dev/null" # attempt to create the path to the log file if neccessary log_path = os.path.dirname(log_file) if log_file not in ('stderr', 'stdout') \ and log_path and not os.path.exists(os.path.dirname(log_file)): log_stderr("WARNING: log path not found; attempting to create %s" % log_path, sys.exc_info()[:2]) # fetch uid, gid so we can do a "chown ..." if isSUSE(): apache_uid, apache_gid = getUidGid('wwwrun', 'www') else: apache_uid, apache_gid = getUidGid('apache', 'apache') try: os.makedirs(log_path) if os.getuid() == 0: os.chown(log_path, apache_uid, 0) else: os.chown(log_path, apache_uid, apache_gid) except: log_stderr("ERROR: unable to create log file path %s" % log_path, sys.exc_info()[:2]) return # At this point, LOG is None and log_file is not None # Get a new LOG LOG = rhnLog(log_file, level) return 0
def __init__(self, log_file, level): self.level = level self.log_info = "0.0.0.0: " self.file = log_file self.pid = os.getpid() self.real = 0 if self.file in ["stderr", "stdout"]: self.fd = getattr(sys, self.file) self.log_info = "" return newfileYN = 0 if not os.path.exists(self.file): newfileYN = 1 # just used for the chown/chmod # else, open it as a real file, with locking and stuff try: # try to open it in line buffered mode self.fd = open(self.file, "a", 1) set_close_on_exec(self.fd) if newfileYN: if isSUSE(): apache_uid, apache_gid = getUidGid('wwwrun', 'www') else: apache_uid, apache_gid = getUidGid('apache', 'apache') if os.getuid() == 0: os.chown(self.file, apache_uid, 0) else: os.chown(self.file, apache_uid, apache_gid) os.chmod(self.file, int('0660', 8)) except: log_stderr("ERROR LOG FILE: Couldn't open log file %s" % self.file, sys.exc_info()[:2]) self.file = "stderr" self.fd = sys.stderr else: self.real = 1
def write_file(self, stream_in): """Writes the contents of stream_in to the filesystem Returns the file size(success) or raises FileCreationError""" dirname = os.path.dirname(self.full_path) createPath(dirname) stat = os.statvfs(dirname) f_bsize = stat[0] # file system block size # misa: it's kind of icky whether to use f_bfree (free blocks) or # f_bavail (free blocks for non-root). f_bavail is more correct, since # you don't want to have the system out of disk space because of # satsync; but people would get confused when looking at the output of # df f_bavail = stat[4] # free blocks freespace = f_bsize * float(f_bavail) if self.file_size is not None and self.file_size > freespace: msg = messages.not_enough_diskspace % (freespace / 1024) log(-1, msg, stream=sys.stderr) # pkilambi: As the metadata download does'nt check for unfetched rpms # abort the sync when it runs out of disc space sys.exit(-1) #raise FileCreationError(msg) if freespace < 5000 * 1024: # arbitrary msg = messages.not_enough_diskspace % (freespace / 1024) log(-1, msg, stream=sys.stderr) # pkilambi: As the metadata download does'nt check for unfetched rpms # abort the sync when it runs out of disc space sys.exit(-1) #raise FileCreationError(msg) fout = open(self.full_path, 'wb') # setting file permissions; NOTE: rhnpush uses apache to write to disk, # hence the 6 setting. if rhnLib.isSUSE(): setPermsPath(self.full_path, user='******', group='www', chmod=int('0644', 8)) else: setPermsPath(self.full_path, user='******', group='apache', chmod=int('0644', 8)) size = 0 try: while 1: buf = stream_in.read(self.buffer_size) if not buf: break buf_len = len(buf) fout.write(buf) size = size + buf_len except IOError: e = sys.exc_info()[1] msg = "IOError: %s" % e log(-1, msg, stream=sys.stderr) # Try not to leave garbage around try: os.unlink(self.full_path) except (OSError, IOError): pass raise_with_tb(FileCreationError(msg), sys.exc_info()[2]) l_file_size = fout.tell() fout.close() if self.file_size is not None and self.file_size != l_file_size: # Something bad happened msg = "Error: file %s has wrong size. Expected %s bytes, got %s bytes" % ( self.full_path, self.file_size, l_file_size) log(-1, msg, stream=sys.stderr) # Try not to leave garbage around try: os.unlink(self.full_path) except (OSError, IOError): pass raise FileCreationError(msg) os.utime(self.full_path, (self.timestamp, self.timestamp)) return l_file_size
def __init__(self, channel_label, repo_type, url=None, fail=False, filters=None, no_errata=False, sync_kickstart=False, latest=False, metadata_only=False, strict=0, excluded_urls=None, no_packages=False, log_dir="reposync", log_level=None, force_kickstart=False, force_all_errata=False, check_ssl_dates=False, force_null_org_content=False): self.regen = False self.fail = fail self.filters = filters or [] self.no_packages = no_packages self.no_errata = no_errata self.sync_kickstart = sync_kickstart self.force_all_errata = force_all_errata self.force_kickstart = force_kickstart self.latest = latest self.metadata_only = metadata_only self.ks_tree_type = 'externally-managed' self.ks_install_type = None initCFG('server.satellite') rhnSQL.initDB() # setup logging log_filename = channel_label + '.log' log_path = default_log_location + log_dir + '/' + log_filename if log_level is None: log_level = 0 CFG.set('DEBUG', log_level) rhnLog.initLOG(log_path, log_level) # os.fchown isn't in 2.4 :/ if isSUSE(): os.system("chgrp www " + log_path) else: os.system("chgrp apache " + log_path) log2disk(0, "Command: %s" % str(sys.argv)) log2disk(0, "Sync of channel started.") self.channel_label = channel_label self.channel = self.load_channel() if not self.channel: log(0, "Channel %s does not exist." % channel_label) if not self.channel['org_id'] or force_null_org_content: self.org_id = None else: self.org_id = int(self.channel['org_id']) if not url: # TODO:need to look at user security across orgs h = rhnSQL.prepare("""select s.id, s.source_url, s.label from rhnContentSource s, rhnChannelContentSource cs where s.id = cs.source_id and cs.channel_id = :channel_id""") h.execute(channel_id=int(self.channel['id'])) source_data = h.fetchall_dict() self.urls = [] if excluded_urls is None: excluded_urls = [] if source_data: for row in source_data: if row['source_url'] not in excluded_urls: self.urls.append( (row['id'], row['source_url'], row['label'])) else: self.urls = [(None, u, None) for u in url] if not self.urls: log2(0, 0, "Channel %s has no URL associated" % channel_label, stream=sys.stderr) self.repo_plugin = self.load_plugin(repo_type) self.strict = strict self.all_packages = [] self.check_ssl_dates = check_ssl_dates # Init cache for computed checksums to not compute it on each reposync run again self.checksum_cache = rhnCache.get(checksum_cache_filename) if self.checksum_cache is None: self.checksum_cache = {}
def __init__(self, channel_label, repo_type, url=None, fail=False, filters=None, no_errata=False, sync_kickstart=False, latest=False, metadata_only=False, strict=0, excluded_urls=None, no_packages=False, log_dir="reposync", log_level=None): self.regen = False self.fail = fail self.filters = filters or [] self.no_packages = no_packages self.no_errata = no_errata self.sync_kickstart = sync_kickstart self.latest = latest self.metadata_only = metadata_only self.ks_tree_type = 'externally-managed' self.ks_install_type = 'generic_rpm' initCFG('server.satellite') rhnSQL.initDB() # setup logging log_filename = channel_label + '.log' log_path = default_log_location + log_dir + '/' + log_filename if log_level is None: log_level = 0 CFG.set('DEBUG', log_level) rhnLog.initLOG(log_path, log_level) # os.fchown isn't in 2.4 :/ if isSUSE(): os.system("chgrp www " + log_path) else: os.system("chgrp apache " + log_path) log2disk(0, "Command: %s" % str(sys.argv)) log2disk(0, "Sync of channel started.") self.channel_label = channel_label self.channel = self.load_channel() if not self.channel: log(0, "Channel %s does not exist." % channel_label) if not url: # TODO:need to look at user security across orgs h = rhnSQL.prepare("""select s.id, s.source_url, s.label, fm.channel_family_id from rhnContentSource s, rhnChannelContentSource cs, rhnChannelFamilyMembers fm where s.id = cs.source_id and cs.channel_id = fm.channel_id and cs.channel_id = :channel_id""") h.execute(channel_id=int(self.channel['id'])) source_data = h.fetchall_dict() self.urls = [] if excluded_urls is None: excluded_urls = [] if source_data: for row in source_data: if row['source_url'] not in excluded_urls: self.urls.append((row['id'], row['source_url'], row['label'], row['channel_family_id'])) else: self.urls = [(None, u, None, None) for u in url] if not self.urls: log2stderr(0, "Channel %s has no URL associated" % channel_label) self.repo_plugin = self.load_plugin(repo_type) self.strict = strict self.all_packages = []