File: ecs_setup.rb

package info (click to toggle)
ruby-mongo 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,020 kB
  • sloc: ruby: 110,810; makefile: 5
file content (72 lines) | stat: -rw-r--r-- 2,499 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
autoload :AwsUtils, 'support/aws_utils'

class EcsSetup
  def run
    opts = {
      region: ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_REGION'),
      access_key_id: ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_ACCESS_KEY_ID'),
      secret_access_key: ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_SECRET_ACCESS_KEY'),
    }

    inspector = AwsUtils::Inspector.new(**opts)

    cluster = inspector.ecs_client.describe_clusters(
      clusters: [ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_ECS_CLUSTER_ARN')],
    ).clusters.first

    orchestrator = AwsUtils::Orchestrator.new(**opts)

    service_name = "mdb-ruby_test_#{SecureRandom.uuid}"
    puts "Using service name: #{service_name}"

    service = orchestrator.provision_auth_ecs_task(
      cluster_name: cluster.cluster_name,
      service_name: service_name,
      security_group_id: ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_ECS_SECURITY_GROUP'),
      subnet_ids: ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_ECS_SUBNETS').split(','),
      task_definition_ref: ENV.fetch('MONGO_RUBY_DRIVER_AWS_AUTH_ECS_TASK_DEFINITION_ARN'),
    )

    puts "Waiting for #{service_name} to become ready"
    orchestrator.wait_for_ecs_ready(
      cluster_name: cluster.cluster_name,
      service_name: service_name,
    )
    puts "... OK"

    status = inspector.ecs_status(
      cluster_name: cluster.cluster_name,
      service_name: service.service_name,
      get_public_ip: false, get_logs: false,
    )

    # Wait for the task to provision itself. In Evergreen I assume the image
    # already comes with SSH configured therefore this step is probably not
    # needed, but when we test using the driver tooling there is a reasonably
    # lengthy post-boot provisioning process that we need to wait for to
    # complete.
    begin
      Timeout.timeout(180) do
        begin
          Timeout.timeout(5) do
            # The StrictHostKeyChecking=no option is important here.
            # Note also that once this connection succeeds, this option
            # need not be passed again when connecting to the same IP.
            puts "Try to connect to #{status[:private_ip]}"
            puts `ssh -o StrictHostKeyChecking=no root@#{status[:private_ip]} id`
          end
        rescue Timeout::Error
          retry
        end
      end
    rescue Timeout::Error
      raise 'The task did not provision itself in 3 minutes'
    end

    File.open('.env.private.ecs', 'w') do |f|
      status.each do |k, v|
        f << "#{k.upcase}=#{v}\n"
      end
    end
  end
end