require 'oci'
require_relative 'alphaNetwork'

compartment_id = "ocid1.compartment.oc1..aaaaaaaa7u3evhcuylmvk32miyjzqwkwov3ckkij3t4hv3ytffuum7p7ni3a"
gsebmcs00001_13_compartment_id = "ocid1.compartment.oc1..aaaaaaaaymztafvorbatzvgosdfsujarypdn3nvsj73qklegjzf4fppzbtaq"
gsebmcs00001_17_compartment_id = "ocid1.compartment.oc1..aaaaaaaadh7wv3lc4qykncennuvhp5clvd5fba3h5d2afgpdsfjgjt6cdb7a"
gsebmcs00001_18_compartment_id = "ocid1.compartment.oc1..aaaaaaaarfzuy7lq6i6vq7kgneuqein3366jnjlidvayv2duamlgt4hef3tq"
gsebmcs00001_19_compartment_id = "ocid1.compartment.oc1..aaaaaaaalrxsqcqqrhae4nfs63y66hjj3qaiyku2qkzz3iw7k677ts56ruja"
gsebmcs00001_25_compartment_id = "ocid1.compartment.oc1..aaaaaaaap4fkhnu6sdldjs5awxfyuryt3ecfyofo6docri7472337lq6vpxq"
gsebmcs00001_41_compartment_id = "ocid1.compartment.oc1..aaaaaaaaudh5wwpzbwtkjbic5hdfcxiv6du5pyjgb6fwqqvb3ac6o5t3obsa"
gsebmcs00001_47_compartment_id = "ocid1.compartment.oc1..aaaaaaaaw4c5mupzoaiin2lkzcpidgva673gjc6olylstd7rgqxqzeckplyq"
gsebmcs00001_48_compartment_id = "ocid1.compartment.oc1..aaaaaaaakxjgdofbhi3kc3xcepkjk4bsjhgbqv2fahvppoi6olwx5bz5tkoa"
gsebmcs00002_02_compartment_id = "ocid1.compartment.oc1..aaaaaaaa4kkb7nptzln7n5ewdh3b3d6aclkktjojnfoced2wykcmozif37sq"
gsebmcs00002_03_compartment_id = "ocid1.compartment.oc1..aaaaaaaadb3mounhnfjqiet6qoywdezq6tgvv6fektxcwlxrafy7p4zo5zra"
gsebmcs00008_38_compartment_id = "ocid1.compartment.oc1..aaaaaaaahdg5uw6eia2ccxespaeglgnuubakkukf232hzkzrkuohzwizfjxq"
gsebmcs00008_47_compartment_id = "ocid1.compartment.oc1..aaaaaaaauo3fg4vumpv4zw7obhuqmc5hevrwm3tze6xhidelvkcgskgzvnsq"
gsebmcs00009_13_compartment_id = "ocid1.compartment.oc1..aaaaaaaakgheggu32wbfvthjv7shxhfq46bizan2vwcwmazyl3zikeb2xxla"
gsebmcs00009_48_compartment_id = "ocid1.compartment.oc1..aaaaaaaa7u3evhcuylmvk32miyjzqwkwov3ckkij3t4hv3ytffuum7p7ni3a"

ssh_key_file = "../Keys/oci_instance_key.pub"
ssh_key = File.open(File.expand_path(ssh_key_file), "rb").read
my_config = OCI::ConfigFileLoader.load_config(config_file_location:'./.oci/config', profile_name:'DEFAULT')
compute_client = OCI::Core::ComputeClient.new(config:my_config)
vcn_client = OCI::Core::VirtualNetworkClient.new(config:my_config)

def launch_instance(compute_client, vcn_client, name, subnet, availability_domain, compartment_id, ssh_key, my_config)
  puts 'Launching instance...'
  image_id = 'ocid1.image.oc1.phx.aaaaaaaabifl2bmaygtu4riw3vcuowl5cqwdzdqzwndqneoybcfcn2pgyc6a' # OL 7.4
  shape = 'BM.Standard1.36'
  #shape = 'VM.Standard1.1'
  request = OCI::Core::Models::LaunchInstanceDetails.new(config:my_config)
  request.availability_domain = availability_domain
  request.compartment_id = compartment_id
  request.display_name = name
  request.image_id = image_id
  request.shape = shape
  request.subnet_id = subnet.id
  request.metadata = {'ssh_authorized_keys' => ssh_key}
  response = compute_client.launch_instance(request)
  instance = response.data

  instance_id = instance.id
  puts "Launched instance '#{instance.display_name}' [#{instance.id}]"
  print "Waiting to reach running state."
  $stdout.flush

  compute_client.get_instance(instance.id).wait_until(:lifecycle_state, OCI::Core::Models::Instance::LIFECYCLE_STATE_RUNNING) { |response|
    if response.data.lifecycle_state == OCI::Core::Models::Instance::LIFECYCLE_STATE_TERMINATED ||
        response.data.lifecycle_state == OCI::Core::Models::Instance::LIFECYCLE_STATE_TERMINATING
      puts "Instance failed to provision."
      throw :stop_succeed # :stop_fail can be used to throw a WaiterFailedError exception. :stop_succeed will stop the waiter silently.
    end

    print '.'
    $stdout.flush
  }

  puts ''
  puts "Instance '#{instance.display_name}' is now running."

  publicIPString = ""
  vnic = OCI::Core::Models::Vnic.new
  compute_client.list_vnic_attachments(instance.compartment_id, {:instance_id=>instance.id}).each do |response|
    response.data.each do |vnic_attachment|
      vnic = vcn_client.get_vnic(vnic_attachment.vnic_id)
      puts "  Private IP = '#{vnic.data.private_ip}'"
      puts "  Public IP  = '#{vnic.data.public_ip}'"
    end
  end

  return vnic
end

def setup_mysql(publicIP)
  puts 'Setup MySQL ...'
  scriptStr = "./runSetupMySQL.sh"
  cmdStr = scriptStr + " " + publicIP
  system (cmdStr)
end

def setup_nodejs(mwPublicIP, dbPublicIP)
  puts 'Setup NodeJS ...'
  scriptStr = "./runSetupNodeJS.sh"
  cmdStr = scriptStr + " " + mwPublicIP + " " + dbPublicIP
  system (cmdStr)
end

availability_domain = "pmne:PHX-AD-3"
#availability_domain = "GxAT:PHX-AD-3"
#availability_domain = "lXWr:PHX-AD-3"
#availability_domain = "MkHF:PHX-AD-3"
vcn = createVCN(vcn_client, compartment_id, my_config)
internet_gateway = createInternetGateway(vcn_client, compartment_id, vcn, my_config)
addIGWRouteRule(vcn_client, internet_gateway, vcn, my_config)
dbSL = createDBSecurityList(vcn_client, compartment_id, vcn, my_config)
mwSL = createMWSecurityList(vcn_client, compartment_id, vcn, my_config)
dbSubnet =  createDBSubnet(vcn_client, vcn, compartment_id, my_config, dbSL.id, availability_domain)
mwSubnet =  createMWSubnet(vcn_client, vcn, compartment_id, my_config, mwSL.id, availability_domain)
dbVMName = "AlphaDB01"
dbVNIC = launch_instance(compute_client, vcn_client, dbVMName, dbSubnet, availability_domain, compartment_id, ssh_key, my_config)
mwVMName = "AlphaMW01"
mwVNIC = launch_instance(compute_client, vcn_client, mwVMName, mwSubnet, availability_domain, compartment_id, ssh_key, my_config)
sleep(60)
setup_mysql (dbVNIC.data.public_ip)
updateDBSecurityLists(vcn_client, dbSL)
sleep(60)
setup_nodejs(mwVNIC.data.public_ip, dbVNIC.data.private_ip)
puts "DB Public IP = " + dbVNIC.data.public_ip
puts "MW Public IP = " + mwVNIC.data.public_ip
puts "Application URL = http://" + mwVNIC.data.public_ip + ":8080/alpha.html"
