无法 运行 GCP 打包程序实例上的 serverspec 测试

Unable to run serverspec test on GCP packer instance

我正在努力 运行 从本地机器对 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 配置为 运行 从特定文件夹进行测试。

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

失败

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

Packer 构建日志

==> 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 测试。

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

您似乎忘记了 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"
}