fly.io-rails 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 441c33814d7b420542d516c828b710b72802b50370a449f8e0a63fb2bbb83460
4
- data.tar.gz: 8f300d43d0b7d0f712b92fba97a38eb71a1e5cf1ecd5caf5e474f0886742a8a1
3
+ metadata.gz: bf047fddfe38280f888b4c2f8e2215172d89bdaaeb3c9e0d082c00014047e110
4
+ data.tar.gz: 5428fb05b28780d0640baf068ba1c3d326fef353771c1cbf8fcd0ccba32b100c
5
5
  SHA512:
6
- metadata.gz: f19969c7b8f4c4af08e5522c429f4237b70bf0af2cdd92908a632204761978a8a17fbf2569d8191cf826167ef1550c793cad18ba0311550c18c1717288757cc0
7
- data.tar.gz: 2e54d69fbb7203c19157ddf6de6851e80ee0fc5c79ea485e728e11d6a645e056bf4fd3802abae612455fabf768782076762788bceb54e7f61437faac7442f14b
6
+ metadata.gz: efbdb4918b945916191ba8bb0452788d73c5066411fef2318eb45b1e6d6d2a6f0218b353bbc835856ab6d405dc3e9b5d8b77fdb3b575d7bd2bc00ebc71bf7c93
7
+ data.tar.gz: 034d4cf87622aa1988c33e33b3b48f5872b5863c7f12f36f3364dcca58ae16f2b347df25cf6f18e1ea19f1e13b1618606e386ec69ed10d4429f5957a147ae1fa
@@ -14,7 +14,7 @@ module Fly
14
14
  include Thor::Base
15
15
  include Thor::Shell
16
16
  include Fly::Scanner
17
- attr_accessor :options
17
+ attr_accessor :options, :dockerfile, :ignorefile
18
18
 
19
19
  def initialize(app=nil, options={})
20
20
  # placate thor
@@ -30,6 +30,7 @@ module Fly
30
30
  @nomad = options[:nomad]
31
31
  @passenger = options[:passenger]
32
32
  @serverless = options[:serverless]
33
+ @eject = options[:eject]
33
34
 
34
35
  # prepare template variables
35
36
  @ruby_version = RUBY_VERSION
@@ -95,7 +96,7 @@ module Fly
95
96
  @procs = {web: 'bin/rails server'}
96
97
  @procs[:web] = "nginx -g 'daemon off;'" if @passenger
97
98
  @procs[:worker] = 'bundle exec sidekiq' if @sidekiq
98
- @procs[:redis] = 'redis-server /etc/redis/redis.conf' if @redis
99
+ @procs[:redis] = 'redis-server /etc/redis/redis.conf' if @redis == :internal
99
100
  @procs.merge! 'anycable-rpc': 'bundle exec anycable --rpc-host=0.0.0.0:50051',
100
101
  'anycable-go': 'env ANYCABLE_RPC_HOST=$ANYCABLE_GO_RPC_HOST /usr/local/bin/anycable-go --port=8082 --host 0.0.0.0' if @anycable
101
102
  end
@@ -126,11 +127,35 @@ module Fly
126
127
  end
127
128
 
128
129
  def generate_dockerfile
129
- app_template 'Dockerfile.erb', 'Dockerfile'
130
+ if @eject or File.exist? 'Dockerfile'
131
+ @dockerfile = 'Dockerfile'
132
+ else
133
+ tmpfile = Tempfile.new('Dockerfile')
134
+ @dockerfile = tmpfile.path
135
+ tmpfile.unlink
136
+ at_exit { File.unlink @dockerfile }
137
+ end
138
+
139
+ if @eject or not File.exist? @dockerfile
140
+ app_template 'Dockerfile.erb', @dockerfile
141
+ end
130
142
  end
131
143
 
132
144
  def generate_dockerignore
133
- app_template 'dockerignore.erb', '.dockerignore'
145
+ if @eject or File.exist? '.dockerignore'
146
+ @ignorefile = '.dockerignore'
147
+ elsif File.exist? '.gitignore'
148
+ @ignorefile = '.gitignore'
149
+ else
150
+ tmpfile = Tempfile.new('Dockerignore')
151
+ @ignoreile = tmpfile.path
152
+ tmpfile.unlink
153
+ at_exit { Filee.unlink @ignorefile }
154
+ end
155
+
156
+ if @eject or not File.exist? @ignorefile
157
+ app_template 'dockerignore.erb', @ignorefile
158
+ end
134
159
  end
135
160
 
136
161
  def generate_nginx_conf
@@ -148,6 +173,7 @@ module Fly
148
173
  end
149
174
 
150
175
  def generate_raketask
176
+ return unless @nomad
151
177
  app_template 'fly.rake.erb', 'lib/tasks/fly.rake'
152
178
  end
153
179
 
@@ -1,6 +1,10 @@
1
+ require 'rake'
2
+
1
3
  module Fly
2
4
  module DSL
3
5
  class Base
6
+ include Rake::DSL
7
+
4
8
  @@blocks = {}
5
9
 
6
10
  def initialize
@@ -3,6 +3,9 @@ module Fly
3
3
  # scan for major features - things that if present will likely affect
4
4
  # more than one artifact that is generated.
5
5
  def scan_rails_app
6
+
7
+ ### database ###
8
+
6
9
  database = YAML.load_file('config/database.yml').
7
10
  dig('production', 'adapter') rescue nil
8
11
 
@@ -14,16 +17,37 @@ module Fly
14
17
  @mysql = true
15
18
  end
16
19
 
17
- gemfile = IO.read('Gemfile') rescue ''
18
- @sidekiq = gemfile.include? 'sidekiq'
19
- @anycable = gemfile.include? 'anycable'
20
- @rmagick = gemfile.include? 'rmagick'
21
- @image_processing = gemfile.include? 'image_processing'
22
- @bootstrap = gemfile.include? 'bootstrap'
23
- @puppeteer = gemfile.include? 'puppeteer'
20
+ ### ruby gems ###
21
+
22
+ @gemfile = []
23
+
24
+ if File.exist? 'Gemfile.lock'
25
+ parser = Bundler::LockfileParser.new(Bundler.read_file('Gemfile.lock'))
26
+ @gemfile += parser.specs.map { |spec, version| spec.name }
27
+ end
28
+
29
+ if File.exist? 'Gemfile'
30
+ @gemfile += Bundler::Definition.build('Gemfile', nil, []).dependencies.map(&:name)
31
+ end
32
+
33
+ @sidekiq = @gemfile.include? 'sidekiq'
34
+ @anycable = @gemfile.include? 'anycable'
35
+ @rmagick = @gemfile.include? 'rmagick'
36
+ @image_processing = @gemfile.include? 'image_processing'
37
+ @bootstrap = @gemfile.include? 'bootstrap'
38
+ @puppeteer = @gemfile.include? 'puppeteer'
39
+
40
+ ### node modules ###
41
+
42
+ @package_json = []
43
+
44
+ if File.exist? 'package.json'
45
+ @package_json += JSON.load_file('package.json')['dependencies'].keys rescue []
46
+ end
47
+
48
+ @puppeteer ||= @package_json.include? 'puppeteer'
24
49
 
25
- package_json = IO.read('package.json') rescue ''
26
- @puppeteer ||= package_json.include? 'puppeteer'
50
+ ### cable/redis ###
27
51
 
28
52
  @cable = ! Dir['app/channels/*.rb'].empty?
29
53
 
@@ -1,3 +1,3 @@
1
1
  module Fly_io
2
- VERSION = '0.2.4'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -20,19 +20,28 @@ class AppGenerator < Rails::Generators::Base
20
20
  def generate_app
21
21
  source_paths.push File.expand_path('../templates', __dir__)
22
22
 
23
+ # the plan is to make eject an option, default to false, but until
24
+ # that is ready, have generate fly:app always eject
25
+ options = options.to_h.dup
26
+ options[:eject] = options[:nomad]
27
+
23
28
  create_app(**options.symbolize_keys)
24
29
 
25
30
  action = Fly::Actions.new(@app, options)
26
31
 
27
32
  action.generate_toml
28
33
  action.generate_fly_config unless File.exist? 'config/fly.rb'
29
- action.generate_dockerfile unless File.exist? 'Dockerfile'
30
- action.generate_dockerignore unless File.exist? '.dockerignore'
31
- action.generate_nginx_conf unless File.exist? 'config/nginx.conf'
32
- action.generate_raketask unless File.exist? 'lib/tasks/fly.rake'
33
- action.generate_procfile unless File.exist? 'Procfile.rake'
34
- action.generate_litefs if options[:litefs] and not File.exist? 'config/litefs'
35
- action.generate_patches
34
+
35
+ if options[:eject]
36
+ action.generate_dockerfile unless File.exist? 'Dockerfile'
37
+ action.generate_dockerignore unless File.exist? '.dockerignore'
38
+ action.generate_nginx_conf unless File.exist? 'config/nginx.conf'
39
+ action.generate_raketask unless File.exist? 'lib/tasks/fly.rake'
40
+ action.generate_procfile unless File.exist? 'Procfile.rake'
41
+ action.generate_litefs if options[:litefs] and not File.exist? 'config/litefs'
42
+ action.generate_patches
43
+ end
44
+
36
45
  action.generate_ipv4
37
46
  action.generate_ipv6
38
47
 
@@ -1,36 +1,9 @@
1
- # syntax = docker/dockerfile:experimental
2
-
3
- # Dockerfile used to build a deployable image for a Rails application.
4
- # Adjust as required.
5
- #
6
- # Common adjustments you may need to make over time:
7
- # * Modify version numbers for Ruby, Bundler, and other products.
8
- # * Add library packages needed at build time for your gems, node modules.
9
- # * Add deployment packages needed by your application
10
- # * Add (often fake) secrets needed to compile your assets
11
-
12
- #######################################################################
13
-
14
- # Learn more about the chosen Ruby stack, Fullstaq Ruby, here:
15
- # https://github.com/evilmartians/fullstaq-ruby-docker.
16
- #
17
- # We recommend using the highest patch level for better security and
18
- # performance.
19
-
20
- ARG RUBY_VERSION=<%= @ruby_version %>
21
- ARG VARIANT=jemalloc-slim
22
- FROM quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-${VARIANT} as base
1
+ # syntax = docker/dockerfile:1
23
2
 
3
+ FROM quay.io/evl.ms/fullstaq-ruby:<%= @ruby_version %>-jemalloc-slim as base
24
4
  LABEL fly_launch_runtime="rails"
25
5
 
26
- <% if @node -%>
27
- ARG NODE_VERSION=<%= @node_version %>
28
- ARG YARN_VERSION=<%= @yarn_version %>
29
- <% end -%>
30
- ARG BUNDLER_VERSION=<%= @bundler_version %>
31
-
32
- ARG RAILS_ENV=production
33
- ENV RAILS_ENV=${RAILS_ENV}
6
+ ENV RAILS_ENV=production
34
7
  <% if @anycable or not @passenger -%>
35
8
  ENV RAILS_LOG_TO_STDOUT true
36
9
  <% end -%>
@@ -45,19 +18,18 @@ ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}
45
18
 
46
19
  RUN mkdir /app
47
20
  WORKDIR /app
48
- RUN mkdir -p tmp/pids
49
21
 
50
22
  <% if @node -%>
51
23
  RUN curl https://get.volta.sh | bash
52
24
  ENV VOLTA_HOME /root/.volta
53
25
  ENV PATH $VOLTA_HOME/bin:/usr/local/bin:$PATH
54
- RUN volta install node@${NODE_VERSION} yarn@${YARN_VERSION} && \
26
+ RUN volta install node@<%= @node_version %> yarn@<%= @yarn_version %> && \
55
27
  gem update --system --no-document && \
56
- gem install -N bundler -v ${BUNDLER_VERSION}
28
+ gem install --no-document bundler --version <%= @bundler_version %>
57
29
 
58
30
  <% else -%>
59
31
  RUN gem update --system --no-document && \
60
- gem install -N bundler -v ${BUNDLER_VERSION}
32
+ gem install --no-document bundler --version <%= @bundler_version %>
61
33
 
62
34
  <% end -%>
63
35
  #######################################################################
@@ -89,7 +61,16 @@ RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
89
61
  FROM build_deps as gems
90
62
 
91
63
  COPY Gemfile* ./
92
- RUN bundle install && rm -rf vendor/bundle/ruby/*/cache
64
+ RUN bundle install && \
65
+ <% if @redis and not @gemfile.include? 'redis' -%>
66
+ <% @bundle_add = true -%>
67
+ bundle add redis && \
68
+ <% end -%>
69
+ <% if @postgresql and not @gemfile.include? 'pg' -%>
70
+ <% @bundle_add = true -%>
71
+ bundle add pg && \
72
+ <% end -%>
73
+ rm -rf vendor/bundle/ruby/*/cache
93
74
 
94
75
  <% if @node -%>
95
76
  #######################################################################
@@ -246,6 +227,9 @@ COPY config/hook_detached_process /etc/nginx/
246
227
 
247
228
  # Deploy your application
248
229
  COPY . .
230
+ <% if @bundle_add -%>
231
+ COPY --from=gems /app/Gemfile* ./
232
+ <% end -%>
249
233
 
250
234
  # Adjust binstubs to run on Linux and set current working directory
251
235
  RUN chmod +x /app/bin/* && \
@@ -45,7 +45,7 @@ namespace :fly do
45
45
  deps << 'db:migrate' if @sqlite3
46
46
  deps = deps.first if deps.length == 1
47
47
 
48
- if @procs.length > 1 ? ':server, [:formation]' : ':server'
48
+ if @procs.length > 1
49
49
  "task :server, [:formation] => #{deps.inspect} do |task, args|"
50
50
  else
51
51
  "task :server => #{deps.inspect} do"
@@ -22,4 +22,12 @@ end
22
22
  redis do
23
23
  plan "Free"
24
24
  end
25
+ <% end -%>
26
+
27
+ <% unless @nomad -%>
28
+ <%=
29
+ rakefile = File.expand_path('fly.rake.erb', __dir__)
30
+ template = ERB.new(IO.read(File.expand_path('fly.rake.erb', __dir__)), trim_mode: '-')
31
+ template.result(binding)
32
+ %>
25
33
  <% end -%>
@@ -1,4 +1,5 @@
1
1
  app = "<%= @app %>"
2
+ <% if @nomad -%>
2
3
  kill_signal = "SIGINT"
3
4
  kill_timeout = 5
4
5
  <% unless @avahi or @nats -%>
@@ -77,3 +78,4 @@ processes = []
77
78
  [[statics]]
78
79
  guest_path = "/app/public"
79
80
  url_prefix = "/"
81
+ <% end -%>
data/lib/tasks/fly.rake CHANGED
@@ -4,6 +4,12 @@ require 'fly.io-rails/actions'
4
4
  require 'toml'
5
5
  require 'json'
6
6
 
7
+ config = File.expand_path('config/fly.rb', Rails.application.root)
8
+ if File.exist? config
9
+ @config = Fly::DSL::Config.new
10
+ @config.instance_eval IO.read(config), config
11
+ end
12
+
7
13
  namespace :fly do
8
14
  desc 'Deploy fly application'
9
15
  task :deploy do
@@ -20,13 +26,13 @@ namespace :fly do
20
26
  action = Fly::Actions.new(app)
21
27
  action.generate_toml if @app
22
28
  action.generate_fly_config unless File.exist? 'config/fly.rb'
23
- action.generate_dockerfile unless File.exist? 'Dockerfile'
24
- action.generate_dockerignore unless File.exist? '.dockerignore'
29
+ action.generate_dockerfile
30
+ action.generate_dockerignore
25
31
  action.generate_raketask unless File.exist? 'lib/tasks/fly.rake'
26
32
  action.generate_procfile unless File.exist? 'Procfile.fly'
27
33
 
28
34
  # build and push an image
29
- out = FlyIoRails::Utils.tee 'fly deploy --build-only --push'
35
+ out = FlyIoRails::Utils.tee "flyctl deploy --build-only --push --dockerfile #{action.dockerfile} --ignorefile #{action.ignorefile}"
30
36
  image = out[/image:\s+(.*)/, 1]&.strip
31
37
 
32
38
  exit 1 unless image
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fly.io-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-16 00:00:00.000000000 Z
11
+ date: 2022-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fly-ruby