sunetdrive/templates/script/create_folders_in_fullnode_buckets.erb.sh

61 lines
2 KiB
Bash
Raw Permalink Normal View History

2023-02-13 09:44:56 +00:00
#!/bin/bash
customer="<%= @customer %>"
environment="<%= @environment %>"
eppn_suffix="<%= @eppn_suffix %>"
include_userbuckets="<%= @include_userbuckets %>"
2023-02-13 09:44:56 +00:00
container="nextcloud_app_1"
yq="/usr/local/bin/yq"
if ! [[ -x ${yq} ]]; then
2024-06-26 07:58:18 +00:00
pip install yq
2023-02-13 09:44:56 +00:00
fi
declare -a directories
if [[ -n ${1} ]]; then
2024-06-26 07:58:18 +00:00
directories=("${@}")
2023-02-13 09:44:56 +00:00
else
2024-06-26 07:58:18 +00:00
directories+=("Arbetsmaterial")
directories+=("Bevarande")
directories+=("Gallringsbart")
2023-02-13 09:44:56 +00:00
fi
2023-02-13 09:44:56 +00:00
olddir="${PWD}"
tempdir=$(mktemp -d)
dirty=0
primary=''
declare -a users=( 'admin' )
2023-02-13 09:44:56 +00:00
cd "${tempdir}" || echo "Could not cd to tempdir"
2024-06-26 07:58:18 +00:00
declare -a projects=( $("${yq}" -r '.project_mapping.'"${customer}"'.'"${environment}"'.assigned | "\(.[].project)"' /etc/hiera/data/common.yaml) )
if [[ "${include_userbuckets}" == "true" ]]; then
2024-06-26 07:58:18 +00:00
primary=$("${yq}" -r '.project_mapping.'"${customer}"'.'"${environment}"'.primary_project' /etc/hiera/data/common.yaml)
projects+=( "${primary}" )
fi
for project in "${projects[@]}"; do
2024-06-26 07:58:18 +00:00
for bucket in $(rclone lsd "${project}:" | awk '{print $NF}' | grep -E -v '^primary'); do
count=$(rclone size --json "${project}:${bucket}" | jq -r .count)
if [[ ${count} -gt 0 ]]; then
echo "Skipping ${project}:${bucket} because it has stuff in it already"
continue
fi
for directory in "${directories[@]}"; do
dirty=1
if [[ -n ${primary} ]] && [[ ${project} == "${primary}" ]] ; then
2023-03-13 11:49:03 +00:00
user=$(echo "${bucket}" | awk -F '-' '{print $1}')
users+=( "${user}@${eppn_suffix}" )
fi
2024-06-26 07:58:18 +00:00
echo "Creating ${project}:${bucket}/${directory} because it looks nice and empty"
temp="README.md"
echo "**${directory}**" >"${temp}"
echo "Var god lämna kvar denna fil/Please leave this file" >>"${temp}"
rclone --no-traverse move "${temp}" "${project}:${bucket}/${directory}"
done
done
2023-02-13 09:44:56 +00:00
done
cd "${olddir}" || echo "could not cd to home dir"
rmdir "${tempdir}"
if [[ ${dirty} -gt 0 ]]; then
for user in "${users[@]}"; do
ssh -t "node3.$(hostname -d)" -l script -i .ssh/id_script "sudo /usr/local/bin/occ ${container} files:scan ${user}"
done
2023-02-13 09:44:56 +00:00
fi