Created
December 17, 2011 20:48
-
-
Save cixelsyd/1491334 to your computer and use it in GitHub Desktop.
"createdb.rb" creates empty MSSQL databases from settings inside data bags
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Author:: Steven Craig <[email protected]> | |
# Cookbook Name:: windows | |
# Recipe:: createdb | |
# | |
# Copyright 2010, Smashrun, Inc. | |
# | |
# 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 WARRANTIES 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 createdb.rb") { level :debug } | |
log("running createdb.rb") { level :info } | |
unless File.exists?("#{node[:createdb][:sqlscripts]}\\#{node[:createdb][:basesql]}.log") | |
running_database = [] | |
database_info = [] | |
# get the configuration information for #{dbname} | |
search(:running_database, "id:#{node[:hostname]}") do |host| | |
running_database << host["database"].split(",") | |
running_database.flatten! | |
running_database.each do |dbname| | |
search(:database_info, "id:#{dbname}") do |db| | |
dbinfo = { "id" => db["id"], | |
"dbname" => db["id"], | |
"backupdrive" => db["backupdrive"], | |
"backupdir" => db["backupdir"], | |
"collation" => db["collation"], | |
"comment" => db["comment"], | |
"datadrive" => db["datadrive"], | |
"datadir" => db["datadir"], | |
"datafile" => db["datafile"], | |
"datainitsize" => db["datainitsize"], | |
"datamaxsize" => db["datamaxsize"], | |
"owner" => db["owner"], | |
"restoredrive" => db["restoredrive"], | |
"restoredir" => db["restoredir"], | |
"transdrive" => db["transdrive"], | |
"transdir" => db["transdir"], | |
"transfile" => db["transfile"], | |
"transinitsize" => db["transinitsize"], | |
"transmaxsize" => db["transmaxsize"] | |
} | |
database_info << dbinfo | |
end | |
end | |
# log(database_info.inspect) {level :debug} | |
# ensure necessary dirs are setup and available | |
database_info.each do |db| | |
["#{node[:createdb][:tempdir]}", "#{node[:createdb][:sqlscripts]}", "#{db["datadrive"]}#{db["datadir"]}", "#{db["transdrive"]}#{db["transdir"]}","#{db["backupdrive"]}#{db["backupdir"]}"].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 | |
end | |
# run createdb | |
execute "#{node[:createdb][:executesql_bat]}" do | |
cwd "#{node[:createdb][:sqlscripts]}" | |
command "#{node[:createdb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]} SQL-SAPASSWD #{node[:createdb][:basesql]} #{node[:createdb][:basesql]}" | |
not_if { File.exists?("#{node[:createdb][:sqlscripts]}\\#{node[:createdb][:basesql]}.log") } | |
timeout 300 | |
end | |
# templated batch file to execute transact-sql create statements | |
log("create #{node[:createdb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]} if necessary") { level :debug } | |
template "#{node[:createdb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]}" do | |
source "#{node[:createdb][:executesql_template]}" | |
variables({ | |
:author_name => "#{node[:createdb][:author_name]}", | |
:author_email => "#{node[:createdb][:author_email]}", | |
:working_dir => "#{node[:createdb][:sqlscripts]}", | |
:sqlcmd_path => "#{node[:createdb][:sqlcmd_path]}", | |
:sqlsausername => "#{node[:createdb][:sqlsausername]}", | |
:executesql_bat => "#{node[:createdb][:executesql_bat]}", | |
:executesql_template => "#{node[:createdb][:executesql_template]}" | |
}) | |
notifies :run, resources(:execute => "#{node[:createdb][:executesql_bat]}") | |
end | |
# ensure transact-sql scripts to createdb are current and execute createdb if necessary | |
templated = nil | |
begin | |
templated = resources(:template => "createdb-#{node[:createdb][:basesql]}.erb") | |
rescue Chef::Exceptions::ResourceNotFound | |
templated = template "#{node[:createdb][:sqlscripts]}\\#{node[:createdb][:basesql]}" do | |
source "createdb-#{node[:createdb][:basesql]}.erb" | |
cookbook "smashrun" | |
backup false | |
variables({ | |
:database => database_info, | |
:author_name => "#{node[:createdb][:author_name]}", | |
:author_email => "#{node[:createdb][:author_email]}", | |
:basesql => "#{node[:createdb][:basesql]}", | |
:basesql_template => "createdb-#{node[:createdb][:basesql]}.erb" | |
}) | |
notifies :run, resources(:execute => "#{node[:createdb][:executesql_bat]}"), :immediately | |
end | |
end | |
sqltools = "false" | |
# variables to store the sql tools installer (future functionality) | |
exe = ::File.basename(node[:createdb][:installer]) | |
cmd = "c:\\chef\\tmp\\#{exe}" | |
# get the sql tools installer (future functionality) | |
remote_file exe do | |
action :nothing | |
source node[:createdb][:url] | |
# not_if { File.exists?(exe) } | |
only_if "#{sqltools}" | |
path cmd | |
backup false | |
end | |
# install sql tools (future functionality) | |
execute exe do | |
action :nothing | |
timeout 1800 | |
command %Q(#{cmd} /q:a /c:"install /q") | |
only_if "#{sqltools}" | |
end | |
end | |
end | |
log("end createdb.rb") { level :info } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment