forked from cpanel/elevate
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprune_openstack
More file actions
executable file
·89 lines (70 loc) · 2.75 KB
/
Copy pathprune_openstack
File metadata and controls
executable file
·89 lines (70 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env perl
use JSON::PP ();
use POSIX ();
use strict;
use warnings;
# Static constants
use constant {
KEY_NAME => "deletethis",
VM_NAME => "app-elevate-cpanel.github.cpanel.net",
};
my $openstack_path = "/usr/bin/openstack" if -x "/usr/bin/openstack";
die "No Openstack CLI binary installed." unless $openstack_path;
# Define "two hours ago" timestamp
my $time_offset = 7200;
my $hammer_time = POSIX::strftime( "%Y-%m-%dT%H:%M:%SZ", gmtime( time - $time_offset ) );
# Run removal functions
remove_stale_entities( 'keys', KEY_NAME, 'keypair' );
remove_stale_entities( 'instances', VM_NAME, 'server' );
# Fetch and format entities
sub get_entities {
my ($entity_type) = @_;
my $entities = {};
my ( $list_command, $show_command );
if ( $entity_type eq 'keys' ) {
$list_command = "$openstack_path keypair list -f json";
$show_command = "$openstack_path keypair show -f json";
}
else {
$list_command = "$openstack_path server list -f json --no-name-lookup";
$show_command = "$openstack_path server show -f json";
}
my $out = _run_cmd( $list_command, 0 );
my $json = JSON::PP->new->decode($out);
foreach my $entry (@$json) {
my $entity_name = length $entry->{ID} ? $entry->{ID} : $entry->{Name};
my $show_out = _run_cmd( qq{$show_command "$entity_name"}, 0 );
my $entity_data = JSON::PP->new->decode($show_out);
$entities->{$entity_name}->{name} = $entity_data->{name};
$entities->{$entity_name}->{created_on} = length $entity_data->{created_at} ? $entity_data->{created_at} : $entity_data->{created};
if ( length $entity_data->{id} ) {
$entities->{$entity_name}->{id} = $entity_data->{id};
}
}
return $entities;
}
# Remove stale entities
sub remove_stale_entities {
my ( $entity_type, $name_pattern, $delete_command ) = @_;
my $entities = get_entities($entity_type);
foreach my $entity ( values %$entities ) {
unless ( length $entity->{id} && length $entity->{name} && length $entity->{created_on} ) {
print "## [WARN]: Skipping entity due to missing keys\n";
next;
}
next unless $entity->{'name'} =~ /${\$name_pattern}/;
if ( $entity->{created_on} lt $hammer_time ) {
print "## [INFO]: Deleting: ID: $entity->{id}, Name: $entity->{name}, Created On: $entity->{created_on}\n";
_run_cmd( "$openstack_path $delete_command delete $entity->{id}", 1 );
}
}
return 0;
}
# Run command subroutine with basic error handling
sub _run_cmd {
my ( $cmd, $logger ) = @_;
print "## [INFO]: Running command: $cmd\n" if $logger;
my $output = `$cmd`;
die "Command failed with error: $?" if $?;
return $output;
}