首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Fabric进行Python部署

使用Fabric进行Python部署
EN

Stack Overflow用户
提问于 2014-03-04 19:39:50
回答 1查看 1.2K关注 0票数 2

到目前为止,我正在学习使用FabricBoto,我已经成功地登录并创建了一个新实例。现在如何在下面使用Python在同一个fab文件中新建的服务器上安装Gunicorn?

也就是说,我希望从这个文件中获得新的实例和pip安装。

这是我的fab.py文件,到目前为止我拥有的是:

代码语言:javascript
复制
SERVER_TYPES = {
                'web' : {
                       'image_id' : 'ami-xxxxxx',
                       'instance_type' : 't1.micro',
                       'security_groups' : [MAIN_SG],
                       'key_name' : MAIN_KP,
                        },
}



class EC2Conn:
    def __init__(self):
        print(_green("Started..."))
        self.ec2conn = None
        self.user = 'fabUser'
        self.access_key = 'xxxxxx'
        self.secret_key = 'xxxxxxxxxxxx'

    def connect(self):
        print(_green("Connecting..."))

        region = ec2.get_region('eu-west-1')
        self.ec2conn = ec2.connection.EC2Connection(self.access_key,
                          self.secret_key, region=region)


    def get_instances(self):
        return self.ec2conn.get_all_instances()


    def create_instance(self, instance_type='web', address=None):
                reservation = self.ec2conn.run_instances( **SERVER_TYPES[instance_type])
                print reservation
                instance = reservation.instances[0]
                time.sleep(10)
                while instance.state != 'running':
                        time.sleep(5)
                        instance.update()
                        print "Instance state: %s" % (instance.state)

                print "instance %s done!" % (instance.id)



def create_instance():
    a = EC2Conn()
    a.connect()
    return a.create_instance()

所以我想要的是这样的东西:

代码语言:javascript
复制
def install_stuff(using self.ec2conn)
      using ec2 instance
      run('pip install gunicorn')

   etc
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-04 20:59:55

在过去我不得不做一些类似的事情。我把我认为重要的部分结合起来了。一旦安装成功,您就可以通过传递主机参数的列表在主机或主机组上运行fabric命令。希望这能有所帮助

代码语言:javascript
复制
from fabric.api import *
import socket
socket.setdefaulttimeout(5)

# Setup fabric details
env.user         = 'fabric-user'
env.key_filename = '/root/.ssh/fabric_rsa'

def execute_remote(command):
    return run(command)

@parallel
def execute_remote_parallel(command):
    return run(command)

def run_fabric(cmd,hosts,in_parallel):
    """ Check the parameters and call the corresponding fabric block"""
    if in_parallel:
        return execute(execute_remote_parallel,command=cmd,hosts=hosts)
    else:
        return execute(execute_remote,command=cmd,hosts=hosts)

class FabricFunctions:    

    def stop_service(self,hosts,in_parallel,servicename):
        cmd = "sudo /sbin/service %s stop" % servicename
        run_fabric(cmd, hosts, in_parallel)

    def start_service(self,hosts,in_parallel,servicename):
        cmd = "sudo /sbin/service %s start" % servicename
        run_fabric(cmd, hosts, in_parallel)


#
#   Example - Untested!
#
f = FabricFunctions()
f.start_service("ec2-instance-ip",False,"httpd")

将新方法添加到fabric functions类以运行pip命令应该非常简单。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22181352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档