Class: DaggerRuby::Client
- Inherits:
-
Object
- Object
- DaggerRuby::Client
- Defined in:
- lib/dagger_ruby/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #cache_volume(name, opts = {}) ⇒ Object
- #close ⇒ Object
- #container(opts = {}) ⇒ Object
- #directory ⇒ Object
- #execute_query(query) ⇒ Object (also: #execute)
- #file(name, contents, permissions: nil) ⇒ Object
- #git(url, opts = {}) ⇒ Object
- #host ⇒ Object
- #http(url, opts = {}) ⇒ Object
-
#initialize(config: nil) ⇒ Client
constructor
A new instance of Client.
- #secret(uri, cache_key: nil) ⇒ Object
- #set_secret(name, value) ⇒ Object
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.}" end end |
Instance Attribute Details
#config ⇒ Object (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 |
#close ⇒ Object
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 |
#directory ⇒ Object
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"] = if 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 |
#host ⇒ Object
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 |