<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/fsouza/go-dockerclient"
	"github.com/gin-gonic/gin"
)

//APIBASE The base of the API that this agent provides
const APIBASE = "/api/1"

//Containers representing information about the running containers
type Containers struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Command string `json:"command"`
	Port    string `json:"port"`
}

//Post represents a post to the server
type Post struct {
	AgentID             string       `json:"agentid"`
	ExpiredAfterSeconds int          `json:"expireAfterSeconds"`
	Containers          []Containers `json:"containers"`
}

var (
	serverURL          string
	agentID            string
	refreshRate        int
	expireAfterSeconds int
	port               string
)

func init() {
	flag.StringVar(&serverURL, "server", "http://localhost:8989", "The server uri where dockmaster is located.")
	flag.StringVar(&agentID, "agent", "localhost", "The name of an Agent. Example: TestQA1.")
	flag.IntVar(&refreshRate, "refresh", 60, "The rate at which this agent should check for changes in seconds.")
	flag.IntVar(&expireAfterSeconds, "expireAfterSeconds", 60, "The rate at which data sent by this agent should expire in seconds.")
	flag.StringVar(&port, "port", "9999", "The port number on which this agent is running on.")

	flag.Parse()
	go startDiscovering()
}

func main() {
	router := gin.Default()
	v1 := router.Group(APIBASE)
	{
		v1.POST("/stop", stopContainer)
		v1.POST("/stopAll", stopAllContainers)
		v1.GET("/inspect/:id", inspectContainer)
	}
	router.Run(":" + port)
}

func stopContainer(c *gin.Context) {
	var container struct {
		ID string `json:"id"`
	}

	//reflect.ValueOf(&t).MethodByName("Foo").Call([]reflect.Value{})
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)
	runningContainers, err := client.ListContainers(docker.ListContainersOptions{All: false})

	for _, v := range runningContainers {
		if v.ID == container.ID {
			err = client.StopContainer(v.ID, 1)
			if err != nil {
				e := ErrorResponse{}
				e.ErrorMessage = "error stopping containers:" + err.Error()
				c.JSON(http.StatusInternalServerError, e)
				return
			}
			m := Message{}
			m.Message = "continer stopped successfully"
			c.JSON(http.StatusOK, m)
		}
	}

	e := ErrorResponse{}
	e.ErrorMessage = "container could not be found!"
	c.JSON(http.StatusNotFound, e)
}

func stopAllContainers(c *gin.Context) {

	var stopErrors [][]string
	for i := range stopErrors {
		stopErrors[i] = make([]string, 0)
	}
	//reflect.ValueOf(&t).MethodByName("Foo").Call([]reflect.Value{})
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)
	runningContainers, err := client.ListContainers(docker.ListContainersOptions{All: false})

	if err != nil {
		e := ErrorResponse{}
		e.ErrorMessage = "error getting all containers:" + err.Error()
		c.JSON(http.StatusInternalServerError, e)
		return
	}

	for _, v := range runningContainers {
		err := client.StopContainer(v.ID, 1)
		if err != nil {
			stopErrors = append(stopErrors, v.Names)
		}
	}

	if len(stopErrors) != 0 {
		var errCon string
		for _, v := range stopErrors {
			errCon += "[" + strings.Join(v, "|") + "]"
		}
		e := ErrorResponse{}
		e.ErrorMessage = "error stopping containers:" + errCon
		c.JSON(http.StatusInternalServerError, e)
		return
	}

	m := Message{}
	m.Message = "all containers successfully stopped"
	c.JSON(http.StatusOK, m)
}

func inspectContainer(c *gin.Context) {
	// cID := c.Param("id")
	// endpoint := "unix:///var/run/docker.sock"
	// client, _ := docker.NewClient(endpoint)
	c.JSON(http.StatusNotImplemented, nil)
}

func startDiscovering() {
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)
	log.Println("Started listening... Refresh rate is :", refreshRate)
	for {
		log.Println("Assembling Post...")
		post := Post{AgentID: agentID, ExpiredAfterSeconds: expireAfterSeconds}
		containers := []Containers{}
		runningContainers, err := client.ListContainers(docker.ListContainersOptions{All: false})
		if err != nil {
			panic("Failed to connect to Docker Client." + err.Error())
		}
		for _, v := range runningContainers {
			c := Containers{}
			c.ID = v.ID
			c.Name = strings.Join(v.Names, ",")
			for _, p := range v.Ports {
				c.Port += p.IP + ":" + p.Type + ":" + strconv.Itoa(int(p.PrivatePort)) + ":" + strconv.Itoa(int(p.PublicPort))
			}
			c.Command = v.Command
			containers = append(containers, c)
		}
		post.Containers = containers

		postString, err := json.Marshal(post)
		if err != nil {
			log.Println("Error occured while trying ot marshal POST:", err.Error())
		}
		req, err := http.NewRequest("POST", serverURL+"/api/1/add", bytes.NewBuffer(postString))
		if err != nil {
			log.Println("Failed to create post request... Trying again later.")
			time.Sleep(time.Second * time.Duration(refreshRate))
			continue
		}

		req.Header.Set("Content-Type", "application/json")
		log.Println("Posting JSON:", string(postString))
		client := &http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			log.Println("Failed to receive from server... Trying again later.")
			time.Sleep(time.Second * time.Duration(refreshRate))
			continue
		}
		defer resp.Body.Close()
		//TODO: Verify the response
		time.Sleep(time.Second * time.Duration(refreshRate))
	}
}
<commit_msg>Implemented inspect and fixed stopContainer as there was no need for the loop.<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/fsouza/go-dockerclient"
	"github.com/gin-gonic/gin"
)

//APIBASE The base of the API that this agent provides
const APIBASE = "/api/1"

//Containers representing information about the running containers
type Containers struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Command string `json:"command"`
	Port    string `json:"port"`
}

//Post represents a post to the server
type Post struct {
	AgentID             string       `json:"agentid"`
	ExpiredAfterSeconds int          `json:"expireAfterSeconds"`
	Containers          []Containers `json:"containers"`
}

var (
	serverURL          string
	agentID            string
	refreshRate        int
	expireAfterSeconds int
	port               string
)

func init() {
	flag.StringVar(&serverURL, "server", "http://localhost:8989", "The server uri where dockmaster is located.")
	flag.StringVar(&agentID, "agent", "localhost", "The name of an Agent. Example: TestQA1.")
	flag.IntVar(&refreshRate, "refresh", 60, "The rate at which this agent should check for changes in seconds.")
	flag.IntVar(&expireAfterSeconds, "expireAfterSeconds", 60, "The rate at which data sent by this agent should expire in seconds.")
	flag.StringVar(&port, "port", "9999", "The port number on which this agent is running on.")

	flag.Parse()
	go startDiscovering()
}

func main() {
	router := gin.Default()
	v1 := router.Group(APIBASE)
	{
		v1.POST("/stop", stopContainer)
		v1.POST("/stopAll", stopAllContainers)
		v1.GET("/inspect/:id", inspectContainer)
	}
	router.Run(":" + port)
}

func stopContainer(c *gin.Context) {
	var container struct {
		ID string `json:"id"`
	}

	//reflect.ValueOf(&t).MethodByName("Foo").Call([]reflect.Value{})
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)

	err := client.StopContainer(container.ID, 1)
	if err != nil {
		e := ErrorResponse{}
		e.ErrorMessage = "error stopping containers:" + err.Error()
		c.JSON(http.StatusInternalServerError, e)
		return
	}
	m := Message{}
	m.Message = "continer stopped successfully"
	c.JSON(http.StatusOK, m)
}

func stopAllContainers(c *gin.Context) {

	var stopErrors [][]string
	for i := range stopErrors {
		stopErrors[i] = make([]string, 0)
	}
	//reflect.ValueOf(&t).MethodByName("Foo").Call([]reflect.Value{})
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)
	runningContainers, err := client.ListContainers(docker.ListContainersOptions{All: false})

	if err != nil {
		e := ErrorResponse{}
		e.ErrorMessage = "error getting all containers:" + err.Error()
		c.JSON(http.StatusInternalServerError, e)
		return
	}

	for _, v := range runningContainers {
		err := client.StopContainer(v.ID, 1)
		if err != nil {
			stopErrors = append(stopErrors, v.Names)
		}
	}

	if len(stopErrors) != 0 {
		var errCon string
		for _, v := range stopErrors {
			errCon += "[" + strings.Join(v, "|") + "]"
		}
		e := ErrorResponse{}
		e.ErrorMessage = "error stopping containers:" + errCon
		c.JSON(http.StatusInternalServerError, e)
		return
	}

	m := Message{}
	m.Message = "all containers successfully stopped"
	c.JSON(http.StatusOK, m)
}

func inspectContainer(c *gin.Context) {
	cID := c.Param("id")
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)
	con, err := client.InspectContainer(cID)
	if err != nil {
		e := ErrorResponse{"error while trying to inspect container:" + err.Error()}
		c.JSON(http.StatusInternalServerError, e)
	}

	c.JSON(http.StatusOK, con)
}

func startDiscovering() {
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)
	log.Println("Started listening... Refresh rate is :", refreshRate)
	for {
		log.Println("Assembling Post...")
		post := Post{AgentID: agentID, ExpiredAfterSeconds: expireAfterSeconds}
		containers := []Containers{}
		runningContainers, err := client.ListContainers(docker.ListContainersOptions{All: false})
		if err != nil {
			panic("Failed to connect to Docker Client." + err.Error())
		}
		for _, v := range runningContainers {
			c := Containers{}
			c.ID = v.ID
			c.Name = strings.Join(v.Names, ",")
			for _, p := range v.Ports {
				c.Port += p.IP + ":" + p.Type + ":" + strconv.Itoa(int(p.PrivatePort)) + ":" + strconv.Itoa(int(p.PublicPort))
			}
			c.Command = v.Command
			containers = append(containers, c)
		}
		post.Containers = containers

		postString, err := json.Marshal(post)
		if err != nil {
			log.Println("Error occured while trying ot marshal POST:", err.Error())
		}
		req, err := http.NewRequest("POST", serverURL+"/api/1/add", bytes.NewBuffer(postString))
		if err != nil {
			log.Println("Failed to create post request... Trying again later.")
			time.Sleep(time.Second * time.Duration(refreshRate))
			continue
		}

		req.Header.Set("Content-Type", "application/json")
		log.Println("Posting JSON:", string(postString))
		client := &http.Client{}
		resp, err := client.Do(req)
		if err != nil {
			log.Println("Failed to receive from server... Trying again later.")
			time.Sleep(time.Second * time.Duration(refreshRate))
			continue
		}
		defer resp.Body.Close()
		//TODO: Verify the response
		time.Sleep(time.Second * time.Duration(refreshRate))
	}
}
<|endoftext|>


<commit_before>/*
 * Spreed WebRTC.
 * Copyright (C) 2013-2015 struktur AG
 *
 * This file is part of Spreed WebRTC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package main

import (
	"crypto/rand"
	pseudoRand "math/rand"
	"time"
)

const (
	dict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW0123456789"
)

func NewRandomString(length int) string {

	buf := make([]byte, length)
	_, err := rand.Read(buf)
	if err != nil {
		// fallback to pseudo-random
		for i := 0; i < length; i++ {
			buf[i] = dict[pseudoRand.Intn(len(dict))]
		}
	} else {
		for i := 0; i < length; i++ {
			buf[i] = dict[int(buf[i])%len(dict)]
		}
	}
	return string(buf)

}

func init() {
	// Make sure to seed default random generator.
	pseudoRand.Seed(time.Now().UTC().UnixNano())
}
<commit_msg>Add missing characters to random string function, so we use the full upper+lowercase alphabet<commit_after>/*
 * Spreed WebRTC.
 * Copyright (C) 2013-2015 struktur AG
 *
 * This file is part of Spreed WebRTC.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package main

import (
	"crypto/rand"
	pseudoRand "math/rand"
	"time"
)

const (
	dict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)

func NewRandomString(length int) string {

	buf := make([]byte, length)
	_, err := rand.Read(buf)
	if err != nil {
		// fallback to pseudo-random
		for i := 0; i < length; i++ {
			buf[i] = dict[pseudoRand.Intn(len(dict))]
		}
	} else {
		for i := 0; i < length; i++ {
			buf[i] = dict[int(buf[i])%len(dict)]
		}
	}
	return string(buf)

}

func init() {
	// Make sure to seed default random generator.
	pseudoRand.Seed(time.Now().UTC().UnixNano())
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"io/ioutil"

	"github.com/blevesearch/bleve"
	"github.com/blevesearch/bleve/mapping"
	"github.com/spf13/cobra"

	"github.com/bobinette/papernet/errors"
)

func init() {
	CreateIndexCommand.PersistentFlags().String("file", "", "mapping file")
	CreateIndexCommand.PersistentFlags().String("index", "", "index directory")

	IndexCommand.AddCommand(&CreateIndexCommand)
	IndexCommand.AddCommand(&IndexAllCommand)
	RootCmd.AddCommand(&IndexCommand)
}

var IndexCommand = cobra.Command{
	Use:   "index",
	Short: "Index papers",
	Long:  "Index papers from their IDs",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) == 0 {
			return errors.New("This command expects ids as arguments")
		}

		if args[0] == "help" {
			return cmd.Help()
		}

		ids, err := ints(args)
		if err != nil {
			return errors.New("ids should be integers", errors.WithCause(err))
		}

		addr := cmd.Flag("store").Value.String()
		store, f, err := createStore(addr)
		defer f()
		if err != nil {
			return errors.New("error opening db", errors.WithCause(err))
		}

		addr = cmd.Flag("index").Value.String()
		index, f, err := createIndex(addr)
		defer f()
		if err != nil {
			return errors.New("error opening index", errors.WithCause(err))
		}

		papers, err := store.Get(ids...)
		if err != nil {
			return errors.New("error getting papers", errors.WithCause(err))
		}

		for _, paper := range papers {
			err = index.Index(paper)
			if err != nil {
				return errors.New("error indexing", errors.WithCause(err))
			}
			cmd.Printf("<Paper %d> indexed\n", paper.ID)
		}
		return nil
	},
}

var IndexAllCommand = cobra.Command{
	Use:   "all",
	Short: "Index all papers",
	Long:  "Index all papers in the store",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) == 1 && args[0] == "help" {
			return cmd.Help()
		}

		addr := cmd.Flag("store").Value.String()
		store, f, err := createStore(addr)
		defer f()
		if err != nil {
			return errors.New("error opening db", errors.WithCause(err))
		}

		addr = cmd.Flag("index").Value.String()
		index, f, err := createIndex(addr)
		defer f()
		if err != nil {
			return errors.New("error opening index", errors.WithCause(err))
		}

		papers, err := store.List()
		if err != nil {
			return errors.New("error getting papers", errors.WithCause(err))
		}

		for _, paper := range papers {
			err = index.Index(paper)
			if err != nil {
				return errors.New("error indexing", errors.WithCause(err))
			}
			cmd.Printf("<Paper %d> indexed\n", paper.ID)
		}
		return nil
	},
}

var CreateIndexCommand = cobra.Command{
	Use:   "create",
	Short: "Create index",
	Long:  "Create index",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) == 1 && args[0] == "help" {
			return cmd.Help()
		}

		filename := cmd.Flag("file").Value.String()
		if filename == "" {
			return errors.New("file argument needed")
		}

		indexPath := cmd.Flag("index").Value.String()
		if filename == "" {
			return errors.New("indexs argument needed")
		}

		data, err := ioutil.ReadFile(filename)
		if err != nil {
			return errors.New("error reading mapping file", errors.WithCause(err))
		}

		var m mapping.IndexMappingImpl
		err = json.Unmarshal(data, &m)

		_, err = bleve.New(indexPath, &m)
		if err != nil {
			return errors.New("error creating index", errors.WithCause(err))
		}
		cmd.Printf("Index created at %s\n", indexPath)

		return nil
	},
}
<commit_msg>Fix create index command<commit_after>package main

import (
	"encoding/json"
	"io/ioutil"

	"github.com/blevesearch/bleve"
	"github.com/blevesearch/bleve/mapping"
	"github.com/spf13/cobra"

	"github.com/bobinette/papernet/errors"
)

func init() {
	CreateIndexCommand.PersistentFlags().String("file", "", "mapping file")
	CreateIndexCommand.PersistentFlags().String("index", "", "index directory")

	IndexCommand.AddCommand(&CreateIndexCommand)
	IndexCommand.AddCommand(&IndexAllCommand)
	RootCmd.AddCommand(&IndexCommand)
}

var IndexCommand = cobra.Command{
	Use:   "index",
	Short: "Index papers",
	Long:  "Index papers from their IDs",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) == 0 {
			return errors.New("This command expects ids as arguments")
		}

		if args[0] == "help" {
			return cmd.Help()
		}

		ids, err := ints(args)
		if err != nil {
			return errors.New("ids should be integers", errors.WithCause(err))
		}

		addr := cmd.Flag("store").Value.String()
		store, f, err := createStore(addr)
		defer f()
		if err != nil {
			return errors.New("error opening db", errors.WithCause(err))
		}

		addr = cmd.Flag("index").Value.String()
		index, f, err := createIndex(addr)
		defer f()
		if err != nil {
			return errors.New("error opening index", errors.WithCause(err))
		}

		papers, err := store.Get(ids...)
		if err != nil {
			return errors.New("error getting papers", errors.WithCause(err))
		}

		for _, paper := range papers {
			err = index.Index(paper)
			if err != nil {
				return errors.New("error indexing", errors.WithCause(err))
			}
			cmd.Printf("<Paper %d> indexed\n", paper.ID)
		}
		return nil
	},
}

var IndexAllCommand = cobra.Command{
	Use:   "all",
	Short: "Index all papers",
	Long:  "Index all papers in the store",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) == 1 && args[0] == "help" {
			return cmd.Help()
		}

		addr := cmd.Flag("store").Value.String()
		store, f, err := createStore(addr)
		defer f()
		if err != nil {
			return errors.New("error opening db", errors.WithCause(err))
		}

		addr = cmd.Flag("index").Value.String()
		index, f, err := createIndex(addr)
		defer f()
		if err != nil {
			return errors.New("error opening index", errors.WithCause(err))
		}

		papers, err := store.List()
		if err != nil {
			return errors.New("error getting papers", errors.WithCause(err))
		}

		for _, paper := range papers {
			err = index.Index(paper)
			if err != nil {
				return errors.New("error indexing", errors.WithCause(err))
			}
			cmd.Printf("<Paper %d> indexed\n", paper.ID)
		}
		return nil
	},
}

var CreateIndexCommand = cobra.Command{
	Use:   "create",
	Short: "Create index",
	Long:  "Create index",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) == 1 && args[0] == "help" {
			return cmd.Help()
		}

		filename := cmd.Flag("mapping").Value.String()
		if filename == "" {
			return errors.New("mapping argument needed")
		}

		indexPath := cmd.Flag("index").Value.String()
		if indexPath == "" {
			return errors.New("index argument needed")
		}

		data, err := ioutil.ReadFile(filename)
		if err != nil {
			return errors.New("error reading mapping file", errors.WithCause(err))
		}

		var m mapping.IndexMappingImpl
		err = json.Unmarshal(data, &m)

		_, err = bleve.New(indexPath, &m)
		if err != nil {
			return errors.New("error creating index", errors.WithCause(err))
		}
		cmd.Printf("Index created at %s\n", indexPath)

		return nil
	},
}
<|endoftext|>


<commit_before>package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/deis/tiller/pkg/client"
	"github.com/deis/tiller/pkg/kubectl"
	"github.com/spf13/cobra"
)

const installDesc = `
This command installs Tiller (the helm server side component) onto your
Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.helm/)
`

const repositoriesPath = ".repositories"
const cachePath = "cache"
const localPath = "local"

var defaultRepo = map[string]string{"default-name": "default-url"}
var tillerImg string

func init() {
	initCmd.Flags().StringVarP(&tillerImg, "tiller-image", "i", "", "override tiller image")
	RootCommand.AddCommand(initCmd)
}

var initCmd = &cobra.Command{
	Use:   "init",
	Short: "Initialize Helm on both client and server.",
	Long:  installDesc,
	RunE:  runInit,
}

// runInit initializes local config and installs tiller to Kubernetes Cluster
func runInit(cmd *cobra.Command, args []string) error {
	if len(args) != 0 {
		return errors.New("This command does not accept arguments. \n")
	}

	if err := ensureHome(os.ExpandEnv(helmHome)); err != nil {
		return err
	}

	if err := installTiller(); err != nil {
		return err
	}

	fmt.Printf("Tiller (the helm server side component) has been installed into your Kubernetes Cluster.\n$HELM_HOME has also been configured at %s.\nHappy Helming!\n", helmHome)
	return nil
}

func installTiller() error {
	// TODO: take value of global flag kubectl and pass that in
	runner := buildKubectlRunner("")

	i := client.NewInstaller()
	i.Tiller["Image"] = tillerImg
	out, err := i.Install(runner)

	if err != nil {
		return fmt.Errorf("error installing %s %s", string(out), err)
	}

	return nil
}

func buildKubectlRunner(kubectlPath string) kubectl.Runner {
	if kubectlPath != "" {
		kubectl.Path = kubectlPath
	}
	return &kubectl.RealRunner{}
}

// ensureHome checks to see if $HELM_HOME exists
//
// If $HELM_HOME does not exist, this function will create it.
func ensureHome(home string) error {
	configDirectories := []string{home, CacheDirectory(home), LocalDirectory(home)}

	for _, p := range configDirectories {
		if fi, err := os.Stat(p); err != nil {
			fmt.Printf("Creating %s \n", p)
			if err := os.MkdirAll(p, 0755); err != nil {
				return fmt.Errorf("Could not create %s: %s", p, err)
			}
		} else if !fi.IsDir() {
			return fmt.Errorf("%s must be a directory", p)
		}
	}

	repoPath := repositoriesFile(home)
	if fi, err := os.Stat(repoPath); err != nil {
		fmt.Printf("Creating %s \n", repoPath)
		if err := ioutil.WriteFile(repoPath, []byte("test-charts: https://www.googleapis.com/storage/v1/b/test-charts/o\n"), 0644); err != nil {
			return err
		}
	} else if fi.IsDir() {
		return fmt.Errorf("%s must be a file, not a directory", repoPath)
	}
	return nil
}

func cacheDirectory(home string) string {
	return filepath.Join(home, cachePath)
}

func repositoriesFile(home string) string {
	return filepath.Join(home, repositoriesPath)
}

func LocalDirectory(home string) string {
	return filepath.Join(home, localPath)
}
<commit_msg>feat(init): add cache.txt to $HELM_HOME/local<commit_after>package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/deis/tiller/pkg/client"
	"github.com/deis/tiller/pkg/kubectl"
	"github.com/spf13/cobra"
)

const installDesc = `
This command installs Tiller (the helm server side component) onto your
Kubernetes Cluster and sets up local configuration in $HELM_HOME (default: ~/.helm/)
`

const repositoriesPath = ".repositories"
const cachePath = "cache"
const localPath = "local"
const localCacheFilePath = localPath + "/cache.txt"

var defaultRepo = map[string]string{"default-name": "default-url"}
var tillerImg string

func init() {
	initCmd.Flags().StringVarP(&tillerImg, "tiller-image", "i", "", "override tiller image")
	RootCommand.AddCommand(initCmd)
}

var initCmd = &cobra.Command{
	Use:   "init",
	Short: "Initialize Helm on both client and server.",
	Long:  installDesc,
	RunE:  runInit,
}

// runInit initializes local config and installs tiller to Kubernetes Cluster
func runInit(cmd *cobra.Command, args []string) error {
	if len(args) != 0 {
		return errors.New("This command does not accept arguments. \n")
	}

	if err := ensureHome(os.ExpandEnv(helmHome)); err != nil {
		return err
	}

	if err := installTiller(); err != nil {
		return err
	}

	fmt.Printf("Tiller (the helm server side component) has been installed into your Kubernetes Cluster.\n$HELM_HOME has also been configured at %s.\nHappy Helming!\n", helmHome)
	return nil
}

func installTiller() error {
	// TODO: take value of global flag kubectl and pass that in
	runner := buildKubectlRunner("")

	i := client.NewInstaller()
	i.Tiller["Image"] = tillerImg
	out, err := i.Install(runner)

	if err != nil {
		return fmt.Errorf("error installing %s %s", string(out), err)
	}

	return nil
}

func buildKubectlRunner(kubectlPath string) kubectl.Runner {
	if kubectlPath != "" {
		kubectl.Path = kubectlPath
	}
	return &kubectl.RealRunner{}
}

// ensureHome checks to see if $HELM_HOME exists
//
// If $HELM_HOME does not exist, this function will create it.
func ensureHome(home string) error {
	configDirectories := []string{home, CacheDirectory(home), LocalDirectory(home)}

	for _, p := range configDirectories {
		if fi, err := os.Stat(p); err != nil {
			fmt.Printf("Creating %s \n", p)
			if err := os.MkdirAll(p, 0755); err != nil {
				return fmt.Errorf("Could not create %s: %s", p, err)
			}
		} else if !fi.IsDir() {
			return fmt.Errorf("%s must be a directory", p)
		}
	}

	repoPath := repositoriesFile(home)
	if fi, err := os.Stat(repoPath); err != nil {
		fmt.Printf("Creating %s \n", repoPath)
		if err := ioutil.WriteFile(repoPath, []byte("local: localhost:8879/charts"), 0644); err != nil {
			return err
		}
	} else if fi.IsDir() {
		return fmt.Errorf("%s must be a file, not a directory", repoPath)
	}

	localCacheFile := LocalDirCacheFile(home)
	if fi, err := os.Stat(localCacheFile); err != nil {
		fmt.Printf("Creating %s \n", localCacheFile)
		_, err := os.Create(localCacheFile)
		if err != nil {
			return err
		}

		os.Symlink(localCacheFile, CacheDirectory(home)+"/local-cache.txt")
	} else if fi.IsDir() {
		return fmt.Errorf("%s must be a file, not a directory.", repoPath)
	}
	return nil
}

func cacheDirectory(home string) string {
	return filepath.Join(home, cachePath)
}

func repositoriesFile(home string) string {
	return filepath.Join(home, repositoriesPath)
}

func LocalDirectory(home string) string {
	return filepath.Join(home, localPath)
}

func LocalDirCacheFile(home string) string {
	return filepath.Join(home, localCacheFilePath)
}
<|endoftext|>


<commit_before>// Copyright 2014, The Serviced Authors. All rights reserved.
// Use of this source code is governed by a
// license that can be found in the LICENSE file.

// Package agent implements a service that runs on a serviced node. It is
// responsible for ensuring that a particular node is running the correct services
// and reporting the state and health of those services back to the master
// serviced.

// This file implements the LoadBalancer interface aspect of the host agent.
package serviced

import (
	"github.com/zenoss/glog"
	"github.com/zenoss/serviced/dao"
	"github.com/zenoss/serviced/domain"

	"errors"
	"github.com/zenoss/serviced/domain/service"
	"strconv"
	"strings"
)

// assert that the HostAgent implements the LoadBalancer interface
var _ LoadBalancer = &HostAgent{}

type ServiceLogInfo struct {
	ServiceID string
	Message   string
}

func (a *HostAgent) SendLogMessage(serviceLogInfo ServiceLogInfo, _ *struct{}) (err error) {
	glog.Infof("Service: %v message: %v", serviceLogInfo.ServiceID, serviceLogInfo.Message)
	return nil
}

func (a *HostAgent) GetServiceEndpoints(serviceId string, response *map[string][]*dao.ApplicationEndpoint) (err error) {
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return
	}
	defer controlClient.Close()

	err = controlClient.GetServiceEndpoints(serviceId, response)
	if err != nil {
		return err
	}

	a.addControlPlaneEndpoint(*response)
	a.addControlPlaneConsumerEndpoint(*response)
	a.addLogstashEndpoint(*response)
	return nil
}

func (a *HostAgent) GetService(serviceId string, response *service.Service) (err error) {
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return
	}
	defer controlClient.Close()

	err = controlClient.GetService(serviceId, response)
	if err != nil {
		return err
	}

	getSvc := func(svcID string) (service.Service, error) {
		svc := service.Service{}
		err := controlClient.GetService(svcID, &svc)
		return svc, err
	}

	return response.Evaluate(getSvc)
}

// Call the master's to retrieve its tenant id
func (a *HostAgent) GetTenantId(serviceId string, tenantId *string) error {
	client, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return err
	}
	defer client.Close()
	return client.GetTenantId(serviceId, tenantId)
}

// GetProxySnapshotQuiece blocks until there is a snapshot request to the service
func (a *HostAgent) GetProxySnapshotQuiece(serviceId string, snapshotId *string) error {
	glog.Errorf("GetProxySnapshotQuiece() Unimplemented")
	return errors.New("unimplemented")
}

// AckProxySnapshotQuiece is called by clients when the snapshot command has
// shown the service is quieced; the agent returns a response when the snapshot is complete
func (a *HostAgent) AckProxySnapshotQuiece(snapshotId string, unused *interface{}) error {
	glog.Errorf("AckProxySnapshotQuiece() Unimplemented")
	return errors.New("unimplemented")
}

// GetHealthCheck returns the health check configuration for a service, if it exists
func (a *HostAgent) GetHealthCheck(serviceId string, healthChecks *map[string]domain.HealthCheck) error {
	glog.V(4).Infof("ControlPlaneAgent.GetHealthCheck()")
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return err
	}
	defer controlClient.Close()

	var svc service.Service
	err = controlClient.GetService(serviceId, &svc)
	if err != nil {
		return err
	}
	*healthChecks = svc.HealthChecks
	return nil
}

// LogHealthCheck proxies RegisterHealthCheck.
func (a *HostAgent) LogHealthCheck(result domain.HealthCheckResult, unused *int) error {
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return err
	}
	defer controlClient.Close()
	err = controlClient.LogHealthCheck(result, unused)
	return err
}

// addControlPlaneEndpoint adds an application endpoint mapping for the master control plane api
func (a *HostAgent) addControlPlaneEndpoint(endpoints map[string][]*dao.ApplicationEndpoint) {
	key := "tcp" + a.uiport
	endpoint := dao.ApplicationEndpoint{}
	endpoint.ServiceID = "controlplane"
	endpoint.Application = "controlplane"
	endpoint.ContainerIP = "127.0.0.1"
	port, err := strconv.Atoi(a.uiport[1:])
	if err != nil {
		glog.Errorf("Unable to interpret ui port.")
		return
	}
	endpoint.ContainerPort = uint16(port)
	endpoint.HostPort = uint16(port)
	endpoint.HostIP = strings.Split(a.master, ":")[0]
	endpoint.Protocol = "tcp"
	a.addEndpoint(key, endpoint, endpoints)
}

// addControlPlaneConsumerEndpoint adds an application endpoint mapping for the master control plane api
func (a *HostAgent) addControlPlaneConsumerEndpoint(endpoints map[string][]*dao.ApplicationEndpoint) {
	key := "tcp:8444"
	endpoint := dao.ApplicationEndpoint{}
	endpoint.ServiceID = "controlplane_consumer"
	endpoint.Application = "controlplane_consumer"
	endpoint.ContainerIP = "127.0.0.1"
	endpoint.ContainerPort = 8444
	endpoint.HostPort = 8443
	endpoint.HostIP = strings.Split(a.master, ":")[0]
	endpoint.Protocol = "tcp"
	a.addEndpoint(key, endpoint, endpoints)
}

// addLogstashEndpoint adds an application endpoint mapping for the master control plane api
func (a *HostAgent) addLogstashEndpoint(endpoints map[string][]*dao.ApplicationEndpoint) {
	key := "tcp:5043"
	endpoint := dao.ApplicationEndpoint{}
	endpoint.ServiceID = "controlplane_logstash"
	endpoint.ContainerIP = "127.0.0.1"
	endpoint.ContainerPort = 5043
	endpoint.HostPort = 5043
	endpoint.HostIP = strings.Split(a.master, ":")[0]
	endpoint.Protocol = "tcp"
	a.addEndpoint(key, endpoint, endpoints)
}

// addEndpoint adds a mapping to defined application, if a mapping does not exist this method creates the list and adds the first element
func (a *HostAgent) addEndpoint(key string, endpoint dao.ApplicationEndpoint, endpoints map[string][]*dao.ApplicationEndpoint) {
	if _, ok := endpoints[key]; !ok {
		endpoints[key] = make([]*dao.ApplicationEndpoint, 0)
	} else {
		if len(endpoints[key]) > 0 {
			glog.Warningf("Service %s has duplicate internal endpoint for key %s len(endpointList)=%d", endpoint.ServiceID, key, len(endpoints[key]))
			for _, ep := range endpoints[key] {
				glog.Warningf(" %+v", *ep)
			}
		}
	}
	endpoints[key] = append(endpoints[key], &endpoint)
}

// GetHostID returns the agent's host id
func (a *HostAgent) GetHostID(string, hostID *string) error {
	glog.V(4).Infof("ControlPlaneAgent.GetHostID(): %s", a.hostID)
	*hostID = a.hostID
	return nil
}

// GetZkDSN returns the agent's zookeeper connection string
func (a *HostAgent) GetZkDSN(string, dsn *string) error {
	localDSN := a.zkClient.ConnectionString()
	*dsn = strings.Replace(localDSN, "127.0.0.1", strings.Split(a.master, ":")[0], -1)
	glog.V(4).Infof("ControlPlaneAgent.GetZkDSN(): %s", *dsn)
	return nil
}
<commit_msg>add endpoint.Application for logstash for previous merge<commit_after>// Copyright 2014, The Serviced Authors. All rights reserved.
// Use of this source code is governed by a
// license that can be found in the LICENSE file.

// Package agent implements a service that runs on a serviced node. It is
// responsible for ensuring that a particular node is running the correct services
// and reporting the state and health of those services back to the master
// serviced.

// This file implements the LoadBalancer interface aspect of the host agent.
package serviced

import (
	"github.com/zenoss/glog"
	"github.com/zenoss/serviced/dao"
	"github.com/zenoss/serviced/domain"

	"errors"
	"github.com/zenoss/serviced/domain/service"
	"strconv"
	"strings"
)

// assert that the HostAgent implements the LoadBalancer interface
var _ LoadBalancer = &HostAgent{}

type ServiceLogInfo struct {
	ServiceID string
	Message   string
}

func (a *HostAgent) SendLogMessage(serviceLogInfo ServiceLogInfo, _ *struct{}) (err error) {
	glog.Infof("Service: %v message: %v", serviceLogInfo.ServiceID, serviceLogInfo.Message)
	return nil
}

func (a *HostAgent) GetServiceEndpoints(serviceId string, response *map[string][]*dao.ApplicationEndpoint) (err error) {
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return
	}
	defer controlClient.Close()

	err = controlClient.GetServiceEndpoints(serviceId, response)
	if err != nil {
		return err
	}

	a.addControlPlaneEndpoint(*response)
	a.addControlPlaneConsumerEndpoint(*response)
	a.addLogstashEndpoint(*response)
	return nil
}

func (a *HostAgent) GetService(serviceId string, response *service.Service) (err error) {
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return
	}
	defer controlClient.Close()

	err = controlClient.GetService(serviceId, response)
	if err != nil {
		return err
	}

	getSvc := func(svcID string) (service.Service, error) {
		svc := service.Service{}
		err := controlClient.GetService(svcID, &svc)
		return svc, err
	}

	return response.Evaluate(getSvc)
}

// Call the master's to retrieve its tenant id
func (a *HostAgent) GetTenantId(serviceId string, tenantId *string) error {
	client, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return err
	}
	defer client.Close()
	return client.GetTenantId(serviceId, tenantId)
}

// GetProxySnapshotQuiece blocks until there is a snapshot request to the service
func (a *HostAgent) GetProxySnapshotQuiece(serviceId string, snapshotId *string) error {
	glog.Errorf("GetProxySnapshotQuiece() Unimplemented")
	return errors.New("unimplemented")
}

// AckProxySnapshotQuiece is called by clients when the snapshot command has
// shown the service is quieced; the agent returns a response when the snapshot is complete
func (a *HostAgent) AckProxySnapshotQuiece(snapshotId string, unused *interface{}) error {
	glog.Errorf("AckProxySnapshotQuiece() Unimplemented")
	return errors.New("unimplemented")
}

// GetHealthCheck returns the health check configuration for a service, if it exists
func (a *HostAgent) GetHealthCheck(serviceId string, healthChecks *map[string]domain.HealthCheck) error {
	glog.V(4).Infof("ControlPlaneAgent.GetHealthCheck()")
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return err
	}
	defer controlClient.Close()

	var svc service.Service
	err = controlClient.GetService(serviceId, &svc)
	if err != nil {
		return err
	}
	*healthChecks = svc.HealthChecks
	return nil
}

// LogHealthCheck proxies RegisterHealthCheck.
func (a *HostAgent) LogHealthCheck(result domain.HealthCheckResult, unused *int) error {
	controlClient, err := NewControlClient(a.master)
	if err != nil {
		glog.Errorf("Could not start ControlPlane client %v", err)
		return err
	}
	defer controlClient.Close()
	err = controlClient.LogHealthCheck(result, unused)
	return err
}

// addControlPlaneEndpoint adds an application endpoint mapping for the master control plane api
func (a *HostAgent) addControlPlaneEndpoint(endpoints map[string][]*dao.ApplicationEndpoint) {
	key := "tcp" + a.uiport
	endpoint := dao.ApplicationEndpoint{}
	endpoint.ServiceID = "controlplane"
	endpoint.Application = "controlplane"
	endpoint.ContainerIP = "127.0.0.1"
	port, err := strconv.Atoi(a.uiport[1:])
	if err != nil {
		glog.Errorf("Unable to interpret ui port.")
		return
	}
	endpoint.ContainerPort = uint16(port)
	endpoint.HostPort = uint16(port)
	endpoint.HostIP = strings.Split(a.master, ":")[0]
	endpoint.Protocol = "tcp"
	a.addEndpoint(key, endpoint, endpoints)
}

// addControlPlaneConsumerEndpoint adds an application endpoint mapping for the master control plane api
func (a *HostAgent) addControlPlaneConsumerEndpoint(endpoints map[string][]*dao.ApplicationEndpoint) {
	key := "tcp:8444"
	endpoint := dao.ApplicationEndpoint{}
	endpoint.ServiceID = "controlplane_consumer"
	endpoint.Application = "controlplane_consumer"
	endpoint.ContainerIP = "127.0.0.1"
	endpoint.ContainerPort = 8444
	endpoint.HostPort = 8443
	endpoint.HostIP = strings.Split(a.master, ":")[0]
	endpoint.Protocol = "tcp"
	a.addEndpoint(key, endpoint, endpoints)
}

// addLogstashEndpoint adds an application endpoint mapping for the master control plane api
func (a *HostAgent) addLogstashEndpoint(endpoints map[string][]*dao.ApplicationEndpoint) {
	key := "tcp:5043"
	endpoint := dao.ApplicationEndpoint{}
	endpoint.ServiceID = "controlplane_logstash"
	endpoint.Application = "controlplane_logstash"
	endpoint.ContainerIP = "127.0.0.1"
	endpoint.ContainerPort = 5043
	endpoint.HostPort = 5043
	endpoint.HostIP = strings.Split(a.master, ":")[0]
	endpoint.Protocol = "tcp"
	a.addEndpoint(key, endpoint, endpoints)
}

// addEndpoint adds a mapping to defined application, if a mapping does not exist this method creates the list and adds the first element
func (a *HostAgent) addEndpoint(key string, endpoint dao.ApplicationEndpoint, endpoints map[string][]*dao.ApplicationEndpoint) {
	if _, ok := endpoints[key]; !ok {
		endpoints[key] = make([]*dao.ApplicationEndpoint, 0)
	} else {
		if len(endpoints[key]) > 0 {
			glog.Warningf("Service %s has duplicate internal endpoint for key %s len(endpointList)=%d", endpoint.ServiceID, key, len(endpoints[key]))
			for _, ep := range endpoints[key] {
				glog.Warningf(" %+v", *ep)
			}
		}
	}
	endpoints[key] = append(endpoints[key], &endpoint)
}

// GetHostID returns the agent's host id
func (a *HostAgent) GetHostID(string, hostID *string) error {
	glog.V(4).Infof("ControlPlaneAgent.GetHostID(): %s", a.hostID)
	*hostID = a.hostID
	return nil
}

// GetZkDSN returns the agent's zookeeper connection string
func (a *HostAgent) GetZkDSN(string, dsn *string) error {
	localDSN := a.zkClient.ConnectionString()
	*dsn = strings.Replace(localDSN, "127.0.0.1", strings.Split(a.master, ":")[0], -1)
	glog.V(4).Infof("ControlPlaneAgent.GetZkDSN(): %s", *dsn)
	return nil
}
<|endoftext|>


<commit_before>package main

//usage: go run mtrparser.go <hostname or ip>

import (
	"encoding/json"
	"fmt"
	"github.com/abh/geoip"
	"github.com/sajal/mtrparser"
	"log"
	"os"
)

func main() {
	if len(os.Args) < 2 {
		log.Fatal("Need mtr destination")
	}
	target := os.Args[1]
	ipv := ""
	if len(os.Args) > 2 {
		ipv = os.Args[2]
	}
	log.Println(target, ipv)
	result, err := mtrparser.ExecuteMTR(target, ipv)
	if err != nil {
		log.Fatal(err)
	}
	gia, err := geoip.OpenType(geoip.GEOIP_ASNUM_EDITION)
	if err != nil {
		log.Println(err)
		gia = nil
	}
	log.Println(gia)
	for _, hop := range result.Hops {
		hop.Summarize(10, gia)
	}

	fmt.Println("Result")
	fmt.Println(result)
	fmt.Println("Json")
	b, err := json.Marshal(result)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))
	fmt.Println("Line by line")
	for idx, item := range result.Hops {
		fmt.Printf("%v : %s %s (%s) Avg: %v, Loss : %v%% Best: %v Worst: %v Last: %v stdDev: %v\n", idx+1, item.Host, item.ASN, item.IP, item.Avg, item.Loss, item.Best, item.Worst, item.Last, item.SD)
	}
}
<commit_msg>update cmd for new library handling<commit_after>package main

//usage: go run mtrparser.go <hostname or ip>

import (
	"encoding/json"
	"fmt"
	"github.com/abh/geoip"
	"github.com/sajal/mtrparser"
	"log"
	"os"
	"strings"
)

func getasnmtr(ip string, gia *geoip.GeoIP) string {
	asntmp, _ := gia.GetName(ip)
	if asntmp != "" {
		splitted := strings.SplitN(asntmp, " ", 2)
		if len(splitted) == 2 {
			return splitted[0]
		}
	}
	return ""
}

func ResolveASNMtr(hop *mtrparser.MtrHop, gia *geoip.GeoIP) {
	hop.ASN = make([]string, len(hop.IP))
	for idx, ip := range hop.IP {
		//TODO...
		hop.ASN[idx] = getasnmtr(ip, gia)
	}
}

func main() {
	if len(os.Args) < 2 {
		log.Fatal("Need mtr destination")
	}
	target := os.Args[1]
	ipv := ""
	if len(os.Args) > 2 {
		ipv = os.Args[2]
	}
	log.Println(target, ipv)
	result, err := mtrparser.ExecuteMTR(target, ipv)
	if err != nil {
		log.Fatal(err)
	}
	gia, err := geoip.OpenType(geoip.GEOIP_ASNUM_EDITION)
	if err != nil {
		log.Println(err)
		gia = nil
	}
	log.Println(gia)
	for _, hop := range result.Hops {
		hop.Summarize(10)
	}
	for _, hop := range result.Hops {
		ResolveASNMtr(hop, gia)
	}
	fmt.Println("Result")
	fmt.Println(result)
	fmt.Println("Json")
	b, err := json.Marshal(result)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))
	fmt.Println("Line by line")
	for idx, item := range result.Hops {
		fmt.Printf("%v : %s %s (%s) Avg: %v, Loss : %v%% Best: %v Worst: %v Last: %v stdDev: %v\n", idx+1, item.Host, item.ASN, item.IP, item.Avg, item.Loss, item.Best, item.Worst, item.Last, item.SD)
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2016 Google Inc.

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 (
	"bufio"
	"encoding/json"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/gorilla/websocket"
)

var (
	wsupgrade = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
)

const (
	maxLineLength = 1000
)

func tailHandler(w http.ResponseWriter, r *http.Request) {
	f, err := os.Open(*squidLog)
	if err != nil {
		log.Printf("File open failed: %v", err)
		http.Error(w, "File open failed", http.StatusInternalServerError)
		return
	}
	pos, err := f.Seek(-maxLineLength, 2)
	if err != nil {
		log.Printf("File seek failed: %v", err)
		http.Error(w, "File seek failed", http.StatusInternalServerError)
		return
	}
	conn, err := wsupgrade.Upgrade(w, r, nil)
	if err != nil {
		log.Printf("Upgrade failed: %v", err)
		http.Error(w, "Upgrade failed", http.StatusBadRequest)
		return
	}
	defer conn.Close()
	sleep := false
	first := true
	for {
		if sleep {
			time.Sleep(time.Second)
		}
		sleep = true

		if pos, err := f.Seek(pos, 0); err != nil {
			log.Printf("File seek failed: %v", err)
			return
		} else {
			if false {
				log.Printf("Reading at pos %d", pos)
			}
		}
		rd := bufio.NewReader(f)
		line, err := rd.ReadString('\n')
		if err == io.EOF {
			//log.Printf("Read %d EOF", len(line))
			continue
		}
		if err != nil {
			log.Printf("File read error: %v", err)
			return
		}
		if !strings.HasSuffix(line, "\n") {
			// Not a complete line yet.
			if _, err := f.Seek(pos, 0); err != nil {
				log.Printf("File seek failed: %v", err)
				return
			}
			continue
		}
		pos += int64(len(line))

		e, err := parseLogEntry(line)
		if err == errSkip {
		} else if err != nil {
			if !first {
				log.Printf("Error parsing log line: %v", err)
			}
			first = false
			continue
		}
		data, err := json.Marshal(e)
		if err != nil {
			log.Printf("Failed to mashal tail: %v", err)
			return
		}
		if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
			log.Printf("Message write failed: %v", err)
			return
		}
		sleep = false
	}
}
<commit_msg>Use inotify(7) to detect log file appends<commit_after>/*
Copyright 2016 Google Inc.

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 (
	"bufio"
	"encoding/json"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/gorilla/websocket"
	"golang.org/x/exp/inotify"
)

var (
	wsupgrade = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
)

const (
	maxLineLength = 1000
)

func tailHandler(w http.ResponseWriter, r *http.Request) {
	f, err := os.Open(*squidLog)
	if err != nil {
		log.Printf("File open failed: %v", err)
		http.Error(w, "File open failed", http.StatusInternalServerError)
		return
	}
	pos, err := f.Seek(-maxLineLength, 2)
	if err != nil {
		log.Printf("File seek failed: %v", err)
		http.Error(w, "File seek failed", http.StatusInternalServerError)
		return
	}

	log.Printf("Creating websocket...")
	conn, err := wsupgrade.Upgrade(w, r, nil)
	if err != nil {
		log.Printf("Upgrade failed: %v", err)
		http.Error(w, "Upgrade failed", http.StatusBadRequest)
		return
	}
	defer func() {
		log.Printf("Closing websocket")
		conn.Close()
	}()
	changeTick := make(chan struct{}, 1)
	changeTick <- struct{}{}

	// Tick every time the file changes, or 10s if it doesn't.
	{
		w, err := inotify.NewWatcher()
		if err != nil {
			log.Fatalf("Couldn't create watcher: %v", err)
		}
		defer w.Close()
		if err := w.AddWatch(*squidLog, inotify.IN_MODIFY); err != nil {
			log.Fatalf("Couldn't add watcher on %s: %v", *squidLog, err)
		}
		go func() {
			defer close(changeTick)
			for {
				select {
				case _, ok := <-w.Event:
					log.Printf("Event %v!", ok)
					if !ok {
						return
					}
				case <-time.After(10 * time.Second):
				}
				select {
				case changeTick <- struct{}{}:
				default:
				}
			}
		}()
	}

	sleep := false
	first := true
	done := websocketDone(conn)
	for {
		if sleep {
			<-changeTick
		}
		sleep = false
		if done() {
			return
		}

		if _, err := f.Seek(pos, 0); err != nil {
			log.Printf("File seek failed: %v", err)
			return
		}
		rd := bufio.NewReader(f)
		line, err := rd.ReadString('\n')
		if err == io.EOF {
			sleep = true
			if err := conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(10*time.Second)); err != nil {
				log.Printf("Ping failed: %v", err)
				return
			}
			continue
		}
		if err != nil {
			log.Printf("File read error: %v", err)
			return
		}
		if !strings.HasSuffix(line, "\n") {
			// Not a complete line yet.
			if _, err := f.Seek(pos, 0); err != nil {
				log.Printf("File seek failed: %v", err)
				return
			}
			sleep = true
			continue
		}
		pos += int64(len(line))

		e, err := parseLogEntry(line)
		if err == errSkip {
		} else if err != nil {
			if !first {
				log.Printf("Error parsing log line: %v", err)
			}
			first = false
			continue
		}
		data, err := json.Marshal(e)
		if err != nil {
			log.Printf("Failed to mashal tail: %v", err)
			return
		}
		if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
			//log.Printf("Message write failed: %v", err)
			return
		}
	}
}

func websocketDone(c *websocket.Conn) func() bool {
	done := make(chan struct{})
	go func() {
		defer close(done)
		typ, _, err := c.ReadMessage()
		if websocket.IsCloseError(err, websocket.CloseGoingAway) {
			return
		}
		if err != nil {
			log.Printf("Error reading message: %v %v", typ, err)
			return
		}
		if typ == websocket.CloseMessage {
			log.Printf("Socket closed using close message")
			return
		}
	}()
	return func() bool {
		select {
		case <-done:
			return true
		default:
			return false
		}
	}
}
<|endoftext|>


<commit_before>// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// Package utils contains internal helper functions for go-ethereum commands.
package utils

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"os/signal"
	"regexp"
	"strings"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/internal/debug"
	"github.com/ethereum/go-ethereum/logger"
	"github.com/ethereum/go-ethereum/logger/glog"
	"github.com/ethereum/go-ethereum/node"
	"github.com/ethereum/go-ethereum/rlp"
	"github.com/peterh/liner"
)

const (
	importBatchSize = 2500
)

var (
	interruptCallbacks = []func(os.Signal){}
)

func openLogFile(Datadir string, filename string) *os.File {
	path := common.AbsolutePath(Datadir, filename)
	file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(fmt.Sprintf("error opening log file '%s': %v", filename, err))
	}
	return file
}

func PromptConfirm(prompt string) (bool, error) {
	var (
		input string
		err   error
	)
	prompt = prompt + " [y/N] "

	// if liner.TerminalSupported() {
	// 	fmt.Println("term")
	// 	lr := liner.NewLiner()
	// 	defer lr.Close()
	// 	input, err = lr.Prompt(prompt)
	// } else {
	fmt.Print(prompt)
	input, err = bufio.NewReader(os.Stdin).ReadString('\n')
	fmt.Println()
	// }

	if len(input) > 0 && strings.ToUpper(input[:1]) == "Y" {
		return true, nil
	} else {
		return false, nil
	}

	return false, err
}

func PromptPassword(prompt string, warnTerm bool) (string, error) {
	if liner.TerminalSupported() {
		lr := liner.NewLiner()
		defer lr.Close()
		return lr.PasswordPrompt(prompt)
	}
	if warnTerm {
		fmt.Println("!! Unsupported terminal, password will be echoed.")
	}
	fmt.Print(prompt)
	input, err := bufio.NewReader(os.Stdin).ReadString('\n')
	fmt.Println()
	return input, err
}

// Fatalf formats a message to standard error and exits the program.
// The message is also printed to standard output if standard error
// is redirected to a different file.
func Fatalf(format string, args ...interface{}) {
	w := io.MultiWriter(os.Stdout, os.Stderr)
	outf, _ := os.Stdout.Stat()
	errf, _ := os.Stderr.Stat()
	if outf != nil && errf != nil && os.SameFile(outf, errf) {
		w = os.Stderr
	}
	fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
	logger.Flush()
	os.Exit(1)
}

func StartNode(stack *node.Node) {
	if err := stack.Start(); err != nil {
		Fatalf("Error starting protocol stack: %v", err)
	}
	go func() {
		sigc := make(chan os.Signal, 1)
		signal.Notify(sigc, os.Interrupt)
		defer signal.Stop(sigc)
		<-sigc
		glog.V(logger.Info).Infoln("Got interrupt, shutting down...")
		go stack.Stop()
		logger.Flush()
		for i := 10; i > 0; i-- {
			<-sigc
			if i > 1 {
				glog.V(logger.Info).Infoln("Already shutting down, please be patient.")
				glog.V(logger.Info).Infoln("Interrupt", i-1, "more times to induce panic.")
			}
		}
		glog.V(logger.Error).Infof("Force quitting: this might not end so well.")
		debug.LoudPanic("boom")
	}()
}

func FormatTransactionData(data string) []byte {
	d := common.StringToByteFunc(data, func(s string) (ret []byte) {
		slice := regexp.MustCompile("\\n|\\s").Split(s, 1000000000)
		for _, dataItem := range slice {
			d := common.FormatData(dataItem)
			ret = append(ret, d...)
		}
		return
	})

	return d
}

func ImportChain(chain *core.BlockChain, fn string) error {
	// Watch for Ctrl-C while the import is running.
	// If a signal is received, the import will stop at the next batch.
	interrupt := make(chan os.Signal, 1)
	stop := make(chan struct{})
	signal.Notify(interrupt, os.Interrupt)
	defer signal.Stop(interrupt)
	defer close(interrupt)
	go func() {
		if _, ok := <-interrupt; ok {
			glog.Info("caught interrupt during import, will stop at next batch")
		}
		close(stop)
	}()
	checkInterrupt := func() bool {
		select {
		case <-stop:
			return true
		default:
			return false
		}
	}

	glog.Infoln("Importing blockchain", fn)
	fh, err := os.Open(fn)
	if err != nil {
		return err
	}
	defer fh.Close()
	stream := rlp.NewStream(fh, 0)

	// Run actual the import.
	blocks := make(types.Blocks, importBatchSize)
	n := 0
	for batch := 0; ; batch++ {
		// Load a batch of RLP blocks.
		if checkInterrupt() {
			return fmt.Errorf("interrupted")
		}
		i := 0
		for ; i < importBatchSize; i++ {
			var b types.Block
			if err := stream.Decode(&b); err == io.EOF {
				break
			} else if err != nil {
				return fmt.Errorf("at block %d: %v", n, err)
			}
			// don't import first block
			if b.NumberU64() == 0 {
				i--
				continue
			}
			blocks[i] = &b
			n++
		}
		if i == 0 {
			break
		}
		// Import the batch.
		if checkInterrupt() {
			return fmt.Errorf("interrupted")
		}
		if hasAllBlocks(chain, blocks[:i]) {
			glog.Infof("skipping batch %d, all blocks present [%x / %x]",
				batch, blocks[0].Hash().Bytes()[:4], blocks[i-1].Hash().Bytes()[:4])
			continue
		}

		if _, err := chain.InsertChain(blocks[:i]); err != nil {
			return fmt.Errorf("invalid block %d: %v", n, err)
		}
	}
	return nil
}

func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool {
	for _, b := range bs {
		if !chain.HasBlock(b.Hash()) {
			return false
		}
	}
	return true
}

func ExportChain(blockchain *core.BlockChain, fn string) error {
	glog.Infoln("Exporting blockchain to", fn)
	fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
	if err != nil {
		return err
	}
	defer fh.Close()
	if err := blockchain.Export(fh); err != nil {
		return err
	}
	glog.Infoln("Exported blockchain to", fn)
	return nil
}

func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, last uint64) error {
	glog.Infoln("Exporting blockchain to", fn)
	// TODO verify mode perms
	fh, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
	if err != nil {
		return err
	}
	defer fh.Close()
	if err := blockchain.ExportN(fh, first, last); err != nil {
		return err
	}
	glog.Infoln("Exported blockchain to", fn)
	return nil
}
<commit_msg>cmd/utils: removed password line endings when not using liner.<commit_after>// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// Package utils contains internal helper functions for go-ethereum commands.
package utils

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"os/signal"
	"regexp"
	"strings"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/internal/debug"
	"github.com/ethereum/go-ethereum/logger"
	"github.com/ethereum/go-ethereum/logger/glog"
	"github.com/ethereum/go-ethereum/node"
	"github.com/ethereum/go-ethereum/rlp"
	"github.com/peterh/liner"
)

const (
	importBatchSize = 2500
)

var (
	interruptCallbacks = []func(os.Signal){}
)

func openLogFile(Datadir string, filename string) *os.File {
	path := common.AbsolutePath(Datadir, filename)
	file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		panic(fmt.Sprintf("error opening log file '%s': %v", filename, err))
	}
	return file
}

func PromptConfirm(prompt string) (bool, error) {
	var (
		input string
		err   error
	)
	prompt = prompt + " [y/N] "

	// if liner.TerminalSupported() {
	// 	fmt.Println("term")
	// 	lr := liner.NewLiner()
	// 	defer lr.Close()
	// 	input, err = lr.Prompt(prompt)
	// } else {
	fmt.Print(prompt)
	input, err = bufio.NewReader(os.Stdin).ReadString('\n')
	fmt.Println()
	// }

	if len(input) > 0 && strings.ToUpper(input[:1]) == "Y" {
		return true, nil
	} else {
		return false, nil
	}

	return false, err
}

func PromptPassword(prompt string, warnTerm bool) (string, error) {
	if liner.TerminalSupported() {
		lr := liner.NewLiner()
		defer lr.Close()
		return lr.PasswordPrompt(prompt)
	}
	if warnTerm {
		fmt.Println("!! Unsupported terminal, password will be echoed.")
	}
	fmt.Print(prompt)
	input, err := bufio.NewReader(os.Stdin).ReadString('\n')
	input = strings.TrimRight(input, "\r\n")
	fmt.Println()
	return input, err
}

// Fatalf formats a message to standard error and exits the program.
// The message is also printed to standard output if standard error
// is redirected to a different file.
func Fatalf(format string, args ...interface{}) {
	w := io.MultiWriter(os.Stdout, os.Stderr)
	outf, _ := os.Stdout.Stat()
	errf, _ := os.Stderr.Stat()
	if outf != nil && errf != nil && os.SameFile(outf, errf) {
		w = os.Stderr
	}
	fmt.Fprintf(w, "Fatal: "+format+"\n", args...)
	logger.Flush()
	os.Exit(1)
}

func StartNode(stack *node.Node) {
	if err := stack.Start(); err != nil {
		Fatalf("Error starting protocol stack: %v", err)
	}
	go func() {
		sigc := make(chan os.Signal, 1)
		signal.Notify(sigc, os.Interrupt)
		defer signal.Stop(sigc)
		<-sigc
		glog.V(logger.Info).Infoln("Got interrupt, shutting down...")
		go stack.Stop()
		logger.Flush()
		for i := 10; i > 0; i-- {
			<-sigc
			if i > 1 {
				glog.V(logger.Info).Infoln("Already shutting down, please be patient.")
				glog.V(logger.Info).Infoln("Interrupt", i-1, "more times to induce panic.")
			}
		}
		glog.V(logger.Error).Infof("Force quitting: this might not end so well.")
		debug.LoudPanic("boom")
	}()
}

func FormatTransactionData(data string) []byte {
	d := common.StringToByteFunc(data, func(s string) (ret []byte) {
		slice := regexp.MustCompile("\\n|\\s").Split(s, 1000000000)
		for _, dataItem := range slice {
			d := common.FormatData(dataItem)
			ret = append(ret, d...)
		}
		return
	})

	return d
}

func ImportChain(chain *core.BlockChain, fn string) error {
	// Watch for Ctrl-C while the import is running.
	// If a signal is received, the import will stop at the next batch.
	interrupt := make(chan os.Signal, 1)
	stop := make(chan struct{})
	signal.Notify(interrupt, os.Interrupt)
	defer signal.Stop(interrupt)
	defer close(interrupt)
	go func() {
		if _, ok := <-interrupt; ok {
			glog.Info("caught interrupt during import, will stop at next batch")
		}
		close(stop)
	}()
	checkInterrupt := func() bool {
		select {
		case <-stop:
			return true
		default:
			return false
		}
	}

	glog.Infoln("Importing blockchain", fn)
	fh, err := os.Open(fn)
	if err != nil {
		return err
	}
	defer fh.Close()
	stream := rlp.NewStream(fh, 0)

	// Run actual the import.
	blocks := make(types.Blocks, importBatchSize)
	n := 0
	for batch := 0; ; batch++ {
		// Load a batch of RLP blocks.
		if checkInterrupt() {
			return fmt.Errorf("interrupted")
		}
		i := 0
		for ; i < importBatchSize; i++ {
			var b types.Block
			if err := stream.Decode(&b); err == io.EOF {
				break
			} else if err != nil {
				return fmt.Errorf("at block %d: %v", n, err)
			}
			// don't import first block
			if b.NumberU64() == 0 {
				i--
				continue
			}
			blocks[i] = &b
			n++
		}
		if i == 0 {
			break
		}
		// Import the batch.
		if checkInterrupt() {
			return fmt.Errorf("interrupted")
		}
		if hasAllBlocks(chain, blocks[:i]) {
			glog.Infof("skipping batch %d, all blocks present [%x / %x]",
				batch, blocks[0].Hash().Bytes()[:4], blocks[i-1].Hash().Bytes()[:4])
			continue
		}

		if _, err := chain.InsertChain(blocks[:i]); err != nil {
			return fmt.Errorf("invalid block %d: %v", n, err)
		}
	}
	return nil
}

func hasAllBlocks(chain *core.BlockChain, bs []*types.Block) bool {
	for _, b := range bs {
		if !chain.HasBlock(b.Hash()) {
			return false
		}
	}
	return true
}

func ExportChain(blockchain *core.BlockChain, fn string) error {
	glog.Infoln("Exporting blockchain to", fn)
	fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
	if err != nil {
		return err
	}
	defer fh.Close()
	if err := blockchain.Export(fh); err != nil {
		return err
	}
	glog.Infoln("Exported blockchain to", fn)
	return nil
}

func ExportAppendChain(blockchain *core.BlockChain, fn string, first uint64, last uint64) error {
	glog.Infoln("Exporting blockchain to", fn)
	// TODO verify mode perms
	fh, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm)
	if err != nil {
		return err
	}
	defer fh.Close()
	if err := blockchain.ExportN(fh, first, last); err != nil {
		return err
	}
	glog.Infoln("Exported blockchain to", fn)
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"context"
	"log"
	"os"
	"strings"
	"time"

	"github.com/datawire/teleproxy/pkg/k8s"
	"github.com/datawire/teleproxy/pkg/limiter"
	"github.com/datawire/teleproxy/pkg/supervisor"
	"github.com/spf13/cobra"
)

var kubernetesNamespace string
var initialSources = make([]string, 0)
var initialFieldSelector string
var initialLabelSelector string
var watchHooks = make([]string, 0)
var notifyReceivers = make([]string, 0)
var port int
var interval time.Duration

var rootCmd = &cobra.Command{
	Use:              "watt",
	Short:            "watt",
	Long:             "watt - watch all the things",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {},
	Run:              runWatt,
}

func init() {
	rootCmd.Flags().StringVarP(&kubernetesNamespace, "namespace", "n", "", "namespace to watch (default: all)")
	rootCmd.Flags().StringSliceVarP(&initialSources, "source", "s", []string{}, "configure an initial static source")
	rootCmd.Flags().StringVar(&initialFieldSelector, "fields", "", "configure an initial field selector string")
	rootCmd.Flags().StringVar(&initialLabelSelector, "labels", "", "configure an initial label selector string")
	rootCmd.Flags().StringSliceVarP(&watchHooks, "watch", "w", []string{}, "configure watch hook(s)")
	rootCmd.Flags().StringSliceVar(&notifyReceivers, "notify", []string{},
		"invoke the program with the given arguments as a receiver")
	rootCmd.Flags().IntVarP(&port, "port", "p", 7000, "configure the snapshot server port")
	rootCmd.Flags().DurationVarP(&interval, "interval", "i", 250*time.Millisecond,
		"configure the rate limit interval")
}

func runWatt(cmd *cobra.Command, args []string) {
	os.Exit(_runWatt(cmd, args))
}

func _runWatt(cmd *cobra.Command, args []string) int {
	if len(initialSources) == 0 {
		log.Println("no initial sources configured")
		return 1
	}

	// XXX: we don't need to create this here anymore
	client, err := k8s.NewClient(nil)
	if err != nil {
		log.Println(err)
		return 1
	}
	kubeAPIWatcher := client.Watcher()
	/*for idx := range initialSources {
		initialSources[idx] = kubeAPIWatcher.Canonical(initialSources[idx])
	}*/

	log.Printf("starting watt...")

	// The aggregator sends the current consul resolver set to the
	// consul watch manager.
	aggregatorToConsulwatchmanCh := make(chan []ConsulWatchSpec)

	// The aggregator sends the current k8s watch set to the
	// kubernetes watch manager.
	aggregatorToKubewatchmanCh := make(chan []KubernetesWatchSpec)

	invoker := NewInvoker(port, notifyReceivers)
	limiter := limiter.NewComposite(limiter.NewUnlimited(), limiter.NewInterval(interval), interval)
	aggregator := NewAggregator(invoker.Snapshots, aggregatorToKubewatchmanCh, aggregatorToConsulwatchmanCh,
		initialSources, ExecWatchHook(watchHooks), limiter)

	kubebootstrap := kubebootstrap{
		namespace:      kubernetesNamespace,
		kinds:          initialSources,
		fieldSelector:  initialFieldSelector,
		labelSelector:  initialLabelSelector,
		kubeAPIWatcher: kubeAPIWatcher,
		notify:         []chan<- k8sEvent{aggregator.KubernetesEvents},
	}

	consulwatchman := consulwatchman{
		WatchMaker: &ConsulWatchMaker{aggregatorCh: aggregator.ConsulEvents},
		watchesCh:  aggregatorToConsulwatchmanCh,
		watched:    make(map[string]*supervisor.Worker),
	}

	kubewatchman := kubewatchman{
		WatchMaker: &KubernetesWatchMaker{kubeAPI: client, notify: aggregator.KubernetesEvents},
		in:         aggregatorToKubewatchmanCh,
	}

	apiServer := &apiServer{
		port:    port,
		invoker: invoker,
	}

	ctx := context.Background()
	s := supervisor.WithContext(ctx)

	s.Supervise(&supervisor.Worker{
		Name: "kubebootstrap",
		Work: kubebootstrap.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "consulwatchman",
		Work: consulwatchman.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "kubewatchman",
		Work: kubewatchman.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "aggregator",
		Work: aggregator.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "invoker",
		Work: invoker.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "api",
		Work: apiServer.Work,
	})

	if errs := s.Run(); len(errs) > 0 {
		msgs := []string{}
		for _, err := range errs {
			msgs = append(msgs, err.Error())
		}
		log.Printf("ERROR(s): %s", strings.Join(msgs, "\n    "))
		return 1
	}

	return 0
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		log.Println(err)
		os.Exit(1)
	}
}
<commit_msg>add version flag to watt<commit_after>package main

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

	"github.com/datawire/teleproxy/pkg/k8s"
	"github.com/datawire/teleproxy/pkg/limiter"
	"github.com/datawire/teleproxy/pkg/supervisor"
	"github.com/spf13/cobra"
)

// Version holds the version of the code. This is intended to be overridden at build time.
var Version = "(unknown version)"

var kubernetesNamespace string
var initialSources = make([]string, 0)
var initialFieldSelector string
var initialLabelSelector string
var watchHooks = make([]string, 0)
var notifyReceivers = make([]string, 0)
var port int
var interval time.Duration
var showVersion bool

var rootCmd = &cobra.Command{
	Use:              "watt",
	Short:            "watt",
	Long:             "watt - watch all the things",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {},
	Run:              runWatt,
}

func init() {
	rootCmd.Flags().StringVarP(&kubernetesNamespace, "namespace", "n", "", "namespace to watch (default: all)")
	rootCmd.Flags().StringSliceVarP(&initialSources, "source", "s", []string{}, "configure an initial static source")
	rootCmd.Flags().StringVar(&initialFieldSelector, "fields", "", "configure an initial field selector string")
	rootCmd.Flags().StringVar(&initialLabelSelector, "labels", "", "configure an initial label selector string")
	rootCmd.Flags().StringSliceVarP(&watchHooks, "watch", "w", []string{}, "configure watch hook(s)")
	rootCmd.Flags().StringSliceVar(&notifyReceivers, "notify", []string{},
		"invoke the program with the given arguments as a receiver")
	rootCmd.Flags().IntVarP(&port, "port", "p", 7000, "configure the snapshot server port")
	rootCmd.Flags().DurationVarP(&interval, "interval", "i", 250*time.Millisecond,
		"configure the rate limit interval")
	rootCmd.Flags().BoolVarP(&showVersion, "version", "", false, "display version information")
}

func runWatt(cmd *cobra.Command, args []string) {
	os.Exit(_runWatt(cmd, args))
}

func _runWatt(cmd *cobra.Command, args []string) int {
	if showVersion {
		fmt.Println("watt", Version)
		return 0
	}

	if len(initialSources) == 0 {
		log.Println("no initial sources configured")
		return 1
	}

	// XXX: we don't need to create this here anymore
	client, err := k8s.NewClient(nil)
	if err != nil {
		log.Println(err)
		return 1
	}
	kubeAPIWatcher := client.Watcher()
	/*for idx := range initialSources {
		initialSources[idx] = kubeAPIWatcher.Canonical(initialSources[idx])
	}*/

	log.Printf("starting watt...")

	// The aggregator sends the current consul resolver set to the
	// consul watch manager.
	aggregatorToConsulwatchmanCh := make(chan []ConsulWatchSpec)

	// The aggregator sends the current k8s watch set to the
	// kubernetes watch manager.
	aggregatorToKubewatchmanCh := make(chan []KubernetesWatchSpec)

	invoker := NewInvoker(port, notifyReceivers)
	limiter := limiter.NewComposite(limiter.NewUnlimited(), limiter.NewInterval(interval), interval)
	aggregator := NewAggregator(invoker.Snapshots, aggregatorToKubewatchmanCh, aggregatorToConsulwatchmanCh,
		initialSources, ExecWatchHook(watchHooks), limiter)

	kubebootstrap := kubebootstrap{
		namespace:      kubernetesNamespace,
		kinds:          initialSources,
		fieldSelector:  initialFieldSelector,
		labelSelector:  initialLabelSelector,
		kubeAPIWatcher: kubeAPIWatcher,
		notify:         []chan<- k8sEvent{aggregator.KubernetesEvents},
	}

	consulwatchman := consulwatchman{
		WatchMaker: &ConsulWatchMaker{aggregatorCh: aggregator.ConsulEvents},
		watchesCh:  aggregatorToConsulwatchmanCh,
		watched:    make(map[string]*supervisor.Worker),
	}

	kubewatchman := kubewatchman{
		WatchMaker: &KubernetesWatchMaker{kubeAPI: client, notify: aggregator.KubernetesEvents},
		in:         aggregatorToKubewatchmanCh,
	}

	apiServer := &apiServer{
		port:    port,
		invoker: invoker,
	}

	ctx := context.Background()
	s := supervisor.WithContext(ctx)

	s.Supervise(&supervisor.Worker{
		Name: "kubebootstrap",
		Work: kubebootstrap.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "consulwatchman",
		Work: consulwatchman.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "kubewatchman",
		Work: kubewatchman.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "aggregator",
		Work: aggregator.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "invoker",
		Work: invoker.Work,
	})

	s.Supervise(&supervisor.Worker{
		Name: "api",
		Work: apiServer.Work,
	})

	if errs := s.Run(); len(errs) > 0 {
		msgs := []string{}
		for _, err := range errs {
			msgs = append(msgs, err.Error())
		}
		log.Printf("ERROR(s): %s", strings.Join(msgs, "\n    "))
		return 1
	}

	return 0
}

func main() {
	if err := rootCmd.Execute(); err != nil {
		log.Println(err)
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package v2

import (
	"os"

	"code.cloudfoundry.org/cli/actors/v2actions"
	oldCmd "code.cloudfoundry.org/cli/cf/cmd"
	"code.cloudfoundry.org/cli/commands"
	"code.cloudfoundry.org/cli/commands/flags"
	"code.cloudfoundry.org/cli/commands/v2/common"
)

//go:generate counterfeiter . UnbindServiceActor

type UnbindServiceActor interface {
	UnbindServiceBySpace(appName string, serviceInstanceName string, spaceGUID string) (v2actions.Warnings, error)
}

type UnbindServiceCommand struct {
	RequiredArgs    flags.BindServiceArgs `positional-args:"yes"`
	usage           interface{}           `usage:"CF_NAME unbind-service APP_NAME SERVICE_INSTANCE"`
	relatedCommands interface{}           `related_commands:"apps, delete-service, services"`

	UI     commands.UI
	Actor  UnbindServiceActor
	Config commands.Config
}

func (cmd *UnbindServiceCommand) Setup(config commands.Config, ui commands.UI) error {
	cmd.UI = ui
	cmd.Config = config

	client, err := common.NewCloudControllerClient(config)
	if err != nil {
		return err
	}
	cmd.Actor = v2actions.NewActor(client)

	return nil
}

func (cmd UnbindServiceCommand) Execute(args []string) error {
	if cmd.Config.Experimental() == false {
		oldCmd.Main(os.Getenv("CF_TRACE"), os.Args)
		return nil
	}

	cmd.UI.DisplayText("This command is in EXPERIMENTAL stage and may change without notice")
	cmd.UI.DisplayNewline()

	err := common.CheckTarget(cmd.Config, true, true)
	if err != nil {
		return err
	}

	space := cmd.Config.TargetedSpace()
	user, err := cmd.Config.CurrentUser()
	if err != nil {
		return err
	}

	cmd.UI.DisplayHeaderFlavorText("Unbinding app {{.AppName}} from service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{
		"AppName":     cmd.RequiredArgs.AppName,
		"ServiceName": cmd.RequiredArgs.ServiceInstanceName,
		"OrgName":     cmd.Config.TargetedOrganization().Name,
		"SpaceName":   space.Name,
		"CurrentUser": user.Name,
	})

	warnings, err := cmd.Actor.UnbindServiceBySpace(cmd.RequiredArgs.AppName, cmd.RequiredArgs.ServiceInstanceName, space.GUID)
	cmd.UI.DisplayWarnings(warnings)
	if err != nil {
		if _, ok := err.(v2actions.ServiceBindingNotFoundError); ok {
			cmd.UI.DisplayWarning("Binding between {{.InstanceName}} and {{.AppName}} did not exist", map[string]interface{}{
				"AppName":      cmd.RequiredArgs.AppName,
				"InstanceName": cmd.RequiredArgs.ServiceInstanceName,
			})
		} else {
			return common.HandleError(err)
		}
	}

	cmd.UI.DisplayOK()

	return nil
}
<commit_msg>add new line to display<commit_after>package v2

import (
	"os"

	"code.cloudfoundry.org/cli/actors/v2actions"
	oldCmd "code.cloudfoundry.org/cli/cf/cmd"
	"code.cloudfoundry.org/cli/commands"
	"code.cloudfoundry.org/cli/commands/flags"
	"code.cloudfoundry.org/cli/commands/v2/common"
)

//go:generate counterfeiter . UnbindServiceActor

type UnbindServiceActor interface {
	UnbindServiceBySpace(appName string, serviceInstanceName string, spaceGUID string) (v2actions.Warnings, error)
}

type UnbindServiceCommand struct {
	RequiredArgs    flags.BindServiceArgs `positional-args:"yes"`
	usage           interface{}           `usage:"CF_NAME unbind-service APP_NAME SERVICE_INSTANCE"`
	relatedCommands interface{}           `related_commands:"apps, delete-service, services"`

	UI     commands.UI
	Actor  UnbindServiceActor
	Config commands.Config
}

func (cmd *UnbindServiceCommand) Setup(config commands.Config, ui commands.UI) error {
	cmd.UI = ui
	cmd.Config = config

	client, err := common.NewCloudControllerClient(config)
	if err != nil {
		return err
	}
	cmd.Actor = v2actions.NewActor(client)

	return nil
}

func (cmd UnbindServiceCommand) Execute(args []string) error {
	if cmd.Config.Experimental() == false {
		oldCmd.Main(os.Getenv("CF_TRACE"), os.Args)
		return nil
	}

	cmd.UI.DisplayText("This command is in EXPERIMENTAL stage and may change without notice")
	cmd.UI.DisplayNewline()

	err := common.CheckTarget(cmd.Config, true, true)
	if err != nil {
		return err
	}

	space := cmd.Config.TargetedSpace()
	user, err := cmd.Config.CurrentUser()
	if err != nil {
		return err
	}

	cmd.UI.DisplayHeaderFlavorText("Unbinding app {{.AppName}} from service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{
		"AppName":     cmd.RequiredArgs.AppName,
		"ServiceName": cmd.RequiredArgs.ServiceInstanceName,
		"OrgName":     cmd.Config.TargetedOrganization().Name,
		"SpaceName":   space.Name,
		"CurrentUser": user.Name,
	})

	warnings, err := cmd.Actor.UnbindServiceBySpace(cmd.RequiredArgs.AppName, cmd.RequiredArgs.ServiceInstanceName, space.GUID)
	cmd.UI.DisplayWarnings(warnings)
	if err != nil {
		if _, ok := err.(v2actions.ServiceBindingNotFoundError); ok {
			cmd.UI.DisplayWarning("Binding between {{.InstanceName}} and {{.AppName}} did not exist", map[string]interface{}{
				"AppName":      cmd.RequiredArgs.AppName,
				"InstanceName": cmd.RequiredArgs.ServiceInstanceName,
			})
		} else {
			return common.HandleError(err)
		}
	}

	cmd.UI.DisplayNewline()
	cmd.UI.DisplayOK()

	return nil
}
<|endoftext|>


<commit_before>package ls

import (
	"context"
	"os"

	"testing"
)

func TestMain(t *testing.T) {
	err := Main(context.Background(),[]string{"."}, os.Stdout, os.Stderr)
	if err != nil {
		t.Fatalf("ls .: %s", err.Error())
	}
	err = Main(context.Background(),[]string{"-l", "ls_test.go"}, os.Stdout, os.Stderr)
	if err != nil {
		t.Fatalf("ls *: %s", err.Error())
	}
}
<commit_msg>go fmt ls_test.go<commit_after>package ls

import (
	"context"
	"os"

	"testing"
)

func TestMain(t *testing.T) {
	err := Main(context.Background(), []string{"."}, os.Stdout, os.Stderr)
	if err != nil {
		t.Fatalf("ls .: %s", err.Error())
	}
	err = Main(context.Background(), []string{"-l", "ls_test.go"}, os.Stdout, os.Stderr)
	if err != nil {
		t.Fatalf("ls *: %s", err.Error())
	}
}
<|endoftext|>


<commit_before>package mpb_test

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/vbauerster/mpb"
	"github.com/vbauerster/mpb/decor"
)

func TestBarCompleted(t *testing.T) {
	p := mpb.New(mpb.WithOutput(ioutil.Discard))
	total := 80
	bar := p.AddBar(int64(total))

	var count int
	for !bar.Completed() {
		time.Sleep(10 * time.Millisecond)
		bar.Increment()
		count++
	}

	p.Wait()
	if count != total {
		t.Errorf("got count: %d, expected %d\n", count, total)
	}
}

func TestBarID(t *testing.T) {
	p := mpb.New(mpb.WithOutput(ioutil.Discard))

	numBars := 3
	bars := make([]*mpb.Bar, numBars)
	for i := 0; i < numBars; i++ {
		bars[i] = p.AddBar(80, mpb.BarID(i))
		go func(bar *mpb.Bar) {
			for i := 0; i < 80; i++ {
				time.Sleep(10 * time.Millisecond)
				bar.Increment()
			}
		}(bars[i])
	}

	p.Wait()
	for wantID, bar := range bars {
		gotID := bar.ID()
		if gotID != wantID {
			t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
		}
	}
}

func TestBarIncrWithReFill(t *testing.T) {
	var buf bytes.Buffer

	width := 100
	p := mpb.New(
		mpb.WithOutput(&buf),
		mpb.WithWidth(width),
	)

	total := 100
	till := 30
	refillChar := '+'

	bar := p.AddBar(100, mpb.BarTrim())

	bar.ResumeFill(refillChar, int64(till))

	for i := 0; i < total; i++ {
		bar.Increment()
		time.Sleep(10 * time.Millisecond)
	}

	p.Wait()

	wantBar := fmt.Sprintf("[%s%s]",
		strings.Repeat(string(refillChar), till-1),
		strings.Repeat("=", total-till-1))

	if !strings.Contains(buf.String(), wantBar) {
		t.Errorf("Want bar: %s, got bar: %s\n", wantBar, buf.String())
	}
}

func TestBarPanics(t *testing.T) {
	var wg sync.WaitGroup
	var buf bytes.Buffer
	p := mpb.New(mpb.WithOutput(&buf), mpb.WithWaitGroup(&wg))

	wantPanic := "Upps!!!"
	numBars := 3
	wg.Add(numBars)

	for i := 0; i < numBars; i++ {
		name := fmt.Sprintf("b#%02d:", i)
		bar := p.AddBar(100, mpb.BarID(i), mpb.PrependDecorators(
			func(s *decor.Statistics, _ chan<- int, _ <-chan int) string {
				if s.ID == 2 && s.Current >= 42 {
					panic(wantPanic)
				}
				return name
			},
		))

		go func() {
			defer wg.Done()
			for i := 0; i < 100; i++ {
				time.Sleep(10 * time.Millisecond)
				bar.Increment()
			}
		}()
	}

	p.Wait()

	wantPanic = fmt.Sprintf("b#%02d panic: %v", 2, wantPanic)

	if !strings.Contains(buf.String(), wantPanic) {
		t.Errorf("Want: %q, got: %q\n", wantPanic, buf.String())
	}
}
<commit_msg>dot import<commit_after>package mpb_test

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"strings"
	"sync"
	"testing"
	"time"

	. "github.com/vbauerster/mpb"
	"github.com/vbauerster/mpb/decor"
)

func TestBarCompleted(t *testing.T) {
	p := New(WithOutput(ioutil.Discard))
	total := 80
	bar := p.AddBar(int64(total))

	var count int
	for !bar.Completed() {
		time.Sleep(10 * time.Millisecond)
		bar.Increment()
		count++
	}

	p.Wait()
	if count != total {
		t.Errorf("got count: %d, expected %d\n", count, total)
	}
}

func TestBarID(t *testing.T) {
	p := New(WithOutput(ioutil.Discard))

	numBars := 3
	bars := make([]*Bar, numBars)
	for i := 0; i < numBars; i++ {
		bars[i] = p.AddBar(80, BarID(i))
		go func(bar *Bar) {
			for i := 0; i < 80; i++ {
				time.Sleep(10 * time.Millisecond)
				bar.Increment()
			}
		}(bars[i])
	}

	p.Wait()
	for wantID, bar := range bars {
		gotID := bar.ID()
		if gotID != wantID {
			t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
		}
	}
}

func TestBarIncrWithReFill(t *testing.T) {
	var buf bytes.Buffer

	width := 100
	p := New(WithOutput(&buf), WithWidth(width))

	total := 100
	till := 30
	refillChar := '+'

	bar := p.AddBar(100, BarTrim())

	bar.ResumeFill(refillChar, int64(till))

	for i := 0; i < total; i++ {
		bar.Increment()
		time.Sleep(10 * time.Millisecond)
	}

	p.Wait()

	wantBar := fmt.Sprintf("[%s%s]",
		strings.Repeat(string(refillChar), till-1),
		strings.Repeat("=", total-till-1))

	if !strings.Contains(buf.String(), wantBar) {
		t.Errorf("Want bar: %s, got bar: %s\n", wantBar, buf.String())
	}
}

func TestBarPanics(t *testing.T) {
	var wg sync.WaitGroup
	var buf bytes.Buffer
	p := New(WithOutput(&buf), WithWaitGroup(&wg))

	wantPanic := "Upps!!!"
	numBars := 3
	wg.Add(numBars)

	for i := 0; i < numBars; i++ {
		name := fmt.Sprintf("b#%02d:", i)
		bar := p.AddBar(100, BarID(i), PrependDecorators(
			func(s *decor.Statistics, _ chan<- int, _ <-chan int) string {
				if s.ID == 2 && s.Current >= 42 {
					panic(wantPanic)
				}
				return name
			},
		))

		go func() {
			defer wg.Done()
			for i := 0; i < 100; i++ {
				time.Sleep(10 * time.Millisecond)
				bar.Increment()
			}
		}()
	}

	p.Wait()

	wantPanic = fmt.Sprintf("b#%02d panic: %v", 2, wantPanic)

	if !strings.Contains(buf.String(), wantPanic) {
		t.Errorf("Want: %q, got: %q\n", wantPanic, buf.String())
	}
}
<|endoftext|>