Class: Nvoi::External::Cloud::Aws

Inherits:
Base
  • Object
show all
Defined in:
lib/nvoi/external/cloud/aws.rb

Overview

AWS provider implements the compute provider interface for AWS EC2

Instance Method Summary collapse

Constructor Details

#initialize(access_key_id, secret_access_key, region) ⇒ Aws

Returns a new instance of Aws.



11
12
13
14
15
16
17
# File 'lib/nvoi/external/cloud/aws.rb', line 11

def initialize(access_key_id, secret_access_key, region)
  @region = region || "us-east-1"
  @client = ::Aws::EC2::Client.new(
    region: @region,
    credentials: ::Aws::Credentials.new(access_key_id, secret_access_key)
  )
end

Instance Method Details

#attach_volume(volume_id, server_id) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/nvoi/external/cloud/aws.rb', line 298

def attach_volume(volume_id, server_id)
  @client.attach_volume(
    volume_id:,
    instance_id: server_id,
    device: "/dev/xvdf"
  )
end

#create_server(opts) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/nvoi/external/cloud/aws.rb', line 197

def create_server(opts)
  ami_id = get_ubuntu_ami

  input = {
    image_id: ami_id,
    instance_type: opts.type,
    min_count: 1,
    max_count: 1,
    user_data: opts.user_data ? Base64.encode64(opts.user_data) : nil,
    tag_specifications: [{
      resource_type: "instance",
      tags: [{ key: "Name", value: opts.name }]
    }]
  }

  # Add network configuration if provided
  unless opts.network_id.blank?
    subnets = @client.describe_subnets(
      filters: [{ name: "vpc-id", values: [opts.network_id] }]
    )
    input[:subnet_id] = subnets.subnets[0].subnet_id unless subnets.subnets.empty?
  end

  # Add security group if provided
  unless opts.firewall_id.blank?
    input[:security_group_ids] = [opts.firewall_id]
  end

  result = @client.run_instances(input)
  raise Errors::ServerCreationError, "no instance created" if result.instances.empty?

  instance_to_server(result.instances[0])
end

#create_volume(opts) ⇒ Object

Volume operations



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/nvoi/external/cloud/aws.rb', line 252

def create_volume(opts)
  resp = @client.describe_instances(instance_ids: [opts.server_id])
  raise Errors::VolumeError, "instance not found: #{opts.server_id}" if resp.reservations.empty?

  instance = resp.reservations[0].instances[0]
  az = instance.placement.availability_zone

  create_resp = @client.create_volume(
    availability_zone: az,
    size: opts.size,
    volume_type: "gp3",
    tag_specifications: [{
      resource_type: "volume",
      tags: [{ key: "Name", value: opts.name }]
    }]
  )

  Types::Volume::Record.new(
    id: create_resp.volume_id,
    name: opts.name,
    size: create_resp.size,
    location: create_resp.availability_zone,
    status: create_resp.state
  )
end

#delete_firewall(id) ⇒ Object



158
159
160
# File 'lib/nvoi/external/cloud/aws.rb', line 158

def delete_firewall(id)
  @client.delete_security_group(group_id: id)
end

#delete_network(id) ⇒ Object



110
111
112
# File 'lib/nvoi/external/cloud/aws.rb', line 110

def delete_network(id)
  @client.delete_vpc(vpc_id: id)
end

#delete_server(id) ⇒ Object



246
247
248
# File 'lib/nvoi/external/cloud/aws.rb', line 246

def delete_server(id)
  @client.terminate_instances(instance_ids: [id])
end

#delete_volume(id) ⇒ Object



294
295
296
# File 'lib/nvoi/external/cloud/aws.rb', line 294

def delete_volume(id)
  @client.delete_volume(volume_id: id)
end

#detach_volume(volume_id) ⇒ Object



306
307
308
# File 'lib/nvoi/external/cloud/aws.rb', line 306

def detach_volume(volume_id)
  @client.detach_volume(volume_id:)
end

#find_or_create_firewall(name) ⇒ Object

Firewall operations



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nvoi/external/cloud/aws.rb', line 116

def find_or_create_firewall(name)
  sg = find_security_group_by_name(name)
  if sg
    return Types::Firewall::Record.new(id: sg.group_id, name:)
  end

  # Get default VPC
  vpcs = @client.describe_vpcs(filters: [{ name: "isDefault", values: ["true"] }])
  raise Errors::NetworkError, "no default VPC found" if vpcs.vpcs.empty?

  # Create security group
  create_resp = @client.create_security_group(
    group_name: name,
    description: "Managed by nvoi",
    vpc_id: vpcs.vpcs[0].vpc_id,
    tag_specifications: [{
      resource_type: "security-group",
      tags: [{ key: "Name", value: name }]
    }]
  )

  # Add SSH ingress rule
  @client.authorize_security_group_ingress(
    group_id: create_resp.group_id,
    ip_permissions: [{
      ip_protocol: "tcp",
      from_port: 22,
      to_port: 22,
      ip_ranges: [{ cidr_ip: "0.0.0.0/0" }]
    }]
  )

  Types::Firewall::Record.new(id: create_resp.group_id, name:)
end

#find_or_create_network(name) ⇒ Object

Network operations



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nvoi/external/cloud/aws.rb', line 21

def find_or_create_network(name)
  vpc = find_vpc_by_name(name)
  if vpc
    return Types::Network::Record.new(
      id: vpc.vpc_id,
      name:,
      ip_range: vpc.cidr_block
    )
  end

  # Create new VPC
  create_resp = @client.create_vpc(
    cidr_block: "10.0.0.0/16",
    tag_specifications: [{
      resource_type: "vpc",
      tags: [{ key: "Name", value: name }]
    }]
  )
  vpc_id = create_resp.vpc.vpc_id

  # Enable DNS hostnames
  @client.modify_vpc_attribute(
    vpc_id:,
    enable_dns_hostnames: { value: true }
  )

  # Create subnet
  subnet_resp = @client.create_subnet(
    vpc_id:,
    cidr_block: "10.0.1.0/24",
    tag_specifications: [{
      resource_type: "subnet",
      tags: [{ key: "Name", value: "#{name}-subnet" }]
    }]
  )

  # Create internet gateway
  igw_resp = @client.create_internet_gateway(
    tag_specifications: [{
      resource_type: "internet-gateway",
      tags: [{ key: "Name", value: "#{name}-igw" }]
    }]
  )
  igw_id = igw_resp.internet_gateway.internet_gateway_id

  # Attach internet gateway to VPC
  @client.attach_internet_gateway(vpc_id:, internet_gateway_id: igw_id)

  # Create route table
  rtb_resp = @client.create_route_table(
    vpc_id:,
    tag_specifications: [{
      resource_type: "route-table",
      tags: [{ key: "Name", value: "#{name}-rtb" }]
    }]
  )
  rtb_id = rtb_resp.route_table.route_table_id

  # Add route to internet gateway
  @client.create_route(
    route_table_id: rtb_id,
    destination_cidr_block: "0.0.0.0/0",
    gateway_id: igw_id
  )

  # Associate route table with subnet
  @client.associate_route_table(
    route_table_id: rtb_id,
    subnet_id: subnet_resp.subnet.subnet_id
  )

  Types::Network::Record.new(
    id: vpc_id,
    name:,
    ip_range: create_resp.vpc.cidr_block
  )
end

#find_server(name) ⇒ Object

Server operations



164
165
166
167
168
169
# File 'lib/nvoi/external/cloud/aws.rb', line 164

def find_server(name)
  instance = find_instance_by_name(name)
  return nil unless instance

  instance_to_server(instance)
end

#find_server_by_id(id) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/nvoi/external/cloud/aws.rb', line 171

def find_server_by_id(id)
  result = @client.describe_instances(instance_ids: [id])
  return nil if result.reservations.empty? || result.reservations[0].instances.empty?

  instance_to_server(result.reservations[0].instances[0])
rescue ::Aws::EC2::Errors::InvalidInstanceIDNotFound
  nil
end

#get_firewall_by_name(name) ⇒ Object



151
152
153
154
155
156
# File 'lib/nvoi/external/cloud/aws.rb', line 151

def get_firewall_by_name(name)
  sg = find_security_group_by_name(name)
  raise Errors::FirewallError, "firewall not found: #{name}" unless sg

  Types::Firewall::Record.new(id: sg.group_id, name:)
end

#get_network_by_name(name) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/nvoi/external/cloud/aws.rb', line 99

def get_network_by_name(name)
  vpc = find_vpc_by_name(name)
  raise Errors::NetworkError, "network not found: #{name}" unless vpc

  Types::Network::Record.new(
    id: vpc.vpc_id,
    name:,
    ip_range: vpc.cidr_block
  )
end

#get_volume(id) ⇒ Object



278
279
280
281
282
283
# File 'lib/nvoi/external/cloud/aws.rb', line 278

def get_volume(id)
  resp = @client.describe_volumes(volume_ids: [id])
  return nil if resp.volumes.empty?

  volume_to_object(resp.volumes[0])
end

#get_volume_by_name(name) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/nvoi/external/cloud/aws.rb', line 285

def get_volume_by_name(name)
  resp = @client.describe_volumes(
    filters: [{ name: "tag:Name", values: [name] }]
  )
  return nil if resp.volumes.empty?

  volume_to_object(resp.volumes[0])
end

#list_instance_typesObject

List available instance types for onboarding



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/nvoi/external/cloud/aws.rb', line 348

def list_instance_types
  # Common instance types including ARM (Graviton)
  common_types = %w[
    t3.micro t3.small t3.medium t3.large t3.xlarge
    t4g.micro t4g.small t4g.medium t4g.large t4g.xlarge
    m5.large m5.xlarge m6g.large m6g.xlarge
    c5.large c5.xlarge c6g.large c6g.xlarge
  ]
  resp = @client.describe_instance_types(instance_types: common_types)
  resp.instance_types.map do |t|
    arch = t.processor_info&.supported_architectures&.first || "x86_64"
    {
      name: t.instance_type,
      vcpus: t.v_cpu_info.default_v_cpus,
      memory: t.memory_info.size_in_mi_b,
      architecture: arch.include?("arm") ? "arm64" : "x86"
    }
  end
rescue StandardError
  # Fallback to static list if API fails
  common_types.map do |t|
    arch = t.include?("g.") ? "arm64" : "x86"  # Graviton types have 'g' suffix
    { name: t, vcpus: nil, memory: nil, architecture: arch }
  end
end

#list_regionsObject

List available regions for onboarding



375
376
377
378
379
380
# File 'lib/nvoi/external/cloud/aws.rb', line 375

def list_regions
  resp = @client.describe_regions
  resp.regions.map do |r|
    { name: r.region_name, endpoint: r.endpoint }
  end
end

#list_serversObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/nvoi/external/cloud/aws.rb', line 180

def list_servers
  result = @client.describe_instances(
    filters: [{
      name: "instance-state-name",
      values: %w[pending running stopping stopped]
    }]
  )

  servers = []
  result.reservations.each do |reservation|
    reservation.instances.each do |instance|
      servers << instance_to_server(instance)
    end
  end
  servers
end

#validate_credentialsObject



340
341
342
343
344
345
# File 'lib/nvoi/external/cloud/aws.rb', line 340

def validate_credentials
  @client.describe_regions
  true
rescue StandardError => e
  raise Errors::ValidationError, "aws credentials invalid: #{e.message}"
end

#validate_instance_type(instance_type) ⇒ Object

Validation operations



326
327
328
329
330
331
# File 'lib/nvoi/external/cloud/aws.rb', line 326

def validate_instance_type(instance_type)
  resp = @client.describe_instance_types(instance_types: [instance_type])
  raise Errors::ValidationError, "invalid AWS instance type: #{instance_type}" if resp.instance_types.empty?

  true
end

#validate_region(region) ⇒ Object



333
334
335
336
337
338
# File 'lib/nvoi/external/cloud/aws.rb', line 333

def validate_region(region)
  resp = @client.describe_regions(region_names: [region])
  raise Errors::ValidationError, "invalid AWS region: #{region}" if resp.regions.empty?

  true
end

#wait_for_device_path(volume_id, _ssh) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/nvoi/external/cloud/aws.rb', line 310

def wait_for_device_path(volume_id, _ssh)
  # AWS provides device path in attachment info
  Utils::Retry.poll(max_attempts: 30, interval: 2) do
    resp = @client.describe_volumes(volume_ids: [volume_id])
    next nil if resp.volumes.empty?

    vol = resp.volumes[0]
    next nil if vol.attachments.empty?

    device = vol.attachments[0].device
    device unless device.blank?
  end
end

#wait_for_server(server_id, max_attempts) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/nvoi/external/cloud/aws.rb', line 231

def wait_for_server(server_id, max_attempts)
  server = Utils::Retry.poll(max_attempts:, interval: 5) do
    resp = @client.describe_instances(instance_ids: [server_id])

    if resp.reservations.any? && resp.reservations[0].instances.any?
      instance = resp.reservations[0].instances[0]
      instance_to_server(instance) if instance.state.name == "running"
    end
  end

  raise Errors::ServerCreationError, "instance did not become running after #{max_attempts} attempts" unless server

  server
end