Пример #1
0
func main() {
	log.SetLevel(log.FatalLevel)

	var coordinator, responseAddr, taskName string
	var taskArgs []string
	flags.StringVarP(&coordinator, "coordinator_url", "c", "", "url of the coordinator")
	flags.StringVarP(&taskName, "task", "t", "", "task to run")
	flags.StringSliceVarP(&taskArgs, "request_arg", "a", []string{}, fmt.Sprintf("task specific argument the form 'key%svalue'. can be set multiple times", argSep))
	flags.StringVarP(&responseAddr, "response_addr", "r", ":4080", "address for response http handler to listen on")
	flags.Parse()

	args, err := parseTaskArgs(taskArgs)
	dieOnError(err)

	result, stream, respErr, err := startResponseServer(responseAddr)
	dieOnError(err)

	dieOnError(makeRequest(coordinator, taskName, responseAddr, args))

	select {
	case err := <-respErr:
		dieOnError(err)
	case result := <-result:
		j, _ := json.Marshal(result)
		fmt.Println(string(j))
	case stream := <-stream:
		dieOnError(acomm.Stream(os.Stdout, stream))
	}
}
Пример #2
0
func initFlags() {
	flag.IntVarP(&options.Port, "port", "p", 80, "The port to listen on")
	flag.StringVarP(&options.StaticDir, "www", "w", ".", "Directory to serve static files from")
	flag.StringVar(&options.StaticPrefix, "www-prefix", "/", "Prefix to serve static files on")
	flag.DurationVar(&options.StaticCacheMaxAge, "max-age", 0, "Set the Cache-Control header for static content with the max-age set to this value, e.g. 24h. Must confirm to http://golang.org/pkg/time/#ParseDuration")
	flag.StringVarP(&options.DefaultPage, "default-page", "d", "", "Default page to send if page not found")
	flag.VarP(&options.Services, "service", "s", "The Kubernetes services to proxy to in the form \"<prefix>=<serviceUrl>\"")
	flag.VarP(&options.Configs, "config-file", "c", "The configuration files to create in the form \"<template>=<output>\"")
	flag.Var(&options.CACerts, "ca-cert", "CA certs used to verify proxied server certificates")
	flag.StringVar(&options.TlsCertFile, "tls-cert", "", "Certificate file to use to serve using TLS")
	flag.StringVar(&options.TlsKeyFile, "tls-key", "", "Certificate file to use to serve using TLS")
	flag.BoolVar(&options.SkipCertValidation, "skip-cert-validation", false, "Skip remote certificate validation - dangerous!")
	flag.BoolVarP(&options.AccessLogging, "access-logging", "l", false, "Enable access logging")
	flag.BoolVar(&options.CompressHandler, "compress", false, "Enable gzip/deflate response compression")
	flag.BoolVar(&options.FailOnUnknownServices, "fail-on-unknown-services", false, "Fail on unknown services in DNS")
	flag.BoolVar(&options.ServeWww, "serve-www", true, "Whether to serve static content")
	flag.Parse()
}
Пример #3
0
func main() {

	// Command line options
	var logLevel string
	flag.StringVarP(&logLevel, "log-level", "l", "info", "log level: debug/info/warning/error/critical/fatal")
	flag.Parse()

	// Logging
	if err := logx.DefaultSetup(logLevel); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "logx.DefaultSetup",
		}).Fatal("Could not set up logrus")
	}

	// Write logs to this directory
	testDir = "cdhcpd-integration-test-" + uuid.New()
	if err := os.Mkdir(testDir, 0755); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "os.Mkdir",
			"path":  testDir,
		}).Fatal("Could not create directory for test logs")
	}
	hconfPath = testDir + "/hypervisors.conf"
	gconfPath = testDir + "/guests.conf"

	// From now on, write the logs from this script to a file in the test directory as well
	var err error
	selfLog, err = os.Create(testDir + "/integrationtest.log")
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "os.Open",
		}).Fatal("Could not open self-log file for writing")
	}
	defer func() {
		if err := selfLog.Sync(); err != nil {
			fmt.Println("Could not sync self-log file")
		}
		if err := selfLog.Close(); err != nil {
			fmt.Println("Could not close self-log file")
			os.Exit(1)
		}
	}()
	log.SetOutput(selfLog)

	// Set up report and global ok
	r := &bytes.Buffer{}
	_, _ = r.WriteString("cdhcpd Integration Test Results\n")
	_, _ = r.WriteString("==================================\n")
	testOk = true

	// Start up processes
	log.Info("Starting etcd")
	ep := newTestProcess("etcd", exec.Command("etcd", "--data-dir", testDir+"/data.etcd",
		"--listen-client-urls", etcdClientAddress,
		"--listen-peer-urls", etcdPeerAddress,
		"--initial-advertise-peer-urls", etcdPeerAddress,
		"--initial-cluster", "default="+etcdPeerAddress,
		"--advertise-client-urls", etcdClientAddress,
	))
	if err := ep.captureOutput(true); err != nil {
		cleanupAfterError(err, "testProcess.captureOutput", r, nil, ep, nil)
	}
	if err := ep.start(); err != nil {
		cleanupAfterError(err, "testProcess.start", r, nil, ep, nil)
	}
	log.Info("Starting cdhcpd")
	dp := newTestProcess("cdhcpd", exec.Command("cdhcpd", "-e", etcdClientAddress,
		"-d", "example.com",
		"-l", logLevel,
		"-c", testDir,
	))
	if err := dp.captureOutput(true); err != nil {
		if err := ep.finish(); err != nil {
			log.Error("Could not close out etcd")
		}
		cleanupAfterError(err, "testProcess.captureOutput", r, nil, ep, dp)
	}
	if err := dp.start(); err != nil {
		if err := ep.finish(); err != nil {
			log.Error("Could not close out etcd")
		}
		cleanupAfterError(err, "testProcess.start", r, nil, ep, dp)
	}

	// Begin test
	log.Info("Running test")
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "on start", "not present", "not present"); !ok {
		log.Warning("Failure testing conf status on start")
		testOk = false
	}

	// Set up context
	e := etcd.NewClient([]string{etcdClientAddress})
	c := lochness.NewContext(e)

	// Roughly follow the steps of the demo's guest-setup.sh
	hs := make(map[string]*lochness.Hypervisor)
	gs := make(map[string]*lochness.Guest)

	// Basic setup
	f, err := testhelper.NewFlavor(c, 1, 512, 1024)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewFlavor", r, e, ep, dp)
	}
	n, err := testhelper.NewNetwork(c)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewNetwork", r, e, ep, dp)
	}
	fw, err := testhelper.NewFirewallGroup(c)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewFirewallGroup", r, e, ep, dp)
	}
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after setup", "created", "created"); !ok {
		log.Warning("Failure testing conf status after setup")
		testOk = false
	}

	// Add a subnet
	s, err := testhelper.NewSubnet(c, "192.168.100.0/24", net.IPv4(192, 168, 100, 1), net.IPv4(192, 168, 100, 10), net.IPv4(192, 168, 100, 200), n)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewSubnet", r, e, ep, dp)
	}
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after subnet creation", "not touched", "not touched"); !ok {
		log.Warning("Failure testing conf status after subnet creation")
		testOk = false
	}

	// Add a hypervisor
	h, err := testhelper.NewHypervisor(c, "fe:dc:ba:98:76:54", net.IPv4(192, 168, 100, 200), net.IPv4(192, 168, 100, 1), net.IPv4(255, 255, 255, 0), "br0", s)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewHypervisor", r, e, ep, dp)
	}
	hs[h.ID] = h
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after hypervisor creation", "changed", "not touched"); !ok {
		log.Warning("Failure testing conf status after hypervisor creation")
		testOk = false
	}
	if ok := reportHasHosts(r, "after hypervisor creation", hs, gs); !ok {
		log.Warning("Failure testing for hosts in confs after hypervisor creation")
		testOk = false
	}

	// Create a guest, without setting the subnet or hypervisor (will NOT appear in the conf)
	g, err := testhelper.NewGuest(c, "A4:75:C1:6B:E3:49", n, nil, f, fw, nil)
	if err != nil {
		cleanupAfterError(err, "testhelper.NewGuest", r, e, ep, dp)
	}
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after guest creation", "not touched", "not touched"); !ok {
		log.Warning("Failure testing conf status after guest creation")
		testOk = false
	}
	if ok := reportHasHosts(r, "after guest creation", hs, gs); !ok {
		log.Warning("Failure testing for hosts in confs after guest creation")
		testOk = false
	}

	// Add the guest to the hypervisor (would normally be performed by cplacerd, having pulled it from the queue)
	if err := h.AddGuest(g); err != nil {
		cleanupAfterError(err, "hypervisor.AddGuest", r, e, ep, dp)
	}
	gs[g.ID] = g
	time.Sleep(time.Second)
	if ok := reportConfStatus(r, "after adding guest to hypervisor", "not touched", "changed"); !ok {
		log.Warning("Failure testing conf status after adding guest to hypervisor")
		testOk = false
	}
	if ok := reportHasHosts(r, "after adding guest to hypervisor", hs, gs); !ok {
		log.Warning("Failure testing for hosts in confs after adding guest to hypervisor")
		testOk = false
	}

	// Sleep for a few seconds to make sure everything finished, then clean up
	time.Sleep(2 * time.Second)
	log.WithField("path", testDir).Info("Creating test output directory")
	showTestStatus(true)
	cleanup(r, e, ep, dp)
}
Пример #4
0
func init() {
	pflag.DurationVarP(&timeoutDur, "timeout", "t", 10*time.Second, "timeout for the command to complete")
	pflag.StringVarP(&username, "user", "u", "root", "user to login with")
	pflag.StringSliceVarP(&hosts, "hosts", "h", nil, "hosts to run command on")
}
Пример #5
0
func main() {
	var ipAddress, domainName, port string
	// define flags passed at runtime, and assign them to the variables defined above
	flag.StringVarP(&ipAddress, "ip", "i", "", "IP Address")
	flag.StringVarP(&domainName, "domain", "d", "", "Domain Name")
	flag.StringVarP(&port, "port", "p", "443", "Port Number")

	flag.Parse()
	if domainName == "" {
		Usage()
		os.Exit(1)
	}
	if ipAddress == "" {
		ip, err := net.LookupHost(domainName)
		if err != nil {
			fmt.Printf("Could not resolve domain name, %v.\n\n", domainName)
			fmt.Printf("Either supply a valid domain name or use the -i switch to supply the ip address.\n")
			fmt.Printf("Domain name lookups are not performed when the user provides the ip address.\n")
			os.Exit(1)
		}
		ipAddress = ip[0] + ":" + port
	} else {
		ipAddress = ipAddress + ":" + port
	}
	//Connect network
	ipConn, err := net.DialTimeout("tcp", ipAddress, 60000*time.Millisecond)
	if err != nil {
		fmt.Printf("Could not connect to %v - %v\n", ipAddress, domainName)
		os.Exit(1)
	} else {
		defer ipConn.Close()
	}
	// Configure tls to look at domainName
	config := tls.Config{ServerName: domainName}
	// Connect to tls
	conn := tls.Client(ipConn, &config)
	defer conn.Close()
	// Handshake with TLS to get cert
	hsErr := conn.Handshake()
	if hsErr != nil {
		fmt.Printf("Client connected to: %v\n", conn.RemoteAddr())
		fmt.Printf("Cert Failed for %v - %v\n", ipAddress, domainName)
		os.Exit(1)
	} else {
		fmt.Printf("Client connected to: %v\n", conn.RemoteAddr())
		fmt.Printf("Cert Checks OK\n")
	}
	state := conn.ConnectionState()
	for i, v := range state.PeerCertificates {
		switch i {
		case 0:
			fmt.Println("Server key information:")
			switch v.Version {
			case 3:
				fmt.Printf("\tVersion: TLS v1.2\n")
			case 2:
				fmt.Printf("\tVersion: TLS v1.1\n")
			case 1:
				fmt.Printf("\tVersion: TLS v1.0\n")
			case 0:
				fmt.Printf("\tVersion: SSL v3\n")
			}
			fmt.Printf("\tCN:\t %v\n\tOU:\t %v\n\tOrg:\t %v\n", v.Subject.CommonName, v.Subject.OrganizationalUnit, v.Subject.Organization)
			fmt.Printf("\tCity:\t %v\n\tState:\t %v\n\tCountry: %v\n", v.Subject.Locality, v.Subject.Province, v.Subject.Country)
			fmt.Printf("SSL Certificate Valid:\n\tFrom:\t %v\n\tTo:\t %v\n", v.NotBefore, v.NotAfter)
			fmt.Printf("Valid Certificate DNS:\n")
			if len(v.DNSNames) >= 1 {
				for dns := range v.DNSNames {
					fmt.Printf("\t%v\n", v.DNSNames[dns])
				}
			} else {
				fmt.Printf("\t%v\n", v.Subject.CommonName)
			}
		case 1:
			fmt.Printf("Issued by:\n\t%v\n\t%v\n\t%v\n", v.Subject.CommonName, v.Subject.OrganizationalUnit, v.Subject.Organization)
		default:
			break
		}
	}

}
Пример #6
0
func main() {

	// Command line options
	var etcdAddress, domain, confPath, logLevel string
	flag.StringVarP(&domain, "domain", "d", "", "domain for lochness; required")
	flag.StringVarP(&etcdAddress, "etcd", "e", "http://127.0.0.1:4001", "address of etcd server")
	flag.StringVarP(&confPath, "conf-dir", "c", "/etc/dhcp/", "dhcpd configuration directory")
	flag.StringVarP(&logLevel, "log-level", "l", "warning", "log level: debug/info/warning/error/critical/fatal")
	flag.Parse()

	// Domain is required
	if domain == "" {
		flag.PrintDefaults()
		os.Exit(1)
	}

	// Logging
	if err := logx.DefaultSetup(logLevel); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "logx.DefaultSetup",
		}).Fatal("Could not set up logrus")
	}

	hconfPath := path.Join(confPath, "hypervisors.conf")
	gconfPath := path.Join(confPath, "guests.conf")

	// Set up fetcher and refresher
	f := NewFetcher(etcdAddress)
	r := NewRefresher(domain)
	err := f.FetchAll()
	if err != nil {
		os.Exit(1)
	}

	// Update at the start of each run
	restart, err := updateConfigs(f, r, hconfPath, gconfPath)
	if restart {
		restartDhcpd()
	}
	if err != nil {
		os.Exit(1)
	}

	// Create the watcher
	w, err := watcher.New(f.etcdClient)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "watcher.New",
		}).Fatal("Could not create watcher")
	}

	// Start watching the necessary etcd prefixs
	prefixes := [...]string{"/lochness/hypervisors", "/lochness/guests", "/lochness/subnets"}
	for _, prefix := range prefixes {
		if err := w.Add(prefix); err != nil {
			log.WithFields(log.Fields{
				"error":  err,
				"func":   "watcher.Add",
				"prefix": prefix,
			}).Fatal("Could not add watch prefix")
		}
	}

	// Channel for indicating work in progress
	// (to coordinate clean exiting between the consumer and the signal handler)
	ready := make(chan struct{}, 1)
	ready <- struct{}{}

	for w.Next() {
		// Remove item to indicate processing has begun
		done := <-ready

		// Integrate the response and update the configs if necessary
		refresh, err := f.IntegrateResponse(w.Response())
		if err != nil {
			log.Info("Error on integration; re-fetching")
			err := f.FetchAll()
			if err != nil {
				os.Exit(1)
			}
			refresh = true
		}
		if refresh {
			restart, err := updateConfigs(f, r, hconfPath, gconfPath)
			if restart {
				restartDhcpd()
			}
			if err != nil {
				log.WithFields(log.Fields{
					"error": err,
					"func":  "updateConfigs",
				}).Warn("Could not create watcher")
			}
		}

		// Return item to indicate processing has completed
		ready <- done
	}
	if err := w.Err(); err != nil {
		log.WithField("error", err).Fatal("Watcher encountered an error")
	}

	// Handle signals for clean shutdown
	sigs := make(chan os.Signal)
	signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)

	s := <-sigs
	log.WithField("signal", s).Info("Signal received; waiting for current task to process")
	<-ready // wait until any current processing is finished
	_ = w.Close()
	log.Info("Exiting")
}