diff --git a/Gemfile b/Gemfile index e01a2f121c0..3eff8edf352 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/lib/ansible/runner.rb b/lib/ansible/runner.rb index 5eb99ec8a5b..c6a57bd3028 100644 --- a/lib/ansible/runner.rb +++ b/lib/ansible/runner.rb @@ -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) - result = AwesomeSpawn.run("ansible-runner", :env => env_vars_hash, :params => params) + + result = wait_for(pid_file(base_dir)) do + AwesomeSpawn.run("ansible-runner", :env => env_vars_hash, :params => params) + end + 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 @@ -354,6 +358,37 @@ 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 wait_for(path) + require "rb-inotify" + dir_name = File.dirname(path) + file_name = File.basename(path) + + path_created = Concurrent::Event.new + + notifier = INotify::Notifier.new + notifier.watch(dir_name, :moved_to, :create) do |event| + # Once the file we are looking for is created set the concurrent event + path_created.set if event.name == file_name + end + + thread = Thread.new { notifier.run } + + res = yield + + # Wait for the target file to exist + path_created.wait + + # Shutdown the inotify loop and join the thread + notifier.close + thread.join + + res + end + PYTHON2_MODULE_PATHS = %w[ /var/lib/manageiq/venv/lib/python2.7/site-packages ].freeze