<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"runtime"
	"strings"
	"syscall"

	"github.com/hyperhq/hyper/daemon"
	"github.com/hyperhq/hyper/docker"
	"github.com/hyperhq/hyper/engine"
	"github.com/hyperhq/hyper/lib/docker/pkg/reexec"
	"github.com/hyperhq/hyper/utils"
	"github.com/hyperhq/runv/driverloader"
	"github.com/hyperhq/runv/hypervisor"
	"github.com/hyperhq/runv/lib/glog"

	"github.com/Unknwon/goconfig"
)

func main() {
	if reexec.Init() {
		return
	}

	fnd := flag.Bool("nondaemon", false, "Not daemonize")
	flDisableIptables := flag.Bool("noniptables", false, "Don't enable iptables rules")
	flConfig := flag.String("config", "", "Config file for hyperd")
	flHost := flag.String("host", "", "Host for hyperd")
	flHelp := flag.Bool("help", false, "Print help message for Hyperd daemon")
	glog.Init()
	flag.Usage = func() { printHelp() }
	flag.Parse()
	if *flHelp == true {
		printHelp()
		return
	}

	if !*fnd {
		cmd, err := exec.LookPath(os.Args[0])
		if err != nil {
			fmt.Println("cannot find path of arg0 ", os.Args[0])
			os.Exit(-1)
		}
		cmd, err = filepath.Abs(cmd)
		if err != nil {
			fmt.Println("cannot find absolute path of arg0 ", os.Args[0])
			os.Exit(-1)
		}

		pid, err := utils.Daemonize()
		if err != nil {
			fmt.Println("faile to daemonize hyperd")
			os.Exit(-1)
		}
		if pid > 0 {
			return
		}

		err = syscall.Exec(cmd, append(os.Args, "--nondaemon"), os.Environ())
		if err != nil {
			fmt.Println("fail to exec in nondaemon mode: ", err.Error())
			os.Exit(-1)
		}

		return
	}

	mainDaemon(*flConfig, *flHost, *flDisableIptables)
}

func printHelp() {
	var helpMessage = `Usage:
  %s [OPTIONS]

Application Options:
  --nondaemon            Not daemonize
  --config=""            Configuration for %s
  --v=0                  Log level fro V logs
  --log_dir              Log directory
  --host                 Host address and port for hyperd(such as --host=tcp://127.0.0.1:12345)
  --logtostderr          Log to standard error instead of files
  --alsologtostderr      Log to standard error as well as files

Help Options:
  -h, --help             Show this help message

`
	fmt.Printf(helpMessage, os.Args[0], os.Args[0])
}

func mainDaemon(config, host string, flDisableIptables bool) {
	glog.V(1).Infof("The config file is %s", config)
	if config == "" {
		config = "/etc/hyper/config"
	}
	if _, err := os.Stat(config); err != nil {
		if os.IsNotExist(err) {
			glog.Errorf("Can not find config file(%s)", config)
			return
		}
		glog.Errorf(err.Error())
		return
	}

	os.Setenv("HYPER_CONFIG", config)
	cfg, err := goconfig.LoadConfigFile(config)
	if err != nil {
		glog.Errorf("Read config file (%s) failed, %s", config, err.Error())
		return
	}
	driver, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Hypervisor")
	if runtime.GOOS == "darwin" {
		driver = "vbox"
	} else {
		if driver == "" {
			// We need to provide a defaut exec drvier
			var drivers = []string{"kvm", "xen", "vbox"}
			driver = utils.GetAvailableDriver(drivers)
		}
		driver = strings.ToLower(driver)
	}
	if driver == "" || (driver != "kvm" && driver != "xen" && driver != "vbox") {
		fmt.Printf("Please specify the exec driver, such as 'kvm', 'xen' or 'vbox'\n")
		return
	}
	hyperRoot, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Root")
	iso, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Vbox")

	if hyperRoot == "" {
		hyperRoot = "/var/lib/hyper"
	}
	if _, err := os.Stat(hyperRoot); err != nil {
		if err := os.MkdirAll(hyperRoot, 0755); err != nil {
			glog.Errorf(err.Error())
			return
		}
	}

	utils.SetHyperEnv(fmt.Sprintf("%s/.hyperconfig", os.Getenv("HOME")), hyperRoot, iso)
	eng := engine.New(config)
	docker.Init()

	d, err := daemon.NewDaemon(eng)
	if err != nil {
		glog.Errorf("The hyperd create failed, %s\n", err.Error())
		return
	}

	if hypervisor.HDriver, err = driverloader.Probe(driver); err != nil {
		glog.Errorf("%s\n", err.Error())
		return
	}
	d.Hypervisor = driver

	disableIptables := cfg.MustBool(goconfig.DEFAULT_SECTION, "DisableIptables", false)
	if err = hypervisor.InitNetwork(d.BridgeIface, d.BridgeIP, disableIptables || flDisableIptables); err != nil {
		glog.Errorf("InitNetwork failed, %s\n", err.Error())
		return
	}<commit_msg>probe the hypervisor's driver to get default driver<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"runtime"
	"strings"
	"syscall"

	"github.com/hyperhq/hyper/daemon"
	"github.com/hyperhq/hyper/docker"
	"github.com/hyperhq/hyper/engine"
	"github.com/hyperhq/hyper/lib/docker/pkg/reexec"
	"github.com/hyperhq/hyper/utils"
	"github.com/hyperhq/runv/driverloader"
	"github.com/hyperhq/runv/hypervisor"
	"github.com/hyperhq/runv/lib/glog"

	"github.com/Unknwon/goconfig"
)

func main() {
	if reexec.Init() {
		return
	}

	fnd := flag.Bool("nondaemon", false, "Not daemonize")
	flDisableIptables := flag.Bool("noniptables", false, "Don't enable iptables rules")
	flConfig := flag.String("config", "", "Config file for hyperd")
	flHost := flag.String("host", "", "Host for hyperd")
	flHelp := flag.Bool("help", false, "Print help message for Hyperd daemon")
	glog.Init()
	flag.Usage = func() { printHelp() }
	flag.Parse()
	if *flHelp == true {
		printHelp()
		return
	}

	if !*fnd {
		cmd, err := exec.LookPath(os.Args[0])
		if err != nil {
			fmt.Println("cannot find path of arg0 ", os.Args[0])
			os.Exit(-1)
		}
		cmd, err = filepath.Abs(cmd)
		if err != nil {
			fmt.Println("cannot find absolute path of arg0 ", os.Args[0])
			os.Exit(-1)
		}

		pid, err := utils.Daemonize()
		if err != nil {
			fmt.Println("faile to daemonize hyperd")
			os.Exit(-1)
		}
		if pid > 0 {
			return
		}

		err = syscall.Exec(cmd, append(os.Args, "--nondaemon"), os.Environ())
		if err != nil {
			fmt.Println("fail to exec in nondaemon mode: ", err.Error())
			os.Exit(-1)
		}

		return
	}

	mainDaemon(*flConfig, *flHost, *flDisableIptables)
}

func printHelp() {
	var helpMessage = `Usage:
  %s [OPTIONS]

Application Options:
  --nondaemon            Not daemonize
  --config=""            Configuration for %s
  --v=0                  Log level fro V logs
  --log_dir              Log directory
  --host                 Host address and port for hyperd(such as --host=tcp://127.0.0.1:12345)
  --logtostderr          Log to standard error instead of files
  --alsologtostderr      Log to standard error as well as files

Help Options:
  -h, --help             Show this help message

`
	fmt.Printf(helpMessage, os.Args[0], os.Args[0])
}

func mainDaemon(config, host string, flDisableIptables bool) {
	glog.V(1).Infof("The config file is %s", config)
	if config == "" {
		config = "/etc/hyper/config"
	}
	if _, err := os.Stat(config); err != nil {
		if os.IsNotExist(err) {
			glog.Errorf("Can not find config file(%s)", config)
			return
		}
		glog.Errorf(err.Error())
		return
	}

	os.Setenv("HYPER_CONFIG", config)
	cfg, err := goconfig.LoadConfigFile(config)
	if err != nil {
		glog.Errorf("Read config file (%s) failed, %s", config, err.Error())
		return
	}

	hyperRoot, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Root")
	iso, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Vbox")

	if hyperRoot == "" {
		hyperRoot = "/var/lib/hyper"
	}
	if _, err := os.Stat(hyperRoot); err != nil {
		if err := os.MkdirAll(hyperRoot, 0755); err != nil {
			glog.Errorf(err.Error())
			return
		}
	}

	utils.SetHyperEnv(fmt.Sprintf("%s/.hyperconfig", os.Getenv("HOME")), hyperRoot, iso)
	eng := engine.New(config)
	docker.Init()

	d, err := daemon.NewDaemon(eng)
	if err != nil {
		glog.Errorf("The hyperd create failed, %s\n", err.Error())
		return
	}

	driver, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Hypervisor")
	if runtime.GOOS == "darwin" {
		driver = "vbox"
		if hypervisor.HDriver, err = driverloader.Probe(driver); err != nil {
			glog.Errorf("%s\n", err.Error())
			return
		}
	} else {
		if driver == "" {
			// We need to provide a defaut exec drvier
			var drivers = []string{"xen", "kvm", "vbox"}
			for _, dri := range drivers {
				driver = strings.ToLower(dri)
				if hypervisor.HDriver, err = driverloader.Probe(driver); err != nil {
					glog.Warningf("%s\n", err.Error())
					continue
				} else {
					break
				}
			}
		}
		if driver == "" || (driver != "kvm" && driver != "xen" && driver != "vbox") {
			glog.Errorf("Please specify the exec driver, such as 'kvm', 'xen' or 'vbox'\n")
			return
		}
	}
	d.Hypervisor = driver
	glog.Infof("The hypervisor's driver is %s", driver)

	disableIptables := cfg.MustBool(goconfig.DEFAULT_SECTION, "DisableIptables", false)
	if err = hypervisor.InitNetwork(d.BridgeIface, d.BridgeIP, disableIptables || flDisableIptables); err != nil {
		glog.Errorf("InitNetwork failed, %s\n", err.Error())
		return
	}
{'fraction_non_alphanumeric': 0.16350331580150926, 'fraction_numerical': 0.0066316030185227534, 'mean_word_length': 3.3025086079685195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3647410', 'n_tokens_mistral': 3530, 'n_tokens_neox': 3265, 'n_words': 750}