Module: Aura::Docker

Defined in:
lib/aura/docker.rb

Overview

Generates production deployment assets next to an .aura file: the transpiled standalone Ruby app, a Dockerfile, and a .dockerignore. Backs the ‘aura deploy` command.

Class Method Summary collapse

Class Method Details

.build(filename) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aura/docker.rb', line 12

def build(filename)
  source = File.read(filename)
  ruby_code = Aura.transpile(source)
  torch_app = ruby_code.include?("Torch::") # ML app needs torch-rb + LibTorch

  dir  = File.dirname(filename)
  base = File.basename(filename, ".aura")
  app_file = File.join(dir, "#{base}.rb")

  File.write(app_file, ruby_code)
  File.write(File.join(dir, "Dockerfile"), dockerfile("#{base}.rb", torch: torch_app))
  File.write(File.join(dir, ".dockerignore"), dockerignore)

  puts "Generated deployment assets:"
  puts "   - #{app_file}"
  puts "   - #{File.join(dir, 'Dockerfile')}"
  puts "   - #{File.join(dir, '.dockerignore')}"
  puts "   Build with: docker build -t #{base} #{dir.empty? ? '.' : dir}"
  puts "   NOTE: Torch apps need LibTorch in the image -- see the Dockerfile comment." if torch_app
end

.dockerfile(app_file, torch: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aura/docker.rb', line 33

def dockerfile(app_file, torch: false)
  gems = "sinatra puma json dotenv"
  gems += " torch-rb torchvision red-datasets" if torch
  libtorch = +""
  if torch
    libtorch = <<~TORCH

      # Torch apps need LibTorch (the native PyTorch C++ library). The simplest
      # path is to base the image on one that already bundles it, or download it:
      #   ENV LIBTORCH /opt/libtorch
      #   RUN curl -L https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-latest.zip -o /tmp/lt.zip \\
      #     && unzip /tmp/lt.zip -d /opt && rm /tmp/lt.zip
      # Then build torch-rb against it: gem install torch-rb -- --with-torch-dir=$LIBTORCH
    TORCH
  end

  <<~DOCKER
    # Generated by Aura v#{Aura::VERSION}
    FROM ruby:3.3-slim

    WORKDIR /app

    RUN apt-get update -qq \\
      && apt-get install -y --no-install-recommends build-essential#{torch ? ' curl unzip' : ''} \\
      && rm -rf /var/lib/apt/lists/*
    #{libtorch}
    RUN gem install #{gems}

    COPY . .

    ENV RACK_ENV=production
    EXPOSE 3000

    CMD ["ruby", "#{app_file}"]
  DOCKER
end

.dockerignoreObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/aura/docker.rb', line 70

def dockerignore
  <<~IGNORE
    .git
    *.aura
    models/*.pth
    data/
    tmp/
    *.log
  IGNORE
end