Skip to content

Commit

Permalink
Use INotify to wait for ansible-runner pid file
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Oct 7, 2020
1 parent 30a0e1a commit 89af2a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ gem "query_relation", "~>0.1.0", :require => false
gem "rails", "~>5.2.4", ">=5.2.4.4"
gem "rails-i18n", "~>5.x"
gem "rake", ">=12.3.3", :require => false
gem "rb-inotify", "~>0.10.0", :require => false
gem "rest-client", "~>2.1.0", :require => false
gem "ripper_ruby_parser", "~>1.5.1", :require => false
gem "ruby-progressbar", "~>1.7.0", :require => false
Expand Down
29 changes: 29 additions & 0 deletions lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ def run_via_cli(hosts, credentials, env_vars, extra_vars, tags: nil, ansible_run

begin
fetch_galaxy_roles(playbook_or_role_args)

notifier = notifier_for(pid_file(base_dir))
result = AwesomeSpawn.run("ansible-runner", :env => env_vars_hash, :params => params)
wait_for(notifier)

res = response(base_dir, ansible_runner_method, result)
ensure
# Clean up the tmp dir for the sync method, for async we will clean it up after the job is finished and we've
Expand Down Expand Up @@ -354,6 +358,31 @@ def env_dir(base_dir)
FileUtils.mkdir_p(File.join(base_dir, "env")).first
end

def pid_file(base_dir)
File.join(base_dir, "pid")
end

def notifier_for(path)
require "rb-inotify"

directory_name = File.dirname(path)

@path_created = Concurrent::Event.new

notifier = INotify::Notifier.new
notifier.watch(directory_name, :moved_to, :create) do |event|
@path_created.set if File.join(directory_name, event.name) == path
end

notifier
end

def wait_for(notifier)
until @path_created.wait(0)
notifier.process
end
end

PYTHON2_MODULE_PATHS = %w[
/var/lib/manageiq/venv/lib/python2.7/site-packages
].freeze
Expand Down

0 comments on commit 89af2a2

Please sign in to comment.