GitLab: Terminal CMDS
Posted: 2024 Jun 18, 13:51
- Starting a Rails console session:
Code: Select all
sudo gitlab-rails console -e productionCode: Select all
docker exec -it <container-id> gitlab-rails consoleCode: Select all
sudo -u git -H bundle exec rails console -e productionCode: Select all
# find the pod
kubectl get pods --namespace <namespace> -lapp=toolbox
# open the Rails console
kubectl exec -it -c toolbox <toolbox-pod-name> -- gitlab-rails console
- Unblock / Re-activate a user:
Code: Select all
gitlab-rails console -e productionCode: Select all
user = User.find_by_email("[email protected]")
user.state = "active"
user.save
exit
- Rails reset root password:
Code: Select all
sudo gitlab-rails console -e productionCode: Select all
user = User.find_by(username: 'root')Code: Select all
user.password = 'NewStrongPassword123!'
user.password_confirmation = 'NewStrongPassword123!'
user.save!
- Manual create a backup:
Code: Select all
gitlab-backup create GZIP_RSYNCABLE=yes- Regenerate Health Check Token via gitlab-rails console and retrieve the New Token:
Code: Select all
sudo gitlab-rails runner "ApplicationSetting.current.update!(health_check_access_token: SecureRandom.hex(20)); puts ApplicationSetting.current.health_check_access_token"- Manually set a Specific Health Check Token:
Code: Select all
sudo gitlab-rails runner "ApplicationSetting.current.update!(health_check_access_token: 'YOUR_CUSTOM_TOKEN')"- Force send mail confirmation code from CLI:
Code: Select all
sudo gitlab-rails console -e productionCode: Select all
user = User.find_by(email: '[email protected]')
user.send_confirmation_instructions
- Test send mail configuration from CLI:
Code: Select all
sudo gitlab-rails console -e productionCode: Select all
Notify.test_email('[email protected]', 'Message Subject', 'Message Body').deliver_now- Change password for username from CLI:
Code: Select all
sudo gitlab-rails console -e productionCode: Select all
user = User.find_by_username('testuser)
user.password = 'YourNewSecurePassword'
user.password_confirmation = 'YourNewSecurePassword'
user.save!
- PSQL Check for background migrations:
Code: Select all
sudo gitlab-psql -c "SELECT job_class_name, table_name, column_name, job_arguments FROM batched_background_migrations WHERE status NOT IN(3, 6);"
- Rails query check pending background migrations:
Code: Select all
sudo gitlab-rails runner -e production 'puts Gitlab::BackgroundMigration.remaining'
sudo gitlab-rails runner -e production 'puts Gitlab::Database::BackgroundMigration::BatchedMigration.queued.count'
- Rails query check for failed background migrations:
Code: Select all
sudo gitlab-rails runner -e production 'puts Gitlab::Database::BackgroundMigration::BatchedMigration.with_status(:failed).count'
- Rails query get all repos in a CSV file based on Last Activity in the last 6 months:
Code: Select all
sudo gitlab-rails console -e productionCode: Select all
require 'csv'
SINCE_DATE = 6.month.ago
OUTPUT_FILE = '/tmp/active_projects_with_owners_6month_ago.csv'
CSV.open(OUTPUT_FILE, 'w') do |csv|
csv << ['Namespace', 'Project', 'URL', 'Last Activity', 'Last Commit', 'Owners']
Project.includes(:namespace, :project_members)
.where('last_activity_at >= ?', SINCE_DATE)
.where(archived: false)
.find_each do |p|
begin
repo_url = "#{Gitlab.config.gitlab.url}/#{p.full_path}"
namespace_path = p.namespace.full_path
last_activity = p.last_activity_at
# Last commit date
last_commit_date = if p.repository.exists? && p.default_branch.present?
p.repository.commits(p.default_branch, limit: 1).first&.committed_date
else
'N/A'
end
# Project-level owners
project_owners = p.project_members
.select { |m| m.access_level == Gitlab::Access::OWNER }
.map { |m| m.user&.email }
# Group-level owners (recursive)
group_owners = []
ns = p.namespace
while ns && ns.type == 'Group'
owners = ns.group_members
.select { |gm| gm.access_level == Gitlab::Access::OWNER }
.map { |gm| gm.user&.email }
group_owners.concat(owners)
ns = ns.parent
end
all_owners = (project_owners + group_owners).compact.uniq
csv << [namespace_path, p.path, repo_url, last_activity, last_commit_date, all_owners.join('; ')]
puts "#{namespace_path}/#{p.path} | Owners: #{all_owners.any? ? all_owners.join('; ') : '(none)'}"
rescue => e
puts " /!\ Error on project #{p.full_path}: #{e.message}"
end
end # <- closes find_each loop
end # <- closes CSV.open
puts " Report saved to #{OUTPUT_FILE}"
- Rails query Send e-mail to all active users in GitLab:
Code: Select all
sudo gitlab-rails console -e productionCode: Select all
User.where(state: 'active').find_each do |user|
begin
Notify.test_email(
user.email,
'Notice from GitLab Admin',
"Hello #{user.name},\n\nThis is a maintenance notice from your GitLab administrator.\n\nRegards,\nGitLab Admin"
).deliver_later # use deliver_later to enqueue (recommended)
puts "Enqueued email to #{user.email}"
rescue => e
puts "/!\ Failed to send to #{user.email}: #{e.message}"
end
end