packer image failed during script - windows

I'm trying to build a windows based AMI on AWS with packer and providing the image with powershell script.
For some reason, the build is terminated in the middle of the script and I can't figure the cause since the script is evaluated to run manually.
Here's the part of my PS script where the build ends:
function DownloadFile([Object[]] $sourceFiles, [string]$targetDirectory) {
$wc = New-Object System.Net.WebClient
foreach ($sourceFile in $sourceFiles) {
$sourceFileName = $sourceFile.SubString($sourceFile.LastIndexOf('/') + 1)
$targetFileName = $targetDirectory + $sourceFileName
$wc.DownloadFile($sourceFile, $targetFileName)
Write-Host "Downloaded $sourceFile to file location $targetFileName"
}}
The files are downloaded and then the build stops with the following error:
amazon-ebs: Downloaded File01 to file location D:\file1.exe
amazon-ebs: Downloaded file1 to file location D:\File2.exe
amazon-ebs: Downloaded file3 to file location D:\File3.exe
==> amazon-ebs: Stopping the source instance...
amazon-ebs: Stopping instance, attempt 1
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Deregistered AMI PACKER-DEMO-tls, id: ami-3e5a3e51
What can be the cause for the build to stop?

Related

Packer error with build from vmx file - "file was not found"

I have a problem that I can't figure out. I'm trying to make my first clone of a VM from Packer (Hashicorp) here is my HCL :
source "vmware-vmx" "basic-example" {
source_path = "D:/HashiCorp/Packer1.8.0/Project/WindowsServer2022.vmx"
vm_name = "Test1"
communicator = "winrm"
winrm_host = "redteam"
winrm_username = "admin"
winrm_password = "Disc0very"
shutdown_command = "shutdown /s"
}
build {
sources = ["sources.vmware-vmx.basic-example"]
}
and when I run my build command I get this result :
D:\HashiCorp\Packer1.8.0\Project>packer build packer_test_win2022CARL.pkr.hcl
vmware-vmx.basic-example: output will be in this color.
==> vmware-vmx.basic-example: Configuring output and export directories...
==> vmware-vmx.basic-example: Creating required virtual machine disks
==> vmware-vmx.basic-example: Cloning source VM...
==> vmware-vmx.basic-example: Deleting output directory...
Build 'vmware-vmx.basic-example' errored after 521 milliseconds 605 microseconds: VMware error: Error: A file was not found
==> Wait completed after 521 milliseconds 605 microseconds
==> Some builds didn't complete successfully and had errors:
--> vmware-vmx.basic-example: VMware error: Error: A file was not found
==> Builds finished but no artifacts were created.
Solved, not enough to point the VMX file, VMware needs other files such as .vmdk, .vmxf etc ... to proceed with the cloning

Ansible with Windows node - How to create a directory

I need to create C:\MSI folder to put there msi files. Here is my task:
tasks:
- name: Copy *.msi files from ./MSI to C:\MSI
file: path=C:\MSI state=directory
But I got the error:
TASK [Copy *.msi files from ./MSI to C:\MSI] ***********************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: + ~~~~~~~~~~~~~~~
fatal: [agentsmith]: FAILED! => {"changed": false, "failed": true, "msg": "The term '/usr/bin/python' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."}
and in verbose mode I see that File module for some reason puts /usr/bin/python to the Powershell script:
TASK [Copy *.msi files from ./MSI to C:\MSI] ***********************************
task path: /home/qaexpert/ansible-lab/tcagent.yml:8
<agentsmith> ESTABLISH WINRM CONNECTION FOR USER: Administrator on PORT 5986 TO agentsmith
<agentsmith> EXEC Set-StrictMode -Version Latest
(New-Item -Type Directory -Path $env:temp -Name "ansible-tmp-1477410445.62-187863101456896").FullName | Write-Host -Separator '';
<agentsmith> PUT "/tmp/tmpqOJYen" TO "C:\Users\Administrator\AppData\Local\Temp\ansible-tmp-1477410445.62-187863101456896\file.ps1"
<agentsmith> EXEC Set-StrictMode -Version Latest
Try
{
/usr/bin/python 'C:\Users\Administrator\AppData\Local\Temp\ansible-tmp-1477410445.62-187863101456896\file.ps1'
}
Catch
...
Ansible looks for /usr/bin/python because the file module needs Python installed on the target system. It is not possible to use normal Ansible modules for Windows target.
Look at the Ansible Windows documentation for details. On Windows hosts only modules listed in the “windows” subcategory of the Ansible module index are available.
To replace the regular file module use the win_file module.

Packer Error: "amazon-ebs: Error querying AMI: AuthFailure"

I'm trying to create the packer build seen here: https://www.packer.io/intro/getting-started/build-image.html
PS C:\dev\tutorials\packer> packer build -var 'aws_access_key=AKI---------' -var 'aws_secret_key==+---------------------------------------' example.json
amazon-ebs output will be in this color.
==> amazon-ebs: Prevalidating AMI Name...
==> amazon-ebs: Error querying AMI: AuthFailure: AWS was not able to validate the provided access credentials
==> amazon-ebs: status code: 401, request id: []
Build 'amazon-ebs' errored: Error querying AMI: AuthFailure: AWS was not able to validate the provided access credentials
status code: 401, request id: []
==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Error querying AMI: AuthFailure: AWS was not able to validate the provided access credentials
status code: 401, request id: []
==> Builds finished but no artifacts were created.
I've tried giving my user the AmazonEC2FullAccess policy in AWS.
Is my command correct?
I am on windows 8, using powershell.
Thank you for your help #MathiasR.Jessen.
The credentials come in a credentials.csv file. I opened this file with Excel, clicked column C2 and copied the value there. This is an issue because excel prepends an equals sign at the beginning of the value box :/

Unable to set jdoconfig.xml and appname.properties templates for the war file deployed in tomcat7/webapps

Recipe:
include_recipe "lgjava"
include_recipe "lgtomcat"
remote_file "/usr/local/tomcat7/webapps/web.war" do #this file places web.war in the specified path
source "http://path/to/web.war"
action :create_if_missing
end
template "jdoconfig.xml" do
source "jdoconfig.xml.erb"
path "/usr/local/tomcat7/webapps/web/WEB-INF/classes/META-INF/jdoconfig.xml"
action :create_if_missing
mode "0755"
end
template "appname.properties" do
source "appname.properties.erb"
path "/usr/local/tomcat7/webapps/web/WEB-INF/classes/appname.properties"
action :create_if_missing
mode "0755"
end
Execution:
Recipe: lgwebapp::default
* remote_file[/usr/local/tomcat7/webapps/web.war] action create_if_missing
- create new file /usr/local/tomcat7/webapps/web.war
- update content in file /usr/local/tomcat7/webapps/web.war from none to ac4b77
(file sizes exceed 10000000 bytes, diff output suppressed)
- restore selinux security context
* template[jdoconfig.xml] action create_if_missing
* Parent directory /usr/local/tomcat7/webapps/web/WEB-INF/classes/META-INF does not exist.
================================================================================
Error executing action `create_if_missing` on resource 'template[jdoconfig.xml]'
================================================================================
Chef::Exceptions::EnclosingDirectoryDoesNotExist
------------------------------------------------
Parent directory /usr/local/tomcat7/webapps/web/WEB-INF/classes/META-INF does not exist.
Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/lgwebapp/recipes/default.rb
20: template "jdoconfig.xml" do
21: source "jdoconfig.xml.erb"
22: path "/usr/local/tomcat7/webapps/web/WEB-INF/classes/META-INF/jdoconfig.xml"
23: action :create_if_missing
24: mode "0755"
25: end
26:
Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/lgwebapp/recipes/default.rb:20:in `from_file'
template("jdoconfig.xml") do
provider Chef::Provider::Template
action [:create_if_missing]
retries 0
retry_delay 2
guard_interpreter :default
path "/usr/local/tomcat7/webapps/web/WEB-INF/classes/META-INF/jdoconfig.xml"
backup 5
atomic_update true
source "jdoconfig.xml.erb"
cookbook_name "lgwebapp"
recipe_name "default"
mode "0755"
end
Recipe: lgtomcat::default
* service[tomcat7] action restart
- restart service service[tomcat7]
Running handlers:
[2015-01-14T10:38:44+00:00] ERROR: Running exception handlers
Running handlers complete
[2015-01-14T10:38:44+00:00] ERROR: Exception handlers complete
[2015-01-14T10:38:44+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 6 resources updated in 6.532117395 seconds
[2015-01-14T10:38:44+00:00] ERROR: template[jdoconfig.xml] (lgwebapp::default line 20) had an error: Chef::Exceptions::EnclosingDirectoryDoesNotExist: Parent directory /usr/local/tomcat7/webapps/web/WEB-INF/classes/META-INF does not exist.
[2015-01-14T10:38:44+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
P.S: the web.war file is deployed under tomcat7/webapps and its extracted too. I manually checked it, the file exists in the specific location. Why is that it's not able to locate using 'templates'?
Take it as stream:
Chef deploys the war then renders the template and try to compare with existing file.
The problem is that when chef tries to get the target file info, Tomcat probably has not yet unpacked the war file at all.
We do this kind of deployment with an ark resource out of tomcat, render the templates and then copy or link (depending on the cases) the temp deploy dir to the webapps tomcat dir.
The below code solved my problem:
ruby_block 'wait for tomcat' do
block do
true until ::File.exists?("#{node['webapp']['webinf_dir']}")
end
end

Recipe fails on crete_if_missing

I am following this tutorial: http://www.gotealeaf.com/blog/chef-basics-for-rails-developers , where they have us making our own recipe. The code bellow is the recipe. The problem is with the block of code that starts with cookbook_file "id_rsa" and ends right before the, # Add Github as known host, comment. I was able to get past the cookbook_file "id_rsa" block and the cookbook_file "id_rsa.pub" block by moving my id_rsa and id_rsa.pub files into the rails-stack/files/default/ directory, but now it breaks when it attempts the sudo_without_password block. Surprisingly, if I provision vagrant after every error thrown by the action: create_if_missing blocks the configuration gets as far as the cookbooks_file "authorization keys" block but it gets stuck there; even after provisioning when I get the error the first time. Any ideas about what is happening? Please be as descriptive as you can, I am relatively new to devops and only know a few of the ins and outs of vagrant and chef. Thanks in advance!
execute "apt-get update" do
command "apt-get update"
end
# OS Dendencies
%w(git ruby-dev build-essential libsqlite3-dev libssl-dev).each do |pkg|
package pkg
end
# Deployer user, sudoer and with known RSA keys
user_account 'deployer' do
create_group true
end
group "sudo" do
action :modify
members "deployer"
append true
end
cookbook_file "id_rsa" do
source "id_rsa"
path "/home/deployer/.ssh/id_rsa"
group "deployer"
owner "deployer"
mode 0600
action :create_if_missing
end
cookbook_file "id_rsa.pub" do
source "id_rsa.pub"
path "/home/deployer/.ssh/id_rsa.pub"
group "deployer"
owner "deployer"
mode 0644
action :create_if_missing
end
# Allow sudo command without password for sudoers
cookbook_file "sudo_without_password" do
source "sudo_without_password"
path "/etc/sudoers.d/sudo_without_password"
group "root"
owner "root"
mode 0440
action :create_if_missing
end
# Authorize yourself to connect to server
cookbook_file "authorized_keys" do
source "authorized_keys"
path "/home/deployer/.ssh/authorized_keys"
group "deployer"
owner "deployer"
mode 0600
action :create
end
# Add Github as known host
ssh_known_hosts_entry 'github.com'
# Install Ruby Version
include_recipe 'ruby_build'
ruby_build_ruby '2.1.2'
link "/usr/bin/ruby" do
to "/usr/local/ruby/2.1.2/bin/ruby"
end
gem_package 'bundler' do
options '--no-ri --no-rdoc'
end
# Install Rails Application
include_recipe "runit"
application 'capistrano-first-steps' do
owner 'deployer'
group 'deployer'
path '/var/www/capistrano-first-steps'
repository 'git#github.com:gotealeaf/capistrano-first-steps.git'
rails do
bundler true
database do
adapter "sqlite3"
database "db/production.sqlite3"
end
end
unicorn do
worker_processes 2
end
end
****EDIT*******
Since writing the question the first time, I've commented out the sudo_without_password block and was able to find a work around by adding
ssh_keygen true
to the user_account 'deployer' block.
I also put an empty authorized_keys file in rails-stack/files/default/ and that helps the cookbook_file 'authorized_keys' block run without errors.
Now I get this error when vagrant/chef tries to pull the example repo
==> default: [2014-12-04T22:44:18+00:00] ERROR: deploy_revision[capistrano-first-steps] (/tmp/vagrant-chef-3/chef-solo-2/cookbooks/application/providers/default.rb line 123) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '128'
==> default: ---- Begin output of git ls-remote "git#github.com:gotealeaf/capistrano-first-steps.git" "HEAD" ----
==> default: STDOUT:
==> default: STDERR: Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts.
==> default: Permission denied (publickey).
==> default: fatal: Could not read from remote repository.
==> default:
==> default: Please make sure you have the correct access rights
==> default: and the repository exists.
==> default: ---- End output of git ls-remote "git#github.com:gotealeaf/capistrano-first-steps.git" "HEAD" ----
==> default: Ran git ls-remote "git#github.com:gotealeaf/capistrano-first-steps.git" "HEAD" returned 128
==> default: [2014-12-04T22:44:18+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
The answer is simple, after I remembered I had a similar issue with puppet. For some reason, not sure why using
git#github.com:gotealeaf/capistrano-first-steps.git
Does not sit well with vagrant/chef/puppet. So, what I did was change the above line to
https://github.com/gotealeaf/capistrano-first-steps
and that did it, my config of the box worked and there were no problems!
You probably have to point the application resource to the private key that will be used used to clone the repo.
application 'capistrano-first-steps' do
...
deploy_key lazy { File.read("/home/deployer/.ssh/id_rsa") }
...
end
More info -- https://supermarket.chef.io/cookbooks/application

Resources