Changeset 304


Ignore:
Timestamp:
Jul 18, 2018, 6:40:31 PM (6 years ago)
Author:
g7moreau
Message:
  • Rewrite disable_host
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ddt/ddt

    r303 r304  
    11101110   my $computer_db = ipamdb_load($COMPUTER_YAML);
    11111111
    1112    if ($ip ne '') {
     1112   if ($ip ne '') { # disable by IP
    11131113      control_syntax_ip($ip);
    11141114      if ( control_exist_ip($computer_db, $ip) == 1 ) {
    1115          print "Error: Unkown IP address: $ip\n";
    1116          exit;
     1115         die "Error: Unkown IP address: $ip [FAILED]\n";
    11171116         }
    11181117
     
    11251124
    11261125         my @domainsetdb = @{$computer_db->{$domainset_current}};
    1127          my $cpt_mac = 0;
    1128          for my $value (@domainsetdb) {
    1129             for my $id (keys %{$value}) {
    1130 
    1131                if ($value->{$id}->{'ip'} eq $ip) {
    1132                   my $timestamp = time;
    1133                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1134                   $computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'enabled'} = 'no';
    1135                   print "L'adresse IP: $ip a été désactivée. Valeur du champs enabled: [".$computer_db->{$domainset_current}[$cpt_mac]->{$id}->{'enabled'}."]\n";
    1136                   ipamdb_save("$COMPUTER_YAML", $computer_db);
    1137                   exit;
    1138                   }
    1139                $cpt_mac++;
     1126         LOOP_ON_COMPUTER:
     1127         for my $computer (@domainsetdb) {
     1128            my ($mac_address, $attribute) = %{$computer};
     1129
     1130            next LOOP_ON_COMPUTER if $attribute->{'ip'} ne $ip;
     1131
     1132            if ($attribute->{'enabled'} eq 'no') {
     1133               print "Info: IP $ip from domain set $domainset_current is already disable [OK]" .
     1134                  " ... Status: $attribute->{'enabled'}\n";
     1135               exit;
    11401136               }
    1141             }
    1142          }
    1143       }
    1144    else {
     1137
     1138            my $timestamp = time;
     1139            $attribute->{'modify_time'} = $timestamp;
     1140            $attribute->{'enabled'}     = 'no';
     1141            ipamdb_save("$COMPUTER_YAML", $computer_db);
     1142            print "Info: Disabling IP $ip from domain set $domainset_current [OK]" .
     1143               " ... Status: $attribute->{'enabled'}\n";
     1144            exit;
     1145            }
     1146         }
     1147      }
     1148   else { # disable by Hostname
    11451149      control_exist_domainset($computer_db, $domainset);
    11461150      if ( control_exist_hostname($computer_db, $domainset, $hostname) == 1 ) {
    1147          die "Error: Unkown host: $hostname, in domain set: $domainset\n";
    1148          }
    1149 
    1150       my $cpt_mac = 0;
    1151       for my $value (@{$computer_db->{$domainset}}) {
    1152          for my $id (keys %{$value}) {
    1153             if (($value->{$id}->{'hostname'} eq $hostname) and ($value->{$id}->{'address_type'} ne 'pool-dhcp')) {
    1154                my $timestamp = time;
    1155                $computer_db->{$domainset}[$cpt_mac]->{$id}->{'modify_time'} = $timestamp;
    1156                $computer_db->{$domainset}[$cpt_mac]->{$id}->{'enabled'} = 'no';
    1157                print "La machine $hostname (domaine: $domainset) a été désactivé du DHCP. Valeur du champs enabled: [".$computer_db->{$domainset}[$cpt_mac]->{$id}->{'enabled'}."]\n";
    1158                ipamdb_save("$COMPUTER_YAML", $computer_db);
     1151         die "Error: Unkown host: $hostname, in domain set: $domainset [FAILED]\n";
     1152         }
     1153
     1154      LOOP_ON_COMPUTER:
     1155      for my  $computer (@{$computer_db->{$domainset}}) {
     1156         my ($mac_address, $attribute) = %{$computer};
     1157
     1158         next LOOP_ON_COMPUTER if $attribute->{'hostname'} ne $hostname;
     1159
     1160         if ($attribute->{'address_type'} ne 'pool-dhcp') {
     1161
     1162            if ($attribute->{'enabled'} eq 'no') {
     1163               print "Info: Host $hostname from domain set $domainset is already disable [OK]" .
     1164                  " ... Status: $attribute->{'enabled'}\n";
    11591165               exit;
    11601166               }
    11611167
    1162             if (($value->{$id}->{'hostname'} eq $hostname) and ($value->{$id}->{'address_type'} eq 'pool-dhcp')) {
    1163                print "Désactivation de la machine $hostname sur le domaine $domainset [FAILED]\n";
    1164                print "La machine $hostname fait partie du pool $hostname.\n";
    1165                exit;
    1166                }
    1167             }
    1168          $cpt_mac++;
    1169          }
    1170       }
    1171    #print "Error: Hostname already exist in domain set attachement $domainset: $hostname\n";
     1168            my $timestamp = time;
     1169            $attribute->{'modify_time'} = $timestamp;
     1170            $attribute->{'enabled'}     = 'no';
     1171            ipamdb_save("$COMPUTER_YAML", $computer_db);
     1172            print "Info: Disabling host $hostname from domain set $domainset [OK]" .
     1173               " ... Status: $attribute->{'enabled'}\n";
     1174            exit;
     1175            }
     1176
     1177         if ($attribute->{'address_type'} eq 'pool-dhcp') {
     1178            die "Error: Host $hostname from domain set $domainset inside a pool [FAILED]" .
     1179            }
     1180         }
     1181      }
    11721182   }
    11731183
     
    11811191   my $cpt_mac;
    11821192   if ( control_exist_mac($computer_db, $mac) == 1 ) {
    1183       print "Error: Unkown physical MAC address: $mac\n";
    1184       exit;
     1193      die "Error: Unkown physical MAC address: $mac [FAILED]\n";
    11851194      }
    11861195
     
    12001209
    12011210         if ($attribute->{'ip'} eq $pool) {
     1211            if ($attribute->{'enabled'} eq 'no') {
     1212               print "Info: Host $mac from pool $pool is already disable [OK]" .
     1213                  " ... Status: $attribute->{'enabled'}\n";
     1214               exit;
     1215               }
    12021216            my $timestamp = time;
    12031217            $attribute->{'modify_time'} = $timestamp;
    12041218            $attribute->{'enabled'}     = 'no';
    12051219            ipamdb_save("$COMPUTER_YAML", $computer_db);
    1206             print "Disabling machine $mac from pool $pool [OK]\n";
    1207             print "+-> Status:" . $attribute->{'enabled'} . "\n";
     1220            print "Info: Disabling host $mac from pool $pool [OK]" .
     1221               " ... Status: $attribute->{'enabled'}\n";
    12081222            exit;
    12091223            }
    12101224         else {
    1211             print "Computer disable $mac [FAILED]\n";
    1212             print "+-> The computer $mac does not belong to the $pool pool.\n";
    1213             exit;
     1225            die "Error: Host disable $mac [FAILED]" .
     1226               " ... The host $mac does not belong to the $pool pool.\n";
    12141227            }
    12151228         }
Note: See TracChangeset for help on using the changeset viewer.