예제 #1
0
 def configure_files(self):
     """ Configure file resources."""
     print "- Configuring Files"
     runtime_start = time.time()
     nsync = 0
     osync = 0
     fail  = 0
     files = self.config['files']
     # Split out files
     _files = [f for f in files if files[f]['is_dir'] is False]
     
     for file in _files:
         action   = files[file]['action']
         ofile = files[file]['path']         
         
         if action == 'create':
             nmode = int(files[file]['mode'],8)
             nuid  = pwd.getpwnam(files[file]['owner'])[2]
             ngid  = grp.getgrnam(files[file]['group'])[2]
             
             # Stage a tempfile to hold new file contents
             _tempfile = tempfile.NamedTemporaryFile()
             _tempfile.write(files[file]['content'])
             _tempfile.flush()
             nfile = _tempfile.name    
             
             # Compare new and old files, sync if permissions or contents mismatch
             if os.path.isfile(ofile):
                 fstat = os.stat(ofile)
                 omode = stat.S_IMODE(fstat.st_mode)
                 ouid  = pwd.getpwuid(fstat.st_uid)[2]
                 ogid  = grp.getgrgid(fstat.st_gid)[2]             
                 if not filecmp.cmp(ofile, nfile) or omode != nmode or ogid != ngid or ouid != nuid:
                     utils.sync_file(ofile, nfile, nuid, ngid, nmode)
                     osync += 1      
                 else:
                     nsync += 1
             elif os.path.dirname(ofile):
                 # Create the file only if the base directory exists
                 open(ofile,'w').close()
                 utils.sync_file(ofile, nfile, nuid, ngid, nmode)
                 osync += 1
             else:
                 print "  Base directory not found, %s required." % (os.path.dirname(ofile))
                 fail += 1
             _tempfile.close()
         elif action == 'remove':
             if os.path.isfile(file):
                 os.remove(ofile)
                 osync += 1
             else:
                 nsync += 1
         else:
             pass
     
     runtime_end = time.time()
     runtime = (runtime_end - runtime_start)
     self.stats['files'] = {'runtime': runtime,'nsync': nsync,'osync': osync,'fail': fail}
예제 #2
0
 def configure_yum_repos(self):
     """Configure YUM repositories."""
     print "- Configuring Repos"  
     old_repo = '/etc/yum.repos.d/config.repo'
     
     # Stage a tempfile to hold new file contents
     _tempfile = tempfile.NamedTemporaryFile()
     _tempfile.write(self.config['repo_data'])
     _tempfile.flush()
     new_repo = _tempfile.name
     
     # Check if repo resource exist, create if missing
     if os.path.isfile(old_repo):
         if not filecmp.cmp(old_repo, new_repo):
             utils.sync_file(old_repo, new_repo, 0, 0, 644)
             self.stats['repos_status'] = "Success: Repos in sync"
         else:
             self.stats['repos_status'] = "Success: Repos in sync"
     else:
         print "  %s not found, creating..." % (old_repo)
         open(old_repo,'w').close()
         utils.sync_file(old_repo, new_repo, 0, 0, 644)
         self.stats['repos_status'] = "Success: Repos in sync"
     _tempfile.close()
예제 #3
0
    def configure_yum_repos(self):
        """Configure YUM repositories."""
        print "- Configuring Repos"
        old_repo = '/etc/yum.repos.d/config.repo'

        # Stage a tempfile to hold new file contents
        _tempfile = tempfile.NamedTemporaryFile()
        _tempfile.write(self.config['repo_data'])
        _tempfile.flush()
        new_repo = _tempfile.name

        # Check if repo resource exist, create if missing
        if os.path.isfile(old_repo):
            if not filecmp.cmp(old_repo, new_repo):
                utils.sync_file(old_repo, new_repo, 0, 0, 644)
                self.stats['repos_status'] = "Success: Repos in sync"
            else:
                self.stats['repos_status'] = "Success: Repos in sync"
        else:
            print "  %s not found, creating..." % (old_repo)
            open(old_repo, 'w').close()
            utils.sync_file(old_repo, new_repo, 0, 0, 644)
            self.stats['repos_status'] = "Success: Repos in sync"
        _tempfile.close()
예제 #4
0
    def configure_files(self):
        """ Configure file resources."""
        print "- Configuring Files"
        runtime_start = time.time()
        nsync = 0
        osync = 0
        fail = 0
        files = self.config['files']
        # Split out files
        _files = [f for f in files if files[f]['is_dir'] is False]

        for file in _files:
            action = files[file]['action']
            ofile = files[file]['path']

            if action == 'create':
                nmode = int(files[file]['mode'], 8)
                nuid = pwd.getpwnam(files[file]['owner'])[2]
                ngid = grp.getgrnam(files[file]['group'])[2]

                # Stage a tempfile to hold new file contents
                _tempfile = tempfile.NamedTemporaryFile()
                _tempfile.write(files[file]['content'])
                _tempfile.flush()
                nfile = _tempfile.name

                # Compare new and old files, sync if permissions or contents mismatch
                if os.path.isfile(ofile):
                    fstat = os.stat(ofile)
                    omode = stat.S_IMODE(fstat.st_mode)
                    ouid = pwd.getpwuid(fstat.st_uid)[2]
                    ogid = grp.getgrgid(fstat.st_gid)[2]
                    if not filecmp.cmp(
                            ofile, nfile
                    ) or omode != nmode or ogid != ngid or ouid != nuid:
                        utils.sync_file(ofile, nfile, nuid, ngid, nmode)
                        osync += 1
                    else:
                        nsync += 1
                elif os.path.dirname(ofile):
                    # Create the file only if the base directory exists
                    open(ofile, 'w').close()
                    utils.sync_file(ofile, nfile, nuid, ngid, nmode)
                    osync += 1
                else:
                    print "  Base directory not found, %s required." % (
                        os.path.dirname(ofile))
                    fail += 1
                _tempfile.close()
            elif action == 'remove':
                if os.path.isfile(file):
                    os.remove(ofile)
                    osync += 1
                else:
                    nsync += 1
            else:
                pass

        runtime_end = time.time()
        runtime = (runtime_end - runtime_start)
        self.stats['files'] = {
            'runtime': runtime,
            'nsync': nsync,
            'osync': osync,
            'fail': fail
        }