diff --git a/bin/runner b/bin/runner index 0698591..c80151c 100755 --- a/bin/runner +++ b/bin/runner @@ -4,5 +4,5 @@ require 'bundler/setup' require_relative '../lib/runner' -GitlabCi::Runner.new +GitlabCi::Runner.new(ARGV.fetch(0, '0')) exit diff --git a/lib/build.rb b/lib/build.rb index 4788187..1a4a038 100644 --- a/lib/build.rb +++ b/lib/build.rb @@ -13,7 +13,8 @@ class Build attr_accessor :id, :commands, :ref, :tmp_file_path, :output, :before_sha, :run_at, :post_message - def initialize(data) + def initialize(runner, data) + @runner = runner @output = "" @post_message = "" @commands = data[:commands].to_a @@ -182,7 +183,7 @@ def repo_exists? end def config - @config ||= Config.new + @config ||= Config.new(@runner) end def project_dir diff --git a/lib/config.rb b/lib/config.rb index ab8ca35..2f72f79 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -9,7 +9,8 @@ module GitlabCi class Config attr_reader :config - def initialize + def initialize(runner) + @runner = runner if File.exists?(config_path) @config = YAML.load_file(config_path) else @@ -26,7 +27,8 @@ def url end def builds_dir - @builds_path ||= File.join($root_path, 'tmp', 'builds') + rid = "runner-" + @runner.to_s + @builds_path ||= File.join($root_path, "tmp", "builds", rid) end def write(key, value) diff --git a/lib/network.rb b/lib/network.rb index 8bdda8a..a8ed071 100644 --- a/lib/network.rb +++ b/lib/network.rb @@ -7,6 +7,10 @@ module GitlabCi class Network include HTTParty + def initialize(runner) + @runner = runner + end + # check for available build from coordinator # and pick a pending one # { @@ -95,7 +99,7 @@ def register_runner(token) private def broadcast message - print "#{Time.now.to_s} | #{message}" + print "#{Time.now.to_s} | #{@runner} | #{message}" end def api_url @@ -107,7 +111,7 @@ def token end def config - @config ||= Config.new + @config ||= Config.new(@runner) end def default_options diff --git a/lib/runner.rb b/lib/runner.rb index 0430164..7a9ce0b 100644 --- a/lib/runner.rb +++ b/lib/runner.rb @@ -5,8 +5,9 @@ module GitlabCi class Runner attr_accessor :current_build, :thread - def initialize - puts '* Gitlab CI Runner started' + def initialize(runner) + @runner = runner + puts "* Gitlab CI Runner #{@runner} started" puts '* Waiting for builds' loop do if running? @@ -34,7 +35,7 @@ def abort_if_timeout def update_build return unless @current_build.completed? - puts "#{Time.now.to_s} | Completed build #{@current_build.id}, #{@current_build.state}." + puts "#{Time.now.to_s} | Completed build #{@current_build.id}, #{@current_build.state} on runner #{@runner}." # Make sure we push latest build info submitted # before we clean build @@ -70,14 +71,14 @@ def get_build end def network - @network ||= Network.new + @network ||= Network.new(@runner) end def run(build_data) - @current_build = GitlabCi::Build.new(build_data) - puts "#{Time.now.to_s} | Starting new build #{@current_build.id}..." + @current_build = GitlabCi::Build.new(@runner, build_data) + puts "#{Time.now.to_s} | Starting new build #{@current_build.id} with runner #{@runner}..." @current_build.run - puts "#{Time.now.to_s} | Build #{@current_build.id} started." + puts "#{Time.now.to_s} | Build #{@current_build.id} on runner #{@runner} started." end def collect_trace diff --git a/lib/setup.rb b/lib/setup.rb index 910d19c..fd0b7cc 100644 --- a/lib/setup.rb +++ b/lib/setup.rb @@ -47,7 +47,7 @@ def register_runner def write_token(token) puts "Runner token: #{token}" - Config.new.write('token', token) + Config.new(0).write("token", token) end end end diff --git a/lib/support/init.d/gitlab_ci_runner b/lib/support/init.d/gitlab_ci_runner index a8a2492..289ff1a 100644 --- a/lib/support/init.d/gitlab_ci_runner +++ b/lib/support/init.d/gitlab_ci_runner @@ -21,7 +21,8 @@ NAME="gitlab-ci-runner" DESC="GitLab CI runner" RUNNERS_REGISTERED=0 RUNNERS_RUNNING=0 -INIT_LOG="/var/log/gitlab_ci_runner.log" +INIT_LOG='/var/log/gitlab_ci_runner.log' +#INIT_LOG='/var/log/gitlab_ci_runner_$RUNNER.log' check_pid() { # Number of registered runners in PID file @@ -54,13 +55,16 @@ start() { # Spawn runners for (( i=1; i<=$RUNNERS_NUM; i++ )) do + # eval is required for making dedicated logs for each logger. Then use $RUNNER substitution in INIT_LOG variable + RUNNER=$((i-1)) + eval "LOGFILE=\"$INIT_LOG\"" # Check log file - if [ ! -f $INIT_LOG ]; then - touch $INIT_LOG - chown $APP_USER $INIT_LOG + if [ ! -f $LOGFILE ]; then + touch $LOGFILE + chown $APP_USER $LOGFILE fi echo "Starting runner #$i" - execute "$START_RUNNER >> $INIT_LOG 2>&1 & echo \$! >> $RUNNERS_PID" + execute "$START_RUNNER $i >> $LOGFILE 2>&1 & echo \$! >> $RUNNERS_PID" done echo "SUCCESS: Started $RUNNERS_NUM $DESC(s)." fi @@ -99,7 +103,7 @@ stop() { ps -C "$PROCESS_NAME" -o "%p" h | xargs kill -USR2 [ $? -eq 0 ] && echo "OK" else - echo "No ghost runners have been found.This is good." + echo "No ghost runners have been found. This is good." fi } diff --git a/spec/build_spec.rb b/spec/build_spec.rb index 0d4fc67..b302ada 100644 --- a/spec/build_spec.rb +++ b/spec/build_spec.rb @@ -3,7 +3,7 @@ describe 'Build' do describe :run do - let(:build) { GitlabCi::Build.new(build_data) } + let(:build) { GitlabCi::Build.new(0, build_data) } before do build.run