diff --git a/data/common/common.yaml b/data/common/common.yaml --- a/data/common/common.yaml +++ b/data/common/common.yaml @@ -2554,7 +2554,17 @@ swh::deploy::worker::loader_opam::user: swhworker swh::deploy::worker::loader_opam::group: swhworker swh::deploy::worker::opam::root_directory: /tmp/opam/ + +# Keep opam.ocaml.org entry as the first one which is the default repository +swh::deploy::worker::opam::default_instance::name: opam.ocaml.org +swh::deploy::worker::opam::default_instance::url: https://opam.ocaml.org + swh::deploy::worker::opam::instances: + coq.inria.fr: https://coq.inria.fr/opam/released + ocamlbench-repo: https://github.com/OCamlPro/ocamlbench-repo.git + +# deprecated +swh::deploy::worker::opam::deprecated::instances: opam.ocaml.org: https://opam.ocaml.org coq.inria.fr: https://coq.inria.fr/opam/released ocamlbench-repo: https://github.com/OCamlPro/ocamlbench-repo.git diff --git a/site-modules/profile/manifests/swh/deploy/worker/loader_opam.pp b/site-modules/profile/manifests/swh/deploy/worker/loader_opam.pp --- a/site-modules/profile/manifests/swh/deploy/worker/loader_opam.pp +++ b/site-modules/profile/manifests/swh/deploy/worker/loader_opam.pp @@ -21,12 +21,23 @@ ], } - $opam_instances = lookup('swh::deploy::worker::opam::instances') - $template_path = "profile/swh/deploy/loader_opam" - $opam_root = lookup('swh::deploy::worker::opam::root_directory') $opam_manage_shared_state = "opam-manage-shared-state" $opam_manage_state_script = "/usr/local/bin/${opam_manage_shared_state}.sh" + + # Install more robust opam root folder maintenance tooling + $default_instance_name = lookup('swh::deploy::worker::opam::default_instance::name') + $default_instance_url = lookup('swh::deploy::worker::opam::default_instance::url') + $other_instances = lookup('swh::deploy::worker::opam::instances') + + $template_path = "profile/swh/deploy/loader_opam" + + # Templates uses variables + # - $user + # - $group + # - $default_instance_name + # - $default_instance_url + # - $other_instances file {$opam_manage_state_script: ensure => 'file', owner => $user, @@ -35,25 +46,39 @@ content => template("${template_path}/${opam_manage_shared_state}.sh.erb"), } + # Templates uses variables + # - $user + # - $group + # - $runparts_systemd_directory + # - $opam_manage_service_name + # - $opam_manage_state_script + + ::systemd::timer { "${opam_manage_shared_state}.timer": + timer_content => template("${template_path}/${opam_manage_shared_state}.timer.erb"), + service_content => template("${template_path}/${opam_manage_shared_state}.service.erb"), + enable => true, + require => [ + Package[$packages], + File[$runparts_systemd_directory], + ], + } + + ################################################################### + # Clean up old opam root maintenance tooling (to drop once applied) + ################################################################### + + $opam_instances = lookup('swh::deploy::worker::opam::deprecated::instances') + each ( $opam_instances ) | $instance, $instance_url | { - $opam_manage_service_name = "${$opam_manage_shared_state}-${instance}" - $opam_manage_shared_state_timer_name = "${opam_manage_service_name}.timer" - - # Templates uses variables - # - $user - # - $group - # - $opam_root - # - $opam_manage_service_name - # - $command - - ::systemd::timer { $opam_manage_shared_state_timer_name: - timer_content => template("${template_path}/${opam_manage_shared_state}.timer.erb"), - service_content => template("${template_path}/${opam_manage_shared_state}.service.erb"), - enable => true, - require => [ - Package[$packages], - File[$opam_manage_state_script], - ], + $opam_manage_service_name = "${opam_manage_shared_state}-${instance}" + $opam_manage_service_path = "${default_systemd_path}/${$opam_manage_service_name}.service" + $opam_manage_shared_state_timer_name = $opam_manage_service_name + $opam_manage_shared_state_timer_path = "${default_systemd_path}/${opam_manage_shared_state_timer_name}.timer" + + # deactivate timer + ::systemd::timer { "${opam_manage_shared_state_timer_name}.timer": + enable => false, + ensure => absent, } } diff --git a/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.service.erb b/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.service.erb --- a/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.service.erb +++ b/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.service.erb @@ -3,7 +3,7 @@ # Changes will be overwritten [Unit] -Description=Software Heritage Manage OPAM shared state (<%= @instance %>) +Description=Software Heritage Manage OPAM shared state Requires=network.target After=network.target @@ -11,7 +11,7 @@ User=<%= @user %> Group=<%= @group %> Type=simple -ExecStart=/usr/local/bin/opam-manage-shared-state.sh <%= @instance %> <%= @instance_url %> +ExecStart=<%= @opam_manage_state_script %> [Install] WantedBy=multi-user.target diff --git a/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.sh.erb b/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.sh.erb --- a/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.sh.erb +++ b/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.sh.erb @@ -3,20 +3,49 @@ # Managed by puppet class profile::swh::deploy::worker::loader_opam # Changes will be overwritten -CMD=/usr/bin/opam +# This script is in charge of bootstraping the opam root folder with the default opam +# repository. + +set -e + ROOT_DIR=<%= @opam_root %> -DEFAULT_INSTANCE="opam.ocaml.org" -INSTANCE=$1 -INSTANCE_URL=$2 - -if [ $INSTANCE = $DEFAULT_INSTANCE ]; then - # Opam instance is considered the main instance so we must initialize the repository - # if not present, otherwise we update it - ( $CMD repo --all --root $ROOT_DIR | grep $INSTANCE_URL && $CMD update --root $ROOT_DIR ) \ - || $CMD init --reinit --bare --no-setup --root $ROOT_DIR $INSTANCE $INSTANCE_URL -else - # Other instances, if present should be updated. If not present, they should be added - # to the main root_dir as extra instances - ( $CMD repo --all --root $ROOT_DIR | grep $INSTANCE_URL && $CMD update --root $ROOT_DIR ) \ - || $CMD repository add --root $ROOT_DIR $INSTANCE $INSTANCE_URL -fi +CMD=/usr/bin/opam + +function init_or_update_opam_root () { + # Ensure opam root directory exists and references the main opam repository as default + # instance. If not, create it from scratch. If properly configured, update the opam + # repository + + instance_name=$1 + instance_url=$2 + + if $CMD repo --all --root $ROOT_DIR | grep -qx $instance_url; then + $CMD update --root $ROOT_DIR + else + $CMD init --reinit --bare --no-setup --root $ROOT_DIR $instance_name $instance_url + fi +} + +function init_or_update_opam_instance () { + # Ensure opam root directory exists and references the main opam repository as default + # instance. If not, create it from scratch. If properly configured, update the opam + # repository. + + # Note that This assumes that the function init_or_update_opam_root already got called + # prior to calling this one. + + instance_name=$1 + instance_url=$2 + + if $CMD repo --all --root $ROOT_DIR | grep -qx $instance_url; then + $CMD update --root $ROOT_DIR + else + $CMD repository add --root $ROOT_DIR --all-switches $instance_name $instance_url + fi +} + +init_or_update_opam_root <%= @default_instance_name %> <%= @default_instance_url %> + +<% @other_instances.each do |instance_name, instance_url| %> +init_or_update_opam_instance <%= instance_name %> <%= instance_url %> +<% end %> diff --git a/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.timer.erb b/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.timer.erb --- a/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.timer.erb +++ b/site-modules/profile/templates/swh/deploy/loader_opam/opam-manage-shared-state.timer.erb @@ -3,9 +3,12 @@ [Timer] OnCalendar=daily -AccuracySec=4h Persistent=true +# every ~4h +AccuracySec=4h +RandomizedDelaySec=1200 +FixedRandomDelay=true Unit=<%= @opam_manage_service_name %>.service [Unit] -Description=Software Heritage Manage OPAM shared state (<%= @instance %>) timer +Description=Software Heritage Manage OPAM shared state timer