在容器内使用fabric来部署应用程序

时间:2017-03-31 09:48:54

标签: jenkins ssh fabric

我正在使用Jenkins设置,其中每个slave都是一个ephemera docker容器,Jenkins用户通过ssh连接连接到容器/ slave。在Jenkins管道的末尾,如果所有测试和分析都很好,则使用结构脚本将应用程序部署到临时环境。

Fabfile:

@task
def deploy():
    require('context', provided_by=(stage,prod,pprod))

    #figure out the package and file names and version
    file_name = [f for f in os.listdir(".") if fnmatch.fnmatch(f, '*.tar.gz')][0]
    package_name = file_name.split('.')[0]#os.path.splitext(fullname)[0]

    setup_ssh_agent()

    # upload the package to the temporary folder on the server
    with cd(env.release_dir):
      put('{}'.format(file_name), '.')   

def setup_ssh_agent():
  local('eval "$(ssh-agent -s)" && ssh-add /home/jenkins/.ssh/id_rsa')

我收到此错误:

fab stage deploy
Agent pid 808
Identity added: /home/jenkins/.ssh/id_rsa (rsa w/o comment)
/usr/lib/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
  passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
[stage.mycompany.com] Login password for 'jenkins': [stage.mycompany.com] Executing task 'deploy'
[localhost] local: eval "$(ssh-agent -s)" && ssh-add /home/jenkins/.ssh/id_rsa
*****upload the package to the releases directory on the provision server*****
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/fabric/main.py", line 756, in main
    *args, **kwargs
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 386, in execute
    multiprocessing
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 276, in _execute
    return task.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/fabric/tasks.py", line 173, in run
    return self.wrapped(*args, **kwargs)
  File "/home/jenkins/workspace/nt-release-pipeline-eokouya-5KGNK7CKO2VEM6GERMMTBRTMUZI3G3LDQB4ZKJF7JVTBVSTTW22Q/fabfile.py", line 72, in deploy
    put('{}'.format(file_name), '.')
  File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 683, in host_prompting_wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/fabric/operations.py", line 346, in put
    ftp = SFTP(env.host_string)
  File "/usr/local/lib/python2.7/dist-packages/fabric/sftp.py", line 33, in __init__
    self.ftp = connections[host_string].open_sftp()
  File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 159, in __getitem__
    self.connect(key)
  File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 151, in connect
    user, host, port, cache=self, seek_gateway=seek_gateway)
  File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 565, in connect
    password = prompt_for_password(text)
  File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 648, in prompt_for_password
    new_password = _password_prompt(password_prompt, stream)
  File "/usr/local/lib/python2.7/dist-packages/fabric/network.py", line 620, in _password_prompt
    return getpass.getpass(prompt.encode('ascii', 'ignore'), stream)
  File "/usr/lib/python2.7/getpass.py", line 83, in unix_getpass
    passwd = fallback_getpass(prompt, stream)
  File "/usr/lib/python2.7/getpass.py", line 118, in fallback_getpass
    return _raw_input(prompt, stream)
  File "/usr/lib/python2.7/getpass.py", line 135, in _raw_input
    raise EOFError
EOFError

ssh身份在登台服务器上被授权,我已经在我的Linux PC上使用该身份测试了与该计算机的ssh连接并且它可以工作,我必须使用该ssh密钥从我的机器运行该脚本并且它也能正常工作

如果有人遇到这个问题,我很想知道你是怎么解决的。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

检查您要部署的服务器是否具有jenkins用户ssh密钥。

看起来函数setup_ssh_agent()没有完成它的工作 jenkins用户没有.ssh / id_rsa .ssh / id_rsa / pub文件的可能原因

然后你必须在本地机器上为用户jenkins

创建它们

sudo su jenkins

cd

mkdir .ssh

cd .ssh

ssh-keygen -t rsa

将id_rsa.pub复制到〜/ .ssh / authorized_keys到目标服务器