fly.io-rails 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd72efc17bf3e1b7826d569f0e72dcb500fb1b49a116b2c2f84805b3d9c05ecc
4
- data.tar.gz: 7154bc14b25e0267d34c461359577bd8a25d9ed5c70cf329b0533d32100e2116
3
+ metadata.gz: a9adb2bd6c53c8d27b9ade8898e4a5fb4621f5711781e35b8a273535e66996ec
4
+ data.tar.gz: 749a208099b67b3bad53c1b729ef873bb911ae7e69368f7b04863aa65519fee8
5
5
  SHA512:
6
- metadata.gz: 345f8065e2fcf7515b27022965ee4669d6d03eb38c713114a77da92806b4063c2860fd35eb3208ad55eb53852797043a05f3dc0fd711c5ac8263e603ada1c8ab
7
- data.tar.gz: 4ce36e49a13734dbedc5c5e4663d9fdb616f44aab70bdf7c63e0ca342b536bdaac820680636fd3bb80ea0c94d648f0fe52b5c58f8669ee938c7d482552b952cb
6
+ metadata.gz: 60db97b7cf7f75b5c5485ca6395c2d6398b54148a8e5fb1547483c272366c052f2d6810a3882fe65eda906627e936555269dc71b1208404ab1d772d29082c640
7
+ data.tar.gz: 0fd8e2988a4ece554272b0a61e63994243bfa23ad421f2ffdd9f0fd7f5beb446ef09cad99acfbdd7c787ed21b398a597ada26f74d203310dd444667aad7a82a2
data/README.md CHANGED
@@ -10,6 +10,25 @@ For usage instructions, see the following guides:
10
10
  * [Lite FS](https://fly.io/docs/rails/advanced-guides/litefs/)
11
11
  * [Terraform](https://fly.io/docs/rails/advanced-guides/terraform/)
12
12
 
13
+ ## Generator options
14
+
15
+ * `--name` name of the application. If a name is not provided, one will be generated for you.
16
+ * `--org` the organization to operate on. Defaults to `personal`.
17
+ * `--region` region to launch the application in. Accepts multiple values, and can be specified multiple times.
18
+ * `--nomad` generates a nomad application instead of a machines application.
19
+ * `--litefs` adds support for replicated sqlite3 databases via [litefs](https://fly.io/blog/introducing-litefs/). Only works on nomad machines currently.
20
+ * `--passenger` run your Rails application with [nginx](https://www.nginx.com/) and [Phusion Passenger](https://www.phusionpassenger.com/).
21
+ * `--serverless` configures your application to exit after 5 minutes of inactivity. Machines will automatically restart when next accessed. Only works with passenger currently.
22
+
23
+ ## Automatically detected features
24
+
25
+ * _ruby_: the deployed application will use the same version of ruby and bundler as your development environment.
26
+ * _node_: if the use of node is detected, node, yarn, and your npm packages will be installed.
27
+ * _sqlite3_: if the production database is sqlite3 a volume will be allocated and the database will be put there.
28
+ * _postgres_: if the production database is postgres a postgres machine will be allocated
29
+ * _redis_: if redis is used for action cable, caching, or sidekiq your redis database will be added to this application. If you don't currently have a redis database, one will be allocated. If redis is used for caching, eviction will be turned on.
30
+ * _sidekiq_: if sidekiq is used it will be launched along side of your rails application.
31
+
13
32
  ## Key files
14
33
 
15
34
  * Entrypoints: [lib/tasks/fly.rake](./lib/tasks/fly.rake), [lib/generators/app_generator.rb](./lib/generators/app_generator.rb), [lib/generators/terraform_generator.rb](.lib/generators/terraform_generator.rb) contain the deploy task, fly:app generator and
@@ -204,7 +204,7 @@ module Fly
204
204
  start = Fly::Machines.create_and_start_machine(app, config: config)
205
205
  machine = start[:id]
206
206
 
207
- if !machine
207
+ if not machine
208
208
  STDERR.puts 'Error starting release machine'
209
209
  PP.pp start, STDERR
210
210
  exit 1
@@ -214,24 +214,27 @@ module Fly
214
214
  timeout: 60, state: 'started'
215
215
 
216
216
  # wait for release to copmlete
217
- status = nil
218
217
  5.times do
219
218
  status = Fly::Machines.wait_for_machine app, machine,
220
- timeout: 60, state: 'stopped'
221
- return machine if status[:ok]
219
+ instance_id: start[:instance_id], timeout: 60, state: 'stopped'
220
+ break if status[:ok]
222
221
  end
223
222
 
224
- # wait for release to copmlete
225
- event = nil
226
- 90.times do
227
- sleep 1
228
- status = Fly::Machines.get_a_machine app, machine
229
- event = status[:events]&.first
230
- return machine if event && event[:type] == 'exit'
231
- end
223
+ if status and status[:ok]
224
+ event = nil
225
+ 300.times do
226
+ status = Fly::Machines.get_a_machine app, start[:id]
227
+ event = status[:events]&.first
228
+ break if event[:type] == 'exit'
229
+ sleep 0.2
230
+ end
232
231
 
233
- STDERR.puts event.to_json
234
- exit 1
232
+ exit_code = event&.dig(:request, :exit_event, :exit_code)
233
+ Fly::Machines.delete_machine app, machine if machine
234
+ return event, exit_code, machine
235
+ else
236
+ return status, nil, nil
237
+ end
235
238
  end
236
239
 
237
240
  def launch(app)
@@ -313,8 +316,14 @@ module Fly
313
316
 
314
317
  # perform release
315
318
  say_status :fly, release_config[:env]['SERVER_COMMAND']
316
- machine = release(app, release_config)
317
- Fly::Machines.delete_machine app, machine if machine
319
+ event, exit_code, machine = release(app, release_config)
320
+
321
+ if exit_code != 0
322
+ STDERR.puts 'Error performing release'
323
+ STDERR.puts (exit_code ? {exit_code: exit_code} : event).inspect
324
+ STDERR.puts "run 'flyctl logs --instance #{machine}' for more information"
325
+ exit 1
326
+ end
318
327
 
319
328
  # start proxy, if necessary
320
329
  endpoint = Fly::Machines::fly_api_hostname!
@@ -374,18 +383,17 @@ module Fly
374
383
  end
375
384
 
376
385
  def terraform(app, image)
377
- # update main.tf with the image name
378
- tf = IO.read('main.tf')
379
- tf[/^\s*image\s*=\s*"(.*?)"/, 1] = image.strip
380
- IO.write 'main.tf', tf
381
-
382
- # find first machine in terraform config file
383
- machines = Fly::HCL.parse(IO.read('main.tf')).find {|block|
384
- block.keys.first == :resource and
385
- block.values.first.keys.first == 'fly_machine'}
386
+ # find first machine using the image ref in terraform config file
387
+ machine = Fly::HCL.parse(IO.read('main.tf')).
388
+ map {|block| block.dig(:resource, 'fly_machine')}.compact.
389
+ find {|machine| machine.values.first[:image] == 'var.image_ref'}
390
+ if not machine
391
+ STDERR.puts 'unable to find fly_machine with image = var.image_ref in main.rf'
392
+ exit 1
393
+ end
386
394
 
387
395
  # extract HCL configuration for the machine
388
- config = machines.values.first.values.first.values.first
396
+ config = machine.values.first
389
397
 
390
398
  # delete HCL specific configuration items
391
399
  %i(services for_each region app name depends_on).each do |key|
@@ -407,40 +415,25 @@ module Fly
407
415
  config[:env] ||= {}
408
416
  config[:env]['SERVER_COMMAND'] = 'bin/rails fly:release'
409
417
 
418
+ # fill in image
419
+ config[:image] = image
420
+
410
421
  # start proxy, if necessary
411
422
  endpoint = Fly::Machines::fly_api_hostname!
412
423
 
413
- # start release machine
414
- STDERR.puts "--> #{config[:env]['SERVER_COMMAND']}"
415
- start = Fly::Machines.create_and_start_machine(app, config: config)
416
- machine = start[:id]
417
-
418
- if !machine
419
- STDERR.puts 'Error starting release machine'
420
- PP.pp start, STDERR
421
- exit 1
422
- end
423
-
424
- # wait for release to copmlete
425
- event = nil
426
- 90.times do
427
- sleep 1
428
- status = Fly::Machines.get_a_machine app, machine
429
- event = status[:events]&.first
430
- break if event && event[:type] == 'exit'
424
+ # perform release, if necessary
425
+ if (IO.read('lib/tasks/fly.rake') rescue '') =~ /^\s*task[ \t]*+:?release"?[ \t]*\S/
426
+ say_status :fly, config[:env]['SERVER_COMMAND']
427
+ event, exit_code, machine = release(app, config)
428
+ else
429
+ exit_code = 0
431
430
  end
432
431
 
433
- # extract exit code
434
- exit_code = event.dig(:request, :exit_event, :exit_code)
435
-
436
432
  if exit_code == 0
437
- # delete release machine
438
- Fly::Machines.delete_machine app, machine
439
-
440
433
  # use terraform apply to deploy
441
434
  ENV['FLY_API_TOKEN'] = `flyctl auth token`.chomp
442
435
  ENV['FLY_HTTP_ENDPOINT'] = endpoint if endpoint
443
- system 'terraform apply -auto-approve'
436
+ system "terraform apply -auto-approve -var=\"image_ref=#{image}\""
444
437
  else
445
438
  STDERR.puts 'Error performing release'
446
439
  STDERR.puts (exit_code ? {exit_code: exit_code} : event).inspect
@@ -1,3 +1,3 @@
1
1
  module Fly_io
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
@@ -9,6 +9,8 @@ class TerraformGenerator < Rails::Generators::Base
9
9
  class_option :region, type: :array, repeatable: true, default: []
10
10
 
11
11
  class_option :litefs, type: :boolean, default: false
12
+ class_option :passenger, type: :boolean, default: false
13
+ class_option :serverless, type: :boolean, default: false
12
14
 
13
15
  def terraform
14
16
  source_paths.push File.expand_path('../templates', __dir__)
@@ -20,6 +22,7 @@ class TerraformGenerator < Rails::Generators::Base
20
22
  action.generate_toml
21
23
  action.generate_dockerfile
22
24
  action.generate_dockerignore
25
+ action.generate_nginx_conf
23
26
  action.generate_terraform
24
27
  action.generate_raketask
25
28
  action.generate_procfile
@@ -2,11 +2,16 @@ terraform {
2
2
  required_providers {
3
3
  fly = {
4
4
  source = "fly-apps/fly"
5
- version = "0.0.18"
5
+ version = "0.0.20"
6
6
  }
7
7
  }
8
8
  }
9
9
 
10
+ variable "image_ref" {
11
+ type = string
12
+ description = "docker images containing the application"
13
+ }
14
+
10
15
  /* uncomment if you want an internal tunnel
11
16
  provider "fly" {
12
17
  useinternaltunnel = true
@@ -49,7 +54,7 @@ resource "fly_machine" "<%= @appName %>Machine" {
49
54
 
50
55
  app = <%= @app.inspect %>
51
56
  name = "<%= @app %>-${each.value}"
52
- image = "quay.io/evl.ms/fullstaq-ruby:<%= @ruby_version %>-jemalloc-slim"
57
+ image = var.image_ref
53
58
 
54
59
  # Scale application resources
55
60
  cpus = 1
data/lib/tasks/fly.rake CHANGED
@@ -37,6 +37,18 @@ namespace :fly do
37
37
  action.generate_ipv6 if @app
38
38
  action.deploy(app, image)
39
39
  end
40
+
41
+ JSON.parse(`fly apps list --json`).each do |info|
42
+ if info['Name'] == app
43
+ 60.times do
44
+ response = Net::HTTP.get_response(URI::HTTPS.build(host: info['Hostname']))
45
+ puts "Server status: #{response.code} #{response.message}"
46
+ break
47
+ rescue Errno::ECONNRESET
48
+ sleep 0.5
49
+ end
50
+ end
51
+ end
40
52
  end
41
53
  end
42
54
 
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.1.7
4
+ version: 0.1.8
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-10-07 00:00:00.000000000 Z
11
+ date: 2022-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fly-ruby
@@ -73,11 +73,11 @@ files:
73
73
  - lib/generators/templates/nginx.conf.erb
74
74
  - lib/generators/templates/patches/action_cable.rb
75
75
  - lib/tasks/fly.rake
76
- homepage: https://github.com/rubys/fly-io.rails
76
+ homepage: https://github.com/rubys/fly.io-rails
77
77
  licenses:
78
78
  - Apache-2.0
79
79
  metadata:
80
- homepage_uri: https://github.com/rubys/fly-io.rails
80
+ homepage_uri: https://github.com/rubys/fly.io-rails
81
81
  post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths: