Mule与ActiveMQ在Python中的比较

-1 投票
1 回答
1279 浏览
提问于 2025-04-15 12:34

我需要管理几个服务器、网络服务和应用服务器(比如Apache和Tomcat),还要进行一些管理操作,比如启动、停止和安装软件。

我想用Python来做这些,因为C++看起来太复杂了,做这个任务效率不高。我不太确定该用什么中间件。ActiveMQ和Mule看起来是不错的选择,虽然它们是用Java写的。我对ActiveMQ了解得比较多,而对ESB了解得很少。

有没有什么建议?Python有没有合适的选项?

我看到有Beanstalk,但感觉太简单了,不够灵活。我需要一个消息系统来进行协调,还需要一种方法把tar.gz文件(软件包)发送到服务器上。

我希望能有一个原生支持Python的消息解决方案。

1 个回答

1

一个示例Python脚本,用于管理多个远程服务器上的各种服务:

接下来是一个简单拼凑的脚本,可以用来管理你有SSH访问权限的服务器上的各种服务。

最好先启动一个ssh-agent,不然你会不停地输入密码。

对于那些需要在远程机器上以管理员身份执行的命令,你会看到使用了“sudo”。这意味着你需要在每台远程机器上修改sudoers文件,添加类似这样的条目(假设你的用户名是 deploy):

Defaults:deploy !requiretty
Defaults:deploy !authenticate

deploy ALL=\
    /sbin/service httpd status,\
    /sbin/service httpd configtest,\
    /sbin/service httpd graceful

前两行允许 deploy 用户在没有tty(终端)或重新输入密码的情况下运行 sudo,这意味着可以直接通过ssh运行,而不需要进一步输入。下面是一个示例Python命令,展示如何在远程机器上使用 sudo

CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd graceful'))

总之,这并不是对你问题的“直接”回答,而是一个例子,说明你可以多么轻松地使用Python和其他一些技术,创建一个完全符合你特定需求的系统管理工具。

顺便提一下,下面的脚本会“清楚地告诉你”如果任何命令返回的状态码大于0,这样你就可以自己分析输出。

这个脚本是在我参与的一个项目开始使用负载均衡器时拼凑出来的,那时在每台服务器上运行所有命令已经不再合理。你可以修改或扩展这个脚本,让它与rsync配合使用来部署文件,甚至可以用来更新你在远程服务器上托管的脚本,以“完成工作”。

#!/usr/bin/python


from optparse import OptionParser
import subprocess
import sys

def die(sMessage):
        print
        print sMessage
        print
        sys.exit(2)


###################################################################################################
# Settings

# The user@host: for the SourceURLs (NO TRAILING SLASH)
RemoteUsers = [
        "deploy@ac10.example.com",
        "deploy@ac11.example.com",
        ]

###################################################################################################
# Global Variables

# optparse.Parser instance
Parser                          = None

# optparse.Values instance full of command line options
Opt                             = None

# List of command line arguments
Arg                                     = None

###################################################################################################
Parser = OptionParser(usage="%prog [options] [Command[, Subcommand]]")


Parser.add_option("--interactive",
        dest    = "Interactive",
        action  = "store_true",
        default = False,
        help    = "Ask before doing each operation."
        )

# Parse command line
Opt, Arg = Parser.parse_args()

def HelpAndExit():
        print "This command is used to run commands on the application servers."
        print
        print "Usage:"
        print "  deploy-control [--interactive] Command"
        print
        print "Options:"
        print "  --interactive   ::   will ask before executing each operation"
        print
        print "Servers:"
        for s in RemoteUsers: print "  " + s
        print
        print "Web Server Commands:"
        print "  deploy-control httpd status"
        print "  deploy-control httpd configtest"
        print "  deploy-control httpd graceful"
        print "  deploy-control loadbalancer in"
        print "  deploy-control loadbalancer out"
        print
        print "App Server Commands:"
        print "  deploy-control 6x6server status"
        print "  deploy-control 6x6server stop"
        print "  deploy-control 6x6server start"
        print "  deploy-control 6x6server status"
        print "  deploy-control wb4server stop"
        print "  deploy-control wb4server start"
        print "  deploy-control wb4server restart"
        print "  deploy-control wb4server restart"
        print
        print "System Commands:"
        print "  deploy-control disk usage"
        print "  deploy-control uptime"
        print
        sys.exit(2)

def YesNo(sPrompt):
        while True:
                s = raw_input(sPrompt)
                if s in ('y', 'yes'):
                        return True
                elif s in ('n', 'no'):
                        return False
                else:
                        print "Invalid input!"


# Implicitly verified below in if/else
Command = tuple(Arg)

if Command in (('help',), ()):
        HelpAndExit()


ResultList = []
###################################################################################################
for UH in RemoteUsers:
        print "-"*80
        print "Running %s command on: %s" % (Command, UH)

        if Opt.Interactive and not YesNo("Do you want to run this command? "):
                print "Skipping!"
                print
                continue

        #----------------------------------------------------------------------------------------------
        if Command == ('httpd', 'configtest'):
                CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd configtest'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('httpd', 'graceful'):
                CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd graceful'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('httpd', 'status'):
                CommandResult = subprocess.call(('ssh', UH, 'sudo /sbin/service httpd status'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('loadbalancer', 'in'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/loadbalancer-in'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('loadbalancer', 'out'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/loadbalancer-out'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('disk', 'usage'):
                CommandResult = subprocess.call(('ssh', UH, 'df -h'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('uptime',):
                CommandResult = subprocess.call(('ssh', UH, 'uptime'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('6x6server', 'status'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-status'))
                if CommandResult > 0:
                        print "Servers not running!!!"

        #----------------------------------------------------------------------------------------------
        elif Command == ('6x6server', 'stop'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-stop'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('6x6server', 'start'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-start'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('6x6server', 'restart'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/6x6server-restart'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('wb4server', 'status'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-status'))
                if CommandResult > 0:
                        print "Servers not running!!!"

        #----------------------------------------------------------------------------------------------
        elif Command == ('wb4server', 'stop'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-stop'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('wb4server', 'start'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-start'))

        #----------------------------------------------------------------------------------------------
        elif Command == ('wb4server', 'restart'):
                CommandResult = subprocess.call(('ssh', UH, 'bin-slave/wb4server-restart'))

        #----------------------------------------------------------------------------------------------
        else:
                print
                print "#"*80
                print
                print "Error: invalid command"
                print
                HelpAndExit()

        #----------------------------------------------------------------------------------------------
        ResultList.append(CommandResult)
        print


###################################################################################################
if any(ResultList):
        print "#"*80
        print "#"*80
        print "#"*80
        print
        print "ERRORS FOUND.  SEE ABOVE"
        print
        sys.exit(0)

else:
        print "-"*80
        print
        print "Looks OK!"
        print
        sys.exit(1)

撰写回答