Skip to main content
 首页 » 编程设计

使用 Fabric 进行 Python 部署

2024年11月01日9JustinYoung

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

即我希望从该文件中获取新实例和 pip 安装。

这是我的 fab.py 文件以及我目前拥有的:

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() 

所以我在寻找这样的东西:

def install_stuff(using self.ec2conn) 
      using ec2 instance 
      run('pip install gunicorn') 
 
   etc 

请您参考如下方法:

过去我不得不做类似的事情。我结合了我认为重要的部分。设置完成后,您可以通过传递主机参数列表在主机或主机组上运行结构命令。希望这有帮助

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") 

向结构函数类添加新方法以运行 pip 命令应该非常简单