Changeset 306


Ignore:
Timestamp:
Jul 18, 2018, 8:37:58 PM (6 years ago)
Author:
g7moreau
Message:
  • Continue to rewrite loop on computer
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ddt/ddt

    r305 r306  
    246246      next if $domainset_current eq 'version';
    247247
    248       for my $value (@{$computer_db->{$domainset_current}}) {
    249          for my $id (keys %{$value}) {
    250             #print "Erreur: cette adresse IP $ip existe déjà\n";
    251             return 0 if $value->{$id}{'ip'} eq $ip;
    252             }
     248      LOOP_ON_COMPUTER:
     249      for my $computer (@{$computer_db->{$domainset_current}}) {
     250         my ($mac_address, $attribute) = %{$computer};
     251         #print "Erreur: cette adresse IP $ip existe déjà\n";
     252         return 0 if $attribute->{'ip'} eq $ip;
    253253         }
    254254      }
    255255
    256256   for my $current_pool (keys %{$computer_db->{'pool'}}) {
    257          #--- Cette partie pour tester les ip des pools est bonne ne plus la changer ---#
    258       my @T_pool_ip = @{ $computer_db->{'pool'}{$current_pool}{'ip'}};
     257      #--- Cette partie pour tester les ip des pools est bonne ne plus la changer ---#
     258      my @T_pool_ip = @{$computer_db->{'pool'}{$current_pool}{'ip'}};
    259259
    260260      for my $pool_ip (@T_pool_ip) {
     
    10131013
    10141014      my @domainsetdb = @{$computer_db->{$domainset_current}};
    1015       my $cpt_mac = 0;
     1015      my $computer_index = 0;
    10161016      for my $value (@domainsetdb) {
    10171017         for my $id (keys %{$value}) {
    10181018            if ($id eq $mac) {
    1019                my $host = $computer_db->{$domainset_current}[$cpt_mac]{$mac};
     1019               my $host = $computer_db->{$domainset_current}[$computer_index]{$mac};
    10201020               next LOOP_ON_DOMAINSET if $host->{'ip'} ne $ip;
    10211021
    10221022               $host->{'modify_time'} = time;
    1023                splice(@{$computer_db->{$domainset_current}}, $cpt_mac => 1);
     1023               splice(@{$computer_db->{$domainset_current}}, $computer_index => 1);
    10241024               push @{$computer_db->{$domainset}}, { $mac => $host };
    10251025
     
    10281028               }
    10291029            }
    1030          $cpt_mac++;
     1030         $computer_index++;
    10311031         }
    10321032      }
     
    11871187   my $computer_db = ipamdb_load($COMPUTER_YAML);
    11881188
    1189    my $cpt_mac;
     1189   my $computer_index;
    11901190   if ( control_exist_mac($computer_db, $mac) == 1 ) {
    11911191      die "Error: Unkown physical MAC address: $mac [FAILED]\n";
     
    12381238
    12391239   control_exist_domainset($computer_db, $domainset) or exit;
    1240    if ($ip ne '') {
     1240
     1241   if ($ip ne '') { # enable by IP
    12411242      control_syntax_ip($ip);
    12421243      if ( control_exist_ip($computer_db, $ip) == 1 ) {
     
    12531254
    12541255         my @domainsetdb = @{$computer_db->{$domainset_current}};
    1255          my $cpt_mac=0;
    1256          for my $value (@domainsetdb) {
    1257             for my $id (keys %{$value}) {
    1258                if ($value->{$id}->{'ip'} eq $ip) {
    1259                   my $timestamp = time;
    1260                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1261                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'enabled'} = 'yes';
    1262                   print "L'adresse IP: $ip a été réactivée. Valeur du champs enabled: [".$computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'enabled'}."]\n";
    1263                   ipamdb_save("$COMPUTER_YAML", $computer_db);
     1256
     1257         LOOP_ON_COMPUTER:
     1258         for my $computer (@domainsetdb) {
     1259            my ($mac_address, $attribute) = %{$computer};
     1260            if ($attribute->{'ip'} eq $ip) {
     1261
     1262               if ($attribute->{'enabled'} eq 'yes') {
     1263                  print "Info: IP $ip belongs to domain set $domainset is already enable [OK]" .
     1264                     " ... Status: $attribute->{'enabled'}\n";
    12641265                  exit;
    12651266                  }
    1266                $cpt_mac=$cpt_mac+1;
     1267
     1268               my $timestamp = time;
     1269               $attribute->{'modify_time'} = $timestamp;
     1270               $attribute->{'enabled'}     = 'yes';
     1271               ipamdb_save("$COMPUTER_YAML", $computer_db);
     1272               print "Info: IP $ip is now enable [OK]" .
     1273                  " ... Status: $attribute->{'enabled'}\n";
     1274               exit;
    12671275               }
    12681276            }
    12691277         }
    12701278      }
    1271    else {
     1279   else { # enable by Hostname
    12721280      if ( control_exist_hostname($computer_db, $domainset, $hostname) == 1 ) {
    12731281         die "Error: Unkown host: $hostname, in domain set: $domainset\n";
    12741282         }
    12751283
    1276       my $cpt_mac=0;
    1277       for my $value (@{$computer_db->{$domainset}}) {
    1278          for my $id (keys %{$value}) {
    1279             if (($value->{$id}->{'hostname'} eq $hostname) and ($value->{$id}->{'address_type'} ne 'pool-dhcp')) {
    1280                my $timestamp = time;
    1281                $computer_db->{$domainset}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1282                $computer_db->{$domainset}[$cpt_mac]->{$id}->{'enabled'} = 'yes';
    1283                print "La machine $hostname (domaine: $domainset) a été réactivée du DHCP. Valeur du champs enabled: [".$computer_db->{$domainset}[$cpt_mac]->{$id}->{'enabled'}."]\n";
    1284                ipamdb_save("$COMPUTER_YAML", $computer_db);
    1285                exit;
    1286                }
    1287 
    1288             if (($value->{$id}->{'hostname'} eq $hostname) and ($value->{$id}->{'address_type'} eq 'pool-dhcp')) {
    1289                print "Réactivation de la machine $hostname sur le domaine $domainset [FAILED]\n";
    1290                print "La machine $hostname fait partie du pool $hostname.\n";
    1291                exit;
    1292                }
    1293             }
    1294          $cpt_mac++;
    1295          }
    1296       }
    1297    #print "La machine $hostname n'existe pas sur le domaineset: $domainset\n";
     1284      LOOP_ON_COMPUTER:
     1285      for my $computer (@{$computer_db->{$domainset}}) {
     1286         my ($mac_address, $attribute) = %{$computer};
     1287         next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname;
     1288
     1289         if ($attribute->{'address_type'} eq 'pool-dhcp') {
     1290            die "Error: Host $hostname from domain set $domainset belongs to a a pool [FAILED]" .
     1291               " ... use 'enable-float' command instead";
     1292            }
     1293
     1294         if ($attribute->{'enabled'} eq 'yes') {
     1295            print "Info: Host $hostname belongs to domain set $domainset is already enable [OK]" .
     1296               " ... Status: $attribute->{'enabled'}\n";
     1297            exit;
     1298            }
     1299
     1300         my $timestamp = time;
     1301         $attribute->{'modify_time'} = $timestamp;
     1302         $attribute->{'enabled'}     = 'yes';
     1303         ipamdb_save("$COMPUTER_YAML", $computer_db);
     1304         print "Info: Host $hostname is now enable [OK]" .
     1305            " ... Status: $attribute->{'enabled'}\n";
     1306         exit;
     1307         }
     1308      }
    12981309   }
    12991310
     
    13051316   my $computer_db = ipamdb_load($COMPUTER_YAML);
    13061317
    1307    my $cpt_mac;
     1318   my $computer_index;
    13081319   if ( control_exist_mac($computer_db, $mac) == 1 ) {
    13091320      print "Adresse MAC $mac non trouvée.\n";
     
    13201331      my @domainsetdb = @{$computer_db->{$domainset_current}};
    13211332
    1322       $cpt_mac=0;
     1333      $computer_index=0;
    13231334      for my $value (@domainsetdb) {
    13241335         for my $id (keys %{$value}) {
    13251336            if ($id eq $mac) {
    13261337               if ($value->{$id}->{'ip'} eq $pool) {
    1327                   #splice(@{$computer_db->($domainset_current)} , $cpt_mac => 1);
    13281338                  my $timestamp = time;
    1329                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1330                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'enabled'} = 'yes';
     1339                  $computer_db->{$domainset_current}[$computer_index]->{$id}->{'modify_time'} = $timestamp;
     1340                  $computer_db->{$domainset_current}[$computer_index]->{$id}->{'enabled'} = 'yes';
    13311341                  ipamdb_save("$COMPUTER_YAML", $computer_db);
    13321342                  print "Réactivation de la machine $mac du pool $pool [OK]\n";
     
    13391349                  }
    13401350               }
    1341             $cpt_mac++;
     1351            $computer_index++;
    13421352            }
    13431353         }
     
    14591469         die "Error: Unkown IP address: $ip\n";
    14601470         }
    1461       my $cpt_mac=0;
     1471      my $computer_index=0;
    14621472      for my $value (@{$computer_db->{$domainset}}) {
    14631473         for my $id (keys %{$value}) {
    14641474            if ($value->{$id}->{'ip'} eq $ip) {
    14651475               my $timestamp = time;
    1466                splice(@{$computer_db->{$domainset}}, $cpt_mac => 1);
     1476               splice(@{$computer_db->{$domainset}}, $computer_index => 1);
    14671477               print "La machine $ip a été supprimer du domaine $domainset\n";
    14681478               ipamdb_save("$COMPUTER_YAML", $computer_db);
     
    14701480               }
    14711481            }
    1472          $cpt_mac++;
     1482         $computer_index++;
    14731483         }
    14741484      #print "La machine $ip n'existe pas sur le domaine $domainset.\n";
     
    14781488         die "Error: Unkown host: $hostname, in domain set: $domainset\n";
    14791489         }
    1480       my $cpt_mac=0;
     1490      my $computer_index=0;
    14811491      for my $value (@{$computer_db->{$domainset}}) {
    14821492         for my $id (keys %{$value}) {
    14831493            if (($value->{$id}->{'hostname'} eq $hostname) and ($value->{$id}->{'address_type'} ne 'pool-dhcp')) {
    14841494               my $timestamp = time;
    1485                splice(@{$computer_db->{$domainset}}, $cpt_mac => 1);
     1495               splice(@{$computer_db->{$domainset}}, $computer_index => 1);
    14861496               print "La machine $hostname a été supprimer du domaine $domainset\n";
    14871497               ipamdb_save("$COMPUTER_YAML", $computer_db);
     
    14951505               }
    14961506            }
    1497          $cpt_mac++;
     1507         $computer_index++;
    14981508         }
    14991509      #print "La machine $hostname n'existe pas sur le domaine $domainset.\n";
     
    15101520   my $computer_db = ipamdb_load($COMPUTER_YAML);
    15111521
    1512    my $cpt_mac;
    15131522   if ( control_exist_mac($computer_db, $mac) == 1 ) {
    15141523      print "Adresse MAC $mac non trouvée.\n";
     
    15251534      my @domainsetdb = @{$computer_db->{$domainset_current}};
    15261535
    1527       $cpt_mac=0;
    1528       for my $value (@domainsetdb) {
    1529          for my $id (keys %{$value}) {
    1530 
    1531             if ($id eq $mac) {
    1532                if ($value->{$id}->{'ip'} eq $pool) {
    1533                   #splice(@{$computer_db->($domainset_current)} , $cpt_mac => 1);
    1534                   splice(@{$computer_db->{$domainset_current}}, $cpt_mac => 1);
    1535                   ipamdb_save("$COMPUTER_YAML", $computer_db);
    1536                   print "Suppression de la machine $mac du pool $pool [OK]\n";
    1537                   exit;
    1538                   }
    1539                else {
    1540                   print "Suppression de la machine $mac [FAILED]\n";
    1541                   print "La machine $mac n'appartient pas au pool $pool.\n";
    1542                   exit;
    1543                   }
    1544                }
    1545             $cpt_mac++;
    1546             }
     1536      my $computer_index = 0;
     1537
     1538      LOOP_ON_COMPUTER:
     1539      for my $computer (@domainsetdb) {
     1540         my ($mac_address, $attribute) = %{$computer};
     1541
     1542         $computer_index++ and next LOOP_ON_COMPUTER if $mac_address ne $mac;
     1543
     1544         if ($attribute->{'ip'} ne $pool) {
     1545            die "Error: Host remove $mac [FAILED]" .
     1546               " ... The host $mac does not belong to the $pool pool.\n";
     1547            }
     1548
     1549         splice(@{$computer_db->{$domainset_current}}, $computer_index => 1);
     1550         ipamdb_save("$COMPUTER_YAML", $computer_db);
     1551         print "Info: remove host $mac from the pool $pool [OK]\n";
     1552         exit;
    15471553         }
    15481554      }
     
    18941900         next if $domainset_current eq 'version';
    18951901
    1896          my $cpt_mac = 0;
     1902         my $computer_index = 0;
    18971903         for my $computer (@{$computer_db->{$domainset_current}}) {
    18981904            for my $id (keys %{$computer}) {
    18991905               if ($computer->{$id}->{'ip'} eq $ip) {
    19001906                  my $timestamp = time;
    1901                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1902                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'pxe_config'}  = $pxe_config;
     1907                  $computer_db->{$domainset_current}[$computer_index]->{$id}->{'modify_time'} = $timestamp;
     1908                  $computer_db->{$domainset_current}[$computer_index]->{$id}->{'pxe_config'}  = $pxe_config;
    19031909                  print "IP Address: $ip, PXE enabled in config: $pxe_config\n";
    19041910                  ipamdb_save("$COMPUTER_YAML", $computer_db);
    19051911                  exit;
    19061912                  }
    1907                $cpt_mac++;
     1913               $computer_index++;
    19081914               }
    19091915            }
     
    19151921         }
    19161922
    1917       my $cpt_mac = 0;
     1923      my $computer_index = 0;
    19181924      for my $value (@{$computer_db->{$domainset}}) {
    19191925         for my $id (keys %{$value}) {
    19201926            if (($value->{$id}->{'hostname'} eq $hostname) and ($value->{$id}->{'address_type'} ne 'pool-dhcp')) {
    19211927               my $timestamp = time;
    1922                $computer_db->{$domainset}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1923                $computer_db->{$domainset}[$cpt_mac]->{$id}->{'pxe_config'}  = $pxe_config;
     1928               $computer_db->{$domainset}[$computer_index]->{$id}->{'modify_time'} = $timestamp;
     1929               $computer_db->{$domainset}[$computer_index]->{$id}->{'pxe_config'}  = $pxe_config;
    19241930               print "Host $hostname ($domainset), PXE enabled in config: $pxe_config\n";
    19251931               ipamdb_save("$COMPUTER_YAML", $computer_db);
     
    19311937               }
    19321938            }
    1933          $cpt_mac++;
     1939         $computer_index++;
    19341940         }
    19351941      }
     
    19761982         next if $domainset_current eq 'version';
    19771983
    1978          my $cpt_mac = 0;
     1984         my $computer_index = 0;
    19791985         for my $computer (@{$computer_db->{$domainset_current}}) {
    19801986            for my $id (keys %{$computer}) {
    19811987               if ($computer->{$id}->{'ip'} eq $ip) {
    1982                   next if not exists $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'pxe_config'};
    1983 
    1984                   my $pxe_config = $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'pxe_config'};
     1988                  next if not exists $computer_db->{$domainset_current}[$computer_index]->{$id}->{'pxe_config'};
     1989
     1990                  my $pxe_config = $computer_db->{$domainset_current}[$computer_index]->{$id}->{'pxe_config'};
    19851991                  my $timestamp  = time;
    1986                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1987                   delete $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'pxe_config'};
     1992                  $computer_db->{$domainset_current}[$computer_index]->{$id}->{'modify_time'} = $timestamp;
     1993                  delete $computer_db->{$domainset_current}[$computer_index]->{$id}->{'pxe_config'};
    19881994                  print "IP Address: $ip, PXE disable from config: $pxe_config\n";
    19891995                  ipamdb_save("$COMPUTER_YAML", $computer_db);
    19901996                  exit;
    19911997                  }
    1992                $cpt_mac++;
     1998               $computer_index++;
    19931999               }
    19942000            }
     
    20002006         }
    20012007
    2002       my $cpt_mac = 0;
     2008      my $computer_index = 0;
    20032009      for my $value (@{$computer_db->{$domainset}}) {
    20042010         for my $id (keys %{$value}) {
     
    20062012               next if not exists $value->{$id}->{'pxe_config'};
    20072013
    2008                my $pxe_config = $computer_db->{$domainset}[$cpt_mac]->{$id}->{'pxe_config'};
     2014               my $pxe_config = $computer_db->{$domainset}[$computer_index]->{$id}->{'pxe_config'};
    20092015               my $timestamp  = time;
    2010                $computer_db->{$domainset}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    2011                delete $computer_db->{$domainset}[$cpt_mac]->{$id}->{'pxe_config'};
     2016               $computer_db->{$domainset}[$computer_index]->{$id}->{'modify_time'} = $timestamp;
     2017               delete $computer_db->{$domainset}[$computer_index]->{$id}->{'pxe_config'};
    20122018               print "Host $hostname ($domainset), PXE disable from config: $pxe_config\n";
    20132019               ipamdb_save("$COMPUTER_YAML", $computer_db);
     
    20192025               }
    20202026            }
    2021          $cpt_mac++;
     2027         $computer_index++;
    20222028         }
    20232029      }
Note: See TracChangeset for help on using the changeset viewer.