diff --git a/lib/barbeque/executor/hako.rb b/lib/barbeque/executor/hako.rb index 43c8835..056bee0 100644 --- a/lib/barbeque/executor/hako.rb +++ b/lib/barbeque/executor/hako.rb @@ -14,11 +14,20 @@ class HakoCommandError < StandardError # @param [String] hako_dir # @param [Hash] hako_env - # @param [String] yaml_dir - def initialize(hako_dir:, hako_env: {}, yaml_dir:, oneshot_notification_prefix:) + # @param [String] definition_dir + # @param [String] yaml_dir (deprecated: renamed to definition_dir) + def initialize(hako_dir:, hako_env: {}, yaml_dir: nil, definition_dir: nil, oneshot_notification_prefix:) @hako_dir = hako_dir @hako_env = hako_env - @yaml_dir = yaml_dir + @definition_dir = + if definition_dir + definition_dir + elsif yaml_dir + warn 'yaml_dir option is renamed to definition_dir. Please update config/barbeque.yml' + yaml_dir + else + raise ArgumentError.new('definition_dir is required') + end @hako_s3_client = HakoS3Client.new(oneshot_notification_prefix) end @@ -104,10 +113,19 @@ def poll_retry(job_retry) private def build_hako_oneshot_command(docker_image, command, envs) - [ - 'bundle', 'exec', 'hako', 'oneshot', '--no-wait', '--tag', docker_image.tag, - *env_options(envs), File.join(@yaml_dir, "#{docker_image.repository}.yml"), '--', *command, - ] + jsonnet_path = File.join(@definition_dir, "#{docker_image.repository}.jsonnet") + yaml_path = File.join(@definition_dir, "#{docker_image.repository}.yml") + + cmd = ['bundle', 'exec', 'hako', 'oneshot', '--no-wait', '--tag', docker_image.tag, *env_options(envs)] + if File.readable?(jsonnet_path) + cmd << jsonnet_path + elsif File.readable?(yaml_path) + cmd << yaml_path + else + raise HakoCommandError.new("No definition found matching '#{docker_image.repository}' in #{@definition_dir}") + end + cmd << '--' + cmd.concat(command) end def env_options(envs) diff --git a/spec/barbeque/executor/hako_spec.rb b/spec/barbeque/executor/hako_spec.rb index 358f037..2226885 100644 --- a/spec/barbeque/executor/hako_spec.rb +++ b/spec/barbeque/executor/hako_spec.rb @@ -9,7 +9,7 @@ described_class.new( hako_dir: hako_directory, hako_env: hako_env, - yaml_dir: '/yamls', + definition_dir: '/yamls', oneshot_notification_prefix: 's3://barbeque/task_statuses?region=ap-northeast-1', ) end @@ -52,6 +52,8 @@ let(:stderr) { '' } before do + allow(File).to receive(:readable?).with("/yamls/#{app_name}.jsonnet").and_return(false) + allow(File).to receive(:readable?).with("/yamls/#{app_name}.yml").and_return(true) expect(Open3).to receive(:capture3).with( hako_env, 'bundle', 'exec', 'hako', 'oneshot', '--no-wait', '--tag', 'latest', @@ -207,6 +209,8 @@ describe '#start_retry' do before do + allow(File).to receive(:readable?).with("/yamls/#{app_name}.jsonnet").and_return(false) + allow(File).to receive(:readable?).with("/yamls/#{app_name}.yml").and_return(true) expect(Open3).to receive(:capture3).with( hako_env, 'bundle', 'exec', 'hako', 'oneshot', '--no-wait', '--tag', 'latest',