Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"postrestoredb.rb" updates schema of MSSQL databases from data bag settings after they have been refreshed
#
# Cookbook Name:: smashrun
# Recipe:: postrestoredb.rb
#
# Copyright 2010, Smashrun, Inc.
# Author:: Steven Craig <support@smashrun.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRdeployIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
log("begin postrestoredb.rb") { level :debug }
log("running postrestoredb.rb") { level :info }
unless File.exists?("#{node[:restoredb][:sqlscripts]}\\deploysql-#{node[:deploy][:tag]}.log")
# ensure necessary dirs are setup and available
["#{node[:restoredb][:tempdir]}", "#{node[:restoredb][:sqlscripts]}", "#{node[:restoredb][:restoredir]}"].each do |dir|
# log("create #{dir} directory if necessary") { level :debug }
directory "#{dir}" do
action :create
not_if { File.exists?("#{dir}") }
recursive true
end
end
database = "smashrun"
projectname = "Web"
# svn export sql script depending on tag
# individual files should be force-exported in case they already exist (force will overwrite)
log("export deploy.sql to #{node[:restoredb][:sqlscripts]}") { level :debug }
subversion "export-deploy.sql_#{database}" do
action :force_export
svn_username "#{node[:deploy][:svnuser]}"
svn_password "#{node[:deploy][:svnpasswd]}"
destination "#{node[:restoredb][:sqlscripts]}\\deploy.sql"
repository "#{node[:deploy][:svnbase]}/tags/#{node[:deploy][:tag]}/#{projectname}/sql/deploy.sql"
revision "HEAD"
end
# always ensure templated batch file to execute transact-sql statements is up-to-date
log("create #{node[:restoredb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]} if necessary") { level :debug }
template "#{node[:restoredb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]}" do
source "#{node[:createdb][:executesql_template]}"
variables({
:author_name => "#{node[:restoredb][:author_name]}",
:author_email => "#{node[:restoredb][:author_email]}",
:working_dir => "#{node[:restoredb][:sqlscripts]}",
:sqlcmd_path => "#{node[:createdb][:sqlcmd_path]}",
:sqlsausername => "#{node[:createdb][:sqlsausername]}",
:executesql_bat => "#{node[:createdb][:executesql_bat]}",
:executesql_template => "#{node[:createdb][:executesql_template]}"
})
end
# tweet
log("tweet begin refresh #{node[:hostname]}-#{database} with #{node[:deploy][:tag]}") { level :debug }
ruby_block "begin refresh #{node[:hostname]}-#{database} with #{node[:deploy][:tag]}" do
block {
node[:tweeter][:admin].each { |a|
Tweeter.tweet(a, "begin refresh #{node[:hostname]}-#{database} with #{node[:deploy][:tag]}") }
}
ignore_failure true
end
# deploy changes to current database
execute "deploy_sql-#{node[:deploy][:tag]}" do
cwd "#{node[:restoredb][:sqlscripts]}"
command "#{node[:restoredb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]} SQL-SAPASSWD deploy.sql deploysql-#{node[:deploy][:tag]}"
timeout 3600
end
# email postrestore report
# log("email postrestore report") { level :debug }
ruby_block "email #{database} tag #{node[:deploy][:tag]} postrestore report" do
only_if "#{node[:restoredb][:emailreport]}"
block {
unless File.exists?("#{node[:restoredb][:sqlscripts]}\\email-#{node[:deploy][:tag]}.log")
node[:emailer][:to].each { |to|
Emailer.send("#{node[:emailer][:from]}", to, "#{node[:hostname]} deploysql report: #{database} tag #{node[:deploy][:tag]}", "attachment only - no inline text", "#{node[:restoredb][:sqlscripts]}\\deploysql-#{node[:deploy][:tag]}.log") }
end
}
ignore_failure true
end
# ticket attach deploysql report
log("update ticket with deploysql report") { level :debug }
ruby_block "update ticket with tag #{node[:deploy][:tag]} deploysql report" do
block {
unless File.exists?("#{node[:restoredb][:sqlscripts]}\\email-#{node[:deploy][:tag]}.log")
fn = "#{node[:restoredb][:sqlscripts]}\\deploysql-#{node[:deploy][:tag]}.log"
fh = File.new(fn, "rb")
sdata = fh.read
data = XMLRPC::Base64.new(sdata)
Tracker.ticketattach("#{node[:build][:ticket]}", "deploysql-#{node[:deploy][:tag]}.log", "#{node[:hostname]} #{database} deploysql report #{node[:deploy][:tag]}", data)
end
}
ignore_failure true
end
end
log("end postrestoredb.rb") { level :info }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.