fly.io-rails 0.1.4-aarch64-linux → 0.1.5-aarch64-linux

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: 22b91ca60f3ae2c948b1ebae2ca41ca8f3d4b086506b879c1f895f00a974b9ca
4
- data.tar.gz: 9de48ba6a2585299e327682b402883756836d4131202ae7ba6051ca5ac27d705
3
+ metadata.gz: 7f265d197c222de755429918a3352d1cc40faaccedb226e176faf8a3950dcfee
4
+ data.tar.gz: 4e666a3e99617b66345bde33fcaa5923e3a8b621d0eb30a983483d90ac0e3219
5
5
  SHA512:
6
- metadata.gz: 4acc687e3cbaca4f6e125c27d11753b5d1e0c9f4476b2b41901aafaf465ece4f952d450478f84681dbd5e8e026a70ad674e93cc80e0b040aff548bf1923a4847
7
- data.tar.gz: 34551d1ee07779d81cc7a3bd7d7c4b3316fc0fdb882d7aa167eca32048b5c4a55cab716f11c51efaccec35a300818de197c3b21dcfc4800ab3f5afca2f07d4e9
6
+ metadata.gz: d65873cf05de4908e47d47ed6891002da3c0c7759b6e5e27d4c2afb16281a631a51a8c5fc94c9ba38d421cba3d7efd7b8d15c5a75772bc3daaa81fba9770fd5e
7
+ data.tar.gz: d5c90c278e1264ad63d60cf723b6962b847779085161ef1ea5c41617ec2df54bc17da3dcc424d40364181c4452905e30acb93985e538168e5fe55763a2dc99cf
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: aarch64-linux
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