|
1 | | -require "fly.io-rails/machines" |
2 | | - |
3 | 1 | class HostingServiceAdapters::Fly < HostingServiceAdapters::Base |
| 2 | + SMALL_GUEST = { "cpu_kind" => "shared", "cpus" => 1, "memory_mb" => 512 } |
| 3 | + LARGE_GUEST = { "cpu_kind" => "performance", "cpus" => 1, "memory_mb" => 2048 } |
| 4 | + |
| 5 | + class Machine < HostingServiceAdapters::Instance |
| 6 | + attr_reader :id, :state, :config, :region, :instance_id, :name |
| 7 | + |
| 8 | + def initialize(id:, instance_id:, state:, config:, region:, name:, **args) |
| 9 | + super(**args) |
| 10 | + @id = id |
| 11 | + @instance_id = instance_id |
| 12 | + @state = state |
| 13 | + @config = config |
| 14 | + @region = region |
| 15 | + @name = name |
| 16 | + end |
| 17 | + end |
| 18 | + |
4 | 19 | def applicable? |
5 | 20 | ENV["FLY_APP_NAME"].present? && ENV["FLY_API_TOKEN"].present? |
6 | 21 | end |
7 | 22 |
|
8 | | - def fetch_instance_count |
9 | | - web_machines.count |
| 23 | + def instances_state |
| 24 | + @instances_state ||= |
| 25 | + HostingServiceAdapters::InstancesState.new( |
| 26 | + instances: current_machines.map { |machine| instance_for_machine(machine) } |
| 27 | + ) |
| 28 | + end |
| 29 | + |
| 30 | + def invalidate_state |
| 31 | + @current_machines = nil |
| 32 | + @instances_state = nil |
| 33 | + end |
| 34 | + |
| 35 | + def force_refresh_state |
| 36 | + invalidate_state |
| 37 | + instances_state |
10 | 38 | end |
11 | 39 |
|
12 | | - def update_instance_count(instance_count) |
13 | | - raise "Instance count must be at least 1" if instance_count < 1 |
| 40 | + def update_instance_group(group:, type:, count:) |
| 41 | + current_instances = instances_state.find_instances(group:, type:) |
14 | 42 |
|
15 | | - if instance_count > fetch_instance_count |
16 | | - scale_up(instance_count - fetch_instance_count) |
17 | | - elsif instance_count < fetch_instance_count |
18 | | - scale_down(fetch_instance_count - instance_count) |
| 43 | + if current_instances.size < count |
| 44 | + create_machines(group:, type:, count: count - current_instances.size) |
| 45 | + invalidate_state |
| 46 | + elsif current_instances.size > count |
| 47 | + destroy_machines(current_instances.first(current_instances.size - count)) |
| 48 | + invalidate_state |
19 | 49 | end |
20 | 50 | end |
21 | 51 |
|
22 | 52 | private |
23 | 53 |
|
24 | | - def scale_up(amount) |
25 | | - config = web_machines.first[:config] |
26 | | - created_machines = |
27 | | - Array |
28 | | - .new(amount) |
29 | | - .map do |
30 | | - Fly::Machines.create_and_start_machine( |
31 | | - ENV.fetch("FLY_APP_NAME"), |
32 | | - { config: config, region: web_machines.first[:region] } |
33 | | - ) |
| 54 | + def machines_api_base |
| 55 | + @machines_api_base ||= ENV["FLY_PRIVATE_IP"].present? ? "http://_api.internal:4280" : "https://api.machines.dev" |
| 56 | + end |
| 57 | + |
| 58 | + def machines_api |
| 59 | + @machines_api ||= |
| 60 | + Faraday.new(url: machines_api_base) do |builder| |
| 61 | + builder.request :authorization, "Bearer", -> { ENV.fetch("FLY_API_TOKEN") } |
| 62 | + builder.request :json |
| 63 | + builder.response :json |
| 64 | + builder.response :raise_error |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def current_machines |
| 69 | + @current_machines ||= machines_api.get("/v1/apps/#{ENV.fetch("FLY_APP_NAME")}/machines").body |
| 70 | + end |
| 71 | + |
| 72 | + def instance_for_machine(machine) # rubocop:disable Metrics/MethodLength |
| 73 | + guest = machine.dig("config", "guest") |
| 74 | + process_group = machine.dig("config", "metadata", "fly_process_group") |
| 75 | + |
| 76 | + Machine.new( |
| 77 | + id: machine["id"], |
| 78 | + instance_id: machine["instance_id"], |
| 79 | + name: machine["name"], |
| 80 | + state: machine["state"], |
| 81 | + config: machine["config"], |
| 82 | + region: machine["region"], |
| 83 | + group: |
| 84 | + case process_group |
| 85 | + when "web" |
| 86 | + :web |
| 87 | + when "shoryuken" |
| 88 | + :worker |
| 89 | + else |
| 90 | + :other |
| 91 | + end, |
| 92 | + type: |
| 93 | + case guest |
| 94 | + when SMALL_GUEST |
| 95 | + :small |
| 96 | + when LARGE_GUEST |
| 97 | + :large |
| 98 | + else |
| 99 | + :other |
34 | 100 | end |
35 | | - Rails.logger.info "Created and started Fly machines: #{created_machines.pluck(:name).join(", ")}" |
36 | | - Rails.logger.info "Waiting for all machines to start" |
37 | | - created_machines.each do |machine| |
38 | | - Fly::Machines.wait_for_machine(ENV.fetch("FLY_APP_NAME"), machine[:id], status: "started") |
39 | | - end |
40 | | - created_machines |
| 101 | + ) |
41 | 102 | end |
42 | 103 |
|
43 | | - def scale_down(amount) |
44 | | - to_destroy = web_machines.first(amount) |
45 | | - to_stop = to_destroy.filter { |machine| machine[:state] != "stopped" } |
46 | | - to_stop.each do |machine| |
47 | | - Rails.logger.info "Stopping Fly machine #{machine[:name]}" |
48 | | - Fly::Machines.stop_machine(ENV.fetch("FLY_APP_NAME"), machine[:id]) |
| 104 | + def guest_config_for_type(type) |
| 105 | + case type |
| 106 | + when :small |
| 107 | + SMALL_GUEST |
| 108 | + when :large |
| 109 | + LARGE_GUEST |
49 | 110 | end |
50 | | - unless to_stop.empty? |
51 | | - Rails.logger.info "Waiting for all machines to stop" |
52 | | - to_stop.each do |machine| |
53 | | - Fly::Machines.wait_for_machine(ENV.fetch("FLY_APP_NAME"), machine[:id], status: "stopped") |
54 | | - end |
| 111 | + end |
| 112 | + |
| 113 | + def fly_process_group_for_group(group) |
| 114 | + case group |
| 115 | + when :web |
| 116 | + "web" |
| 117 | + when :worker |
| 118 | + "shoryuken" |
55 | 119 | end |
| 120 | + end |
| 121 | + |
| 122 | + def machine_create_body(group:, type:) |
| 123 | + template_machine = instances_state.find_instances(group:).first |
| 124 | + config_template = |
| 125 | + template_machine.config.merge( |
| 126 | + "guest" => guest_config_for_type(type), |
| 127 | + "metadata" => { |
| 128 | + "fly_process_group" => fly_process_group_for_group(group) |
| 129 | + } |
| 130 | + ) |
| 131 | + { config: config_template, region: template_machine.region } |
| 132 | + end |
| 133 | + |
| 134 | + def create_machines(group:, type:, count:) |
| 135 | + create_body = machine_create_body(group:, type:) |
56 | 136 |
|
57 | | - to_destroy.each do |machine| |
58 | | - Rails.logger.info "Deleting Fly machine #{machine[:name]}" |
59 | | - Fly::Machines.delete_machine(ENV.fetch("FLY_APP_NAME"), machine[:id]) |
| 137 | + Rails.logger.info "Creating #{count} #{type} #{group} machine(s)" |
| 138 | + created_machines = |
| 139 | + Array.new(count) { |_n| machines_api.post("/v1/apps/#{ENV.fetch("FLY_APP_NAME")}/machines", create_body).body } |
| 140 | + Rails.logger.info "Created and started Fly machines: #{created_machines.pluck("name").join(", ")}" |
| 141 | + |
| 142 | + Rails.logger.info "Waiting for all machines to start" |
| 143 | + created_machines.each do |machine| |
| 144 | + machines_api.get("/v1/apps/#{ENV.fetch("FLY_APP_NAME")}/machines/#{machine["id"]}/wait?state=started") |
60 | 145 | end |
61 | 146 |
|
62 | | - to_destroy |
| 147 | + created_machines |
63 | 148 | end |
64 | 149 |
|
65 | | - def web_machines |
66 | | - @web_machines ||= |
67 | | - begin |
68 | | - Fly::Machines.fly_api_hostname! |
69 | | - Fly::Machines |
70 | | - .list_machines(ENV.fetch("FLY_APP_NAME"), nil) |
71 | | - .filter { |machine| machine[:config][:metadata][:fly_process_group] == "web" } |
72 | | - end |
| 150 | + def destroy_machines(instances) |
| 151 | + instances.each do |instance| |
| 152 | + Rails.logger.info "Stopping Fly machine #{instance.name}" |
| 153 | + machines_api.post("/v1/apps/#{ENV.fetch("FLY_APP_NAME")}/machines/#{instance.id}/stop") |
| 154 | + end |
| 155 | + |
| 156 | + Rails.logger.info "Waiting for all machines to stop" |
| 157 | + instances.each do |instance| |
| 158 | + machines_api.get( |
| 159 | + "/v1/apps/#{ENV.fetch("FLY_APP_NAME")}/machines/#{instance.id}/wait?\ |
| 160 | +state=stopped&instance_id=#{instance.instance_id}" |
| 161 | + ) |
| 162 | + |
| 163 | + Rails.logger.info "Destroying Fly machine #{instance.name}" |
| 164 | + machines_api.delete("/v1/apps/#{ENV.fetch("FLY_APP_NAME")}/machines/#{instance.id}") |
| 165 | + end |
73 | 166 | end |
74 | 167 | end |
0 commit comments