Module: Platform

Defined in:
lib/commands/platform.rb

Class Method Summary collapse

Class Method Details

.call_status_api(hash) ⇒ Object

make api call to check platform status.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/commands/platform.rb', line 234

def self.call_status_api(hash)
	begin
		url = URI.parse("#{hash['parameters']['url']}/api/platforms/#{hash['parameters']['env']}/status_check")
		http = Net::HTTP.new(url.host, url.port)
		request = Net::HTTP::Get.new(url.request_uri)
		request.basic_auth(hash['parameters']['login'],hash['parameters']['password'])
		http.request(request)
	rescue Exception => e
		puts "[ ERROR ]".colorize(:red)+" Invalid url: Please enter a valid url.".colorize(:white)
		puts "[ EXCEPTION LOG DETAILS ]".colorize(:red)
		puts e
		exit
	end
end

.create(hash) ⇒ Object

create a new platform on cwp



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
# File 'lib/commands/platform.rb', line 52

def self.create(hash)
	bc = self.validate_blueprint(hash["parameters"]["blueprint"])
	oc =  self.organization_and_compute_resource(hash)
	begin
		url = URI.parse("#{hash['parameters']['url']}/api/platforms/deploy")
		http = Net::HTTP.new(url.host, url.port)
		request = Net::HTTP::Post.new(url.request_uri)
		request.set_form_data({ "baseline" => hash["parameters"]["baseline"], "instance" => hash["parameters"]["instance"], "reference_name" => hash["parameters"]["reference_name"], "yaml_content" => bc, "compute_resource_id" => oc["compute_resource_id"], "organization_id" => oc["organization_id"] })
		request.basic_auth(hash["parameters"]['login'],hash["parameters"]['password'])
		response = http.request(request)
	rescue Exception => e
		puts "[ ERROR ]".colorize(:red)+" Invalid url: Please enter a valid url.".colorize(:white)
		puts "[ EXCEPTION LOG DETAILS ]".colorize(:red)
		puts e
		exit
	end
	begin
		# Pretty print response json from API
		puts "[ RESPONSE CODE ]".colorize(:blue)
		puts response.code.colorize(:white)
		puts "[ RESPONSE JSON ]".colorize(:blue)
		puts JSON.pretty_generate(JSON.parse(response.body)).colorize(:white)
	rescue Exception => e
		puts response.body
	end
end

.fetch_compute_resource_id(params, compute_resources) ⇒ Object

Match for right compute resource and fetch the ID



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/commands/platform.rb', line 112

def self.fetch_compute_resource_id(params, compute_resources)
	compute_resource_id = ""
	unless compute_resources.nil?
		compute_resources.each do |cr|
			if cr["compute_resource_name"] == params["parameters"]["compute_resource"]
				compute_resource_id = cr["compute_resource_id"]
			end
		end
	end
	return compute_resource_id
end

.organization_and_compute_resource(params) ⇒ Object

Set organization and compute resource ID as setters



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/commands/platform.rb', line 95

def self.organization_and_compute_resource(params)
	begin
		url = URI.parse("#{params['parameters']['url']}/api/organizations/#{params['parameters']['organization']}/compute_resources")
		http = Net::HTTP.new(url.host, url.port)
		request = Net::HTTP::Get.new(url.request_uri)
		request.basic_auth(params["parameters"]['login'],params["parameters"]['password'])
		response = http.request(request)
		organization_compute_resources = JSON.parse(response.body)
		Hash["organization_id" => organization_compute_resources["organization_id"], "compute_resource_id" => self.fetch_compute_resource_id(params, organization_compute_resources["compute_resources"])]
	rescue Exception => e
		puts "[ ERROR ]".colorize(:red)+" Invalid url: Please enter a valid url.".colorize(:white)
		puts "[ EXCEPTION LOG DETAILS ]".colorize(:red)
		puts e
		exit
	end
end

print status check common for wait and status



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/commands/platform.rb', line 249

def self.print_status(response)
	begin
		# Pretty print response json from API
		puts "[ RESPONSE CODE ]".colorize(:blue)
		puts response.code.colorize(:white)
		puts "[ RESPONSE JSON ]".colorize(:blue)
		puts JSON.pretty_generate(JSON.parse(response.body)).colorize(:white)
	rescue Exception => e
		puts response.body
	end
end

.process_from_arguments(params) ⇒ Object

build hash from arguments



3
4
5
6
7
8
9
# File 'lib/commands/platform.rb', line 3

def self.process_from_arguments(params)
	if self.validate(params)
		self.create(params)
	else
		exit
	end
end

.validate(hash) ⇒ Object

validate hash either from yaml file or arguments



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
# File 'lib/commands/platform.rb', line 11

def self.validate(hash)
	has_error = true
	if(hash["parameters"]["blueprint"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" blueprint is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["url"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" url is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["reference_name"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" reference_name is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["baseline"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" baseline is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["instance"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" instance is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["compute_resource"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" compute_resource is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["organization"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" organization is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["login"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" login is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	if(hash["parameters"]["password"].nil?)
		has_error = false
		puts "[ ERROR ]".colorize(:red)+" password is mandatory. Please specify in configuration file or in command line.".colorize(:white)
	end
	return has_error
end

.validate_and_check_status(hash) ⇒ Object

method to return status log



150
151
152
153
154
155
# File 'lib/commands/platform.rb', line 150

def self.validate_and_check_status(hash)
	if self.validate_delete_and_status_params(hash)
		response = self.call_status_api(hash)
		self.print_status(response)
	end
end

.validate_and_check_wait_status(hash) ⇒ Object

wait status method



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/commands/platform.rb', line 157

def self.validate_and_check_wait_status(hash)
	if hash["parameters"]["status"].nil?
		puts "[ ERROR ]".colorize(:red)+" Please enter status=xxx".colorize(:white)
		self.validate_delete_and_status_params(hash)
		exit
	else
		if self.validate_delete_and_status_params(hash)
			timeout_limit = hash["parameters"]["timeout"].nil? ? "2h".split("") : hash["parameters"]["timeout"].split("")
			custom_timeout =
			begin 
				if (timeout_limit[1].downcase == "m")
					timeout_limit[0].to_i
				elsif (timeout_limit[1].downcase == "h")
					( timeout_limit[0].to_i * 60 )
				elsif (timeout_limit[1].downcase == "d")
					( 24*60*timeout_limit[0].to_i )
				else
					( 2*60 )
				end
			rescue Exception => e
				( 2*60 )
			end
			# untill custom timeout is expired. 
			(custom_timeout+1).times do |num|
				if ( num == custom_timeout )
					puts "[ TIMEOUT ]".colorize(:red)+" error status code 2".colorize(:white)
					exit
				else
					response = self.call_status_api(hash) 
					begin
						body = JSON.parse(response.body)
						if (body["deploy_status"]["status"] == "failed")
							puts "[ PLATFORM DEPLOYMENT FAILED ]".colorize(:red)+" error status code 1".colorize(:white)
							self.print_status(response)
							break
						else
							if (body["deploy_status"]["status"] == hash["parameters"]["status"])
								puts "[ PLATFORM DEPLOYED SUCCESSFULLY ]".colorize(:green)+" error status code 0".colorize(:white)
								self.print_status(response)
								break
							else
								self.print_status(response)
							end
						end
					rescue Exception => e
						self.print_status(response)
						break
					end	
				end
				puts "[ CHECKING STATUS PLEASE WAIT ] ".colorize(:yellow)
				sleep(60)
			end
		end
	end
end

.validate_blueprint(blueprint) ⇒ Object

validate the blueprint file path in params



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/commands/platform.rb', line 79

def self.validate_blueprint(blueprint)
	if (File.extname(blueprint) != ".yaml")
		puts "[ ERROR ]".colorize(:red)+" Invalid blueprint file, File should be of .yaml extension.".colorize(:white)
		exit
	else
		begin
			File.read(blueprint)
		rescue Exception => e
			puts "[ ERROR ]".colorize(:red)+" Could not read blueprint file path, upload a valid file path.".colorize(:white)
			puts "[ EXCEPTION LOG DETAILS ]".colorize(:red)
			puts e
			exit
		end
	end
end

.validate_delete_and_status_params(hash) ⇒ Object

validate delete hash



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/commands/platform.rb', line 213

def self.validate_delete_and_status_params(hash)
	has_error = true
	if hash["parameters"]["env"].nil?
		puts "[ ERROR ]".colorize(:red)+" Please enter env=environment_name".colorize(:white)
		has_error = false
	end
	if hash["parameters"]["login"].nil?
		puts "[ ERROR ]".colorize(:red)+" Please enter login=xyz".colorize(:white)
		has_error = false
	end
	if hash["parameters"]["password"].nil?
		puts "[ ERROR ]".colorize(:red)+" Please enter password=***".colorize(:white)
		has_error = false
	end
	if hash["parameters"]["url"].nil?
		puts "[ ERROR ]".colorize(:red)+" Please enter url".colorize(:white)
		has_error = false
	end
	return has_error
end

.validate_delete_platform(hash) ⇒ Object

Destroy a platform from cwp



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/commands/platform.rb', line 124

def self.validate_delete_platform(hash)
		if self.validate_delete_and_status_params(hash)
			begin
				url = URI.parse("#{hash['parameters']['url']}/api/platforms/#{hash['parameters']['env']} ")
				http = Net::HTTP.new(url.host, url.port)
				request = Net::HTTP::Delete.new(url.request_uri)
				request.basic_auth(hash['parameters']['login'],hash['parameters']['password'])
				response = http.request(request)
			rescue Exception => e
				puts "[ ERROR ]".colorize(:red)+" Invalid url: Please enter a valid url.".colorize(:white)
				puts "[ EXCEPTION LOG DETAILS ]".colorize(:red)
				puts e
				exit
			end
			begin
        # Pretty print response json from API
        puts "[ RESPONSE CODE ]".colorize(:blue)
        puts response.code.colorize(:white)
        puts "[ RESPONSE JSON ]".colorize(:blue)
        puts JSON.pretty_generate(JSON.parse(response.body)).colorize(:white)
    rescue Exception => e
    	puts response.body
    end
end
end