Sha256: 0f15498c7aa0db7d3433a6b8429b753825903595013d6f3b158590755b1b8ef0

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Floe
  class Workflow
    class Runner
      class Docker < Floe::Workflow::Runner
        def initialize(options = {})
          require "awesome_spawn"
          require "tempfile"

          super

          @network = options.fetch("network", "bridge")
        end

        def run!(resource, env = {}, secrets = {})
          raise ArgumentError, "Invalid resource" unless resource&.start_with?("docker://")

          image = resource.sub("docker://", "")

          params  = ["run", :rm]
          params += [[:net, "host"]] if network == "host"
          params += env.map { |k, v| [:e, "#{k}=#{v}"] } if env

          secrets_file = nil

          if secrets && !secrets.empty?
            secrets_file = Tempfile.new
            secrets_file.write(secrets.to_json)
            secrets_file.flush

            params << [:e, "SECRETS=/run/secrets"]
            params << [:v, "#{secrets_file.path}:/run/secrets:z"]
          end

          params << image

          logger.debug("Running docker: #{AwesomeSpawn.build_command_line("docker", params)}")
          result = AwesomeSpawn.run!("docker", :params => params)

          [result.exit_status, result.output]
        ensure
          secrets_file&.close!
        end

        private

        attr_reader :network
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
floe-0.3.0 lib/floe/workflow/runner/docker.rb