-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcleanup.rb
89 lines (75 loc) · 3.54 KB
/
cleanup.rb
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
#
# Copyright: Copyright 2013-2020 Chef Software, Inc.
# License: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Author:: Siddheshwar More (<[email protected]>)
require "mixlib/shellout"
module CleanupTestResources
def self.validate_params
unset_env_var = []
# OPENSTACK_USERNAME, OPENSTACK_PASSWORD and OPENSTACK_AUTH_URL are mandatory params to run knife openstack commands.
%w{OPENSTACK_USERNAME OPENSTACK_PASSWORD OPENSTACK_AUTH_URL}.each do |os_env_var|
unset_env_var << os_env_var if ENV[os_env_var].nil?
end
err_msg = "\nPlease set #{unset_env_var.join(", ")} environment"
err_msg = err_msg + (unset_env_var.length > 1 ? " variables " : " variable ") + "to cleanup test resources."
unless unset_env_var.empty?
puts err_msg
exit 1
end
end
# Use Mixlib::ShellOut to run knife openstack commands.
def self.run(command_line)
shell_out = Mixlib::ShellOut.new("#{command_line}")
shell_out.timeout = 3000
shell_out.run_command
shell_out
end
# Use knife openstack to delete servers.
def self.cleanup_resources
delete_resources = []
# Openstack credentials use during knife openstack command run.
openstack_creds = "--openstack-username '#{ENV["OPENSTACK_USERNAME"]}' --openstack-password '#{ENV["OPENSTACK_PASSWORD"]}' --openstack-api-endpoint #{ENV["OPENSTACK_AUTH_URL"]}"
# List all servers in openstack using knife openstack server list command.
list_command = "knife openstack server list #{openstack_creds}"
list_output = run(list_command)
# Check command exitstatus. Non zero exitstatus indicates command execution fails.
if list_output.exitstatus != 0
puts "Cleanup Test Resources failed. Please check Openstack user name, password and auth url are correct. Error: #{list_output.stderr}."
exit list_output.exitstatus
else
servers = list_output.stdout
end
# We use "os-integration-test-<platform>-<randomNumber>" pattern for server name during integration tests run. So use "os-integration-test-" pattern to find out servers created during integration tests run.
servers.each_line do |line|
if line.include?("os-integration-test-") || (line.include?("openstack-") && line.include?("opscode-ci-ssh"))
# Extract and add instance id of server to delete_resources list.
delete_resources << { "id" => line.split(" ").first, "name" => line.split(" ")[1] }
end
end
# Delete servers
delete_resources.each do |resource|
delete_command = "knife openstack server delete #{resource["id"]} #{openstack_creds} --yes"
delete_output = run(delete_command)
# check command exitstatus. Non zero exitstatus indicates command execution fails.
if delete_output.exitstatus != 0
puts "Unable to delete server #{resource["name"]}: #{resource["id"]}. Error: #{delete_output.stderr}."
else
puts "Deleted server #{resource["name"]}: #{resource["id"]}."
end
end
end
end
CleanupTestResources.validate_params
CleanupTestResources.cleanup_resources