Chef fails when trying to deploy a Torquebox app on EC2 - amazon-ec2

I'm pretty new to Chef/ops in general but I'm trying to deploy on EC2 and I keep running into this error. The stack trace file mentioned in the error does not exist so that's making it kind of hard to debug the problem. Also, whenever Chef fails, the permission on my EC2 box gets denied and I have to spin up a new instance. For reference, the original source of this file is from here: http://janitor.se/blog/2013/07/04/easier-neo4j-dot-rb-deployments-with-chef-plus-capistrano-plus-torquebox/
The error looks like this:
* script[install torquebox backstage] action run
- execute "bash" "/tmp/chef-script20131011-2067-1phzfkw"
[2013-10-11T03:47:51+00:00] ERROR: Running exception handlers
[2013-10-11T03:47:51+00:00] ERROR: Exception handlers complete
[2013-10-11T03:47:51+00:00] FATAL: Stacktrace dumped to /tmp/chef-solo/chef-stacktrace.out
Chef Client failed. 71 resources updated
[2013-10-11T03:47:51+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
ERROR: RuntimeError: chef-solo failed. See output above.
My recipe file - torquebox.rb:
download_file = File.join("/tmp", File.basename(node.torquebox.download_url))
remote_file download_file do
source node.torquebox.download_url
mode 00644
action :create_if_missing
notifies :run, "script[install-torquebox]", :immediately
end
script "install-torquebox" do
Chef::Log.info("Installing torquebox zip file...")
user 'torquebox'
interpreter "bash"
code <<-EOH
unzip -o #{download_file} -d /opt/torquebox/
ln -s /opt/torquebox/torquebox-2.3.2 /opt/torquebox/current
EOH
notifies :run, "execute[change ownership to torquebox]"
not_if do
File.exists? "/opt/torquebox/torquebox-2.3.2"
end
# not_if do
# File.exists? "/opt/torquebox/torquebox-2.2.0"
# end
#action :nothing
end
template "/etc/profile.d/torquebox.sh" do
source "torquebox.sh.erb"
mode 00644
owner 'root'
variables(
:jboss_user => node[:torquebox_env][:jboss_user],
:torquebox_home => node[:torquebox_env][:home],
:jboss_pidfile => node[:torquebox_env][:jboss_pidfile],
:jboss_console_log => node[:torquebox_env][:jboss_console_log],
:jboss_config => node[:torquebox][:configuration_file],
:jruby_opts => node[:torquebox_env][:jruby_opts],
:java_environment_file => node[:java][:java_environment]
)
notifies :restart, "service[jboss-as-standalone]"
end
# install torquebox backstage
#
execute "change ownership to torquebox" do
user "root"
cwd "/opt"
Chef::Log.info("changing ownership for torquebox")
command "chown -Rv 1000.1000 /opt/torquebox"
notifies :run, "script[install torquebox backstage]"
action :nothing
end
script "install torquebox backstage" do
Chef::Log.info("Installing torquebox backstage file...")
interpreter "bash"
user "torquebox"
cwd "/opt/torquebox"
code <<-EOH
export TORQUEBOX_HOME=/opt/torquebox/torquebox-2.3.2
export JAVA_HOME=/opt/jdk7/
export JBOSS_HOME=$TORQUEBOX_HOME/jboss
export JRUBY_HOME=$TORQUEBOX_HOME/jruby
export PATH=$JBOSS_HOME/bin:$JRUBY_HOME/bin:$JAVA_HOME/bin:$PATH
EOH
# jruby -S gem install torquebox-backstage
# jruby -S gem install ruby-shadow
# jruby -S backstage deploy
not_if do
File.exists? "/opt/torquebox/current/jruby/bin/backstage"
end
end
directory '/opt/apps/' do
owner "torquebox"
group "torqubox"
mode 00755
action :create
not_if do
File.exists? "/opt/apps/"
end
end
directory "/etc/jboss-as" do
owner "root"
group "root"
mode 00755
action :create
not_if do
File.exists? "/etc/jboss-as"
end
end
directory "/var/log/jboss-as" do
owner "torquebox"
group "torquebox"
mode 00755
action :create
not_if do
File.exists? "/var/log/jboss-as"
end
end
template "/etc/init.d/jboss-as-standalone" do
source "jboss-as-standalone.sh.erb"
variables(:environment_file => node[:torquebox][:environment_file])
mode 00755
owner 'root'
notifies :restart, "service[jboss-as-standalone]"
end
service "jboss-as-standalone" do
supports :status => true, :restart => true, :stop => true, :start => true
action [ :enable, :start ]
end
#if clustered then if cluster name is staging_cluster, so proxy name will be staging_cluster_proxy
#so for every cookbook, it should be paired with two of them
proxy_nodes = []
clustered_nodes = []
Chef::Log.warn("clustered status #{node[:torquebox][:clustered]}")
if node[:torquebox][:clustered]
clustered_nodes = search(:node, "roles:#{node[:torquebox][:cluster_name]}")
proxy_nodes = search(:node, "roles:#{node[:torquebox][:cluster_name]}_proxy")
template "/opt/torquebox/current/jboss/standalone/configuration/standalone-ha.xml" do
source "standalone-ha.xml.erb"
variables(:node_name => node.name, :node_ipaddress => node.ipaddress, :cluster_name => node[:torquebox][:cluster_name], :clustered_nodes => clustered_nodes, :proxy_nodes => proxy_nodes )
mode "0644"
notifies :restart, "service[jboss-as-standalone]"
end
end
if proxy_nodes.count == 0 and node[:torquebox][:clustered] == true
Chef::Log.warn("There is no proxy defined, cluster may not function")
end
if !node[:torquebox][:clustered]
template "/opt/torquebox/current/jboss/standalone/configuration/standalone.xml" do
source "standalone.xml.erb"
variables(:node_name => node.name, :node_ipaddress => node.ipaddress)
mode "0644"
notifies :restart, "service[jboss-as-standalone]"
end
end
template "/opt/torquebox/current/jboss/bin/standalone.conf" do
source "standalone.conf.erb"
mode "00644"
owner 'torquebox'
variables(:jboss_config => node[:torquebox][:configuration_file])
notifies :restart, "service[jboss-as-standalone]"
end
cookbook_file "/etc/jboss-as/jboss-as.conf" do
source "jboss-as.conf"
mode 00644
owner 'root'
end
# NGINX!
# this enables our site, kinda like a2ensite
execute 'enable-site' do
command "ln -sf /etc/nginx/sites-available/#{node[:server_name]} /etc/nginx/sites-enabled/#{node[:server_name]}"
notifies :restart, 'service[nginx]'
end
# Our configuration template. Take a look at templates/nginx.conf.erb to see what's going on.
template "/etc/nginx/sites-available/#{node[:server_name]}" do
source 'nginx.erb'
owner 'root'
group 'root'
mode 0644
notifies :run, "execute[enable-site]", :immediately
variables(
server_name: node.server_name
)
end
service 'nginx'
service 'jboss-as-standalone'

Related

Chef Error executing action `start` on resource 'service[httpd]'

Here is my very basic recipes/default.rb file;
package "httpd" do
action :install
end
node["apache"]["sites"].each do |sitename, data|
document_root = "/content/sites/#{sitename}"
directory document_root do
mode "0755"
recursive true
end
template "/etc/httpd/conf.d/#{sitename}.conf" do
source "vhost.erb"
mode "0644"
variables(
:document_root => document_root,
:port => data["port"],
:domain => data["domain"]
)
notifies :restart, "service[httpd]"
end
end
service "httpd" do
action [:enable, :start]
end
When I run the chef-client in the node it returns the following error:
Error executing action `start` on resource 'service[httpd]'
================================================================================
Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '1'
---- Begin output of /sbin/service httpd start ----
STDOUT: Starting httpd: [FAILED]
STDERR: Syntax error on line 15 of /etc/httpd/conf.d/stedelahunty2.conf:
order takes one argument, 'allow,deny', 'deny,allow', or 'mutual-failure'
---- End output of /sbin/service httpd start ----
Ran /sbin/service httpd start returned 1
Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/apache/recipes/default.rb
35: service "httpd" do
36: action [:enable, :start]
37: end
Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/apache/recipes/default.rb:35:in `from_file'
service("httpd") do
action [:enable, :start]
supports {:restart=>false, :reload=>false, :status=>true}
retries 0
retry_delay 2
default_guard_interpreter :default
service_name "httpd"
enabled true
pattern "httpd"
declared_type :service
cookbook_name "apache"
recipe_name "default"
end
I've tried renaming it apache, changing the options to ':restart', commenting out entirely but that means httpd fails to start. I just need a simple way to restart the service after the chef run has completed.
Again, apologies for the novice question; I'm very new to coding.
Cheers
That's not a chef problem. Apache httpd reports
Syntax error on line 15 of /etc/httpd/conf.d/stedelahunty2.conf: order takes one argument, 'allow,deny', 'deny,allow', or 'mutual-failure'

Mixlib::ShellOut - timeout

I am trying to use Mixlib::ShellOut to execute commands under ruby_block inside a chef recipe.
In Some situations, we cannot complete the task in 600 seconds, and I would like extend further. I have added command in below way,
ruby_block "#{host_short_name}_reg_chef_node" do
block do
puts "Registering Chef Node #{host_full_name}"
_command = "cd #{node['nodeManager']['app']['base_dir']}; #{node['nodeManager']['knife']['binary']} bootstrap --sudo #{host_full_name}"
_command += " --ssh-user #{node['nodeManager']['admin']['user']} --no-host-key-verify --identity-file #{node['nodeManager']['admin']['keyfile']}"
_command +=" --environment #{params[:environment]} --run-list 'role[#{params[:role_hash]['role']}]'"
puts _command
vsphere_output = Mixlib::ShellOut.new(_command, :timeout => 10000)
vsphere_output.run_command
puts "Output: #{vsphere_output.stdout}"
puts "Error : #{vsphere_output.stderr}"
end
action :nothing
end
and I suspect it is not respecting timeout value. Please advise.

Error occured while running task Cap deploy:setup During deployment using Capistrano with Unicorn

Here is error which occurs after running task Cap deploy:setup to deploy ubuntu server using Capistrano and Unicorn. Please help me out here if you know the answer?
here is error reflecting on terminal-
[100.116.4.74] sh -c 'cp -RPp /home/deployer/apps/mcash/shared/cached-copy /home/deployer/apps/mcash/releases/20120417074244 && (echo 8f69f0a524dcecef478bad74df4a983d3cdad480 > /home/deployer/apps/mcash/releases/20120417074244/REVISION)'
** [out :: 100.116.4.75] cp: cannot create directory `/home/deployer/apps/mcash/releases/20120417074244'
** [out :: 100.116.4.75] : No such file or directory
failed: "sh -c 'cp -RPp /home/deployer/apps/mcash/shared/cached-copy /home/deployer/apps/mcash/releases/20120417074244 && (echo 8f69f0a524dcecef478bad74df4a983d3cdad480 > /home/deployer/apps/mcash/releases/20120417074244/REVISION)'" on 100.116.4.74
Here is entire deploy.rb file-
require "bundler/capistrano"
role :web, "200.116.4.75"
role :app, "200.116.4.75"
role :db, "200.116.4.75", :primary => true
set :application, "mcasher"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, :git
set :repository, "git-server:mcasher.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end

Using the ruby gem net-ssh-multi to execute a sudo command on multiple servers at once

In a previous question I figured out how to start a password-authenticated ssh sessions on multiple servers to run a single command. Now I need to be able to execute a "sudo" command. The problem is, that net-ssh-multi does not allocate a pseudo terminal (pty), which sudo needs to run, resulting in the following error:
[127.0.0.1 : stderr] sudo: sorry, you must have a tty to run sudo
According to the documentation, a pseudo-terminal can be allocated with a method call to a channel object, however, the following code does not work: it generates the "no tty" error above:
require 'net/ssh'
require 'net/ssh/multi'
Net::SSH::Multi.start do |session|
# define the servers we want to use
my_ticket.servers.each do |session_server|
session.use session_server , :user => user_name , \
:password => user_pass
end
# execute commands on all servers
session.exec 'sudo ls /root' do |channel, stream, data|
if data =~ /^\[sudo\] password for user:/
channel.request_pty # <- problem must be here.
channel.send_data user_pass
end
end
# run the aggregated event loop
session.loop
end
$ ruby --version
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
Can you try something like this:
channel.request_pty do |c, success|
if success
command = "sudo YOUR_COMMAND"
c.exec(command) do |c, success|
# Some processing
end
end
end
In this case 'sudo' is inside.
You need to request a pty before running the command.
session.open_channel do |ch|
ch.request_pty
ch.exec "sudo ls /root"
end
Also you may remove the tty requeriment from /etc/sudoers. To do it run visudo and comment Defaults requiretty
This is what I wound up doing, thanks to #Christian and this wonderful Pastie:
Net::SSH::Multi.start do |session|
# define the servers we want to use
my_ticket.servers.each do |session_server|
session.use session_server , :user => my_ticket.user_name , \
:password => my_ticket.user_pass
end
session.open_channel do |channel|
channel.request_pty do |c, success|
raise "could not request pty" unless success
channel.exec "sudo YOUR_COMMAND"
channel.on_data do |c_, data|
if data = /\[sudo\]/
channel.send_data(#password + "\n")
end
puts data
end
end
end
# run the aggregated event loop
session.loop
end

Executing Password Change over Ruby Net-SSH

I am looking to execute a password change over Net-ssh and this code seems to hang:
Net::SSH.start(server_ip, "user", :verbose => :debug ) do |session|
session.process.popen3("ls") do |input, output, error|
["old_pass","test", "test"].each do |x|
input.puts x
end
end
end
I know the connection works because using a simple exec I can get the output from ls on the remote server, but this hangs.
Any ideas?
The last message from debug is that the public key succeeded.
This one will solve your issue ,, note this script to change password for a list of servers list in file
#~~~~~~~~~~~~~~~~~~~~~~~
# Change Password is a simple script to change the password for a list of servers
# Coded by : Sabry Saleh
# License : GPL2
#~~~~~~~~~~~~~~~~~~~~~~~
#=-Notes-=
# You have to install ruby + net-ssh gems
# sudo gem install net-ssh
#~~~~~~~~~~~~~~~~~~~~~~~
require 'net/ssh'
host = IO.readlines('test1.txt') # full path of servers' list
port = 22 # SSH port
user = 'username' # username
i = 0
while i < host.length
Net::SSH.start(host[i], user , :password => "User pass" , :port=> port) do |ssh|
ssh.open_channel do |channel|
channel.on_request "exit-status" do |channel, data|
$exit_status = data.read_long
end
channel.request_pty do |channel, success|
channel.exec("sudo passwd UserName") # Logged user shuold be root or sudoers memeber
if success
channel.on_data do |channel, data|
puts data.inspect.chomp("\r\n")
channel.send_data("New pass\n") # put the New password you need to set
sleep 0.1
end
else
puts "FAILED!!"
end
end
channel.wait
puts "SUCCESS!!" if $exit_status == 0
end
end
i += 1
end

Resources