Example #1
0
def run_update_if_not_already_run(sudo_password, env):
    global update_run_this_execution
    if not update_run_this_execution:
        logger.info("Running apt-get update...")
        iuprocess.run_sudo_program([APT_GET_PATH, "-q", "-y", "update"], sudo_password,
                                   logger, env=env)
        update_run_this_execution=True
Example #2
0
 def run(self, selection_lines):
     with NamedTempFile(selection_lines) as f:
         iuprocess.run_sudo_program(["/usr/bin/debconf-set-selections",
                                     f.name],
                                    self.ctx._get_sudo_password(self),
                                    self.ctx.logger,
                                    cwd="/usr/bin")
Example #3
0
    def run(self, apache_config, timeout_tries=5, time_between_tries=2.0):
        """If you check whether apache is running just after it was (re)started, it may report that it
        isn't running when it is infact running. This is because apache takes a few seconds before it writes
        its pid file out. Thus, we need to use a timeout to see if apache could really be up when the status
        command reports that it is down. If you haven't just started apache, you can probably reduce the
        timeout duration. Note that time_between_tries is in seconds, so the default timeout is 10 seconds.
        """
        controller_exe = apache_config.controller_exe
        action._check_file_exists(controller_exe, self)
        if controller_exe=='/opt/local/apache2/bin/apachectl':
            """The apachectl for macports is broken. We determine whether apache is
            running by looking for the process
            """
            return _macports_apache_is_running(controller_exe, self.ctx._get_sudo_password(self))

        for i in range(timeout_tries):
            try:
                iuprocess.run_sudo_program([controller_exe, "status"],
                                           self.ctx._get_sudo_password(self), self.ctx.logger)
                self.ctx.logger.debug("Apache status is up")
                return True
            except iuprocess.SudoBadRc, e:
                if e.rc == 1: # this means the server is down
                    self.ctx.logger.debug("Apache status is down")
                    if i != (timeout_tries-1): time.sleep(time_between_tries)
                else:
                    exc_info = sys.exc_info()
                    self.ctx.logger.exception("Unexpected exception when checking for apache status occurred in resource %s" %
                                              self.ctx.props.id)
                    raise convert_exc_to_user_error(exc_info, errors[ERR_APACHE_STATUS], nested_exc_info=e.get_nested_exc_info(),
                                                    msg_args={"id":self.ctx.props.id})
Example #4
0
def dpkg_install(package_file, sudo_password, may_have_dependencies=True):
    """Given a package's .deb file, install using dpkg. may_have_dependencies should
    be left at True if it is possible that the .deb package has dependencies that aren't
    being managed by Engage. In this case, it will run an apt-get update to make
    sure the package database is up-to-date. If you know that you don't have dependencies,
    set this to False to suppress the apt-get update call, which can fail due
    to remote servers being temporarily unavailable.
    """
    env = _get_env_for_aptget()
    if not os.path.exists(DPKG_PATH):
        raise UserError(errors[ERR_DPKG_NOT_FOUND],
                        msg_args={"path":DPKG_PATH})
    if not os.path.exists(package_file):
        raise UserError(errors[ERR_PKG_FILE_NOT_FOUND],
                        msg_args={"path":package_file})
    
    try:
        if may_have_dependencies:
            run_update_if_not_already_run(sudo_password, env)
        iuprocess.run_sudo_program([DPKG_PATH, "-i", package_file], sudo_password,
                                   logger, env=env)
    except iuprocess.SudoError, e:
        exc_info = sys.exc_info()
        sys.exc_clear()
        raise convert_exc_to_user_error(exc_info, errors[ERR_DPKG_INSTALL],
                                        msg_args={"path":package_file},
                                        nested_exc_info=e.get_nested_exc_info())
Example #5
0
 def run(self):
     p = self.ctx.props
     _check_file_exists(p.mysql_install_db_script, self)
     procutils.run_sudo_program([p.mysql_install_db_script],
                                self.ctx._get_sudo_password(self), logger,
                                cwd=os.path.dirname(p.mysql_install_db_script),
                                user=p.output_ports.mysql_admin.mysql_user)
Example #6
0
 def run(self, apache_config):
     """Stop the apache server. apache_config is the port
     containing the apache config variables.
     """
     controller_exe = apache_config.controller_exe
     action._check_file_exists(controller_exe, self)
     iuprocess.run_sudo_program([controller_exe, "stop"],
                                self.ctx._get_sudo_password(self),
                                self.ctx.logger)
Example #7
0
def add_config_file_line(config_file, line, sudo_password):
    """Add line to config file (see above for detailed description).
    This is just a wrapper over _add_config_file_line(). Unless we are
    running as root, we need to spawn a subprocess and run it under sudo
    """ 
    if processutils.is_running_as_root():
        _add_config_file_line(config_file, line)
    else:
        processutils.run_sudo_program([sys.executable, __file__, config_file, line],
                                   sudo_password, logger)
Example #8
0
def apt_get_install(package_list, sudo_password):
    env = _get_env_for_aptget()
    if not os.path.exists(APT_GET_PATH):
        raise UserError(errors[ERR_APT_GET_NOT_FOUND],
                        msg_args={"path":APT_GET_PATH})
    
    try:
        run_update_if_not_already_run(sudo_password, env)
        iuprocess.run_sudo_program([APT_GET_PATH, "-q", "-y", "install"]+package_list, sudo_password,
                                   logger,
                                   env=env)
    except iuprocess.SudoError, e:
        exc_info = sys.exc_info()
        sys.exc_clear()
        raise convert_exc_to_user_error(exc_info, errors[ERR_APT_GET_INSTALL],
                                        msg_args={"pkgs":package_list.__repr__()},
                                        nested_exc_info=e.get_nested_exc_info())
Example #9
0
def update(self, always_run=True):
    """ACTION: Run the apt-get update command to update the list of available
    packages. By default always run the update command, even if it was already
    run, as it is assumed that an explicit call readlly needs the update. This
    is the case for add_apt_repository, where subsequent packages won't
    even be visible.
    """
    global update_run_this_execution
    if always_run or (not update_run_this_execution):
        self.ctx.logger.info("Running apt-get update...")
        iuprocess.run_sudo_program([APT_GET_PATH, "-q", "-y", "update"],
                                   self.ctx._get_sudo_password(self),
                                   self.ctx.logger,
                                   env=_get_env_for_aptget())
        update_run_this_execution = True
    else:
        self.ctx.logger.info("ignoring request for apt-get update, as update was already run")
Example #10
0
    def run(self, password_file, username, password, apache_config):
        class LogProxy:
            """We need to wrap the logger and capture any action events, as those may
            contain the password.
            """
            def __init__(self, logger):
                #Set attribute.
                self._logger = logger
            def __getattr__(self, attrib):
                if attrib == "action":
                    return self.action
                else:
                    return getattr(self._logger, attrib)
            def action(self, msg):
                pass

        htpasswd_exe = apache_config.htpasswd_exe
        action._check_file_exists(htpasswd_exe, self)
        if os.path.exists(password_file):
            cmd = [htpasswd_exe, "-b", password_file, username, password]
        else:
            cmd = [htpasswd_exe, "-b", "-c", password_file, username, password]
        if self.ctx._get_sudo_password(self)==None or iuprocess.is_running_as_root():
            self.ctx.logger.action("%s <password>" % " ".join(cmd[0:-1]))
            rc = iuprocess.run_and_log_program(cmd, {}, LogProxy(self.ctx.logger))
            if rc != 0:
                raise UserError(errors[ERR_APACHE_HTPASSWD],
                                msg_args={"exe": htpasswd_exe,
                                          "file": password_file,
                                          "id":self.ctx.props.id},
                                developer_msg="return code was %d" % rc)
        else:
            try:
                logger.action("sudo %s <password>" % " ".join(cmd[0:-1]))
                iuprocess.run_sudo_program(cmd, self.ctx._get_sudo_password(self),
                                           LogProxy(self.ctx.logger))
            except Exception, e:
                exc_info = sys.exc_info()
                logger.exception("exception in htpasswd: %s, resource %s" % (e.__repr__(),
                                                                             ctx.props.id))
                raise convert_exc_to_user_error(exc_info, errors[ERR_APACHE_HTPASSWD],
                                                msg_args={"exe": htpasswd_exe,
                                                          "file": password_file,
                                                          "id":self.ctx.props.id},
                                                developer_msg="exception" % e.__repr__())
Example #11
0
 def run(self, apache_config):
     """Restart the apache server. apache_config is the port
     containing the apache config variables.
     """
     controller_exe = apache_config.controller_exe
     action._check_file_exists(controller_exe, self)
     ## iuprocess.run_sudo_program([controller_exe, "restart"],
     ##                            self.ctx._get_sudo_password(self),
     ##                            self.ctx.logger)
     # The restart command doesn't seem to work in some
     # situations (see ticket #203). We do a real stop and start
     # instead.
     iuprocess.run_sudo_program([controller_exe, "stop"],
                                self.ctx._get_sudo_password(self),
                                self.ctx.logger)
     iuprocess.run_sudo_program([controller_exe, "start"],
                                self.ctx._get_sudo_password(self),
                                self.ctx.logger)
Example #12
0
 def run(self, package_list):
     """Install the specified port(s). If you have variants, include them in
     the package list. Expects the context to have an input port "macports"
     with a property "macports_exe".
     """
     port_exe = self.ctx.props.input_ports.macports.macports_exe
     action._check_file_exists(port_exe, self)
     try:
         iuprocess.run_sudo_program([port_exe, "install"]+package_list,
                                    self.ctx._get_sudo_password(self),
                                    self.ctx.logger,
                                    cwd=os.path.dirname(port_exe),
                                    env=ENV)
     except iuprocess.SudoError, e:
         exc_info = sys.exc_info()
         self.ctx.logger.exception("Port install for %s failed, unexpected exception" % package_list)
         sys.exc_clear()
         raise convert_exc_to_user_error(exc_info, errors[ERR_MACPORTS_INSTALL],
                                         msg_args={"pkg":package_list.__repr__(),
                                                   "id":self.ctx.props.id},
                                         nested_exc_info=e.get_nested_exc_info())
Example #13
0
 def prompt_for_password():
     if dry_run:
         logger.info("prompt for password '" + prompt + "', to be stored at key '%s'" % SUDO_PW_KEY)
         return "test"
     else:
         second_try = False
         while True:
             pw1 = getpass.getpass(prompt + ":")
             if pw1 == "" and second_try:
                 raise Exception("User requested cancel of install")
             pw2 = getpass.getpass(prompt + " (re-enter):")
             if pw1 == pw2:
                 logger.debug("Test the sudo password by running an ls")
                 try:
                     # test that the password actually works
                     procutils.run_sudo_program(["/bin/ls", "/"], pw1, logger)
                     logger.debug("Successfully validated sudo password")
                     return pw1
                 except Exception, e:
                     logger.debug("Sudo password is not working, got exception %s" % e)
                     print "The sudo password is not working, please re-enter the password or hit return to cancel install"
                     second_try = True
             else:
                 print "Sorry, passwords do not match!"