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) ⇒ Object



72
73
74
75
76
# File 'lib/dagger_ruby/client.rb', line 72

def cache_volume(name)
  query = QueryBuilder.new
  query = query.chain_operation("cacheVolume", { "key" => name })
  CacheVolume.new(query, self)
end

#closeObject



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

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dagger_ruby/client.rb', line 127

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

#fileObject



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

def file
  File.new(QueryBuilder.new("file"), self)
end

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dagger_ruby/client.rb', line 82

def git(url, opts = {})
  args = { "url" => url }
  args["keepGitDir"] = opts[:keep_git_dir] if opts.key?(:keep_git_dir)
  args["sshKnownHosts"] = opts[:ssh_known_hosts] if opts[:ssh_known_hosts]
  if opts[:ssh_auth_socket]
    args["sshAuthSocket"] =
      opts[:ssh_auth_socket].is_a?(DaggerObject) ? opts[:ssh_auth_socket].id : opts[:ssh_auth_socket]
  end
  args["httpAuthUsername"] = opts[:http_auth_username] if opts[:http_auth_username]
  if opts[:http_auth_token]
    args["httpAuthToken"] =
      opts[:http_auth_token].is_a?(DaggerObject) ? opts[:http_auth_token].id : opts[:http_auth_token]
  end
  if opts[:http_auth_header]
    args["httpAuthHeader"] =
      opts[:http_auth_header].is_a?(DaggerObject) ? opts[:http_auth_header].id : opts[:http_auth_header]
  end

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

#hostObject



78
79
80
# File 'lib/dagger_ruby/client.rb', line 78

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

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



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

def http(url, opts = {})
  args = { "url" => url }
  args["name"] = opts[:name] if opts[:name]
  args["permissions"] = opts[:permissions] if opts[:permissions]
  if opts[:auth_header]
    args["authHeader"] =
      opts[:auth_header].is_a?(DaggerObject) ? opts[:auth_header].id : opts[:auth_header]
  end

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

#secretObject



68
69
70
# File 'lib/dagger_ruby/client.rb', line 68

def secret
  Secret.new(QueryBuilder.new("secret"), self)
end

#set_secret(name, value) ⇒ Object



119
120
121
122
123
# File 'lib/dagger_ruby/client.rb', line 119

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