Handle hostnames with upper-case letters
[webmin.git] / software / rhn-lib.pl
1 # rhn-lib.pl
2 # Functions for installing packages from the redhat network
3
4 $up2date_config = "/etc/sysconfig/rhn/up2date";
5 $rhn_sysconfig = "/etc/sysconfig/rhn/rhnsd";
6
7 sub list_update_system_commands
8 {
9 return ("up2date");
10 }
11
12 # update_system_install([package])
13 # Install some package with up2date
14 sub update_system_install
15 {
16 local $update = $_[0] || $in{'update'};
17 local @rv;
18 print "<b>",&text('rhn_install', "<tt>up2date $update</tt>"),"</b><p>\n";
19 print "<pre>";
20 &additional_log('exec', undef, "up2date \"$update\"");
21 local $qm = quotemeta($update);
22 &open_execute_command(CMD, "up2date $qm", 2);
23 local $got_error = 0;
24 while(<CMD>) {
25         while(s/^[^\015]+\015([^\012])/$1/) { }
26         $got_error++ if (/error|failed/i);
27         if (/installing.*\/([^\/\s]+)\-([^\-]+)\-([^\-]+)\.rpm/i) {
28                 push(@rv, $1);
29                 }
30         print;
31         }
32 close(CMD);
33 print "</pre>\n";
34 if ($got_error) {
35         print "<b>$text{'rhn_failed'}</b><p>\n";
36         @rv = ( );
37         }
38 else {
39         print "<b>$text{'rhn_ok'}</b><p>\n";
40         }
41 return @rv;
42 }
43
44 # update_system_form()
45 # Show a form for configuring the redhat update agent
46 sub update_system_form
47 {
48 print &ui_subheading($text{'rhn_form'});
49 print &ui_form_start("rhn_check.cgi");
50 print &ui_table_start($text{'rhn_header'}, undef, 2);
51
52 # Started at boot?
53 &foreign_require("init", "init-lib.pl");
54 local $auto = &init::action_status("rhnsd");
55 print &ui_table_row($text{'rhn_auto'},
56         &ui_yesno_radio("auto", $auto == 2 ? 1 : 0));
57
58 # Checking interval
59 local %rhnsd;
60 &read_env_file($rhn_sysconfig, \%rhnsd);
61 print &ui_table_row($text{'rhn_interval'},
62         &ui_textbox("interval", $rhnsd{'INTERVAL'}, 5)." ".$text{'rhn_secs'});
63
64 # Proxy server
65 local $conf = &read_up2date_config();
66 local $prx = $conf->{'pkgProxy'} ? $conf->{'pkgProxy'}->{'value'}
67                                  : $conf->{'httpProxy'}->{'value'};
68 print &ui_table_row($text{'rhn_proxy'},
69         &ui_radio("proxy_on", $conf->{'enableProxy'}->{'value'} ? 1 : 0,
70                   [ [ 0, $text{'rhn_none'} ],
71                     [ 1, &ui_textbox("proxy", $prx, 40) ] ]));
72
73 # Packages to skip
74 print &ui_table_row($text{'rhn_skip'},
75         &ui_textarea("skip",
76           join("\n", split(/;/, $conf->{'pkgSkipList'}->{'value'})), 5, 40));
77
78 print &ui_table_end();
79 print &ui_form_end([ [ undef, $text{'rhn_apply'} ],
80                      [ "now", $text{'rhn_now'} ] ]);
81 }
82
83 # read_up2date_config()
84 sub read_up2date_config
85 {
86 local %conf;
87 local $lnum = 0;
88 &open_readfile(CONF, $up2date_config);
89 while(<CONF>) {
90         s/\r|\n//g; s/#.*$//;
91         if (/^([^=\s]+)=(.*)$/) {
92                 $conf{$1} = { 'name' => $1,
93                               'value' => $2,
94                               'line' => $lnum };
95                 }
96         $lnum++;
97         }
98 close(CONF);
99 return \%conf;
100 }
101
102 # save_up2date_config(&config, name, value)
103 sub save_up2date_config
104 {
105 local $lref = &read_file_lines($up2date_config);
106 local $old = $_[0]->{$_[1]};
107 if ($old) {
108         $lref->[$old->{'line'}] = "$_[1]=$_[2]";
109         }
110 }
111
112 # update_system_available()
113 # Returns a list of packages available from RHN
114 sub update_system_available
115 {
116 local @rv;
117 open(UP2DATE, "up2date -l --showall |");
118 while(<UP2DATE>) {
119         s/\r|\n//g;
120         if (/^(\S+)\-([^\-]+)\-([^\-]+)\.([^\.]+)$/) {
121                 push(@rv, { 'name' => $1,
122                             'version' => "$2-$3",
123                             'arch' => $4 });
124                 }
125         }
126 close(UP2DATE);
127 return @rv;
128 }
129
130 1;
131