Last active
March 21, 2019 06:35
-
-
Save lesstif/b9af08eb5eecf0dfc7ca2065c2c45dfc to your computer and use it in GitHub Desktop.
change to yum mirror repository to korea ISP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
사용법
kakao 로 변경
/etc/yum.repos.d/CentOS-Base.repo 을 cdnetwork으로 변경
현재 폴더에 naver.repos 를 생성
참고
우분투 mirror 변경은 change-ubuntu-mirror.sh 를 참고