Пример #1
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})
Пример #2
0
    def run(self, package):
        """Run the load operation on the specified port(s).

        Unfortunately, there's no way to query if a port is already loaded.
        To get already this, we make the operation itempotent: we try the
        port load. If it fails, we scan the results to see if we get the error
        message indicating the port was already loaded. In that case, we ignore
        the error.
        """
        port_exe = self.ctx.props.input_ports.macports.macports_exe
        action._check_file_exists(port_exe, self)
        try:
            re_map = {"already_loaded": concat(lit("Error: Target org.macports.load returned:"),
                                               one_or_more(any_except_newline()),
                                               line_ends_with(lit("Already loaded"))).get_value()}
            (rc, result_map) = \
                 iuprocess.run_sudo_program_and_scan_results([port_exe, "load",
                                                              package],
                                                             re_map, self.ctx.logger,
                                                             self.ctx._get_sudo_password(self),
                                                             log_output=True,
                                                             cwd=os.path.dirname(port_exe),
                                                             env=ENV)
        except iuprocess.SudoError, e:
            self.ctx.logger.exception("Port load for %s failed, unexpected exception" % package)
            exc_info = sys.exc_info()
            sys.exc_clear()
            raise convert_exc_to_user_error(exc_info, errors[ERR_MACPORTS_LOAD],
                                            msg_args={"pkg":package, "id":self.ctx.props.id},
                                            nested_exc_info=e.get_nested_exc_info())
Пример #3
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)
Пример #4
0
 def dry_run(self, password_file, username, password, apache_config):
     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]
     self.ctx.logger.debug(" ".join(cmd))
Пример #5
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)
Пример #6
0
def add_environment_vars_to_classpath(self, classpath_file, env_mapping):
    _check_file_exists(classpath_file, self)
    backup_file = classpath_file + ".orig"
    if not os.path.exists(backup_file):
        shutil.copyfile(classpath_file, backup_file)
    assignments = ""
    for (var, value) in env_mapping.items():
        assignments += '%s="%s"\n' % (var, value)
    with open(classpath_file, "a") as f:
        f.write(assignments)
Пример #7
0
 def run(self, src_config_file, apache_config, new_name=None):
     action._check_file_exists(src_config_file, self)
     (uid, gid, mode) = fileutils.get_file_permissions(apache_config.config_file)
     if new_name:
         target_path = os.path.join(apache_config.module_config_dir,
                                    new_name)
     else:
         target_path = os.path.join(apache_config.module_config_dir,
                                    os.path.basename(src_config_file))
     iuprocess.sudo_copy([src_config_file, target_path], self.ctx._get_sudo_password(self), self.ctx.logger)
     iuprocess.sudo_set_file_permissions(target_path, uid, gid, mode, self.ctx.logger, self.ctx._get_sudo_password(self))
Пример #8
0
 def run(self, tomcat_port, app_path, warfile_path, admin_password,
         update=False, tag=None):
     _check_file_exists(warfile_path, self)
     deploy_app_fn(self.ctx.props.id,
                   tomcat_port.hostname,
                   tomcat_port.manager_port,
                   app_path,
                   warfile_path,
                   tomcat_port.admin_user,
                   admin_password,
                   update=update, tag=tag)
Пример #9
0
def check_installed(self, package):
    """Validate that a macports package is installed (e.g. in a post-install check)
    """
    port_exe = self.ctx.props.input_ports.macports.macports_exe
    action._check_file_exists(port_exe, self)
    (rc, map) = iuprocess.run_program_and_scan_results([port_exe, "installed", package],
                                                       {"found": re.escape(package) + ".*" + re.escape("(active)"),
                                                        'not_found': re.escape("None of the specified ports are installed.")},
                                                        self.ctx.logger,
                                                       log_output=True,
                                                       cwd=os.path.dirname(port_exe),
                                                       shell=True, env=ENV)
    if rc==0 and map['found']:
        return
    else:
        raise UserError(errors[ERR_POST_INSTALL],
                        msg_args={"pkg":package, "id":self.ctx.props.id})
Пример #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__())
Пример #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)
Пример #12
0
 def run(self, package):
     port_exe = self.ctx.props.input_ports.macports.macports_exe
     action._check_file_exists(port_exe, self)
     logger.debug("PATH=%s, HOME=%s" % (ENV["PATH"], ENV["HOME"]))
     (rc, map) = iuprocess.run_program_and_scan_results([port_exe, "installed", package],
                                                        {"found": re.escape(package) + ".*" + re.escape("(active)"),
                                                         'not_found': re.escape("None of the specified ports are installed.")},
                                                         self.ctx.logger,
                                                        log_output=True,
                                                        cwd=os.path.dirname(port_exe),
                                                        shell=True, env=ENV)
     if rc==0 and map['found']:
         return True
     elif rc==0 and map['not_found']:
         return False
     else:
         raise UserError(errors[ERR_MACPORTS_PKG_QUERY],
                         msg_args={"pkg":package, "id":self.ctx.props.id})
Пример #13
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())
Пример #14
0
 def run(self, user, password, script_file, database=None):
     p = self.ctx.props
     _check_file_exists(script_file, self)
     cmd = [p.input_ports.mysql_admin.mysql_client_exe, "-u", user,
            "--password=%s" % password]
     if database:
         cmd.extend(["-D", database])
     # make a version of cmd safe for logging
     no_pw_cmd = copy.copy(cmd)
     no_pw_cmd[3] = "--password=****"
     self.ctx.logger.debug(' '.join(no_pw_cmd))
     with open(script_file, "r") as sf:
         input_data = sf.read()
     rc = procutils.run_and_log_program(cmd, None, self.ctx.logger,
                                        input=input_data,
                                        hide_command=True,
                                        hide_input=True)
     if rc!=0:
         raise UserError(errors[ERR_MYSQL_SCRIPT],
                         msg_args={"id":p.id,
                                   "cmd":' '.join(cmd),
                                   "script":script_file},
                         developer_msg="rc was %d, command was: '%s'" %
                         (rc, command_text))
Пример #15
0
 def dry_run(self, package):
     port_exe = self.ctx.props.input_ports.macports.macports_exe
     action._check_file_exists(port_exe, self)
     return None
Пример #16
0
 def dry_run(self, package_list):
     port_exe = self.ctx.props.input_ports.macports.macports_exe
     action._check_file_exists(port_exe, self)
     self.ctx.logger.action("sudo %s" % ([port_exe, "install"]+package_list).__repr__())
Пример #17
0
 def dry_run(self, tomcat_port, app_path, warfile_path,
             admin_password, update=False,
         tag=None):
     _check_file_exists(warfile_path, self)
Пример #18
0
 def dry_run(self, apache_config, timeout_tries=5, time_between_tries=2.0):
     controller_exe = apache_config.controller_exe
     action._check_file_exists(controller_exe, self)
     return False
Пример #19
0
 def run(self, src, dest):
     _check_file_exists(src, self)
     shutil.copyfile(src, dest)
Пример #20
0
 def dry_run(self, package_list):
     _check_file_exists(APT_GET_PATH, self)
Пример #21
0
def is_database_installed(self, psql_exe, database_name, database_user):
    _check_file_exists(psql_exe, self)
    rc = procutils.run_and_log_program([psql_exe, '-d', database_name,
                                        '-U', database_user, '-c', r'\d'], None,
                                       self.ctx.logger, os.path.dirname(psql_exe))
    return rc==0
Пример #22
0
 def dry_run(self, apache_config):
     controller_exe = apache_config.controller_exe
     action._check_file_exists(controller_exe, self)
     self.ctx.logger.debug("%s stop" % controller_exe)