<commit_before>require 'rubygems'
require 'logger'
require "right_api_client"
require 'yaml'
@logger = Logger.new(STDOUT)


def cloud()
  @client.clouds(id: @config[:cloud_id]).show
end
def create_network(name)
  network=@client.networks.index(filter: ["name==#{name}","cloud_href==#{@cloud.href}"]).first
  if network
    @logger.info "network found #{network.show.name}"
    sgs = @cloud.security_groups.index(filter: ["network_href==#{network.href}"])
    sgs.each{|sg| @cloud.security_groups(id: sg.href.split('/').last).destroy unless sg.name=='default'}
    network_id=network.href.split('/').last
    nng= @client.network_gateways.index(filter: ["network_href==#{network.href}"]).first
    nng.update(network_gateway:{network_href:''}) if nng
    subnets = @cloud.subnets.index(filter: ["network_href==#{network.href}"])
    subnets.each{|subnet| @cloud.subnets(id: subnet.href.split('/').last).destroy}
    route_tables = @client.route_tables.index(filter: ["network_href==#{network.href}","name<>rt-"])
    route_tables.each{|route_table| @client.route_tables(id: route_table.href.split('/').last).destroy} 
    @client.networks(id: network_id).destroy 

    @logger.info "network #{network.show.name} deleted"
  end
  
  new_network=@client.networks.create(network:{cidr_block: '10.0.0.0/16',cloud_href: @cloud.href,
      name: name})
  @logger.info "new network #{new_network.show.name} created" if new_network
  @client.networks(id: new_network.href.split('/').last).show
end

def create_admin_security_group()

  sg= @cloud.security_groups.create(security_group: {name: "#{@network.name}-admin", network_href: @network.href})
  @logger.info "security group #{sg.show.name} created" if sg
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '0.0.0.0/0', 
      direction: 'ingress', protocol: 'tcp',protocol_details: {end_port: '22',start_port: "22"},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} 22-22 added" if sgr
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '0.0.0.0/0', 
      direction:'ingress', protocol: 'tcp',protocol_details:{end_port: '3389',start_port: 3389},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} 3389 added" if sgr
  sg
end
def create_nathost_security_group()

  sg= @cloud.security_groups.create(security_group: {name: "#{@network.name}-nathost", network_href: @network.href})
  @logger.info "security group #{sg.show.name} created" if sg
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'ingress', protocol: 'all',protocol_details:{start_port: '0',end_port: "65535"},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} ingress 0-65535 added" if sgr
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'egress', protocol: 'all',protocol_details:{start_port: '0',end_port: '65565'},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} egress 0-65535 added" if sgr

  sg
end
def create_passthrough_security_group()
  sg= @cloud.security_groups.create(security_group: {name: "#{@network.name}-private_pass_through", network_href: @network.href})
  @logger.info "security group #{sg.show.name} created" if sg
  sgr = sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'ingress', protocol: 'all',protocol_details:{end_port: '0',start_port: '65535'},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} ingress 0-65535 added" if sgr
  sgr = sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'egress', protocol: 'all',protocol_details:{end_port: '0',start_port: '65535'},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} egress 0-65535 added" if sgr
  sg
end

def create_subnet(name,cidr_block, datacenter)

  subnet = @cloud.subnets.create(subnet: {cidr_block: cidr_block, name: name ,
      datacenter_href: datacenter.href, network_href: @network.href,description: name}) 
  while subnet.show.state=='pending'
    sleep 30
    @logger.info "subnet status: #{subnet.show.state}"
  end
  @logger.info "Created subnet #{name} for datacenter #{datacenter.name}"
  subnet.show
end

def create_rt_public()
  rt = @client.route_tables.index(filter: ["name==public","network_href==#{@network.href}",
      "cloud_href==#{@cloud.href}"]).first
  unless rt
    rt = @client.route_tables.create(route_table: {name: 'public', 
        network_href: @network.href, cloud_href: @cloud.href})
    while rt.show.state=='pending'
      sleep 30
      @logger.info "subnet status: #{rt.show.state}"
    end
    rt.show.routes.create(route: {destination_cidr_block: '0.0.0.0/0', 
        next_hop_type: 'network_gateway', next_hop_href: @gateway.href ,route_table_href: rt.href})
  end
  rt.show
end
def create_rt_private()
  rt = @client.route_tables.index(filter: ["name==private","network_href==#{@network.href}",
      "cloud_href==#{@cloud.href}"]).first
  unless rt
    rt = @client.route_tables.create(route_table: {name: 'private', cloud_href: @cloud.href, network_href: @network.href})
    while rt.show.state=='pending'
      sleep 30
      @logger.info "subnet status: #{rt.show.state}"
    end
  end
  rt.show
end

def create_gw()
  ng=@client.network_gateways.index(filter: ["name==#{@network.name}-igw","cloud_href==#{@cloud.href}"]).first
  if ng
    @logger.info "network gateway #{ng.show.name} found"
    network_id=ng.href.split('/').last
    @client.network_gateways(id: network_id).destroy 
    @logger.info "network gateway #{ng.show.name} deleted"
  else
    ng= @client.network_gateways.create(network_gateway:{name: "#{@network.name}-igw", 
        cloud_href: @cloud.href, type: 'internet'})
    ng.update(network_gateway:{network_href: @network.href})
    @logger.info "Network Gateway #{ng.show.name} created."
  end
  while ng.show.state == 'pending'
    sleep 30
  end
  
  ng.show
end

def create_eip()
end

def create_nat_host()
  server_template = @client.server_templates(id: @config[:nat_host_server_template_id] ).show
  ssh_key = @cloud.ssh_keys.index(filter: ["resource_uid==#{@config[:nat_server_ssh_key]}"]).first

  sgs = @cloud.security_groups.index(filter: ["name==#{@network.name}"]).collect{|s| s.href}
  subnet  = @cloud.subnets.index(filter: ["name==public","network_href==#{@network.href}"]).first
  server  = @deployment.show.servers.create(server: { name: @config[:vpc_server_name],  
      instance: {cloud_href: @cloud.href, 
        associate_public_ip_address: true, datacenter_href: subnet.datacenter.href,
        security_group_hrefs: sgs ,server_template_href: server_template.href,
        ssh_key_href: ssh_key.href, subnet_hrefs: [subnet.href]}})
  server.launch
  server
end

def add_nat_to_rt(server)
  rt = @client.route_tables.index(filter: ["name==private","network_href==#{@network.href}"]).first
  @logger.info server.href
  while  server.show.state == 'pending'<commit_msg>fix config name.<commit_after>require 'rubygems'
require 'logger'
require "right_api_client"
require 'yaml'
@logger = Logger.new(STDOUT)


def cloud()
  @client.clouds(id: @config[:cloud_id]).show
end
def create_network(name)
  network=@client.networks.index(filter: ["name==#{name}","cloud_href==#{@cloud.href}"]).first
  if network
    @logger.info "network found #{network.show.name}"
    sgs = @cloud.security_groups.index(filter: ["network_href==#{network.href}"])
    sgs.each{|sg| @cloud.security_groups(id: sg.href.split('/').last).destroy unless sg.name=='default'}
    network_id=network.href.split('/').last
    nng= @client.network_gateways.index(filter: ["network_href==#{network.href}"]).first
    nng.update(network_gateway:{network_href:''}) if nng
    subnets = @cloud.subnets.index(filter: ["network_href==#{network.href}"])
    subnets.each{|subnet| @cloud.subnets(id: subnet.href.split('/').last).destroy}
    route_tables = @client.route_tables.index(filter: ["network_href==#{network.href}","name<>rt-"])
    route_tables.each{|route_table| @client.route_tables(id: route_table.href.split('/').last).destroy} 
    @client.networks(id: network_id).destroy 

    @logger.info "network #{network.show.name} deleted"
  end
  
  new_network=@client.networks.create(network:{cidr_block: '10.0.0.0/16',cloud_href: @cloud.href,
      name: name})
  @logger.info "new network #{new_network.show.name} created" if new_network
  @client.networks(id: new_network.href.split('/').last).show
end

def create_admin_security_group()

  sg= @cloud.security_groups.create(security_group: {name: "#{@network.name}-admin", network_href: @network.href})
  @logger.info "security group #{sg.show.name} created" if sg
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '0.0.0.0/0', 
      direction: 'ingress', protocol: 'tcp',protocol_details: {end_port: '22',start_port: "22"},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} 22-22 added" if sgr
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '0.0.0.0/0', 
      direction:'ingress', protocol: 'tcp',protocol_details:{end_port: '3389',start_port: 3389},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} 3389 added" if sgr
  sg
end
def create_nathost_security_group()

  sg= @cloud.security_groups.create(security_group: {name: "#{@network.name}-nathost", network_href: @network.href})
  @logger.info "security group #{sg.show.name} created" if sg
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'ingress', protocol: 'all',protocol_details:{start_port: '0',end_port: "65535"},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} ingress 0-65535 added" if sgr
  sgr=sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'egress', protocol: 'all',protocol_details:{start_port: '0',end_port: '65565'},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} egress 0-65535 added" if sgr

  sg
end
def create_passthrough_security_group()
  sg= @cloud.security_groups.create(security_group: {name: "#{@network.name}-private_pass_through", network_href: @network.href})
  @logger.info "security group #{sg.show.name} created" if sg
  sgr = sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'ingress', protocol: 'all',protocol_details:{end_port: '0',start_port: '65535'},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} ingress 0-65535 added" if sgr
  sgr = sg.show.security_group_rules.create(security_group_rule: {cidr_ips: '10.0.0.0/16', 
      direction:'egress', protocol: 'all',protocol_details:{end_port: '0',start_port: '65535'},
      source_type: 'cidr_ips'})
  @logger.info "security group rule for #{sg.show.name} egress 0-65535 added" if sgr
  sg
end

def create_subnet(name,cidr_block, datacenter)

  subnet = @cloud.subnets.create(subnet: {cidr_block: cidr_block, name: name ,
      datacenter_href: datacenter.href, network_href: @network.href,description: name}) 
  while subnet.show.state=='pending'
    sleep 30
    @logger.info "subnet status: #{subnet.show.state}"
  end
  @logger.info "Created subnet #{name} for datacenter #{datacenter.name}"
  subnet.show
end

def create_rt_public()
  rt = @client.route_tables.index(filter: ["name==public","network_href==#{@network.href}",
      "cloud_href==#{@cloud.href}"]).first
  unless rt
    rt = @client.route_tables.create(route_table: {name: 'public', 
        network_href: @network.href, cloud_href: @cloud.href})
    while rt.show.state=='pending'
      sleep 30
      @logger.info "subnet status: #{rt.show.state}"
    end
    rt.show.routes.create(route: {destination_cidr_block: '0.0.0.0/0', 
        next_hop_type: 'network_gateway', next_hop_href: @gateway.href ,route_table_href: rt.href})
  end
  rt.show
end
def create_rt_private()
  rt = @client.route_tables.index(filter: ["name==private","network_href==#{@network.href}",
      "cloud_href==#{@cloud.href}"]).first
  unless rt
    rt = @client.route_tables.create(route_table: {name: 'private', cloud_href: @cloud.href, network_href: @network.href})
    while rt.show.state=='pending'
      sleep 30
      @logger.info "subnet status: #{rt.show.state}"
    end
  end
  rt.show
end

def create_gw()
  ng=@client.network_gateways.index(filter: ["name==#{@network.name}-igw","cloud_href==#{@cloud.href}"]).first
  if ng
    @logger.info "network gateway #{ng.show.name} found"
    network_id=ng.href.split('/').last
    @client.network_gateways(id: network_id).destroy 
    @logger.info "network gateway #{ng.show.name} deleted"
  else
    ng= @client.network_gateways.create(network_gateway:{name: "#{@network.name}-igw", 
        cloud_href: @cloud.href, type: 'internet'})
    ng.update(network_gateway:{network_href: @network.href})
    @logger.info "Network Gateway #{ng.show.name} created."
  end
  while ng.show.state == 'pending'
    sleep 30
  end
  
  ng.show
end

def create_eip()
end

def create_nat_host()
  server_template = @client.server_templates(id: @config[:nat_host_server_template_id] ).show
  ssh_key = @cloud.ssh_keys.index(filter: ["resource_uid==#{@config[:nat_server_ssh_key]}"]).first

  sgs = @cloud.security_groups.index(filter: ["name==#{@network.name}"]).collect{|s| s.href}
  subnet  = @cloud.subnets.index(filter: ["name==public","network_href==#{@network.href}"]).first
  server  = @deployment.show.servers.create(server: { name: @config[:nat_server_name],  
      instance: {cloud_href: @cloud.href, 
        associate_public_ip_address: true, datacenter_href: subnet.datacenter.href,
        security_group_hrefs: sgs ,server_template_href: server_template.href,
        ssh_key_href: ssh_key.href, subnet_hrefs: [subnet.href]}})
  server.launch
  server
end

def add_nat_to_rt(server)
  rt = @client.route_tables.index(filter: ["name==private","network_href==#{@network.href}"]).first
  @logger.info server.href
  while  server.show.state == 'pending'
{'fraction_non_alphanumeric': 0.16423128015101726, 'fraction_numerical': 0.01761868139551143, 'mean_word_length': 5.02020202020202, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 16, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2911874', 'n_tokens_mistral': 5278, 'n_tokens_neox': 5097, 'n_words': 889}