【问题标题】:Using fabric inside a container to deploy an application使用容器内的结构部署应用程序
【发布时间】:2017-07-18 21:20:51
【问题描述】:

我正在使用 Jenkins 设置,其中每个从站都是一个临时 docker 容器,Jenkins 用户通过 ssh 连接连接到容器/从站。在 Jenkins 流水线结束时,如果所有测试和分析都正常,则使用结构脚本将应用部署到暂存环境。

Fab 文件:

@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 电脑上使用该身份测试了与该机器的 ssh 连接并且它可以工作,我必须使用该 ssh 密钥从我的机器运行该脚本并且它也可以工作.

如果有人遇到过这个问题,我很想知道您是如何解决的。 提前致谢。

【问题讨论】:

    标签: jenkins ssh fabric


    【解决方案1】:

    检查你要部署的服务器是否有 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 到目标服务器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 2023-03-15
      • 2021-11-24
      • 2018-11-07
      • 2017-02-19
      • 1970-01-01
      • 2014-11-14
      相关资源
      最近更新 更多