Replace content_from_file extension with simpler edit_file technique
authorTom Hughes <tom@compton.nu>
Sat, 23 Nov 2013 00:23:36 +0000 (00:23 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 23 Nov 2013 00:28:48 +0000 (00:28 +0000)
cookbooks/chef/libraries/content_from_file.rb [deleted file]
cookbooks/chef/libraries/edit_file.rb [new file with mode: 0644]
cookbooks/osqa/recipes/default.rb
cookbooks/otrs/recipes/default.rb
cookbooks/thinkup/recipes/default.rb
cookbooks/web/definitions/rails_port.rb
cookbooks/web/recipes/cgimap.rb
cookbooks/wordpress/definitions/wordpress_site.rb

diff --git a/cookbooks/chef/libraries/content_from_file.rb b/cookbooks/chef/libraries/content_from_file.rb
deleted file mode 100644 (file)
index d8d0fe7..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-class Chef
-  class Resource
-    class File
-      def content_from_file(file, &block)
-        @content_file = file
-        @content_block = block
-      end
-
-      def content(text = nil)
-        if text
-          @content = text
-        elsif @content
-          @content
-        elsif @content_file
-          ::File.new(@content_file).collect do |line|
-            line = @content_block.call(line)
-          end.join("")
-        end
-      end
-    end
-  end
-end
diff --git a/cookbooks/chef/libraries/edit_file.rb b/cookbooks/chef/libraries/edit_file.rb
new file mode 100644 (file)
index 0000000..1c8e263
--- /dev/null
@@ -0,0 +1,9 @@
+class Chef
+  class Recipe
+    def edit_file(file, &block)
+      ::File.new(file).collect do |line|
+        line = yield line
+      end.join("")
+    end
+  end
+end
index 2ed82d2..766f2f4 100644 (file)
@@ -92,22 +92,24 @@ node[:osqa][:sites].each do |site|
     notifies :reload, resources(:service => "apache2")
   end
 
+  settings = edit_file "#{directory}/osqa/settings_local.py.dist" do |line|
+    line.gsub!(/^( *)'ENGINE': '.*',/, "\\1'ENGINE': 'django.db.backends.postgresql_psycopg2',")
+    line.gsub!(/^( *)'NAME': '.*',/, "\\1'NAME': '#{database_name}',")
+    line.gsub!(/^( *)'USER': '.*',/, "\\1'USER': '#{database_user}',")
+    line.gsub!(/^( *)'PASSWORD': '.*',/, "\\1'PASSWORD': '#{database_password}',")
+    line.gsub!(/^CACHE_BACKEND = .*/, "CACHE_BACKEND = 'memcached://127.0.0.1:11211/'")
+    line.gsub!(/^APP_URL = 'http:\/\/'/, "APP_URL = 'http://#{name}'")
+    line.gsub!(/^TIME_ZONE = 'America\/New_York'/, "TIME_ZONE = 'Europe/London'")
+    line.gsub!(/^DISABLED_MODULES = \[([^\]]+)\]/, "DISABLED_MODULES = [\\1, 'localauth', 'facebookauth', 'oauthauth']")
+
+    line
+  end
+
   file "#{directory}/osqa/settings_local.py" do
     owner site_user
     group site_group
     mode 0644
-    content_from_file "#{directory}/osqa/settings_local.py.dist" do |line|
-      line.gsub!(/^( *)'ENGINE': '.*',/, "\\1'ENGINE': 'django.db.backends.postgresql_psycopg2',")
-      line.gsub!(/^( *)'NAME': '.*',/, "\\1'NAME': '#{database_name}',")
-      line.gsub!(/^( *)'USER': '.*',/, "\\1'USER': '#{database_user}',")
-      line.gsub!(/^( *)'PASSWORD': '.*',/, "\\1'PASSWORD': '#{database_password}',")
-      line.gsub!(/^CACHE_BACKEND = .*/, "CACHE_BACKEND = 'memcached://127.0.0.1:11211/'")
-      line.gsub!(/^APP_URL = 'http:\/\/'/, "APP_URL = 'http://#{name}'")
-      line.gsub!(/^TIME_ZONE = 'America\/New_York'/, "TIME_ZONE = 'Europe/London'")
-      line.gsub!(/^DISABLED_MODULES = \[([^\]]+)\]/, "DISABLED_MODULES = [\\1, 'localauth', 'facebookauth', 'oauthauth']")
-
-      line
-    end
+    content settings
     notifies :reload, resources(:service => "apache2")
   end
 end
index 901c0ee..487fddf 100644 (file)
@@ -59,29 +59,29 @@ execute "untar-otrs-#{version}" do
   not_if { File.exist?("/opt/otrs-#{version}") }
 end
 
+config = edit_file "/opt/otrs-#{version}/Kernel/Config.pm.dist" do |line|
+  line.gsub!(/^( *)\$Self->{Database} = 'otrs'/, "\\1$Self->{Database} = '#{database_name}'")
+  line.gsub!(/^( *)\$Self->{DatabaseUser} = 'otrs'/, "\\1$Self->{DatabaseUser} = '#{database_user}'")
+  line.gsub!(/^( *)\$Self->{DatabasePw} = 'some-pass'/, "\\1$Self->{DatabasePw} = '#{database_password}'")
+  line.gsub!(/^( *)\$Self->{Database} = 'otrs'/, "\\1$Self->{Database} = '#{database_name}'")
+  line.gsub!(/^( *\$Self->{DatabaseDSN} = "DBI:mysql:)/, "#\\1")
+  line.gsub!(/^#( *\$Self->{DatabaseDSN} = "DBI:Pg:.*;host=)/, "\\1")
+
+  line
+end
+
 file "/opt/otrs-#{version}/Kernel/Config.pm" do
   owner user
   group "www-data"
   mode 0664
-  content_from_file "/opt/otrs-#{version}/Kernel/Config.pm.dist" do |line|
-    line.gsub!(/^( *)\$Self->{Database} = 'otrs'/, "\\1$Self->{Database} = '#{database_name}'")
-    line.gsub!(/^( *)\$Self->{DatabaseUser} = 'otrs'/, "\\1$Self->{DatabaseUser} = '#{database_user}'")
-    line.gsub!(/^( *)\$Self->{DatabasePw} = 'some-pass'/, "\\1$Self->{DatabasePw} = '#{database_password}'")
-    line.gsub!(/^( *)\$Self->{Database} = 'otrs'/, "\\1$Self->{Database} = '#{database_name}'")
-    line.gsub!(/^( *\$Self->{DatabaseDSN} = "DBI:mysql:)/, "#\\1")
-    line.gsub!(/^#( *\$Self->{DatabaseDSN} = "DBI:Pg:.*;host=)/, "\\1")
-
-    line
-  end
+  content config
 end
 
 file "/opt/otrs-#{version}/Kernel/Config/GenericAgent.pm" do
   owner user
   group "www-data"
   mode 0664
-  content_from_file "/opt/otrs-#{version}/Kernel/Config/GenericAgent.pm.dist" do |line|
-    line
-  end
+  content IO.read("/opt/otrs-#{version}/Kernel/Config/GenericAgent.pm.dist")
 end
 
 link "/opt/otrs" do
index 90992ac..2586446 100644 (file)
@@ -77,35 +77,39 @@ directory "/srv/thinkup.openstreetmap.org/webapp/_lib/view/compiled_view" do
   mode "0755"
 end
 
+thinkup_config = edit_file "/srv/thinkup.openstreetmap.org/webapp/config.sample.inc.php" do |line|
+  line.gsub!(/^(\$THINKUP_CFG\['site_root_path'\] *=) '[^']*';$/, "\\1 '/';")
+  line.gsub!(/^(\$THINKUP_CFG\['timezone'\] *=) '[^']*';$/, "\\1 'Europe/London';")
+  line.gsub!(/^(\$THINKUP_CFG\['db_user'\] *=) '[^']*';$/, "\\1 'thinkup';")
+  line.gsub!(/^(\$THINKUP_CFG\['db_password'\] *=) '[^']*';$/, "\\1 '#{passwords["database"]}';")
+  line.gsub!(/^(\$THINKUP_CFG\['db_name'\] *=) '[^']*';$/, "\\1 'thinkup';")
+
+  line
+end
+
 file "/srv/thinkup.openstreetmap.org/webapp/config.inc.php" do
   owner "root"
   group "root"
   mode 0644
-  content_from_file "/srv/thinkup.openstreetmap.org/webapp/config.sample.inc.php" do |line|
-    line.gsub!(/^(\$THINKUP_CFG\['site_root_path'\] *=) '[^']*';$/, "\\1 '/';")
-    line.gsub!(/^(\$THINKUP_CFG\['timezone'\] *=) '[^']*';$/, "\\1 'Europe/London';")
-    line.gsub!(/^(\$THINKUP_CFG\['db_user'\] *=) '[^']*';$/, "\\1 'thinkup';")
-    line.gsub!(/^(\$THINKUP_CFG\['db_password'\] *=) '[^']*';$/, "\\1 '#{passwords["database"]}';")
-    line.gsub!(/^(\$THINKUP_CFG\['db_name'\] *=) '[^']*';$/, "\\1 'thinkup';")
-
-    line
-  end
+  content thinkup_config
   notifies :reload, resources(:service => "apache2")
 end
 
+thinkup_cron = edit_file "/srv/thinkup.openstreetmap.org/extras/cron/config.sample" do |line|
+  line.gsub!(/^thinkup="[^"]*"$/, "thinkup=\"/srv/thinkup.openstreetmap.org\"")
+  line.gsub!(/^thinkup_username="[^"]*"$/, "thinkup_username=\"openstreetmap@jonno.cix.co.uk\"")
+  line.gsub!(/^thinkup_password="[^"]*"$/, "thinkup_password=\"#{passwords["admin"]}\"")
+  line.gsub!(/^php="[^"]*"$/, "php=\"/usr/bin/php\"")
+  line.gsub!(/^#crawl_interval=[0-9]+$/, "crawl_interval=30")
+
+  line
+end
+
 file "/srv/thinkup.openstreetmap.org/extras/cron/config" do
   owner "root"
   group "thinkup"
   mode 0640
-  content_from_file "/srv/thinkup.openstreetmap.org/extras/cron/config.sample" do |line|
-    line.gsub!(/^thinkup="[^"]*"$/, "thinkup=\"/srv/thinkup.openstreetmap.org\"")
-    line.gsub!(/^thinkup_username="[^"]*"$/, "thinkup_username=\"openstreetmap@jonno.cix.co.uk\"")
-    line.gsub!(/^thinkup_password="[^"]*"$/, "thinkup_password=\"#{passwords["admin"]}\"")
-    line.gsub!(/^php="[^"]*"$/, "php=\"/usr/bin/php\"")
-    line.gsub!(/^#crawl_interval=[0-9]+$/, "crawl_interval=30")
-
-    line
-  end
+  content thinkup_cron
 end
 
 template "/etc/cron.d/thinkup" do
index ba022fc..be6e0be 100644 (file)
@@ -145,72 +145,74 @@ define :rails_port, :action => [ :create, :enable ] do
     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
   end
 
-  file "#{rails_directory}/config/application.yml" do
-    owner rails_user
-    group rails_group
-    mode 0664
-    content_from_file "#{rails_directory}/config/example.application.yml" do |line|
-      line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
+  application_yml = edit_file "#{rails_directory}/config/example.application.yml" do |line|
+    line.gsub!(/^( *)server_url:.*$/, "\\1server_url: \"#{name}\"")
 
-      if params[:email_from]
-        line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
-      end
+    if params[:email_from]
+      line.gsub!(/^( *)email_from:.*$/, "\\1email_from: \"#{params[:email_from]}\"")
+    end
 
-      line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
+    line.gsub!(/^( *)status:.*$/, "\\1status: :#{status}")
 
-      if params[:messages_domain]
-        line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
-      end
+    if params[:messages_domain]
+      line.gsub!(/^( *)#messages_domain:.*$/, "\\1messages_domain: \"#{params[:messages_domain]}\"")
+    end
 
-      line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
+    line.gsub!(/^( *)#geonames_username:.*$/, "\\1geonames_username: \"openstreetmap\"")
 
-      if params[:quova_username]
-        line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
-        line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
-      end
+    if params[:quova_username]
+      line.gsub!(/^( *)#quova_username:.*$/, "\\1quova_username: \"#{params[:quova_username]}\"")
+      line.gsub!(/^( *)#quova_password:.*$/, "\\1quova_password: \"#{params[:quova_password]}\"")
+    end
 
-      if params[:soft_memory_limit]
-        line.gsub!(/^( *)#soft_memory_limit:.*$/, "\\1soft_memory_limit: #{params[:soft_memory_limit]}")
-      end
+    if params[:soft_memory_limit]
+      line.gsub!(/^( *)#soft_memory_limit:.*$/, "\\1soft_memory_limit: #{params[:soft_memory_limit]}")
+    end
 
-      if params[:hard_memory_limit]
-        line.gsub!(/^( *)#hard_memory_limit:.*$/, "\\1hard_memory_limit: #{params[:hard_memory_limit]}")
-      end
+    if params[:hard_memory_limit]
+      line.gsub!(/^( *)#hard_memory_limit:.*$/, "\\1hard_memory_limit: #{params[:hard_memory_limit]}")
+    end
 
-      if params[:gpx_dir]
-        line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
-        line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
-      end
+    if params[:gpx_dir]
+      line.gsub!(/^( *)gpx_trace_dir:.*$/, "\\1gpx_trace_dir: \"#{params[:gpx_dir]}/traces\"")
+      line.gsub!(/^( *)gpx_image_dir:.*$/, "\\1gpx_image_dir: \"#{params[:gpx_dir]}/images\"")
+    end
 
-      if params[:attachments_dir]
-        line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
-      end
+    if params[:attachments_dir]
+      line.gsub!(/^( *)attachments_dir:.*$/, "\\1attachments_dir: \"#{params[:attachments_dir]}\"")
+    end
 
-      if params[:log_path]
-        line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
-      end
+    if params[:log_path]
+      line.gsub!(/^( *)#log_path:.*$/, "\\1log_path: \"#{params[:log_path]}\"")
+    end
 
-      if params[:memcache_servers]
-        line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
-      end
+    if params[:memcache_servers]
+      line.gsub!(/^( *)#memcache_servers:.*$/, "\\1memcache_servers: [ \"#{params[:memcache_servers].join("\", \"")}\" ]")
+    end
 
-      if params[:potlatch2_key]
-        line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
-      end
+    if params[:potlatch2_key]
+      line.gsub!(/^( *)#potlatch2_key:.*$/, "\\1potlatch2_key: \"#{params[:potlatch2_key]}\"")
+    end
 
-      if params[:id_key]
-        line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
-      end
+    if params[:id_key]
+      line.gsub!(/^( *)#id_key:.*$/, "\\1id_key: \"#{params[:id_key]}\"")
+    end
 
-      if params[:oauth_key]
-        line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
-      end
+    if params[:oauth_key]
+      line.gsub!(/^( *)#oauth_key:.*$/, "\\1oauth_key: \"#{params[:oauth_key]}\"")
+    end
 
-      line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
-      line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
+    line.gsub!(/^( *)require_terms_seen:.*$/, "\\1require_terms_seen: true")
+    line.gsub!(/^( *)require_terms_agreed:.*$/, "\\1require_terms_agreed: true")
 
-      line
-    end
+    line
+  end
+
+  file "#{rails_directory}/config/application.yml" do
+    owner rails_user
+    group rails_group
+    mode 0664
+    content application_yml
     notifies :touch, resources(:file => "#{rails_directory}/tmp/restart.txt")
   end
 
index 41b27f0..c1f8e89 100644 (file)
@@ -82,27 +82,29 @@ else
   database_readonly = node[:web][:status] == "database_readonly"
 end
 
+cgimap_init = edit_file "#{cgimap_directory}/scripts/cgimap.init" do |line|
+  line.gsub!(/^CGIMAP_HOST=.*;/, "CGIMAP_HOST=#{database_host};")
+  line.gsub!(/^CGIMAP_DBNAME=.*;/, "CGIMAP_DBNAME=openstreetmap;")
+  line.gsub!(/^CGIMAP_USERNAME=.*;/, "CGIMAP_USERNAME=rails;")
+  line.gsub!(/^CGIMAP_PASSWORD=.*;/, "CGIMAP_PASSWORD=#{db_passwords['rails']};")
+  line.gsub!(/^CGIMAP_PIDFILE=.*;/, "CGIMAP_PIDFILE=#{pid_directory}/cgimap.pid;")
+  line.gsub!(/^CGIMAP_LOGFILE=.*;/, "CGIMAP_LOGFILE=#{log_directory}/cgimap.log;")
+  line.gsub!(/^CGIMAP_MEMCACHE=.*;/, "CGIMAP_MEMCACHE=rails1,rails2,rails3;")
+
+  line.gsub!(/\/home\/rails\/bin\/map/, "#{cgimap_directory}/map")
+
+  if database_readonly
+    line.gsub!(/--daemon/, "--daemon --readonly")
+  end
+
+  line
+end
+
 file "/etc/init.d/cgimap" do
   owner "root"
   group "root"
   mode 0755
-  content_from_file "#{cgimap_directory}/scripts/cgimap.init" do |line|
-    line.gsub!(/^CGIMAP_HOST=.*;/, "CGIMAP_HOST=#{database_host};")
-    line.gsub!(/^CGIMAP_DBNAME=.*;/, "CGIMAP_DBNAME=openstreetmap;")
-    line.gsub!(/^CGIMAP_USERNAME=.*;/, "CGIMAP_USERNAME=rails;")
-    line.gsub!(/^CGIMAP_PASSWORD=.*;/, "CGIMAP_PASSWORD=#{db_passwords['rails']};")
-    line.gsub!(/^CGIMAP_PIDFILE=.*;/, "CGIMAP_PIDFILE=#{pid_directory}/cgimap.pid;")
-    line.gsub!(/^CGIMAP_LOGFILE=.*;/, "CGIMAP_LOGFILE=#{log_directory}/cgimap.log;")
-    line.gsub!(/^CGIMAP_MEMCACHE=.*;/, "CGIMAP_MEMCACHE=rails1,rails2,rails3;")
-
-    line.gsub!(/\/home\/rails\/bin\/map/, "#{cgimap_directory}/map")
-
-    if database_readonly
-      line.gsub!(/--daemon/, "--daemon --readonly")
-    end
-
-    line
-  end
+  content cgimap_init
 end
 
 if ["database_offline", "api_offline"].include?(node[:web][:status])
index 8cd887e..fcdf694 100644 (file)
@@ -64,35 +64,37 @@ define :wordpress_site, :action => [ :create, :enable ] do
     notifies :reload, "service[apache2]"
   end
 
+  wp_config = edit_file "#{directory}/wp-config-sample.php" do |line|
+    line.gsub!(/database_name_here/, database_name)
+    line.gsub!(/username_here/, database_user)
+    line.gsub!(/password_here/, database_password)
+    line.gsub!(/wp_/, database_prefix)
+
+    line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:auth_key]}'")
+    line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:secure_auth_key]}'")
+    line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:logged_in_key]}'")
+    line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:nonce_key]}'")
+    line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:auth_salt]}'")
+    line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:secure_auth_salt]}'")
+    line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:logged_in_salt]}'")
+    line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:nonce_salt]}'")
+
+    if line =~ /define\('WP_DEBUG'/
+      line += "\n"
+      line += "/**\n"
+      line += " * Don't allow file editing.\n"
+      line += " */\n"
+      line += "define('DISALLOW_FILE_EDIT', true);\n"
+    end
+
+    line
+  end
+
   file "#{directory}/wp-config.php" do
     owner node[:wordpress][:user]
     group node[:wordpress][:group]
     mode 0644
-    content_from_file "#{directory}/wp-config-sample.php" do |line|
-      line.gsub!(/database_name_here/, database_name)
-      line.gsub!(/username_here/, database_user)
-      line.gsub!(/password_here/, database_password)
-      line.gsub!(/wp_/, database_prefix)
-
-      line.gsub!(/('AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:auth_key]}'")
-      line.gsub!(/('SECURE_AUTH_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:secure_auth_key]}'")
-      line.gsub!(/('LOGGED_IN_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:logged_in_key]}'")
-      line.gsub!(/('NONCE_KEY', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:nonce_key]}'")
-      line.gsub!(/('AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:auth_salt]}'")
-      line.gsub!(/('SECURE_AUTH_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:secure_auth_salt]}'")
-      line.gsub!(/('LOGGED_IN_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:logged_in_salt]}'")
-      line.gsub!(/('NONCE_SALT', *)'put your unique phrase here'/, "\\1'#{node[:wordpress][:sites][name][:nonce_salt]}'")
-
-      if line =~ /define\('WP_DEBUG'/
-        line += "\n"
-        line += "/**\n"
-        line += " * Don't allow file editing.\n"
-        line += " */\n"
-        line += "define('DISALLOW_FILE_EDIT', true);\n"
-      end
-
-      line
-    end
+    content wp_config
     notifies :reload, "service[apache2]"
   end