templates/postgresql/config/rubber/deploy-postgresql.rb in rubber-2.1.2 vs templates/postgresql/config/rubber/deploy-postgresql.rb in rubber-2.2.0
- old
+ new
@@ -111,9 +111,56 @@
chmod 700 #{rubber_env.postgresql_data_dir}
ENDSCRIPT
end
desc <<-DESC
+ Promotes a slave instance to master
+ DESC
+ task :promote_slave do
+ master_alias = get_env('MASTER', "Master alias (e.g. db01)", true)
+ slave_alias = get_env('SLAVE', "Slave alias (e.g. db02)", true)
+
+ # remove the master instance so rubber doesn't try to deploy to it
+ # Stays running so needs to be manually deleted
+ master_instance = rubber_instances.remove(master_alias)
+ fatal "Master Instance does not exist: #{master_alias}" unless master_instance
+
+ slave_instance = rubber_instances[slave_alias]
+ fatal "Slave Instance does not exist: #{slave_alias}" unless slave_instance
+
+ # remove all db roles from slave
+ slave_instance.roles.delete_if {|ir| ir.name =~ /db|postgresql/ }
+
+ # add in master db roles to slave
+ new_roles = [Rubber::Configuration::RoleItem.parse("postgresql_master")]
+ new_roles = Rubber::Configuration::RoleItem.expand_role_dependencies(new_roles, get_role_dependencies)
+ slave_instance.roles = (slave_instance.roles + new_roles).uniq
+
+ rubber_instances.save()
+
+ begin
+ Timeout::timeout(10) do
+ logger.info "Stopping server on original master #{master_alias}"
+ rsudo "service monit stop || true", :hosts => master_instance.full_name
+ rsudo "service postgresql stop || true", :hosts => master_instance.full_name
+ end
+ rescue StandardError
+ logger.info "Failed to connect to original master, promoting slave anyway"
+ end
+
+ logger.info "Triggering slave promotion on new master #{slave_alias}"
+ rsudo "touch #{rubber_env.postgresql_data_dir}/trigger_file", :hosts => slave_instance.full_name
+
+ logger.info "The master instance has been removed from instances, but remains running:"
+ logger.info "#{master_alias}, #{master_instance.instance_id}, #{master_instance.external_ip}"
+ logger.info ''
+ logger.info "Roles for #{slave_alias} are now:"
+ logger.info slave_instance.roles.inspect
+ logger.info ''
+ logger.info "Promotion complete, you should commit the instance file and deploy"
+ end
+
+ desc <<-DESC
Starts the postgresql daemons
DESC
task :start, :roles => [:postgresql_master, :postgresql_slave] do
rsudo "#{rubber_env.postgresql_ctl} start"
end
\ No newline at end of file