Unix/Linux 系统自动化管理 系统更新篇(4)
- UID
- 1066743
|
Unix/Linux 系统自动化管理 系统更新篇(4)
(5)获取需要在节点上进行安装的 rpm 包代码清单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| sub getNodeRpmList
{
my ($rpmlistfile) = @_;
my @shortnamelist;
open(RPMLIST,"<$rpmlistfile")
|| print "Fail to open rpm list file.\n";
while (<RPMLIST>) {
chomp;
if ( $_ =~ /^#.*$/ ) {
next;
}
#$rpmliststring = $rpmliststring ." " . $_;
push @shortnamelist , $_;
}
close(RPMLIST);
my %temphash;
#Remove repeated entry from namelist
foreach my $rpmname (@shortnamelist)
{
$temphash{$rpmname} = 1 if (defined $rpmname);
}
@shortnamelist = keys %temphash;
$rpmliststring = join(" ",@shortnamelist);
print "rpmliststring = $rpmliststring \n" if $:EBUG;
return $rpmliststring;
}
|
(6)main 入口主体部分 .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
| #!/usr/bin/perl
use strict;
#main()
my $updateSWConf;
my @children = ();
my $pid;
my $retVal = 0;
# 命令行参数解析
if ($ARGV[0] eq "-c")
{
$updateSWConf = $ARGV[1];
if( $updateSWConf eq "" )
{
$updateSWConf = "/etc/updateSW.conf";
}
}
# 检查是否指定需要更新的节点
if ( ($ARGV[2] ne "-n") || ($ARGV[2] eq "") || ($ARGV[3] eq "") )
{
print "pls specify the updated nodes with – n.\n";
}
my $argLine = $ARGV[3];
chomp($argLine);
my @nodeNameArray = split ",", $argLine;
# 创建 yum 配置文件
createyumConf($::distro_name, $::repo_base_url, $::repo_base_name);
# 创建 yum 软件仓库
$retVal = createRepo($::NODE_YUM_CONF_FILE)
if ($retVal == 1) exit 1;
# 获取每个节点需要更新的软件包的列表
my $rpmList = getNodeRpmList($updateSWConf);
# 对每个节点进行轮循
foreach my $nodeName (@nodeNameArray)
{
if ($nodeName eq "")
continue;
FORK:
{
# 针对每个节点创建进程
if ($pid = fork)
{
#parent, store pid in array
push(@children, $pid);
}
elsif (defined $pid)
{
# 将创建的 yum 配置文件传送到需要被远程部署的节点上
#child, issue the update command
$retVal = runCmd("scp $::NODE_YUM_CONF_FILE \
root@nodeName::NODE_YUM_CONF_DIR");
if ($retVal)
{
$retVal = 1;
exit $retVal;
}
# 将需要运行的更新脚本文件传送到需要被远程部署的节点上
$retVal = runCmd("scp $::NODEUPDATESWCMD \
root@nodeName::NODE_UPDATESWCMD_DIR");
if ($retVal)
{
$retVal = 1;
exit $retVal;
}
# 通过 shh 在远程节点上执行更新脚本,进行软件更新
$retVal = runCmd("ssh root@nodeName '$::NODEUPDATESWCMD – s 1 – c \
$::NODE_YUM_CONF_FILE – n $rpmList'");
if ($retVal)
{
$retVal = 1;
}
exit $retVal;
}
else
{
#couldn't fork, can't use msg catalog since no
# procs are available.
printf "updateLinuxSW: Unable to fork child process.\n";
}
}
}
#wait until all children processes have ended.
foreach $pid (@children)
{
waitpid($pid, 0);
}
undef @children;
exit 0;
|
updateSWorSys.pl 文件 命令格式:updateSWorSys.pl – s IsUpdateSWFlag – c nodeyumConfile – p rpmList
该可执行文件主要包含在各节点上执行的 yum 进行升级与更新软件包或系统代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| #!/usr/bin/perl
use strict;
#main()
my $IsUpdateSW;
my $nodeyumConfile;
my $output;
# 命令行参数解析
if ($ARGV[0] eq "-s")
{
$IsUpdatSW = $ARGV[1];
if( $IsUpdatSW eq "" )
{
$IsUpdatSW = 1;
}
}
else
{
$IsUpdatSW = 1;
}
# 检查是否指定了 yum 配置文件
if ($ARGV[2] eq "-c")
{
$nodeyumConfile = $ARGV[3];
if( $nodeyumConfile eq "" )
{
$nodeyumConfile = "/var/opt/yum/config/yum.conf";
}
}
# 检查是否指定了需要更新的软件名
if ( ($ARGV[4] ne "-p") || ($ARGV[4] eq "") || ($ARGV[4] eq "") )
{
print "pls specify the updated software name with – p.\n";
}
my $nodeSWName = $ARGV[5];
# 判断是进行软件更新还是系统更新
if ($IsUpdateSW)
{
$output = runCmd("yum -y -c $nodeyumConfile update $nodeSWName", -1);
}
else
{
$output = runCmd("yum -y -t -d 2 -c $nodeyumConfile update", -1);
}
if ( defined($:EBUG) )
{
print "The yum output: $output\n";
}
if ($::RETVAL)
{
print "yum errors, please refer to the yum output for details";
$$::RETVAL = 1;
}
exit $::RETVAL;
|
|
|
|
|
|
|