From 45c5fe3c51b684fbca8cdab530879c4439438bbe Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 16 Sep 2018 18:27:37 +0100 Subject: [PATCH] Add support for using cgimap with dev api instances --- cookbooks/dev/recipes/default.rb | 124 ++++++++++++++++-- .../dev/templates/default/apache.rails.erb | 25 +++- .../templates/default/cgimap.environment.erb | 10 ++ 3 files changed, 146 insertions(+), 13 deletions(-) create mode 100644 cookbooks/dev/templates/default/cgimap.environment.erb diff --git a/cookbooks/dev/recipes/default.rb b/cookbooks/dev/recipes/default.rb index c93176e25..999cc4ba3 100644 --- a/cookbooks/dev/recipes/default.rb +++ b/cookbooks/dev/recipes/default.rb @@ -41,14 +41,8 @@ package %w[ php-pear php-pgsql php-sqlite3 -] - -package %w[ pngcrush pngquant -] - -package %w[ python python-argparse python-beautifulsoup @@ -57,6 +51,25 @@ package %w[ python-magic python-psycopg2 python-gdal + g++ + gcc + make + autoconf + automake + libtool + libfcgi-dev + libxml2-dev + libmemcached-dev + libboost-regex-dev + libboost-system-dev + libboost-program-options-dev + libboost-date-time-dev + libboost-filesystem-dev + libboost-locale-dev + libpqxx-dev + libcrypto++-dev + libyajl-dev + zlib1g-dev ] nodejs_package "svgo" @@ -190,10 +203,29 @@ if node[:postgresql][:clusters][:"9.5/main"] mode 0o755 end + systemd_service "cgimap@" do + description "OpenStreetMap API Server" + type "forking" + environment_file "/etc/default/cgimap-%i" + user "apis" + exec_start "/srv/%i.apis.dev.openstreetmap.org/cgimap/openstreetmap-cgimap --daemon --port $CGIMAP_PORT --instances 5" + exec_reload "/bin/kill -HUP $MAINPID" + private_tmp true + private_devices true + protect_system "full" + protect_home true + no_new_privileges true + restart "on-failure" + end + + cgimap_port = 9000 + node[:dev][:rails].each do |name, details| database_name = details[:database] || "apis_#{name}" site_name = "#{name}.apis.dev.openstreetmap.org" - rails_directory = "/srv/#{name}.apis.dev.openstreetmap.org" + site_directory = "/srv/#{name}.apis.dev.openstreetmap.org" + rails_directory = "#{site_directory}/rails" + cgimap_directory = "#{site_directory}/cgimap" if details[:repository] site_aliases = details[:aliases] || [] @@ -212,6 +244,12 @@ if node[:postgresql][:clusters][:"9.5/main"] extension "btree_gist" end + directory site_directory do + owner "apis" + group "apis" + mode 0o755 + end + rails_port site_name do ruby node[:passenger][:ruby_version] directory rails_directory @@ -236,6 +274,60 @@ if node[:postgresql][:clusters][:"9.5/main"] notifies :restart, "rails_port[#{site_name}]" end + if details[:cgimap_repository] + git cgimap_directory do + action :sync + repository details[:cgimap_repository] + revision details[:cgimap_revision] + user "apis" + group "apis" + end + + execute "#{cgimap_directory}/autogen.sh" do + action :nothing + command "./autogen.sh" + cwd cgimap_directory + user "apis" + group "apis" + subscribes :run, "git[#{cgimap_directory}]", :immediate + end + + execute "#{cgimap_directory}/configure" do + action :nothing + command "./configure --with-fcgi=/usr --with-boost-libdir=/usr/lib/x86_64-linux-gnu" + cwd cgimap_directory + user "apis" + group "apis" + subscribes :run, "execute[#{cgimap_directory}/autogen.sh]", :immediate + end + + execute "#{cgimap_directory}/Makefile" do + action :nothing + command "make -j" + cwd cgimap_directory + user "apis" + group "apis" + subscribes :run, "execute[#{cgimap_directory}/configure]", :immediate + notifies :restart, "service[cgimap@#{name}]" + end + + template "/etc/default/cgimap-#{name}" do + source "cgimap.environment.erb" + owner "root" + group "root" + mode 0o640 + variables :cgimap_port => cgimap_port, + :database_port => node[:postgresql][:clusters][:"9.5/main"][:port], + :database_name => database_name, + :rails_directory => rails_directory + notifies :restart, "service[cgimap@#{name}]" + end + + service "cgimap@#{name}" do + action [:start, :enable] + end + end + ssl_certificate site_name do domains [site_name] + site_aliases notifies :reload, "service[apache2]" @@ -243,14 +335,28 @@ if node[:postgresql][:clusters][:"9.5/main"] apache_site site_name do template "apache.rails.erb" - variables :application_name => name, :aliases => site_aliases, :secret_key_base => secret_key_base + variables :application_name => name, + :aliases => site_aliases, + :secret_key_base => secret_key_base, + :cgimap_enabled => details.key?(:cgimap_repository), + :cgimap_port => cgimap_port end + + cgimap_port += 1 else apache_site site_name do action [:delete] end - directory rails_directory do + service "cgimap@#{name}" do + action [:stop, :disable] + end + + file "/etc/default/cgimap-#{name}" do + action :delete + end + + directory site_directory do action :delete recursive true end diff --git a/cookbooks/dev/templates/default/apache.rails.erb b/cookbooks/dev/templates/default/apache.rails.erb index c8f5f779d..edd8a94a3 100644 --- a/cookbooks/dev/templates/default/apache.rails.erb +++ b/cookbooks/dev/templates/default/apache.rails.erb @@ -14,7 +14,7 @@ CustomLog /var/log/apache2/<%= @name %>-access.log combined ErrorLog /var/log/apache2/<%= @name %>-error.log - DocumentRoot /srv/<%= @name %>/public + DocumentRoot /srv/<%= @name %>/rails/public RailsEnv production PassengerAppGroupName <%= @application_name %> @@ -29,6 +29,23 @@ ForceType text/x-cross-domain-policy +<% if @cgimap_enabled -%> + + # Pass authentication related headers to cgimap + + CGIPassAuth On + + + # Pass supported calls to cgimap + RewriteEngine on + RewriteRule ^/api/0\.6/map$ fcgi://127.0.0.1:<%= @cgimap_port %>$0 [P] + RewriteCond %{REQUEST_METHOD} ^(HEAD|GET)$ + RewriteRule ^/api/0\.6/(node|way|relation|changeset)/[0-9]+$ fcgi://127.0.0.1:<%= @cgimap_port %>$0 [P] + RewriteRule ^/api/0\.6/(node|way|relation)/[0-9]+/history$ fcgi://127.0.0.1:<%= @cgimap_port %>$0 [P] + RewriteRule ^/api/0\.6/(way|relation)/[0-9]+/full$ fcgi://127.0.0.1:<%= @cgimap_port %>$0 [P] + RewriteRule ^/api/0\.6/(nodes|ways|relations)$ fcgi://127.0.0.1:<%= @cgimap_port %>$0 [P] + RewriteRule ^/api/0\.6/changeset/[0-9]+/download$ fcgi://127.0.0.1:<%= @cgimap_port %>$0 [P] +<% end -%> @@ -49,14 +66,14 @@ Header set X-Robots-Tag "noindex, nofollow" -/public> +/rails/public> Require all granted -/app/assets> +/rails/app/assets> Require all granted -/vendor/assets> +/rails/vendor/assets> Require all granted diff --git a/cookbooks/dev/templates/default/cgimap.environment.erb b/cookbooks/dev/templates/default/cgimap.environment.erb new file mode 100644 index 000000000..7fb1efde2 --- /dev/null +++ b/cookbooks/dev/templates/default/cgimap.environment.erb @@ -0,0 +1,10 @@ +# DO NOT EDIT - This file is being maintained by Chef + +CGIMAP_PORT="<%= @cgimap_port %>" +CGIMAP_DBPORT="<%= @database_port %>" +CGIMAP_DBNAME="<%= @database_name %>" +CGIMAP_USERNAME="apis" +CGIMAP_LOGFILE="<%= @rails_directory %>/log/cgimap.log" +CGIMAP_MEMCACHE="127.0.0.1" +CGIMAP_RATELIMIT="204800" +CGIMAP_MAXDEBT="250" -- 2.39.5