Skip to content

Instantly share code, notes, and snippets.

@hroncok
Last active June 30, 2017 20:22
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hroncok/4478541 to your computer and use it in GitHub Desktop.
This Bash script helps you to replace mirrorlist with baseurl in your mock config files. It selects the first item in the mirrorlist (should be good enough). To update the mirrors, revert config from backup (.bak) and run again.
#!/usr/bin/env bash
# Copyright (c) 2013, Miro Hrončok <miro@hroncok.cz>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
# =============================================================================
#
# This script helps you to replace mirrorlist with baseurl in your mock config
# files. It selects the first item in the mirrorlist (should be good enough).
# To update the mirrors, revert config from backup (.bak) and run again.
if [ -z "$1" ]; then
echo "Usage: $0 list of files to modify"
echo "For example: $0 /etc/mock/*.cfg"
fi
for FILE in "$@"; do
echo -n "$FILE: "
if [ ! -f "$FILE" ]; then
echo "Not a file"
continue
fi
if [ ! -w "$FILE" ]; then
echo "Cannot write"
continue
fi
BAK=".bak"
grep '^mirrorlist=' "$FILE" | while read URL; do
URL="${URL##mirrorlist=}"
wget -q "$URL" -O /tmp/m2b-mirrorlist
NEWURL="`head -2 /tmp/m2b-mirrorlist | tail -1`"
sed -i$BAK "s|mirrorlist=$URL|baseurl=$NEWURL|" "$FILE"
BAK=""
rm -f /tmp/m2b-mirrorlist
done
echo "Done"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment