-1

我需要管理几台服务器、网络服务、应用服务器(Apache、Tomcat)并管理它们(启动停止、安装软件)。

我想使用 Python,因为 C++ 似乎很复杂,而且对于事物任务的效率较低。我不确定要使用哪个中间件。ActiveMQ 和 Mule 似乎是一个不错的选择,尽管它们是用 Java 编写的。我对 ActiveMQ 了解得更好,但我对 ESB 知之甚少。

有什么建议吗?Python有什么选择吗?

我看到有beantalk,但是太简单和不灵活。我需要一个用于协调的消息系统,以及一种将 tar.gz 文件发送到服务器(软件包)的方法。

我当时有一个 Python 原生的消息传递解决方案。

4

1 回答 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用户在sudo没有 tty 或重新输入密码的情况下运行——这意味着它可以直接在 ssh 上运行而无需进一步输入。sudo这是一个在遥控器上利用的示例 python 命令:

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

无论如何,这不是对您问题的“直接”回答,而是说明您可以轻松地使用 python 和其他几种技术来创建一个 100% 适合您的特定需求的系统管理工具。

顺便说一句,如果任何命令返回的退出状态 > 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)
于 2009-06-29T16:29:14.040 回答