本地和远程的Python Fabric部署

时间:2015-10-22 03:54:01

标签: python fabric

我使用fabric来自动完成我在本地计算机或远程服务器上运行的许多任务。

所有任务都是单独的。但我想要的是有一个新的任务,在本地机器上做一些工作(构建图像,上传)和一些在服务器上工作(重新加载服务器)

如果我手动运行命令,它们可以工作:

fab build_file
fab remote deploy_file

这是一个输出:

> fab remote deploy_file
[localhost] Executing task 'remote'
[docker.me] Executing task 'deploy_file'
[docker.me] run: echo 'reload image'
[docker.me] out: reload image
[docker.me] out:


Done.
Disconnecting from docker.me... done.

但如果我尝试结合本地和远程任务,这就是我得到的:

> fab do_all
[localhost] Executing task 'do_all'
[localhost] local: echo 'make image'
make image
[localhost] local: echo 'upload image'
upload image
[localhost] run: echo 'reload image'

Fatal error: Low level socket error connecting to host localhost on port 22: Connection refused (tried 1 time)

Underlying exception:
    Connection refused

Aborting.

有没有办法在单个Uber任务中执行本地和远程任务?

我的fabfile.py:

from fabric.api import *

env.run = local
env.hosts = ['localhost']

@task
def remote():
    env.run = run
    env.hosts = ["docker.me"]

@task
def build_file():
    env.run("echo 'make image'")
    env.run("echo 'upload image'")


@task
def deploy_file():
    env.run("echo 'reload image'")


@task
def do_all():
    build_file()
    remote()
    deploy_file()

1 个答案:

答案 0 :(得分:2)

我可能会忽略这一点,但为什么不简单地做这样的事情

...
@interface MySignInClass <GIDSignInDelegate>
...
-(void) signInWithGoogle
{
    GIDSignIn *signIn = [GIDSignIn sharedInstance];
    signIn.clientID = MY_GOOGLE_CLIENT_ID;
    signIn.shouldFetchBasicProfile = YES;
    signIn.scopes = [NSArray arrayWithObjects:@"https://www.googleapis.com/auth/userinfo.profile", @"openid", nil];
    signIn.delegate = self;
    if([signIn hasAuthInKeychain]) {
        [signIn signInSilently];
    } else {
        [signIn signIn];
    }
}
...
- (void)signIn:(GIDSignIn *)signIn 
    didSignInForUser:(GIDGoogleUser *)user
           withError:(NSError *)error 
{
    if (error != nil) {
        [self handleSignInError:error]; // Handle error
    }
    else {
        NSString *idToken = user.authentication.idToken;
        NSDictionary* logins = @{@"accounts.google.com": idToken};
        self.credentialsProvider = [[AWSCognitoCredentialsProvider alloc] 
           initWithRegionType:MY_COGNITO_REGION_TYPE
                   identityId:nil
               identityPoolId:MY_COGNITO_IDENTITY_POOL
                       logins:logins];
       AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] 
            initWithRegion:MY_COGNITO_REGION                                                              
       credentialsProvider:self.credentialsProvider];
       AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
       // AWSTask stuff removed for simplicity
       AWSTask* task = [self.credentialsProvider getIdentityId];
       ...
    }
}
...
- (void)signIn:(GIDSignIn *)signIn
     didDisconnectWithUser:(GIDGoogleUser *)user
                 withError:(NSError *)error 
{
    [self handleGoogleSignout]; // Do signout stuff
}
...

编辑:

我明白了。你的代码基本上没问题。只有一个细节。使用以下代码,您应该没问题:

from fabric.api import *

env.hosts = ['docker.me']

@task
def all():
    local("echo 'make image'")
    local("echo 'upload image'")
    run("echo 'reload image'")
相关问题