fly.io-rails 0.0.1-x86_64-darwin → 0.0.3-x86_64-darwin

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: f6ac0bc3732ed8cdf9c621c0696f26cf856552778e0be48af5d7d68348288a8c
4
- data.tar.gz: c6599e74063af9b1dd618bfc1e9cfb3f7fad56d06cb92c85c60fcee4745d0a12
3
+ metadata.gz: 3f76f7f8f758e7f3df3c22ed93a6ac83e543515844e6f7fce91d3902ac45f53a
4
+ data.tar.gz: 23741fd32ddcd05f7bd6b7aaec10143f2c3c1c03b7a92a37f8561d7685468609
5
5
  SHA512:
6
- metadata.gz: 2865d142fb9d3f379109ac48ae86953f47a4314de32fa390fe51b67e85f77a877d85c43017e8847c5346737f38a565f465253f1206fc47f5e4131edc3b91078b
7
- data.tar.gz: 88e26f046ca4447ad03b918de1540128dd0db162de021ed2346f50c7a98deeadc9ca97d5529be37ad7da6b5728839d221d7694403e025c6df9dfdc7288a90525
6
+ metadata.gz: f80a247c806c267b4ed8c8f9daff5b5669fc38c8a3377345b151ab29612a7e6e25b46905c66520f73c1f05fe6afaba17fabd50a0e0f59542e7c1482f213200ca
7
+ data.tar.gz: 5da664fc355d08af2b5bfb1dbd761262d743c6d387ddbe4e1bdbaf457847705d24eec78c56e45757cd89809fee3dac034ae2b91983f2d4c04425f77c5eeec11a
data/README.md CHANGED
@@ -6,26 +6,7 @@ Add [Fly.io](https://fly.io) support to [Rails](https://rubyonrails.org/).
6
6
 
7
7
  <u>pre-alpha</u>.
8
8
 
9
- In fact, the gem has not been yet been pushed to the rubygems
10
- repository as while creating a github repository is an easily reversible act,
11
- wiping all memory of a mis-named gem is considerably more challenging.
12
-
13
- It currently is possible to build the gem files, install the prerequisite
14
- `ruby-fly` gem, and then install the platform specific gem using `--local`.
15
- You can also add the gem to your Gemfile using the `path:` argument to the
16
- `gem` statement/method call. Of course, all of this will be taken care of
17
- for you once this gem has been published.
18
-
19
- ## Quickstart/summary
20
-
21
- ```sh
22
- rails new demo
23
- cd demo
24
- bundle add fly.io-rails
25
- flyctl auth login
26
- bin/rails fly:launch
27
- bin/rails deploy
28
- ```
9
+ For usage instructions, see [Fly.io Rails Advanced Guide: Terraform](https://fly.io/docs/rails/advanced-guides/terraform/).
29
10
 
30
11
  ## Build instructions
31
12
 
@@ -38,152 +19,3 @@ every supported platform as well as an additional gem that doesn't
38
19
  include a binary.
39
20
 
40
21
  To download new binaries, run `rake clobber` then `rake package` agein.
41
-
42
- ## Description
43
-
44
- - `bundle add fly.io-rails`
45
-
46
- This will install `ruby-fly` middleware, add new Rails tasks, extend the
47
- channel and job generatotors, and platform binaries for
48
- [flyctl](https://github.com/superfly/flyctl#readme). I've followed the
49
- trail blazed by [nokogiri](https://nokogiri.org/) and others to make
50
- platform binaries happen.
51
-
52
- - `bin/rails fly:launch`
53
-
54
- This won't be a mere front enty to [`flyctl
55
- launch`](https://fly.io/docs/flyctl/launch/). It will use
56
- [`thor`](https://github.com/rails/thor#thor) to rewrite the configuration
57
- for the `production` environment as well as tweak the `Dockerfile` and
58
- `fly.toml` based on your application - in particular the configuration for
59
- ActiveRecord, ActiveJob, and ActionCable. The people at Fly will know what
60
- databases and message queing systems work best on their platform and should
61
- make it easy to do the right thing.
62
-
63
- As an example, the default production database for a new application in
64
- Rails is sqlite3. Either this will need to be replaced by Postgre in
65
- the Rails configuration *or* a volume will need to be defined, mounted,
66
- and the Rails configuration modified to point to the new mount point.
67
-
68
- This gem should pick one of those paths as the default, and provide an option to chose other paths.
69
-
70
- - `bin/rails generate channel`, `bin/rails generate job`, etc.
71
-
72
- Rails applications are generally not invented fully formed. They evolve and
73
- add features. I don't want people to think about having to configure rails
74
- AND configure fly when then add features. Generators that developers
75
- already use today should be able to update both the Rails and Fly
76
- configurations in a consistent manner.
77
-
78
- For now, both the Rails tasks and generators don't actually modify the Rails
79
- configuratio to support Fly, instead they merely output the string `Configuring
80
- fly...`. Let your imagination run wild.
81
-
82
- ## Motivation
83
-
84
- Oversimplifying and exagerating to make a point, `flyctl launch` generates an
85
- initial fly configuration based on the state of the application at launch time
86
- but leaves configuring your Rails application up to you. The initial Fly
87
- configuration may need to be tweaked, and both the fly and Rails configurations
88
- will need to be maintained as the application evolves.
89
-
90
- From a Rails developer perspective, this makes fly an additional framework that
91
- must be learned and attended to.
92
-
93
- This can all be changed with a single `bundle add` command. Everything from
94
- new `rails` tasks to extending the behavior of existing generators to making
95
- changes to configuration to monkeypatching Rails internals itself are on the
96
- table.
97
-
98
- A few sublte but important mindset changes are necessary to pull this off:
99
-
100
- * Instead of "we support every (or perhaps even only 'most') Rails
101
- configuration" the mindset we should strive for is "we provide a default
102
- production configuration that works for most, and provide options to add or
103
- replace components as desired".
104
-
105
- We should be bold and daring. We should chose a default web server, a
106
- default database, a default active job queue adapter, a default action
107
- cable subscription adapter, etc., etc., etc.
108
-
109
- Over time, this should encompas everything needed for monitoring and
110
- deployment. Requirements for things like log file management should be
111
- anticipated and accounted for.
112
-
113
- * Any configuration artifact that is generated and needs to be checked into
114
- the application's source control repository needs to be [beautiful
115
- code](https://rubyonrails.org/doctrine#beautiful-code). If you look at
116
- configuration files provided by either `rails new` or by rails generators,
117
- they have comments. They don't configure things that don't apply to you.
118
-
119
- A concrete example: Rails 7 applications default to import maps. A
120
- `Dockerfile` generated for such an app should not contain code that deals
121
- with `yarn` or `node_modules`.
122
-
123
- And as a closing remark - to be fair adding Rails support to Fly and adding Fly
124
- support to Rails are more complementary than competing efforts.
125
-
126
- ## Future
127
-
128
- > Que sera, sera
129
-
130
- -- Doris Day
131
-
132
- I don't know what the future is going to hold, but some guesses:
133
-
134
- * It is plausible that a code base written in Ruby and with access to
135
- libraries like [Thos](http://whatisthor.com/) and focused exclusively on
136
- Rails may attract more contributions from the Rails community than a
137
- codebase written in Go and targetting many disparate frameworks.
138
- * Flyctl launch can continue to provide a basic Fly configuration for Rails,
139
- but if this effort is successful these configurations would largely be
140
- replaced by more tailors configurations that are updated by generators and
141
- rake tasks as the application evolves.
142
- * Not every flyctl command needs to have a Rails wrapper -- only the common
143
- task do. It is quiet OK for developers to deal directly with fly when it
144
- makes sence to do so. But those flyctl command that are wrapped may need
145
- options added that enable them to be run without the need to prompt the
146
- user.
147
- * This gem could be built and published alongside the flyctl executables, and
148
- `flyctl version update` could detect whether or not it was installed as a
149
- gem and react appropriately.
150
-
151
- ## Call to Action
152
-
153
- > good ideas and bad code build communities, the other three combinations do
154
- > not
155
-
156
- -- Stefano Mazzocchi
157
-
158
- > the best way to get the right answer on the internet is not to ask a
159
- > question; it's to post the wrong answer.
160
-
161
- -- Ward Cunningham
162
-
163
- I don't presume that any specific line of code in this initial implementation
164
- will last the test of time. Heck, I'm not even confident enough in the
165
- proposed name to register the gem, though the name does feel *rails-like* -
166
- just take a look at any Rails `Gemfile` to see.
167
-
168
- My hope is that there is enough scaffolding here to not only make clear what
169
- the possibilities are, but also enough structure so that it is fairly obvious
170
- where new logic should go, together there is not only enough promise and
171
- structure to attract a community.
172
-
173
- Some starter ideas:
174
-
175
- * Can we make `bin/rails deploy` smart enough to invoke `flyctl auth login` if
176
- you are not logged in and `bin/rails fly:launch` if the application had not
177
- previously been launched, with the goal of reducing the number of commands a
178
- user has to issue to get started. Also I'm impressed by the way auth login
179
- launches a browser, can we do the same for fly launch?
180
-
181
- * While a number of application configuration changes are made through
182
- generators, not all are. For example, upgrading the version of Ruby to be
183
- used. Perhaps we could create tasks for some of these, but likely it will
184
- be worthwhile to create a `fly:reconfig` task.
185
-
186
- Finally, once this repository is in the superfly github organization and
187
- a gem has been published, this README should be rewritten from focus on
188
- raison d'être and to a focus on what value it would bring to those that
189
- install and use it.
Binary file
@@ -0,0 +1,36 @@
1
+ require 'open3'
2
+
3
+ module FlyIoRails
4
+ module Utils
5
+
6
+ def tee cmd
7
+ say_status :run, cmd
8
+ FlyIoRails::Utils.tee cmd
9
+ end
10
+
11
+ def self.tee cmd
12
+ data = {:out => [], :err => []}
13
+
14
+ Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
15
+ { out: stdout, err: stderr }.each do |key, stream|
16
+ Thread.new do
17
+ until (raw_line = stream.gets).nil? do
18
+ data[key].push raw_line
19
+
20
+ if key == :out
21
+ STDOUT.print raw_line
22
+ else
23
+ STDERR.print raw_line
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ thread.join
30
+ end
31
+
32
+ [data[:out].join, data[:err].join]
33
+ end
34
+
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Fly_io
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/fly.io-rails.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rails'
2
2
  require 'fly.io-rails/generators'
3
+ require 'fly.io-rails/utils'
3
4
 
4
5
  class FlyIoRailtie < Rails::Railtie
5
6
  rake_tasks do
@@ -0,0 +1,114 @@
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
23
+
24
+ ARG BUNDLER_VERSION=<%= @bundler_version %>
25
+
26
+ ARG RAILS_ENV=production
27
+ ENV RAILS_ENV=${RAILS_ENV}
28
+
29
+ ENV RAILS_SERVE_STATIC_FILES true
30
+ ENV RAILS_LOG_TO_STDOUT true
31
+
32
+ ARG BUNDLE_WITHOUT=development:test
33
+ ARG BUNDLE_PATH=vendor/bundle
34
+ ENV BUNDLE_PATH ${BUNDLE_PATH}
35
+ ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}
36
+
37
+ RUN mkdir /app
38
+ WORKDIR /app
39
+ RUN mkdir -p tmp/pids
40
+
41
+ #######################################################################
42
+
43
+ # install packages only needed at build time
44
+
45
+ FROM base as build_deps
46
+
47
+ ARG BUILD_PACKAGES="git build-essential libpq-dev wget vim curl gzip xz-utils libsqlite3-dev"
48
+ ENV BUILD_PACKAGES ${BUILD_PACKAGES}
49
+
50
+ RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
51
+ --mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
52
+ apt-get update -qq && \
53
+ apt-get install --no-install-recommends -y ${BUILD_PACKAGES} \
54
+ && rm -rf /var/lib/apt/lists /var/cache/apt/archives
55
+
56
+ #######################################################################
57
+
58
+ # install gems
59
+
60
+ FROM build_deps as gems
61
+
62
+ RUN gem update --system --no-document && \
63
+ gem install -N bundler -v ${BUNDLER_VERSION}
64
+
65
+ COPY Gemfile* ./
66
+ RUN bundle install && rm -rf vendor/bundle/ruby/*/cache
67
+
68
+ #######################################################################
69
+
70
+ # install deployment packages
71
+
72
+ FROM base
73
+
74
+ ARG DEPLOY_PACKAGES="postgresql-client file vim curl gzip libsqlite3-0"
75
+ ENV DEPLOY_PACKAGES=${DEPLOY_PACKAGES}
76
+
77
+ RUN --mount=type=cache,id=prod-apt-cache,sharing=locked,target=/var/cache/apt \
78
+ --mount=type=cache,id=prod-apt-lib,sharing=locked,target=/var/lib/apt \
79
+ apt-get update -qq && \
80
+ apt-get install --no-install-recommends -y \
81
+ ${DEPLOY_PACKAGES} \
82
+ && rm -rf /var/lib/apt/lists /var/cache/apt/archives
83
+
84
+ # copy installed gems
85
+ COPY --from=gems /app /app
86
+ COPY --from=gems /usr/lib/fullstaq-ruby/versions /usr/lib/fullstaq-ruby/versions
87
+ COPY --from=gems /usr/local/bundle /usr/local/bundle
88
+
89
+ #######################################################################
90
+
91
+ # Deploy your application
92
+ COPY . .
93
+
94
+ # Adjust binstubs to run on Linux and set current working directory
95
+ RUN chmod +x /app/bin/* && \
96
+ sed -i 's/ruby.exe/ruby/' /app/bin/* && \
97
+ sed -i '/^#!/aDir.chdir File.expand_path("..", __dir__)' /app/bin/*
98
+
99
+ # The following enable assets to precompile on the build server. Adjust
100
+ # as necessary. If no combination works for you, see:
101
+ # https://fly.io/docs/rails/getting-started/existing/#access-to-environment-variables-at-build-time
102
+ ENV SECRET_KEY_BASE 1
103
+ # ENV AWS_ACCESS_KEY_ID=1
104
+ # ENV AWS_SECRET_ACCESS_KEY=1
105
+
106
+ # Run build task defined in lib/tasks/fly.rake
107
+ ARG BUILD_COMMAND="bin/rails fly:build"
108
+ RUN ${BUILD_COMMAND}
109
+
110
+ # Default server start instructions. Generally Overridden by fly.toml.
111
+ ENV PORT 8080
112
+ ARG SERVER_COMMAND="bin/rails fly:server"
113
+ ENV SERVER_COMMAND ${SERVER_COMMAND}
114
+ CMD ${SERVER_COMMAND}
@@ -0,0 +1,16 @@
1
+ .git
2
+ tmp
3
+ !tmp/pids
4
+ log
5
+ public/assets
6
+ public/packs
7
+ .bundle
8
+
9
+ db/*.sqlite3
10
+ db/*.sqlite3-*
11
+
12
+ storage
13
+ config/master.key
14
+ config/credentials/*.key
15
+
16
+ node_modules
@@ -0,0 +1,23 @@
1
+ # commands used to deploy a Rails application
2
+ namespace :fly do
3
+ # BUILD step:
4
+ # - changes to the filesystem made here DO get deployed
5
+ # - NO access to secrets, volumes, databases
6
+ # - Failures here prevent deployment
7
+ task :build => 'assets:precompile'
8
+
9
+ # RELEASE step:
10
+ # - changes to the filesystem made here are DISCARDED
11
+ # - full access to secrets, databases
12
+ # - failures here prevent deployment
13
+ task :release => 'db:migrate'
14
+
15
+ # SERVER step:
16
+ # - changes to the filesystem made here are deployed
17
+ # - full access to secrets, databases
18
+ # - failures here result in VM being stated, shutdown, and rolled back
19
+ # to last successful deploy (if any).
20
+ task :server do
21
+ sh 'bin/rails server'
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ terraform {
2
+ required_providers {
3
+ fly = {
4
+ source = "fly-apps/fly"
5
+ version = "0.0.10"
6
+ }
7
+ }
8
+ }
9
+
10
+ resource "fly_ip" "<%= @appName %>Ipv4" {
11
+ app = <%= @app.inspect %>
12
+ type = "v4"
13
+ }
14
+
15
+ resource "fly_ip" "<%= @appName %>Ipv6" {
16
+ app = <%= @app.inspect %>
17
+ type = "v6"
18
+ }
19
+
20
+ resource "fly_machine" "<%= @appName %>Machine" {
21
+ for_each = toset(<%= @regions.inspect %>)
22
+ app = <%= @app.inspect %>
23
+ region = each.value
24
+ name = "<%= @app %>-${each.value}"
25
+ image = "quay.io/evl.ms/fullstaq-ruby:<%= @ruby_version %>-jemalloc-slim"
26
+ services = [
27
+ {
28
+ ports = [
29
+ {
30
+ port = 443
31
+ handlers = ["tls", "http"]
32
+ },
33
+ {
34
+ port = 80
35
+ handlers = ["http"]
36
+ }
37
+ ]
38
+ "protocol" : "tcp",
39
+ "internal_port" : 8080
40
+ },
41
+ ]
42
+ }
@@ -0,0 +1,44 @@
1
+ require 'open3'
2
+
3
+ class TerraformGenerator < Rails::Generators::Base
4
+ include FlyIoRails::Utils
5
+
6
+ class_option :name, type: :string, required: false
7
+ class_option :org, type: :string, default: 'personal'
8
+ class_option :region, type: :array, repeatable: true, default: []
9
+
10
+ def terraform
11
+ cmd = if options[:name]
12
+ "flyctl apps create #{options[:name].inspect} --org #{options[:org].inspect}"
13
+ else
14
+ "flyctl apps create --generate-name --org #{options[:org].inspect}"
15
+ end
16
+
17
+ output, error = tee cmd
18
+ exit 1 unless output =~ /^New app created: /
19
+
20
+ @app = output.split.last
21
+ create_file 'fly.toml', "app = #{@app.inspect}\n"
22
+
23
+ if options[:region].empty?
24
+ @regions = JSON.parse(`flyctl regions list --json`)['Regions'].
25
+ map {|region| region['Code']}
26
+ else
27
+ @regions = options[:regions].flatten
28
+ end
29
+
30
+ source_paths.push File.expand_path('./templates', __dir__)
31
+
32
+ @ruby_version = RUBY_VERSION
33
+ @bundler_version = Bundler::VERSION
34
+ @appName = @app.gsub('-', '_').camelcase(:lower)
35
+ template 'Dockerfile.erb', 'Dockerfile'
36
+ template 'dockerignore.erb', '.dockerignore'
37
+ template 'main.tf.erb', 'main.tf'
38
+ template 'fly.rake.erb', 'lib/tasks/fly.rake'
39
+
40
+ ENV['FLY_API_TOKEN'] = `flyctl auth token`
41
+ tee 'terraform init'
42
+ end
43
+ end
44
+
data/lib/tasks/fly.rake CHANGED
@@ -1,26 +1,20 @@
1
1
  namespace :fly do
2
- desc 'Launch a new app'
3
- task :launch do
4
- sh 'flyctl launch'
5
-
6
- # note: Rake task argument syntax isn't particularly user friendy,
7
- # but Rake is a Ruby program and we have full access to ARGV,
8
- # meaning we can do our own thing with OptionParser or whatever.
9
- # The only real caveat if we do so is that we need to exit the
10
- # program at the completion of the task lest Rails tries to interpet
11
- # the next argument as the name of the next task to execute.
12
-
13
- Rake.rake_output_message 'Customizing Dockerfile...'
14
-
15
- exit
16
- end
17
-
18
2
  desc 'Deploy fly application'
19
3
  task :deploy do
20
- sh 'flyctl deploy'
4
+ out, err = FlyIoRails::Utils.tee 'fly deploy --build-only --push'
5
+ image = err[/image:\s+(.*)/, 1]
6
+
7
+ if image
8
+ tf = IO.read('main.tf')
9
+ tf[/^\s*image\s*=\s*"(.*?)"/, 1] = image
10
+ IO.write 'main.tf', tf
11
+
12
+ ENV['FLY_API_TOKEN'] = `flyctl auth token`
13
+ FlyIoRails::Utils.tee 'terraform apply -auto-approve'
14
+ end
21
15
  end
22
16
  end
23
17
 
24
18
  # Alias, for convenience
25
19
  desc 'Deploy fly application'
26
- task deploy: 'fly:deploy'
20
+ task deploy: 'fly:deploy'
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.0.1
4
+ version: 0.0.3
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-13 00:00:00.000000000 Z
11
+ date: 2022-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fly-ruby
@@ -39,7 +39,13 @@ files:
39
39
  - lib/fly.io-rails.rb
40
40
  - lib/fly.io-rails/generators.rb
41
41
  - lib/fly.io-rails/platforms.rb
42
+ - lib/fly.io-rails/utils.rb
42
43
  - lib/fly.io-rails/version.rb
44
+ - lib/generators/templates/Dockerfile.erb
45
+ - lib/generators/templates/dockerignore.erb
46
+ - lib/generators/templates/fly.rake.erb
47
+ - lib/generators/templates/main.tf.erb
48
+ - lib/generators/terraform_generator.rb
43
49
  - lib/tasks/fly.rake
44
50
  homepage: https://github.com/rubys/fly-io.rails
45
51
  licenses:
@@ -61,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
67
  - !ruby/object:Gem::Version
62
68
  version: '0'
63
69
  requirements: []
64
- rubygems_version: 3.3.22
70
+ rubygems_version: 3.3.11
65
71
  signing_key:
66
72
  specification_version: 4
67
73
  summary: Rails support for Fly-io