Class: DockerEndpoint

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/continuous_integration/tasks.rb

Overview

Class to perform operations on receiving http calls

Instance Method Summary collapse

Constructor Details

#initialize(_param) ⇒ DockerEndpoint

Returns a new instance of DockerEndpoint.



6
7
8
9
10
11
12
13
14
15
# File 'lib/continuous_integration/tasks.rb', line 6

def initialize(_param)
  @resource_path = '/cards'
  @repo_name = ''
  @process_status = ''
  @result = ''
  @result_api = ''
  @result_ui = ''
  @git_branch = ''
@response = ''
end

Instance Method Details

#do_POST(request, _response) ⇒ Object

curl localhost:8080/docker POST call made by quay to CI server



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
# File 'lib/continuous_integration/tasks.rb', line 19

def do_POST(request, _response)

  #print "Request is #{request}"
  # parse JSOn to hash key
  request_hash = JSON.parse(request.body, symbolize_names: true)

  begin
  # fetch the github repo name
  @repo_name = request_hash[:name]

  # fetch the github branch name
  @git_branch = request_hash[:trigger_metadata][:ref]
  @git_branch = @git_branch.split('/')
  @git_branch = @git_branch.last

#loop in case already processing a request
while true
	if $LOCK
		sleep 10
		puts "Waiting"
	else
		puts "File not there"
		break
	end
end

perform_operations

  _response.body = JSON.generate @response
  _response['Content-Type'] = 'application/json'

  rescue StandardError => error
	
	#unset lock on exceptions
	$LOCK = false

	print "Not a valid Quay post " + error.to_s
  end
end

#docker_updateObject

update docker images locally



78
79
80
81
82
83
84
85
# File 'lib/continuous_integration/tasks.rb', line 78

def docker_update
  Dir.chdir(DOCKER_PATH) do
    # perform docker images update
    `sudo docker-compose kill`
    `sudo docker-compose rm`
    `sudo docker-compose up -d`
  end
end

#generate_logObject



110
111
112
113
114
115
116
# File 'lib/continuous_integration/tasks.rb', line 110

def generate_log
  @response = {
    'Docker logs' => @process_status,
    'API logs' => @result_api,
    'SE logs' => @result_ui
  }
end

#perform_operationsObject

method to perform various CI operations



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/continuous_integration/tasks.rb', line 60

def perform_operations

	#set lock
	$LOCK = true

	docker_update
	sleep 10

	run_api_tests
	run_ui_tests
	generate_log
	slack_post

	#unset lock
	$LOCK = false
end

#run_api_testsObject

Action - run api tests



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/continuous_integration/tasks.rb', line 88

def run_api_tests
  Dir.chdir(API_SPECS_PATH) do
    # shell command to run my tests using rake.
    # I am using `aha` shell tool to grab shell output to html
    cmd = "RUBYOPT='-W0' HOST=#{HOST} RMQ_HOST=#{HOST} RMQ_VHOST=/
    		bundle exec rake cars:#{@git_branch} | aha --black
    		>logs/#{@git_branch}/$(date +\%d-\%m-\%Y-\%H-\%s)-API-run.htm"
    @result_api = `#{cmd}`
    # or use ` .... ` instead of %x[ .... ] to run commands
  end
end

#run_ui_testsObject



100
101
102
103
104
105
106
107
108
# File 'lib/continuous_integration/tasks.rb', line 100

def run_ui_tests
  Dir.chdir(UI_SPECS_PATH) do
    # navigate to specs dir
    cmd = "HOST='https://#{HOST}' BROWSER=phantomjs SCREENS=true
    	bundle exec rake selenium:#{@git_branch} |
    	aha --black >logs/#{@git_branch}/$(date +\%d-\%m-\%Y-\%H-\%s)-UI-run.htm"
    @result_ui = `#{cmd}`
  end
end

#slack_postObject



118
119
120
# File 'lib/continuous_integration/tasks.rb', line 118

def slack_post
  # curl to post to slack for the team to see the results
end