0

我正在努力从本地机器上对 GCP 打包程序实例运行 serverspec 测试。我创建了一个打包器 json 配置文件,如下所示在 GCP 上创建一个图像:

{
"variables": {
    "project": "gcp-project",
    "image_family": "centos-7",
    "username": "centos",
    "zone": "us-central1-c",
    "version": "latest"
},

"builders": [
    {
        "type": "googlecompute",
        "account_file": "account.json",
        "project_id": "{{user `project`}}",
        "zone": "{{user `zone`}}",
        "source_image":"centos-7-v20171025",
        "image_name": "sftp-{{user `image_family`}}-{{user `version`}}-{{timestamp}}",
        "image_family": "{{user `image_family`}}",
        "image_description": "sftp - from packer",
        "ssh_username": "{{user `username`}}",
        "machine_type": "g1-small"
    }
],

"provisioners": [
    {
        "type": "shell-local",
        "command": "rake spec TARGET_HOST=remotehost"
    }
]

}

我正在执行rake spec TARGET_HOST=(ip of packer instance)从本地机器运行 serverspec 测试,并且 spec_helper.rb 配置了 ssh 登录,如下所示:

host = ENV['TARGET_HOST']
options = Net::SSH::Config.for(host)
options[:user] = 'centos'
set :host,        options[:host_name] || host
set :ssh_options, options

并且 Rakefile 配置为从特定文件夹运行测试。

运行打包程序构建命令后packer build -var-file=variables.json sftp.json| tee build.log

它失败了

Net::SSH::AuthenticationFailed:
Authentication failed for user centos@1.2.3.4

打包程序构建日志

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Using image: centos-7-v20171025
==> googlecompute: Creating instance...
    googlecompute: Loading zone: us-central1-c
    googlecompute: Loading machine type: g1-small
    googlecompute: Loading network: default
    googlecompute: Requesting instance creation...
    googlecompute: Waiting for creation operation to complete...
    googlecompute: Instance has been created!
==> googlecompute: Waiting for the instance to become running...
    googlecompute: IP: 1.2.3.4
==> googlecompute: Waiting for SSH to become available...
==> googlecompute: Connected to SSH!
==> googlecompute: Executing local command: rake spec TARGET_HOST=1.2.3.4
    googlecompute: An error occurred while loading ./spec/1.2.3.4/sftp_spec.rb.
    googlecompute: On host '1.2.3.4'
    googlecompute: Failure/Error:
    googlecompute:   describe service('sshd'), :if => os[:family] == 'redhat' do
    googlecompute:     it { should be_enabled }
    googlecompute:     it { should be_running }
    googlecompute:   end
    googlecompute: Net::SSH::AuthenticationFailed:
    googlecompute:   Authentication failed for user centos@1.2.3.4

由于此错误,我无法在远程打包程序实例上使用 ssh 从本地计算机运行 serverspec 测试。

任何答案将不胜感激。非常感谢。

4

1 回答 1

0

您似乎缺少centos用户的密码。

将此添加到spec_helper.rb

options[:password] = ENV['LOGIN_PASSWORD']

并将您的调整shell-local为:

{
    "type": "shell-local",
    "command": "LOGIN_PASSWORD='{{ user `sftp_password` }}' rake spec TARGET_HOST=remotehost"
}
于 2017-11-15T07:28:33.187 回答