lib/rubber/recipes/rubber/utils.rb in sml-rubber-0.9.13 vs lib/rubber/recipes/rubber/utils.rb in sml-rubber-1.5.5

- old
+ new

@@ -14,27 +14,30 @@ end instance_alias = ENV['ALIAS'] = rubber.get_env("ALIAS", "Hostname to use for staging instance", true, RUBBER_ENV) default_roles = rubber_env.staging_roles || "*" roles = ENV['ROLES'] = rubber.get_env("ROLES", "Roles to use for staging instance", true, default_roles) - # some bootstraps update code (bootstrap_db) but if you don't have that role, need to do it here - # Since release directory variable gets reused by cap, we have to just do the symlink here - doing - # a update again will fail - set :rubber_code_was_updated, false - after "deploy:update_code" do - set :rubber_code_was_updated, true - end - if rubber_instances[instance_alias] logger.info "Instance already exists, skipping to bootstrap" else rubber.create end + + # stop everything before so monit doesn't start stuff during bootstrapping + # if its already installed due to a bundled instance + deploy.stop rescue nil + rubber.bootstrap - # stop everything in case we have a bundled instance with monit, etc starting at boot + + # stop everything after in case package upgrades during bootstrap start up + # services - we should be able to safely do a deploy:start below deploy.stop rescue nil - if ! rubber_code_was_updated + + # some bootstraps update code (bootstrap_db) but if you don't have that role, need to do it here + # Since release directory variable gets reused by cap, we have to just do the symlink here - doing + # a update again will fail + if ! fetch(:rubber_code_was_updated, false) deploy.update_code end deploy.symlink deploy.migrate deploy.start @@ -103,12 +106,12 @@ # group each role's servers into slices, but combine slices across roles slices = [] servers.each do |rolename, svrs| next if svrs.size == 0 - # figure out size of each slice by deviding server count by # of groups - slice_size = svrs.size / (options.delete(:groups) || 2) + # figure out size of each slice by dividing server count by # of groups + slice_size = (Float(svrs.size) / (options.delete(:groups) || 2)).round slice_size = 1 if slice_size == 0 slice_idx = 0 svrs.each_slice(slice_size) do |srv_slice| slices[slice_idx] ||= [] slices[slice_idx] += srv_slice @@ -145,28 +148,36 @@ _get_ip rescue ConnectionError end return local_alias end - def prepare_script(name, contents) + def prepare_script(name, contents, stop_on_error_cmd=rubber_env.stop_on_error_cmd) script = "/tmp/#{name}" # this lets us abort a script if a command in the middle of it errors out - contents = "#{rubber_env.stop_on_error_cmd}\n#{contents}" if rubber_env.stop_on_error_cmd + contents = "#{stop_on_error_cmd}\n#{contents}" if stop_on_error_cmd put(contents, script) return script end - def run_script(name, contents) + def run_script(name, contents, opts = {}) + args = opts.delete(:script_args) script = prepare_script(name, contents) - run "sh #{script}" + run "bash #{script} #{args}", opts end - def sudo_script(name, contents) + def sudo_script(name, contents, opts = {}) + args = opts.delete(:script_args) script = prepare_script(name, contents) - sudo "sh #{script}" + run "#{sudo} bash -l #{script} #{args}", opts end + def top.rsudo(command, opts = {}, &block) + user = opts.delete(:as) + args = "-H -u #{user}" if user + run "#{sudo opts} #{args} bash -l -c '#{command}'", opts, &block + end + def get_env(name, desc, required=false, default=nil) value = ENV.delete(name) msg = "#{desc}" msg << " [#{default}]" if default msg << ": " @@ -198,6 +209,18 @@ end end return opts end + # some bootstraps update code (bootstrap_db), so keep track so we don't do it multiple times + after "deploy:update_code" do + set :rubber_code_was_updated, true + end + + def update_code_for_bootstrap + unless (fetch(:rubber_code_was_updated, false)) + deploy.setup + deploy.update_code + end + end + end \ No newline at end of file