From 120d68bd5f64a3789f997f4c7a7a39f83babe9d0 Mon Sep 17 00:00:00 2001
From: Gabriel Mazetto <gabriel@gitlab.com>
Date: Mon, 30 Sep 2024 17:56:02 +0000
Subject: [PATCH] Unified Backup: Update gitlab-backup-cli to have custom
 process title

---
 bin/gitlab-backup-cli                         |  2 +-
 .../lib/gitlab/backup/cli.rb                  | 15 ++++++
 .../backup/cli/commands/backup_subcommand.rb  |  2 +
 .../backup/cli/commands/restore_subcommand.rb |  2 +
 .../spec/gitlab/backup/cli_spec.rb            | 47 +++++++++++++++++++
 5 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/bin/gitlab-backup-cli b/bin/gitlab-backup-cli
index 37524e2766f5f..01c1105b36fca 100755
--- a/bin/gitlab-backup-cli
+++ b/bin/gitlab-backup-cli
@@ -11,4 +11,4 @@ GITLAB_PATH = File.expand_path('../', __dir__)
 require_relative '../config/boot'
 require 'gitlab/backup/cli'
 
-Gitlab::Backup::Cli::Runner.start(ARGV)
+Gitlab::Backup::Cli.start(ARGV)
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb
index 018fc0d0fa9b1..fa329c8365ac9 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli.rb
@@ -29,6 +29,21 @@ module Cli
 
       Error = Class.new(StandardError)
 
+      # Entrypoint for the application
+      # Run any initialization logic from here
+      def self.start(argv)
+        # Set a custom process name
+        update_process_title!
+
+        Gitlab::Backup::Cli::Runner.start(argv)
+      end
+
+      def self.update_process_title!(status_message = nil)
+        process_title = status_message ? "gitlab-backup-cli: #{status_message}" : "gitlab-backup-cli"
+
+        Process.setproctitle(process_title)
+      end
+
       def self.rails_environment!
         require File.join(GITLAB_PATH, 'config/application')
 
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/backup_subcommand.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/backup_subcommand.rb
index 8f949a439338f..010a94ad11c7f 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/backup_subcommand.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/backup_subcommand.rb
@@ -28,6 +28,8 @@ class BackupSubcommand < Command
 
           desc 'all', 'Creates a backup including repositories, database and local files'
           def all
+            Gitlab::Backup::Cli.update_process_title!('backup all')
+
             duration = measure_duration do
               Gitlab::Backup::Cli::Output.info("Initializing environment...")
               Gitlab::Backup::Cli.rails_environment!
diff --git a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/restore_subcommand.rb b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/restore_subcommand.rb
index 6cdc0fef23085..d8f569226871b 100644
--- a/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/restore_subcommand.rb
+++ b/gems/gitlab-backup-cli/lib/gitlab/backup/cli/commands/restore_subcommand.rb
@@ -9,6 +9,8 @@ class RestoreSubcommand < Command
 
           desc 'all BACKUP_ID', 'Restores a backup including repositories, database and local files'
           def all(backup_id)
+            Gitlab::Backup::Cli.update_process_title!("restore all from #{backup_id}")
+
             duration = measure_duration do
               Gitlab::Backup::Cli::Output.info("Initializing environment...")
               Gitlab::Backup::Cli.rails_environment!
diff --git a/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb b/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb
index db3aaeccb0512..b59c3bc9175f6 100644
--- a/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb
+++ b/gems/gitlab-backup-cli/spec/gitlab/backup/cli_spec.rb
@@ -1,7 +1,54 @@
 # frozen_string_literal: true
 
 RSpec.describe Gitlab::Backup::Cli do
+  subject(:cli) { described_class }
+
+  around do |example|
+    previous_title = get_process_title
+    example.run
+    Process.setproctitle(previous_title)
+  end
+
   it "has a version number" do
     expect(Gitlab::Backup::Cli::VERSION).not_to be nil
   end
+
+  describe '.start' do
+    it 'sets the process title', :silence_output do
+      cli.start([])
+
+      expect(get_process_title).to eq('gitlab-backup-cli')
+    end
+
+    it 'delegates to Runner.start' do
+      argv = ['version']
+
+      expect(Gitlab::Backup::Cli::Runner).to receive(:start).with(argv)
+
+      cli.start(argv)
+    end
+  end
+
+  describe '.update_process_title!' do
+    context 'without any parameters' do
+      it 'sets a process title to `gitlab-backup-cli`' do
+        cli.update_process_title!
+
+        expect(get_process_title).to eq('gitlab-backup-cli')
+      end
+    end
+
+    context 'with parameters' do
+      it 'sets a process title to `gitlab-backup-cli: ` including provided content' do
+        cli.update_process_title!('context info')
+
+        expect(get_process_title).to eq('gitlab-backup-cli: context info')
+      end
+    end
+  end
+
+  def get_process_title
+    ps = `ps -p #{Process.pid} -o command`
+    ps.split("\n").last.strip
+  end
 end
-- 
GitLab