summaryrefslogtreecommitdiffstats
path: root/systemservice/config/library/system_manager_config/order/launch_order_xml2cfg.pl
blob: 1060107408c82e63c99c3e154cfad9eccf30077b (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/perl -w 
use strict;

use XML::XPath;
use File::Copy;

my $main_file="launch_order_main.xml";
my $body_file="launch_order_body.xml";


sub usage(){
  print STDERR "USAGE:$0 *.order *.cfg\n"
}
    



#=== MAIN ===
my $ret=system("which xmllint > /dev/null");
if($ret != 0){
    print STDERR  "It need xmllint. Please install it.\n";
    exit 1;
}

if(2 != @ARGV){
    usage();
    exit 1;
}

my $infile=$ARGV[0];
my $launchfile=$ARGV[1];


#ARG CHECK
if($infile !~ /.+\.order$/){
  print STDERR "$infile is not *.order\n";
  exit 1;
}elsif(! -e $infile){
  print "$infile not found\n";
  usage();
  exit 1;
}

if($launchfile !~ /.+\.cfg$/){
  print STDERR "$launchfile is not *.cfg\n";
  exit 1;
}elsif(! -e $launchfile){
  print "$launchfile not found\n";
  usage();
  exit 1;
}


system("cp -f $infile $body_file");

#check xml vaild 
$ret=system("xmllint --noout --valid $main_file");
if($ret != 0){
  print STDERR "XML is Invalid. \n";
  exit 1;
}

my $date=`LANG=en date`;
print "#  This file is created from $infile and $launchfile\n";
print "#  created date : $date\n";

my %gnamemap;
my %gidmap;

open(FIN,"<$launchfile")  or die("error :$!");
while (my $line = <FIN>){
  if($line =~ /^Launch/){
    $line =~ s/^[^|]+=//g;
    my @args =  split(/\|/,$line);
    $gnamemap{$args[0]}=$args[1];
    $gidmap{$args[1]}=$args[0];
  }
}
close(FIN);

foreach my $key (sort{$a <=> $b} keys(%gidmap)){
   print "#$key:$gidmap{$key}\n";
}


my $xml = XML::XPath->new(filename=>$main_file);

my $orders = $xml->find('/launch_order/order');
foreach my $order ($orders->get_nodelist) {
  my $groups = $order->find('./group');
  
  my $oname=$order->findvalue('@oname');
  my $fixed_group=$order->findvalue('@fixed_group');

  print "[$oname]\n";
  print "order=";
  
  my %l_gidmap = %gidmap;
  my $sep="";
  #fix groups
  if($fixed_group ne "NULL"){
    if(! exists $gnamemap{$fixed_group}){
      die("$fixed_group not found");
    }
    foreach my $gid (sort{$a <=> $b} keys(%l_gidmap)){
      print "$sep$gid";
      $sep="|";
      my $str=$l_gidmap{$gid};
      delete $l_gidmap{$gid};
      if($str eq $fixed_group){
        last;
      }
    }
  }

  # re-order group via *.order
  my %waitow;
  foreach my $group ($groups->get_nodelist) {
    my $gname=$group->findvalue('@gname');
    my $waittime=XML::XPath::Number::value($group->findvalue('@waittime'));
    
    my $gid=$gnamemap{$gname};
    
    if(! exists $gnamemap{$gname}){
      die("$oname:$gname is not defined");
    }

    if(! exists $l_gidmap{$gid}){
      die("$oname:$gname is multiple defined");
    }

    print "$sep$gid";
    $sep="|";
    delete $l_gidmap{$gid};
    
    if($waittime > 0){
      $waitow{$gname} = $waittime;
    }
  }
  
#  Output waittime configuration

  #out rest groups
  foreach my $gid (sort{$a <=> $b} keys(%l_gidmap)){
    print "$sep$gid";
    $sep="|";
  }
  
  print "\n";
  if(keys(%waitow) > 0 ){
    print "owlist=";
    $sep="";
    foreach my $owgroup ( keys(%waitow)) {
        print "${sep}oww_${owgroup}";
        $sep="|";
    }
    print "\n";
    
    foreach my $owgroup ( keys(%waitow)) {
        print "oww_${owgroup}=$waitow{$owgroup}\n";
    }

    
  }
  print "\n";
}