diff --git a/lib/DBOD/Monitoring/Appdynamics.pm b/lib/DBOD/Monitoring/Appdynamics.pm index 15b115c..d296882 100644 --- a/lib/DBOD/Monitoring/Appdynamics.pm +++ b/lib/DBOD/Monitoring/Appdynamics.pm @@ -105,10 +105,10 @@ sub enable { sub is_enabled { my ($servername, $config) = @_; - my $host = $config->{host}; - my $port = $config->{port}; - my $user = $config->{user}; - my $password = $config->{password}; + my $host = $config->{appdynamics}->{host}; + my $port = $config->{appdynamics}->{port}; + my $user = $config->{appdynamics}->{user}; + my $password = $config->{appdynamics}->{password}; my $dsn = "DBI:mysql:database=dbtuna;host=$host;port=$port"; my $db = DBOD::DB->new( @@ -125,7 +125,7 @@ sub is_enabled { INFO "<$servername> is already defined"; return $TRUE; } else { - ERROR "<$servername> is found <$rows> not enabled"; + INFO "<$servername> is found <$rows> not enabled"; return $FALSE; } @@ -133,10 +133,10 @@ sub is_enabled { sub disable { my ($servername, $config) = @_; - my $host = $config->{host}; - my $port = $config->{port}; - my $user = $config->{user}; - my $password = $config->{password}; + my $host = $config->{appdynamics}->{host}; + my $port = $config->{appdynamics}->{port}; + my $user = $config->{appdynamics}->{user}; + my $password = $config->{appdynamics}->{password}; my $dsn = "DBI:mysql:database=dbtuna;host=$host;port=$port"; my $db = DBOD::DB->new( diff --git a/lib/DBOD/Network/Api.pm b/lib/DBOD/Network/Api.pm index 829bd1e..600788a 100644 --- a/lib/DBOD/Network/Api.pm +++ b/lib/DBOD/Network/Api.pm @@ -208,4 +208,21 @@ sub create_entity { } } +sub delete_entity { + my ($entity, $config) = @_; + my $client = _api_client($config,1); + $client->DELETE(join '/', + $config->{'api'}->{'entity_endpoint'}, $entity); + my %result; + $result{'code'} = $client->responseCode(); + if ($result{'code'} eq '204') { + INFO 'Entity deleted: ' . $entity; + return $OK; + } else { + ERROR 'Failed to contact API server'; + DEBUG Dumper $client; + return $ERROR; + } +} + 1; diff --git a/lib/DBOD/Network/IPalias.pm b/lib/DBOD/Network/IPalias.pm index d422d35..dfc0cfe 100644 --- a/lib/DBOD/Network/IPalias.pm +++ b/lib/DBOD/Network/IPalias.pm @@ -57,7 +57,7 @@ sub add_alias { return scalar $ERROR; } else { - INFO sprintf("Registerd alias: %s to dnsname: %s, host: %s", + INFO sprintf("Registered alias: %s to dnsname: %s, host: %s", $response->{alias}, $response->{dns_name}, $host); return scalar $OK; } @@ -78,20 +78,21 @@ sub remove_alias { # # Returns false if it fails, true if it succeeds - my ($input, $config) = @_; - my $dbname = $input->{dbname}; - my $host = $input->{hosts}->[0]; + my ($metadata, $config) = @_; + my $dbname = $metadata->{db_name}; + my $hosts = join(',',@{$metadata->{hosts}}); my $response = DBOD::Network::Api::get_ip_alias($dbname, $config); - DEBUG 'get_ip_alias API response: ' . Dumper $response; - + unless (defined $response){ + INFO "No ip alias found for $dbname, no need to remove it"; + return scalar $OK; + } my $res = DBOD::Network::Api::remove_ip_alias($dbname, $config); if (( defined $res) && ( $res == $OK)) { - my $resp = shift @{$response->{'response'}}; # Register ip alias to dns name on the CERN Network service - DBOD::Network::LanDB::remove_ip_alias($resp->{dnsname}, $resp->{alias}, $config); + DBOD::Network::LanDB::remove_ip_alias($response->{dns_name}, $response->{alias}, $config); # Generates DNS entry my $cmd = $config->{'ipalias'}->{'change_command'}; - my $command = $cmd . " --dnsname=" . $resp->{dnsname} . " --rm_ip=" . $host; + my $command = $cmd . " --dnsname=" . $response->{dns_name} . " --rm_ip=" . $hosts; DEBUG 'Executing ' . $command; my $return_code = DBOD::Runtime::run_cmd(cmd => $command); if ($return_code == $ERROR) { @@ -100,14 +101,14 @@ sub remove_alias { return scalar $ERROR; } else { - INFO sprintf("Registerd alias: %s to dnsname: %s, host: %s", - $resp->{alias}, $resp->{dnsname}, $host); + INFO sprintf("Removed alias: %s to dnsname: %s, hosts: %s", + $response->{alias}, $response->{dns_name}, $hosts); return scalar $OK; } } else { # An error occurred removing the alias - ERROR "Error removing alias from host: %s", $host; + ERROR "Error removing alias from hosts: %s", $hosts; return scalar $ERROR; } } diff --git a/lib/DBOD/Network/Ldap.pm b/lib/DBOD/Network/Ldap.pm index 103d75a..6d6bf48 100644 --- a/lib/DBOD/Network/Ldap.pm +++ b/lib/DBOD/Network/Ldap.pm @@ -154,4 +154,24 @@ sub create_instance { } +sub delete_instance { + my ($instance, $config) = @_; + $instance= 'dod_'.$instance; + DEBUG 'Deleting LDAP entity: ' . $instance; + my $conn = get_connection($config); + my $entities= get_entity($conn, "SC-ENTITY=$instance,SC-CATEGORY=entities,OU=syscontrol,DC=cern,DC=ch"); + if(scalar @$entities == 0){ + INFO 'no entry in Ldap'; + } + # taken from https://github.com/perl-ldap/perl-ldap/blob/master/contrib/recursive-ldap-delete.pl: + # delete the entries found in a sorted way: + # those with more "," (= more elements) in their DN, which are deeper in the DIT, first + # trick for the sorting: tr/,// returns number of , (see perlfaq4 for details) + foreach my $e (sort { $b->dn =~ tr/,// <=> $a->dn =~ tr/,// } @$entities) { + $conn->delete($e); + } + return scalar $OK; +} + + 1; diff --git a/scripts/dbod-destroy b/scripts/dbod-destroy index f9f4dec..ad1bbcd 100755 --- a/scripts/dbod-destroy +++ b/scripts/dbod-destroy @@ -37,15 +37,18 @@ BEGIN { } -my $job = dbod_destroy->new_with_options(); +my $job = dbod_destroy->new_with_options(allow_empty_metadata => $TRUE); sub body { - - $job->log->debug("first line of body"); + unless((defined $job->metadata) && (scalar keys %{$job->metadata})){ + $job->log->info("Metadata is not defined, nothing to delete"); + $job->_result($DBOD::OK); + exit $job->_result(); + } + $job->log->debug("Unregister MySQL and PG instances in AppDynamics"); # Unregister MySQL and PG instances in AppDynamics if ($job->metadata->{db_type} eq 'MYSQL' or $job->metadata->{db_type} eq 'PG') { # Unregister entity in Appdynamics - if ((defined $job->metadata) && (scalar keys %{$job->metadata})) { $job->log->debug( Dumper $job->config() ); if (DBOD::Monitoring::Appdynamics::is_enabled($job->entity, $job->config())) { unless (DBOD::Monitoring::Appdynamics::disable($job->entity, $job->config()) == $DBOD::OK) { @@ -53,70 +56,30 @@ sub body { $job->_result( $DBOD::ERROR ); } } - } else { - $job->log->error( "Entity not found through API. Skipping AppDynamics unregistration" ); - $job->_result( $DBOD::ERROR ); - } } - $job->log->debug("after unregister appdyn"); - exit 43; - # Delete entity in LDAP - # Probably just need the name we can remove input - #$input->{entity} = 'dod_' . $job->entity; - #my $buf = 'dbod-' . $input->{dbname} . '.cern.ch'; - #$buf =~ s/\_/\-/g; - #$input->{ipalias} = $buf; + $job->log->debug("Delete Ldap"); # Delete LDAP using the API unless (DBOD::Network::Ldap::delete_instance($job->entity, $job->config) == $DBOD::OK) { $job->log->error( "Error deleting instance in LDAP" ); $job->_result( $DBOD::ERROR ); } - - $job->log->debug("after LDAP"); - # Delete IP alias and from API - unless ($job->no_ip_alias()) { - unless (DBOD::Network::IPalias::remove2_alias($job->entity, $job->config) == $DBOD::OK) { + $job->log->debug("Delete IP alias"); + # Delete IP alias + unless (DBOD::Network::IPalias::remove_alias($job->metadata, $job->config) == $DBOD::OK) { $job->log->error( "Error deleting IP Alias" ); $job->_result( $DBOD::ERROR ); return; - } } - - $job->log->debug("after IP alias"); - # delete metadata entry and register in the API - unless ($job->no_api()) { + $job->log->debug("Delete entity from API"); + # delete entity from API unless (DBOD::Network::Api::delete_entity($job->entity, $job->config) == $DBOD::OK) { - $job->log->error("Error registering the entity in the API"); + $job->log->error("Error deleting the entity in the API"); $job->_result($DBOD::ERROR); return; } - } - $job->log->debug("after metadata"); -# # Delete volumes -# unless ($job->skip_volumes_creation()) { -# -# # TODO InfluxDb type is not supported in volume creation. Temporal mask. Revert when it is supported -# my $volume_type = $input->{subcategory}; -# if ($volume_type eq "InfluxDB"){ -# $volume_type = "PG"; -# } -# -# my $cmd = "/etc/init.d/syscontrol -i dfm.db-cmode DFM_create_dod_volumes_Cmode -name $input->{dbname} -size $input->{size} -server $input->{hosts}->[0] -type ${volume_type} -debug"; -# -# $job->log->debug( "Executing: $cmd" ); -# my $vol_creation_output = `$cmd`; -# if (DBOD::Runtime::result_code($vol_creation_output)) { -# $job->log->error( "An error ocurred creating volumes:" ); -# $job->log->error( $vol_creation_output ); -# $job->_output($DBOD::ERROR); -# return; -# } -# -# $input->{'serverlogs'} = $serverlogs; -# $input->{'serverdata'} = $serverdata; -# } -# + $job->log->debug("after metadata"); + # Delete volumes? # Job done! unless (defined $job->_result()){