Skip to content

Commit

Permalink
SCRIPTS: Completed scripts/dbod-destroy.
Browse files Browse the repository at this point in the history
* Add subs that I needed in lib/DBOD/Network/Api.pm and
lib/DBOD/Network/Ldap.pm to remove properly the instance.
* Modify the remove_alias from lib/DBOD/Network/IPalias.pm,
the input hash names have been modified to reflect the change in the API.
* Change lib/DBOD/Monitoring/Appdynamics.pm to solve #60 and solve #61
  • Loading branch information
Charles Delort authored and icot committed Nov 28, 2016
1 parent 1095d53 commit 113f6c5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 74 deletions.
18 changes: 9 additions & 9 deletions lib/DBOD/Monitoring/Appdynamics.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -125,18 +125,18 @@ 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;
}

}

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(
Expand Down
17 changes: 17 additions & 0 deletions lib/DBOD/Network/Api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
25 changes: 13 additions & 12 deletions lib/DBOD/Network/IPalias.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand Down
20 changes: 20 additions & 0 deletions lib/DBOD/Network/Ldap.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
69 changes: 16 additions & 53 deletions scripts/dbod-destroy
Original file line number Diff line number Diff line change
Expand Up @@ -37,86 +37,49 @@ 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) {
$job->log->error( "Error unregistering instance in AppDynamics" );
$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()){
Expand Down

0 comments on commit 113f6c5

Please sign in to comment.