Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
change to yum mirror repository to korea ISP.
<?php
// mirror list
$mirror_list = [
'cdnetworks' => 'http://centos.mirror.cdnetworks.com/$releasever/os/$basearch/',
'naver' => 'http://mirror.navercorp.com/centos/$releasever/os/$basearch/',
'kakao' => 'http://mirror.kakao.com/centos/$releasever/os/$basearch/',
];
// root 가 아니면 현재 폴더에 생성
$dryrun = true;
if (is_root_user()) {
$dryrun = false;
}
$opt = "f:rhkcno:";
$options = getopt($opt);
if (count($options) === 0 || isset($options['h'])) {
help($argc, $argv, $mirror_list);
}
$file = $options['f'] ?? '/etc/yum.repos.d/CentOS-Base.repo';
$mirror = null;
if (isset($options['n'])) {
$mirror[] = 'naver';
$mirror[] = $mirror_list['naver'];
} elseif (isset($options['k'])) {
$mirror[] = 'kakao';
$mirror[] = $mirror_list['kakao'];
} else {
// default
$mirror[] = 'cdnetworks';
$mirror[] = $mirror_list['cdnetworks'];
}
if (isset($options['r'])) {
restore_repos_file();
}
// generate repository file.
change_mirror($dryrun, $file, $mirror);
/**
* 저장소 파일 생성 & 변경
*
* @param $dryrun
* @param $repositoryFile
* @param $mirror
*/
function change_mirror($dryrun, $repositoryFile, $mirror)
{
if (! file_exists($repositoryFile)) {
die("'$repositoryFile' File not found: ");
}
// 저장할 파일
$outFile = $mirror[0].".repos";
$pi = pathinfo($repositoryFile);
$target = null;
// root 로 실행중이면 repos 폴더에 저장
if ($dryrun) {
$outFile = fopen($outFile, "w+");
} else {
$target = $pi['dirname'] . DIRECTORY_SEPARATOR . $outFile;
$outFile = fopen($target, "w+");
}
// INI_SCANNER_RAW 가 아니면 hash mark 를 주석으로 처리하지 않음.
$ini = parse_ini_file($repositoryFile, true, INI_SCANNER_RAW);
foreach ($ini as $section => $lines){
// 주석 건너 뜀
if (substr($section,0, 1) === '#') {
continue;
}
$properties = [];
fwrite($outFile, PHP_EOL . "[$section]" . PHP_EOL);
foreach ($lines as $key => $value) {
if (substr($key,0, 1) === '#') {
continue;
}
if (substr($key, 0, strlen('mirrorlist')) === 'mirrorlist') {
$key = '#' . $key;
}
$properties[$key] = $value;
}
$properties['baseurl'] = $mirror[1];
foreach ($properties as $key => $value) {
fwrite($outFile, $key . '=' . $value . PHP_EOL);
}
}
fclose($outFile);
if (! $dryrun) {
if (is_link ($repositoryFile)) {
unlink($repositoryFile);
}
// backup
if (file_exists($repositoryFile)) {
if (file_exists($repositoryFile . ".org")) {
rename($repositoryFile, $repositoryFile . ".org-".mt_rand(1,100));
} else {
rename($repositoryFile, $repositoryFile . ".org");
}
}
//symlink($pi['dirname'] . DIRECTORY_SEPARATOR . $pi['basename'], $outFile);
$link = $pi['dirname'] . DIRECTORY_SEPARATOR . $pi['basename'];
symlink($target, $link);
print("you need to disable fastmirror plugin(enabled=0) in the '/etc/yum/pluginconf.d/fastestmirror.conf' file" . PHP_EOL);
}
}
/**
* print help
*
* @param $argc
* @param $argv
* @param $mirror_list
*/
function help($argc, $argv, $mirror_list)
{
print "USAGE: php $argv[0] [OPTION]" . PHP_EOL;
$cwd = getcwd();
$usage =<<< USAGE
-f REPOSITORY_FILE specify the repository file(default '/etc/yum.repos.d/CentOS-Base.repo')
-o OUTPUT_FILE specify the modified repository file
(if the user is root then save to '/etc/yum.repos.d/cdnetworks.repos' otherwise
{$cwd}/cdnetworks.repos)
-r restore to origin repository file
-c use cdnetwork mirror({$mirror_list['cdnetworks']})
-n use naver mirror({$mirror_list['naver']})
-k use kakao mirror({$mirror_list['kakao']})
USAGE;
print $usage . PHP_EOL;
exit(1);
}
/**
* 원래 저장소 파일로 복구
*
*/
function restore_repos_file()
{
die("Not yet implemented.");
}
/**
* root 로 실행 여부
*
* @return bool
*/
function is_root_user()
{
if (function_exists('posix_getuid')) {
if (posix_getuid() === 0) {
return true;
}
}
return false;
}
@lesstif

This comment has been minimized.

Copy link
Owner Author

commented Mar 21, 2019

사용법

kakao 로 변경

sudo php yum-mirror-change.php -k

/etc/yum.repos.d/CentOS-Base.repo 을 cdnetwork으로 변경

sudo php yum-mirror-change.php -c -f /etc/yum.repos.d/CentOS-Base.repo

현재 폴더에 naver.repos 를 생성

php yum-mirror-change.php -n

참고

우분투 mirror 변경은 change-ubuntu-mirror.sh 를 참고

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.