fly.io-rails 0.0.1-x86_64-darwin → 0.0.2-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: f6ac0bc3732ed8cdf9c621c0696f26cf856552778e0be48af5d7d68348288a8c
4
- data.tar.gz: c6599e74063af9b1dd618bfc1e9cfb3f7fad56d06cb92c85c60fcee4745d0a12
3
+ metadata.gz: 2db3a8d63c0e20c181fafeb01586c9a2da214ab367290a895eb6fa68368e0385
4
+ data.tar.gz: f7a027b506b11e2676af2a8713f19ebdfa47bb676f930e1b7919ae53211d53d6
5
5
  SHA512:
6
- metadata.gz: 2865d142fb9d3f379109ac48ae86953f47a4314de32fa390fe51b67e85f77a877d85c43017e8847c5346737f38a565f465253f1206fc47f5e4131edc3b91078b
7
- data.tar.gz: 88e26f046ca4447ad03b918de1540128dd0db162de021ed2346f50c7a98deeadc9ca97d5529be37ad7da6b5728839d221d7694403e025c6df9dfdc7288a90525
6
+ metadata.gz: 6bb73a5fedde79eb533d31c53a5c4f78e5cd00feec1ec7d5f35ee1f66977cd285066516221d85f6c195af707d3702f9b9b7803062cbd0d04836943c70f5975a1
7
+ data.tar.gz: 208994a22e9a915b88ad1ec9eb07e0134ee4d5ff87edb9cc014b872c8be0f897e0db6903a730834cde6c011c0b059fa7042f13ff35b4e952ce77a2a83059ed31
@@ -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.2'
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.2
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