evrone-ci-router 0.2.0.pre0 → 0.2.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/bin/evrone-ci-router +5 -0
  3. data/fixtures/jobs_publisher +13 -0
  4. data/lib/evrone/ci/router.rb +112 -37
  5. data/lib/evrone/ci/router/build.rb +29 -3
  6. data/lib/evrone/ci/router/build_matrix.rb +7 -1
  7. data/lib/evrone/ci/router/cli.rb +67 -0
  8. data/lib/evrone/ci/router/configuration.rb +7 -1
  9. data/lib/evrone/ci/router/consumers/builds_consumer.rb +1 -1
  10. data/lib/evrone/ci/router/initializers/amqp.rb +2 -1
  11. data/lib/evrone/ci/router/{queue.rb → script_builder.rb} +10 -6
  12. data/lib/evrone/ci/router/{middleware/travis → script_builder}/env.rb +1 -1
  13. data/lib/evrone/ci/router/{middleware/travis → script_builder}/ruby.rb +1 -1
  14. data/lib/evrone/ci/router/{middleware/travis → script_builder}/script.rb +1 -1
  15. data/lib/evrone/ci/router/travis.rb +2 -2
  16. data/lib/evrone/ci/router/version.rb +1 -1
  17. data/spec/lib/build_matrix_spec.rb +13 -2
  18. data/spec/lib/build_spec.rb +33 -0
  19. data/spec/lib/configuration_spec.rb +4 -2
  20. data/spec/lib/router_spec.rb +116 -0
  21. data/spec/lib/{queue_spec.rb → script_builder_spec.rb} +7 -7
  22. data/spec/lib/travis_spec.rb +2 -2
  23. metadata +18 -38
  24. data/bin/cli +0 -3
  25. data/bin/git_ssh +0 -3
  26. data/bin/jobs_publisher +0 -12
  27. data/bin/workers +0 -13
  28. data/lib/evrone/ci/router/middleware/create_build_matrix.rb +0 -54
  29. data/lib/evrone/ci/router/middleware/create_dirs.rb +0 -25
  30. data/lib/evrone/ci/router/middleware/fetch_commit_info.rb +0 -22
  31. data/lib/evrone/ci/router/middleware/fetch_source.rb +0 -36
  32. data/lib/evrone/ci/router/middleware/log_build.rb +0 -24
  33. data/lib/evrone/ci/router/middleware/update_build_status.rb +0 -75
  34. data/spec/lib/middleware/create_build_matrix_spec.rb +0 -73
  35. data/spec/lib/middleware/create_dirs_spec.rb +0 -26
  36. data/spec/lib/middleware/fetch_commit_info_spec.rb +0 -23
  37. data/spec/lib/middleware/fetch_source_spec.rb +0 -27
  38. data/spec/lib/middleware/log_build_spec.rb +0 -14
  39. data/spec/lib/middleware/update_build_status_spec.rb +0 -70
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require File.expand_path("../../lib/evrone/ci/router", __FILE__)
4
-
5
- trap('INT') {
6
- Thread.new do
7
- Evrone::Common::AMQP.shutdown
8
- end.join
9
- }
10
-
11
- Evrone::Common::AMQP::Supervisor::Threaded.build(
12
- Evrone::CI::Router::BuildsConsumer => 1,
13
- ).run
@@ -1,54 +0,0 @@
1
- module Evrone
2
- module CI
3
- class Router
4
- module Middleware
5
-
6
- CreateBuildMatrix = Struct.new(:app) do
7
-
8
- include Helper::Logger
9
-
10
- def call(env)
11
- if create_build_matrix_and_delivery(env)
12
- app.call env
13
- else
14
- -1
15
- end
16
- end
17
-
18
- private
19
-
20
- def create_build_matrix_and_delivery(env)
21
- source_travis = load_travis(env.repo_dir)
22
- if source_travis
23
- matrix = Router::BuildMatrix.new source_travis
24
- update_build env.build, matrix
25
-
26
- matrix.travises.each_with_index do |travis, idx|
27
- delivery_job env.build, travis, idx+1
28
- end
29
- true
30
- else
31
- false
32
- end
33
- end
34
-
35
- def delivery_job(build, travis, number)
36
- message = build.to_perform_job_message travis, number
37
- logger.info "delivery job #{message.id}.#{number} #{travis.to_matrix_s}"
38
- Router::JobsConsumer.publish message
39
- end
40
-
41
- def update_build(build, matrix)
42
- build.matrix = matrix.keys
43
- build.jobs_count = matrix.travises.size
44
- end
45
-
46
- def load_travis(repo_dir)
47
- Router::Travis.from_file repo_dir.join(".travis.yml")
48
- end
49
-
50
- end
51
- end
52
- end
53
- end
54
- end
@@ -1,25 +0,0 @@
1
- require 'evrone/ci/common'
2
-
3
- module Evrone
4
- module CI
5
- class Router
6
- module Middleware
7
-
8
- CreateDirs = Struct.new(:app) do
9
-
10
- include Common::Helper::Shell
11
- include Helper::Config
12
-
13
- def call(env)
14
- env.repo_dir = env.path_prefix.join(config.repo_dir_name)
15
- mkdir env.repo_dir
16
-
17
- app.call env
18
- end
19
-
20
- end
21
- end
22
- end
23
- end
24
- end
25
-
@@ -1,22 +0,0 @@
1
- require 'evrone/ci/common'
2
-
3
- module Evrone
4
- module CI
5
- class Router
6
- module Middleware
7
-
8
- FetchCommitInfo = Struct.new(:app) do
9
-
10
- def call(env)
11
- if env.scm
12
- env.build.commit_info = env.scm.commit_info
13
- end
14
- app.call env
15
- end
16
-
17
- end
18
- end
19
- end
20
- end
21
- end
22
-
@@ -1,36 +0,0 @@
1
- require 'evrone/ci/common'
2
-
3
- module Evrone
4
- module CI
5
- class Router
6
- module Middleware
7
-
8
- FetchSource = Struct.new(:app) do
9
-
10
- include Helper::Logger
11
-
12
- def call(env)
13
- env.scm = create_scm(env)
14
- if env.scm.fetch == 0
15
- app.call env
16
- else
17
- -1
18
- end
19
- end
20
-
21
- private
22
-
23
- def create_scm(env)
24
- SCM::Git.new(env.build.message.src,
25
- env.build.message.sha,
26
- env.repo_dir,
27
- deploy_key: env.build.message.deploy_key,
28
- &env.build.method(:add_to_output))
29
- end
30
-
31
- end
32
- end
33
- end
34
- end
35
- end
36
-
@@ -1,24 +0,0 @@
1
- module Evrone
2
- module CI
3
- class Router
4
- module Middleware
5
-
6
- LogBuild = Struct.new(:app) do
7
-
8
- include Router::Helper::Logger
9
-
10
- def call(env)
11
- logger.tagged("BUILD #{env.build.message.id}") do
12
- logger.info "starting build"
13
- rs = app.call env
14
- logger.info "done build"
15
- rs
16
- end
17
- end
18
-
19
- end
20
- end
21
- end
22
- end
23
- end
24
-
@@ -1,75 +0,0 @@
1
- module Evrone
2
- module CI
3
- class Router
4
- module Middleware
5
-
6
- UpdateBuildStatus = Struct.new(:app) do
7
-
8
- include Helper::Logger
9
-
10
- STARTED = 2
11
- FINISHED = 3
12
- BROKEN = 4
13
- FAILED = 5
14
-
15
- def call(env)
16
-
17
- update_status env.build, STARTED
18
- rs = -1
19
- begin
20
- rs = app.call env
21
- rescue Exception => e
22
- logger.error("ERROR: #{e.inspect}\n BACKTRACE:\n#{e.backtrace.map{|i| " #{i}" }.join("\n")}")
23
- end
24
-
25
- case
26
- when rs == 0
27
- update_status env.build, FINISHED
28
- when rs > 0
29
- update_status env.build, BROKEN
30
- when rs < 0
31
- update_status env.build, FAILED
32
- end
33
-
34
- rs
35
- end
36
-
37
- private
38
-
39
- def update_status(build, status)
40
- publish_status create_message(build, status), status
41
- end
42
-
43
- def create_message(build, status)
44
- tm = Time.now
45
- attributes = {
46
- build_id: build.message.id,
47
- status: status,
48
- tm: tm.to_i,
49
- tm_usec: tm.usec,
50
- matrix: build.matrix || [],
51
- jobs_count: build.jobs_count || 0,
52
- }
53
-
54
- if build.commit_info
55
- attributes.merge!(
56
- commit_author: build.commit_info.author,
57
- commit_author_email: build.commit_info.email,
58
- commit_sha: build.commit_info.sha,
59
- commit_message: build.commit_info.message
60
- )
61
- end
62
- Message::BuildStatus.new attributes
63
- end
64
-
65
- def publish_status(message, status)
66
- logger.info "delivered build status #{message.inspect}"
67
- BuildStatusConsumer.publish message
68
- end
69
-
70
- end
71
- end
72
- end
73
- end
74
- end
75
-
@@ -1,73 +0,0 @@
1
- require 'spec_helper'
2
- require 'pathname'
3
-
4
- describe Evrone::CI::Router::Middleware::CreateBuildMatrix do
5
- let(:app) { ->(_) { 0 } }
6
- let(:build) { create :build }
7
- let(:env) { OpenStruct.new build: build, repo_dir: repo_dir }
8
- let(:repo_dir) { Pathname.new fixture_path("repo") }
9
- let(:matrix) { described_class.new app }
10
-
11
- it { should be }
12
-
13
- context "call" do
14
- let(:travis) { create :travis, attributes: { rvm: %w{ 1.9.3 2.0.0 } } }
15
- subject { matrix.call env }
16
-
17
- context "successfuly" do
18
- before do
19
- mock(matrix).load_travis(repo_dir) { travis }
20
- end
21
-
22
- it { should eq 0 }
23
-
24
- it "should create and delivery jobs " do
25
- expect{ subject }.to change{
26
- Evrone::CI::Router::JobsConsumer.messages.count
27
- }.by(2)
28
- end
29
-
30
- context "first delivered job" do
31
- let(:m) { Evrone::CI::Router::JobsConsumer.messages.first }
32
- subject { m }
33
- before { matrix.call env }
34
-
35
- its("job_id") { should eq 1 }
36
- end
37
-
38
- context "last delivered job" do
39
- let(:m) { Evrone::CI::Router::JobsConsumer.messages.last }
40
- subject { m }
41
- before { matrix.call env }
42
-
43
- its("job_id") { should eq 2 }
44
- end
45
-
46
- context "update build" do
47
- it "should assign jobs_count" do
48
- expect{ subject }.to change(build, :jobs_count).from(nil).to(2)
49
- end
50
-
51
- it "should assign matrix" do
52
- expect{ subject }.to change(build, :matrix).from(nil).to(%w{rvm})
53
- end
54
- end
55
- end
56
-
57
- context "when travis.yml not found" do
58
-
59
- before do
60
- mock(Evrone::CI::Router::Travis).from_file(anything){ nil }
61
- end
62
-
63
- it "should return -1" do
64
- expect(subject).to eq(-1)
65
- end
66
-
67
- it "cannot delivery any job" do
68
- expect{ subject }.to_not change(Evrone::CI::Router::JobsConsumer.messages, :size)
69
- end
70
-
71
- end
72
- end
73
- end
@@ -1,26 +0,0 @@
1
- require 'spec_helper'
2
- require 'pathname'
3
- require 'fileutils'
4
-
5
- describe Evrone::CI::Router::Middleware::CreateDirs do
6
- let(:build) { create :build }
7
- let(:path_prefix) { Pathname.new '/tmp/.test' }
8
- let(:app) { ->(_) { 0 } }
9
- let(:env) { OpenStruct.new build: build, path_prefix: path_prefix }
10
-
11
- subject { described_class.new app }
12
-
13
- before { FileUtils.rm_rf path_prefix }
14
- after { FileUtils.rm_rf path_prefix }
15
-
16
- context "call" do
17
- subject{ described_class.new(app).call env }
18
- it { should eq 0 }
19
-
20
- it "should create and assign to env repo_dir" do
21
- subject
22
- expect(env.repo_dir.to_s).to eq '/tmp/.test/repo'
23
- expect(File.directory? env.repo_dir).to be
24
- end
25
- end
26
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Evrone::CI::Router::Middleware::FetchCommitInfo do
4
- let(:build) { create :build }
5
- let(:env) { OpenStruct.new build: build, scm: scm }
6
- let(:app) { ->(_) { 0 } }
7
- let(:commit_info) { 'commit_info' }
8
- let(:scm) { 'scm' }
9
- subject { described_class.new(app).call env }
10
-
11
- before do
12
- mock(scm).commit_info { commit_info }
13
- end
14
-
15
- it { should eq 0 }
16
-
17
- it "should assign commit info to build" do
18
- subject
19
- expect(build.commit_info).to eq commit_info
20
- end
21
-
22
- end
23
-
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
- require 'pathname'
3
- require 'fileutils'
4
-
5
- describe Evrone::CI::Router::Middleware::FetchSource do
6
- let(:path_prefix) { '/tmp/.test' }
7
- let(:build) { create :build, options }
8
- let(:options) { {} }
9
- let(:repo_dir) { Pathname.new '/tmp/.test' }
10
- let(:app) { ->(_) { 0 } }
11
- let(:env) { OpenStruct.new build: build, repo_dir: repo_dir }
12
- subject { described_class.new(app).call env }
13
-
14
- before { FileUtils.rm_rf(repo_dir) }
15
- after { FileUtils.rm_rf(repo_dir) }
16
-
17
- it "should checkout repo" do
18
- expect(subject).to eq 0
19
- expect(File.directory? "/tmp/.test/.git").to be
20
- end
21
-
22
- context "when failed" do
23
- let(:options) { { src: "/not-exists.git" } }
24
- it { should eq(-1) }
25
- end
26
- end
27
-
@@ -1,14 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Evrone::CI::Router::Middleware::LogBuild do
4
- let(:build) { create :build }
5
- let(:app) { ->(_) { 0 } }
6
- let(:env) { OpenStruct.new build: build }
7
-
8
- subject { described_class.new app }
9
-
10
- context "call" do
11
- subject{ described_class.new(app).call env }
12
- it { should eq 0 }
13
- end
14
- end
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Evrone::CI::Router::Middleware::UpdateBuildStatus do
4
- let(:exit_code) { 0 }
5
- let(:app) { ->(_) { exit_code } }
6
- let(:build) { create :build }
7
- let(:env) { OpenStruct.new build: build }
8
- let(:mid) { described_class.new app }
9
- let(:messages) { Evrone::CI::Router::BuildStatusConsumer.messages }
10
-
11
- subject { mid.call env }
12
-
13
- it "should delivery 2 messages" do
14
- expect {
15
- subject
16
- }.to change(messages, :size).by(2)
17
- end
18
-
19
- { 0 => 3, 1 => 4, -1 => 5 }.each do |code, status|
20
- context "when exit code is #{code}" do
21
- let(:exit_code) { code }
22
- it { should eq code }
23
-
24
- context "messages" do
25
- before { mid.call env }
26
-
27
- context "first" do
28
- subject { messages.first }
29
- it_should_behave_like "UpdateBuildStatus message" do
30
- its(:status) { should eq 2 }
31
- end
32
- end
33
-
34
- context "last" do
35
- subject { messages.last }
36
- it_should_behave_like "UpdateBuildStatus message" do
37
- its(:status) { should eq status }
38
- end
39
- end
40
-
41
- end
42
- end
43
- end
44
-
45
- context "when raise exception" do
46
- let(:app) { ->(_) { raise "Ignore Me" } }
47
- it { should eq(-1) }
48
-
49
- context "messages" do
50
- before { mid.call env }
51
-
52
- context "first" do
53
- subject { messages.first }
54
- it_should_behave_like "UpdateBuildStatus message" do
55
- its(:status) { should eq 2 }
56
- end
57
- end
58
-
59
- context "last" do
60
- subject { messages.last }
61
- it_should_behave_like "UpdateBuildStatus message" do
62
- its(:status) { should eq 5 }
63
- end
64
- end
65
-
66
- end
67
- end
68
-
69
- end
70
-