blib/lib/MYDan/Util/Reborn.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 203 CC 30 SCAR 37.1
CC
30
Cov
0%
new
90.0
do
42.0
ksget
42.0
CRAP 40.7
1 package MYDan::Util::Reborn;
2 use strict;
3 use warnings;
4  
5 our $VERSION = 1;
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Util::Reborn;
10 MYDan::Util::Reborn->new( url => '' )->do();
11  
12 =cut
13  
14 sub new
15 {
16 my ( $class, %self ) = @_;
17 map{die "$_ undef" unless $self{$_} }qw( url mac ipaddr netmask gateway hostname dns );
18  
19 my ( @disk, @sdev ) = `fdisk -l 2>/dev/null`;
20 map{ push @sdev, $1 if $_ =~ m#(/dev/[a-zA-Z0-9]+)\s+\*\s# }@disk;
21 die "nofind sdev" unless @sdev;
22  
23 my %df; map{ my @t = split /\s+/, $_; $df{$t[0]} = $t[-1]; }`df 2>/dev/null`;
24  
25 map{
26 if( $df{$_} && ( $df{$_} eq '/' || $df{$_} eq '/boot' ) )
27 {
28 die "boot nomatch." if $self{boot} && $self{boot} ne $df{$_};
29 $self{boot} = $df{$_};
30 }
31 }@sdev;
32  
33 die "nofind boot path." unless $self{boot};
34 $self{boot} = '' if $self{boot} eq '/boot';
35 $self{boot} = '/boot' if $self{boot} eq '/';
36  
37 bless \%self, ref $class || $class;
38 }
39  
40 sub do
41 {
42 my $this = shift;
43  
44 my ( $ksurl, $kscont ) = $this->{ks} ? ( $this->{ks} , $this->uaget( $this->{ks} ) ) : $this->ksget();
45  
46 $this->checkversion( $kscont );
47 $this->call( $kscont );
48  
49 my $hostinfo = join ':', map{ $this->{$_} }qw( hostname ipaddr netmask gateway dns );
50 my $cmd = "ks=$ksurl nofb text ksdevice=link BOOTIF=01-$this->{mac} osdrive=sda ip=dhcp net.ifnames=0 biosdevname=0 HOSTINFO=$hostinfo";
51
52 my $title = sprintf "MYDan reborn ( %s )", $ksurl =~ /([^\/]+)\.ks\.cfg$/ ? $1 : $ksurl =~ /([^\/]+)$/ ? $1 : 'install';
53  
54 if( -f '/boot/grub2/grub.cfg' || -f '/boot/grub/grub.cfg' )
55 {
56 $this->grub2( $cmd, $title );
57 }
58 elsif( -f '/boot/grub/grub.conf' )
59 {
60 $this->grub( $cmd, $title );
61 }
62 else
63 {
64 die "nofind grub or grub2.\n";
65 }
66 }
67  
68 sub grub2
69 {
70 my ( $this, $cmd, $title ) = @_;
71  
72 my ( $v, $conf ) = -f '/boot/grub2/grub.cfg' ? ( '2', '/boot/grub2/grub.cfg' ) : ( '', '/boot/grub/grub.cfg' );
73 my $temp = '/tmp/05_custom.' . time;
74  
75 die "cp 40_custom fail: $!" if system "cp /etc/grub.d/40_custom $temp";
76 open my $H, ">>", $temp or die "open $temp fail: $!";
77 my $cont = `cat $conf`;
78 die "nofind set root=" unless $cont =~ /set root='([a-zA-Z0-9,]+)'/;
79 print $H <<EOF;
80 menuentry "$title" {
81 set root='$1'
82 linux $this->{boot}/install/vmlinuz $cmd
83 initrd $this->{boot}/install/initrd.img
84 }
85 EOF
86 close($H);
87 die "reborn fail:$!" if system "cp $temp /etc/grub.d/05_custom && grub$v-mkconfig -o $conf && grub$v-set-default 0";
88 }
89  
90 sub grub
91 {
92 my ( $this, $cmd, $title ) = @_;
93  
94 my $conf = '/boot/grub/grub.conf';
95 my $cbak = "$conf-" . time;
96 my $temp = '/tmp/grub.conf.' . time;
97  
98 die "cp $cbak fail:$!" if system "cp -a $conf $cbak";
99 open my $H, ">", $temp or die "open $temp fail: $!";
100 print $H <<EOF;
101 default=0
102 timeout=10
103 #hiddenmenu
104 title $title
105 root (hd0,0)
106 kernel $this->{boot}/install/vmlinuz $cmd
107 initrd $this->{boot}/install/initrd.img
108 EOF
109 close($H);
110 die "reborn fail:$!" if system "cat $cbak | grep -v default | grep -v timeout | grep -v hiddenmenu | grep -v splashimage >> $temp && cp $temp $conf" ;
111 }
112  
113 sub call
114 {
115 for( split /\n/, $_[1] )
116 {
117 next unless $_ =~ /^#MYDan::CALL=(.+)$/;
118 print "call: $1\n";
119 die "call $1 fail: $!" if system $1;
120 }
121 }
122  
123 sub checkversion
124 {
125 my ( $this, $cont ) = @_;
126 my $v = $cont =~ /VERSION=(\d+)/ ? $1 : return;
127 die "VERSION:$VERSION ks.VERSION=$v notmatch.\n" unless $v == $VERSION;
128 }
129  
130 sub ksget
131 {
132 my $this = shift;
133  
134 die "notty.\n" unless -t STDIN;
135  
136 die "get kslist fail.\n" unless my $list = $this->uaget( $this->{url} );
137 die "kslist null.\n" unless my @list = split /\n/, $list;;
138  
139 my %list; map{ $list{$_} = $this->uaget( $_ ) }@list;
140 die "kslist null.\n" unless @list = grep{ $list{$_} }@list;
141  
142 my $x;
143 while(1)
144 {
145 map { printf "[ %d ] %s\n", $_ + 1, $list[$_] } 0 .. $#list;
146 print "please select:";
147  
148 $x = <STDIN>;
149 chomp $x;
150 last if $x =~ /^\d+$/ && $x <= @list;
151 }
152 return ( $list[$x-1], $list{$list[$x-1]} );
153 }
154  
155 sub uaget
156 {
157 my ( $this, $url ) = @_;
158 my $r = `curl -k --connect-timeout 3 '$url' 2>/dev/null`;
159 warn "get $url: null\n" if $this->{verbose} && ! $r;
160 return $r;
161 }
162  
163 1;