<commit_before>package main

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"text/tabwriter"

	"github.com/chzyer/readline"
	"github.com/fatih/color"
)

type Shell struct {
	colibri       *Colibri
	currentFlower *Flower
	rl            *readline.Instance
}

func NewShell() *Shell {
	return &Shell{
		colibri: NewColibri(),
	}
}

func (shell *Shell) buildConfig() *readline.Config {
	return &readline.Config{
		Prompt: 	shell.buildPrompt(),
		AutoComplete:	shell.buildCompleter(),
	}
}

func (shell *Shell) buildPrompt() string {
	// let's create the fancy prompt
	promptSuffix := " "
	if shell.currentFlower != nil {
		promptSuffix += fmt.Sprintf("(%s~%s) ",
			color.CyanString(shell.currentFlower.Container.ShortID),
			color.YellowString(shell.currentFlower.Container.Name))
	}

	return fmt.Sprintf("%s%s", color.MagentaString("\\/°-"), promptSuffix)
}

func (shell *Shell) buildCompleter() *readline.PrefixCompleter {
	completer := readline.NewPrefixCompleter(
		readline.PcItem("fly-to",
			readline.PcItemDynamic(completeIdentifiers(shell.colibri))),
		readline.PcItem("ps"),
		readline.PcItem("clear"),
		readline.PcItem("exit"),
		readline.PcItem("help"),
	)

	// TODO add flower commands
	//completer.SetChildren()

	return completer
}

func completeIdentifiers(colibri *Colibri) func(string) []string {
	return func(line string) []string {
		colibri.Refresh()
		return append(colibri.ListNames(), colibri.ListShortIDs()...)
	}
}

func (shell *Shell) cmdFlyTo(identifier string) {
	flower := shell.colibri.GetFlower(identifier)

	switch flower {
	case nil:
		fmt.Fprintln(os.Stderr, color.RedString("Unknown container: is it a flower?"))
	default:
		// TODO parse flower

		shell.currentFlower = flower
		shell.Build()
	}
}

func (shell *Shell) cmdClear() {
	switch runtime.GOOS {
	case "windows":
		cmd := exec.Command("cmd", "/c", "cls")
		cmd.Stdout = os.Stdout
		cmd.Run()
	default:
		cmd := exec.Command("clear")
		cmd.Stdout = os.Stdout
		cmd.Run()
	}
}

func (shell *Shell) cmdExit() {
	switch shell.currentFlower {
	case nil:
		fmt.Fprintln(os.Stdout, "Bye!")
		os.Exit(0)
	default:
		shell.currentFlower = nil
		shell.Build()
	}
}

func (shell *Shell) cmdHelp() {
	fmt.Fprintln(os.Stdout, "USAGE")
	w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
	fmt.Fprintf(w, "%s\t\t\t%s\n", "fly-to CONTAINER", "Loads commands from CONTAINER (id or name).")
	fmt.Fprintf(w, "%s\t\t\t%s\n", "ps", "Shows containers which are exposing commands.")
	fmt.Fprintf(w, "%s\t\t\t%s\n", "clear", "Clears the screen.")
	fmt.Fprintf(w, "%s\t\t\t%s\n", "exit", "Quits the current flower or the application.")
	fmt.Fprintf(w, "%s\t\t\t%s\n", "help", "Shows this information.")
	w.Flush()
}

func (shell *Shell) Build() {
	switch shell.rl {
	case nil:
		rl, err := readline.NewEx(shell.buildConfig())
		if err != nil {
			panic(err)
		}

		shell.rl = rl
	default:
		shell.rl.SetConfig(shell.buildConfig())
	}
}

func (shell *Shell) Start() {
	defer shell.rl.Close()

	for {
		line, err := shell.rl.Readline()
		if err != nil {
			break
		}

		line = strings.TrimSpace(line)
		if len(line) == 0 {
			continue
		}

		args := strings.Fields(line)
		name := args[0]
		switch name {
		case  "fly-to":
			if len(args) > 1 {
				shell.cmdFlyTo(args[1])
			}
		case  "ps":
			println("List of flowers")
		case "clear":
			shell.cmdClear()
		case "exit":
			shell.cmdExit()
		case "help":
			shell.cmdHelp()
		}
	}
}

func main() {
	//shell := NewShell()
	//shell.Build()
	//shell.Start()
	colibri := NewColibri()
	colibri.Refresh()
	flower := colibri.GetFlower("examples_flower_1_1")
	_, err := flower.Parse()
	if err != nil {
		panic(err)
	}

}<commit_msg>deleting main.bak.go<commit_after><|endoftext|>


<commit_before>package router

import (
	"sync"

	"github.com/wandoulabs/codis/pkg/models"
	"github.com/wandoulabs/codis/pkg/proxy/group"
)

type Slot struct {
	Id int

	Info  *models.Slot
	Group *models.ServerGroup

	dst, src struct {
		group *group.Group
	}
	jobs sync.WaitGroup
}
<commit_msg>slot add rwlock<commit_after>package router

import (
	"sync"

	"github.com/wandoulabs/codis/pkg/models"
	"github.com/wandoulabs/codis/pkg/proxy/group"
)

type Slot struct {
	Id int

	Info  *models.Slot
	Group *models.ServerGroup

	dst, src struct {
		group *group.Group
	}
	lock sync.RWMutex
	jobs sync.WaitGroup
}

func (s *Slot) Acquire() {
	s.lock.Lock()
}

func (s *Slot) Release() {
	s.lock.Unlock()
}
<|endoftext|>


<commit_before>package jwt

import (
    "github.com/kataras/iris"
    "github.com/kataras/iris/core/router"
)

type jwtRouter struct {
    standard Standard
}

func (r *jwtRouter) Routes(server *iris.Application) {

    jwtRoutes := server.Party("/jwt")
    {
        jwtRoutes.Post("/generate", r.generate)

        jwtRoutes.Post("/auth", r.auth)

        jwtRoutes.Put("/upgrade", r.upgrade)
    }
}

func NewRouter(standard Standard) *jwtRouter {
    return &jwtRouter{
        standard: standard,
    }
}<commit_msg>remove unused package<commit_after>package jwt

import (
    "github.com/kataras/iris"
)

type jwtRouter struct {
    standard Standard
}

func (r *jwtRouter) Routes(server *iris.Application) {

    jwtRoutes := server.Party("/jwt")
    {
        jwtRoutes.Post("/generate", r.generate)

        jwtRoutes.Post("/auth", r.auth)

        jwtRoutes.Put("/upgrade", r.upgrade)
    }
}

func NewRouter(standard Standard) *jwtRouter {
    return &jwtRouter{
        standard: standard,
    }
}<|endoftext|>


<commit_before>/*
Copyright 2017 The Kubernetes Authors.

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 scheme

import (
	admissionv1 "k8s.io/api/admission/v1"
	admissionv1beta1 "k8s.io/api/admission/v1beta1"
	admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
	admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
	appsv1 "k8s.io/api/apps/v1"
	appsv1beta1 "k8s.io/api/apps/v1beta1"
	appsv1beta2 "k8s.io/api/apps/v1beta2"
	authenticationv1 "k8s.io/api/authentication/v1"
	authenticationv1beta1 "k8s.io/api/authentication/v1beta1"
	authorizationv1 "k8s.io/api/authorization/v1"
	authorizationv1beta1 "k8s.io/api/authorization/v1beta1"
	autoscalingv1 "k8s.io/api/autoscaling/v1"
	autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
	batchv1 "k8s.io/api/batch/v1"
	batchv1beta1 "k8s.io/api/batch/v1beta1"
	batchv2alpha1 "k8s.io/api/batch/v2alpha1"
	certificatesv1beta1 "k8s.io/api/certificates/v1beta1"
	corev1 "k8s.io/api/core/v1"
	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
	imagepolicyv1alpha1 "k8s.io/api/imagepolicy/v1alpha1"
	networkingv1 "k8s.io/api/networking/v1"
	policyv1beta1 "k8s.io/api/policy/v1beta1"
	rbacv1 "k8s.io/api/rbac/v1"
	rbacv1alpha1 "k8s.io/api/rbac/v1alpha1"
	rbacv1beta1 "k8s.io/api/rbac/v1beta1"
	schedulingv1alpha1 "k8s.io/api/scheduling/v1alpha1"
	settingsv1alpha1 "k8s.io/api/settings/v1alpha1"
	storagev1 "k8s.io/api/storage/v1"
	storagev1beta1 "k8s.io/api/storage/v1beta1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
	"k8s.io/apimachinery/pkg/runtime/schema"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/client-go/kubernetes/scheme"
)

// Register all groups in the kubectl's registry, but no componentconfig group since it's not in k8s.io/api
// The code in this file mostly duplicate the install under k8s.io/kubernetes/pkg/api and k8s.io/kubernetes/pkg/apis,
// but does NOT register the internal types.
func init() {
	// Register external types for Scheme
	metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
	utilruntime.Must(metav1beta1.AddMetaToScheme(Scheme))
	utilruntime.Must(metav1.AddMetaToScheme(Scheme))
	utilruntime.Must(scheme.AddToScheme(Scheme))

	utilruntime.Must(Scheme.SetVersionPriority(corev1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(admissionv1beta1.SchemeGroupVersion, admissionv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(admissionregistrationv1beta1.SchemeGroupVersion, admissionregistrationv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(appsv1beta1.SchemeGroupVersion, appsv1beta2.SchemeGroupVersion, appsv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(authenticationv1.SchemeGroupVersion, authenticationv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(authorizationv1.SchemeGroupVersion, authorizationv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(autoscalingv1.SchemeGroupVersion, autoscalingv2beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(batchv1.SchemeGroupVersion, batchv1beta1.SchemeGroupVersion, batchv2alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(certificatesv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(extensionsv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(imagepolicyv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(networkingv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(policyv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(rbacv1.SchemeGroupVersion, rbacv1beta1.SchemeGroupVersion, rbacv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(schedulingv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(settingsv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(storagev1.SchemeGroupVersion, storagev1beta1.SchemeGroupVersion))
}
<commit_msg>Copy CSR v1beta1 to v1<commit_after>/*
Copyright 2017 The Kubernetes Authors.

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 scheme

import (
	admissionv1 "k8s.io/api/admission/v1"
	admissionv1beta1 "k8s.io/api/admission/v1beta1"
	admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
	admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
	appsv1 "k8s.io/api/apps/v1"
	appsv1beta1 "k8s.io/api/apps/v1beta1"
	appsv1beta2 "k8s.io/api/apps/v1beta2"
	authenticationv1 "k8s.io/api/authentication/v1"
	authenticationv1beta1 "k8s.io/api/authentication/v1beta1"
	authorizationv1 "k8s.io/api/authorization/v1"
	authorizationv1beta1 "k8s.io/api/authorization/v1beta1"
	autoscalingv1 "k8s.io/api/autoscaling/v1"
	autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
	batchv1 "k8s.io/api/batch/v1"
	batchv1beta1 "k8s.io/api/batch/v1beta1"
	batchv2alpha1 "k8s.io/api/batch/v2alpha1"
	certificatesv1 "k8s.io/api/certificates/v1"
	certificatesv1beta1 "k8s.io/api/certificates/v1beta1"
	corev1 "k8s.io/api/core/v1"
	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
	imagepolicyv1alpha1 "k8s.io/api/imagepolicy/v1alpha1"
	networkingv1 "k8s.io/api/networking/v1"
	policyv1beta1 "k8s.io/api/policy/v1beta1"
	rbacv1 "k8s.io/api/rbac/v1"
	rbacv1alpha1 "k8s.io/api/rbac/v1alpha1"
	rbacv1beta1 "k8s.io/api/rbac/v1beta1"
	schedulingv1alpha1 "k8s.io/api/scheduling/v1alpha1"
	settingsv1alpha1 "k8s.io/api/settings/v1alpha1"
	storagev1 "k8s.io/api/storage/v1"
	storagev1beta1 "k8s.io/api/storage/v1beta1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
	"k8s.io/apimachinery/pkg/runtime/schema"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/client-go/kubernetes/scheme"
)

// Register all groups in the kubectl's registry, but no componentconfig group since it's not in k8s.io/api
// The code in this file mostly duplicate the install under k8s.io/kubernetes/pkg/api and k8s.io/kubernetes/pkg/apis,
// but does NOT register the internal types.
func init() {
	// Register external types for Scheme
	metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
	utilruntime.Must(metav1beta1.AddMetaToScheme(Scheme))
	utilruntime.Must(metav1.AddMetaToScheme(Scheme))
	utilruntime.Must(scheme.AddToScheme(Scheme))

	utilruntime.Must(Scheme.SetVersionPriority(corev1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(admissionv1beta1.SchemeGroupVersion, admissionv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(admissionregistrationv1beta1.SchemeGroupVersion, admissionregistrationv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(appsv1beta1.SchemeGroupVersion, appsv1beta2.SchemeGroupVersion, appsv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(authenticationv1.SchemeGroupVersion, authenticationv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(authorizationv1.SchemeGroupVersion, authorizationv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(autoscalingv1.SchemeGroupVersion, autoscalingv2beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(batchv1.SchemeGroupVersion, batchv1beta1.SchemeGroupVersion, batchv2alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(certificatesv1.SchemeGroupVersion, certificatesv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(extensionsv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(imagepolicyv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(networkingv1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(policyv1beta1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(rbacv1.SchemeGroupVersion, rbacv1beta1.SchemeGroupVersion, rbacv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(schedulingv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(settingsv1alpha1.SchemeGroupVersion))
	utilruntime.Must(Scheme.SetVersionPriority(storagev1.SchemeGroupVersion, storagev1beta1.SchemeGroupVersion))
}
<|endoftext|>


<commit_before>// +build windows

package main

import "github.com/tischda/timer/registry"

func init() {
	shell = "cmd"
	shellCmdFlag = "/c"
	timer = theTimer{registry: registry.RealRegistry()}
}
<commit_msg>not a constructor<commit_after>// +build windows

package main

import "github.com/tischda/timer/registry"

func init() {
	shell = "cmd"
	shellCmdFlag = "/c"
	timer = theTimer{registry: registry.RealRegistry{}}
}
<|endoftext|>


<commit_before>package hal

import (
	"fmt"
	"os"
	"strings"
)

var admins []string

// NewAuth returns a pointer to an initialized Auth
func NewAuth(r *Robot) *Auth {
	a := &Auth{robot: r}
	if admins := os.Getenv("HAL_AUTH_ADMIN"); admins != "" {
		a.admins = strings.Split(admins, ",")
	}

	r.Handle(assignUserRoleHandler)

	return a
}

// Auth type to group authentication methods
type Auth struct {
	robot  *Robot
	admins []string
}

// HasRole checks whether a user located by id has a given role(s)
func (a *Auth) HasRole(id string, roles ...string) bool {
	u, err := a.robot.Users.Get(id)
	if err != nil {
		return false
	}

	if len(u.Roles) == 0 {
		return false
	}

	for _, r := range roles {
		for _, b := range u.Roles {
			if b == r {
				return true
			}
		}
	}

	return false
}

// UsersWithRole returns a slice of Users that have a given role
func (a *Auth) UsersWithRole(role string) (users []User) {
	for _, u := range a.robot.Users.All() {
		if a.HasRole(u.ID, role) {
			users = append(users, u)
		}
	}
	return
}

func (a *Auth) addRole(u User, r string) error {
	if r == "admin" {
		return fmt.Errorf(`the "admin" role can only be defined by the HAL_AUTH_ADMIN environment variable`)
	}

	if a.HasRole(u.ID, r) {
		return fmt.Errorf("%s already has the %s role", u.Name, r)
	}

	u.Roles = append(u.Roles, r)
	a.robot.Users.Set(u.ID, u)

	return nil
}

func assignUserRole(res *Response) error {
	n := strings.TrimSpace(res.Match[1])
	r := strings.ToLower(res.Match[3])

	for _, i := range []string{"", "who", "what", "where", "when", "why"} {
		if i == n {
			return nil // don't match
		}
	}

	u, err := res.Robot.Users.GetByName(n)
	if err != nil {
		return res.Reply(n + " does not exist")
	}

	if err := res.Robot.Auth.addRole(u, r); err != nil {
		return res.Reply(err.Error())
	}

	return res.Reply(fmt.Sprintf("%s now has the %s role", n, r))
}

var assignUserRoleHandler = &Handler{
	Pattern: `(?i)@?(.+) (has) (["'\w: -_]+) (role)`,
	Method:  RESPOND,
	Run:     assignUserRole,
}
<commit_msg>auth role enhancements<commit_after>package hal

import (
	"fmt"
	"os"
	"strings"
)

var admins []string

// NewAuth returns a pointer to an initialized Auth
func NewAuth(r *Robot) *Auth {
	a := &Auth{robot: r}
	if admins := os.Getenv("HAL_AUTH_ADMIN"); admins != "" {
		a.admins = strings.Split(admins, ",")
	}

	r.Handle(
		addUserRoleHandler,
		removeUserRoleHandler,
	)

	return a
}

// Auth type to group authentication methods
type Auth struct {
	robot  *Robot
	admins []string
}

// HasRole checks whether a user located by id has a given role(s)
func (a *Auth) HasRole(id string, roles ...string) bool {
	u, err := a.robot.Users.Get(id)
	if err != nil {
		return false
	}

	if len(u.Roles) == 0 {
		return false
	}

	for _, r := range roles {
		for _, b := range u.Roles {
			if b == r {
				return true
			}
		}
	}

	return false
}

// UsersWithRole returns a slice of Users that have a given role
func (a *Auth) UsersWithRole(role string) (users []User) {
	for _, u := range a.robot.Users.All() {
		if a.HasRole(u.ID, role) {
			users = append(users, u)
		}
	}
	return
}

func (a *Auth) addRole(u User, r string) error {
	if r == "admin" {
		return fmt.Errorf(`the "admin" role can only be defined by the HAL_AUTH_ADMIN environment variable`)
	}

	if a.HasRole(u.ID, r) {
		return fmt.Errorf("%s already has the %s role", u.Name, r)
	}

	u.Roles = append(u.Roles, r)
	a.robot.Users.Set(u.ID, u)

	return nil
}

func (a *Auth) removeRole(u User, r string) error {
	if r == "admin" {
		return fmt.Errorf(`the "admin" role can only be defined by the HAL_AUTH_ADMIN environment variable`)
	}

	if !a.HasRole(u.ID, r) {
		return fmt.Errorf("%s already did not have the %s role", u.Name, r)
	}

	roles := make([]string, len(u.Roles)-1)

	for _, role := range u.Roles {
		if role != r {
			roles = append(roles, r)
		}
	}

	u.Roles = roles
	a.robot.Users.Set(u.ID, u)

	return nil
}

func addUserRole(res *Response) error {
	n := strings.TrimSpace(res.Match[1])
	r := strings.ToLower(res.Match[3])

	for _, i := range []string{"", "who", "what", "where", "when", "why"} {
		if i == n {
			return nil // don't match
		}
	}

	u, err := res.Robot.Users.GetByName(n)
	if err != nil {
		return res.Reply(err.Error())
	}

	if err := res.Robot.Auth.addRole(u, r); err != nil {
		return res.Reply(err.Error())
	}

	return res.Reply(fmt.Sprintf("%s now has the %s role", n, r))
}

var addUserRoleHandler = &Handler{
	Pattern: `(?i)@?(.+) (has)(?: the)? (["'\w: -_]+) (role)`,
	Method:  RESPOND,
	Run:     addUserRole,
}

func removeUserRole(res *Response) error {
	n := strings.TrimSpace(res.Match[1])
	r := strings.ToLower(res.Match[3])

	for _, i := range []string{"", "who", "what", "where", "when", "why"} {
		if i == n {
			return nil // don't match
		}
	}

	u, err := res.Robot.Users.GetByName(n)
	if err != nil {
		return res.Reply(err.Error())
	}

	if err := res.Robot.Auth.removeRole(u, r); err != nil {
		return res.Reply(err.Error())
	}

	return res.Reply(fmt.Sprintf("%s no longer has the %s role", n, r))
}

var removeUserRoleHandler = &Handler{
	Pattern: `(?i)@?(.+) (doesn't have|does not have)(?: the)? (["'\w: -_]+) (role)`,
	Method:  RESPOND,
	Run:     removeUserRole,
}
<|endoftext|>


<commit_before>package workloads

// Launcher defines how workload should be run using specified Provisioners.
type Launcher interface {
	// Launch launches the workload.
	// Returned Task is able to stop & monitor the provisioned process.
	Launch() (Task, error)
}
<commit_msg>Refactored comment.<commit_after>package workloads

// Launcher responsibility is to launch previously configured job.
type Launcher interface {
	// Launch launches the workload.
	// Returned Task is able to stop & monitor the provisioned process.
	Launch() (Task, error)
}
<|endoftext|>


<commit_before>//  Copyright 2018 Istio Authors
//
//  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 env

import (
	"fmt"
	"go/build"
	"os"
	"path"
	"strings"

	"runtime"

	"istio.io/pkg/log"
)

var (
	// GOPATH environment variable
	// nolint: golint
	GOPATH Variable = "GOPATH"

	// TOP environment variable
	// nolint: golint
	TOP Variable = "TOP"

	// ISTIO_GO environment variable
	// nolint: golint
	ISTIO_GO Variable = "ISTIO_GO"

	// ISTIO_BIN environment variable
	// nolint: golint
	ISTIO_BIN Variable = "ISTIO_BIN"

	// ISTIO_OUT environment variable
	// nolint: golint
	ISTIO_OUT Variable = "ISTIO_OUT"

	// HUB is the Docker hub to be used for images.
	// nolint: golint
	HUB Variable = "HUB"

	// TAG is the Docker tag to be used for images.
	// nolint: golint
	TAG Variable = "TAG"

	// PULL_POLICY is the image pull policy to use when rendering templates.
	// nolint: golint
	PULL_POLICY Variable = "PULL_POLICY"

	// ISTIO_TEST_KUBE_CONFIG is the Kubernetes configuration file to use for testing. If a configuration file
	// is specified on the command-line, that takes precedence.
	// nolint: golint
	ISTIO_TEST_KUBE_CONFIG Variable = "ISTIO_TEST_KUBE_CONFIG"

	// IstioTop has the top of the istio tree, matches the env variable from make.
	IstioTop = TOP.ValueOrDefaultFunc(getDefaultIstioTop)

	// IstioSrc is the location if istio source ($TOP/src/istio.io/istio
	IstioSrc = path.Join(IstioTop, "src/istio.io/istio")

	// IstioBin is the location of the binary output directory
	IstioBin = verifyFile(ISTIO_BIN, ISTIO_BIN.ValueOrDefaultFunc(getDefaultIstioBin))

	// IstioOut is the location of the output directory ($TOP/out)
	IstioOut = verifyFile(ISTIO_OUT, ISTIO_OUT.ValueOrDefaultFunc(getDefaultIstioOut))

	// TODO: Some of these values are overlapping. We should re-align them.

	// IstioRoot is the root of the Istio source repository.
	IstioRoot = path.Join(GOPATH.Value(), "/src/istio.io/istio")

	// ChartsDir is the Kubernetes Helm chart directory in the repository
	ChartsDir = path.Join(IstioRoot, "install/kubernetes/helm")

	// IstioChartDir is the Kubernetes Helm chart directory in the repository
	IstioChartDir = path.Join(ChartsDir, "istio")

	CrdsFilesDir = path.Join(ChartsDir, "istio-init/files")

	// BookInfoRoot is the root folder for the bookinfo samples
	BookInfoRoot = path.Join(IstioRoot, "samples/bookinfo")

	// BookInfoKube is the book info folder that contains Yaml deployment files.
	BookInfoKube = path.Join(BookInfoRoot, "platform/kube")

	// ServiceAccountFilePath is the helm service account file.
	ServiceAccountFilePath = path.Join(ChartsDir, "helm-service-account.yaml")

	// RedisInstallFilePath is the redis installation file.
	RedisInstallFilePath = path.Join(IstioRoot, "pkg/test/framework/components/redis/redis.yaml")
)

func getDefaultIstioTop() string {
	// Assume it is run inside istio.io/istio
	current, _ := os.Getwd()
	idx := strings.Index(current, "/src/istio.io/istio")
	if idx > 0 {
		return current[0:idx]
	}
	return current // launching from GOTOP (for example in goland)
}

func getDefaultIstioBin() string {
	return fmt.Sprintf("%s/bin", build.Default.GOPATH)
}

func getDefaultIstioOut() string {
	return fmt.Sprintf("%s/out/%s_%s", build.Default.GOPATH, runtime.GOOS, runtime.GOARCH)
}

func verifyFile(v Variable, f string) string {
	if !fileExists(f) {
		log.Warnf("unable to resolve %s. Dir %s does not exist", v, f)
		return ""
	}
	return f
}

func fileExists(f string) bool {
	return CheckFileExists(f) == nil
}

func CheckFileExists(path string) error {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return err
	}
	return nil
}
<commit_msg>Use default gopath when computing IstioRoot if GOPATH env var isn't set (#14709)<commit_after>//  Copyright 2018 Istio Authors
//
//  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 env

import (
	"fmt"
	"go/build"
	"os"
	"path"
	"strings"

	"runtime"

	"istio.io/pkg/log"
)

var (
	// GOPATH environment variable
	// nolint: golint
	GOPATH Variable = "GOPATH"

	// TOP environment variable
	// nolint: golint
	TOP Variable = "TOP"

	// ISTIO_GO environment variable
	// nolint: golint
	ISTIO_GO Variable = "ISTIO_GO"

	// ISTIO_BIN environment variable
	// nolint: golint
	ISTIO_BIN Variable = "ISTIO_BIN"

	// ISTIO_OUT environment variable
	// nolint: golint
	ISTIO_OUT Variable = "ISTIO_OUT"

	// HUB is the Docker hub to be used for images.
	// nolint: golint
	HUB Variable = "HUB"

	// TAG is the Docker tag to be used for images.
	// nolint: golint
	TAG Variable = "TAG"

	// PULL_POLICY is the image pull policy to use when rendering templates.
	// nolint: golint
	PULL_POLICY Variable = "PULL_POLICY"

	// ISTIO_TEST_KUBE_CONFIG is the Kubernetes configuration file to use for testing. If a configuration file
	// is specified on the command-line, that takes precedence.
	// nolint: golint
	ISTIO_TEST_KUBE_CONFIG Variable = "ISTIO_TEST_KUBE_CONFIG"

	// IstioTop has the top of the istio tree, matches the env variable from make.
	IstioTop = TOP.ValueOrDefaultFunc(getDefaultIstioTop)

	// IstioSrc is the location if istio source ($TOP/src/istio.io/istio
	IstioSrc = path.Join(IstioTop, "src/istio.io/istio")

	// IstioBin is the location of the binary output directory
	IstioBin = verifyFile(ISTIO_BIN, ISTIO_BIN.ValueOrDefaultFunc(getDefaultIstioBin))

	// IstioOut is the location of the output directory ($TOP/out)
	IstioOut = verifyFile(ISTIO_OUT, ISTIO_OUT.ValueOrDefaultFunc(getDefaultIstioOut))

	// TODO: Some of these values are overlapping. We should re-align them.

	// IstioRoot is the root of the Istio source repository.
	IstioRoot = path.Join(GOPATH.ValueOrDefault(build.Default.GOPATH), "/src/istio.io/istio")

	// ChartsDir is the Kubernetes Helm chart directory in the repository
	ChartsDir = path.Join(IstioRoot, "install/kubernetes/helm")

	// IstioChartDir is the Kubernetes Helm chart directory in the repository
	IstioChartDir = path.Join(ChartsDir, "istio")

	CrdsFilesDir = path.Join(ChartsDir, "istio-init/files")

	// BookInfoRoot is the root folder for the bookinfo samples
	BookInfoRoot = path.Join(IstioRoot, "samples/bookinfo")

	// BookInfoKube is the book info folder that contains Yaml deployment files.
	BookInfoKube = path.Join(BookInfoRoot, "platform/kube")

	// ServiceAccountFilePath is the helm service account file.
	ServiceAccountFilePath = path.Join(ChartsDir, "helm-service-account.yaml")

	// RedisInstallFilePath is the redis installation file.
	RedisInstallFilePath = path.Join(IstioRoot, "pkg/test/framework/components/redis/redis.yaml")
)

func getDefaultIstioTop() string {
	// Assume it is run inside istio.io/istio
	current, _ := os.Getwd()
	idx := strings.Index(current, "/src/istio.io/istio")
	if idx > 0 {
		return current[0:idx]
	}
	return current // launching from GOTOP (for example in goland)
}

func getDefaultIstioBin() string {
	return fmt.Sprintf("%s/bin", build.Default.GOPATH)
}

func getDefaultIstioOut() string {
	return fmt.Sprintf("%s/out/%s_%s", build.Default.GOPATH, runtime.GOOS, runtime.GOARCH)
}

func verifyFile(v Variable, f string) string {
	if !fileExists(f) {
		log.Warnf("unable to resolve %s. Dir %s does not exist", v, f)
		return ""
	}
	return f
}

func fileExists(f string) bool {
	return CheckFileExists(f) == nil
}

func CheckFileExists(path string) error {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"golang.org/x/crypto/bcrypt"
	"net/http"
)

func LoginHandler(w http.ResponseWriter, r *http.Request) {
	//Gets the username and password from the form data
	//Checks the validity of it. And sets cookie if valid.
	err := r.ParseForm()
	if err != nil {
		panic(err)
	}
	username := r.Form["name"][0]
	password := r.Form["password"][0]

	err = authorize(username, password)

	if err == nil {
		cookie := &http.Cookie{Name: "rcs", Value: username, MaxAge: 3600, Secure: false, HttpOnly: true, Raw: username}
		http.SetCookie(w, cookie)
		http.Redirect(w, r, "/", 302)
	} else {
		w.Write([]byte("Wrong password"))
	}
}

func SignUpHandler(w http.ResponseWriter, r *http.Request) {

	//Obtains username and password from the form
	//Generates bcrypt password
	//Inserts the username and hashed password into db
	//Creates a k8s namespace for the new user
	//Redirects the user back to the login page so that
	//he can login.

	err := r.ParseForm()

	if err != nil {
		panic(err)
	}

	username := r.Form["name"][0]
	password := r.Form["password"][0]

	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)

	if err != nil {
		fmt.Println(err)
	}
	_, err = DB.Exec("insert into login values($1, $2)", username, hashedPassword)

	CreateNamespace(username)

	http.Redirect(w, r, "/login", 302)
}

func authorize(username string, password string) (autherr error) {
	//Looks up the username and password in the database
	//check its validity

	var dbpassword string

	rows, err := DB.Query("Select password from login where name=$1", username)

	if err != nil {
		panic(err)
	}

	for rows.Next() {
		err = rows.Scan(&dbpassword)

		if err != nil {
			panic(err)
		}

		break

	}

	err = bcrypt.CompareHashAndPassword([]byte(dbpassword), []byte(password))

	if err == nil {

		return nil

	} else {
		autherr = fmt.Errorf("Cannot authorize %q", username)

		return autherr
	}

}
<commit_msg>Removed excessive newlines in auth.go<commit_after>package main

import (
	"fmt"
	"golang.org/x/crypto/bcrypt"
	"net/http"
)

func LoginHandler(w http.ResponseWriter, r *http.Request) {
	//Gets the username and password from the form data
	//Checks the validity of it. And sets cookie if valid.
	err := r.ParseForm()
	if err != nil {
		panic(err)
	}
	username := r.Form["name"][0]
	password := r.Form["password"][0]

	err = authorize(username, password)

	if err == nil {
		cookie := &http.Cookie{Name: "rcs", Value: username, MaxAge: 3600, Secure: false, HttpOnly: true, Raw: username}
		http.SetCookie(w, cookie)
		http.Redirect(w, r, "/", 302)
	} else {
		w.Write([]byte("Wrong password"))
	}
}

func SignUpHandler(w http.ResponseWriter, r *http.Request) {

	//Obtains username and password from the form
	//Generates bcrypt password
	//Inserts the username and hashed password into db
	//Creates a k8s namespace for the new user
	//Redirects the user back to the login page so that
	//he can login.

	err := r.ParseForm()

	if err != nil {
		panic(err)
	}

	username := r.Form["name"][0]
	password := r.Form["password"][0]
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)

	if err != nil {
		fmt.Println(err)
	}
	_, err = DB.Exec("insert into login values($1, $2)", username, hashedPassword)
	CreateNamespace(username)
	http.Redirect(w, r, "/login", 302)
}

func authorize(username string, password string) (autherr error) {
	//Looks up the username and password in the database
	//check its validity
	var dbpassword string
	rows, err := DB.Query("Select password from login where name=$1", username)

	if err != nil {
		panic(err)
	}

	for rows.Next() {
		err = rows.Scan(&dbpassword)
		if err != nil {
			panic(err)
		}
		break
	}

	err = bcrypt.CompareHashAndPassword([]byte(dbpassword), []byte(password))
	if err == nil {
		return nil
	} else {
		autherr = fmt.Errorf("Cannot authorize %q", username)
		return autherr
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2014 The Kubernetes Authors.

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 watch_test

import (
	"reflect"
	"sync"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/util/wait"
	. "k8s.io/apimachinery/pkg/watch"
)

type myType struct {
	ID    string
	Value string
}

func (obj *myType) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (obj *myType) DeepCopyObject() runtime.Object {
	if obj == nil {
		return nil
	}
	clone := *obj
	return &clone
}

func TestBroadcaster(t *testing.T) {
	table := []Event{
		{Type: Added, Object: &myType{"foo", "hello world 1"}},
		{Type: Added, Object: &myType{"bar", "hello world 2"}},
		{Type: Modified, Object: &myType{"foo", "goodbye world 3"}},
		{Type: Deleted, Object: &myType{"bar", "hello world 4"}},
	}

	// The broadcaster we're testing
	m := NewBroadcaster(0, WaitIfChannelFull)

	// Add a bunch of watchers
	const testWatchers = 2
	wg := sync.WaitGroup{}
	wg.Add(testWatchers)
	for i := 0; i < testWatchers; i++ {
		w, err := m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
		// Verify that each watcher gets the events in the correct order
		go func(watcher int, w Interface) {
			tableLine := 0
			for {
				event, ok := <-w.ResultChan()
				if !ok {
					break
				}
				if e, a := table[tableLine], event; !reflect.DeepEqual(e, a) {
					t.Errorf("Watcher %v, line %v: Expected (%v, %#v), got (%v, %#v)",
						watcher, tableLine, e.Type, e.Object, a.Type, a.Object)
				} else {
					t.Logf("Got (%v, %#v)", event.Type, event.Object)
				}
				tableLine++
			}
			wg.Done()
		}(i, w)
	}

	for i, item := range table {
		t.Logf("Sending %v", i)
		m.Action(item.Type, item.Object)
	}

	m.Shutdown()

	wg.Wait()
}

func TestBroadcasterWatcherClose(t *testing.T) {
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w.Stop()
	m.Shutdown()
	if _, open := <-w.ResultChan(); open {
		t.Errorf("Stop didn't work?")
	}
	if _, open := <-w2.ResultChan(); open {
		t.Errorf("Shutdown didn't work?")
	}
	// Extra stops don't hurt things
	w.Stop()
	w2.Stop()
}

func TestBroadcasterWatcherStopDeadlock(t *testing.T) {
	done := make(chan bool)
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	go func(w0, w1 Interface) {
		// We know Broadcaster is in the distribute loop once one watcher receives
		// an event. Stop the other watcher while distribute is trying to
		// send to it.
		select {
		case <-w0.ResultChan():
			w1.Stop()
		case <-w1.ResultChan():
			w0.Stop()
		}
		close(done)
	}(w, w2)
	m.Action(Added, &myType{})
	select {
	case <-time.After(wait.ForeverTestTimeout):
		t.Error("timeout: deadlocked")
	case <-done:
	}
	m.Shutdown()
}

func TestBroadcasterDropIfChannelFull(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	var err error
	for i := range watches {
		watches[i], err = m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
	}

	// Send a couple events before closing the broadcast channel.
	t.Log("Sending event 1")
	m.Action(event1.Type, event1.Object)
	t.Log("Sending event 2")
	m.Action(event2.Type, event2.Object)
	m.Shutdown()

	// Pull events from the queue.
	wg := sync.WaitGroup{}
	wg.Add(len(watches))
	for i := range watches {
		// Verify that each watcher only gets the first event because its watch
		// queue of length one was full from the first one.
		go func(watcher int, w Interface) {
			defer wg.Done()
			e1, ok := <-w.ResultChan()
			if !ok {
				t.Errorf("Watcher %v failed to retrieve first event.", watcher)
			}
			if e, a := event1, e1; !reflect.DeepEqual(e, a) {
				t.Errorf("Watcher %v: Expected (%v, %#v), got (%v, %#v)",
					watcher, e.Type, e.Object, a.Type, a.Object)
			}
			t.Logf("Got (%v, %#v)", e1.Type, e1.Object)
			e2, ok := <-w.ResultChan()
			if ok {
				t.Errorf("Watcher %v received second event (%v, %#v) even though it shouldn't have.",
					watcher, e2.Type, e2.Object)
			}
		}(i, watches[i])
	}
	wg.Wait()
}

func BenchmarkBroadCaster(b *testing.B) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	m := NewBroadcaster(0, WaitIfChannelFull)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			m.Action(event1.Type, event1.Object)
		}
	})
	b.StopTimer()
}

func TestBroadcasterWatchAfterShutdown(t *testing.T) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	m := NewBroadcaster(0, WaitIfChannelFull)
	m.Shutdown()

	_, err := m.Watch()
	assert.EqualError(t, err, "broadcaster already stopped", "Watch should report error id broadcaster is shutdown")

	_, err = m.WatchWithPrefix([]Event{event1, event2})
	assert.EqualError(t, err, "broadcaster already stopped", "WatchWithPrefix should report error id broadcaster is shutdown")
}

func TestBroadcasterSendEventAfterShutdown(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event := Event{Type: Added, Object: &myType{"foo", "hello world"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	for i := range watches {
		watches[i], _ = m.Watch()
	}
	m.Shutdown()

	// Send a couple events after closing the broadcast channel.
	t.Log("Sending event")

	err := m.Action(event.Type, event.Object)
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")

	sendOnClosed, err := m.ActionOrDrop(event.Type, event.Object)
	assert.Equal(t, sendOnClosed, false, "ActionOrDrop should return false if broadcaster is already shutdown")
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")
}
<commit_msg>Write Unit test to imitate Panic<commit_after>/*
Copyright 2014 The Kubernetes Authors.

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 watch

import (
	"reflect"
	"sync"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/util/wait"
)

type myType struct {
	ID    string
	Value string
}

func (obj *myType) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (obj *myType) DeepCopyObject() runtime.Object {
	if obj == nil {
		return nil
	}
	clone := *obj
	return &clone
}

func TestBroadcaster(t *testing.T) {
	table := []Event{
		{Type: Added, Object: &myType{"foo", "hello world 1"}},
		{Type: Added, Object: &myType{"bar", "hello world 2"}},
		{Type: Modified, Object: &myType{"foo", "goodbye world 3"}},
		{Type: Deleted, Object: &myType{"bar", "hello world 4"}},
	}

	// The broadcaster we're testing
	m := NewBroadcaster(0, WaitIfChannelFull)

	// Add a bunch of watchers
	const testWatchers = 2
	wg := sync.WaitGroup{}
	wg.Add(testWatchers)
	for i := 0; i < testWatchers; i++ {
		w, err := m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
		// Verify that each watcher gets the events in the correct order
		go func(watcher int, w Interface) {
			tableLine := 0
			for {
				event, ok := <-w.ResultChan()
				if !ok {
					break
				}
				if e, a := table[tableLine], event; !reflect.DeepEqual(e, a) {
					t.Errorf("Watcher %v, line %v: Expected (%v, %#v), got (%v, %#v)",
						watcher, tableLine, e.Type, e.Object, a.Type, a.Object)
				} else {
					t.Logf("Got (%v, %#v)", event.Type, event.Object)
				}
				tableLine++
			}
			wg.Done()
		}(i, w)
	}

	for i, item := range table {
		t.Logf("Sending %v", i)
		m.Action(item.Type, item.Object)
	}

	m.Shutdown()

	wg.Wait()
}

func TestBroadcasterWatcherClose(t *testing.T) {
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w.Stop()
	m.Shutdown()
	if _, open := <-w.ResultChan(); open {
		t.Errorf("Stop didn't work?")
	}
	if _, open := <-w2.ResultChan(); open {
		t.Errorf("Shutdown didn't work?")
	}
	// Extra stops don't hurt things
	w.Stop()
	w2.Stop()
}

func TestBroadcasterWatcherStopDeadlock(t *testing.T) {
	done := make(chan bool)
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	go func(w0, w1 Interface) {
		// We know Broadcaster is in the distribute loop once one watcher receives
		// an event. Stop the other watcher while distribute is trying to
		// send to it.
		select {
		case <-w0.ResultChan():
			w1.Stop()
		case <-w1.ResultChan():
			w0.Stop()
		}
		close(done)
	}(w, w2)
	m.Action(Added, &myType{})
	select {
	case <-time.After(wait.ForeverTestTimeout):
		t.Error("timeout: deadlocked")
	case <-done:
	}
	m.Shutdown()
}

func TestBroadcasterDropIfChannelFull(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	var err error
	for i := range watches {
		watches[i], err = m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
	}

	// Send a couple events before closing the broadcast channel.
	t.Log("Sending event 1")
	m.Action(event1.Type, event1.Object)
	t.Log("Sending event 2")
	m.Action(event2.Type, event2.Object)
	m.Shutdown()

	// Pull events from the queue.
	wg := sync.WaitGroup{}
	wg.Add(len(watches))
	for i := range watches {
		// Verify that each watcher only gets the first event because its watch
		// queue of length one was full from the first one.
		go func(watcher int, w Interface) {
			defer wg.Done()
			e1, ok := <-w.ResultChan()
			if !ok {
				t.Errorf("Watcher %v failed to retrieve first event.", watcher)
			}
			if e, a := event1, e1; !reflect.DeepEqual(e, a) {
				t.Errorf("Watcher %v: Expected (%v, %#v), got (%v, %#v)",
					watcher, e.Type, e.Object, a.Type, a.Object)
			}
			t.Logf("Got (%v, %#v)", e1.Type, e1.Object)
			e2, ok := <-w.ResultChan()
			if ok {
				t.Errorf("Watcher %v received second event (%v, %#v) even though it shouldn't have.",
					watcher, e2.Type, e2.Object)
			}
		}(i, watches[i])
	}
	wg.Wait()
}

func BenchmarkBroadCaster(b *testing.B) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	m := NewBroadcaster(0, WaitIfChannelFull)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			m.Action(event1.Type, event1.Object)
		}
	})
	b.StopTimer()
}

func TestBroadcasterWatchAfterShutdown(t *testing.T) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	m := NewBroadcaster(0, WaitIfChannelFull)
	m.Shutdown()

	_, err := m.Watch()
	assert.EqualError(t, err, "broadcaster already stopped", "Watch should report error id broadcaster is shutdown")

	_, err = m.WatchWithPrefix([]Event{event1, event2})
	assert.EqualError(t, err, "broadcaster already stopped", "WatchWithPrefix should report error id broadcaster is shutdown")
}

func TestBroadcasterSendEventAfterShutdown(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event := Event{Type: Added, Object: &myType{"foo", "hello world"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	for i := range watches {
		watches[i], _ = m.Watch()
	}
	m.Shutdown()

	// Send a couple events after closing the broadcast channel.
	t.Log("Sending event")

	err := m.Action(event.Type, event.Object)
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")

	sendOnClosed, err := m.ActionOrDrop(event.Type, event.Object)
	assert.Equal(t, sendOnClosed, false, "ActionOrDrop should return false if broadcaster is already shutdown")
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")
}

// Test this since we see usage patterns where the broadcaster and watchers are
// stopped simultaneously leading to races.
func TestBroadcasterShutdownRace(t *testing.T) {
	m := NewBroadcaster(1, WaitIfChannelFull)
	stopCh := make(chan struct{})

	// Add a bunch of watchers
	const testWatchers = 2
	for i := 0; i < testWatchers; i++ {
		i := i

		_, err := m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
		// This is how we force the watchers to close down independently of the
		// eventbroadcaster, see real usage pattern in startRecordingEvents()
		go func() {
			<-stopCh
			t.Log("Stopping Watchers")
			m.stopWatching(int64(i))
		}()
	}

	event := Event{Type: Added, Object: &myType{"foo", "hello world"}}
	err := m.Action(event.Type, event.Object)
	if err != nil {
		t.Fatalf("error sending event: %v", err)
	}

	// Manually simulate m.Shutdown() but change it to force a race scenario
	// 1. Close watcher stopchannel, so watchers are closed independently of the
	// eventBroadcaster
	// 2. Shutdown the m.incoming slightly Before m.stopped so that the watcher's
	// call of Blockqueue can pass the m.stopped check.
	m.blockQueue(func() {
		close(stopCh)
		close(m.incoming)
		time.Sleep(1 * time.Millisecond)
		close(m.stopped)
	})
	m.distributing.Wait()
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
)

type Server struct {
	hostport string
}

func (s *Server) fetchToFile(resource, file string) {
	target := s.getPath(resource)
	defer target.Body.Close()

	f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0755)

	if err != nil {
		log.Panicf("Couldn't open %s for writing", file, err)
	}

	io.Copy(f, target.Body)
}

func (s *Server) getPath(path string) *http.Response {
	resource := fmt.Sprintf("http://%s/%s", s.hostport)
	log.Printf("Fetching %s", resource)

	resp, err := http.Get(resource)
	if err != nil {
		log.Panicf("Couldn't fetch target", err)
	}

	return resp
}

func (s *Server) FetchTarget() {
	s.fetchToFile("target", "target")
}

func setupWorkDir() {
	var err error
	// TODO(richo) Ephemeral workdirs for concurrency
	if err = os.Mkdir("work", 0755); err != nil {
		log.Panicf("Couldn't make workdir", err)
	}
	if err = os.Chdir("work"); err != nil {
		log.Panicf("Couldn't change to workdir", err)
	}
}

func fetchCorpus(server string) {
}

func main() {
	args := os.Args
	if len(args) != 2 {
		log.Printf("Usage: ./client server:port")
		return
	}

	setupWorkDir()

	server := Server{args[1]}
	server.FetchTarget()
}
<commit_msg>Fix format string<commit_after>package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
)

type Server struct {
	hostport string
}

func (s *Server) fetchToFile(resource, file string) {
	target := s.getPath(resource)
	defer target.Body.Close()

	f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0755)

	if err != nil {
		log.Panicf("Couldn't open %s for writing", file, err)
	}

	io.Copy(f, target.Body)
}

func (s *Server) getPath(path string) *http.Response {
	resource := fmt.Sprintf("http://%s/%s", s.hostport, path)
	log.Printf("Fetching %s", resource)

	resp, err := http.Get(resource)
	if err != nil {
		log.Panicf("Couldn't fetch target", err)
	}

	return resp
}

func (s *Server) FetchTarget() {
	s.fetchToFile("target", "target")
}

func setupWorkDir() {
	var err error
	// TODO(richo) Ephemeral workdirs for concurrency
	if err = os.Mkdir("work", 0755); err != nil {
		log.Panicf("Couldn't make workdir", err)
	}
	if err = os.Chdir("work"); err != nil {
		log.Panicf("Couldn't change to workdir", err)
	}
}

func fetchCorpus(server string) {
}

func main() {
	args := os.Args
	if len(args) != 2 {
		log.Printf("Usage: ./client server:port")
		return
	}

	setupWorkDir()

	server := Server{args[1]}
	server.FetchTarget()
}
<|endoftext|>


<commit_before>// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
// snippet-start:[sts.go-v2.AssumeRole]
package main

import (
	"context"
	"flag"
	"fmt"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/sts"
)

// STSAssumeRoleAPI defines the interface for the AssumeRole function.
// We use this interface to test the function using a mocked service.
type STSAssumeRoleAPI interface {
	AssumeRole(ctx context.Context,
		params *sts.AssumeRoleInput,
		optFns ...func(*sts.Options)) (*sts.AssumeRoleOutput, error)
}

// TakeRole gets temporary security credentials to access resources.
// Inputs:
//     c is the context of the method call, which includes the AWS Region.
//     api is the interface that defines the method call.
//     input defines the input arguments to the service call.
// Output:
//     If successful, an AssumeRoleOutput object containing the result of the service call and nil.
//     Otherwise, nil and an error from the call to AssumeRole.
func TakeRole(c context.Context, api STSAssumeRoleAPI, input *sts.AssumeRoleInput) (*sts.AssumeRoleOutput, error) {
	return api.AssumeRole(c, input)
}

func main() {
	roleARN := flag.String("r", "", "The Amazon Resource Name (ARN) of the role to assume")
	sessionName := flag.String("s", "", "The name of the session")

	if *roleARN == "" || *sessionName == "" {
		fmt.Println("You must supply a role ARN and session name")
		fmt.Println("-r ROLE-ARN -s SESSION-NAME")
		return
	}

	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		panic("configuration error, " + err.Error())
	}

	client := sts.NewFromConfig(cfg)

	input := &sts.AssumeRoleInput{
		RoleArn:         roleARN,
		RoleSessionName: sessionName,
	}

	result, err := TakeRole(context.TODO(), client, input)
	if err != nil {
		fmt.Println("Got an error assuming the role:")
		fmt.Println(err)
		return
	}

	fmt.Println(result.AssumedRoleUser)
}

// snippet-end:[sts.go-v2.AssumeRole]
<commit_msg>add flag.parse for assume role (#2356)<commit_after>// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
// snippet-start:[sts.go-v2.AssumeRole]
package main

import (
	"context"
	"flag"
	"fmt"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/sts"
)

// STSAssumeRoleAPI defines the interface for the AssumeRole function.
// We use this interface to test the function using a mocked service.
type STSAssumeRoleAPI interface {
	AssumeRole(ctx context.Context,
		params *sts.AssumeRoleInput,
		optFns ...func(*sts.Options)) (*sts.AssumeRoleOutput, error)
}

// TakeRole gets temporary security credentials to access resources.
// Inputs:
//     c is the context of the method call, which includes the AWS Region.
//     api is the interface that defines the method call.
//     input defines the input arguments to the service call.
// Output:
//     If successful, an AssumeRoleOutput object containing the result of the service call and nil.
//     Otherwise, nil and an error from the call to AssumeRole.
func TakeRole(c context.Context, api STSAssumeRoleAPI, input *sts.AssumeRoleInput) (*sts.AssumeRoleOutput, error) {
	return api.AssumeRole(c, input)
}

func main() {
	roleARN := flag.String("r", "", "The Amazon Resource Name (ARN) of the role to assume")
	sessionName := flag.String("s", "", "The name of the session")
	flag.Parse()

	if *roleARN == "" || *sessionName == "" {
		fmt.Println("You must supply a role ARN and session name")
		fmt.Println("-r ROLE-ARN -s SESSION-NAME")
		return
	}

	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		panic("configuration error, " + err.Error())
	}

	client := sts.NewFromConfig(cfg)

	input := &sts.AssumeRoleInput{
		RoleArn:         roleARN,
		RoleSessionName: sessionName,
	}

	result, err := TakeRole(context.TODO(), client, input)
	if err != nil {
		fmt.Println("Got an error assuming the role:")
		fmt.Println(err)
		return
	}

	fmt.Println(result.AssumedRoleUser)
}

// snippet-end:[sts.go-v2.AssumeRole]
<|endoftext|>


<commit_before>package client

import (
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/unversioned"
	"k8s.io/kubernetes/pkg/client/restclient"
)

func GetAppControllerClient() (*AppControllerClient, error) {
	version := unversioned.GroupVersion{
		Version: "v1alpha1",
	}

	config := &restclient.Config{
		Host:    "http://localhost:8800",
		APIPath: "/apis/appcontroller.k8s",
		ContentConfig: restclient.ContentConfig{
			GroupVersion:         &version,
			NegotiatedSerializer: api.Codecs,
		},
	}
	client, err := New(config)
	if err != nil {
		return nil, err
	}
	return client, nil
}
<commit_msg>Provide k8s cluster url as a param, while creating a client<commit_after>package client

import (
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/unversioned"
	"k8s.io/kubernetes/pkg/client/restclient"
)

func GetAppControllerClient(url string) (*AppControllerClient, error) {
	version := unversioned.GroupVersion{
		Version: "v1alpha1",
	}

	config := &restclient.Config{
		Host:    url,
		APIPath: "/apis/appcontroller.k8s",
		ContentConfig: restclient.ContentConfig{
			GroupVersion:         &version,
			NegotiatedSerializer: api.Codecs,
		},
	}
	client, err := New(config)
	if err != nil {
		return nil, err
	}
	return client, nil
}
<|endoftext|>


<commit_before>package cluster

import (
	"fmt"
	"time"

	"github.com/gogo/protobuf/proto"
	"github.com/influxdb/influxdb/cluster/internal"
	"github.com/influxdb/influxdb/models"
)

//go:generate protoc --gogo_out=. internal/data.proto

// MapShardRequest represents the request to map a remote shard for a query.
type MapShardRequest struct {
	pb internal.MapShardRequest
}

// ShardID of the map request
func (m *MapShardRequest) ShardID() uint64 { return m.pb.GetShardID() }

// Query returns the Shard map request's query
func (m *MapShardRequest) Query() string { return m.pb.GetQuery() }

// ChunkSize returns Shard map request's chunk size
func (m *MapShardRequest) ChunkSize() int32 { return m.pb.GetChunkSize() }

// SetShardID sets the map request's shard id
func (m *MapShardRequest) SetShardID(id uint64) { m.pb.ShardID = &id }

// SetQuery sets the Shard map request's Query
func (m *MapShardRequest) SetQuery(query string) { m.pb.Query = &query }

// SetChunkSize sets the Shard map request's chunk size
func (m *MapShardRequest) SetChunkSize(chunkSize int32) { m.pb.ChunkSize = &chunkSize }

// MarshalBinary encodes the object to a binary format.
func (m *MapShardRequest) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&m.pb)
}

// UnmarshalBinary populates MapShardRequest from a binary format.
func (m *MapShardRequest) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &m.pb); err != nil {
		return err
	}
	return nil
}

// MapShardResponse represents the response returned from a remote MapShardRequest call
type MapShardResponse struct {
	pb internal.MapShardResponse
}

// NewMapShardResponse returns the response returned from a remote MapShardRequest call
func NewMapShardResponse(code int, message string) *MapShardResponse {
	m := &MapShardResponse{}
	m.SetCode(code)
	m.SetMessage(message)
	return m
}

// Code returns the Shard map response's code
func (r *MapShardResponse) Code() int { return int(r.pb.GetCode()) }

// Message returns the the Shard map response's Message
func (r *MapShardResponse) Message() string { return r.pb.GetMessage() }

// TagSets returns Shard map response's tag sets
func (r *MapShardResponse) TagSets() []string { return r.pb.GetTagSets() }

// Fields returns the Shard map response's Fields
func (r *MapShardResponse) Fields() []string { return r.pb.GetFields() }

// Data returns the Shard map response's Data
func (r *MapShardResponse) Data() []byte { return r.pb.GetData() }

// SetCode sets the Shard map response's code
func (r *MapShardResponse) SetCode(code int) { r.pb.Code = proto.Int32(int32(code)) }

// SetMessage sets Shard map response's message
func (r *MapShardResponse) SetMessage(message string) { r.pb.Message = &message }

// SetTagSets sets Shard map response's tagsets
func (r *MapShardResponse) SetTagSets(tagsets []string) { r.pb.TagSets = tagsets }

// SetFields sets the Shard map response's Fields
func (r *MapShardResponse) SetFields(fields []string) { r.pb.Fields = fields }

// SetData sets the Shard map response's Data
func (r *MapShardResponse) SetData(data []byte) { r.pb.Data = data }

// MarshalBinary encodes the object to a binary format.
func (r *MapShardResponse) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&r.pb)
}

// UnmarshalBinary populates WritePointRequest from a binary format.
func (r *MapShardResponse) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &r.pb); err != nil {
		return err
	}
	return nil
}

// WritePointsRequest represents a request to write point data to the cluster
type WritePointsRequest struct {
	Database         string
	RetentionPolicy  string
	ConsistencyLevel ConsistencyLevel
	Points           []models.Point
}

// AddPoint adds a point to the WritePointRequest with field name 'value'
func (w *WritePointsRequest) AddPoint(name string, value interface{}, timestamp time.Time, tags map[string]string) {
	w.Points = append(w.Points, models.NewPoint(
		name, tags, map[string]interface{}{"value": value}, timestamp,
	))
}

// WriteShardRequest represents the a request to write a slice of points to a shard
type WriteShardRequest struct {
	pb internal.WriteShardRequest
}

// WriteShardResponse represents the response returned from a remote WriteShardRequest call
type WriteShardResponse struct {
	pb internal.WriteShardResponse
}

// SetShardID sets the ShardID
func (w *WriteShardRequest) SetShardID(id uint64) { w.pb.ShardID = &id }

// ShardID gets the ShardID
func (w *WriteShardRequest) ShardID() uint64 { return w.pb.GetShardID() }

// Points returns the time series Points
func (w *WriteShardRequest) Points() []models.Point { return w.unmarshalPoints() }

// AddPoint adds a new time series point
func (w *WriteShardRequest) AddPoint(name string, value interface{}, timestamp time.Time, tags map[string]string) {
	w.AddPoints([]models.Point{models.NewPoint(
		name, tags, map[string]interface{}{"value": value}, timestamp,
	)})
}

// AddPoints adds a new time series point
func (w *WriteShardRequest) AddPoints(points []models.Point) {
	for _, p := range points {
		w.pb.Points = append(w.pb.Points, []byte(p.String()))
	}
}

// MarshalBinary encodes the object to a binary format.
func (w *WriteShardRequest) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&w.pb)
}

// UnmarshalBinary populates WritePointRequest from a binary format.
func (w *WriteShardRequest) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &w.pb); err != nil {
		return err
	}
	return nil
}

func (w *WriteShardRequest) unmarshalPoints() []models.Point {
	points := make([]models.Point, len(w.pb.GetPoints()))
	for i, p := range w.pb.GetPoints() {
		pt, err := models.ParsePoints(p)
		if err != nil {
			// A error here means that one node parsed the point correctly but sent an
			// unparseable version to another node.  We could log and drop the point and allow
			// anti-entropy to resolve the discrepancy but this shouldn't ever happen.
			panic(fmt.Sprintf("failed to parse point: `%v`: %v", string(p), err))
		}
		points[i] = pt[0]
	}
	return points
}

// SetCode sets the Code
func (w *WriteShardResponse) SetCode(code int) { w.pb.Code = proto.Int32(int32(code)) }

// SetMessage sets the Message
func (w *WriteShardResponse) SetMessage(message string) { w.pb.Message = &message }

// Code returns the Code
func (w *WriteShardResponse) Code() int { return int(w.pb.GetCode()) }

// Message returns the Message
func (w *WriteShardResponse) Message() string { return w.pb.GetMessage() }

// MarshalBinary encodes the object to a binary format.
func (w *WriteShardResponse) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&w.pb)
}

// UnmarshalBinary populates WritePointRequest from a binary format.
func (w *WriteShardResponse) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &w.pb); err != nil {
		return err
	}
	return nil
}
<commit_msg>Update rpc.go<commit_after>package cluster

import (
	"fmt"
	"time"

	"github.com/gogo/protobuf/proto"
	"github.com/influxdb/influxdb/cluster/internal"
	"github.com/influxdb/influxdb/models"
)

//go:generate protoc --gogo_out=. internal/data.proto

// MapShardRequest represents the request to map a remote shard for a query.
type MapShardRequest struct {
	pb internal.MapShardRequest
}

// ShardID of the map request
func (m *MapShardRequest) ShardID() uint64 { return m.pb.GetShardID() }

// Query returns the Shard map request's query
func (m *MapShardRequest) Query() string { return m.pb.GetQuery() }

// ChunkSize returns Shard map request's chunk size
func (m *MapShardRequest) ChunkSize() int32 { return m.pb.GetChunkSize() }

// SetShardID sets the map request's shard id
func (m *MapShardRequest) SetShardID(id uint64) { m.pb.ShardID = &id }

// SetQuery sets the Shard map request's Query
func (m *MapShardRequest) SetQuery(query string) { m.pb.Query = &query }

// SetChunkSize sets the Shard map request's chunk size
func (m *MapShardRequest) SetChunkSize(chunkSize int32) { m.pb.ChunkSize = &chunkSize }

// MarshalBinary encodes the object to a binary format.
func (m *MapShardRequest) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&m.pb)
}

// UnmarshalBinary populates MapShardRequest from a binary format.
func (m *MapShardRequest) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &m.pb); err != nil {
		return err
	}
	return nil
}

// MapShardResponse represents the response returned from a remote MapShardRequest call
type MapShardResponse struct {
	pb internal.MapShardResponse
}

// NewMapShardResponse returns the response returned from a remote MapShardRequest call
func NewMapShardResponse(code int, message string) *MapShardResponse {
	m := &MapShardResponse{}
	m.SetCode(code)
	m.SetMessage(message)
	return m
}

// Code returns the Shard map response's code
func (r *MapShardResponse) Code() int { return int(r.pb.GetCode()) }

// Message returns the the Shard map response's Message
func (r *MapShardResponse) Message() string { return r.pb.GetMessage() }

// TagSets returns Shard map response's tag sets
func (r *MapShardResponse) TagSets() []string { return r.pb.GetTagSets() }

// Fields returns the Shard map response's Fields
func (r *MapShardResponse) Fields() []string { return r.pb.GetFields() }

// Data returns the Shard map response's Data
func (r *MapShardResponse) Data() []byte { return r.pb.GetData() }

// SetCode sets the Shard map response's code
func (r *MapShardResponse) SetCode(code int) { r.pb.Code = proto.Int32(int32(code)) }

// SetMessage sets Shard map response's message
func (r *MapShardResponse) SetMessage(message string) { r.pb.Message = &message }

// SetTagSets sets Shard map response's tagsets
func (r *MapShardResponse) SetTagSets(tagsets []string) { r.pb.TagSets = tagsets }

// SetFields sets the Shard map response's Fields
func (r *MapShardResponse) SetFields(fields []string) { r.pb.Fields = fields }

// SetData sets the Shard map response's Data
func (r *MapShardResponse) SetData(data []byte) { r.pb.Data = data }

// MarshalBinary encodes the object to a binary format.
func (r *MapShardResponse) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&r.pb)
}

// UnmarshalBinary populates WritePointRequest from a binary format.
func (r *MapShardResponse) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &r.pb); err != nil {
		return err
	}
	return nil
}

// WritePointsRequest represents a request to write point data to the cluster
type WritePointsRequest struct {
	Database         string
	RetentionPolicy  string
	ConsistencyLevel ConsistencyLevel
	Points           []models.Point
}

// AddPoint adds a point to the WritePointRequest with field key 'value'
func (w *WritePointsRequest) AddPoint(name string, value interface{}, timestamp time.Time, tags map[string]string) {
	w.Points = append(w.Points, models.NewPoint(
		name, tags, map[string]interface{}{"value": value}, timestamp,
	))
}

// WriteShardRequest represents the a request to write a slice of points to a shard
type WriteShardRequest struct {
	pb internal.WriteShardRequest
}

// WriteShardResponse represents the response returned from a remote WriteShardRequest call
type WriteShardResponse struct {
	pb internal.WriteShardResponse
}

// SetShardID sets the ShardID
func (w *WriteShardRequest) SetShardID(id uint64) { w.pb.ShardID = &id }

// ShardID gets the ShardID
func (w *WriteShardRequest) ShardID() uint64 { return w.pb.GetShardID() }

// Points returns the time series Points
func (w *WriteShardRequest) Points() []models.Point { return w.unmarshalPoints() }

// AddPoint adds a new time series point
func (w *WriteShardRequest) AddPoint(name string, value interface{}, timestamp time.Time, tags map[string]string) {
	w.AddPoints([]models.Point{models.NewPoint(
		name, tags, map[string]interface{}{"value": value}, timestamp,
	)})
}

// AddPoints adds a new time series point
func (w *WriteShardRequest) AddPoints(points []models.Point) {
	for _, p := range points {
		w.pb.Points = append(w.pb.Points, []byte(p.String()))
	}
}

// MarshalBinary encodes the object to a binary format.
func (w *WriteShardRequest) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&w.pb)
}

// UnmarshalBinary populates WritePointRequest from a binary format.
func (w *WriteShardRequest) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &w.pb); err != nil {
		return err
	}
	return nil
}

func (w *WriteShardRequest) unmarshalPoints() []models.Point {
	points := make([]models.Point, len(w.pb.GetPoints()))
	for i, p := range w.pb.GetPoints() {
		pt, err := models.ParsePoints(p)
		if err != nil {
			// A error here means that one node parsed the point correctly but sent an
			// unparseable version to another node.  We could log and drop the point and allow
			// anti-entropy to resolve the discrepancy but this shouldn't ever happen.
			panic(fmt.Sprintf("failed to parse point: `%v`: %v", string(p), err))
		}
		points[i] = pt[0]
	}
	return points
}

// SetCode sets the Code
func (w *WriteShardResponse) SetCode(code int) { w.pb.Code = proto.Int32(int32(code)) }

// SetMessage sets the Message
func (w *WriteShardResponse) SetMessage(message string) { w.pb.Message = &message }

// Code returns the Code
func (w *WriteShardResponse) Code() int { return int(w.pb.GetCode()) }

// Message returns the Message
func (w *WriteShardResponse) Message() string { return w.pb.GetMessage() }

// MarshalBinary encodes the object to a binary format.
func (w *WriteShardResponse) MarshalBinary() ([]byte, error) {
	return proto.Marshal(&w.pb)
}

// UnmarshalBinary populates WritePointRequest from a binary format.
func (w *WriteShardResponse) UnmarshalBinary(buf []byte) error {
	if err := proto.Unmarshal(buf, &w.pb); err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>/*
 * Mini Copy (C) 2014, 2015 Minio, 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 (
	"fmt"
	"strings"

	"github.com/minio-io/cli"
	"github.com/minio-io/mc/pkg/console"
)

// List of commands
var (
	accessCmd = cli.Command{
		Name:   "access",
		Usage:  "Set permissions [public, private, readonly] for buckets and folders.",
		Action: runAccessCmd,
		CustomHelpTemplate: `Name:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} PERMISSION TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:

   1. Set bucket to "private" on Amazon S3 object storage
      $ mc {{.Name}} private https://s3.amazonaws.com/burningman2011

   2. Set bucket to "public" on Amazon S3 object storage
      $ mc {{.Name}} public https://s3.amazonaws.com/shared

   3. Set folder to world readwrite (chmod 777) on local filesystem
      $ mc {{.Name}} public /shared/Music

`,
	}
	catCmd = cli.Command{
		Name:   "cat",
		Usage:  "Concantenate objects or files to standard output",
		Action: runCatCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} SOURCE {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. Concantenate an object from Amazon S3 object storage to mplayer standard input
      $ mc {{.Name}} https://s3.amazonaws.com/jukebox/klingon_opera_aktuh_maylotah.ogg | mplayer -

   2. Concantenate a file from local filesystem to standard output.
      $ mc {{.Name}} khitomer-accords.txt

   3. Concantenate multiple files from local filesystem to standard output.
      $ mc {{.Name}} *.txt > newfile.txt
`,
	}
	cpCmd = cli.Command{
		Name:   "cp",
		Usage:  "Copy objects and files",
		Action: runCopyCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} SOURCE TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. Copy an object from Amazon S3 object storage to local fileystem.
      $ mc {{.Name}} https://s3.amazonaws.com/jukebox/klingon_opera_aktuh_maylotah.ogg wakeup.ogg

   2. Copy a bucket recursively from Minio object storage to Amazon S3 object storage
      $ mc {{.Name}} http://localhost:9000/photos/burningman2011... https://s3.amazonaws.com/burningman/

   3. Copy a local folder recursively to Minio object storage and Amazon S3 object storage
      $ mc {{.Name}} backup/... http://localhost:9000/archive/ https://s3.amazonaws.com/archive/

   4. Copy an object from Amazon S3 object storage to local filesystem on Windows.
      $ mc {{.Name}} s3:documents/2014/... Documents\backup\2014

`,
	}

	lsCmd = cli.Command{
		Name:   "ls",
		Usage:  "List files and objects",
		Action: runListCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}} TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. List objects on Minio object storage
      $ mc {{.Name}} http://localhost:9000/backup/
      2015-03-28 12:47:50 PDT      51.00 MB 2006-Jan-1/backup.tar.gz
      2015-03-31 14:46:33 PDT      55.00 MB 2006-Mar-1/backup.tar.gz

   2. List buckets on Amazon S3 object storage
      $ mc {{.Name}} https://s3.amazonaws.com/
      2015-01-20 15:42:00 PST               rom
      2015-01-15 00:05:40 PST               zek

   3. List buckets and objects from Minio object storage and Amazon S3 object storage
      $ mc {{.Name}} https://s3.amazonaws.com/ http://localhost:9000/backup/
      2015-01-20 15:42:00 PST               rom
      2015-01-15 00:05:40 PST               zek
      2015-03-28 12:47:50 PDT      51.00 MB 2006-Jan-1/backup.tar.gz
      2015-03-31 14:46:33 PDT      55.00 MB 2006-Mar-1/backup.tar.gz

   4. List objects on local filesystem on Windows
      $ mc {{.Name}} C:\Users\Worf
      2015-03-28 12:47:50 PDT      11.00 MB Martok\Klingon Council Ministers.pdf
      2015-03-31 14:46:33 PDT      15.00 MB Gowron\Khitomer Conference Details.pdf

`,
	}

	mbCmd = cli.Command{
		Name:   "mb",
		Usage:  "Make a bucket",
		Action: runMakeBucketCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}} TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. Create a bucket on Amazon S3 object storage
      $ mc {{.Name}} https://s3.amazonaws.com/public-document-store

   2. Create a bucket on Minio object storage
      $ mc {{.Name}} http://localhost:9000/mongodb-backup

   3. Create multiple buckets on Amazon S3 object storage and Minio object storage
      $ mc {{.Name}} https://s3.amazonaws.com/public-photo-store https://s3.amazonaws.com/public-store http://localhost:9000/mongodb-backup

`,
	}
	//   Configure minio client
	//
	//   ----
	//   NOTE: that the configure command only writes values to the config file.
	//   It does not use any configuration values from the environment variables.
	//
	//   One needs to edit configuration file manually, this is purposefully done
	//   so to avoid taking credentials over cli arguments. It is a security precaution
	//   ----
	//
	configCmd = cli.Command{
		Name:   "config",
		Usage:  "Generate configuration \"" + mustGetMcConfigPath() + "\" file.",
		Action: runConfigCmd,
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "alias",
				Usage: "Add URL aliases into config",
			},
		},
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}} generate {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}
EXAMPLES:
   1. Generate mc config
      $ mc config generate

   2. Add alias URLs
      $ mc config --alias "zek https://s3.amazonaws.com/"

`,
	}
	updateCmd = cli.Command{
		Name:        "update",
		Usage:       "Check for new software updates",
		Description: "",
		Action:      doUpdateCmd,
		Flags: []cli.Flag{
			cli.BoolFlag{
				Name:  "yes, y",
				Usage: "Download and update local binary",
			},
		},
	}
)

// Collection of mc commands currently supported are
//
// ----
// ls     List files and objects
// cp     Copy objects and files
// mb     Make a bucket
// access Set permissions [public, private, readonly] for buckets and folders.
// cat    Concantenate an object to standard output
// config Generate configuration "/home/harsha/.mc/config.json" file.
// update Check for new software updates
// ----
//
var commands = []cli.Command{
	lsCmd,
	cpCmd,
	mbCmd,
	accessCmd,
	catCmd,
	configCmd,
	updateCmd,
	// Add your new commands starting from here
}

// Collection of mc flags currently supported
//
// --theme       "minimal"Choose a console theme from this list [*minimal*, nocolor, white]
// --debug       Enable HTTP tracing
// --quiet, -q   Supress chatty console output
// --retry "5"   Number of retry count
// --version, -v print the version
//
var (
	flags = []cli.Flag{
		cli.StringFlag{
			Name:  "theme",
			Value: console.GetDefaultThemeName(),
			Usage: fmt.Sprintf("Choose a console theme from this list [%s]", func() string {
				keys := []string{}
				for _, themeName := range console.GetThemeNames() {
					if console.GetThemeName() == themeName {
						themeName = "*" + themeName + "*"
					}
					keys = append(keys, themeName)
				}
				return strings.Join(keys, ", ")
			}()),
		},
		cli.BoolFlag{
			Name:  "debug",
			Usage: "Enable HTTP tracing",
		},
		cli.BoolFlag{
			Name:  "quiet, q",
			Usage: "Supress chatty console output",
		},
		cli.IntFlag{
			Name:  "retry",
			Usage: "Number of retry count",
			Value: 5,
		},
		// Add your new flags starting here
	}
)
<commit_msg>Update cp documentation no multi target<commit_after>/*
 * Mini Copy (C) 2014, 2015 Minio, 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 (
	"fmt"
	"strings"

	"github.com/minio-io/cli"
	"github.com/minio-io/mc/pkg/console"
)

// List of commands
var (
	accessCmd = cli.Command{
		Name:   "access",
		Usage:  "Set permissions [public, private, readonly] for buckets and folders.",
		Action: runAccessCmd,
		CustomHelpTemplate: `Name:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} PERMISSION TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:

   1. Set bucket to "private" on Amazon S3 object storage
      $ mc {{.Name}} private https://s3.amazonaws.com/burningman2011

   2. Set bucket to "public" on Amazon S3 object storage
      $ mc {{.Name}} public https://s3.amazonaws.com/shared

   3. Set folder to world readwrite (chmod 777) on local filesystem
      $ mc {{.Name}} public /shared/Music

`,
	}
	catCmd = cli.Command{
		Name:   "cat",
		Usage:  "Concantenate objects or files to standard output",
		Action: runCatCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} SOURCE {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. Concantenate an object from Amazon S3 object storage to mplayer standard input
      $ mc {{.Name}} https://s3.amazonaws.com/jukebox/klingon_opera_aktuh_maylotah.ogg | mplayer -

   2. Concantenate a file from local filesystem to standard output.
      $ mc {{.Name}} khitomer-accords.txt

   3. Concantenate multiple files from local filesystem to standard output.
      $ mc {{.Name}} *.txt > newfile.txt
`,
	}
	cpCmd = cli.Command{
		Name:   "cp",
		Usage:  "Copy objects and files",
		Action: runCopyCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} SOURCE TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. Copy an object from Amazon S3 object storage to local fileystem.
      $ mc {{.Name}} Music/*.ogg https://s3.amazonaws.com/jukebox/

   2. Copy a bucket recursively from Minio object storage to Amazon S3 object storage
      $ mc {{.Name}} http://localhost:9000/photos/burningman2011... https://s3.amazonaws.com/burningman/

   3. Copy a local folder recursively to Minio object storage and Amazon S3 object storage
      $ mc {{.Name}} backup/... http://localhost:9000/archive/

   4. Copy an object from Amazon S3 object storage to local filesystem on Windows.
      $ mc {{.Name}} s3:documents/2014/... backup/2014

`,
	}

	lsCmd = cli.Command{
		Name:   "ls",
		Usage:  "List files and objects",
		Action: runListCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}} TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. List objects on Minio object storage
      $ mc {{.Name}} http://localhost:9000/backup/
      2015-03-28 12:47:50 PDT      51.00 MB 2006-Jan-1/backup.tar.gz
      2015-03-31 14:46:33 PDT      55.00 MB 2006-Mar-1/backup.tar.gz

   2. List buckets on Amazon S3 object storage
      $ mc {{.Name}} https://s3.amazonaws.com/
      2015-01-20 15:42:00 PST               rom
      2015-01-15 00:05:40 PST               zek

   3. List buckets and objects from Minio object storage and Amazon S3 object storage
      $ mc {{.Name}} https://s3.amazonaws.com/ http://localhost:9000/backup/
      2015-01-20 15:42:00 PST               rom
      2015-01-15 00:05:40 PST               zek
      2015-03-28 12:47:50 PDT      51.00 MB 2006-Jan-1/backup.tar.gz
      2015-03-31 14:46:33 PDT      55.00 MB 2006-Mar-1/backup.tar.gz

   4. List objects on local filesystem on Windows
      $ mc {{.Name}} C:\Users\Worf
      2015-03-28 12:47:50 PDT      11.00 MB Martok\Klingon Council Ministers.pdf
      2015-03-31 14:46:33 PDT      15.00 MB Gowron\Khitomer Conference Details.pdf

`,
	}

	mbCmd = cli.Command{
		Name:   "mb",
		Usage:  "Make a bucket",
		Action: runMakeBucketCmd,
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}} TARGET [TARGET...] {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}

EXAMPLES:
   1. Create a bucket on Amazon S3 object storage
      $ mc {{.Name}} https://s3.amazonaws.com/public-document-store

   2. Create a bucket on Minio object storage
      $ mc {{.Name}} http://localhost:9000/mongodb-backup

   3. Create multiple buckets on Amazon S3 object storage and Minio object storage
      $ mc {{.Name}} https://s3.amazonaws.com/public-photo-store https://s3.amazonaws.com/public-store http://localhost:9000/mongodb-backup

`,
	}
	//   Configure minio client
	//
	//   ----
	//   NOTE: that the configure command only writes values to the config file.
	//   It does not use any configuration values from the environment variables.
	//
	//   One needs to edit configuration file manually, this is purposefully done
	//   so to avoid taking credentials over cli arguments. It is a security precaution
	//   ----
	//
	configCmd = cli.Command{
		Name:   "config",
		Usage:  "Generate configuration \"" + mustGetMcConfigPath() + "\" file.",
		Action: runConfigCmd,
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "alias",
				Usage: "Add URL aliases into config",
			},
		},
		CustomHelpTemplate: `NAME:
   mc {{.Name}} - {{.Usage}}

USAGE:
   mc {{.Name}} generate {{if .Description}}

DESCRIPTION:
   {{.Description}}{{end}}{{if .Flags}}

FLAGS:
   {{range .Flags}}{{.}}
   {{end}}{{ end }}
EXAMPLES:
   1. Generate mc config
      $ mc config generate

   2. Add alias URLs
      $ mc config --alias "zek https://s3.amazonaws.com/"

`,
	}
	updateCmd = cli.Command{
		Name:        "update",
		Usage:       "Check for new software updates",
		Description: "",
		Action:      doUpdateCmd,
		Flags: []cli.Flag{
			cli.BoolFlag{
				Name:  "yes, y",
				Usage: "Download and update local binary",
			},
		},
	}
)

// Collection of mc commands currently supported are
//
// ----
// ls     List files and objects
// cp     Copy objects and files
// mb     Make a bucket
// access Set permissions [public, private, readonly] for buckets and folders.
// cat    Concantenate an object to standard output
// config Generate configuration "/home/harsha/.mc/config.json" file.
// update Check for new software updates
// ----
//
var commands = []cli.Command{
	lsCmd,
	cpCmd,
	mbCmd,
	accessCmd,
	catCmd,
	configCmd,
	updateCmd,
	// Add your new commands starting from here
}

// Collection of mc flags currently supported
//
// --theme       "minimal"Choose a console theme from this list [*minimal*, nocolor, white]
// --debug       Enable HTTP tracing
// --quiet, -q   Supress chatty console output
// --retry "5"   Number of retry count
// --version, -v print the version
//
var (
	flags = []cli.Flag{
		cli.StringFlag{
			Name:  "theme",
			Value: console.GetDefaultThemeName(),
			Usage: fmt.Sprintf("Choose a console theme from this list [%s]", func() string {
				keys := []string{}
				for _, themeName := range console.GetThemeNames() {
					if console.GetThemeName() == themeName {
						themeName = "*" + themeName + "*"
					}
					keys = append(keys, themeName)
				}
				return strings.Join(keys, ", ")
			}()),
		},
		cli.BoolFlag{
			Name:  "debug",
			Usage: "Enable HTTP tracing",
		},
		cli.BoolFlag{
			Name:  "quiet, q",
			Usage: "Supress chatty console output",
		},
		cli.IntFlag{
			Name:  "retry",
			Usage: "Number of retry count",
			Value: 5,
		},
		// Add your new flags starting here
	}
)
<|endoftext|>


<commit_before>package main

import (
	gocontext "context"
	"encoding/json"
	"io/ioutil"
	"path/filepath"
	"runtime"

	"github.com/Sirupsen/logrus"
	"github.com/crosbymichael/console"
	"github.com/docker/containerd/api/services/execution"
	"github.com/docker/containerd/api/types/mount"
	protobuf "github.com/gogo/protobuf/types"
	"github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
)

var rwm = "rwm"

const rootfsPath = "rootfs"

func spec(id string, args []string, tty bool) *specs.Spec {
	return &specs.Spec{
		Version: specs.Version,
		Platform: specs.Platform{
			OS:   runtime.GOOS,
			Arch: runtime.GOARCH,
		},
		Root: specs.Root{
			Path:     rootfsPath,
			Readonly: true,
		},
		Process: specs.Process{
			Args: args,
			Env: []string{
				"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
			},
			Terminal:        tty,
			Cwd:             "/",
			NoNewPrivileges: true,
		},
		Mounts: []specs.Mount{
			{
				Destination: "/proc",
				Type:        "proc",
				Source:      "proc",
			},
			{
				Destination: "/dev",
				Type:        "tmpfs",
				Source:      "tmpfs",
				Options:     []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
			},
			{
				Destination: "/dev/pts",
				Type:        "devpts",
				Source:      "devpts",
				Options:     []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
			},
			{
				Destination: "/dev/shm",
				Type:        "tmpfs",
				Source:      "shm",
				Options:     []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
			},
			{
				Destination: "/dev/mqueue",
				Type:        "mqueue",
				Source:      "mqueue",
				Options:     []string{"nosuid", "noexec", "nodev"},
			},
			{
				Destination: "/sys",
				Type:        "sysfs",
				Source:      "sysfs",
				Options:     []string{"nosuid", "noexec", "nodev"},
			},
			{
				Destination: "/run",
				Type:        "tmpfs",
				Source:      "tmpfs",
				Options:     []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
			},
			{
				Destination: "/etc/resolv.conf",
				Type:        "bind",
				Source:      "/etc/resolv.conf",
				Options:     []string{"rbind", "ro"},
			},
			{
				Destination: "/etc/hosts",
				Type:        "bind",
				Source:      "/etc/hosts",
				Options:     []string{"rbind", "ro"},
			},
			{
				Destination: "/etc/localtime",
				Type:        "bind",
				Source:      "/etc/localtime",
				Options:     []string{"rbind", "ro"},
			},
		},
		Hostname: id,
		Linux: &specs.Linux{
			Resources: &specs.LinuxResources{
				Devices: []specs.LinuxDeviceCgroup{
					{
						Allow:  false,
						Access: &rwm,
					},
				},
			},
			Namespaces: []specs.LinuxNamespace{
				{
					Type: "pid",
				},
				{
					Type: "ipc",
				},
				{
					Type: "uts",
				},
				{
					Type: "mount",
				},
				{
					Type: "network",
				},
			},
		},
	}
}

func customSpec(configPath string) (*specs.Spec, error) {
	b, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, err
	}
	var s specs.Spec
	if err := json.Unmarshal(b, &s); err != nil {
		return nil, err
	}
	if s.Root.Path != rootfsPath {
		logrus.Warnf("ignoring Root.Path %q, setting %q forcibly", s.Root.Path, rootfsPath)
		s.Root.Path = rootfsPath
	}
	return &s, nil
}

var runCommand = cli.Command{
	Name:  "run",
	Usage: "run a container",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "id",
			Usage: "id of the container",
		},
		cli.BoolFlag{
			Name:  "tty,t",
			Usage: "allocate a TTY for the container",
		},
		cli.StringFlag{
			Name:  "rootfs,r",
			Usage: "path to the container's root filesystem",
		},
		cli.StringFlag{
			Name:  "runtime-config",
			Usage: "custom runtime config (config.json)",
		},
		cli.StringFlag{
			Name:  "runtime",
			Usage: "runtime name (linux, windows, vmware-linux)",
			Value: "linux",
		},
	},
	Action: func(context *cli.Context) error {
		id := context.String("id")
		if id == "" {
			return errors.New("container id must be provided")
		}

		containers, err := getExecutionService(context)
		if err != nil {
			return err
		}
		tmpDir, err := getTempDir(id)
		if err != nil {
			return err
		}
		events, err := containers.Events(gocontext.Background(), &execution.EventsRequest{})
		if err != nil {
			return err
		}
		// for ctr right now just do a bind mount
		rootfs := []*mount.Mount{
			{
				Type:   "bind",
				Source: context.String("rootfs"),
				Options: []string{
					"rw",
					"rbind",
				},
			},
		}

		var s *specs.Spec
		if config := context.String("runtime-config"); config == "" {
			s = spec(id, []string(context.Args()), context.Bool("tty"))
		} else {
			s, err = customSpec(config)
			if err != nil {
				return err
			}
		}
		data, err := json.Marshal(s)
		if err != nil {
			return err
		}
		create := &execution.CreateRequest{
			ID: id,
			Spec: &protobuf.Any{
				TypeUrl: specs.Version,
				Value:   data,
			},
			Rootfs:   rootfs,
			Runtime:  context.String("runtime"),
			Terminal: context.Bool("tty"),
			Stdin:    filepath.Join(tmpDir, "stdin"),
			Stdout:   filepath.Join(tmpDir, "stdout"),
			Stderr:   filepath.Join(tmpDir, "stderr"),
		}
		if create.Terminal {
			con := console.Current()
			defer con.Reset()
			if err := con.SetRaw(); err != nil {
				return err
			}
		}
		fwg, err := prepareStdio(create.Stdin, create.Stdout, create.Stderr, create.Terminal)
		if err != nil {
			return err
		}
		// Ensure we read all io
		defer fwg.Wait()

		response, err := containers.Create(gocontext.Background(), create)
		if err != nil {
			return err
		}
		if _, err := containers.Start(gocontext.Background(), &execution.StartRequest{
			ID: response.ID,
		}); err != nil {
			return err
		}
		status, err := waitContainer(events, response)
		if err != nil {
			return err
		}
		if _, err := containers.Delete(gocontext.Background(), &execution.DeleteRequest{
			ID: response.ID,
		}); err != nil {
			return err
		}
		if status != 0 {
			return cli.NewExitError("", int(status))
		}
		return nil
	},
}
<commit_msg>Do not read all IO if container is not started.<commit_after>package main

import (
	gocontext "context"
	"encoding/json"
	"io/ioutil"
	"path/filepath"
	"runtime"

	"github.com/Sirupsen/logrus"
	"github.com/crosbymichael/console"
	"github.com/docker/containerd/api/services/execution"
	"github.com/docker/containerd/api/types/mount"
	protobuf "github.com/gogo/protobuf/types"
	"github.com/opencontainers/runtime-spec/specs-go"
	"github.com/pkg/errors"
	"github.com/urfave/cli"
)

var rwm = "rwm"

const rootfsPath = "rootfs"

func spec(id string, args []string, tty bool) *specs.Spec {
	return &specs.Spec{
		Version: specs.Version,
		Platform: specs.Platform{
			OS:   runtime.GOOS,
			Arch: runtime.GOARCH,
		},
		Root: specs.Root{
			Path:     rootfsPath,
			Readonly: true,
		},
		Process: specs.Process{
			Args: args,
			Env: []string{
				"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
			},
			Terminal:        tty,
			Cwd:             "/",
			NoNewPrivileges: true,
		},
		Mounts: []specs.Mount{
			{
				Destination: "/proc",
				Type:        "proc",
				Source:      "proc",
			},
			{
				Destination: "/dev",
				Type:        "tmpfs",
				Source:      "tmpfs",
				Options:     []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
			},
			{
				Destination: "/dev/pts",
				Type:        "devpts",
				Source:      "devpts",
				Options:     []string{"nosuid", "noexec", "newinstance", "ptmxmode=0666", "mode=0620", "gid=5"},
			},
			{
				Destination: "/dev/shm",
				Type:        "tmpfs",
				Source:      "shm",
				Options:     []string{"nosuid", "noexec", "nodev", "mode=1777", "size=65536k"},
			},
			{
				Destination: "/dev/mqueue",
				Type:        "mqueue",
				Source:      "mqueue",
				Options:     []string{"nosuid", "noexec", "nodev"},
			},
			{
				Destination: "/sys",
				Type:        "sysfs",
				Source:      "sysfs",
				Options:     []string{"nosuid", "noexec", "nodev"},
			},
			{
				Destination: "/run",
				Type:        "tmpfs",
				Source:      "tmpfs",
				Options:     []string{"nosuid", "strictatime", "mode=755", "size=65536k"},
			},
			{
				Destination: "/etc/resolv.conf",
				Type:        "bind",
				Source:      "/etc/resolv.conf",
				Options:     []string{"rbind", "ro"},
			},
			{
				Destination: "/etc/hosts",
				Type:        "bind",
				Source:      "/etc/hosts",
				Options:     []string{"rbind", "ro"},
			},
			{
				Destination: "/etc/localtime",
				Type:        "bind",
				Source:      "/etc/localtime",
				Options:     []string{"rbind", "ro"},
			},
		},
		Hostname: id,
		Linux: &specs.Linux{
			Resources: &specs.LinuxResources{
				Devices: []specs.LinuxDeviceCgroup{
					{
						Allow:  false,
						Access: &rwm,
					},
				},
			},
			Namespaces: []specs.LinuxNamespace{
				{
					Type: "pid",
				},
				{
					Type: "ipc",
				},
				{
					Type: "uts",
				},
				{
					Type: "mount",
				},
				{
					Type: "network",
				},
			},
		},
	}
}

func customSpec(configPath string) (*specs.Spec, error) {
	b, err := ioutil.ReadFile(configPath)
	if err != nil {
		return nil, err
	}
	var s specs.Spec
	if err := json.Unmarshal(b, &s); err != nil {
		return nil, err
	}
	if s.Root.Path != rootfsPath {
		logrus.Warnf("ignoring Root.Path %q, setting %q forcibly", s.Root.Path, rootfsPath)
		s.Root.Path = rootfsPath
	}
	return &s, nil
}

var runCommand = cli.Command{
	Name:  "run",
	Usage: "run a container",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "id",
			Usage: "id of the container",
		},
		cli.BoolFlag{
			Name:  "tty,t",
			Usage: "allocate a TTY for the container",
		},
		cli.StringFlag{
			Name:  "rootfs,r",
			Usage: "path to the container's root filesystem",
		},
		cli.StringFlag{
			Name:  "runtime-config",
			Usage: "custom runtime config (config.json)",
		},
		cli.StringFlag{
			Name:  "runtime",
			Usage: "runtime name (linux, windows, vmware-linux)",
			Value: "linux",
		},
	},
	Action: func(context *cli.Context) error {
		id := context.String("id")
		if id == "" {
			return errors.New("container id must be provided")
		}

		containers, err := getExecutionService(context)
		if err != nil {
			return err
		}
		tmpDir, err := getTempDir(id)
		if err != nil {
			return err
		}
		events, err := containers.Events(gocontext.Background(), &execution.EventsRequest{})
		if err != nil {
			return err
		}
		// for ctr right now just do a bind mount
		rootfs := []*mount.Mount{
			{
				Type:   "bind",
				Source: context.String("rootfs"),
				Options: []string{
					"rw",
					"rbind",
				},
			},
		}

		var s *specs.Spec
		if config := context.String("runtime-config"); config == "" {
			s = spec(id, []string(context.Args()), context.Bool("tty"))
		} else {
			s, err = customSpec(config)
			if err != nil {
				return err
			}
		}
		data, err := json.Marshal(s)
		if err != nil {
			return err
		}
		create := &execution.CreateRequest{
			ID: id,
			Spec: &protobuf.Any{
				TypeUrl: specs.Version,
				Value:   data,
			},
			Rootfs:   rootfs,
			Runtime:  context.String("runtime"),
			Terminal: context.Bool("tty"),
			Stdin:    filepath.Join(tmpDir, "stdin"),
			Stdout:   filepath.Join(tmpDir, "stdout"),
			Stderr:   filepath.Join(tmpDir, "stderr"),
		}
		if create.Terminal {
			con := console.Current()
			defer con.Reset()
			if err := con.SetRaw(); err != nil {
				return err
			}
		}
		fwg, err := prepareStdio(create.Stdin, create.Stdout, create.Stderr, create.Terminal)
		if err != nil {
			return err
		}

		response, err := containers.Create(gocontext.Background(), create)
		if err != nil {
			return err
		}
		if _, err := containers.Start(gocontext.Background(), &execution.StartRequest{
			ID: response.ID,
		}); err != nil {
			return err
		}

		// Ensure we read all io only if container started successfully.
		defer fwg.Wait()

		status, err := waitContainer(events, response)
		if err != nil {
			return err
		}
		if _, err := containers.Delete(gocontext.Background(), &execution.DeleteRequest{
			ID: response.ID,
		}); err != nil {
			return err
		}
		if status != 0 {
			return cli.NewExitError("", int(status))
		}
		return nil
	},
}
<|endoftext|>