From 3c815fafd5be1dffd058c497577c2ed23e421d40 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sat, 15 Sep 2018 12:44:51 +0100 Subject: [PATCH] Rework cplanet file locations --- cookbooks/cplanet/attributes/default.rb | 1 + cookbooks/cplanet/recipes/default.rb | 57 ++++++++----------- ...date-planet.erb => planet-update-file.erb} | 7 +-- .../templates/default/planet-update.cron.erb | 3 + .../templates/default/planet-update.erb | 19 +++++++ .../default/planet-update.logrotate.erb | 5 ++ .../cplanet/templates/default/update.erb | 30 ---------- 7 files changed, 56 insertions(+), 66 deletions(-) create mode 100644 cookbooks/cplanet/attributes/default.rb rename cookbooks/cplanet/templates/default/{update-planet.erb => planet-update-file.erb} (77%) create mode 100644 cookbooks/cplanet/templates/default/planet-update.cron.erb create mode 100644 cookbooks/cplanet/templates/default/planet-update.erb create mode 100644 cookbooks/cplanet/templates/default/planet-update.logrotate.erb delete mode 100644 cookbooks/cplanet/templates/default/update.erb diff --git a/cookbooks/cplanet/attributes/default.rb b/cookbooks/cplanet/attributes/default.rb new file mode 100644 index 000000000..e1e8c3836 --- /dev/null +++ b/cookbooks/cplanet/attributes/default.rb @@ -0,0 +1 @@ +default[:cplanet][:jobs] = {} diff --git a/cookbooks/cplanet/recipes/default.rb b/cookbooks/cplanet/recipes/default.rb index 074671e7a..1d3b6499c 100644 --- a/cookbooks/cplanet/recipes/default.rb +++ b/cookbooks/cplanet/recipes/default.rb @@ -17,55 +17,48 @@ # limitations under the License. # -user = "cplanet" -basedir = "/srv/cplanet" -planet_source = "https://planet.openstreetmap.org/pbf/planet-latest.osm.pbf" - package %w[ pyosmium ] -directory basedir do +template "/usr/local/bin/planet-update" do + source "planet-update.erb" owner "root" group "root" mode 0o755 end -%w[bin jobs log].each do |dir| - directory "#{basedir}/#{dir}" do - owner "root" - group "root" - mode 0o755 - end +template "/usr/local/bin/planet-update-file" do + source "planet-update-file.erb" + owner "root" + group "root" + mode 0o755 end -directory "#{basedir}/planet" do - owner user - group user +directory "/var/lib/planet" do + owner "planet" + group "planet" mode 0o755 end -%w[update update-planet].each do |file| - template "#{basedir}/bin/#{file}" do - source "#{file}.erb" - owner "root" - group "root" - mode 0o755 - variables :basedir => basedir, :user => user - end +remote_file "/var/lib/planet/planet.pbf" do + action :create_if_missing + source "https://planet.openstreetmap.org/pbf/planet-latest.osm.pbf" + owner "planet" + group "planet" + mode 0o644 end -remote_file "#{basedir}/planet/planet.pbf" do - action :create_if_missing - source planet_source - owner user - group user +template "/etc/cron.d/planet-update" do + source "planet-update.cron.erb" + owner "root" + group "root" mode 0o644 end -cron "update-planet" do - hour 1 - minute 17 - user "root" - command "#{basedir}/bin/update" +template "/etc/logrotate.d/planet-update" do + source "planet-update.logrotate.erb" + owner "root" + group "root" + mode 0o644 end diff --git a/cookbooks/cplanet/templates/default/update-planet.erb b/cookbooks/cplanet/templates/default/planet-update-file.erb similarity index 77% rename from cookbooks/cplanet/templates/default/update-planet.erb rename to cookbooks/cplanet/templates/default/planet-update-file.erb index 6c77eafa6..36786e232 100644 --- a/cookbooks/cplanet/templates/default/update-planet.erb +++ b/cookbooks/cplanet/templates/default/planet-update-file.erb @@ -4,18 +4,18 @@ # setup -PLANETDIR="<%= @basedir %>/planet" +PLANETDIR="/var/lib/planet" PLANETPREV="${PLANETDIR}/planet-previous.pbf" PLANETCURR="${PLANETDIR}/planet.pbf" PLANETNEW="${PLANETDIR}/planet-new.pbf" PLANETTMP="${PLANETDIR}/planet-tmp.pbf" -pyosmium-up-to-date -v -o $PLANETNEW $PLANETCURR +pyosmium-up-to-date -v -o "$PLANETNEW" "$PLANETCURR" retval=$? while [ $retval -eq 1 ]; do mv "$PLANETNEW" "$PLANETTMP" - pyosmium-up-to-date -v -o $PLANETNEW $PLANETTMP + pyosmium-up-to-date -v -o "$PLANETNEW" "$PLANETTMP" retval=$? rm "$PLANETTMP" done @@ -28,4 +28,3 @@ fi mv "$PLANETCURR" "$PLANETPREV" mv "$PLANETNEW" "$PLANETCURR" - diff --git a/cookbooks/cplanet/templates/default/planet-update.cron.erb b/cookbooks/cplanet/templates/default/planet-update.cron.erb new file mode 100644 index 000000000..a12c53ec5 --- /dev/null +++ b/cookbooks/cplanet/templates/default/planet-update.cron.erb @@ -0,0 +1,3 @@ +# DO NOT EDIT - This file is being maintained by Chef + +17 1 * * * planet /usr/local/bin/planet-update diff --git a/cookbooks/cplanet/templates/default/planet-update.erb b/cookbooks/cplanet/templates/default/planet-update.erb new file mode 100644 index 000000000..683214e18 --- /dev/null +++ b/cookbooks/cplanet/templates/default/planet-update.erb @@ -0,0 +1,19 @@ +#!/bin/sh + +# DO NOT EDIT - This file is being maintained by Chef + +exec > /var/log/planet-update.log 2>&1 + +echo "Updating planet file..." + +runuser -u planet -- /usr/local/bin/planet-update-file + +echo "Running jobs..." +<% node[:cplanet][:jobs].each do |job| -%> + +echo "Running '<%= job[:command] %>' as user '<%= job[:user] %>'..." + +runuser -u "<%= job[:user] %>" -- "<%= job[:command] %>" +<% end -%> + +echo "Done." diff --git a/cookbooks/cplanet/templates/default/planet-update.logrotate.erb b/cookbooks/cplanet/templates/default/planet-update.logrotate.erb new file mode 100644 index 000000000..a3ca8acec --- /dev/null +++ b/cookbooks/cplanet/templates/default/planet-update.logrotate.erb @@ -0,0 +1,5 @@ +/var/log/planet-update.log { + compress + notifempty + missingok +} diff --git a/cookbooks/cplanet/templates/default/update.erb b/cookbooks/cplanet/templates/default/update.erb deleted file mode 100644 index c1b1b90bb..000000000 --- a/cookbooks/cplanet/templates/default/update.erb +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/sh - -# DO NOT EDIT - This file is being maintained by Chef - -USER="<%= @user %>" -DIR="<%= @basedir %>" -LOGDIR="${DIR}/log" - -# start logging - -exec >"${LOGDIR}/$(date +%Y%m%d-%H%M).log" 2>&1 - -echo "Updating planet file..." -sudo -u "$USER" "$DIR/bin/update-planet" - -echo "Running jobs..." -if [ -d "$DIR/jobs" ]; then - for job in `find "$DIR/jobs" -type f -executable | sort`; do - user=`stat '--format=%U' $job` - echo "Running '$job' as user '$user'..." - su -c "$job" "$user" - done -fi - -echo -echo "Expire old logs..." -find "${LOGDIR}" -mtime +28 -delete - -echo "Done." - -- 2.43.2