From 26a30fe7a9b7d933933b370a8208928c6cedbae7 Mon Sep 17 00:00:00 2001 From: Charles Delort Date: Mon, 31 Oct 2016 10:55:13 +0100 Subject: [PATCH 1/4] SCRIPTS: Add dbod-destroy --- lib/DBOD/Monitoring/Appdynamics.pm | 18 ++--- lib/DBOD/Network/Api.pm | 17 +++++ lib/DBOD/Network/IPalias.pm | 25 +++---- lib/DBOD/Network/Ldap.pm | 20 ++++++ scripts/dbod-destroy | 106 +++++++++++++++++++++++++++++ 5 files changed, 165 insertions(+), 21 deletions(-) create mode 100755 scripts/dbod-destroy 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 new file mode 100755 index 0000000..a33ef19 --- /dev/null +++ b/scripts/dbod-destroy @@ -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(); From c91b2bf583228b936d383086fd34709c205ae1b8 Mon Sep 17 00:00:00 2001 From: Charles Delort Date: Fri, 11 Nov 2016 10:37:32 +0100 Subject: [PATCH 2/4] SCRIPTS: Add delete_local_files.sh --- scripts/delete_local_files.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 scripts/delete_local_files.sh diff --git a/scripts/delete_local_files.sh b/scripts/delete_local_files.sh new file mode 100644 index 0000000..7d64112 --- /dev/null +++ b/scripts/delete_local_files.sh @@ -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 is $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; +grep -vwE $instance_name_uppercase /etc/fstab.bak > /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-slow-queries-rotation; + +echo "unmounting and deletion done for instance $instance_name"; + From d43bccf734a75e4072a85a71792dbff805805ed7 Mon Sep 17 00:00:00 2001 From: Ignacio Coterillo Date: Fri, 11 Nov 2016 10:52:14 +0100 Subject: [PATCH 3/4] Cosmetic changes --- scripts/delete_local_files.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/delete_local_files.sh b/scripts/delete_local_files.sh index 7d64112..83b343c 100644 --- a/scripts/delete_local_files.sh +++ b/scripts/delete_local_files.sh @@ -3,19 +3,19 @@ if [ "$#" -ne 1 ]; then echo "Use: delete_local_files.sh instance_name" exit -1 fi -instance_name=$1; -instance_name_uppercase=${1^^}; -echo "instance is $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; -grep -vwE $instance_name_uppercase /etc/fstab.bak > /etc/fstab; +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; +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-slow-queries-rotation; +rm -f /etc/logrotate.d/dod_${instance_name}-*; -echo "unmounting and deletion done for instance $instance_name"; +echo "Unmounting and deletion done for instance ${instance_name}"; From 9c9d5e113c25b62fdeee1655a3a398764afb87c3 Mon Sep 17 00:00:00 2001 From: Ignacio Coterillo Date: Mon, 28 Nov 2016 17:55:05 +0100 Subject: [PATCH 4/4] TEST: Fixed method call parameters in AppDynamics tests --- t/appdynamics.t | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/t/appdynamics.t b/t/appdynamics.t index 063cec7..279a8aa 100644 --- a/t/appdynamics.t +++ b/t/appdynamics.t @@ -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'); };