From d398c167caf9425163cdd89b1641a1898494757f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Kar=C3=A9kinian?= Date: Tue, 14 May 2019 16:39:21 +0200 Subject: [PATCH] Allow to pass extra attributes to backup PostgreSQL databases --- site-cookbooks/backup/attributes/default.rb | 3 ++- .../backup/templates/default/backup.rb.erb | 9 +++++++-- .../kosmos-ejabberd/recipes/backup.rb | 18 ++++++++++++------ .../kosmos-mastodon/recipes/default.rb | 16 +++++----------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/site-cookbooks/backup/attributes/default.rb b/site-cookbooks/backup/attributes/default.rb index 0d9fd55..4ce200a 100644 --- a/site-cookbooks/backup/attributes/default.rb +++ b/site-cookbooks/backup/attributes/default.rb @@ -27,7 +27,8 @@ default["backup"]["mysql"]["username"] = "root" default["backup"]["mysql"]["host"] = "localhost" # PostgreSQL default settings -default["backup"]["postgresql"]["databases"] = [] +default["backup"]["postgresql"]["databases"] = {} +default["backup"]["postgresql"]["username"] = "postgres" default["backup"]["postgresql"]["host"] = "localhost" default["backup"]["postgresql"]["port"] = 5432 diff --git a/site-cookbooks/backup/templates/default/backup.rb.erb b/site-cookbooks/backup/templates/default/backup.rb.erb index d844c20..5267f07 100644 --- a/site-cookbooks/backup/templates/default/backup.rb.erb +++ b/site-cookbooks/backup/templates/default/backup.rb.erb @@ -17,9 +17,14 @@ KosmosBackup.new(:default, 'default backup') do <%- end -%> <%- if node["backup"]["postgresql"] -%> -<%- node["backup"]["postgresql"]["databases"].each do |db_name| -%> - database PostgreSQL, :"<%= db_name.to_sym %>" do |db| +<%- node["backup"]["postgresql"]["databases"].each do |db_name, h| -%> + database PostgreSQL, :"<%= db_name %>" do |db| db.name = "<%= db_name %>" + <%- unless h.nil? -%> + <%- h.each do |k, v| -%> + db.<%= k %> = "<%= v %>" + <%- end -%> + <%- end -%> end <%- end -%> <%- end -%> diff --git a/site-cookbooks/kosmos-ejabberd/recipes/backup.rb b/site-cookbooks/kosmos-ejabberd/recipes/backup.rb index 2be4e78..57fb43a 100644 --- a/site-cookbooks/kosmos-ejabberd/recipes/backup.rb +++ b/site-cookbooks/kosmos-ejabberd/recipes/backup.rb @@ -24,16 +24,22 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +postgresql_data_bag_item = data_bag_item('credentials', 'postgresql') + unless node.chef_environment == "development" # backup the data dir and the config files node.override["backup"]["archives"]["ejabberd"] = ["/opt/ejabberd", "/var/www/xmpp.kosmos.org", "/var/www/xmpp.5apps.com"] - unless node["backup"]["postgresql"]["databases"].include? "ejabberd" - node.override["backup"]["postgresql"]["databases"] = - node["backup"]["postgresql"]["databases"].to_a << "ejabberd" + unless node["backup"]["postgresql"]["databases"].keys.include? "ejabberd" + node.override["backup"]["postgresql"]["databases"]["ejabberd"] = { + username: "ejabberd", + password: postgresql_data_bag_item['ejabberd_user_password'] + } end - unless node["backup"]["postgresql"]["databases"].include? "ejabberd_5apps" - node.override["backup"]["postgresql"]["databases"] = - node["backup"]["postgresql"]["databases"].to_a << "ejabberd_5apps" + unless node["backup"]["postgresql"]["databases"].keys.include? "ejabberd_5apps" + node.override["backup"]["postgresql"]["databases"]["ejabberd_5apps"] = { + username: "ejabberd", + password: postgresql_data_bag_item['ejabberd_user_password'] + } end include_recipe "backup" end diff --git a/site-cookbooks/kosmos-mastodon/recipes/default.rb b/site-cookbooks/kosmos-mastodon/recipes/default.rb index a9c866f..99bd11d 100644 --- a/site-cookbooks/kosmos-mastodon/recipes/default.rb +++ b/site-cookbooks/kosmos-mastodon/recipes/default.rb @@ -209,17 +209,11 @@ end # unless node.chef_environment == "development" - node.override["backup"]["postgresql"]["host"] = "localhost" - unless platform?('ubuntu') && node[:platform_version].to_f < 18.04 - node.override["backup"]["postgresql"]["username"] = "mastodon" - node.override["backup"]["postgresql"]["password"] = postgresql_data_bag_item['mastodon_user_password'] - else - node.override["backup"]["postgresql"]["username"] = "postgres" - node.override["backup"]["postgresql"]["password"] = node['postgresql']['password']['postgres'] - end - unless node["backup"]["postgresql"]["databases"].include? 'mastodon' - node.override["backup"]["postgresql"]["databases"] = - node["backup"]["postgresql"]["databases"].to_a << "mastodon" + unless node["backup"]["postgresql"]["databases"].keys.include? 'mastodon' + node.override["backup"]["postgresql"]["databases"]["mastodon"] = { + username: "mastodon", + password: postgresql_data_bag_item['mastodon_user_password'] + } end include_recipe "backup"