foreach my $on_device (sort {$a cmp $b} keys %{$anvil->data->{network_manager}{want}{ip_on}})
{
my $on_device_uuid = $anvil->data->{interface}{device}{$on_device}{uuid};
my $ip_address = $anvil->data->{network_manager}{want}{ip_on}{$on_device}{ip_address};
my $subnet_mask = $anvil->data->{network_manager}{want}{ip_on}{$on_device}{subnet_mask};
my $gateway = $anvil->data->{network_manager}{want}{ip_on}{$on_device}{gateway};
my $dns = $anvil->data->{network_manager}{want}{ip_on}{$on_device}{dns};
my $clear_ip_from = "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
's1:on_device' => $on_device,
's2:on_device_uuid' => $on_device_uuid,
's3:ip_address' => $ip_address,
's4:subnet_mask' => $subnet_mask,
's5:gateway' => $gateway,
's6:dns' => $dns,
}});
if (($subnet_mask !~ /^\d+$/) or ($subnet_mask < 1) or ($subnet_mask > 32))
{
# Convert to CIDR
my $cidr = $anvil->Convert->cidr({subnet_mask => $subnet_mask});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { cidr => $cidr }});
if (not $cidr)
{
print "[ ERROR ] - The subnet_mask: [".$subnet_mask."] is not valid. It must be either a CIDR notation, or a dotted-decimal mask that can be translated to CIDR notation.\n";
$anvil->nice_exit({exit_code => 1});
}
$subnet_mask = $cidr;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { subnet_mask => $subnet_mask }});
}
print "Checking to see if the IP address: [".$ip_address."/".$subnet_mask."] is assigned to: [".$on_device."] yet.\n";
if (exists $anvil->data->{interface}{ipv4}{$ip_address})
{
my $ip_uuid = $anvil->data->{interface}{ipv4}{$ip_address}{on_uuid};
my $current_device = $anvil->data->{interface}{uuid}{$ip_uuid}{device};
my $ip_sequence = $anvil->data->{interface}{ipv4}{$ip_address}{sequence};
my $current_subnet_mask = $anvil->data->{interface}{uuid}{$ip_uuid}{ipv4}{ip}{$ip_sequence}{subnet_mask};
my $current_gateway = $anvil->data->{interface}{uuid}{$ip_uuid}{ipv4}{gateway};
my $current_dns = $anvil->data->{interface}{uuid}{$ip_uuid}{ipv4}{dns};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
's1:ip_uuid' => $ip_uuid,
's2:current_device' => $current_device,
's3:ip_sequence' => $ip_sequence,
's4:current_subnet_mask' => $current_subnet_mask,
's5:current_gateway' => $current_gateway,
's6:current_dns' => $current_dns,
}});
die if not $ip_uuid;
print "- The IP exists, checking if it needs to be updated.\n";
if ($on_device ne $current_device)
{
print "- The IP address is on: [".$current_device."], will move the IP.\n";
$clear_ip_from = $current_device;
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { clear_ip_from => $clear_ip_from }});
}
elsif ($subnet_mask ne $current_subnet_mask)
{
print "- The current subnet mask is: [".$current_subnet_mask."], will update.\n";
}
elsif ($gateway ne $current_gateway)
{
print "- The current gateway is: [".$current_gateway."], will update.\n";
}
elsif ($dns ne $current_dns)
{
print "- The current DNS is: [".$current_dns."], will update.\n";
}
else
{
print "- No update is needed.\n";
next;
}
}
else
{
print "- The IP address needs to be assigned.\n";
}
if ($clear_ip_from)
{
my $old_uuid = $anvil->data->{interface}{device}{$clear_ip_from}{uuid};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { old_uuid => $old_uuid }});
print " - Clearing the IP from: [".$old_uuid."] (".$clear_ip_from.")\n";
my ($output, $return_code) = modify_connection($anvil, $old_uuid, "ipv4.method", "disabled");