2023-03-15 09:58:12 +01:00
# Shows help
default :
2023-03-25 14:40:07 +01:00
@just --list --justfile { { justfile( ) } }
2023-03-15 09:58:12 +01:00
2023-11-20 15:29:06 +01:00
run_directory_path := justfile_directory( ) + "/run"
templates_directory_path := justfile_directory( ) + "/templates"
optimization_vars_files_file_path := run_directory_path + "/optimization-vars-files.state"
2023-03-15 09:58:12 +01:00
# Pulls external Ansible roles
2023-11-20 15:29:06 +01:00
roles : _requirements -yml
2023-03-25 15:15:08 +01:00
#!/usr/bin/env sh
2023-03-25 14:40:07 +01:00
if [ -x " $( command -v agru) " ] ; then
2024-08-12 12:51:17 +02:00
echo "[NOTE] This command just updates the roles, but if you want to update everything at once (playbook, roles, etc.) - use 'just update'"
2023-11-19 13:24:57 +01:00
agru -r { { justfile_directory( ) } } /requirements.yml
2023-03-25 14:40:07 +01:00
else
2024-08-12 12:45:28 +02:00
echo "[NOTE] You are using the standard ansible-galaxy tool to install roles, which is slow and lacks other features. We recommend installing the 'agru' tool to speed up the process: https://github.com/etkecc/agru#where-to-get"
2024-08-12 12:51:17 +02:00
echo "[NOTE] This command just updates the roles, but if you want to update everything at once (playbook, roles, etc.) - use 'just update'"
2023-11-19 13:24:57 +01:00
rm -rf roles/galaxy
2023-11-20 15:29:06 +01:00
ansible-galaxy install -r { { justfile_directory( ) } } /requirements.yml -p roles/galaxy/ --force
2023-03-25 14:40:07 +01:00
fi
2023-03-15 09:58:12 +01:00
2023-11-20 15:29:06 +01:00
# Optimizes the playbook based on stored configuration (vars.yml paths)
optimize-restore :
#!/usr/bin/env sh
2023-11-20 15:41:42 +01:00
if [ -f "{{ optimization_vars_files_file_path }}" ] ; then
2023-11-20 15:29:06 +01:00
just --justfile { { justfile( ) } } \
_optimize-for-var-paths \
2023-11-20 15:41:42 +01:00
$( cat { { optimization_vars_files_file_path } } )
2023-11-20 15:29:06 +01:00
else
echo " Cannot restore optimization state from a file ( $optimization_vars_files_file_path ), because it doesn't exist "
exit 1
fi
# Clears optimizations and resets the playbook to a non-optimized state
optimize-reset : && _clean_template_derived_files
#!/usr/bin/env sh
rm -f { { run_directory_path } } /*.srchash
rm -f { { optimization_vars_files_file_path } }
# Optimizes the playbook based on the enabled components for all hosts in the inventory
optimize inventory_path='inventory' : _reconfigure -for -all -hosts
_reconfigure-for-all-hosts inventory_path='inventory' :
#!/usr/bin/env sh
just --justfile { { justfile( ) } } \
_optimize-for-var-paths \
2023-11-22 14:03:27 +01:00
$( find { { inventory_path } } /host_vars/ -maxdepth 2 -name 'vars.yml' -exec readlink -f { } \; )
2023-11-20 15:29:06 +01:00
# Optimizes the playbook based on the enabled components for a single host
optimize-for-host hostname inventory_path='inventory' :
#!/usr/bin/env sh
just --justfile { { justfile( ) } } \
_optimize-for-var-paths \
2023-11-22 14:03:27 +01:00
$( find { { inventory_path } } /host_vars/{ { hostname } } -maxdepth 1 -name 'vars.yml' -exec readlink -f { } \; )
2023-11-20 15:29:06 +01:00
# Optimizes the playbook based on the enabled components found in the given vars.yml files
_optimize-for-var-paths +PATHS :
#!/usr/bin/env sh
echo '{{ PATHS }}' > { { optimization_vars_files_file_path } }
just --justfile { { justfile( ) } } _save_hash_for_file { { templates_directory_path } } /requirements.yml { { justfile_directory( ) } } /requirements.yml
just --justfile { { justfile( ) } } _save_hash_for_file { { templates_directory_path } } /setup.yml { { justfile_directory( ) } } /setup.yml
just --justfile { { justfile( ) } } _save_hash_for_file { { templates_directory_path } } /group_vars_mash_servers { { justfile_directory( ) } } /group_vars/mash_servers
/usr/bin/env python { { justfile_directory( ) } } /bin/optimize.py \
--vars-paths= '{{ PATHS }}' \
--src-requirements-yml-path= { { templates_directory_path } } /requirements.yml \
--dst-requirements-yml-path= { { justfile_directory( ) } } /requirements.yml \
--src-setup-yml-path= { { templates_directory_path } } /setup.yml \
--dst-setup-yml-path= { { justfile_directory( ) } } /setup.yml \
--src-group-vars-yml-path= { { templates_directory_path } } /group_vars_mash_servers \
--dst-group-vars-yml-path= { { justfile_directory( ) } } /group_vars/mash_servers
2024-08-12 12:45:28 +02:00
# Updates the playbook and installs the necessary Ansible roles pinned in requirements.yml. If a -u flag is passed, also updates the requirements.yml file with new role versions (if available)
update *flags : update -playbook -only
#!/usr/bin/env sh
if [ -x " $( command -v agru) " ] ; then
echo { { if flags = = "" { "Installing roles pinned in requirements.yml..." } else if flags = = "-u" { "Updating roles and pinning new versions in requirements.yml..." } else { "Unknown flags passed" } } }
agru -r { { templates_directory_path } } /requirements.yml { { flags } }
just --justfile { { justfile( ) } } versions
just --justfile { { justfile( ) } } opml
else
echo "[NOTE] You are using the standard ansible-galaxy tool to install roles, which is slow and lacks other features. We recommend installing the 'agru' tool to speed up the process: https://github.com/etkecc/agru#where-to-get"
echo "Installing roles..."
rm -rf roles/galaxy
ansible-galaxy install -r requirements.yml -p roles/galaxy/ --force
fi
# Updates the playbook without installing/updating Ansible roles
update-playbook-only :
@echo "Updating playbook..."
@git stash -q
@git pull -q
@-git stash pop -q
2023-03-25 14:41:54 +01:00
2023-03-15 09:58:12 +01:00
# Runs ansible-lint against all roles in the playbook
lint :
2023-03-25 14:40:07 +01:00
ansible-lint
2023-03-15 09:58:12 +01:00
2023-07-22 10:27:49 +02:00
# dumps an OPML file with extracted git feeds for roles
opml :
@echo "generating opml..."
@python bin/feeds.py . dump
2024-04-12 20:19:28 +02:00
# dumps versions of the components found in the roles to the VERSIONS.md file
versions :
@echo "generating versions..."
@python bin/versions.py
2023-03-15 09:58:12 +01:00
# Runs the playbook with --tags=install-all,start and optional arguments
install-all *extra_args : (run -tags "install -all , start " extra_args )
# Runs installation tasks for a single service
2023-03-20 13:45:27 +01:00
install-service service *extra_args :
2023-03-28 09:52:48 +02:00
just --justfile { { justfile( ) } } run \
--tags= install-{ { service } } ,start-group \
--extra-vars= group = { { service } } \
--extra-vars= devture_systemd_service_manager_service_restart_mode = one-by-one { { extra_args } }
2023-03-15 09:58:12 +01:00
# Runs the playbook with --tags=setup-all,start and optional arguments
setup-all *extra_args : (run -tags "setup -all , start " extra_args )
# Runs the playbook with the given list of arguments
2023-11-20 15:29:06 +01:00
run +extra_args : _requirements -yml _setup -yml _group -vars -mash -servers
2023-04-01 05:33:54 +02:00
ansible-playbook -i inventory/hosts setup.yml { { extra_args } }
2023-03-15 09:58:12 +01:00
# Runs the playbook with the given list of comma-separated tags and optional arguments
run-tags tags *extra_args :
2023-03-25 14:40:07 +01:00
just --justfile { { justfile( ) } } run --tags= { { tags } } { { extra_args } }
2023-03-15 09:58:12 +01:00
# Starts all services
start-all *extra_args : (run -tags "start -all " extra_args )
# Starts a specific service group
start-group group *extra_args :
2023-03-25 14:40:07 +01:00
@just --justfile { { justfile( ) } } run-tags start-group --extra-vars= "group={{ group }}" { { extra_args } }
2023-03-15 09:58:12 +01:00
# Stops all services
stop-all *extra_args : (run -tags "stop -all " extra_args )
# Stops a specific service group
stop-group group *extra_args :
2023-03-25 14:40:07 +01:00
@just --justfile { { justfile( ) } } run-tags stop-group --extra-vars= "group={{ group }}" { { extra_args } }
2023-11-19 13:24:57 +01:00
# Prepares the requirements.yml file
2023-11-20 15:29:06 +01:00
_requirements-yml :
@just --justfile { { justfile( ) } } _ensure_file_prepared { { templates_directory_path } } /requirements.yml { { justfile_directory( ) } } /requirements.yml
2023-11-19 14:25:28 +01:00
# Prepares the setup.yml file
2023-11-20 15:29:06 +01:00
_setup-yml :
@just --justfile { { justfile( ) } } _ensure_file_prepared { { templates_directory_path } } /setup.yml { { justfile_directory( ) } } /setup.yml
2023-11-20 10:05:18 +01:00
2023-11-20 10:07:31 +01:00
# Prepares the group_vars/mash_servers file
2023-11-20 15:29:06 +01:00
_group-vars-mash-servers :
@just --justfile { { justfile( ) } } _ensure_file_prepared { { templates_directory_path } } /group_vars_mash_servers { { justfile_directory( ) } } /group_vars/mash_servers
2023-11-20 10:07:31 +01:00
2023-11-20 10:05:18 +01:00
_ensure_file_prepared src_path dst_path :
2023-11-19 14:25:28 +01:00
#!/usr/bin/env sh
2023-11-20 15:29:06 +01:00
dst_file_name = $( basename "{{ dst_path }}" )
hash_path = { { run_directory_path } } "/" $dst_file_name ".srchash"
2023-11-20 10:05:18 +01:00
src_hash = $( md5sum { { src_path } } | cut -d ' ' -f 1)
if [ ! -f "{{ dst_path }}" ] || [ ! -f " $hash_path " ] ; then
cp { { src_path } } { { dst_path } }
echo $src_hash > $hash_path
else
current_hash = $( cat $hash_path )
if [ " $current_hash " != " $src_hash " ] ; then
cp { { src_path } } { { dst_path } }
echo $src_hash > $hash_path
2023-11-20 15:29:06 +01:00
2023-11-20 15:41:42 +01:00
if [ -f "{{ optimization_vars_files_file_path }}" ] ; then
2023-11-20 15:29:06 +01:00
just --justfile { { justfile( ) } } \
_optimize-for-var-paths \
2023-11-20 15:41:42 +01:00
$( cat { { optimization_vars_files_file_path } } )
2023-11-20 15:29:06 +01:00
fi
2023-11-20 10:05:18 +01:00
fi
2023-11-19 14:25:28 +01:00
fi
2023-11-20 15:29:06 +01:00
_save_hash_for_file src_path dst_path :
#!/usr/bin/env sh
dst_file_name = $( basename "{{ dst_path }}" )
hash_path = { { run_directory_path } } "/" $dst_file_name ".srchash"
src_hash = $( md5sum { { src_path } } | cut -d ' ' -f 1)
echo $src_hash > $hash_path
_clean_template_derived_files :
#!/usr/bin/env sh
if [ -f "{{ justfile_directory() }}/requirements.yml" ] ; then
rm { { justfile_directory( ) } } /requirements.yml
fi
if [ -f "{{ justfile_directory() }}/setup.yml" ] ; then
rm { { justfile_directory( ) } } /setup.yml
fi
if [ -f "{{ justfile_directory() }}/group_vars/mash_servers" ] ; then
rm { { justfile_directory( ) } } /group_vars/mash_servers
fi