<commit_before>// Copyright (c) 2017 <NAME>
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/romana/core/common"
	"github.com/romana/core/common/api"
	"github.com/romana/core/common/client"

	"github.com/vishvananda/netlink"
)

const (
	DefaultRouteTableId = 10
)

func main() {
	var err error

	etcdEndpoints := flag.String("endpoints", "", "csv list of etcd endpoints to romana storage")
	etcdPrefix := flag.String("prefix", "", "string that prefixes all romana keys in etcd")
	hostname := flag.String("hostname", "", "name of the host in romana database")
	romanaRouteTableId := flag.Int("route-table-id", DefaultRouteTableId, "id that romana route table should have in /etc/iproute2/rt_tables")
	mock := flag.Bool("mock", false, "")
	flag.Parse()

	romanaConfig := common.Config{
		EtcdEndpoints: strings.Split(*etcdEndpoints, ","),
		EtcdPrefix:    *etcdPrefix,
	}

	if *hostname == "" {
		*hostname, err = os.Hostname()
		if err != nil {
			panic(err)
		}
	}

	var romanaClient RomanaClient
	if *mock {
		romanaClient = MockClient{}
	} else {

		rClient, err := client.NewClient(&romanaConfig)
		if err != nil {
			panic(err)
		}

		romanaClient = RomanaClientAdaptor{rClient}

	}

	err = ensureRouteTableExist(*romanaRouteTableId)
	if err != nil {
		panic(err)
	}

	err = ensureRomanaRouteRule(*romanaRouteTableId)
	if err != nil {
		panic(err)
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	blocksChannel := WatchBlocks(ctx, romanaClient)
	hostsChannel := WatchHosts(ctx, romanaClient)

	hosts := IpamHosts(romanaClient.ListHosts().Hosts)
	for {
		select {
		case blocks := <-blocksChannel:
			err := flushRomanaTable()
			if err != nil {
				fmt.Printf("failed to flush romana route table err=(%s)", err)
				continue
			}

			for _, block := range blocks {
				if block.Host == *hostname {
					log.Printf("Block %v is local and does not require a route on that host", block)
					continue
				}
<commit_msg>Enable sysctls nad gateway provisioning in simple agent<commit_after>// Copyright (c) 2017 Pani Networks
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/romana/core/common"
	"github.com/romana/core/common/api"
	"github.com/romana/core/common/client"
	log "github.com/romana/rlog"

	"github.com/vishvananda/netlink"
)

const (
	DefaultRouteTableId = 10
	DefaultGwIP         = "127.42.0.1"
)

func main() {
	var err error

	etcdEndpoints := flag.String("endpoints", "", "csv list of etcd endpoints to romana storage")
	etcdPrefix := flag.String("prefix", "", "string that prefixes all romana keys in etcd")
	hostname := flag.String("hostname", "", "name of the host in romana database")
	provisionIface := flag.Bool("provision-iface", false, "create romana-gw interface and ip")
	provisionSysctls := flag.Bool("provision-sysctls", false, "configure routing sysctls")
	romanaRouteTableId := flag.Int("route-table-id", DefaultRouteTableId, "id that romana route table should have in /etc/iproute2/rt_tables")
	mock := flag.Bool("mock", false, "")
	flag.Parse()

	romanaConfig := common.Config{
		EtcdEndpoints: strings.Split(*etcdEndpoints, ","),
		EtcdPrefix:    *etcdPrefix,
	}

	if *hostname == "" {
		*hostname, err = os.Hostname()
		if err != nil {
			panic(err)
		}
	}

	var romanaClient RomanaClient
	if *mock {
		romanaClient = MockClient{}
	} else {

		rClient, err := client.NewClient(&romanaConfig)
		if err != nil {
			panic(err)
		}

		romanaClient = RomanaClientAdaptor{rClient}

	}

	hosts := IpamHosts(romanaClient.ListHosts().Hosts)
	if *provisionIface {
		err := CreateRomanaGW()
		if err != nil {
			log.Infof("Failed to create romana-gw interface. %s", err)
			os.Exit(2)
		}

		err = SetRomanaGwIP(DefaultGwIP)
		if err != nil {
			log.Infof("Failed to install ip address on romana-gw interface. %s", err)
			os.Exit(2)
		}

	}

	if *provisionSysctls {
		err := ProvisionSysctls()
		if err != nil {
			log.Infof("Failed to provision systls %s", err)
			os.Exit(2)
		}
	}

	err = ensureRouteTableExist(*romanaRouteTableId)
	if err != nil {
		panic(err)
	}

	err = ensureRomanaRouteRule(*romanaRouteTableId)
	if err != nil {
		panic(err)
	}

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	blocksChannel := WatchBlocks(ctx, romanaClient)
	hostsChannel := WatchHosts(ctx, romanaClient)

	for {
		select {
		case blocks := <-blocksChannel:
			err := flushRomanaTable()
			if err != nil {
				fmt.Printf("failed to flush romana route table err=(%s)", err)
				continue
			}

			for _, block := range blocks {
				if block.Host == *hostname {
					log.Printf("Block %v is local and does not require a route on that host", block)
					continue
				}

{'fraction_non_alphanumeric': 0.12736572890025574, 'fraction_numerical': 0.005456095481670929, 'mean_word_length': 3.7306451612903224, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 1, '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': '2316228', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1926, 'n_words': 570}