2017-11-14 15 views
0

ローカルマシンからGCPパッカーインスタンスでserverspec testを実行するのが苦労しています。 GCP上の画像を作成するために、次のように私は、パッカーのJSON設定ファイルを作成しました:GCPパッカーインスタンスでserverspec testを実行できません

{ 
"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" 
    } 
] 

}

と私は、SSHを使用して構成されているローカルマシンとspec_helper.rbからserverspecのテストを実行するためにrake spec TARGET_HOST=(ip of packer instance)を実行しています

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

それが原因私はSSHを使用して、ローカルマシンからserverspecテストを実行することができません。このエラーに

==> 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 [email protected] 

をログビルド

Net::SSH::AuthenticationFailed: 
Authentication failed for user [email protected] 

パッカーで失敗しますリモートパッカーインスタンス上にあります。

任意の回答があります。どうもありがとう。

+0

これはserverspec Rakefile/spec_helper相互作用のベストプラクティスから厄介偏差の一種であるが、しかし、その答えとして、これは純粋に 'net-ssh'宝石の使用に伴う問題であり、packer、serverspec、rspec、またはgcpとは無関係です。この問題を解決するには、sshキーまたはパスワードを使用してnet-ssh引数に追加します。 –

+0

@MattSchuchard私は、serverpec Rakefile/spec_helperのやりとりから逸脱しているわけではありません。鍵のパスを有効な 'private_key_file'へのパスで〜/ .ssh/configファイルに設定していませんでした。とにかく、サーバ仕様ガイドライン –

答えて

0

centosユーザーのパスワードが不足しているようです。

spec_helper.rbにこれを追加します。

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

をそして、あなたのshell-localにajust:

{ 
    "type": "shell-local", 
    "command": "LOGIN_PASSWORD='{{ user `sftp_password` }}' rake spec TARGET_HOST=remotehost" 
} 
+0

CentOSと最近のDebianイメージにはデフォルトでroot sshアクセスが無効になっているので、 'centos'ユーザは' ssh_username'として作成されているのでパスワードを持っていません。パッカーのドキュメントからssh_usernameを任意のユーザーに設定します。これは、パッカーがsudoアクセスで作成するものです。 https://www.packer.io/docs/builders/googlecompute.html –

+1

そして、serverspecがどのように認証されると思いますか?次に、既知の 'ssh_private_key_file'を使う必要があります。https://www.packer.io/docs/templates/communicator.html#ssh_private_key_fileとhttps://devops.stackexchange.com/a/1349 –

+0

ありがとうございますあなたが正しく、 'ssh_private_key_file'が見つからず、'〜/ .ssh/config'ファイルに有効な 'private_key_file'へのパスを持つエントリを作成しました –

関連する問題