Class: DaggerRuby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dagger_ruby/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dagger_ruby/client.rb', line 22

def initialize(config: nil)
  @config = config || Config.new

  port = ENV.fetch("DAGGER_SESSION_PORT", nil)
  @session_token = ENV.fetch("DAGGER_SESSION_TOKEN", nil)

  unless port && @session_token
    raise ConnectionError,
          "This script must be run within a Dagger session.\nRun with: dagger run ruby script.rb [args...]"
  end

  @endpoint = "http://127.0.0.1:#{port}/query"
  @uri = URI(@endpoint)
  @auth_header = "Basic #{Base64.strict_encode64("#{@session_token}:")}"

  if @config.log_output
    logger = Logger.new(@config.log_output)
    logger.level = Logger::INFO
    @logger = logger
  end

  begin
    execute_query("query { container { id } }")
  rescue StandardError => e
    raise ConnectionError, "Failed to connect to Dagger engine: #{e.message}"
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/dagger_ruby/client.rb', line 20

def config
  @config
end

Instance Method Details

#cache_volume(name, opts = {}) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/dagger_ruby/client.rb', line 76

def cache_volume(name, opts = {})
  args = { "key" => name }
  args["source"] = graphql_id(opts[:source]) if opts[:source]
  args["sharing"] = QueryBuilder.enum_value(opts[:sharing]) if opts[:sharing]
  args["owner"] = opts[:owner] if opts[:owner]
  query = QueryBuilder.new.chain_operation("cacheVolume", args)
  CacheVolume.new(query, self)
end

#closeObject



122
# File 'lib/dagger_ruby/client.rb', line 122

def close; end

#container(opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/dagger_ruby/client.rb', line 50

def container(opts = {})
  query = QueryBuilder.new
  query = if opts[:platform]
            query.chain_operation("container", { "platform" => opts[:platform] })
          else
            query.chain_operation("container")
          end
  Container.new(query, self)
end

#directoryObject



60
61
62
# File 'lib/dagger_ruby/client.rb', line 60

def directory
  Directory.new(QueryBuilder.new("directory"), self)
end

#execute_query(query) ⇒ Object Also known as: execute



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dagger_ruby/client.rb', line 124

def execute_query(query)
  http = Net::HTTP.new(@uri.host, @uri.port)
  http.read_timeout = @config.timeout
  http.open_timeout = 10

  request = Net::HTTP::Post.new(@uri.path)
  request["Content-Type"] = "application/json"
  request["Authorization"] = @auth_header
  request["User-Agent"] = "Dagger Ruby"
  request.body = { query: query }.to_json

  response = http.request(request)
  handle_response(response)
end

#file(name, contents, permissions: nil) ⇒ Object



64
65
66
67
68
# File 'lib/dagger_ruby/client.rb', line 64

def file(name, contents, permissions: nil)
  args = { "name" => name, "contents" => contents }
  args["permissions"] = permissions if permissions
  File.new(QueryBuilder.new.chain_operation("file", args), self)
end

#git(url, opts = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dagger_ruby/client.rb', line 89

def git(url, opts = {})
  args = { "url" => url }
  args["sshKnownHosts"] = opts[:ssh_known_hosts] if opts[:ssh_known_hosts]
  args["sshAuthSocket"] = graphql_id(opts[:ssh_auth_socket]) if opts[:ssh_auth_socket]
  args["httpAuthUsername"] = opts[:http_auth_username] if opts[:http_auth_username]
  args["httpAuthToken"] = graphql_id(opts[:http_auth_token]) if opts[:http_auth_token]
  args["httpAuthHeader"] = graphql_id(opts[:http_auth_header]) if opts[:http_auth_header]
  args["experimentalServiceHost"] = graphql_id(opts[:service_host]) if opts[:service_host]

  query = QueryBuilder.new
  query = query.chain_operation("git", args)
  GitRepository.new(query, self)
end

#hostObject



85
86
87
# File 'lib/dagger_ruby/client.rb', line 85

def host
  Host.new(QueryBuilder.new("host"), self)
end

#http(url, opts = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dagger_ruby/client.rb', line 103

def http(url, opts = {})
  args = { "url" => url }
  args["name"] = opts[:name] if opts[:name]
  args["permissions"] = opts[:permissions] if opts[:permissions]
  args["checksum"] = opts[:checksum] if opts[:checksum]
  args["authHeader"] = graphql_id(opts[:auth_header]) if opts[:auth_header]
  args["experimentalServiceHost"] = graphql_id(opts[:service_host]) if opts[:service_host]

  query = QueryBuilder.new
  query = query.chain_operation("http", args)
  File.new(query, self)
end

#secret(uri, cache_key: nil) ⇒ Object



70
71
72
73
74
# File 'lib/dagger_ruby/client.rb', line 70

def secret(uri, cache_key: nil)
  args = { "uri" => uri }
  args["cacheKey"] = cache_key if cache_key
  Secret.new(QueryBuilder.new.chain_operation("secret", args), self)
end

#set_secret(name, value) ⇒ Object



116
117
118
119
120
# File 'lib/dagger_ruby/client.rb', line 116

def set_secret(name, value)
  query = QueryBuilder.new
  query = query.chain_operation("setSecret", { "name" => name, "plaintext" => value })
  Secret.new(query, self)
end