Skip to content

Instantly share code, notes, and snippets.

@elico
Last active March 24, 2017 14:56
Show Gist options
  • Save elico/dc1af72344231d6d49af4eacecfae8df to your computer and use it in GitHub Desktop.
Save elico/dc1af72344231d6d49af4eacecfae8df to your computer and use it in GitHub Desktop.
centos mirrros external acl helper suite
#!/usr/bin/env ruby
require 'net/http'
require 'timeout'
require 'syslog'
def log(msg)
Syslog.log(Syslog::LOG_ERR, '%s', msg)
end
# peerslist = YAML.load_file('peersfile.yaml')
$listfile = ARGV[0]
$listfile = '/etc/squid/centosrepos.txt' if ARGV[0].nil? || (!ARGV[0].nil? && ARGV[0].empty?)
$mirrorlist = File.open($listfile).readlines
$lastfilestat = File.stat($listfile).mtime
$bypass = false
$bypass = true if !File.stat($listfile).file?
def testlist(url)
return false if $bypass
$mirrorlist = File.open($listfile).readlines if $lastfilestat != File.stat($listfile).mtime
$mirrorlist.each do |mirrorurl|
return true if url.start_with?(mirrorurl.chomp)
end
return false
end
def concurrency?
request = STDIN.gets
if request && (request.match /^[0-9]+\ /)
conc(request)
return true
else
noconc(request)
return false
end
end
def conc(request)
return unless request
request = request.split
if request[0] && request[1]
log("original request [#{request.join(' ')}].") if $debug
result = testlist(request[1])
resposnse = if result
request[0] + ' OK'
else
request[0] + ' ERR'
end
log("response => [#{resposnse}] , for => [#{request.join(' ')}].") if $debug
puts resposnse
else
log('original request [had a problem].') if $debug
resposnse = request[0] + 'ERR'
log("modified response [#{url}].") if $debug
puts resposnse
end
end
def noconc(request)
return unless request
request = request.split
if request[0]
log("Original request [#{request.join(' ')}].") if $debug
result = testlist(request[0])
resposnse = if result
'OK'
else
'ERR'
end
log("response => [#{resposnse}] , for => [#{request.join(' ')}].") if $debug
puts resposnse
else
log('Original request [had a problem].') if $debug
resposnse = 'ERR'
log("response => [#{resposnse}] , for => [#{request.join(' ')}].") if $debug
puts resposnse
end
end
def validr?(request)
if request.ascii_only? && request.valid_encoding?
true
else
STDERR.puts("errorness line#{request}")
false
end
end
def main
Syslog.open('centos-mirror-external-acl.rb', Syslog::LOG_PID)
log('Started centos-mirror-external-acl.rb')
STDERR.puts('centos-mirror-external-acl.rb')
c = concurrency?
if c
while request = STDIN.gets
conc(request) if validr?(request)
end
else
while request = STDIN.gets
noconc(request) if validr?(request)
end
end
end
$debug = false
STDOUT.sync = true
main
#!/usr/bin/env ruby
require "rubygems"
require 'uri'
require 'net/http'
require "nokogiri"
url = "http://mirror-status.centos.org/"
response = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)
doc = Nokogiri::HTML(response.body, nil, 'UTF-8')
mirrorstable = doc.xpath("/html/body/blockquote[3]/table")
serverlinks = []
mirrorstable[0].css("tr").each do |row|
row.css("td").css("a").map { |link| serverlinks << link['href'] }
end
puts serverlinks
#!/usr/bin/env bash
TMPLISTFILE=/tmp/centosmirrors.txt
STATICLISTFILEDIR=/etc/squid
STATICLISTFILE=`echo "$STATICLISTFILEDIR/centosrepos.txt"`
DEBUG=$1
if [ "$*" == "" ]; then
DEBUG=0
fi
if [ "$DEBUG" -eq "1" ];then
set -x
fi
if [ -d "$STATICLISTFILEDIR" ]; then
if [ "$DEBUG" -eq "1" ];then
echo "OK dir exists"
fi
else
echo "DIRECTORY: $STATICLISTFILEDIR doesn't exits"
exit 1
fi
if [ -e "$STATICLISTFILE" ];then
if [ "$DEBUG" -eq "1" ];then
echo "OK file exists"
fi
else
touch $STATICLISTFILE
fi
curl "http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates" 2>/dev/null >$TMPLISTFILE && \
cat $STATICLISTFILE $TMPLISTFILE |sort|uniq >$TMPLISTFILE-current && \
diff $TMPLISTFILE-current $STATICLISTFILE
if [ "$?" -eq "0" ];then
## file was not changed
if [ "$DEBUG" -eq "1" ];then
echo "list was not changed"
fi
else
cp $TMPLISTFILE-current $STATICLISTFILE
rm $TMPLISTFILE-current
rm $TMPLISTFILE
fi
set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment