Created
November 2, 2024 05:02
-
-
Save lexoyo/ab3ce801e659473b02437d0275d85ab7 to your computer and use it in GitHub Desktop.
Scripts to create gitlab projects out of bare repositories I have on the server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script pour lancer la console GitLab et exécuter le script Ruby | |
# Vérifier si le script est exécuté en tant que root | |
if [ "$EUID" -ne 0 ]; then | |
echo "Veuillez exécuter ce script en tant que root." | |
exit | |
fi | |
# Vérifier si le chemin du dépôt est passé en paramètre | |
# if [ -z "$1" ]; then | |
# echo "Veuillez fournir le chemin de l'ancien dépôt en paramètre." | |
# exit | |
# fi | |
old_repo_path="$1" | |
# Lancer la console GitLab | |
/opt/gitlab/bin/gitlab-rails console <<EOF | |
# Charger le script Ruby dans la console | |
$(cat <<'RUBY' | |
# Étape 1: Créer un projet dans GitLab | |
def create_project(project_name, namespace_id, creator_id) | |
project = Project.create!( | |
name: project_name, | |
path: project_name.parameterize, | |
namespace_id: namespace_id, | |
creator_id: creator_id, | |
visibility_level: Gitlab::VisibilityLevel::INTERNAL | |
) | |
puts "Projet créé avec ID : #{project.id}" | |
project | |
rescue ActiveRecord::RecordInvalid => e | |
puts "Erreur de validation lors de la création du projet : #{e.record.errors.full_messages}" | |
nil | |
end | |
# Étape 2: Calculer le chemin hashé du dépôt pour un projet donné | |
require 'digest' | |
def calculate_hashed_path(project_id) | |
hash = Digest::SHA256.hexdigest(project_id.to_s) | |
hashed_path = "/var/opt/gitlab/git-data/repositories/@hashed/#{hash[0..1]}/#{hash[2..3]}/#{hash}.git" | |
puts "Chemin de dépôt hashé : #{hashed_path}" | |
hashed_path | |
end | |
# Étape 3: Créer le répertoire parent du nouveau dépôt si nécessaire | |
def create_parent_directory(new_repo_path) | |
parent_directory = File.dirname(new_repo_path) | |
unless Dir.exist?(parent_directory) | |
FileUtils.mkdir_p(parent_directory) | |
puts "Répertoire parent créé : #{parent_directory}" | |
end | |
end | |
# Étape 4: Déplacer le dépôt vers le nouveau chemin | |
def move_repository(old_repo_path, new_repo_path) | |
if Dir.exist?(old_repo_path) | |
if !Dir.exist?(new_repo_path) | |
FileUtils.mv(old_repo_path, new_repo_path) | |
puts "Dépôt déplacé de #{old_repo_path} vers #{new_repo_path}" | |
else | |
puts "Erreur : Le chemin cible #{new_repo_path} existe déjà." | |
end | |
else | |
puts "Erreur : L'ancien chemin #{old_repo_path} n'existe pas." | |
end | |
rescue Errno::EACCES => e | |
puts "Erreur de permission lors du déplacement : #{e.message}" | |
rescue Errno::ENOENT => e | |
puts "Erreur : Fichier ou répertoire introuvable - #{e.message}" | |
end | |
# Étape 5: Extraire le nom du projet à partir des variables d'environnement | |
def extract_project_name(repo_path) | |
#env_file_content = `git --git-dir=#{repo_path} show main:.env` | |
#env_file_content.each_line do |line| | |
# if line.start_with?("PROD_DOMAIN") | |
# return line.split("=").last.strip | |
# end | |
#end | |
"project_#{File.basename(repo_path)}" | |
rescue => e | |
puts "Erreur lors de l'extraction du nom du projet : #{e.message}" | |
"default_project_name_#{File.basename(repo_path)}" | |
end | |
# Exemple d'utilisation des fonctions | |
namespace_id = 37 # ID du namespace 'recovery' | |
creator_id = 34 # ID de l'utilisateur 'lexoyo' | |
# Passer l'ancien chemin du dépôt en tant que paramètre | |
old_repo_path = "${old_repo_path}" | |
# Extraire le nom du projet à partir des variables d'environnement | |
project_name = extract_project_name(old_repo_path) | |
# 1. Créer le projet | |
project = create_project(project_name, namespace_id, creator_id) | |
if project | |
# 2. Calculer le chemin hashé | |
new_repo_path = calculate_hashed_path(project.id) | |
# 3. Créer le répertoire parent | |
create_parent_directory(new_repo_path) | |
# 4. Déplacer le dépôt vers le nouveau chemin | |
move_repository(old_repo_path, new_repo_path) | |
end | |
RUBY | |
) | |
EOF |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Temporary file to store unprocessed paths | |
temp_file=$(mktemp) | |
# File to store successfully processed paths | |
processed_file="processed.txt" | |
# Ensure processed file exists | |
touch "$processed_file" | |
# Loop through each line in the paths.txt file | |
while IFS= read -r old_repo_path; do | |
# Echo the file being processed | |
echo "Processing: $old_repo_path" | |
# Replace ${old_repo_path} in the script and run it | |
if sed "s|\${old_repo_path}|$old_repo_path|g" ./import-repo.sh | bash; then | |
# If the script runs successfully, add the path to the processed file | |
echo "$old_repo_path" >> "$processed_file" | |
echo "Processed successfully: $old_repo_path" | |
else | |
# If the script fails, add this path to the temp file | |
echo "$old_repo_path" >> "$temp_file" | |
echo "Failed to process: $old_repo_path" | |
fi | |
# Wait 30 seconds before processing the next path | |
sleep 30 | |
done < paths.txt | |
# Overwrite paths.txt with unprocessed paths | |
mv "$temp_file" paths.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/var/opt/gitlab/git-data/repositories/@hashed/28/58/2858dcd1057d3eae7f7d5f782167e261153c01551450a628cee722509f6529.git | |
/var/opt/gitlab/git-data/repositories/@hashed/76/a5/76a50887d8f1c2e9301755428990ad819ee21c25b43215cf524541e0503269.git | |
/var/opt/gitlab/git-data/repositories/@hashed/e7/f6/e7f6c011776e8db7cd330b54174fd76f7d16b612387a5ffcfb81e6f0919683.git | |
/var/opt/gitlab/git-data/repositories/@hashed/4e/07/4e07408562bedb8b60ce05c1decfe3ad16b730967de01f640b7e4729b49fce.git | |
/var/opt/gitlab/git-data/repositories/@hashed/4e/c9/4ec9599fc203d176a301536c2e091a19bc8527b255bd6818810a42c5fed14a.git | |
/var/opt/gitlab/git-data/repositories/@hashed/c8/37/c837649cce43f2729138e72cc3157057ac82599a59be72765a477f22d14a54.git | |
/var/opt/gitlab/git-data/repositories/@hashed/da/4e/da4ea2a5506f2693eae190d9361f31793c98a1adade51d93533a6f520ace1c.git |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment