Add support for using cgimap with dev api instances
authorTom Hughes <tom@compton.nu>
Sun, 16 Sep 2018 17:27:37 +0000 (18:27 +0100)
committerTom Hughes <tom@compton.nu>
Sun, 16 Sep 2018 19:25:33 +0000 (20:25 +0100)
cookbooks/dev/recipes/default.rb
cookbooks/dev/templates/default/apache.rails.erb
cookbooks/dev/templates/default/cgimap.environment.erb [new file with mode: 0644]

index c93176e..999cc4b 100644 (file)
@@ -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
index c8f5f77..edd8a94 100644 (file)
@@ -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 %>
         <Files crossdomain.xml>
                 ForceType text/x-cross-domain-policy
         </Files>
+<% if @cgimap_enabled -%>
+
+        # Pass authentication related headers to cgimap
+        <Location />
+                CGIPassAuth On
+        </Location>
+
+        # 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 -%>
 </VirtualHost>
 
 <VirtualHost *:80>
         Header set X-Robots-Tag "noindex, nofollow"
 </VirtualHost>
 
-<Directory /srv/<%= @name %>/public>
+<Directory /srv/<%= @name %>/rails/public>
         Require all granted
 </Directory>
 
-<Directory /srv/<%= @name %>/app/assets>
+<Directory /srv/<%= @name %>/rails/app/assets>
         Require all granted
 </Directory>
 
-<Directory /srv/<%= @name %>/vendor/assets>
+<Directory /srv/<%= @name %>/rails/vendor/assets>
         Require all granted
 </Directory>
diff --git a/cookbooks/dev/templates/default/cgimap.environment.erb b/cookbooks/dev/templates/default/cgimap.environment.erb
new file mode 100644 (file)
index 0000000..7fb1efd
--- /dev/null
@@ -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"