Class: E2B::Template
- Inherits:
-
Object
- Object
- E2B::Template
- Defined in:
- lib/e2b/template.rb
Constant Summary collapse
- DEFAULT_BASE_IMAGE =
"e2bdev/base"- DEFAULT_RESOLVE_SYMLINKS =
false- BASE_STEP_NAME =
"base"- FINALIZE_STEP_NAME =
"finalize"
Class Method Summary collapse
- .alias_exists(alias_name, api_key: nil, access_token: nil, domain: nil) ⇒ Object
- .assign_tags(target_name, tags, api_key: nil, access_token: nil, domain: nil) ⇒ Object
- .build(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, skip_cache: false, on_build_logs: nil, api_key: nil, access_token: nil, domain: nil, **opts) ⇒ Object
- .build_in_background(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, skip_cache: false, on_build_logs: nil, api_key: nil, access_token: nil, domain: nil, **opts) ⇒ Object
- .exists(name, api_key: nil, access_token: nil, domain: nil) ⇒ Object
- .get_build_status(build_info = nil, logs_offset: nil, api_key: nil, access_token: nil, domain: nil, template_id: nil, build_id: nil) ⇒ Object
- .get_tags(template_id, api_key: nil, access_token: nil, domain: nil) ⇒ Object
- .remove_tags(name, tags, api_key: nil, access_token: nil, domain: nil) ⇒ Object
- .to_dockerfile(template) ⇒ Object
- .to_json(template, compute_hashes: true) ⇒ Object
- .wait_for_build_finish(build_info = nil, logs_offset: 0, on_build_logs: nil, logs_refresh_frequency: 0.2, api_key: nil, access_token: nil, domain: nil, template_id: nil, build_id: nil, build_step_origins: nil) ⇒ Object
Instance Method Summary collapse
- #add_mcp_server(servers) ⇒ Object
- #apt_install(packages, no_install_recommends: false) ⇒ Object
- #beta_dev_container_prebuild(devcontainer_directory) ⇒ Object
- #beta_set_dev_container_start(devcontainer_directory) ⇒ Object (also: #beta_set_devcontainer_start)
- #bun_install(packages = nil, g: false, dev: false) ⇒ Object
- #copy(src, dest, force_upload: nil, user: nil, mode: nil, resolve_symlinks: nil) ⇒ Object
- #copy_items(items) ⇒ Object
- #from_aws_registry(image, access_key_id:, secret_access_key:, region:) ⇒ Object
- #from_base_image ⇒ Object
- #from_bun_image(variant = "latest") ⇒ Object
- #from_debian_image(variant = "stable") ⇒ Object
- #from_dockerfile(dockerfile_content_or_path) ⇒ Object
- #from_gcp_registry(image, service_account_json:) ⇒ Object
- #from_image(image, username: nil, password: nil) ⇒ Object
- #from_node_image(variant = "lts") ⇒ Object
- #from_python_image(version = "3") ⇒ Object
- #from_template(template) ⇒ Object
- #from_ubuntu_image(variant = "latest") ⇒ Object
- #git_clone(url, path = nil, branch: nil, depth: nil, user: nil) ⇒ Object
-
#initialize(file_context_path: nil, file_ignore_patterns: []) ⇒ Template
constructor
A new instance of Template.
- #make_dir(path, mode: nil, user: nil) ⇒ Object
- #make_symlink(src, dest, user: nil, force: false) ⇒ Object
- #npm_install(packages = nil, g: false, dev: false) ⇒ Object
- #pip_install(packages = nil, g: true) ⇒ Object
- #remove(path, force: false, recursive: false, user: nil) ⇒ Object
- #rename(src, dest, force: false, user: nil) ⇒ Object
- #run_cmd(cmd, user: nil) ⇒ Object
- #set_envs(envs) ⇒ Object
- #set_ready_cmd(ready_cmd) ⇒ Object
- #set_start_cmd(start_cmd, ready_cmd = nil) ⇒ Object
- #set_user(user) ⇒ Object
- #set_workdir(workdir) ⇒ Object
- #skip_cache ⇒ Object
- #to_dockerfile ⇒ Object
- #to_h(compute_hashes: false) ⇒ Object
- #to_json(compute_hashes: true) ⇒ Object
Constructor Details
#initialize(file_context_path: nil, file_ignore_patterns: []) ⇒ Template
Returns a new instance of Template.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/e2b/template.rb', line 433 def initialize(file_context_path: nil, file_ignore_patterns: []) @file_context_path = (file_context_path || default_file_context_path).to_s @file_ignore_patterns = Array(file_ignore_patterns) @base_image = DEFAULT_BASE_IMAGE @base_template = nil @registry_config = nil @start_cmd = nil @ready_cmd = nil @force = false @force_next_layer = false @instructions = [] @base_source_location = capture_source_location @finalization_source_location = nil end |
Class Method Details
.alias_exists(alias_name, api_key: nil, access_token: nil, domain: nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/e2b/template.rb', line 31 def alias_exists(alias_name, api_key: nil, access_token: nil, domain: nil) credentials = resolve_credentials(api_key: api_key, access_token: access_token) http_client = build_http_client(**credentials, domain: resolve_domain(domain)) http_client.get("/templates/aliases/#{escape_path_segment(alias_name)}") true rescue E2B::NotFoundError false rescue E2B::AuthenticationError => e return true if e.status_code == 403 raise end |
.assign_tags(target_name, tags, api_key: nil, access_token: nil, domain: nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/e2b/template.rb', line 44 def (target_name, , api_key: nil, access_token: nil, domain: nil) credentials = resolve_credentials(api_key: api_key, access_token: access_token) http_client = build_http_client(**credentials, domain: resolve_domain(domain)) response = http_client.post("/templates/tags", body: { target: target_name, tags: () }) Models::TemplateTagInfo.from_hash(response) end |
.build(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, skip_cache: false, on_build_logs: nil, api_key: nil, access_token: nil, domain: nil, **opts) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/e2b/template.rb', line 137 def build(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, skip_cache: false, on_build_logs: nil, api_key: nil, access_token: nil, domain: nil, **opts) on_build_logs&.call(Models::TemplateLogEntryStart.new(timestamp: Time.now, message: "Build started")) build_info = build_in_background( template, name: name, alias_name: alias_name || opts[:alias] || opts["alias"], tags: , cpu_count: cpu_count, memory_mb: memory_mb, skip_cache: skip_cache, on_build_logs: on_build_logs, api_key: api_key, access_token: access_token, domain: domain ) on_build_logs&.call(log_entry("Waiting for logs...")) wait_for_build_finish( build_info, on_build_logs: on_build_logs, api_key: api_key, access_token: access_token, domain: domain ) build_info ensure on_build_logs&.call(Models::TemplateLogEntryEnd.new(timestamp: Time.now, message: "Build finished")) end |
.build_in_background(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, skip_cache: false, on_build_logs: nil, api_key: nil, access_token: nil, domain: nil, **opts) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/e2b/template.rb', line 170 def build_in_background(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, skip_cache: false, on_build_logs: nil, api_key: nil, access_token: nil, domain: nil, **opts) alias_name ||= opts[:alias] || opts["alias"] resolved_name = normalize_build_name(name: name, alias_name: alias_name) template.send(:force_build!) if skip_cache credentials = resolve_credentials(api_key: api_key, access_token: access_token) http_client = build_http_client(**credentials, domain: resolve_domain(domain)) = Array().any? ? " with tags #{Array().join(', ')}" : "" on_build_logs&.call(log_entry("Requesting build for template: #{resolved_name}#{}")) create_response = http_client.post("/v3/templates", body: { name: resolved_name, tags: , cpuCount: cpu_count, memoryMB: memory_mb }) build_info = Models::BuildInfo.new( alias_name: resolved_name, name: resolved_name, tags: create_response["tags"] || create_response[:tags] || [], template_id: create_response["templateID"] || create_response[:templateID], build_id: create_response["buildID"] || create_response[:buildID], build_step_origins: template.send(:build_step_origins) ) on_build_logs&.call( log_entry("Template created with ID: #{build_info.template_id}, Build ID: #{build_info.build_id}") ) instructions = template.send(:instructions_with_hash_metadata) upload_copy_instructions( http_client, template, build_info, instructions, on_build_logs: on_build_logs ) on_build_logs&.call(log_entry("All file uploads completed")) on_build_logs&.call(log_entry("Starting building...")) http_client.post( "/v2/templates/#{escape_path_segment(build_info.template_id)}/builds/#{escape_path_segment(build_info.build_id)}", body: template.send(:build_payload, instructions) ) build_info end |
.exists(name, api_key: nil, access_token: nil, domain: nil) ⇒ Object
27 28 29 |
# File 'lib/e2b/template.rb', line 27 def exists(name, api_key: nil, access_token: nil, domain: nil) alias_exists(name, api_key: api_key, access_token: access_token, domain: domain) end |
.get_build_status(build_info = nil, logs_offset: nil, api_key: nil, access_token: nil, domain: nil, template_id: nil, build_id: nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/e2b/template.rb', line 73 def get_build_status(build_info = nil, logs_offset: nil, api_key: nil, access_token: nil, domain: nil, template_id: nil, build_id: nil) resolved_template_id, resolved_build_id = extract_build_identifiers( build_info, template_id: template_id, build_id: build_id, build_step_origins: nil ) credentials = resolve_credentials(api_key: api_key, access_token: access_token) http_client = build_http_client(**credentials, domain: resolve_domain(domain)) params = {} params[:logsOffset] = logs_offset unless logs_offset.nil? response = http_client.get( "/templates/#{escape_path_segment(resolved_template_id)}/builds/#{escape_path_segment(resolved_build_id)}/status", params: params ) Models::TemplateBuildStatusResponse.from_hash(response) end |
.get_tags(template_id, api_key: nil, access_token: nil, domain: nil) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/e2b/template.rb', line 65 def (template_id, api_key: nil, access_token: nil, domain: nil) credentials = resolve_credentials(api_key: api_key, access_token: access_token) http_client = build_http_client(**credentials, domain: resolve_domain(domain)) response = http_client.get("/templates/#{escape_path_segment(template_id)}/tags") Array(response).map { |item| Models::TemplateTag.from_hash(item) } end |
.remove_tags(name, tags, api_key: nil, access_token: nil, domain: nil) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/e2b/template.rb', line 55 def (name, , api_key: nil, access_token: nil, domain: nil) credentials = resolve_credentials(api_key: api_key, access_token: access_token) http_client = build_http_client(**credentials, domain: resolve_domain(domain)) http_client.delete("/templates/tags", body: { name: name, tags: () }) nil end |
.to_dockerfile(template) ⇒ Object
23 24 25 |
# File 'lib/e2b/template.rb', line 23 def to_dockerfile(template) template.to_dockerfile end |
.to_json(template, compute_hashes: true) ⇒ Object
19 20 21 |
# File 'lib/e2b/template.rb', line 19 def to_json(template, compute_hashes: true) template.to_json(compute_hashes: compute_hashes) end |
.wait_for_build_finish(build_info = nil, logs_offset: 0, on_build_logs: nil, logs_refresh_frequency: 0.2, api_key: nil, access_token: nil, domain: nil, template_id: nil, build_id: nil, build_step_origins: nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/e2b/template.rb', line 95 def wait_for_build_finish(build_info = nil, logs_offset: 0, on_build_logs: nil, logs_refresh_frequency: 0.2, api_key: nil, access_token: nil, domain: nil, template_id: nil, build_id: nil, build_step_origins: nil) resolved_template_id, resolved_build_id, resolved_build_step_origins = extract_build_identifiers( build_info, template_id: template_id, build_id: build_id, build_step_origins: build_step_origins ) current_logs_offset = logs_offset loop do status = get_build_status( nil, logs_offset: current_logs_offset, api_key: api_key, access_token: access_token, domain: domain, template_id: resolved_template_id, build_id: resolved_build_id ) current_logs_offset += status.log_entries.length status.log_entries.each { |entry| on_build_logs.call(entry) } if on_build_logs case status.status when "building", "waiting" sleep_for_build_poll(logs_refresh_frequency) when "ready" return status when "error" raise build_error( status.reason&. || "Unknown build error occurred.", step: status.reason&.step, source_location: build_step_source_location(status.reason&.step, resolved_build_step_origins) ) else raise build_error("Unknown build status: #{status.status}") end end end |
Instance Method Details
#add_mcp_server(servers) ⇒ Object
672 673 674 675 676 677 678 679 680 681 682 |
# File 'lib/e2b/template.rb', line 672 def add_mcp_server(servers) unless @base_template == "mcp-gateway" raise build_error( "MCP servers can only be added to mcp-gateway template", source_location: capture_source_location ) end server_list = Array(servers).map(&:to_s) run_cmd("mcp-gateway pull #{server_list.join(' ')}", user: "root") end |
#apt_install(packages, no_install_recommends: false) ⇒ Object
660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/e2b/template.rb', line 660 def apt_install(packages, no_install_recommends: false) package_list = Array(packages).map(&:to_s) install_flags = no_install_recommends ? "--no-install-recommends " : "" run_cmd( [ "apt-get update", "DEBIAN_FRONTEND=noninteractive DEBCONF_NOWARNINGS=yes apt-get install -y #{install_flags}#{package_list.join(' ')}" ], user: "root" ) end |
#beta_dev_container_prebuild(devcontainer_directory) ⇒ Object
695 696 697 698 |
# File 'lib/e2b/template.rb', line 695 def beta_dev_container_prebuild(devcontainer_directory) ensure_devcontainer_template! run_cmd("devcontainer build --workspace-folder #{devcontainer_directory}", user: "root") end |
#beta_set_dev_container_start(devcontainer_directory) ⇒ Object Also known as: beta_set_devcontainer_start
700 701 702 703 704 705 706 |
# File 'lib/e2b/template.rb', line 700 def beta_set_dev_container_start(devcontainer_directory) ensure_devcontainer_template! set_start_cmd( "sudo devcontainer up --workspace-folder #{devcontainer_directory} && sudo /prepare-exec.sh #{devcontainer_directory} | sudo tee /devcontainer.sh > /dev/null && sudo chmod +x /devcontainer.sh && sudo touch /devcontainer.up", E2B.wait_for_file("/devcontainer.up") ) end |
#bun_install(packages = nil, g: false, dev: false) ⇒ Object
651 652 653 654 655 656 657 658 |
# File 'lib/e2b/template.rb', line 651 def bun_install(packages = nil, g: false, dev: false) package_list = packages.nil? ? nil : Array(packages).map(&:to_s) args = ["bun", "install"] args << "-g" if g args << "--dev" if dev args.concat(package_list) if package_list run_cmd(args.join(" "), user: g ? "root" : nil) end |
#copy(src, dest, force_upload: nil, user: nil, mode: nil, resolve_symlinks: nil) ⇒ Object
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/e2b/template.rb', line 538 def copy(src, dest, force_upload: nil, user: nil, mode: nil, resolve_symlinks: nil) source_location = capture_source_location Array(src).each do |source| source_path = source.to_s validate_relative_path!(source_path, source_location: source_location) @instructions << { type: "COPY", args: [ source_path, dest.to_s, user || "", mode ? format("%04o", mode) : "" ], force: !!force_upload || @force_next_layer, forceUpload: force_upload, resolveSymlinks: resolve_symlinks.nil? ? DEFAULT_RESOLVE_SYMLINKS : resolve_symlinks, sourceLocation: source_location } end self end |
#copy_items(items) ⇒ Object
563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/e2b/template.rb', line 563 def copy_items(items) items.each do |item| copy( copy_item_value(item, :src), copy_item_value(item, :dest), force_upload: copy_item_value(item, :forceUpload, required: false), user: copy_item_value(item, :user, required: false), mode: copy_item_value(item, :mode, required: false), resolve_symlinks: copy_item_value(item, :resolveSymlinks, required: false) ) end self end |
#from_aws_registry(image, access_key_id:, secret_access_key:, region:) ⇒ Object
487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/e2b/template.rb', line 487 def from_aws_registry(image, access_key_id:, secret_access_key:, region:) @base_image = image @base_template = nil @registry_config = { type: "aws", awsAccessKeyId: access_key_id, awsSecretAccessKey: secret_access_key, awsRegion: region } @force = true if @force_next_layer @base_source_location = capture_source_location self end |
#from_base_image ⇒ Object
468 469 470 |
# File 'lib/e2b/template.rb', line 468 def from_base_image from_image(DEFAULT_BASE_IMAGE) end |
#from_bun_image(variant = "latest") ⇒ Object
464 465 466 |
# File 'lib/e2b/template.rb', line 464 def from_bun_image(variant = "latest") from_image("oven/bun:#{variant}") end |
#from_debian_image(variant = "stable") ⇒ Object
448 449 450 |
# File 'lib/e2b/template.rb', line 448 def from_debian_image(variant = "stable") from_image("debian:#{variant}") end |
#from_dockerfile(dockerfile_content_or_path) ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/e2b/template.rb', line 513 def from_dockerfile(dockerfile_content_or_path) @base_template = nil @registry_config = nil @base_image = E2B::DockerfileParser.parse(dockerfile_content_or_path, self) @force = true if @force_next_layer @base_source_location = capture_source_location self rescue TemplateError => e raise template_error( e., source_location: capture_source_location, status_code: e.status_code, headers: e.headers ) end |
#from_gcp_registry(image, service_account_json:) ⇒ Object
501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/e2b/template.rb', line 501 def from_gcp_registry(image, service_account_json:) @base_image = image @base_template = nil @registry_config = { type: "gcp", serviceAccountJson: read_gcp_service_account_json(service_account_json) } @force = true if @force_next_layer @base_source_location = capture_source_location self end |
#from_image(image, username: nil, password: nil) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/e2b/template.rb', line 472 def from_image(image, username: nil, password: nil) @base_image = image @base_template = nil @registry_config = if username && password { type: "registry", username: username, password: password } end @force = true if @force_next_layer @base_source_location = capture_source_location self end |
#from_node_image(variant = "lts") ⇒ Object
460 461 462 |
# File 'lib/e2b/template.rb', line 460 def from_node_image(variant = "lts") from_image("node:#{variant}") end |
#from_python_image(version = "3") ⇒ Object
456 457 458 |
# File 'lib/e2b/template.rb', line 456 def from_python_image(version = "3") from_image("python:#{version}") end |
#from_template(template) ⇒ Object
529 530 531 532 533 534 535 536 |
# File 'lib/e2b/template.rb', line 529 def from_template(template) @base_template = template @base_image = nil @registry_config = nil @force = true if @force_next_layer @base_source_location = capture_source_location self end |
#from_ubuntu_image(variant = "latest") ⇒ Object
452 453 454 |
# File 'lib/e2b/template.rb', line 452 def from_ubuntu_image(variant = "latest") from_image("ubuntu:#{variant}") end |
#git_clone(url, path = nil, branch: nil, depth: nil, user: nil) ⇒ Object
684 685 686 687 688 689 690 691 692 693 |
# File 'lib/e2b/template.rb', line 684 def git_clone(url, path = nil, branch: nil, depth: nil, user: nil) args = ["git", "clone", url.to_s] if branch args << "--branch #{branch}" args << "--single-branch" end args << "--depth #{depth}" if depth args << path.to_s if path run_cmd(args.join(" "), user: user) end |
#make_dir(path, mode: nil, user: nil) ⇒ Object
726 727 728 729 730 731 |
# File 'lib/e2b/template.rb', line 726 def make_dir(path, mode: nil, user: nil) args = ["mkdir", "-p"] args << "-m #{format('%04o', mode)}" if mode args.concat(Array(path).map(&:to_s)) run_cmd(args.join(" "), user: user) end |
#make_symlink(src, dest, user: nil, force: false) ⇒ Object
733 734 735 736 737 738 739 |
# File 'lib/e2b/template.rb', line 733 def make_symlink(src, dest, user: nil, force: false) args = ["ln", "-s"] args << "-f" if force args << src.to_s args << dest.to_s run_cmd(args.join(" "), user: user) end |
#npm_install(packages = nil, g: false, dev: false) ⇒ Object
642 643 644 645 646 647 648 649 |
# File 'lib/e2b/template.rb', line 642 def npm_install(packages = nil, g: false, dev: false) package_list = packages.nil? ? nil : Array(packages).map(&:to_s) args = ["npm", "install"] args << "-g" if g args << "--save-dev" if dev args.concat(package_list) if package_list run_cmd(args.join(" "), user: g ? "root" : nil) end |
#pip_install(packages = nil, g: true) ⇒ Object
634 635 636 637 638 639 640 |
# File 'lib/e2b/template.rb', line 634 def pip_install(packages = nil, g: true) package_list = packages.nil? ? nil : Array(packages).map(&:to_s) args = ["pip", "install"] args << "--user" unless g args.concat(package_list || ["."]) run_cmd(args.join(" "), user: g ? "root" : nil) end |
#remove(path, force: false, recursive: false, user: nil) ⇒ Object
710 711 712 713 714 715 716 |
# File 'lib/e2b/template.rb', line 710 def remove(path, force: false, recursive: false, user: nil) args = ["rm"] args << "-r" if recursive args << "-f" if force args.concat(Array(path).map(&:to_s)) run_cmd(args.join(" "), user: user) end |
#rename(src, dest, force: false, user: nil) ⇒ Object
718 719 720 721 722 723 724 |
# File 'lib/e2b/template.rb', line 718 def rename(src, dest, force: false, user: nil) args = ["mv"] args << "-f" if force args << src.to_s args << dest.to_s run_cmd(args.join(" "), user: user) end |
#run_cmd(cmd, user: nil) ⇒ Object
578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/e2b/template.rb', line 578 def run_cmd(cmd, user: nil) commands = Array(cmd).map(&:to_s) source_location = capture_source_location @instructions << { type: "RUN", args: [commands.join(" && "), user || ""], force: @force_next_layer, sourceLocation: source_location } self end |
#set_envs(envs) ⇒ Object
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/e2b/template.rb', line 616 def set_envs(envs) return self if envs.empty? args = envs.each_with_object([]) do |(key, value), values| values << key.to_s values << value.to_s end source_location = capture_source_location @instructions << { type: "ENV", args: args, force: @force_next_layer, sourceLocation: source_location } self end |
#set_ready_cmd(ready_cmd) ⇒ Object
753 754 755 756 757 |
# File 'lib/e2b/template.rb', line 753 def set_ready_cmd(ready_cmd) @ready_cmd = normalize_ready_cmd(ready_cmd) @finalization_source_location = capture_source_location self end |
#set_start_cmd(start_cmd, ready_cmd = nil) ⇒ Object
746 747 748 749 750 751 |
# File 'lib/e2b/template.rb', line 746 def set_start_cmd(start_cmd, ready_cmd = nil) @start_cmd = start_cmd.to_s @ready_cmd = normalize_ready_cmd(ready_cmd) unless ready_cmd.nil? @finalization_source_location = capture_source_location self end |
#set_user(user) ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/e2b/template.rb', line 604 def set_user(user) source_location = capture_source_location @instructions << { type: "USER", args: [user.to_s], force: @force_next_layer, sourceLocation: source_location } self end |
#set_workdir(workdir) ⇒ Object
592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/e2b/template.rb', line 592 def set_workdir(workdir) source_location = capture_source_location @instructions << { type: "WORKDIR", args: [workdir.to_s], force: @force_next_layer, sourceLocation: source_location } self end |
#skip_cache ⇒ Object
741 742 743 744 |
# File 'lib/e2b/template.rb', line 741 def skip_cache @force_next_layer = true self end |
#to_dockerfile ⇒ Object
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/e2b/template.rb', line 767 def to_dockerfile if @base_template raise template_error( "Cannot convert template built from another template to Dockerfile. Templates based on other templates can only be built using the E2B API.", source_location: capture_source_location ) end raise template_error("No base image specified for template", source_location: capture_source_location) unless @base_image dockerfile = +"FROM #{@base_image}\n" @instructions.each do |instruction| case instruction[:type] when "RUN" dockerfile << "RUN #{instruction[:args][0]}\n" when "COPY" dockerfile << "COPY #{instruction[:args][0]} #{instruction[:args][1]}\n" when "ENV" values = instruction[:args].each_slice(2).map { |key, value| "#{key}=#{value}" } dockerfile << "ENV #{values.join(' ')}\n" else dockerfile << "#{instruction[:type]} #{instruction[:args].join(' ')}\n" end end dockerfile << "ENTRYPOINT #{@start_cmd}\n" if @start_cmd dockerfile end |
#to_h(compute_hashes: false) ⇒ Object
759 760 761 |
# File 'lib/e2b/template.rb', line 759 def to_h(compute_hashes: false) build_payload(compute_hashes ? : @instructions) end |
#to_json(compute_hashes: true) ⇒ Object
763 764 765 |
# File 'lib/e2b/template.rb', line 763 def to_json(compute_hashes: true) JSON.pretty_generate(to_h(compute_hashes: compute_hashes)) end |