fly.io-rails 0.1.2 → 0.1.3

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: 90684da96d9f92f4540828899639869c1edfdba0103f44f68ce5036f87084471
4
- data.tar.gz: 90321e28165c4b3e686c1b0920e8fd2a0700edaa9674b2ff0e6a814135b9c712
3
+ metadata.gz: b0cfcab66d8f161efea6825e771f1bd95f3f57af5e6163dbdec933281a372f78
4
+ data.tar.gz: 84f1312372f746f150b3dc5dd0d5b35a31d180573c541a42228ae2e8b903c5d7
5
5
  SHA512:
6
- metadata.gz: 87a139a001b7b333dcc5abc0756ec5bdbf38a39e1a327f0ff9d6c6cffde9a282b6961deba487d9a1c08ad2e26496f616d8a99f094f5a724927c3e9a3017bfc76
7
- data.tar.gz: 8d9f3657f904f57109cb37cf8ce3cff8d028e35d50356cafd2f1b7dbe8cfa77ba4f6d2de078fe5fce2f8aa1d98ed59ddf94a27a2398f27b7e30cd71e01184a30
6
+ metadata.gz: c8dc7129a3b3a06ef98234c590b20e7d14d5d4091744079f353a308e703bcca1bb1aa89ece7c713c7d63723e42c68b90f4a5cfed1bd00cb9f78ebcfdd2411210
7
+ data.tar.gz: fa3c2b69e0672cc5c7c2115b17a0bcf5e4fd0adc46a0d1f0b6ee5a4cfc909b56dfe8ce4d5115435e59a903285357a4260521716e90b24397415af01f6798c2c0
@@ -1,5 +1,6 @@
1
1
  require 'open3'
2
2
  require 'thor'
3
+ require 'toml'
3
4
  require 'active_support'
4
5
  require 'active_support/core_ext/string/inflections'
5
6
  require 'fly.io-rails/machines'
@@ -15,8 +16,8 @@ module Fly
15
16
  include Fly::Scanner
16
17
  attr_accessor :options
17
18
 
18
- def initialize(app = nil, regions = nil)
19
- self.app = app if app
19
+ def initialize(app, regions = nil)
20
+ self.app = app
20
21
 
21
22
  @ruby_version = RUBY_VERSION
22
23
  @bundler_version = Bundler::VERSION
@@ -50,6 +51,11 @@ module Fly
50
51
  self.app = TOML.load_file('fly.toml')['app']
51
52
  end
52
53
 
54
+ def app_template template_file, destination
55
+ app
56
+ template template_file, destination
57
+ end
58
+
53
59
  def app=(app)
54
60
  @app = app
55
61
  @appName = @app.gsub('-', '_').camelcase(:lower)
@@ -58,33 +64,27 @@ module Fly
58
64
  source_paths.push File::expand_path('../generators/templates', __dir__)
59
65
 
60
66
  def generate_toml
61
- app
62
- template 'fly.toml.erb', 'fly.toml'
67
+ app_template 'fly.toml.erb', 'fly.toml'
63
68
  end
64
69
 
65
70
  def generate_fly_config
66
- app
67
- template 'fly.rb.erb', 'config/fly.rb'
71
+ app_template 'fly.rb.erb', 'config/fly.rb'
68
72
  end
69
73
 
70
74
  def generate_dockerfile
71
- app
72
- template 'Dockerfile.erb', 'Dockerfile'
75
+ app_template 'Dockerfile.erb', 'Dockerfile'
73
76
  end
74
77
 
75
78
  def generate_dockerignore
76
- app
77
- template 'dockerignore.erb', '.dockerignore'
79
+ app_template 'dockerignore.erb', '.dockerignore'
78
80
  end
79
81
 
80
82
  def generate_terraform
81
- app
82
- template 'main.tf.erb', 'main.tf'
83
+ app_template 'main.tf.erb', 'main.tf'
83
84
  end
84
85
 
85
86
  def generate_raketask
86
- app
87
- template 'fly.rake.erb', 'lib/tasks/fly.rake'
87
+ app_template 'fly.rake.erb', 'lib/tasks/fly.rake'
88
88
  end
89
89
 
90
90
  def generate_key
@@ -195,47 +195,16 @@ module Fly
195
195
  exit 1
196
196
  end
197
197
 
198
- def deploy(app, image)
199
-
198
+ def launch(app)
200
199
  secrets = JSON.parse(`flyctl secrets list --json`).
201
200
  map {|secret| secret["Name"]}
202
201
 
203
- config = {
204
- region: @region,
205
- app: app,
206
- name: "#{app}-machine",
207
- image: image,
208
- guest: {
209
- cpus: @config.machine.cpus,
210
- cpu_kind: @config.machine.cpu_kind,
211
- memory_mb: @config.machine.memory_mb
212
- },
213
- services: [
214
- {
215
- ports: [
216
- {port: 443, handlers: ["tls", "http"]},
217
- {port: 80, handlers: ["http"]}
218
- ],
219
- protocol: "tcp",
220
- internal_port: 8080
221
- }
222
- ]
223
- }
224
-
225
202
  unless secrets.include? 'RAILS_MASTER_KEY'
226
203
  generate_key
227
204
  end
228
205
 
229
206
  if @sqlite3
230
- volume = create_volume(app, @region, @config.sqlite3.size)
231
-
232
- config[:mounts] = [
233
- { volume: volume, path: '/mnt/volume' }
234
- ]
235
-
236
- config[:env] = {
237
- "DATABASE_URL" => "sqlite3:///mnt/volume/production.sqlite3"
238
- }
207
+ @volume = create_volume(app, @region, @config.sqlite3.size)
239
208
  elsif @postgresql and not secrets.include? 'DATABASE_URL'
240
209
  secret = create_postgres(app, @org, @region,
241
210
  @config.postgres.vm_size,
@@ -261,30 +230,80 @@ module Fly
261
230
  system cmd
262
231
  end
263
232
  end
233
+ end
264
234
 
265
- # build config for release machine, overriding server command
266
- release_config = config.dup
267
- release_config.delete :services
268
- release_config.delete :mounts
269
- release_config[:env] = { 'SERVER_COMMAND' => 'bin/rails fly:release' }
270
-
271
- # perform release
272
- say_status :fly, release_config[:env]['SERVER_COMMAND']
273
- machine = release(app, release_config)
274
- Fly::Machines.delete_machine app, machine if machine
235
+ def deploy(app, image)
236
+ launch(app)
275
237
 
276
- # start proxy, if necessary
277
- endpoint = Fly::Machines::fly_api_hostname!
238
+ # default config
239
+ config = {
240
+ region: @region,
241
+ app: app,
242
+ name: "#{app}-machine",
243
+ image: image,
244
+ guest: {
245
+ cpus: @config.machine.cpus,
246
+ cpu_kind: @config.machine.cpu_kind,
247
+ memory_mb: @config.machine.memory_mb
248
+ },
249
+ services: [
250
+ {
251
+ ports: [
252
+ {port: 443, handlers: ["tls", "http"]},
253
+ {port: 80, handlers: ["http"]}
254
+ ],
255
+ protocol: "tcp",
256
+ internal_port: 8080
257
+ }
258
+ ]
259
+ }
278
260
 
279
- # stop previous instances - list will fail on first run
280
- stdout, stderr, status = Open3.capture3('fly machines list --json')
281
- unless stdout.empty?
282
- JSON.parse(stdout).each do |list|
283
- next if list['id'] == machine
284
- system "fly machines remove --force #{list['id']}"
261
+ # only run release step if there is a non-empty release task in fly.rake
262
+ if (IO.read('lib/tasks/fly.rake') rescue '') =~ /^\s*task[ \t]*+:?release"?[ \t]*\S/
263
+ # build config for release machine, overriding server command
264
+ release_config = config.dup
265
+ release_config.delete :services
266
+ release_config.delete :mounts
267
+ release_config[:env] = { 'SERVER_COMMAND' => 'bin/rails fly:release' }
268
+
269
+ # perform release
270
+ say_status :fly, release_config[:env]['SERVER_COMMAND']
271
+ machine = release(app, release_config)
272
+ Fly::Machines.delete_machine app, machine if machine
273
+
274
+ # start proxy, if necessary
275
+ endpoint = Fly::Machines::fly_api_hostname!
276
+
277
+ # stop previous instances - list will fail on first run
278
+ stdout, stderr, status = Open3.capture3('fly machines list --json')
279
+ unless stdout.empty?
280
+ JSON.parse(stdout).each do |list|
281
+ next if list['id'] == machine
282
+ system "fly machines remove --force #{list['id']}"
283
+ end
285
284
  end
286
285
  end
287
286
 
287
+ # configure sqlite3 (can be overridden by fly.toml)
288
+ if @sqlite3
289
+ config[:mounts] = [
290
+ { volume: @volume, path: '/mnt/volume' }
291
+ ]
292
+
293
+ config[:env] = {
294
+ "DATABASE_URL" => "sqlite3:///mnt/volume/production.sqlite3"
295
+ }
296
+ end
297
+
298
+ # process toml overrides
299
+ toml = (TOML.load_file('fly.toml') rescue {})
300
+ config[:env] = toml['env'] if toml['env']
301
+ config[:services] = toml['services'] if toml['services']
302
+ if toml['mounts']
303
+ mounts = toml['mounts']
304
+ config[:mounts] = [ { volume: mounts['source'], path: mounts['destination'] } ]
305
+ end
306
+
288
307
  # start app
289
308
  say_status :fly, "start #{app}"
290
309
  start = Fly::Machines.create_and_start_machine(app, config: config)
@@ -39,7 +39,6 @@ module FlyIoRails
39
39
  exit 1 unless output =~ /^New app created: /
40
40
 
41
41
  @app = output.split.last
42
- template 'fly.toml.erb', 'fly.toml' if defined? template # rake tasks are on their own
43
42
 
44
43
  unless regions.empty?
45
44
  @regions = regions.flatten
@@ -1,3 +1,3 @@
1
1
  module Fly_io
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -15,6 +15,7 @@ class AppGenerator < Rails::Generators::Base
15
15
 
16
16
  action = Fly::Actions.new(@app, options[:region])
17
17
 
18
+ action.generate_toml
18
19
  action.generate_fly_config unless File.exist? 'config/fly.rb'
19
20
  action.generate_dockerfile unless File.exist? 'Dockerfile'
20
21
  action.generate_dockerignore unless File.exist? '.dockerignore'
@@ -22,8 +23,8 @@ class AppGenerator < Rails::Generators::Base
22
23
  action.generate_patches
23
24
  action.generate_ipv4
24
25
  action.generate_ipv6
25
- action.generate_key
26
-
26
+
27
+ action.launch(@app)
27
28
  end
28
29
  end
29
- end
30
+ end
@@ -15,6 +15,7 @@ class TerraformGenerator < Rails::Generators::Base
15
15
 
16
16
  action = Fly::Actions.new(@app, options[:region])
17
17
 
18
+ action.generate_toml
18
19
  action.generate_dockerfile
19
20
  action.generate_dockerignore
20
21
  action.generate_terraform
@@ -1,4 +1,56 @@
1
1
  app = "<%= @app %>"
2
+ kill_signal = "SIGINT"
3
+ kill_timeout = 5
4
+ processes = []
5
+
6
+ [build]
7
+ [build.args]
8
+ BUILD_COMMAND = "bin/rails fly:build"
9
+ SERVER_COMMAND = "bin/rails fly:server"
2
10
 
3
11
  [deploy]
4
- release_command = "bundle exec rails fly:release"
12
+ release_command = "bin/rails fly:release"
13
+
14
+ [env]
15
+ PORT = "8080"
16
+ <% if @sqlite3 -%>
17
+ DATABASE_URL = "sqlite3:///mnt/volume/production.sqlite3"
18
+
19
+ [mounts]
20
+ source = <%= "#{app.gsub('-', '_')}_volume".inspect %>
21
+ destination = "/mnt/volume"
22
+ <% end -%>
23
+
24
+ [experimental]
25
+ allowed_public_ports = []
26
+ auto_rollback = true
27
+
28
+ [[services]]
29
+ http_checks = []
30
+ internal_port = 8080
31
+ processes = ["app"]
32
+ protocol = "tcp"
33
+ script_checks = []
34
+ [services.concurrency]
35
+ hard_limit = 25
36
+ soft_limit = 20
37
+ type = "connections"
38
+
39
+ [[services.ports]]
40
+ force_https = true
41
+ handlers = ["http"]
42
+ port = 80
43
+
44
+ [[services.ports]]
45
+ handlers = ["tls", "http"]
46
+ port = 443
47
+
48
+ [[services.tcp_checks]]
49
+ grace_period = "1s"
50
+ interval = "15s"
51
+ restart_limit = 0
52
+ timeout = "2s"
53
+
54
+ [[statics]]
55
+ guest_path = "/app/public"
56
+ url_prefix = "/"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fly.io-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby