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, disk_size_mb: nil, 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, disk_size_mb: nil, 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.
446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/e2b/template.rb', line 446 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, disk_size_mb: nil, 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 169 170 |
# File 'lib/e2b/template.rb', line 137 def build(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, disk_size_mb: nil, 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, disk_size_mb: disk_size_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, disk_size_mb: nil, skip_cache: false, on_build_logs: nil, api_key: nil, access_token: nil, domain: nil, **opts) ⇒ Object
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 221 222 223 224 225 |
# File 'lib/e2b/template.rb', line 172 def build_in_background(template, name: nil, alias_name: nil, tags: nil, cpu_count: 2, memory_mb: 1024, disk_size_mb: nil, 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}#{}")) body = { name: resolved_name, tags: , cpuCount: cpu_count, memoryMB: memory_mb } body[:diskSizeMB] = disk_size_mb if disk_size_mb create_response = http_client.post("/v3/templates", body: body) 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
685 686 687 688 689 690 691 692 693 694 695 |
# File 'lib/e2b/template.rb', line 685 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
673 674 675 676 677 678 679 680 681 682 683 |
# File 'lib/e2b/template.rb', line 673 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
708 709 710 711 |
# File 'lib/e2b/template.rb', line 708 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
713 714 715 716 717 718 719 |
# File 'lib/e2b/template.rb', line 713 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
664 665 666 667 668 669 670 671 |
# File 'lib/e2b/template.rb', line 664 def bun_install(packages = nil, g: false, dev: false) package_list = packages.nil? ? nil : Array(packages).map(&:to_s) args = %w[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
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
# File 'lib/e2b/template.rb', line 551 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
576 577 578 579 580 581 582 583 584 585 586 587 588 589 |
# File 'lib/e2b/template.rb', line 576 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
500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/e2b/template.rb', line 500 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
481 482 483 |
# File 'lib/e2b/template.rb', line 481 def from_base_image from_image(DEFAULT_BASE_IMAGE) end |
#from_bun_image(variant = "latest") ⇒ Object
477 478 479 |
# File 'lib/e2b/template.rb', line 477 def from_bun_image(variant = "latest") from_image("oven/bun:#{variant}") end |
#from_debian_image(variant = "stable") ⇒ Object
461 462 463 |
# File 'lib/e2b/template.rb', line 461 def from_debian_image(variant = "stable") from_image("debian:#{variant}") end |
#from_dockerfile(dockerfile_content_or_path) ⇒ Object
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/e2b/template.rb', line 526 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
514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/e2b/template.rb', line 514 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
485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/e2b/template.rb', line 485 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
473 474 475 |
# File 'lib/e2b/template.rb', line 473 def from_node_image(variant = "lts") from_image("node:#{variant}") end |
#from_python_image(version = "3") ⇒ Object
469 470 471 |
# File 'lib/e2b/template.rb', line 469 def from_python_image(version = "3") from_image("python:#{version}") end |
#from_template(template) ⇒ Object
542 543 544 545 546 547 548 549 |
# File 'lib/e2b/template.rb', line 542 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
465 466 467 |
# File 'lib/e2b/template.rb', line 465 def from_ubuntu_image(variant = "latest") from_image("ubuntu:#{variant}") end |
#git_clone(url, path = nil, branch: nil, depth: nil, user: nil) ⇒ Object
697 698 699 700 701 702 703 704 705 706 |
# File 'lib/e2b/template.rb', line 697 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
739 740 741 742 743 744 |
# File 'lib/e2b/template.rb', line 739 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
746 747 748 749 750 751 752 |
# File 'lib/e2b/template.rb', line 746 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
655 656 657 658 659 660 661 662 |
# File 'lib/e2b/template.rb', line 655 def npm_install(packages = nil, g: false, dev: false) package_list = packages.nil? ? nil : Array(packages).map(&:to_s) args = %w[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
647 648 649 650 651 652 653 |
# File 'lib/e2b/template.rb', line 647 def pip_install(packages = nil, g: true) package_list = packages.nil? ? nil : Array(packages).map(&:to_s) args = %w[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
723 724 725 726 727 728 729 |
# File 'lib/e2b/template.rb', line 723 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
731 732 733 734 735 736 737 |
# File 'lib/e2b/template.rb', line 731 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
591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/e2b/template.rb', line 591 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
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/e2b/template.rb', line 629 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
766 767 768 769 770 |
# File 'lib/e2b/template.rb', line 766 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
759 760 761 762 763 764 |
# File 'lib/e2b/template.rb', line 759 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
617 618 619 620 621 622 623 624 625 626 627 |
# File 'lib/e2b/template.rb', line 617 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
605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/e2b/template.rb', line 605 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
754 755 756 757 |
# File 'lib/e2b/template.rb', line 754 def skip_cache @force_next_layer = true self end |
#to_dockerfile ⇒ Object
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/e2b/template.rb', line 780 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 unless @base_image raise template_error("No base image specified for template", source_location: capture_source_location) end 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
772 773 774 |
# File 'lib/e2b/template.rb', line 772 def to_h(compute_hashes: false) build_payload(compute_hashes ? : @instructions) end |
#to_json(compute_hashes: true) ⇒ Object
776 777 778 |
# File 'lib/e2b/template.rb', line 776 def to_json(compute_hashes: true) JSON.pretty_generate(to_h(compute_hashes: compute_hashes)) end |