Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cdelort dev #64

Merged
merged 4 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
106 changes: 106 additions & 0 deletions scripts/dbod-destroy
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env perl
# Copyright (C) 2015, CERN
# This software is distributed under the terms of the GNU General Public
# Licence version 3 (GPL Version 3), copied verbatim in the file "LICENSE".
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as Intergovernmental Organization
# or submit itself to any jurisdiction.


# Extension of the base DBOD::Job class to add extra required parameters
package dbod_destroy;
use Moose;
use DBOD::Templates;

extends 'DBOD::Job';
with 'MooseX::Getopt::Usage',
'MooseX::Getopt::Usage::Role::Man';

# Main
package main;

use strict;
use warnings;

use Log::Log4perl;
use Data::Dumper;
use DBOD;
use DBOD::Runtime;
use DBOD::Network::Api;
use DBOD::Network::IPalias;
use DBOD::Templates;
use DBOD::Monitoring::Appdynamics;

# Initiates logger
BEGIN {
Log::Log4perl->easy_init() ;
}


my $job = dbod_destroy->new_with_options(allow_empty_metadata => $TRUE);

sub 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
$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 );
}
}
}
$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("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;
}

# Delete local files
$job->log->debug("Delete local files");
# Call to the delete_local_DBOD_files.sh script
# will only work from the local machine...

# Delete volumes? Will only work on dbod-daemon...
#$job->log->debug("Delete volumes");
#my $cmd = "/etc/init.d/syscontrol -i dfm.db-cmode DFM_delete_dod_volumes_Cmode -enti dod_$job->entity -debug";
#$job->log->debug( "Executing: $cmd" );
#my $vol_deletion_output = `$cmd`;
#if (DBOD::Runtime::result_code($vol_deletion_output)) {
# $job->log->error( "An error ocurred deleting volumes:" );
# $job->log->error( $vol_deletion_output );
# $job->_output($DBOD::ERROR);
# return;
#}

$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 deleting the entity in the API");
$job->_result($DBOD::ERROR);
return;
}
# Job done!
unless (defined $job->_result()){
$job->_result($DBOD::OK);
} # Else there was a previous non-fatal error
# Job done!
}

$job->run(\&body, );
exit $job->_result();
21 changes: 21 additions & 0 deletions scripts/delete_local_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Use: delete_local_files.sh instance_name"
exit -1
fi
instance_name=$1
instance_name_uppercase=${1^^}
echo "Instance: $instance_name_uppercase Unmounting the volumes, and deleting the scripts"
umount /ORA/dbs02/${instance_name_uppercase}
umount /ORA/dbs03/${instance_name_uppercase}
cp /etc/fstab /etc/fstab.bak
sed -i '/${instance_name_uppercase}/d' /etc/fstab

find /ORA/dbs01/syscontrol/local/logs/dod/ -depth -name "*dod_${instance_name}_*" -delete
find /etc/rc.d/rc*/ /etc/rc.d/init.d/ /etc/init.d/ -depth -name "*dod_${instance_name}" -delete

# Only for MySQL:
rm -f /etc/logrotate.d/dod_${instance_name}-*;

echo "Unmounting and deletion done for instance ${instance_name}";

8 changes: 4 additions & 4 deletions t/appdynamics.t
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ my $data = \%metadata;

subtest 'is_enabled' => sub {
$mock_db->mock('do', sub {return 1;});
is(DBOD::Monitoring::Appdynamics::is_enabled('testserver', $conf->{appdynamics}), $TRUE,
is(DBOD::Monitoring::Appdynamics::is_enabled('testserver', $conf), $TRUE,
'is_enabled: true');
$mock_db->mock('do', sub {return 0;});
is(DBOD::Monitoring::Appdynamics::is_enabled('testserver', $conf->{appdynamics}), $FALSE,
is(DBOD::Monitoring::Appdynamics::is_enabled('testserver', $conf), $FALSE,
'is_enabled: false');
};

subtest 'disable' => sub {
$mock_db->mock('do', sub {return 1;});
is(DBOD::Monitoring::Appdynamics::disable('testserver', $conf->{appdynamics}), $OK,
is(DBOD::Monitoring::Appdynamics::disable('testserver', $conf), $OK,
'disable: SUCCESS');
$mock_db->mock('do', sub {return 0;});
is(DBOD::Monitoring::Appdynamics::disable('testserver', $conf->{appdynamics}), $ERROR,
is(DBOD::Monitoring::Appdynamics::disable('testserver', $conf), $ERROR,
'disable: FAIL');
};

Expand Down