예제 #1
0
    def _yum_local_update(self, package_name, packages_dir, proc_niceness):
        logger.debug('Installing {0}'.format(package_name))
        #TODO: figure out if restart needed
        restart = 'false'

        rpms = glob.glob(os.path.join(packages_dir, '*.rpm'))

        cmd = [
            'nice', '-n',
            CpuPriority.niceness_to_string(proc_niceness), yum.yum_cmd,
            '--nogpgcheck', 'localupdate', '-y'
        ]
        cmd.extend(rpms)

        try:
            output, error = self.utilcmds.run_command(cmd)

            if error:
                raise Exception(error)

        except Exception as e:
            logger.error('Faled to install {0}'.format(package_name))
            logger.exception(e)

            return 'false', str(e), restart

        logger.debug('Done installing {0}'.format(package_name))

        return 'true', '', restart
예제 #2
0
    def _yum_update(self, package_name, proc_niceness):
        logger.debug('Updating: {0}'.format(package_name))

        #TODO: figure out if restart needed after update
        restart = 'false'

        try:
            cmd = [
                'nice', '-n',
                CpuPriority.niceness_to_string(proc_niceness), yum.yum_cmd,
                'update', '-y', package_name
            ]
            result, err = self.utilcmds.run_command(cmd)

            if err:
                if 'warning:' in err:
                    pass
                else:
                    raise Exception(err)

        except Exception as e:
            logger.error('Faled to update {0}'.format(package_name))
            logger.exception(e)

            return 'false', str(e), restart

        logger.debug('Done updating {0}'.format(package_name))

        return 'true', '', restart
예제 #3
0
    def _rpm_install(self, update_dir, proc_niceness):
        install_command = [
            'nice', '-n',
            CpuPriority.niceness_to_string(proc_niceness), '/bin/rpm', '-U',
            update_dir
        ]

        try:
            result, err = self.utilcmds.run_command(install_command)

            if err:
                raise Exception(err)

            logger.info("Installed all packages in: " + update_dir)

            return 'true', ''

        except Exception as e:
            logger.error("Failed to install packages in: " + update_dir)

            return 'false', str(e)

        else:
            logger.info("No directory provided.")

        return 'false', 'Update dir: ' + update_dir
예제 #4
0
    def _apt_install(self, package_name, proc_niceness):
        logger.debug('Installing {0}'.format(package_name))

        install_command = [
            'nice', '-n',
            CpuPriority.niceness_to_string(proc_niceness), self.APT_GET_EXE,
            'install', '-y', package_name
        ]

        #TODO: figure out if restart needed
        restart = 'false'

        # TODO: parse out the error, if any
        try:
            result, err = self.utilcmds.run_command(install_command)

            if err:
                err_lower = err.lower()
                # Catch non-error related messages
                if 'reading changelogs' in err_lower:
                    pass
                elif 'dpkg-preconfigure: unable to re-open stdin' in err_lower:
                    pass
                else:
                    raise Exception(err)

        except Exception as e:
            logger.error('Faled to install {0}'.format(package_name))
            logger.exception(e)

            return 'false', str(e), restart

        logger.debug('Done installing {0}'.format(package_name))

        return 'true', '', restart
예제 #5
0
    def _dpkg_install(self, update_dir, proc_niceness):
        """Install an update or install a new package.

        If an update directory is given, it must be made sure that
        the directory contains all of the dependencies for ALL of the
        packages inside the directory.
        """

        if update_dir:
            install_command = [
                'nice', '-n',
                CpuPriority.niceness_to_string(proc_niceness), 'dpkg', '-i',
                '-R', update_dir
            ]

            try:
                result, err = self.utilcmds.run_command(install_command)

                if err:
                    raise Exception(err)

                logger.info("Installed all packages in: " + update_dir)

                return 'true', ''

            except Exception as e:
                logger.error("Failed to install packages in: " + update_dir)

                return 'false', str(e)
        else:
            logger.info("No directory provided.")

        return 'false', 'Update dir: ' + update_dir
예제 #6
0
    def _yum_local_update(self, package_name, packages_dir, proc_niceness):
        logger.debug('Installing {0}'.format(package_name))
        #TODO: figure out if restart needed
        restart = 'false'

        rpms = glob.glob(os.path.join(packages_dir, '*.rpm'))

        cmd = [
            'nice',
            '-n',
            CpuPriority.niceness_to_string(proc_niceness),
            yum.yum_cmd,
            '--nogpgcheck',
            'localupdate',
            '-y'
        ]
        cmd.extend(rpms)

        try:
            output, error = self.utilcmds.run_command(cmd)

            if error:
                raise Exception(error)

        except Exception as e:
            logger.error('Faled to install {0}'.format(package_name))
            logger.exception(e)

            return 'false', str(e), restart

        logger.debug('Done installing {0}'.format(package_name))

        return 'true', '', restart
예제 #7
0
    def _rpm_install(self, update_dir, proc_niceness):
        install_command = [
            'nice',
            '-n',
            CpuPriority.niceness_to_string(proc_niceness),
            '/bin/rpm',
            '-U',
            update_dir
        ]

        try:
            result, err = self.utilcmds.run_command(install_command)

            if err:
                raise Exception(err)

            logger.info("Installed all packages in: " + update_dir)

            return 'true', ''

        except Exception as e:
            logger.error("Failed to install packages in: " + update_dir)

            return 'false', str(e)

        else:
            logger.info("No directory provided.")

        return 'false', 'Update dir: ' + update_dir
예제 #8
0
    def _yum_update(self, package_name, proc_niceness):
        logger.debug('Updating: {0}'.format(package_name))

        #TODO: figure out if restart needed after update
        restart = 'false'

        try:
            cmd = [
                'nice',
                '-n',
                CpuPriority.niceness_to_string(proc_niceness),
                yum.yum_cmd,
                'update',
                '-y',
                package_name
            ]
            result, err = self.utilcmds.run_command(cmd)

            if err:
                if 'warning:' in err:
                    pass
                else:
                    raise Exception(err)

        except Exception as e:
            logger.error('Faled to update {0}'.format(package_name))
            logger.exception(e)

            return 'false', str(e), restart

        logger.debug('Done updating {0}'.format(package_name))

        return 'true', '', restart
예제 #9
0
    def installer(self, pkg, proc_niceness):

        installer_cmd = [
            "nice",
            "-n",
            CpuPriority.niceness_to_string(proc_niceness),
            self.installer_cmd,
            "-pkg",
            "%s" % pkg,
            "-target",
            "/",
        ]

        proc = subprocess.Popen(installer_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        raw_output, _stderr = proc.communicate()

        unknown_output = []
        success = "false"
        error = ""

        for output in raw_output.splitlines():

            logger.debug(output)

            # All known output begins with 'installer' so remove it if present.
            if output.find("installer:") == 0:
                output = output.partition(":")[2].strip()

            # Known successful output:
            # 'The upgrade was successful.'
            # 'The install was successful.'
            if "successful" in output:
                success = "true"
                error = ""
                break

            elif "Package name is" in output:
                continue

            # Similar output:
            # Installing at base path
            # Upgrading at base path
            elif "at base path" in output:
                continue

            else:
                # Assuming failure.
                unknown_output.append(output)
                error = ""

        if len(unknown_output) != 0:
            error = ". ".join([output for output in unknown_output])

        return success, error
예제 #10
0
    def installer(self, pkg, proc_niceness):

        installer_cmd = [
            'nice', '-n',
            CpuPriority.niceness_to_string(proc_niceness), self.installer_cmd,
            '-pkg',
            '%s' % pkg, '-target', '/'
        ]

        proc = subprocess.Popen(installer_cmd,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        raw_output, _stderr = proc.communicate()

        unknown_output = []
        success = 'false'
        error = ''

        for output in raw_output.splitlines():

            logger.debug(output)

            # All known output begins with 'installer' so remove it if present.
            if output.find('installer:') == 0:
                output = output.partition(':')[2].strip()

            # Known successful output:
            # 'The upgrade was successful.'
            # 'The install was successful.'
            if 'successful' in output:
                success = 'true'
                error = ''
                break

            elif 'Package name is' in output:
                continue

            # Similar output:
            # Installing at base path
            # Upgrading at base path
            elif 'at base path' in output:
                continue

            else:
                # Assuming failure.
                unknown_output.append(output)
                error = ''

        if len(unknown_output) != 0:
            error = ". ".join([output for output in unknown_output])

        return success, error
예제 #11
0
    def _apt_install(self, package_name, proc_niceness):
        logger.debug('Installing {0}'.format(package_name))

        install_command = [
            'nice',
            '-n',
            CpuPriority.niceness_to_string(proc_niceness),
            self.APT_GET_EXE,
            'install',
            '-y',
            package_name
        ]

        #TODO: figure out if restart needed
        restart = 'false'

        # TODO: parse out the error, if any
        try:
            result, err = self.utilcmds.run_command(install_command)

            if err:
                err_lower = err.lower()
                # Catch non-error related messages
                if 'reading changelogs' in err_lower:
                    pass
                elif 'dpkg-preconfigure: unable to re-open stdin' in err_lower:
                    pass
                else:
                    raise Exception(err)

        except Exception as e:
            logger.error('Faled to install {0}'.format(package_name))
            logger.exception(e)

            return 'false', str(e), restart

        logger.debug('Done installing {0}'.format(package_name))

        return 'true', '', restart
예제 #12
0
    def _dpkg_install(self, update_dir, proc_niceness):
        """Install an update or install a new package.

        If an update directory is given, it must be made sure that
        the directory contains all of the dependencies for ALL of the
        packages inside the directory.
        """

        if update_dir:
            install_command = [
                'nice',
                '-n',
                CpuPriority.niceness_to_string(proc_niceness),
                'dpkg',
                '-i',
                '-R',
                update_dir
            ]

            try:
                result, err = self.utilcmds.run_command(install_command)

                if err:
                    raise Exception(err)

                logger.info("Installed all packages in: " + update_dir)

                return 'true', ''

            except Exception as e:
                logger.error("Failed to install packages in: " + update_dir)

                return 'false', str(e)
        else:
            logger.info("No directory provided.")

        return 'false', 'Update dir: ' + update_dir
예제 #13
0
    def softwareupdate(self, update_name, proc_niceness):

        #  Need to wrap call to /usr/sbin/softwareupdate with a utility
        # that makes softwareupdate think it is connected to a tty-like
        # device so its output is unbuffered so we can get progress info
        # '-v' (verbose) option is available on OSX > 10.5

        cmd = [
            "nice",
            "-n",
            CpuPriority.niceness_to_string(proc_niceness),
            self.ptyexec_cmd,
            self.softwareupdate_cmd,
            "-v",
            "-i",
            update_name,
        ]
        logger.debug("Running softwareupdate: " + str(cmd))

        success = "false"
        error = ""

        if not os.path.exists(self.ptyexec_cmd):
            raise PtyExecMissingException(settings.BinPath)

        try:
            job = launchd.Job(cmd)
            job.start()
        except launchd.LaunchdJobException as e:
            error_message = "Error with launchd job (%s): %s" % (cmd, str(e))
            logger.error(error_message)
            logger.critical("Skipping softwareupdate run.")

            return "false", error_message

        while True:

            output = job.stdout.readline()

            if not output:
                if job.returncode() is not None:
                    break
                else:
                    # no data, but we're still running
                    # sleep a bit before checking for more output
                    time.sleep(2)
                    continue

            # Checking output to verify results.
            output = output.decode("UTF-8").strip()
            if output.startswith("Installed "):
                # 10.6 / 10.7 / 10.8 Successful install of package name.
                success = "true"
                error = ""
                break

            elif output.startswith("Done with"):
                success = "true"
                error = ""
                break

            #            elif output.startswith('Done '):
            #                # 10.5. Successful install of package name.
            #                install_successful = True

            elif "No such update" in output:
                # 10.8 When a package cannot be found.
                success = "false"
                error = "Update not found."
                break

            elif output.startswith("Error "):
                # 10.8 Error statement
                # Note: Checking for updates doesn't display the
                # 'Error' string when connection is down.
                if "Internet connection appears to be offline" in output:
                    error = "Could not download files."
                else:
                    error = output

                    success = "false"

                break

            elif "restart immediately" in output:
                # Ignore if output is indicating a restart. Exact line:
                # "You have installed one or more updates that requires that
                # you restart your computer.  Please restart immediately."
                continue

            elif output.startswith("Package failed"):
                success = "false"
                error = output
                logger.debug(error)
                break

            elif (
                output == ""
                or output.startswith("Progress")
                or output.startswith("Done")
                or output.startswith("Running package")
                or output.startswith("Copyright")
                or output.startswith("Software Update Tool")
                or output.startswith("Downloading")
                or output.startswith("Moving items into place")
                or output.startswith("Writing package receipts")
                or output.startswith("Removing old files")
                or output.startswith("Registering updated components")
                or output.startswith("Waiting for other installations")
                or output.startswith("Writing files")
                or output.startswith("Cleaning up")
                or output.startswith("Registering updated applications")
                or output.startswith("About")
                or output.startswith("Less than a minute")
            ):
                # Output to ignore
                continue

            elif (
                output.startswith("Checking packages")
                or output.startswith("Installing")
                or output.startswith("Optimizing system for installed software")
                or output.startswith("Waiting to install")
                or output.startswith("Validating packages")
                or output.startswith("Finding available software")
                or output.startswith("Downloaded")
            ):
                # Output to display
                logger.debug("softwareupdate: " + output)

            else:
                success = "false"
                error = "softwareupdate (unknown): " + output
                logger.debug(error)

        #        return_code = job.returncode()
        #        if return_code == 0:
        #            # get SoftwareUpdate's LastResultCode
        #            su_path = '/Library/Preferences/com.apple.SoftwareUpdate.plist'
        #            su_prefs = plist.convert_and_read_plist(su_path)
        #            last_result_code = su_prefs['LastResultCode'] or 0
        #            if last_result_code > 2:
        #                return_code = last_result_code

        logger.debug("Done with softwareupdate.")
        return success, error
예제 #14
0
    def softwareupdate(self, update_name, proc_niceness):

        #  Need to wrap call to /usr/sbin/softwareupdate with a utility
        # that makes softwareupdate think it is connected to a tty-like
        # device so its output is unbuffered so we can get progress info
        # '-v' (verbose) option is available on OSX > 10.5

        cmd = [
            'nice', '-n',
            CpuPriority.niceness_to_string(proc_niceness), self.ptyexec_cmd,
            self.softwareupdate_cmd, '-v', '-i', update_name
        ]
        logger.debug("Running softwareupdate: " + str(cmd))

        success = 'false'
        error = ''

        if not os.path.exists(self.ptyexec_cmd):
            raise PtyExecMissingException(settings.BinPath)

        try:
            job = launchd.Job(cmd)
            job.start()
        except launchd.LaunchdJobException as e:
            error_message = 'Error with launchd job (%s): %s' % (cmd, str(e))
            logger.error(error_message)
            logger.critical('Skipping softwareupdate run.')

            return 'false', error_message

        while True:

            output = job.stdout.readline()

            if not output:
                if job.returncode() is not None:
                    break
                else:
                    # no data, but we're still running
                    # sleep a bit before checking for more output
                    time.sleep(2)
                    continue

            # Checking output to verify results.
            output = output.decode('UTF-8').strip()
            if output.startswith('Installed '):
                # 10.6 / 10.7 / 10.8 Successful install of package name.
                success = 'true'
                error = ''
                break

            elif output.startswith('Done with'):
                success = 'true'
                error = ''
                break

            #            elif output.startswith('Done '):
            #                # 10.5. Successful install of package name.
            #                install_successful = True

            elif 'No such update' in output:
                # 10.8 When a package cannot be found.
                success = 'false'
                error = "Update not found."
                break

            elif output.startswith('Error '):
                # 10.8 Error statement
                # Note: Checking for updates doesn't display the
                # 'Error' string when connection is down.
                if "Internet connection appears to be offline" in output:
                    error = "Could not download files."
                else:
                    error = output

                    success = 'false'

                break

            elif 'restart immediately' in output:
                # Ignore if output is indicating a restart. Exact line:
                # "You have installed one or more updates that requires that
                # you restart your computer.  Please restart immediately."
                continue

            elif output.startswith('Package failed'):
                success = 'false'
                error = output
                logger.debug(error)
                break

            elif (output == '' or output.startswith('Progress')
                  or output.startswith('Done')
                  or output.startswith('Running package')
                  or output.startswith('Copyright')
                  or output.startswith('Software Update Tool')
                  or output.startswith('Downloading')
                  or output.startswith('Moving items into place')
                  or output.startswith('Writing package receipts')
                  or output.startswith('Removing old files')
                  or output.startswith('Registering updated components')
                  or output.startswith('Waiting for other installations')
                  or output.startswith('Writing files')
                  or output.startswith('Cleaning up')
                  or output.startswith('Registering updated applications')
                  or output.startswith('About')
                  or output.startswith('Less than a minute')):
                # Output to ignore
                continue

            elif (output.startswith('Checking packages')
                  or output.startswith('Installing') or
                  output.startswith('Optimizing system for installed software')
                  or output.startswith('Waiting to install')
                  or output.startswith('Validating packages')
                  or output.startswith('Finding available software')
                  or output.startswith('Downloaded')):
                # Output to display
                logger.debug('softwareupdate: ' + output)

            else:
                success = 'false'
                error = "softwareupdate (unknown): " + output
                logger.debug(error)


#        return_code = job.returncode()
#        if return_code == 0:
#            # get SoftwareUpdate's LastResultCode
#            su_path = '/Library/Preferences/com.apple.SoftwareUpdate.plist'
#            su_prefs = plist.convert_and_read_plist(su_path)
#            last_result_code = su_prefs['LastResultCode'] or 0
#            if last_result_code > 2:
#                return_code = last_result_code

        logger.debug("Done with softwareupdate.")
        return success, error