fly.io-rails 0.1.0-arm64-darwin → 0.1.1-arm64-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: b89a8e920dd7cee9cea81a48e67f8b8381a9b6b5e16d9c46a946afe1f43f00fc
4
- data.tar.gz: 242c56aba9b12d77fc15054d4c7ab769a4c4680aaf1fb85838d05c51fa186b56
3
+ metadata.gz: 1fd535a7501f08c7e1314a14b6a2875b334182fca732dd994df20ac2979e0bf9
4
+ data.tar.gz: e9667f63230a373fd8feb431ca7a551e74a6b17a118418f2f13f37da80086964
5
5
  SHA512:
6
- metadata.gz: 344e69d07ca770e1c925533a16a483d2449c10627a25cfe20b0ca29e329be93a5fb22d3506cd926f488e046bbdaac52f197756b79a54953e93f9e6190f2e0dfa
7
- data.tar.gz: 662d1dde14456597587ed990c1b2718f0112ea0f9b16a1405ee825c4d36d5ddca4f4344d3fe45c24cd1bc3a5309b053b416b1f2b8d4e561179d02e6090745071
6
+ metadata.gz: dafa2169129603c071bcf199f215074e66ae166c4fd7cf78feb081276d14f1460c3fb3f81186a0aa1cf0708fdc47ca5e62dd0ce4fe742002369abfe4eb043f34
7
+ data.tar.gz: 75d78bab12736653419348eca68f94fdcd4bd20532e6310ade12fc96f8a78065d973ebfcf7fd276972da28b44701251c31421421928c64a5d716fce70521301c
Binary file
@@ -85,7 +85,7 @@ module Fly
85
85
  def create_volume(app, region, size)
86
86
  volume = "#{app.gsub('-', '_')}_volume"
87
87
  volumes = JSON.parse(`flyctl volumes list --json`).
88
- map {|volume| volume[' Name']}
88
+ map {|volume| volume['Name']}
89
89
 
90
90
  unless volumes.include? volume
91
91
  cmd = "flyctl volumes create #{volume} --app #{app} --region #{region} --size #{size}"
@@ -97,14 +97,31 @@ module Fly
97
97
  end
98
98
 
99
99
  def create_postgres(app, org, region, vm_size, volume_size, cluster_size)
100
- cmd = "fly postgres create --name #{app}-db --org #{org} --region #{region} --vm-size #{vm_size} --volume-size #{volume_size} --initial-cluster-size #{cluster_size}"
100
+ cmd = "flyctl postgres create --name #{app}-db --org #{org} --region #{region} --vm-size #{vm_size} --volume-size #{volume_size} --initial-cluster-size #{cluster_size}"
101
101
  say_status :run, cmd
102
102
  output = FlyIoRails::Utils.tee(cmd)
103
103
  output[%r{postgres://\S+}]
104
104
  end
105
105
 
106
+ def create_redis(app, org, region, eviction)
107
+ # see if redis is already defined
108
+ name = `flyctl redis list`.lines[1..-2].map(&:split).
109
+ find {|tokens| tokens[1] == org}&.first
110
+
111
+ if name
112
+ secret = `flyctl redis status #{name}`[%r{redis://\S+}]
113
+ return secret if secret
114
+ end
115
+
116
+ # create a new redis
117
+ cmd = "flyctl redis create --org #{org} --name #{app}-redis --region #{region} --no-replicas #{eviction} --plan Free"
118
+ say_status :run, cmd
119
+ output = FlyIoRails::Utils.tee(cmd)
120
+ output[%r{redis://\S+}]
121
+ end
122
+
106
123
  def release(app, config)
107
- start = Fly::Machines.create_start_machine(app, config: config)
124
+ start = Fly::Machines.create_and_start_machine(app, config: config)
108
125
  machine = start[:id]
109
126
 
110
127
  if !machine
@@ -113,15 +130,27 @@ module Fly
113
130
  exit 1
114
131
  end
115
132
 
133
+ status = Fly::Machines.wait_for_machine app, machine,
134
+ timeout: 60, state: 'started'
135
+
116
136
  # wait for release to copmlete
117
137
  status = nil
118
138
  5.times do
119
- status = Fly::Machines.wait_for_machine app, machine,
120
- timeout: 60, status: 'stopped'
121
- return machine if status[:ok]
139
+ status = Fly::Machines.wait_for_machine app, machine,
140
+ timeout: 60, state: 'stopped'
141
+ return machine if status[:ok]
122
142
  end
123
143
 
124
- STDERR.puts status.to_json
144
+ # wait for release to copmlete
145
+ event = nil
146
+ 90.times do
147
+ sleep 1
148
+ status = Fly::Machines.get_a_machine app, machine
149
+ event = status[:events]&.first
150
+ return machine if event && event[:type] == 'exit'
151
+ end
152
+
153
+ STDERR.puts event.to_json
125
154
  exit 1
126
155
  end
127
156
 
@@ -130,7 +159,7 @@ module Fly
130
159
  map {|region| region['Code']} rescue []
131
160
  region = regions.first || 'iad'
132
161
 
133
- secrets = JSON.parse(`fly secrets list --json`).
162
+ secrets = JSON.parse(`flyctl secrets list --json`).
134
163
  map {|secret| secret["Name"]}
135
164
 
136
165
  config = {
@@ -173,9 +202,33 @@ module Fly
173
202
  elsif database == 'postgresql' and not secrets.include? 'DATABASE_URL'
174
203
  secret = create_postgres(app, @org, region, 'shared-cpu-1x', 1, 1)
175
204
 
176
- cmd = "fly secrets set DATABASE_URL=#{secret}"
177
- say_status :run, cmd
178
- system cmd
205
+ if secret
206
+ cmd = "flyctl secrets set --stage DATABASE_URL=#{secret}"
207
+ say_status :run, cmd
208
+ system cmd
209
+ end
210
+ end
211
+
212
+ # Enable redis if mentioned as a cache provider or a cable provider.
213
+ # Set eviction policy to true if a cache provider, else false.
214
+ eviction = nil
215
+
216
+ if (YAML.load_file('config/cable.yml').dig('production', 'adapter') rescue false)
217
+ eviction = '--disable-eviction'
218
+ end
219
+
220
+ if (IO.read('config/environments/production.rb') =~ /redis/i rescue false)
221
+ eviction = '--enable-eviction'
222
+ end
223
+
224
+ if eviction and not secrets.include? 'REDIS_URL'
225
+ secret = create_redis(app, @org, region, eviction)
226
+
227
+ if secret
228
+ cmd = "flyctl secrets set --stage REDIS_URL=#{secret}"
229
+ say_status :run, cmd
230
+ system cmd
231
+ end
179
232
  end
180
233
 
181
234
  # build config for release machine, overriding server command
@@ -192,9 +245,15 @@ module Fly
192
245
  # start proxy, if necessary
193
246
  endpoint = Fly::Machines::fly_api_hostname!
194
247
 
248
+ # stop previous instances
249
+ JSON.parse(`fly machines list --json`).each do |list|
250
+ next if list['id'] == machine
251
+ system "fly machines remove --force #{list['id']}"
252
+ end
253
+
195
254
  # start app
196
255
  say_status :fly, "start #{app}"
197
- start = Fly::Machines.create_start_machine(app, config: config)
256
+ start = Fly::Machines.create_and_start_machine(app, config: config)
198
257
  machine = start[:id]
199
258
 
200
259
  if !machine
@@ -251,7 +310,7 @@ module Fly
251
310
 
252
311
  # start release machine
253
312
  STDERR.puts "--> #{config[:env]['SERVER_COMMAND']}"
254
- start = Fly::Machines.create_start_machine(app, config: config)
313
+ start = Fly::Machines.create_and_start_machine(app, config: config)
255
314
  machine = start[:id]
256
315
 
257
316
  if !machine
@@ -80,7 +80,7 @@ module Fly
80
80
  get "/v1/apps/#{app}"
81
81
  end
82
82
 
83
- # create_start_machine 'user-functions', name: 'quirky_machine', config: {
83
+ # create_and_start_machine 'user-functions', name: 'quirky_machine', config: {
84
84
  # image: 'flyio/fastify-functions',
85
85
  # env: {'APP_ENV' => 'production'},
86
86
  # services: [
@@ -94,7 +94,7 @@ module Fly
94
94
  # }
95
95
  # ]
96
96
  # }
97
- def self.create_start_machine app, options
97
+ def self.create_and_start_machine app, options
98
98
  post "/v1/apps/#{app}/machines", options
99
99
  end
100
100
 
@@ -1,3 +1,3 @@
1
1
  module Fly_io
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
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.0
4
+ version: 0.1.1
5
5
  platform: arm64-darwin
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-23 00:00:00.000000000 Z
11
+ date: 2022-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fly-ruby