Class: VagrantPlugins::OCI::Action::RunInstance

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::IsPortOpen
Defined in:
lib/vagrant-oci/action/run_instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ RunInstance

Returns a new instance of RunInstance.



12
13
14
15
# File 'lib/vagrant-oci/action/run_instance.rb', line 12

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_oci::action::run_instance")
end

Instance Method Details

#call(env) ⇒ Object



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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/vagrant-oci/action/run_instance.rb', line 17

def call(env)
  # Initialize metrics if they haven't been
  env[:metrics] ||= {}

  provider_config = env[:machine].provider_config
  oci_config = env[:oci_config]
   = {}
  .store('hostname', env[:machine].name)

  if provider_config.image_regex and provider_config.image_id.nil?
    @@image_list ||= {}
    @@image_list[oci_config.tenancy] ||= {}
    @@image_list[oci_config.tenancy][provider_config.region] ||= env[:oci_compute].list_images(provider_config.image_compartment_id).data.map(&:to_hash).sort_by {|i| i['display-name']}.reverse
    r = Regexp.new(provider_config.image_regex)
    provider_config.image_id = @@image_list[oci_config.tenancy][provider_config.region].filter {|i| r.match(i[:displayName])}.first[:id]
  end

  if provider_config.subnet_id.nil? and provider_config.subnet_vcn_compartment_name and provider_config.subnet_vcn_dns_label and provider_config.subnet_compartment_name and provider_config.subnet_dns_label
    @@vcn_list ||= {}
    @@vcn_list[oci_config.tenancy] ||= {}
    @@vcn_list[oci_config.tenancy][provider_config.region] ||= env[:oci_network].list_vcns(provider_config.subnet_vcn_compartment_id).data.map(&:to_hash)
    vcn_map = @@vcn_list[oci_config.tenancy][provider_config.region].inject({}) {|a, v| a[v[:dnsLabel]] = v[:id]; a}
    vcn_id = vcn_map[provider_config.subnet_vcn_dns_label]

    @@subnet_list ||= {}
    @@subnet_list[oci_config.tenancy] ||= {}
    @@subnet_list[oci_config.tenancy][provider_config.region] ||= {}
    @@subnet_list[oci_config.tenancy][provider_config.region][vcn_id] ||= env[:oci_network].list_subnets(provider_config.subnet_compartment_id, vcn_id).data.map(&:to_hash)
    subnet_map = @@subnet_list[oci_config.tenancy][provider_config.region][vcn_id].inject({}) {|a, v| a[v[:dnsLabel]] = v[:id]; a}
    provider_config.subnet_id = subnet_map[provider_config.subnet_dns_label]
  end

  create_vnic_details = ::OCI::Core::Models::CreateVnicDetails.new(:subnet_id => provider_config.subnet_id)

  ssh_private_key_path = env[:machine].config.ssh.private_key_path.first
  ssh_private_key = File.read(File.expand_path(ssh_private_key_path)).chomp
  key = OpenSSL::PKey::RSA.new(ssh_private_key)
  type = key.ssh_type
  data = [key.to_blob].pack('m0')
  openssh_pubkey = "#{type} #{data}"
  .store('ssh_authorized_keys', openssh_pubkey)

  api_request = ::OCI::Core::Models::LaunchInstanceDetails.new
  api_request.availability_domain = env[:ad_name]
  api_request.compartment_id = provider_config.server_compartment_id
  api_request.display_name = env[:machine].name
  api_request.shape = provider_config.shape
  api_request.image_id = provider_config.image_id
  api_request. = 
  api_request.create_vnic_details = create_vnic_details
  api_request.freeform_tags = provider_config.freeform_tags
  api_request.defined_tags = provider_config.defined_tags
  compute_api = env[:oci_compute]

  request_start_time = Time.now.to_i
  puts "Launching new instance .."
  ci = launch_instance_data = env[:oci_compute].launch_instance(api_request).data.to_hash
  env[:machine].id = ci[:id]

  vnic_attachments_data = nil
  puts "Waiting for vnic attachments .."
  loop do
    vnic_attachments_data = env[:oci_compute].list_vnic_attachments(provider_config.server_compartment_id, :instance_id => ci[:id]).data.map(&:to_hash)
    break if vnic_attachments_data.any?
    sleep 5
  end

  vnic_id = vnic_attachments_data.first[:vnicId]
  vnic_data = env[:oci_network].get_vnic(vnic_id).data.to_hash
  private_ip = vnic_data[:privateIp]
  public_ip = vnic_data[:publicIp]
  ip_addr = provider_config.use_private_network ? private_ip : public_ip || private_ip

  env[:ui].info(I18n.t("vagrant_oci.waiting_for_ready"))
  original_stderr = $stderr.clone
  $stderr.reopen(File.new(IO::NULL, 'w'))
  begin
    loop do
      conn = nil
      begin
        Timeout::timeout(10) do
          conn = Net::SSH.start(ip_addr, env[:machine].config.ssh.username, key_data: [ssh_private_key])
        end
      rescue
        conn = nil
      end
      break if conn
    end
    $stderr.reopen(original_stderr)
    env[:metrics]["instance_ready_time"] = Time.now.to_i - request_start_time
    @logger.info("Time for instance ready: #{env[:metrics]["instance_ready_time"]}")
    env[:ui].info(I18n.t("vagrant_oci.ready"))
  rescue
    env[:interrupted] = true
  end
end