Here's a Salt state that clones a git repo, and if it fails, deletes the target directory before retrying:
{% set repo_url = 'https://github.com/example/repo.git' %}
{% set repo_path = '/opt/myproject' %}
clone_project:
git.cloned:
- name: {{ repo_url }}
- target: {{ repo_path }}
- user: deploy
- branch: main
- require:
- pkg: git_package
# Cleanup and retry if clone fails
cmd.run:
- name: |
if [ $? -ne 0 ]; then
rm -rf {{ repo_path }}
salt-call state.apply clone_project
fi
- onlyif: test -d {{ repo_path }}
- require:
- git: clone_project
git_package:
pkg.installed:
- name: git
A more robust version with additional error handling:
{% set repo_url = 'https://github.com/example/repo.git' %}
{% set repo_path = '/opt/myproject' %}
ensure_repo_cloned:
pkg.installed:
- name: git
# First attempt to clone
git.cloned:
- name: {{ repo_url }}
- target: {{ repo_path }}
- user: deploy
- branch: main
- require:
- pkg: ensure_repo_cloned
# Cleanup and retry mechanism
cmd.run:
- name: |
# Check if clone failed
if [ ! -d "{{ repo_path }}/.git" ]; then
# Remove partial or failed clone
rm -rf {{ repo_path }}
# Create parent directory if not exists
mkdir -p $(dirname {{ repo_path }})
# Retry clone
git clone {{ repo_url }} {{ repo_path }}
# Check if retry succeeded
if [ $? -ne 0 ]; then
echo "Failed to clone repository after cleanup" >&2
exit 1
fi
fi
- onlyif: test ! -d {{ repo_path }}/.git
- require:
- git: ensure_repo_cloned
# Optional: Ensure correct ownership
file.directory:
- name: {{ repo_path }}
- user: deploy
- group: deploy
- recurse:
- user
- group
- require:
- cmd: ensure_repo_cloned
Key features of this state:
- Checks for
.git directory to confirm successful clone
- Removes target directory if clone fails
- Creates parent directory if needed
- Attempts manual git clone as a fallback
- Sets correct ownership after clone
More advanced version with logging:
{% set repo_url = 'https://github.com/example/repo.git' %}
{% set repo_path = '/opt/myproject' %}
{% set max_retries = 3 %}
clone_project:
pkg.installed:
- name: git
git.cloned:
- name: {{ repo_url }}
- target: {{ repo_path }}
- user: deploy
- branch: main
- require:
- pkg: clone_project
# Advanced retry with logging
cmd.run:
- name: |
retry_count=0
while [ $retry_count -lt {{ max_retries }} ]; do
# Check if clone exists and is valid
if [ ! -d "{{ repo_path }}/.git" ]; then
# Log attempt
echo "Retry $((retry_count+1)): Cloning {{ repo_url }}" >> /var/log/salt/repo_clone.log
# Remove existing directory
rm -rf {{ repo_path }}
# Attempt clone
git clone {{ repo_url }} {{ repo_path }}
# Check clone success
if [ $? -eq 0 ] && [ -d "{{ repo_path }}/.git" ]; then
echo "Clone successful on attempt $((retry_count+1))" >> /var/log/salt/repo_clone.log
exit 0
fi
else
# Valid clone exists
exit 0
fi
# Increment retry counter
retry_count=$((retry_count+1))
# Wait before next retry
sleep 10
done
# Final failure
echo "Failed to clone {{ repo_url }} after {{ max_retries }} attempts" >> /var/log/salt/repo_clone.log
exit 1
- onlyif: test ! -d {{ repo_path }}/.git
- require:
- git: clone_project
# Ensure correct permissions
file.directory:
- name: {{ repo_path }}
- user: deploy
- group: deploy
- recurse:
- user
- group
- require:
- cmd: clone_project