<commit_before>#!/usr/bin/env ruby

# Use this script to install and provision contrail nodes.
# sudo ruby $PWD/contrail-kubernetes/scripts/opencontrail-install/contrail_install.rb

raise 'Must run with superuser privilages' unless Process.uid == 0

@ws="#{File.dirname($0)}"
require "#{@ws}/util"
STDOUT.sync = true

sh("\grep aurora /etc/hostname", true)
@controller_host = $?.to_i == 0 ? "aurora" : "kubernetes-master"

# Initialize default interfaces and user account names
# TODO Take via command line options
@intf = "eth0"
@user = "ubuntu"
if File.directory? "/vagrant" then
    @intf = "eth1"
    @user = "vagrant"
end

# Find platform OS
sh(%{\grep -i "ubuntu 14" /etc/issue 2>&1 > /dev/null}, true)
@platform = $?.to_i == 0 ? "ubuntu1404" : "fedora20"

@control_node_introspect_port = @controller_host == "aurora" ? 9083 : "8083"

require "#{@ws}/#{@platform}/install"

# Update ssh configuration
def ssh_setup
    conf=<<EOF
UserKnownHostsFile=/dev/null
StrictHostKeyChecking=no
LogLevel=QUIET
EOF
    sh("mkdir -p /root/.ssh")
    File.open("/root/.ssh/config", "a") { |fp| fp.puts(conf) }
    sh("chmod 600 /root/.ssh/config")

    # Add ssh config to ~@user also.
    sh("mkdir -p /home/#{@user}/.ssh")
    File.open("/home/#{@user}/.ssh/config", "a") { |fp| fp.puts(conf) }
    sh("chmod 600 /home/#{@user}/.ssh/config")
    sh("chown #{@user}.#{@user} /home/#{@user}/.ssh/config")
    sh("chown #{@user}.#{@user} /home/#{@user}/.ssh/.")
end

# Do initial setup
def initial_setup
    @resolvers = sh("\grep -w nameserver /etc/resolv.conf").split("\n")
    ssh_setup
    sh("service hostname restart", true) if @platform !~ /fedora/
#   @contrail_controller = IPSocket.getaddress(@controller_host)
    @contrail_controller =
        sh(%{grep #{@controller_host} /etc/hosts | awk '{print $1}'})
    error "Cannot resolve contrail-controller host" \
        if @contrail_controller.empty?

    # Make sure that localhost resolves to 127.0.0.1
    sh(%{\grep -q "127\.0\.0\.1.*localhost" /etc/hosts}, true)
    sh("echo 127.0.0.1 localhost >> /etc/hosts") if $?.to_i != 0

    Dir.chdir("#{@ws}")
end

def update_controller_etc_hosts
    # Update /etc/hosts with the IP address
    @controller_ip, mask, gw, prefix_len = get_intf_ip(@intf)

    rip = sh("\grep #{@controller_host} /etc/hosts | awk '{print $1}'", true)
    return if rip != "127.0.0.1" and !rip.empty?

    # If @controller_host resolves to 127.0.0.1, take it out of /etc/hosts
    sh("sed -i '/127.0.0.1 #{@controller_host}/d' /etc/hosts") \
        if rip == "127.0.0.1"
    sh("echo #{@controller_ip} #{@controller_host} >> /etc/hosts")
end

def verify_controller
    sh("netstat -anp | \grep LISTEN | \grep -w 5672", false, 10, 3) # RabbitMQ
    sh("netstat -anp | \grep LISTEN | \grep -w 2181", false, 10, 3) # ZooKeeper
    sh("netstat -anp | \grep LISTEN | \grep -w 9160", false, 10, 3) # Cassandra
    sh("netstat -anp |\grep LISTEN | \grep -w #{@control_node_introspect_port}",
       false, 10, 3) # Control-Node
    sh("netstat -anp | \grep LISTEN | \grep -w 5998", false, 10, 3) # discovery
    sh("netstat -anp | \grep LISTEN | \grep -w 6379", false, 10, 3) # redis
    sh("netstat -anp | \grep LISTEN | \grep -w 8443", false, 10, 3) # IFMAP
    sh("netstat -anp | \grep LISTEN | \grep -w 8082", false, 10, 3) # API-Server
    sh("netstat -anp | \grep LISTEN | \grep -w 8086", false, 10, 3) # Collector
    sh("netstat -anp | \grep LISTEN | \grep -w 8081", false, 10, 3) # OpServer
    sh("netstat -anp | \grep LISTEN | \grep -w 8143", false, 10, 3) # WebUI
    sh("netstat -anp | \grep LISTEN | \grep -w 8070", false, 10, 3) # WebUI

    puts "All contrail controller components up"
end

# Provision contrail-controller
def provision_contrail_controller
    update_controller_etc_hosts

    sh(%{sed -i 's/Xss180k/Xss280k/' /etc/cassandra/conf/cassandra-env.sh})
    sh(%{echo "api-server:api-server" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{echo "schema-transformer:schema-transformer" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{echo "svc-monitor:svc-monitor" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{echo "control-user:control-user-passwd" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{sed -i 's/911%(process_num)01d/5998/' /etc/contrail/supervisord_config_files/contrail-discovery.ini})
    sh(%{sed -i 's/91%(process_num)02d/8082/' /etc/contrail/supervisord_config_files/contrail-api.ini})
    sh(%{sed -i 's/# port=5998/port=5998/' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/# server=127.0.0.1/server=127.0.0.1/' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/# port=5998/port=5998/' /etc/contrail/contrail-collector.conf})
    sh(%{sed -i 's/# server=0.0.0.0/server=127.0.0.1/' /etc/contrail/contrail-collector.conf})
    sh(%{sed -i 's/# user=control-user/user=control-user/g' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/# password=control-user-passwd/password=control-user-passwd/' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/Xss180k/Xss280k/' /etc/cassandra/conf/cassandra-env.sh})

    # Fix webui config
    sh("ln -sf /usr/bin/nodejs /usr/bin/node", true) if !File.file? /usr/bin/node
    sh(%{sed -i "s/config.orchestration.Manager = 'openstack'/config.orchestration.Manager = 'none'/" /etc/contrail/config.global.js})
    sh(%{sed -i 's/8080/8070/' /etc/contrail/config.global.js})

    if @platform =~ /fedora/
        sh("service cassandra restart")
        sh("service zookeeper restart")<commit_msg>fix syntax issue<commit_after>#!/usr/bin/env ruby

# Use this script to install and provision contrail nodes.
# sudo ruby $PWD/contrail-kubernetes/scripts/opencontrail-install/contrail_install.rb

raise 'Must run with superuser privilages' unless Process.uid == 0

@ws="#{File.dirname($0)}"
require "#{@ws}/util"
STDOUT.sync = true

sh("\grep aurora /etc/hostname", true)
@controller_host = $?.to_i == 0 ? "aurora" : "kubernetes-master"

# Initialize default interfaces and user account names
# TODO Take via command line options
@intf = "eth0"
@user = "ubuntu"
if File.directory? "/vagrant" then
    @intf = "eth1"
    @user = "vagrant"
end

# Find platform OS
sh(%{\grep -i "ubuntu 14" /etc/issue 2>&1 > /dev/null}, true)
@platform = $?.to_i == 0 ? "ubuntu1404" : "fedora20"

@control_node_introspect_port = @controller_host == "aurora" ? 9083 : "8083"

require "#{@ws}/#{@platform}/install"

# Update ssh configuration
def ssh_setup
    conf=<<EOF
UserKnownHostsFile=/dev/null
StrictHostKeyChecking=no
LogLevel=QUIET
EOF
    sh("mkdir -p /root/.ssh")
    File.open("/root/.ssh/config", "a") { |fp| fp.puts(conf) }
    sh("chmod 600 /root/.ssh/config")

    # Add ssh config to ~@user also.
    sh("mkdir -p /home/#{@user}/.ssh")
    File.open("/home/#{@user}/.ssh/config", "a") { |fp| fp.puts(conf) }
    sh("chmod 600 /home/#{@user}/.ssh/config")
    sh("chown #{@user}.#{@user} /home/#{@user}/.ssh/config")
    sh("chown #{@user}.#{@user} /home/#{@user}/.ssh/.")
end

# Do initial setup
def initial_setup
    @resolvers = sh("\grep -w nameserver /etc/resolv.conf").split("\n")
    ssh_setup
    sh("service hostname restart", true) if @platform !~ /fedora/
#   @contrail_controller = IPSocket.getaddress(@controller_host)
    @contrail_controller =
        sh(%{grep #{@controller_host} /etc/hosts | awk '{print $1}'})
    error "Cannot resolve contrail-controller host" \
        if @contrail_controller.empty?

    # Make sure that localhost resolves to 127.0.0.1
    sh(%{\grep -q "127\.0\.0\.1.*localhost" /etc/hosts}, true)
    sh("echo 127.0.0.1 localhost >> /etc/hosts") if $?.to_i != 0

    Dir.chdir("#{@ws}")
end

def update_controller_etc_hosts
    # Update /etc/hosts with the IP address
    @controller_ip, mask, gw, prefix_len = get_intf_ip(@intf)

    rip = sh("\grep #{@controller_host} /etc/hosts | awk '{print $1}'", true)
    return if rip != "127.0.0.1" and !rip.empty?

    # If @controller_host resolves to 127.0.0.1, take it out of /etc/hosts
    sh("sed -i '/127.0.0.1 #{@controller_host}/d' /etc/hosts") \
        if rip == "127.0.0.1"
    sh("echo #{@controller_ip} #{@controller_host} >> /etc/hosts")
end

def verify_controller
    sh("netstat -anp | \grep LISTEN | \grep -w 5672", false, 10, 3) # RabbitMQ
    sh("netstat -anp | \grep LISTEN | \grep -w 2181", false, 10, 3) # ZooKeeper
    sh("netstat -anp | \grep LISTEN | \grep -w 9160", false, 10, 3) # Cassandra
    sh("netstat -anp |\grep LISTEN | \grep -w #{@control_node_introspect_port}",
       false, 10, 3) # Control-Node
    sh("netstat -anp | \grep LISTEN | \grep -w 5998", false, 10, 3) # discovery
    sh("netstat -anp | \grep LISTEN | \grep -w 6379", false, 10, 3) # redis
    sh("netstat -anp | \grep LISTEN | \grep -w 8443", false, 10, 3) # IFMAP
    sh("netstat -anp | \grep LISTEN | \grep -w 8082", false, 10, 3) # API-Server
    sh("netstat -anp | \grep LISTEN | \grep -w 8086", false, 10, 3) # Collector
    sh("netstat -anp | \grep LISTEN | \grep -w 8081", false, 10, 3) # OpServer
    sh("netstat -anp | \grep LISTEN | \grep -w 8143", false, 10, 3) # WebUI
    sh("netstat -anp | \grep LISTEN | \grep -w 8070", false, 10, 3) # WebUI

    puts "All contrail controller components up"
end

# Provision contrail-controller
def provision_contrail_controller
    update_controller_etc_hosts

    sh(%{sed -i 's/Xss180k/Xss280k/' /etc/cassandra/conf/cassandra-env.sh})
    sh(%{echo "api-server:api-server" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{echo "schema-transformer:schema-transformer" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{echo "svc-monitor:svc-monitor" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{echo "control-user:control-user-passwd" >> /etc/ifmap-server/basicauthusers.properties})
    sh(%{sed -i 's/911%(process_num)01d/5998/' /etc/contrail/supervisord_config_files/contrail-discovery.ini})
    sh(%{sed -i 's/91%(process_num)02d/8082/' /etc/contrail/supervisord_config_files/contrail-api.ini})
    sh(%{sed -i 's/# port=5998/port=5998/' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/# server=127.0.0.1/server=127.0.0.1/' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/# port=5998/port=5998/' /etc/contrail/contrail-collector.conf})
    sh(%{sed -i 's/# server=0.0.0.0/server=127.0.0.1/' /etc/contrail/contrail-collector.conf})
    sh(%{sed -i 's/# user=control-user/user=control-user/g' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/# password=control-user-passwd/password=control-user-passwd/' /etc/contrail/contrail-control.conf})
    sh(%{sed -i 's/Xss180k/Xss280k/' /etc/cassandra/conf/cassandra-env.sh})

    # Fix webui config
    if !File.file? "/usr/bin/node" then
        sh("ln -sf /usr/bin/nodejs /usr/bin/node", true)
    end
    sh(%{sed -i "s/config.orchestration.Manager = 'openstack'/config.orchestration.Manager = 'none'/" /etc/contrail/config.global.js})
    sh(%{sed -i 's/8080/8070/' /etc/contrail/config.global.js})

    if @platform =~ /fedora/
        sh("service cassandra restart")
        sh("service zookeeper restart")
{'fraction_non_alphanumeric': 0.1832640520513284, 'fraction_numerical': 0.04156876920296403, 'mean_word_length': 3.4144395692062224, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 20, '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': '1684489', 'n_tokens_mistral': 4624, 'n_tokens_neox': 4227, 'n_words': 941}