fly.io-rails 0.1.0-x86_64-darwin → 0.1.1-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: 279659e9ff2c6d4fa486aba5929c13ae9ddf6fe1865fb6e121bfb6dc19c6aa4d
4
- data.tar.gz: be8189f491a628b30d9b891717d2b47a1a09478a178fbede07e0830e2214d44c
3
+ metadata.gz: 9ecdac6ea9c27372c9f51d70add0f343940a1bfca16c26501e9ceb15fb989ee3
4
+ data.tar.gz: a0c29f6f12b131be05f716e21c9efa0b91427db9cbbda8f8625eaccea12f8b4e
5
5
  SHA512:
6
- metadata.gz: cb7b41e1b72cbf1610a544c619f7767ceebd34abb4704f5a166077faaf6e6ac2735bc3ff14992744422628faebf7044993d15d03515ad2d1c9b3652d24695b28
7
- data.tar.gz: 24fa9990ac50e68459e1f762cf89d398c6891e35d8e5a3b17f1e78341b3f03d69329dde7f7a34e4fdd9ce250eead90296f1e708d3b95e2c6c1787058100e5f24
6
+ metadata.gz: d301a4cb2ca9f9fe85962feb5785abbfdcc91226bb5a2d4049bae90b0744fb5e12b12512092800c302bd70d2822a3d6dd9f6f145f75c34166a2bee8163ef6d72
7
+ data.tar.gz: 879f6e285cf4180d72f174926b368d20967e46f1d32154398cba2e1a245a444e05badfc6d7c4ee2cae42f32392968517b389b20542f97f2c8b80a7510677e07a
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: 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-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