fly.io-rails 0.1.4-x86_64-darwin → 0.1.5-x86_64-darwin

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: b1f1f8eac6cd28b180df67c861408142b219c2d8523583919ca66bb12ded23cd
4
- data.tar.gz: 82a84e889648133520c4819b24bd1aaa7ac752c195f414964df170d25db67de9
3
+ metadata.gz: 55370806937acab455f03d4971b889d1ee1b6127e9fa4a523750c90b7224a2d5
4
+ data.tar.gz: ae64fb18e008db2390db9ae109548c1b6874fc0a1e4ba0cac571f038b468f9d3
5
5
  SHA512:
6
- metadata.gz: e741dd35010d444516d2aa0ffb1560608edd119902e4c9225753426ec7ba7f7ba537e2d0c9c2624e35b927aae17ef79909da0ef02b635c68f5dc2f909476dd91
7
- data.tar.gz: 93bfc9b3eb4db893d522204387310b4f78819fda7e98c010cbb68c6f4d358a869a6ac74017fe3acf5bf2dfb59a1fc25520eefd20fbfcb1c75335fd7b381930b8
6
+ metadata.gz: 759a1dbc80d533824d71f5650e4e536f8a634395b9a75d0c2ce71c1fe689bee656164a3a77e3e4dc031d9a3f8e1f77500188ed6132c9e6509bf26a71c65bbe2b
7
+ data.tar.gz: 563d8f653b03f98b9ce40a6089f115eae7d23c5a84983c758daf6bc99fc39ed8f8a2833f4ce10888092c95bcd31c341727298928614e8c925c969cb431dc04df
data/README.md CHANGED
@@ -2,11 +2,28 @@
2
2
 
3
3
  Add [Fly.io](https://fly.io) support to [Rails](https://rubyonrails.org/).
4
4
 
5
- ## Status
5
+ ## Usage
6
+
7
+ For usage instructions, see the following guides:
8
+
9
+ * [Machine API](https://fly.io/docs/rails/advanced-guides/machine/)
10
+ * [Lite FS](https://fly.io/docs/rails/advanced-guides/litefs/)
11
+ * [Terraform](https://fly.io/docs/rails/advanced-guides/terraform/)
12
+
13
+ ## Key files
14
+
15
+ * 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
16
+ fly:terraform generator respectively.
17
+ * [lib/fly.io-rails/actions.rb](./lib/fly.io-rails/actions.rb) contains Thor actions used by the
18
+ rake task and generators. Does some funky stuff to allow Thor actions to
19
+ be called from Rake.
20
+ * [lib/fly.io-rails/machines.rb](./lib/fly.io-rails/machines.rb) wraps Fly.io's machine API as a Ruby module.
21
+ * [lib/generators/templates](./lib/generators/templates) contains erb
22
+ templates for all of the files produced primarily by the generator, but also
23
+ by the deploy task.
24
+ * [Rakefile](./Rakefile) used to build gems. Includes native binaries for each supported platform.
6
25
 
7
- <u>pre-alpha</u>.
8
26
 
9
- For usage instructions, see [Fly.io Rails Advanced Guide: Terraform](https://fly.io/docs/rails/advanced-guides/terraform/).
10
27
 
11
28
  ## Build instructions
12
29
 
@@ -19,3 +36,46 @@ every supported platform as well as an additional gem that doesn't
19
36
  include a binary.
20
37
 
21
38
  To download new binaries, run `rake clobber` then `rake package` agein.
39
+
40
+ ## Debugging instructions
41
+
42
+ This gem provides a Railtie, with rake tasks and a generator that uses
43
+ Thor and templates. Being in Ruby, there is no "compile" step. That
44
+ coupled with Bundler "local overrides" makes testing a breeze. And
45
+ Rails 7 applications without node dependencies are quick to create.
46
+
47
+ A script like the following will destroy previous fly applications,
48
+ create a new rails app, add and then override this gem, and finally
49
+ copy in any files that would need to be manually edited.
50
+
51
+ ```
52
+ if [ -e welcome/fly.toml ]; then
53
+ app=$(awk -e '/^app\s+=/ { print $3 }' welcome/fly.toml | sed 's/"//g')
54
+ fly apps destroy -y $app
55
+ fi
56
+ rm -rf welcome
57
+ rails new welcome
58
+ cd welcome
59
+ bundle config disable_local_branch_check true
60
+ bundle config set --local local.fly.io-rails /Users/rubys/git/fly.io-rails
61
+ bundle add fly.io-rails --git https://github.com/rubys/fly.io-rails.git
62
+ cp ../routes.rb config
63
+ # bin/rails generate terraform
64
+ # fly secrets set FLY_API_TOKEN=$(fly auth token)
65
+ # bin/rails generate controller job start complete status
66
+ # bin/rails generate job machine
67
+ # cp ../job_controller.rb app/controllers/
68
+ # cp ../machine_job.rb app/jobs/
69
+ ```
70
+
71
+ Once created, I rerun using:
72
+
73
+ ```
74
+ cd ..; sh redo-welcome; cd welcome;
75
+ ```
76
+
77
+ Generally after the finaly semicolon, I have commands like
78
+ `bin/rails generate fly:app --litefs; fly deploy`. Rerunning
79
+ after I make a change is a matter of pushing the up arrow until
80
+ I find this command and then pressing enter.
81
+
Binary file
@@ -17,11 +17,17 @@ module Fly
17
17
  attr_accessor :options
18
18
 
19
19
  def initialize(app, options={})
20
+ # placate thor
21
+ @options = {}
22
+ @destination_stack = [Dir.pwd]
23
+
24
+ # extract options
20
25
  self.app = app
21
- regions = options[:region].flatten || []
26
+ regions = options[:region]&.flatten || []
22
27
  @litefs = options[:litefs]
23
- @set_stage = options[:nomad] ? 'set' : 'set --stage'
28
+ @nomad = options[:nomad]
24
29
 
30
+ # prepare template variablesl
25
31
  @ruby_version = RUBY_VERSION
26
32
  @bundler_version = Bundler::VERSION
27
33
  @node = File.exist? 'node_modules'
@@ -29,9 +35,9 @@ module Fly
29
35
  @node_version = @node ? `node --version`.chomp.sub(/^v/, '') : '16.17.0'
30
36
  @org = Fly::Machines.org
31
37
 
32
- @options = {}
33
- @destination_stack = [Dir.pwd]
38
+ @set_stage = @nomad ? 'set' : 'set --stage'
34
39
 
40
+ # determine region
35
41
  if !regions or regions.empty?
36
42
  @regions = JSON.parse(`flyctl regions list --json --app #{app}`)['Regions'].
37
43
  map {|region| region['Code']} rescue []
@@ -42,11 +48,13 @@ module Fly
42
48
  @region = @regions.first || 'iad'
43
49
  @regions = [@region] if @regions.empty?
44
50
 
51
+ # Process DSL
45
52
  @config = Fly::DSL::Config.new
46
53
  if File.exist? 'config/fly.rb'
47
54
  @config.instance_eval IO.read('config/fly.rb')
48
55
  end
49
56
 
57
+ # set additional variables based on application source
50
58
  scan_rails_app
51
59
  end
52
60
 
@@ -91,6 +99,11 @@ module Fly
91
99
  app_template 'fly.rake.erb', 'lib/tasks/fly.rake'
92
100
  end
93
101
 
102
+ def generate_procfile
103
+ return unless @sidekiq
104
+ app_template 'Procfile.fly.erb', 'Procfile.fly'
105
+ end
106
+
94
107
  def generate_litefs
95
108
  app_template 'litefs.yml.erb', 'config/litefs.yml'
96
109
  end
@@ -147,6 +160,7 @@ module Fly
147
160
 
148
161
  def create_postgres(app, org, region, vm_size, volume_size, cluster_size)
149
162
  cmd = "flyctl postgres create --name #{app}-db --org #{org} --region #{region} --vm-size #{vm_size} --volume-size #{volume_size} --initial-cluster-size #{cluster_size}"
163
+ cmd += ' --machines' unless @nomad
150
164
  say_status :run, cmd
151
165
  output = FlyIoRails::Utils.tee(cmd)
152
166
  output[%r{postgres://\S+}]
@@ -10,7 +10,7 @@ module Fly
10
10
  # the caller is trusted and does pass through unsanitized user input.
11
11
  #
12
12
  module Machines
13
- @@api_token = nil
13
+ @@api_token = ENV['FLY_API_TOKEN']
14
14
  @@fly_api_hostname = nil
15
15
 
16
16
  # determine fly api hostname. Returns nil if no proxy is running
@@ -160,14 +160,25 @@ module Fly
160
160
  api(path) {|uri| request = Net::HTTP::Delete.new(uri) }
161
161
  end
162
162
 
163
+ # graphql -- see https://til.simonwillison.net/fly/undocumented-graphql-api
164
+ def self.graphql(query)
165
+ api('/graphql', 'https://api.fly.io/') do |path|
166
+ request = Net::HTTP::Post.new(path)
167
+ request.body = { query: query }.to_json
168
+ request
169
+ end
170
+ end
171
+
163
172
  # common processing for all APIs
164
- def self.api(path, &make_request)
165
- uri = URI("http://#{fly_api_hostname}#{path}")
173
+ def self.api(path, host=nil, &make_request)
174
+ host ||= "http://#{fly_api_hostname}"
175
+ uri = URI.join(host, path)
166
176
  http = Net::HTTP.new(uri.host, uri.port)
177
+ http.use_ssl = true if uri.instance_of? URI::HTTPS
167
178
 
168
179
  request = make_request.call(uri.request_uri)
169
180
 
170
- @@api_token ||= `fly auth token`.chomp
181
+ @@api_token ||= `flyctl auth token`.chomp
171
182
  headers = {
172
183
  "Authorization" => "Bearer #{@@api_token}",
173
184
  "Content-Type" => "application/json",
@@ -12,6 +12,8 @@ module Fly
12
12
  @postgresql = true
13
13
  end
14
14
 
15
+ @sidekiq = IO.read('Gemfile').include? 'sidekiq' rescue false
16
+
15
17
  if (YAML.load_file('config/cable.yml').dig('production', 'adapter') rescue false)
16
18
  @redis_cable = true
17
19
  end
@@ -20,7 +22,7 @@ module Fly
20
22
  @redis_cache = true
21
23
  end
22
24
 
23
- @redis = @redis_cable || @redis_cache
25
+ @redis = @redis_cable || @redis_cache || @sidekiq
24
26
  end
25
27
  end
26
28
  end
@@ -1,3 +1,3 @@
1
1
  module Fly_io
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
data/lib/fly.io-rails.rb CHANGED
@@ -3,9 +3,25 @@ require 'fly.io-rails/generators'
3
3
  require 'fly.io-rails/utils'
4
4
 
5
5
  class FlyIoRailtie < Rails::Railtie
6
+ # load rake tasks
6
7
  rake_tasks do
7
8
  Dir[File.expand_path('tasks/*.rake', __dir__)].each do |file|
8
9
  load file
9
10
  end
10
11
  end
12
+
13
+ # set FLY_IMAGE_NAME on Nomad VMs
14
+ if not ENV['FLY_IMAGE_NAME'] and ENV['FLY_APP_NAME'] and ENV['FLY_API_TOKEN']
15
+ require 'fly.io-rails/machines'
16
+
17
+ ENV['FLY_IMAGE_REF'] = Fly::Machines.graphql(%{
18
+ query {
19
+ app(name: "#{ENV['FLY_APP_NAME']}") {
20
+ currentRelease {
21
+ imageRef
22
+ }
23
+ }
24
+ }
25
+ }).dig(:data, :app, :currentRelease, :imageRef)
26
+ end
11
27
  end
@@ -23,6 +23,7 @@ class AppGenerator < Rails::Generators::Base
23
23
  action.generate_dockerfile unless File.exist? 'Dockerfile'
24
24
  action.generate_dockerignore unless File.exist? '.dockerignore'
25
25
  action.generate_raketask unless File.exist? 'lib/tasks/fly.rake'
26
+ action.generate_procfile unless File.exist? 'Procfile.rake'
26
27
  action.generate_litefs if options[:litefs] and not File.exist? 'config/litefs'
27
28
  action.generate_patches
28
29
  action.generate_ipv4
@@ -22,6 +22,7 @@ class TerraformGenerator < Rails::Generators::Base
22
22
  action.generate_dockerignore
23
23
  action.generate_terraform
24
24
  action.generate_raketask
25
+ action.generate_procfile
25
26
  action.generate_patches
26
27
 
27
28
  action.generate_key
@@ -81,6 +81,9 @@ RUN gem update --system --no-document && \
81
81
 
82
82
  COPY Gemfile* ./
83
83
  RUN bundle install && rm -rf vendor/bundle/ruby/*/cache
84
+ <% if @sidekiq -%>
85
+ RUN gem install foreman
86
+ <% end -%>
84
87
 
85
88
  <% if @node -%>
86
89
  #######################################################################
@@ -0,0 +1,4 @@
1
+ web: bin/rails server
2
+ <% if @sidekiq -%>
3
+ worker: bundle exec sidekiq
4
+ <% end -%>
@@ -28,7 +28,13 @@ namespace :fly do
28
28
  <%- else -%>
29
29
  task :server => :swapfile do
30
30
  <%- end -%>
31
+ <%- if @sidekiq -%>
32
+ Bundler.with_original_env do
33
+ sh 'foreman start --procfile=Procfile.fly'
34
+ end
35
+ <%- else -%>
31
36
  sh 'bin/rails server'
37
+ <%- end -%>
32
38
  end
33
39
 
34
40
  # optional SWAPFILE task:
data/lib/tasks/fly.rake CHANGED
@@ -22,6 +22,7 @@ namespace :fly do
22
22
  action.generate_dockerfile unless File.exist? 'Dockerfile'
23
23
  action.generate_dockerignore unless File.exist? '.dockerignore'
24
24
  action.generate_raketask unless File.exist? 'lib/tasks/fly.rake'
25
+ action.generate_procfile unless File.exist? 'Procfile.fly'
25
26
 
26
27
  # build and push an image
27
28
  out = FlyIoRails::Utils.tee 'fly deploy --build-only --push'
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.4
4
+ version: 0.1.5
5
5
  platform: x86_64-darwin
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-27 00:00:00.000000000 Z
11
+ date: 2022-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fly-ruby
@@ -63,6 +63,7 @@ files:
63
63
  - lib/generators/fly/app_generator.rb
64
64
  - lib/generators/fly/terraform_generator.rb
65
65
  - lib/generators/templates/Dockerfile.erb
66
+ - lib/generators/templates/Procfile.fly.erb
66
67
  - lib/generators/templates/dockerignore.erb
67
68
  - lib/generators/templates/fly.rake.erb
68
69
  - lib/generators/templates/fly.rb.erb