<commit_before>package utils

import (
	"bytes"
	"fmt"
	"io"
	"strings"
	"unicode"

	"github.com/graphql-go/graphql/language/ast"
	"github.com/graphql-go/graphql/language/kinds"
)

// Line represents a line of text in context of a byte array
type Line struct {
	Start  int
	End    int
	Source []byte
}

// Text returns the contents of the line as a string
func (line Line) Text() string {
	return string(line.Source[line.Start:line.End])
}

// IsStartOfLine returns true if index is inside the white space between
// the beginning of the line and any non white space character
func (line Line) IsStartOfLine(index int) bool {
	pos := line.Start
	if index < line.Start || index > line.End {
		return false
	}
	for _, c := range line.Text() {
		if unicode.IsSpace(rune(c)) == true {
			pos++
			continue
		}

		if index <= pos {
			return true
		}

		break
	}
	return false
}

// GetLine takes a byte array and a start position
// returns a line
func GetLine(src []byte, index int) (Line, error) {
	start, end := index, index+1
	l := len(src)

	if index > l {
		return Line{}, fmt.Errorf("index out of range")
	}

	// Find the beginning of the line
	for start > 0 && src[start-1] != '\n' {
		start--
	}

	// Find the end of the line
	for end < l && src[end-1] != '\n' {
		end++
	}

	return Line{
		Source: src,
		Start:  start,
		End:    end,
	}, nil
}

// GetCommentBlock takes a byte array and a start position
// and may return a comment block if it finds one
func GetCommentBlock(src []byte, index int) (block []string) {

	pos := index
	linegap := false

	for {
		line, err := GetLine(src, pos)
		if err != nil {
			break
		}

		if line.IsStartOfLine(index) == false && linegap == false {
			return
		}

		text := strings.TrimSpace(line.Text())

		if strings.HasPrefix(text, "#") && linegap == true {
			comment := strings.TrimSpace(strings.TrimLeft(text, "#"))

			// Prepend new data to the comment block
			block = append([]string{comment}, block...)
			linegap = false
		}

		if linegap == true {
			break
		}

		pos = line.Start - 1
		linegap = true

		if pos <= 0 {
			break
		}
	}

	return block
}

// ParseType Convert a schema type to a ast.Type
func ParseType(t string, loc *ast.Location) ast.Type {

	if strings.HasSuffix(t, "!") {
		return &ast.NonNull{
			Kind: kinds.NonNull,
			Loc:  loc,
		}
	}

	if strings.HasPrefix(t, "[") && strings.HasSuffix(t, "]") {
		return &ast.List{
			Kind: kinds.List,
			Loc:  loc,
		}
	}

	return &ast.Named{
		Kind: kinds.Named,
		Loc:  loc,
	}
}

// LineCounter counts lines in a string/byte buffer
func LineCounter(r io.Reader) (int, error) {
	buf := make([]byte, 32*1024)
	count := 0
	lineSep := []byte{'\n'}

	for {
		c, err := r.Read(buf)
		count += bytes.Count(buf[:c], lineSep)

		switch {
		case err == io.EOF:
			return count, nil

		case err != nil:
			return count, err
		}
	}
}
<commit_msg>Add simple Lifo stack<commit_after>package utils

import (
	"bytes"
	"fmt"
	"io"
	"strings"
	"unicode"

	"github.com/graphql-go/graphql/language/ast"
	"github.com/graphql-go/graphql/language/kinds"
)

// Line represents a line of text in context of a byte array
type Line struct {
	Start  int
	End    int
	Source []byte
}

// Text returns the contents of the line as a string
func (line Line) Text() string {
	return string(line.Source[line.Start:line.End])
}

// IsStartOfLine returns true if index is inside the white space between
// the beginning of the line and any non white space character
func (line Line) IsStartOfLine(index int) bool {
	pos := line.Start
	if index < line.Start || index > line.End {
		return false
	}
	for _, c := range line.Text() {
		if unicode.IsSpace(rune(c)) == true {
			pos++
			continue
		}

		if index <= pos {
			return true
		}

		break
	}
	return false
}

// GetLine takes a byte array and a start position
// returns a line
func GetLine(src []byte, index int) (Line, error) {
	start, end := index, index+1
	l := len(src)

	if index > l {
		return Line{}, fmt.Errorf("index out of range")
	}

	// Find the beginning of the line
	for start > 0 && src[start-1] != '\n' {
		start--
	}

	// Find the end of the line
	for end < l && src[end-1] != '\n' {
		end++
	}

	return Line{
		Source: src,
		Start:  start,
		End:    end,
	}, nil
}

// GetCommentBlock takes a byte array and a start position
// and may return a comment block if it finds one
func GetCommentBlock(src []byte, index int) (block []string) {

	pos := index
	linegap := false

	for {
		line, err := GetLine(src, pos)
		if err != nil {
			break
		}

		if line.IsStartOfLine(index) == false && linegap == false {
			return
		}

		text := strings.TrimSpace(line.Text())

		if strings.HasPrefix(text, "#") && linegap == true {
			comment := strings.TrimSpace(strings.TrimLeft(text, "#"))

			// Prepend new data to the comment block
			block = append([]string{comment}, block...)
			linegap = false
		}

		if linegap == true {
			break
		}

		pos = line.Start - 1
		linegap = true

		if pos <= 0 {
			break
		}
	}

	return block
}

// ParseType Convert a schema type to a ast.Type
func ParseType(t string, loc *ast.Location) ast.Type {

	if strings.HasSuffix(t, "!") {
		return &ast.NonNull{
			Kind: kinds.NonNull,
			Loc:  loc,
		}
	}

	if strings.HasPrefix(t, "[") && strings.HasSuffix(t, "]") {
		return &ast.List{
			Kind: kinds.List,
			Loc:  loc,
		}
	}

	return &ast.Named{
		Kind: kinds.Named,
		Loc:  loc,
	}
}

// LineCounter counts lines in a string/byte buffer
func LineCounter(r io.Reader) (int, error) {
	buf := make([]byte, 32*1024)
	count := 0
	lineSep := []byte{'\n'}

	for {
		c, err := r.Read(buf)
		count += bytes.Count(buf[:c], lineSep)

		switch {
		case err == io.EOF:
			return count, nil

		case err != nil:
			return count, err
		}
	}
}

// Lifo stack
type Lifo struct {
	Stack []interface{}
}

// Push a new element on the top of the stack
func (lifo *Lifo) Push(elem interface{}) {
	prepend := []interface{}{
		elem,
	}
	lifo.Stack = append(prepend, lifo.Stack...)
}

// Pop an element from the top of the stack
func (lifo *Lifo) Pop() interface{} {
	elem := lifo.Stack[0]
	lifo.Stack = lifo.Stack[1:]

	return elem
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"sync"
	"time"

	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/client/cache"
	"k8s.io/kubernetes/pkg/client/restclient"
	client "k8s.io/kubernetes/pkg/client/unversioned"
	controllerframework "k8s.io/kubernetes/pkg/controller/framework"
	"k8s.io/kubernetes/pkg/labels"
	"k8s.io/kubernetes/pkg/runtime"
	"k8s.io/kubernetes/pkg/watch"
)

const (
	scaleNS = "scale-ns"
)

type rcJob struct {
	kubeClient *client.Client
}

func ExitError(msg string, args ...interface{}) {
	fmt.Println("exiting with error:")
	fmt.Printf(msg+"\n", args)
	os.Exit(1)
}

func main() {
	var apisrvAddr string
	var rcNum int
	var podNum int
	var chaosTestOnly bool
	flag.StringVar(&apisrvAddr, "addr", "localhost:8080", "APIServer addr")
	flag.IntVar(&rcNum, "rc", 1000, "number of RC")
	flag.IntVar(&podNum, "pod", 100, "number of pods per RC")
	flag.BoolVar(&chaosTestOnly, "chaos", false, "running chaos testing only")
	flag.Parse()

	c, err := createClient(apisrvAddr)
	if err != nil {
		ExitError("createClient failed: %v", err)
	}

	fmt.Printf("Creating %d rc, each of %d pods\n", rcNum, podNum)

	if !chaosTestOnly {
		createPods(c, rcNum, podNum)
	}

	// introduce chaos
	var wg sync.WaitGroup
	wg.Add(rcNum)
	for i := 0; i < rcNum; i++ {
		// TODO: clean up on failure
		go func(id int) {
			defer wg.Done()
			deletePodsRandomly(c, id, podNum)
			waitRCRecoverPods(c, id, podNum)
		}(i)
	}
	wg.Wait()
	fmt.Println("Success...")
}

func createPods(c *client.Client, rcNum, podNum int) {
	var wg sync.WaitGroup
	wg.Add(rcNum)
	for i := 0; i < rcNum; i++ {
		// TODO: clean up on failure
		go func(id int) {
			defer wg.Done()
			createRC(c, id, podNum)
			waitRCCreatePods(c, id, podNum)
		}(i)
	}
	wg.Wait()
}

func createRC(c *client.Client, id, podNum int) {
	rc := &api.ReplicationController{
		ObjectMeta: api.ObjectMeta{
			Name: makeRCName(id),
		},
		Spec: api.ReplicationControllerSpec{
			Replicas: int32(podNum),
			Selector: makeLabel(id),
			Template: &api.PodTemplateSpec{
				ObjectMeta: api.ObjectMeta{
					Labels: makeLabel(id),
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name:  "none",
							Image: "none",
						},
					},
				},
			},
		},
	}
	if _, err := c.ReplicationControllers(scaleNS).Create(rc); err != nil {
		ExitError("create RC failed: %v", err)
	}
	fmt.Printf("created rc: %s\n", makeRCName(id))
}

func waitRCCreatePods(c *client.Client, id, podNum int) {
	informer := createPodInformer(c, labels.SelectorFromSet(labels.Set(makeLabel(id))))

	doneCh := make(chan struct{})
	total := 0
	informer.AddEventHandler(controllerframework.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			total += 1
			if total == podNum {
				close(doneCh)
			}
		},
	})

	stopCh := make(chan struct{})
	defer close(stopCh)
	go informer.Run(stopCh)

	store := informer.GetStore()
	start := time.Now()
	for {
		select {
		case <-doneCh:
			if len(store.List()) != podNum {
				panic("unexpected")
			}
			fmt.Printf("rc (%s) created %d pods\n", makeRCName(id), podNum)
			return
		case <-time.After(1 * time.Minute):
			fmt.Printf("%v passed, rc (%s) has created %d pods\n", time.Since(start), makeRCName(id), len(store.List()))
		}
	}
}

func deletePodsRandomly(c *client.Client, id, podNum int) {
	podList, err := c.Pods(scaleNS).List(api.ListOptions{
		LabelSelector: labels.SelectorFromSet(labels.Set(makeLabel(id))),
	})
	if err != nil {
		ExitError("list pods failed: %v", err)
	}
	for i, pod := range podList.Items {
		if i%2 != 0 {
			continue
		}
		if err := c.Pods(scaleNS).Delete(pod.Name, api.NewDeleteOptions(0)); err != nil {
			ExitError("delete pod (%s) failed: %v", pod.Name, err)
		}
		fmt.Printf("rc (%s) deleted pod %s\n", makeRCName(id), pod.Name)
	}
}

func waitRCRecoverPods(c *client.Client, id, podNum int) {
	informer := createPodInformer(c, labels.SelectorFromSet(labels.Set(makeLabel(id))))
	store := informer.GetStore()

	doneCh := make(chan bool, 1)
	informer.AddEventHandler(controllerframework.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			if len(store.List()) == podNum {
				select {
				case doneCh <- true:
				default:
				}
			}
		},
	})
	stopCh := make(chan struct{})
	defer close(stopCh)
	go informer.Run(stopCh)

	if len(store.List()) == podNum {
		fmt.Printf("rc (%s) recovered to %d pods\n", makeRCName(id), podNum)
		return
	}
	start := time.Now()
	for {
		select {
		case <-doneCh:
			fmt.Printf("rc (%s) recovered to %d pods\n", makeRCName(id), podNum)
			return
		case <-time.After(1 * time.Minute):
			fmt.Printf("%v passed, rc (%s) has created %d pods\n", time.Since(start), makeRCName(id), len(store.List()))
		}
	}

}

func createPodInformer(c *client.Client, labelSelector labels.Selector) controllerframework.SharedInformer {
	informer := controllerframework.NewSharedInformer(
		&cache.ListWatch{
			ListFunc: func(options api.ListOptions) (runtime.Object, error) {
				options.LabelSelector = labelSelector
				return c.Pods(scaleNS).List(options)
			},
			WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
				options.LabelSelector = labelSelector
				return c.Pods(scaleNS).Watch(options)
			},
		},
		&api.Pod{},
		0,
	)
	return informer
}

func createClient(addr string) (*client.Client, error) {
	cfg := &restclient.Config{
		Host:  fmt.Sprintf("http://%s", addr),
		QPS:   1000,
		Burst: 1000,
	}
	c, err := client.New(cfg)
	if err != nil {
		return nil, err
	}
	return c, nil
}

func makeRCName(id int) string {
	return fmt.Sprintf("scale-rc-%d", id)
}

func makeLabel(id int) map[string]string {
	return map[string]string{"name": fmt.Sprintf("scale-label-%d", id)}
}
<commit_msg>rewrite watch in etcd client<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"runtime/debug"
	"strings"
	"sync"
	"time"

	"github.com/coreos/etcd/clientv3"
	"golang.org/x/net/context"

	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/client/cache"
	"k8s.io/kubernetes/pkg/client/restclient"
	client "k8s.io/kubernetes/pkg/client/unversioned"
	controllerframework "k8s.io/kubernetes/pkg/controller/framework"
	"k8s.io/kubernetes/pkg/labels"
	"k8s.io/kubernetes/pkg/runtime"
	"k8s.io/kubernetes/pkg/watch"
)

const (
	scaleNS = "scale-ns"
)

type rcJob struct {
	kubeClient *client.Client
}

func ExitError(msg string, args ...interface{}) {
	fmt.Println("exiting with error:")
	fmt.Printf(msg+"\n", args...)
	debug.PrintStack()
	os.Exit(1)
}

func main() {
	var apisrvAddr string
	var etcdAddr string
	var rcNum int
	var podNum int
	var chaosTestOnly bool
	flag.StringVar(&apisrvAddr, "addr", "localhost:8080", "APIServer addr")
	flag.StringVar(&etcdAddr, "etcdaddr", "http://localhost:2379", "etcd addrs, splitted by ','. kube client is flawed...")
	flag.IntVar(&rcNum, "rc", 1000, "number of RC")
	flag.IntVar(&podNum, "pod", 100, "number of pods per RC")
	flag.BoolVar(&chaosTestOnly, "chaos", false, "running chaos testing only")
	flag.Parse()

	c, err := createClient(apisrvAddr)
	if err != nil {
		ExitError("createClient failed: %v", err)
	}

	fmt.Printf("Creating %d rc, each of %d pods\n", rcNum, podNum)

	if !chaosTestOnly {
		createPods(c, rcNum, podNum)
	}

	endpoints := strings.Split(etcdAddr, ",")
	cfg := clientv3.Config{
		Endpoints: endpoints,
	}
	etcdClient, err := clientv3.New(cfg)
	if err != nil {
		ExitError("etcd client New (%s) failed: %v", endpoints, err)
	}

	// introduce chaos
	var wg sync.WaitGroup
	wg.Add(rcNum)
	for i := 0; i < rcNum; i++ {
		// TODO: clean up on failure
		go func(id int) {
			defer wg.Done()
			deletePodsRandomly(c, id, podNum)
			waitRCRecoverPods(context.TODO(), c, id, podNum, etcdClient)
		}(i)
	}
	wg.Wait()
	fmt.Println("Success...")
}

func createPods(c *client.Client, rcNum, podNum int) {
	var wg sync.WaitGroup
	wg.Add(rcNum)
	for i := 0; i < rcNum; i++ {
		// TODO: clean up on failure
		go func(id int) {
			defer wg.Done()
			createRC(c, id, podNum)
			waitRCCreatePods(c, id, podNum)
		}(i)
	}
	wg.Wait()
}

func createRC(c *client.Client, id, podNum int) {
	rc := &api.ReplicationController{
		ObjectMeta: api.ObjectMeta{
			Name: makeRCName(id),
		},
		Spec: api.ReplicationControllerSpec{
			Replicas: int32(podNum),
			Selector: makeLabel(id),
			Template: &api.PodTemplateSpec{
				ObjectMeta: api.ObjectMeta{
					Labels: makeLabel(id),
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name:  "none",
							Image: "none",
						},
					},
				},
			},
		},
	}
	if _, err := c.ReplicationControllers(scaleNS).Create(rc); err != nil {
		ExitError("create RC failed: %v", err)
	}
	fmt.Printf("created rc: %s\n", makeRCName(id))
}

func waitRCCreatePods(c *client.Client, id, podNum int) {
	informer := createPodInformer(c, labels.SelectorFromSet(labels.Set(makeLabel(id))))

	doneCh := make(chan struct{})
	total := 0
	informer.AddEventHandler(controllerframework.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
			total += 1
			if total == podNum {
				close(doneCh)
			}
		},
	})

	stopCh := make(chan struct{})
	defer close(stopCh)
	go informer.Run(stopCh)

	store := informer.GetStore()
	start := time.Now()
	for {
		select {
		case <-doneCh:
			if len(store.List()) != podNum {
				panic("unexpected")
			}
			fmt.Printf("rc (%s) created %d pods\n", makeRCName(id), podNum)
			return
		case <-time.After(1 * time.Minute):
			fmt.Printf("%v passed, rc (%s) has created %d pods\n", time.Since(start), makeRCName(id), len(store.List()))
		}
	}
}

func deletePodsRandomly(c *client.Client, id, podNum int) {
	podList := listPods(c, id)
	for i, pod := range podList.Items {
		if i%2 != 0 {
			continue
		}
		if err := c.Pods(scaleNS).Delete(pod.Name, api.NewDeleteOptions(0)); err != nil {
			ExitError("delete pod (%s) failed: %v", pod.Name, err)
		}
		fmt.Printf("rc (%s) deleted pod %s\n", makeRCName(id), pod.Name)
	}
}

func waitRCRecoverPods(ctx context.Context, c *client.Client, id, podNum int, etcdClient *clientv3.Client) {
	key := fmt.Sprintf("/registry/pods/%s/%s-", scaleNS, makeRCName(id))
	getResp, err := etcdClient.Get(ctx, key, clientv3.WithPrefix())
	if err != nil {
		ExitError("etcdclient range prefix (%s) failed: %v", key, err)
	}
	total := len(getResp.Kvs)
	w := etcdClient.Watch(ctx, key, clientv3.WithPrefix(), clientv3.WithRev(getResp.Header.Revision+1))

	start := time.Now()
	for {
		if total == podNum {
			break
		}
		select {
		case wr, ok := <-w:
			if !ok {
				panic("unexpected")
			}
			for _, ev := range wr.Events {
				if !ev.IsCreate() {
					ExitError("not create event: key (%s)", ev.Kv.Key)
				}
				fmt.Printf("Recreated pod: key (%s)\n", ev.Kv.Key)
				total++
			}
		case <-time.After(1 * time.Minute):
			fmt.Printf("%v passed, rc (%s) has recovered to %d pods\n", time.Since(start), makeRCName(id), total)
		}
	}
	fmt.Printf("rc (%s) created %d pods\n", makeRCName(id), podNum)
	podList := listPods(c, id)
	if len(podList.Items) != podNum {
		panic("unexpected")
	}
}

func listPods(c *client.Client, id int) *api.PodList {
	podList, err := c.Pods(scaleNS).List(api.ListOptions{
		LabelSelector: labels.SelectorFromSet(labels.Set(makeLabel(id))),
	})
	if err != nil {
		ExitError("list pods failed: %v", err)
	}
	return podList
}

func createPodInformer(c *client.Client, labelSelector labels.Selector) controllerframework.SharedInformer {
	informer := controllerframework.NewSharedInformer(
		&cache.ListWatch{
			ListFunc: func(options api.ListOptions) (runtime.Object, error) {
				options.LabelSelector = labelSelector
				return c.Pods(scaleNS).List(options)
			},
			WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
				options.LabelSelector = labelSelector
				return c.Pods(scaleNS).Watch(options)
			},
		},
		&api.Pod{},
		0,
	)
	return informer
}

func createClient(addr string) (*client.Client, error) {
	cfg := &restclient.Config{
		Host:  fmt.Sprintf("http://%s", addr),
		QPS:   1000,
		Burst: 1000,
	}
	c, err := client.New(cfg)
	if err != nil {
		return nil, err
	}
	return c, nil
}

func makeRCName(id int) string {
	return fmt.Sprintf("scale-rc-%d", id)
}

func makeLabel(id int) map[string]string {
	return map[string]string{"name": fmt.Sprintf("scale-label-%d", id)}
}
<|endoftext|>


<commit_before>// Copyright 2013 Miek Gieben. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pkcs11

//VENDOR SPECIFIC RANGE FOR nCIPHER NETWORK HSMs
const (
        NFCK_VENDOR_NCIPHER     = 0xde436972
        CKA_NCIPHER     = NFCK_VENDOR_NCIPHER
        CKM_NCIPHER     = NFCK_VENDOR_NCIPHER
        CKK_NCIPHER     = NFCK_VENDOR_NCIPHER
)

//VENDOR SPECIFIC MECHANISMS FOR HMAC ON NCIPHER HSMS WHERE NCIPHER DOES NOT ALLOW USE OF GENERIC_SECRET KEYS
const (
	CKM_NC_SHA_1_HMAC_KEY_GEN       = (CKM_NCIPHER + 0x3)  /* no params */
	CKM_NC_MD5_HMAC_KEY_GEN         = (CKM_NCIPHER + 0x6)  /* no params */
	CKM_NC_SHA224_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x24) /* no params */
	CKM_NC_SHA256_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x25) /* no params */
	CKM_NC_SHA384_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x26) /* no params */
	CKM_NC_SHA512_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x27) /* no params */

)

<commit_msg>Add vendor-specific constants for NSS (#92)<commit_after>// Copyright 2013 Miek Gieben. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pkcs11

//VENDOR SPECIFIC RANGE FOR nCIPHER NETWORK HSMs
const (
        NFCK_VENDOR_NCIPHER     = 0xde436972
        CKA_NCIPHER     = NFCK_VENDOR_NCIPHER
        CKM_NCIPHER     = NFCK_VENDOR_NCIPHER
        CKK_NCIPHER     = NFCK_VENDOR_NCIPHER
)

//VENDOR SPECIFIC MECHANISMS FOR HMAC ON NCIPHER HSMS WHERE NCIPHER DOES NOT ALLOW USE OF GENERIC_SECRET KEYS
const (
	CKM_NC_SHA_1_HMAC_KEY_GEN       = (CKM_NCIPHER + 0x3)  /* no params */
	CKM_NC_MD5_HMAC_KEY_GEN         = (CKM_NCIPHER + 0x6)  /* no params */
	CKM_NC_SHA224_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x24) /* no params */
	CKM_NC_SHA256_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x25) /* no params */
	CKM_NC_SHA384_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x26) /* no params */
	CKM_NC_SHA512_HMAC_KEY_GEN      = (CKM_NCIPHER + 0x27) /* no params */

)

//VENDOR SPECIFIC RANGE FOR MOZILLA NSS
const (
	NSSCK_VENDOR_NSS = 0x4E534350
	CKO_NSS = (CKO_VENDOR_DEFINED | NSSCK_VENDOR_NSS)
	CKK_NSS = (CKK_VENDOR_DEFINED | NSSCK_VENDOR_NSS)
	CKC_NSS = (CKC_VENDOR_DEFINED | NSSCK_VENDOR_NSS)
	CKA_NSS = (CKA_VENDOR_DEFINED | NSSCK_VENDOR_NSS)
	CKA_TRUST = (CKA_NSS + 0x2000)
	CKM_NSS = (CKM_VENDOR_DEFINED | NSSCK_VENDOR_NSS)
	CKR_NSS = (CKM_VENDOR_DEFINED | NSSCK_VENDOR_NSS)
	CKT_VENDOR_DEFINED = 0x80000000
	CKT_NSS = (CKT_VENDOR_DEFINED | NSSCK_VENDOR_NSS)
)

//VENDOR SPECIFIC VALUES FOR MOZILLA NSS
const (
	CKO_NSS_CRL                               = (CKO_NSS + 1)
	CKO_NSS_SMIME                             = (CKO_NSS + 2)
	CKO_NSS_TRUST                             = (CKO_NSS + 3)
	CKO_NSS_BUILTIN_ROOT_LIST                 = (CKO_NSS + 4)
	CKO_NSS_NEWSLOT                           = (CKO_NSS + 5)
	CKO_NSS_DELSLOT                           = (CKO_NSS + 6)
	CKK_NSS_PKCS8                             = (CKK_NSS + 1)
	CKK_NSS_JPAKE_ROUND1                      = (CKK_NSS + 2)
	CKK_NSS_JPAKE_ROUND2                      = (CKK_NSS + 3)
	CKK_NSS_CHACHA20                          = (CKK_NSS + 4)
	CKA_NSS_URL                               = (CKA_NSS + 1)
	CKA_NSS_EMAIL                             = (CKA_NSS + 2)
	CKA_NSS_SMIME_INFO                        = (CKA_NSS + 3)
	CKA_NSS_SMIME_TIMESTAMP                   = (CKA_NSS + 4)
	CKA_NSS_PKCS8_SALT                        = (CKA_NSS + 5)
	CKA_NSS_PASSWORD_CHECK                    = (CKA_NSS + 6)
	CKA_NSS_EXPIRES                           = (CKA_NSS + 7)
	CKA_NSS_KRL                               = (CKA_NSS + 8)
	CKA_NSS_PQG_COUNTER                       = (CKA_NSS + 20)
	CKA_NSS_PQG_SEED                          = (CKA_NSS + 21)
	CKA_NSS_PQG_H                             = (CKA_NSS + 22)
	CKA_NSS_PQG_SEED_BITS                     = (CKA_NSS + 23)
	CKA_NSS_MODULE_SPEC                       = (CKA_NSS + 24)
	CKA_NSS_OVERRIDE_EXTENSIONS               = (CKA_NSS + 25)
	CKA_NSS_JPAKE_SIGNERID                    = (CKA_NSS + 26)
	CKA_NSS_JPAKE_PEERID                      = (CKA_NSS + 27)
	CKA_NSS_JPAKE_GX1                         = (CKA_NSS + 28)
	CKA_NSS_JPAKE_GX2                         = (CKA_NSS + 29)
	CKA_NSS_JPAKE_GX3                         = (CKA_NSS + 30)
	CKA_NSS_JPAKE_GX4                         = (CKA_NSS + 31)
	CKA_NSS_JPAKE_X2                          = (CKA_NSS + 32)
	CKA_NSS_JPAKE_X2S                         = (CKA_NSS + 33)
	CKA_NSS_MOZILLA_CA_POLICY                 = (CKA_NSS + 34)
	CKA_TRUST_DIGITAL_SIGNATURE               = (CKA_TRUST + 1)
	CKA_TRUST_NON_REPUDIATION                 = (CKA_TRUST + 2)
	CKA_TRUST_KEY_ENCIPHERMENT                = (CKA_TRUST + 3)
	CKA_TRUST_DATA_ENCIPHERMENT               = (CKA_TRUST + 4)
	CKA_TRUST_KEY_AGREEMENT                   = (CKA_TRUST + 5)
	CKA_TRUST_KEY_CERT_SIGN                   = (CKA_TRUST + 6)
	CKA_TRUST_CRL_SIGN                        = (CKA_TRUST + 7)
	CKA_TRUST_SERVER_AUTH                     = (CKA_TRUST + 8)
	CKA_TRUST_CLIENT_AUTH                     = (CKA_TRUST + 9)
	CKA_TRUST_CODE_SIGNING                    = (CKA_TRUST + 10)
	CKA_TRUST_EMAIL_PROTECTION                = (CKA_TRUST + 11)
	CKA_TRUST_IPSEC_END_SYSTEM                = (CKA_TRUST + 12)
	CKA_TRUST_IPSEC_TUNNEL                    = (CKA_TRUST + 13)
	CKA_TRUST_IPSEC_USER                      = (CKA_TRUST + 14)
	CKA_TRUST_TIME_STAMPING                   = (CKA_TRUST + 15)
	CKA_TRUST_STEP_UP_APPROVED                = (CKA_TRUST + 16)
	CKA_CERT_SHA1_HASH                        = (CKA_TRUST + 100)
	CKA_CERT_MD5_HASH                         = (CKA_TRUST + 101)
	CKM_NSS_AES_KEY_WRAP                      = (CKM_NSS + 1)
	CKM_NSS_AES_KEY_WRAP_PAD                  = (CKM_NSS + 2)
	CKM_NSS_HKDF_SHA1                         = (CKM_NSS + 3)
	CKM_NSS_HKDF_SHA256                       = (CKM_NSS + 4)
	CKM_NSS_HKDF_SHA384                       = (CKM_NSS + 5)
	CKM_NSS_HKDF_SHA512                       = (CKM_NSS + 6)
	CKM_NSS_JPAKE_ROUND1_SHA1                 = (CKM_NSS + 7)
	CKM_NSS_JPAKE_ROUND1_SHA256               = (CKM_NSS + 8)
	CKM_NSS_JPAKE_ROUND1_SHA384               = (CKM_NSS + 9)
	CKM_NSS_JPAKE_ROUND1_SHA512               = (CKM_NSS + 10)
	CKM_NSS_JPAKE_ROUND2_SHA1                 = (CKM_NSS + 11)
	CKM_NSS_JPAKE_ROUND2_SHA256               = (CKM_NSS + 12)
	CKM_NSS_JPAKE_ROUND2_SHA384               = (CKM_NSS + 13)
	CKM_NSS_JPAKE_ROUND2_SHA512               = (CKM_NSS + 14)
	CKM_NSS_JPAKE_FINAL_SHA1                  = (CKM_NSS + 15)
	CKM_NSS_JPAKE_FINAL_SHA256                = (CKM_NSS + 16)
	CKM_NSS_JPAKE_FINAL_SHA384                = (CKM_NSS + 17)
	CKM_NSS_JPAKE_FINAL_SHA512                = (CKM_NSS + 18)
	CKM_NSS_HMAC_CONSTANT_TIME                = (CKM_NSS + 19)
	CKM_NSS_SSL3_MAC_CONSTANT_TIME            = (CKM_NSS + 20)
	CKM_NSS_TLS_PRF_GENERAL_SHA256            = (CKM_NSS + 21)
	CKM_NSS_TLS_MASTER_KEY_DERIVE_SHA256      = (CKM_NSS + 22)
	CKM_NSS_TLS_KEY_AND_MAC_DERIVE_SHA256     = (CKM_NSS + 23)
	CKM_NSS_TLS_MASTER_KEY_DERIVE_DH_SHA256   = (CKM_NSS + 24)
	CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE    = (CKM_NSS + 25)
	CKM_NSS_TLS_EXTENDED_MASTER_KEY_DERIVE_DH = (CKM_NSS + 26)
	CKM_NSS_CHACHA20_KEY_GEN                  = (CKM_NSS + 27)
	CKM_NSS_CHACHA20_POLY1305                 = (CKM_NSS + 28)
	CKM_NSS_PKCS12_PBE_SHA224_HMAC_KEY_GEN    = (CKM_NSS + 29)
	CKM_NSS_PKCS12_PBE_SHA256_HMAC_KEY_GEN    = (CKM_NSS + 30)
	CKM_NSS_PKCS12_PBE_SHA384_HMAC_KEY_GEN    = (CKM_NSS + 31)
	CKM_NSS_PKCS12_PBE_SHA512_HMAC_KEY_GEN    = (CKM_NSS + 32)
	CKR_NSS_CERTDB_FAILED                     = (CKR_NSS + 1)
	CKR_NSS_KEYDB_FAILED                      = (CKR_NSS + 2)
	CKT_NSS_TRUSTED                           = (CKT_NSS + 1)
	CKT_NSS_TRUSTED_DELEGATOR                 = (CKT_NSS + 2)
	CKT_NSS_MUST_VERIFY_TRUST                 = (CKT_NSS + 3)
	CKT_NSS_NOT_TRUSTED                       = (CKT_NSS + 10)
	CKT_NSS_TRUST_UNKNOWN                     = (CKT_NSS + 5)
)
<|endoftext|>


<commit_before>// this is just a helper package that i (cs) write for multiple ssh access to
// servers
package main

import (
	"flag"
	"fmt"
	"log"
	"os/exec"
	"strings"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/elb"
)

var (
	// ELB & EC2 -> AmazonEC2ReadOnlyAccess
	flagAWSSecret = flag.String("awsSecret", "", "aws secret key")
	flagAWSAccess = flag.String("awsAccess", "", "aws access key")

	flagHost       = flag.String("env", "prod", "env name")
	flagFiltered   = flag.Bool("filtered", false, "filter by ec2 tags")
	flagFolderPath = flag.String("flagFolderPath", "/Users/siesta/Documents/koding/credential", "credential repo folder path")
)

var ELBS = map[string]string{
	// app
	"prod":       "awseb-e-x-AWSEBLoa-2AG3XORA8JXC",
	"latest":     "awseb-e-3-AWSEBLoa-1S2VPBAQXDRW9",
	"sandbox":    "awseb-e-p-AWSEBLoa-1POHSLP6A7STY",
	"monitoring": "awseb-e-j-AWSEBLoa-AQBHUYZM5ZX6",

	// proxy
	"proxy-eu-west-1":      "awseb-e-s-AWSEBLoa-1LOTB5BKTJJBW",
	"proxy-us-east-1":      "awseb-e-a-AWSEBLoa-RTLJ62SKJY5G",
	"proxy-us-west-2":      "awseb-e-7-AWSEBLoa-1V808KG9PDQH5",
	"proxy-ap-southeast-1": "awseb-e-u-AWSEBLoa-15H1DQTBBUMG",
	"proxy-dev-us-e-1":     "awseb-e-e-AWSEBLoa-A6GVWAANHT2N",
}

var Tags = map[string]string{
	// app
	"prod":    "prod.koding.com",
	"latest":  "latest.koding.com",
	"sandbox": "sandbox.koding.com",
}

var ELB2Region = map[string]string{
	// proxies
	"awseb-e-s-AWSEBLoa-1LOTB5BKTJJBW": "eu-west-1",
	"awseb-e-a-AWSEBLoa-RTLJ62SKJY5G":  "us-east-1",
	"awseb-e-7-AWSEBLoa-1V808KG9PDQH5": "us-west-2",
	"awseb-e-u-AWSEBLoa-15H1DQTBBUMG":  "ap-southeast-1",

	// app ELBs
	"awseb-e-j-AWSEBLoa-AQBHUYZM5ZX6":  "us-east-1",
	"awseb-e-x-AWSEBLoa-2AG3XORA8JXC":  "us-east-1",
	"awseb-e-3-AWSEBLoa-1S2VPBAQXDRW9": "us-east-1",
	"awseb-e-p-AWSEBLoa-1POHSLP6A7STY": "us-east-1",
	"awseb-e-e-AWSEBLoa-A6GVWAANHT2N":  "us-east-1",
}

func getEC2(elbName string) *ec2.EC2 {
	reg, ok := ELB2Region[elbName]
	if !ok {
		panic(elbName)
	}

	ses := session.New(&aws.Config{
		Credentials: credentials.NewStaticCredentials(
			*flagAWSAccess,
			*flagAWSSecret,
			"",
		),
		Region:     aws.String(reg),
		MaxRetries: aws.Int(5),
	})

	return ec2.New(ses)
}

func getELB(elbName string) *elb.ELB {
	reg, ok := ELB2Region[elbName]
	if !ok {
		panic(elbName)
	}

	ses := session.New(&aws.Config{
		Credentials: credentials.NewStaticCredentials(
			*flagAWSAccess,
			*flagAWSSecret,
			"",
		),
		Region:     aws.String(reg),
		MaxRetries: aws.Int(5),
	})

	return elb.New(ses)
}

func main() {

	flag.Parse()

	currentELBInstances := make([]string, 0)

	elbNames, ok := ELBS[*flagHost]
	if !ok {
		log.Fatalf("%s not found", *flagHost)
	}

	if *flagFiltered {
		tagName := Tags[*flagHost]
		var err error
		currentELBInstances, err = GetchInstancesByTag(elbNames, tagName)
		if err != nil {
			log.Fatal(err.Error())
		}
	} else {
		for _, elbName := range strings.Split(elbNames, ",") {
			currentInstances, err := fetchProdELBAttachedInstances(elbName)
			if err != nil {
				log.Fatal(err.Error())
			}
			currentELBInstances = append(currentELBInstances, currentInstances...)
		}
	}

	f := createI2csshString(*flagHost, currentELBInstances)
	_, err := exec.Command("i2cssh", strings.Fields(f)...).Output()
	if err != nil {
		log.Fatal(err.Error())
	}
}

var paramToKey = map[string]string{
	"prod":       "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"latest":     "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"sandbox":    "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"monitoring": "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",

	"proxy-eu-west-1":      "/private_keys/koding-eb-deployment-eu-west-1-2015-06.pem",
	"proxy-us-east-1":      "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"proxy-us-west-2":      "/private_keys/koding-eb-deployment-us-west-2-2015-06.pem",
	"proxy-ap-southeast-1": "/private_keys/koding-eb-deployment-ap-southeast-1-2015-06.pem",
	"proxy-dev-us-e-1":     "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
}

func createI2csshString(param string, instances []string) string {
	return fmt.Sprintf(
		"--forward-agent --login ec2-user --rows 3 --broadcast -Xi=%s  --machines %s",
		*flagFolderPath+paramToKey[param],
		strings.Join(instances, ","),
	)
}

// fetch currently attached instances to the prod ELB
func fetchProdELBAttachedInstances(elbName string) ([]string, error) {
	params := &elb.DescribeLoadBalancersInput{
		LoadBalancerNames: []*string{
			aws.String(elbName),
		},
	}
	res, err := getELB(elbName).DescribeLoadBalancers(params)
	if err != nil {
		return nil, err
	}

	if len(res.LoadBalancerDescriptions) < 1 {
		return nil, fmt.Errorf("%s ELB not found!", elbName)
	}

	attachedInstances := make([]*string, 0)
	for _, inst := range res.LoadBalancerDescriptions[0].Instances {
		attachedInstances = append(attachedInstances, inst.InstanceId)
	}

	resp, err := getEC2(elbName).DescribeInstances(
		&ec2.DescribeInstancesInput{
			InstanceIds: attachedInstances,
		},
	)
	if err != nil {
		return nil, err
	}

	instanceIps := make([]string, 0)
	for _, reservations := range resp.Reservations {
		for _, instance := range reservations.Instances {
			if *instance.PublicIpAddress != "" {
				instanceIps = append(instanceIps, *instance.PublicIpAddress)
			}
		}
	}

	return instanceIps, nil
}

func GetchInstancesByTag(elbName string, environmentTag string) ([]string, error) {
	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			{
				Name: aws.String("tag-key"),
				Values: []*string{
					aws.String("environment"),
				},
			},
			{
				Name: aws.String("tag-value"),
				Values: []*string{
					aws.String(environmentTag),
				},
			},
		},
	}
	resp, err := getEC2(elbName).DescribeInstances(params)
	if err != nil {
		return nil, err
	}

	instances := make([]string, 0)
	for _, reservations := range resp.Reservations {
		for _, instance := range reservations.Instances {
			if *instance.PublicIpAddress != "" {
				instances = append(instances, *instance.PublicIpAddress)
			}
		}
	}

	return instances, nil
}
<commit_msg>scripts: update vars<commit_after>// this is just a helper package that i (cs) write for multiple ssh access to
// servers
package main

import (
	"flag"
	"fmt"
	"log"
	"os/exec"
	"strings"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/aws/aws-sdk-go/service/elb"
)

var (
	// ELB & EC2 -> AmazonEC2ReadOnlyAccess
	flagAWSSecret = flag.String("awsSecret", "", "aws secret key")
	flagAWSAccess = flag.String("awsAccess", "", "aws access key")

	flagHost       = flag.String("env", "prod", "env name")
	flagFiltered   = flag.Bool("filtered", false, "filter by ec2 tags")
	flagFolderPath = flag.String("flagFolderPath", "/Users/siesta/Documents/koding/credential", "credential repo folder path")
)

var ELBS = map[string]string{
	// app
	"prod":       "awseb-e-x-AWSEBLoa-2AG3XORA8JXC",
	"latest":     "awseb-e-3-AWSEBLoa-1S2VPBAQXDRW9",
	"sandbox":    "awseb-e-p-AWSEBLoa-1POHSLP6A7STY",
	"monitoring": "awseb-e-j-AWSEBLoa-AQBHUYZM5ZX6",
	// proxy
	"proxy-eu-west-1":      "awseb-e-s-AWSEBLoa-1LOTB5BKTJJBW",
	"proxy-us-east-1":      "awseb-e-a-AWSEBLoa-RTLJ62SKJY5G",
	"proxy-us-west-2":      "awseb-e-7-AWSEBLoa-1V808KG9PDQH5",
	"proxy-ap-southeast-1": "awseb-e-u-AWSEBLoa-15H1DQTBBUMG",
	"proxy-dev-us-e-1-v2":  "awseb-e-g-AWSEBLoa-16NJI1FAJCMRG",
}

var Tags = map[string]string{
	// app
	"prod":    "prod.koding.com",
	"latest":  "latest.koding.com",
	"sandbox": "sandbox.koding.com",
}

var ELB2Region = map[string]string{
	// proxies
	"awseb-e-s-AWSEBLoa-1LOTB5BKTJJBW": "eu-west-1",
	"awseb-e-a-AWSEBLoa-RTLJ62SKJY5G":  "us-east-1",
	"awseb-e-7-AWSEBLoa-1V808KG9PDQH5": "us-west-2",
	"awseb-e-u-AWSEBLoa-15H1DQTBBUMG":  "ap-southeast-1",

	// app ELBs
	"awseb-e-j-AWSEBLoa-AQBHUYZM5ZX6":  "us-east-1",
	"awseb-e-x-AWSEBLoa-2AG3XORA8JXC":  "us-east-1",
	"awseb-e-3-AWSEBLoa-1S2VPBAQXDRW9": "us-east-1",
	"awseb-e-p-AWSEBLoa-1POHSLP6A7STY": "us-east-1",
	"awseb-e-g-AWSEBLoa-16NJI1FAJCMRG": "us-east-1",
}

func getEC2(elbName string) *ec2.EC2 {
	reg, ok := ELB2Region[elbName]
	if !ok {
		panic(elbName)
	}

	ses := session.New(&aws.Config{
		Credentials: credentials.NewStaticCredentials(
			*flagAWSAccess,
			*flagAWSSecret,
			"",
		),
		Region:     aws.String(reg),
		MaxRetries: aws.Int(5),
	})

	return ec2.New(ses)
}

func getELB(elbName string) *elb.ELB {
	reg, ok := ELB2Region[elbName]
	if !ok {
		panic(elbName)
	}

	ses := session.New(&aws.Config{
		Credentials: credentials.NewStaticCredentials(
			*flagAWSAccess,
			*flagAWSSecret,
			"",
		),
		Region:     aws.String(reg),
		MaxRetries: aws.Int(5),
	})

	return elb.New(ses)
}

func main() {

	flag.Parse()

	currentELBInstances := make([]string, 0)

	elbNames, ok := ELBS[*flagHost]
	if !ok {
		log.Fatalf("%s not found", *flagHost)
	}

	if *flagFiltered {
		tagName := Tags[*flagHost]
		var err error
		currentELBInstances, err = GetchInstancesByTag(elbNames, tagName)
		if err != nil {
			log.Fatal(err.Error())
		}
	} else {
		for _, elbName := range strings.Split(elbNames, ",") {
			currentInstances, err := fetchProdELBAttachedInstances(elbName)
			if err != nil {
				log.Fatal(err.Error())
			}
			currentELBInstances = append(currentELBInstances, currentInstances...)
		}
	}

	f := createI2csshString(*flagHost, currentELBInstances)
	_, err := exec.Command("i2cssh", strings.Fields(f)...).Output()
	if err != nil {
		log.Fatal(err.Error())
	}
}

var paramToKey = map[string]string{
	"prod":       "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"latest":     "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"sandbox":    "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"monitoring": "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",

	"proxy-eu-west-1":      "/private_keys/koding-eb-deployment-eu-west-1-2015-06.pem",
	"proxy-us-east-1":      "/private_keys/koding-eb-deployment-us-east-1-2015-06.pem",
	"proxy-us-west-2":      "/private_keys/koding-eb-deployment-us-west-2-2015-06.pem",
	"proxy-ap-southeast-1": "/private_keys/koding-eb-deployment-ap-southeast-1-2015-06.pem",
	"proxy-dev-us-e-1-v2":  "/private_keys/koding-eb-deployment-dev-2016-10.pem",
}

func createI2csshString(param string, instances []string) string {
	return fmt.Sprintf(
		"--forward-agent --login ec2-user --rows 3 --broadcast -Xi=%s  --machines %s",
		*flagFolderPath+paramToKey[param],
		strings.Join(instances, ","),
	)
}

// fetch currently attached instances to the prod ELB
func fetchProdELBAttachedInstances(elbName string) ([]string, error) {
	params := &elb.DescribeLoadBalancersInput{
		LoadBalancerNames: []*string{
			aws.String(elbName),
		},
	}
	res, err := getELB(elbName).DescribeLoadBalancers(params)
	if err != nil {
		return nil, err
	}

	if len(res.LoadBalancerDescriptions) < 1 {
		return nil, fmt.Errorf("%s ELB not found!", elbName)
	}

	attachedInstances := make([]*string, 0)
	for _, inst := range res.LoadBalancerDescriptions[0].Instances {
		attachedInstances = append(attachedInstances, inst.InstanceId)
	}

	resp, err := getEC2(elbName).DescribeInstances(
		&ec2.DescribeInstancesInput{
			InstanceIds: attachedInstances,
		},
	)
	if err != nil {
		return nil, err
	}

	instanceIps := make([]string, 0)
	for _, reservations := range resp.Reservations {
		for _, instance := range reservations.Instances {
			if *instance.PublicIpAddress != "" {
				instanceIps = append(instanceIps, *instance.PublicIpAddress)
			}
		}
	}

	return instanceIps, nil
}

func GetchInstancesByTag(elbName string, environmentTag string) ([]string, error) {
	params := &ec2.DescribeInstancesInput{
		Filters: []*ec2.Filter{
			{
				Name: aws.String("tag-key"),
				Values: []*string{
					aws.String("environment"),
				},
			},
			{
				Name: aws.String("tag-value"),
				Values: []*string{
					aws.String(environmentTag),
				},
			},
		},
	}
	resp, err := getEC2(elbName).DescribeInstances(params)
	if err != nil {
		return nil, err
	}

	instances := make([]string, 0)
	for _, reservations := range resp.Reservations {
		for _, instance := range reservations.Instances {
			if *instance.PublicIpAddress != "" {
				instances = append(instances, *instance.PublicIpAddress)
			}
		}
	}

	return instances, nil
}
<|endoftext|>


<commit_before>package manta

import (
	"github.com/dotabuff/manta/dota"
)

// Represents the state of an entity
type PacketEntity struct {
	Index         int32
	ClassId       int32
	ClassName     string
	ClassBaseline *Properties
	Properties    *Properties
	Serial        int32

	flatTbl *dt
}

// Represents a Packet Entity Event Type
type EntityEventType int

// Possible Packet Entity Event Types
const (
	EntityEventType_Create  = EntityEventType(1)
	EntityEventType_Update  = EntityEventType(2)
	EntityEventType_Destroy = EntityEventType(3)
	EntityEventType_Other   = EntityEventType(4)
)

// Represents a packet entity update that happened this tick.
type packetEntityUpdate struct {
	pe *PacketEntity
	t  EntityEventType
}

// Get a property from the entity. Prefers reading from the entity properties,
// falling back to the baseline properties if necessary.
func (pe *PacketEntity) Fetch(key string) (interface{}, bool) {
	if v, ok := pe.Properties.Fetch(key); ok {
		return v, true
	}
	return pe.ClassBaseline.Fetch(key)
}

// Fetches a bool
func (pe *PacketEntity) FetchBool(key string) (bool, bool) {
	if v, ok := pe.Properties.FetchBool(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchBool(key)
}

// Fetches an int32
func (pe *PacketEntity) FetchInt32(key string) (int32, bool) {
	if v, ok := pe.Properties.FetchInt32(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchInt32(key)
}

// Fetches a uint32
func (pe *PacketEntity) FetchUint32(key string) (uint32, bool) {
	if v, ok := pe.Properties.FetchUint32(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchUint32(key)
}

// Fetches a uint64
func (pe *PacketEntity) FetchUint64(key string) (uint64, bool) {
	if v, ok := pe.Properties.FetchUint64(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchUint64(key)
}

// Fetches a float32
func (pe *PacketEntity) FetchFloat32(key string) (float32, bool) {
	if v, ok := pe.Properties.FetchFloat32(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchFloat32(key)
}

// Fetches a string
func (pe *PacketEntity) FetchString(key string) (string, bool) {
	if v, ok := pe.Properties.FetchString(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchString(key)
}

// A function that can handle a game event.
type packetEntityHandler func(*PacketEntity, EntityEventType) error

// Registers a packet entity event handler.
func (p *Parser) OnPacketEntity(fn packetEntityHandler) {
	p.packetEntityHandlers = append(p.packetEntityHandlers, fn)
}

// Internal callback for CSVCMsg_PacketEntities.
func (p *Parser) onCSVCMsg_PacketEntities(m *dota.CSVCMsg_PacketEntities) error {
	// Skip processing if we're configured not to.
	if !p.ProcessPacketEntities {
		return nil
	}

	_debugfl(5, "pTick=%d isDelta=%v deltaFrom=%d updatedEntries=%d maxEntries=%d baseline=%d updateBaseline=%v", p.Tick, m.GetIsDelta(), m.GetDeltaFrom(), m.GetUpdatedEntries(), m.GetMaxEntries(), m.GetBaseline(), m.GetUpdateBaseline())

	// Skip processing full updates after the first. We'll process deltas instead.
	if !m.GetIsDelta() && p.packetEntityFullPackets > 0 {
		return nil
	}

	// Updates pending
	updates := []*packetEntityUpdate{}

	r := NewReader(m.GetEntityData())
	index := int32(-1)
	pe := &PacketEntity{}
	ok := false

	// Iterate over all entries
	for i := 0; i < int(m.GetUpdatedEntries()); i++ {
		// Read the index delta from the buffer. This is an implementation
		// from Alice. An alternate implementation from Yasha has the same result.
		delta := r.readUBitVar()
		index += int32(delta) + 1
		_debugfl(5, "index delta is %d to %d", delta, index)

		// Read the type of update based on two booleans.
		// This appears to be backwards from source 1:
		// true+true used to be "create", now appears to be false+true?
		// This seems suspcious.
		eventType := EntityEventType_Other
		if r.readBoolean() {
			if r.readBoolean() {
				eventType = EntityEventType_Destroy
			} else {
				eventType = EntityEventType_Other
			}
		} else {
			if r.readBoolean() {
				eventType = EntityEventType_Create
			} else {
				eventType = EntityEventType_Update
			}
		}

		_debugfl(5, "update type is %d, %v", eventType, index)

		// Proceed based on the update type
		switch eventType {
		case EntityEventType_Create:
			// Sometimes we're told to create an existing entity.
			// The data doesn't appear to ever change, so just throw it away.
			if pe, ok = p.PacketEntities[index]; ok {
				// We already have an existing entity here, reuse it.
				r.seekBits(p.classIdSize + 25)
			} else {
				// Create a new PacketEntity.
				pe = &PacketEntity{
					Index:      index,
					ClassId:    int32(r.readBits(p.classIdSize)),
					Serial:     int32(r.readBits(25)),
					Properties: NewProperties(),
				}

				// Get the associated class
				if pe.ClassName, ok = p.ClassInfo[pe.ClassId]; !ok {
					_panicf("unable to find class %d", pe.ClassId)
				}

				// Get the associated baseline
				if pe.ClassBaseline, ok = p.ClassBaselines[pe.ClassId]; !ok {
					_panicf("unable to find class baseline %d", pe.ClassId)
				}

				// Get the associated serializer
				if pe.flatTbl, ok = p.serializers[pe.ClassName][0]; !ok {
					_panicf("unable to find serializer for class %s", pe.ClassName)
				}

				// Register the packetEntity with the parser.
				p.PacketEntities[index] = pe
			}

			// Read properties
			pe.Properties.Merge(ReadProperties(r, pe.flatTbl))

		case EntityEventType_Update:
			// Find the existing packetEntity
			pe, ok = p.PacketEntities[index]
			if !ok {
				_panicf("unable to find packet entity %d for update", index)
			}

			// Read properties and update the packetEntity
			pe.Properties.Merge(ReadProperties(r, pe.flatTbl))

		case EntityEventType_Destroy:
			if pe, ok = p.PacketEntities[index]; !ok {
				_panicf("unable to find packet entity %d for delete", index)
			}

			delete(p.PacketEntities, index)
		}

		// Add the update to the list of pending updates.
		updates = append(updates, &packetEntityUpdate{pe, eventType})
	}

	// Update the full packet count.
	if !m.GetIsDelta() {
		p.packetEntityFullPackets += 1
	}

	// Offer all packet entity updates to callback handlers. This is done
	// only after all updates have been processed to ensure consistent state.
	for _, u := range updates {
		for _, h := range p.packetEntityHandlers {
			if err := h(u.pe, u.t); err != nil {
				return err
			}
		}
	}

	return nil
}
<commit_msg>Remove PE optimization, better describe event types<commit_after>package manta

import (
	"github.com/dotabuff/manta/dota"
)

// Represents the state of an entity
type PacketEntity struct {
	Index         int32
	ClassId       int32
	ClassName     string
	ClassBaseline *Properties
	Properties    *Properties
	Serial        int32

	flatTbl *dt
}

// Represents a Packet Entity Event Type
type EntityEventType int

// Possible Packet Entity Event Types
const (
	EntityEventType_None   = EntityEventType(0)
	EntityEventType_Create = EntityEventType(1)
	EntityEventType_Update = EntityEventType(2)
	EntityEventType_Delete = EntityEventType(3)
	EntityEventType_Leave  = EntityEventType(4)
)

// Represents a packet entity update that happened this tick.
type packetEntityUpdate struct {
	pe *PacketEntity
	t  EntityEventType
}

// Get a property from the entity. Prefers reading from the entity properties,
// falling back to the baseline properties if necessary.
func (pe *PacketEntity) Fetch(key string) (interface{}, bool) {
	if v, ok := pe.Properties.Fetch(key); ok {
		return v, true
	}
	return pe.ClassBaseline.Fetch(key)
}

// Fetches a bool
func (pe *PacketEntity) FetchBool(key string) (bool, bool) {
	if v, ok := pe.Properties.FetchBool(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchBool(key)
}

// Fetches an int32
func (pe *PacketEntity) FetchInt32(key string) (int32, bool) {
	if v, ok := pe.Properties.FetchInt32(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchInt32(key)
}

// Fetches a uint32
func (pe *PacketEntity) FetchUint32(key string) (uint32, bool) {
	if v, ok := pe.Properties.FetchUint32(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchUint32(key)
}

// Fetches a uint64
func (pe *PacketEntity) FetchUint64(key string) (uint64, bool) {
	if v, ok := pe.Properties.FetchUint64(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchUint64(key)
}

// Fetches a float32
func (pe *PacketEntity) FetchFloat32(key string) (float32, bool) {
	if v, ok := pe.Properties.FetchFloat32(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchFloat32(key)
}

// Fetches a string
func (pe *PacketEntity) FetchString(key string) (string, bool) {
	if v, ok := pe.Properties.FetchString(key); ok {
		return v, true
	}
	return pe.ClassBaseline.FetchString(key)
}

// A function that can handle a game event.
type packetEntityHandler func(*PacketEntity, EntityEventType) error

// Registers a packet entity event handler.
func (p *Parser) OnPacketEntity(fn packetEntityHandler) {
	p.packetEntityHandlers = append(p.packetEntityHandlers, fn)
}

// Internal callback for CSVCMsg_PacketEntities.
func (p *Parser) onCSVCMsg_PacketEntities(m *dota.CSVCMsg_PacketEntities) error {
	// Skip processing if we're configured not to.
	if !p.ProcessPacketEntities {
		return nil
	}

	_debugfl(5, "pTick=%d isDelta=%v deltaFrom=%d updatedEntries=%d maxEntries=%d baseline=%d updateBaseline=%v", p.Tick, m.GetIsDelta(), m.GetDeltaFrom(), m.GetUpdatedEntries(), m.GetMaxEntries(), m.GetBaseline(), m.GetUpdateBaseline())

	// Skip processing full updates after the first. We'll process deltas instead.
	if !m.GetIsDelta() && p.packetEntityFullPackets > 0 {
		return nil
	}

	// Updates pending
	updates := []*packetEntityUpdate{}

	r := NewReader(m.GetEntityData())
	index := int32(-1)
	pe := &PacketEntity{}
	ok := false

	// Iterate over all entries
	for i := 0; i < int(m.GetUpdatedEntries()); i++ {
		// Read the index delta from the buffer. This is an implementation
		// from Alice. An alternate implementation from Yasha has the same result.
		delta := r.readUBitVar()
		index += int32(delta) + 1
		_debugfl(5, "index delta is %d to %d", delta, index)

		// Read the type of update based on two booleans.
		// This appears to be backwards from source 1:
		// true+true used to be "create", now appears to be false+true?
		// This seems suspcious.
		eventType := EntityEventType_None
		if r.readBoolean() {
			if r.readBoolean() {
				eventType = EntityEventType_Delete
			} else {
				eventType = EntityEventType_Leave
			}
		} else {
			if r.readBoolean() {
				eventType = EntityEventType_Create
			} else {
				eventType = EntityEventType_Update
			}
		}

		_debugfl(5, "update type is %d, %v", eventType, index)

		// Proceed based on the update type
		switch eventType {
		case EntityEventType_Create:
			// Create a new PacketEntity.
			pe = &PacketEntity{
				Index:      index,
				ClassId:    int32(r.readBits(p.classIdSize)),
				Serial:     int32(r.readBits(25)),
				Properties: NewProperties(),
			}

			// Get the associated class
			if pe.ClassName, ok = p.ClassInfo[pe.ClassId]; !ok {
				_panicf("unable to find class %d", pe.ClassId)
			}

			// Get the associated baseline
			if pe.ClassBaseline, ok = p.ClassBaselines[pe.ClassId]; !ok {
				_panicf("unable to find class baseline %d", pe.ClassId)
			}

			// Get the associated serializer
			if pe.flatTbl, ok = p.serializers[pe.ClassName][0]; !ok {
				_panicf("unable to find serializer for class %s", pe.ClassName)
			}

			// Register the packetEntity with the parser.
			p.PacketEntities[index] = pe

			// Read properties
			pe.Properties.Merge(ReadProperties(r, pe.flatTbl))

		case EntityEventType_Update:
			// Find the existing packetEntity
			pe, ok = p.PacketEntities[index]
			if !ok {
				_panicf("unable to find packet entity %d for update", index)
			}

			// Read properties and update the packetEntity
			pe.Properties.Merge(ReadProperties(r, pe.flatTbl))

		case EntityEventType_Delete:
			if pe, ok = p.PacketEntities[index]; !ok {
				_panicf("unable to find packet entity %d for delete", index)
			}

			delete(p.PacketEntities, index)

		case EntityEventType_Leave:
			// TODO: Decide how we want to handle this
		}

		// Add the update to the list of pending updates.
		updates = append(updates, &packetEntityUpdate{pe, eventType})
	}

	// Update the full packet count.
	if !m.GetIsDelta() {
		p.packetEntityFullPackets += 1
	}

	// Offer all packet entity updates to callback handlers. This is done
	// only after all updates have been processed to ensure consistent state.
	for _, u := range updates {
		for _, h := range p.packetEntityHandlers {
			if err := h(u.pe, u.t); err != nil {
				return err
			}
		}
	}

	return nil
}
<|endoftext|>


<commit_before>package x86_64

import (
	uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn"

	"../../models"
	"../../syscalls"
)

var darwinSyscalls = map[int]string{
	1:   "exit",
	2:   "fork",
	3:   "read",
	4:   "write",
	5:   "open",
	6:   "close",
	7:   "wait4",
	9:   "link",
	10:  "unlink",
	73:  "munmap",
	197: "mmap",
	199: "lseek",
}

func DarwinInit(u models.Usercorn, args, env []string) error {
	return AbiInit(u, args, env, LinuxSyscall)
}

func DarwinSyscall(u models.Usercorn) {
	rax, _ := u.RegRead(uc.X86_REG_RAX)
	num := int(rax - 0x2000000)
	name, _ := darwinSyscalls[num]
	ret, _ := u.Syscall(num, name, syscalls.RegArgs(u, AbiRegs))
	u.RegWrite(uc.X86_REG_RAX, ret)
}

func DarwinInterrupt(u models.Usercorn, intno uint32) {
	if intno == 0x80 {
		DarwinSyscall(u)
	}
}

func init() {
	Arch.RegisterOS(&models.OS{Name: "darwin", Init: DarwinInit, Interrupt: DarwinInterrupt})
}
<commit_msg>fix darwin using wrong syscall backend<commit_after>package x86_64

import (
	uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn"

	"../../models"
	"../../syscalls"
)

var darwinSyscalls = map[int]string{
	1:   "exit",
	2:   "fork",
	3:   "read",
	4:   "write",
	5:   "open",
	6:   "close",
	7:   "wait4",
	9:   "link",
	10:  "unlink",
	73:  "munmap",
	197: "mmap",
	199: "lseek",
}

func DarwinInit(u models.Usercorn, args, env []string) error {
	return AbiInit(u, args, env, DarwinSyscall)
}

func DarwinSyscall(u models.Usercorn) {
	rax, _ := u.RegRead(uc.X86_REG_RAX)
	num := int(rax - 0x2000000)
	name, _ := darwinSyscalls[num]
	ret, _ := u.Syscall(num, name, syscalls.RegArgs(u, AbiRegs))
	u.RegWrite(uc.X86_REG_RAX, ret)
}

func DarwinInterrupt(u models.Usercorn, intno uint32) {
	if intno == 0x80 {
		DarwinSyscall(u)
	}
}

func init() {
	Arch.RegisterOS(&models.OS{Name: "darwin", Init: DarwinInit, Interrupt: DarwinInterrupt})
}
<|endoftext|>


<commit_before>// ProveCheck looks for an active proof in the logged in user's id
// table for a service, username pair.

package engine

import (
	"github.com/keybase/client/go/libkb"
)

type ProveCheckArg struct {
	Service  string // the remote service name
	Username string // the user's username on Service
}

// ProveCheck is an engine.
type ProveCheck struct {
	libkb.Contextified
	arg   *ProveCheckArg
	proof libkb.RemoteProofChainLink
}

// NewProveCheck creates a ProveCheck engine.
func NewProveCheck(g *libkb.GlobalContext, arg *ProveCheckArg) *ProveCheck {
	return &ProveCheck{
		Contextified: libkb.NewContextified(g),
		arg:          arg,
	}
}

// Name is the unique engine name.
func (e *ProveCheck) Name() string {
	return "ProveCheck"
}

// GetPrereqs returns the engine prereqs.
func (e *ProveCheck) GetPrereqs() EnginePrereqs {
	return EnginePrereqs{Session: true}
}

// RequiredUIs returns the required UIs.
func (e *ProveCheck) RequiredUIs() []libkb.UIKind {
	return []libkb.UIKind{}
}

// SubConsumers returns the other UI consumers for this engine.
func (e *ProveCheck) SubConsumers() []libkb.UIConsumer {
	return nil
}

// Run starts the engine.
func (e *ProveCheck) Run(ctx *Context) error {
	st := libkb.GetServiceType(e.arg.Service)
	if st == nil {
		return libkb.BadServiceError{Service: e.arg.Service}
	}

	me, err := libkb.LoadMe(libkb.LoadUserArg{ForceReload: true})
	if err != nil {
		return err
	}

	proofs := me.IDTable().GetActiveProofsFor(st)
	if len(proofs) == 0 {
		return libkb.ProofNotFoundForServiceError{Service: e.arg.Service}
	}
	last := proofs[len(proofs)-1]
	if last.GetRemoteUsername() != e.arg.Username {
		return libkb.ProofNotFoundForUsernameError{Service: e.arg.Service, Username: e.arg.Username}
	}
	e.proof = last

	return nil
}

func (e *ProveCheck) Proof() libkb.RemoteProofChainLink {
	return e.proof
}
<commit_msg>changed id_table lookup<commit_after>// ProveCheck looks for an active proof in the logged in user's id
// table for a service, username pair.

package engine

import (
	"github.com/keybase/client/go/libkb"
)

type ProveCheckArg struct {
	Service  string // the remote service name
	Username string // the user's username on Service
}

// ProveCheck is an engine.
type ProveCheck struct {
	libkb.Contextified
	arg   *ProveCheckArg
	proof libkb.RemoteProofChainLink
}

// NewProveCheck creates a ProveCheck engine.
func NewProveCheck(g *libkb.GlobalContext, arg *ProveCheckArg) *ProveCheck {
	return &ProveCheck{
		Contextified: libkb.NewContextified(g),
		arg:          arg,
	}
}

// Name is the unique engine name.
func (e *ProveCheck) Name() string {
	return "ProveCheck"
}

// GetPrereqs returns the engine prereqs.
func (e *ProveCheck) GetPrereqs() EnginePrereqs {
	return EnginePrereqs{Session: true}
}

// RequiredUIs returns the required UIs.
func (e *ProveCheck) RequiredUIs() []libkb.UIKind {
	return []libkb.UIKind{}
}

// SubConsumers returns the other UI consumers for this engine.
func (e *ProveCheck) SubConsumers() []libkb.UIConsumer {
	return nil
}

// Run starts the engine.
func (e *ProveCheck) Run(ctx *Context) error {
	st := libkb.GetServiceType(e.arg.Service)
	if st == nil {
		return libkb.BadServiceError{Service: e.arg.Service}
	}

	me, err := libkb.LoadMe(libkb.LoadUserArg{ForceReload: true})
	if err != nil {
		return err
	}

	proofs := me.IDTable().GetActiveProofsFor(st)
	if len(proofs) == 0 {
		return libkb.ProofNotFoundForServiceError{Service: e.arg.Service}
	}
	for i := len(proofs) - 1; i >= 0; i-- {
		p := proofs[i]
		if p.GetRemoteUsername() != e.arg.Username {
			continue
		}
		if p.GetUID() != me.GetUID() {
			continue
		}
		e.proof = p
		return nil
	}
	return libkb.ProofNotFoundForUsernameError{Service: e.arg.Service, Username: e.arg.Username}
}

func (e *ProveCheck) Proof() libkb.RemoteProofChainLink {
	return e.proof
}
<|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 main

import (
	"errors"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"k8s.io/kubernetes/pkg/apis/core/v1/helper"

	"github.com/golang/glog"
	"github.com/kubernetes-incubator/external-storage/lib/controller"
	"k8s.io/api/core/v1"
	storage "k8s.io/api/storage/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

const (
	provisionerNameKey = "PROVISIONER_NAME"
)

type nfsProvisioner struct {
	client kubernetes.Interface
	server string
	path   string
}

const (
	mountPath = "/persistentvolumes"
)

var _ controller.Provisioner = &nfsProvisioner{}

func (p *nfsProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) {
	if options.PVC.Spec.Selector != nil {
		return nil, fmt.Errorf("claim Selector is not supported")
	}
	glog.V(4).Infof("nfs provisioner: VolumeOptions %v", options)

	pvcNamespace := options.PVC.Namespace
	pvcName := options.PVC.Name

	pvName := strings.Join([]string{pvcNamespace, pvcName, options.PVName}, "-")

	fullPath := filepath.Join(mountPath, pvName)
	glog.V(4).Infof("creating path %s", fullPath)
	if err := os.MkdirAll(fullPath, 0777); err != nil {
		return nil, errors.New("unable to create directory to provision new pv: " + err.Error())
	}
	os.Chmod(fullPath, 0777)

	path := filepath.Join(p.path, pvName)

	pv := &v1.PersistentVolume{
		ObjectMeta: metav1.ObjectMeta{
			Name: options.PVName,
		},
		Spec: v1.PersistentVolumeSpec{
			PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy,
			AccessModes:                   options.PVC.Spec.AccessModes,
			MountOptions:                  options.MountOptions,
			Capacity: v1.ResourceList{
				v1.ResourceName(v1.ResourceStorage): options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)],
			},
			PersistentVolumeSource: v1.PersistentVolumeSource{
				NFS: &v1.NFSVolumeSource{
					Server:   p.server,
					Path:     path,
					ReadOnly: false,
				},
			},
		},
	}
	return pv, nil
}

func (p *nfsProvisioner) Delete(volume *v1.PersistentVolume) error {
	path := volume.Spec.PersistentVolumeSource.NFS.Path
	pvName := filepath.Base(path)
	oldPath := filepath.Join(mountPath, pvName)
	if _, err := os.Stat(oldPath); os.IsNotExist(err) {
		glog.Warningf("path %s does not exist, deletion skipped", oldPath)
		return nil
	}
	// Get the storage class for this volume.
	storageClass, err := p.getClassForVolume(volume)
	if err != nil {
		return err
	}
	// Determine if the "archiveOnDelete" parameter exists.
	// If it exists and has a falsey value, delete the directory.
	// Otherwise, archive it.
	archiveOnDelete, exists := storageClass.Parameters["archiveOnDelete"]
	archiveBool, err := strconv.ParseBool(archiveOnDelete)
	if err != nil {
		return err
	}
	if exists && !archiveBool {
		return os.RemoveAll(oldPath)
	}

	archivePath := filepath.Join(mountPath, "archived-"+pvName)
	glog.V(4).Infof("archiving path %s to %s", oldPath, archivePath)
	return os.Rename(oldPath, archivePath)

}

// getClassForVolume returns StorageClass
func (p *nfsProvisioner) getClassForVolume(pv *v1.PersistentVolume) (*storage.StorageClass, error) {
	if p.client == nil {
		return nil, fmt.Errorf("Cannot get kube client")
	}
	className := helper.GetPersistentVolumeClass(pv)
	if className == "" {
		return nil, fmt.Errorf("Volume has no storage class")
	}
	class, err := p.client.StorageV1().StorageClasses().Get(className, metav1.GetOptions{})
	if err != nil {
		return nil, err
	}
	return class, nil
}

func main() {
	flag.Parse()
	flag.Set("logtostderr", "true")

	server := os.Getenv("NFS_SERVER")
	if server == "" {
		glog.Fatal("NFS_SERVER not set")
	}
	path := os.Getenv("NFS_PATH")
	if path == "" {
		glog.Fatal("NFS_PATH not set")
	}
	provisionerName := os.Getenv(provisionerNameKey)
	if provisionerName == "" {
		glog.Fatalf("environment variable %s is not set! Please set it.", provisionerNameKey)
	}

	// Create an InClusterConfig and use it to create a client for the controller
	// to use to communicate with Kubernetes
	config, err := rest.InClusterConfig()
	if err != nil {
		glog.Fatalf("Failed to create config: %v", err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		glog.Fatalf("Failed to create client: %v", err)
	}

	// The controller needs to know what the server version is because out-of-tree
	// provisioners aren't officially supported until 1.5
	serverVersion, err := clientset.Discovery().ServerVersion()
	if err != nil {
		glog.Fatalf("Error getting server version: %v", err)
	}

	clientNFSProvisioner := &nfsProvisioner{
		client: clientset,
		server: server,
		path:   path,
	}
	// Start the provision controller which will dynamically provision efs NFS
	// PVs
	pc := controller.NewProvisionController(clientset, provisionerName, clientNFSProvisioner, serverVersion.GitVersion)
	pc.Run(wait.NeverStop)
}
<commit_msg>Fix archiveOnDelete parsing<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 main

import (
	"errors"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"k8s.io/kubernetes/pkg/apis/core/v1/helper"

	"github.com/golang/glog"
	"github.com/kubernetes-incubator/external-storage/lib/controller"
	"k8s.io/api/core/v1"
	storage "k8s.io/api/storage/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
)

const (
	provisionerNameKey = "PROVISIONER_NAME"
)

type nfsProvisioner struct {
	client kubernetes.Interface
	server string
	path   string
}

const (
	mountPath = "/persistentvolumes"
)

var _ controller.Provisioner = &nfsProvisioner{}

func (p *nfsProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) {
	if options.PVC.Spec.Selector != nil {
		return nil, fmt.Errorf("claim Selector is not supported")
	}
	glog.V(4).Infof("nfs provisioner: VolumeOptions %v", options)

	pvcNamespace := options.PVC.Namespace
	pvcName := options.PVC.Name

	pvName := strings.Join([]string{pvcNamespace, pvcName, options.PVName}, "-")

	fullPath := filepath.Join(mountPath, pvName)
	glog.V(4).Infof("creating path %s", fullPath)
	if err := os.MkdirAll(fullPath, 0777); err != nil {
		return nil, errors.New("unable to create directory to provision new pv: " + err.Error())
	}
	os.Chmod(fullPath, 0777)

	path := filepath.Join(p.path, pvName)

	pv := &v1.PersistentVolume{
		ObjectMeta: metav1.ObjectMeta{
			Name: options.PVName,
		},
		Spec: v1.PersistentVolumeSpec{
			PersistentVolumeReclaimPolicy: options.PersistentVolumeReclaimPolicy,
			AccessModes:                   options.PVC.Spec.AccessModes,
			MountOptions:                  options.MountOptions,
			Capacity: v1.ResourceList{
				v1.ResourceName(v1.ResourceStorage): options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)],
			},
			PersistentVolumeSource: v1.PersistentVolumeSource{
				NFS: &v1.NFSVolumeSource{
					Server:   p.server,
					Path:     path,
					ReadOnly: false,
				},
			},
		},
	}
	return pv, nil
}

func (p *nfsProvisioner) Delete(volume *v1.PersistentVolume) error {
	path := volume.Spec.PersistentVolumeSource.NFS.Path
	pvName := filepath.Base(path)
	oldPath := filepath.Join(mountPath, pvName)
	if _, err := os.Stat(oldPath); os.IsNotExist(err) {
		glog.Warningf("path %s does not exist, deletion skipped", oldPath)
		return nil
	}
	// Get the storage class for this volume.
	storageClass, err := p.getClassForVolume(volume)
	if err != nil {
		return err
	}
	// Determine if the "archiveOnDelete" parameter exists.
	// If it exists and has a falsey value, delete the directory.
	// Otherwise, archive it.
	archiveOnDelete, exists := storageClass.Parameters["archiveOnDelete"]
	if exists {
		archiveBool, err := strconv.ParseBool(archiveOnDelete)
		if err != nil {
			return err
		}
		if !archiveBool {
			return os.RemoveAll(oldPath)
		}
	}

	archivePath := filepath.Join(mountPath, "archived-"+pvName)
	glog.V(4).Infof("archiving path %s to %s", oldPath, archivePath)
	return os.Rename(oldPath, archivePath)

}

// getClassForVolume returns StorageClass
func (p *nfsProvisioner) getClassForVolume(pv *v1.PersistentVolume) (*storage.StorageClass, error) {
	if p.client == nil {
		return nil, fmt.Errorf("Cannot get kube client")
	}
	className := helper.GetPersistentVolumeClass(pv)
	if className == "" {
		return nil, fmt.Errorf("Volume has no storage class")
	}
	class, err := p.client.StorageV1().StorageClasses().Get(className, metav1.GetOptions{})
	if err != nil {
		return nil, err
	}
	return class, nil
}

func main() {
	flag.Parse()
	flag.Set("logtostderr", "true")

	server := os.Getenv("NFS_SERVER")
	if server == "" {
		glog.Fatal("NFS_SERVER not set")
	}
	path := os.Getenv("NFS_PATH")
	if path == "" {
		glog.Fatal("NFS_PATH not set")
	}
	provisionerName := os.Getenv(provisionerNameKey)
	if provisionerName == "" {
		glog.Fatalf("environment variable %s is not set! Please set it.", provisionerNameKey)
	}

	// Create an InClusterConfig and use it to create a client for the controller
	// to use to communicate with Kubernetes
	config, err := rest.InClusterConfig()
	if err != nil {
		glog.Fatalf("Failed to create config: %v", err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		glog.Fatalf("Failed to create client: %v", err)
	}

	// The controller needs to know what the server version is because out-of-tree
	// provisioners aren't officially supported until 1.5
	serverVersion, err := clientset.Discovery().ServerVersion()
	if err != nil {
		glog.Fatalf("Error getting server version: %v", err)
	}

	clientNFSProvisioner := &nfsProvisioner{
		client: clientset,
		server: server,
		path:   path,
	}
	// Start the provision controller which will dynamically provision efs NFS
	// PVs
	pc := controller.NewProvisionController(clientset, provisionerName, clientNFSProvisioner, serverVersion.GitVersion)
	pc.Run(wait.NeverStop)
}
<|endoftext|>


<commit_before>package gopac

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestIsPlainHostName(t *testing.T) {
	assert.True(t, isPlainHostName("www"), "'www' should be a valid plain host")
	assert.False(t, isPlainHostName("www.netscape.com"), "'www.netscape.com' should not be a valid plain host")
}
<commit_msg>feature(dnsDomainIs): add built-in dnsDomainIs function<commit_after>package gopac

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestIsPlainHostName(t *testing.T) {
	assert.True(t, isPlainHostName("www"), "'www' should be a valid plain host")
	assert.False(t, isPlainHostName("www.netscape.com"), "'www.netscape.com' should not be a valid plain host")
}

func TestDnsDomainIs(t *testing.T) {
	assert.True(t, dnsDomainIs("www.netscape.com", ".netscape.com"), "'www.netscape.com' should be a valid host for domain '.netscape.com'")
	assert.False(t, dnsDomainIs("www", ".netscape.com"), "'www' should not be a valid host for domain '.netscape.com'")
	assert.False(t, dnsDomainIs("www.mcom.com", ".netscape.com"), "'www.mcom.com' should not be a valid host for domain '.netscape.com'")
}
<|endoftext|>


<commit_before>package bagman

import (
	"syscall"
	"sync"
	"errors"
	"log"
)

// Volume tracks the amount of available space on a volume (disk),
// as well as the amount of space claimed for pending operations.
// The purpose is to allow the bag processor to try to determine
// ahead of time whether the underlying disk has enough space to
// accomodate the file it just pulled off the queue. We want to
// avoid downloading 100GB files when we know ahead of time that
// we don't have enough space to process them.
type Volume struct {
	path             string
	mutex            *sync.Mutex
	initialFree      uint64
	claimed          uint64
	messageLog       *log.Logger
}

// NewVolume creates a new Volume structure to track the amount
// of available space and claimed space on a volume (disk).
func NewVolume(path string, messageLog *log.Logger) (*Volume, error) {
	volume := new(Volume)
	volume.mutex = &sync.Mutex{}
	volume.path = path
	volume.claimed = 0
	volume.messageLog = messageLog
	initialFree, err := volume.currentFreeSpace()
	if err != nil {
		messageLog.Println("[ERROR] volume.go could not measure " +
			"free space on storage volume")
		return nil, err
	}
	volume.initialFree = initialFree
	messageLog.Printf("[INFO] Initial free space on storage volume = %d bytes",
		initialFree)
	return volume, nil
}

// InitialFreeSpace returns the number of bytes available to an
// unprivileged user on the volume at the time the Volume struct
// was initialized.
func (volume *Volume) InitialFreeSpace() (numBytes uint64) {
	return volume.initialFree
}

// Claimed space returns the number of bytes reserved for pending
// operations, including downloading and untarring bag archives.
func (volume *Volume) ClaimedSpace() (numBytes uint64) {
	return volume.claimed
}

// currentFreeSpace returns the number of bytes currently available
// to unprivileged users on the underlying volume. This number comes
// directly from the operating system's statfs call, and does not
// take into account the number of bytes reserved for pending operations.
func (volume *Volume)currentFreeSpace() (numBytes uint64, err error) {
	stat := &syscall.Statfs_t{}
	err = syscall.Statfs(volume.path, stat)
	if err != nil {
		return 0, err
	}
	freeBytes := uint64(stat.Bsize) * uint64(stat.Bavail)
	return freeBytes, nil
}

// AvailableSpace returns an approximate number of free bytes currently
// available to unprivileged users on the underlying volume, minus the
// number of bytes reserved for pending processes. The value returned
// will never be 100% accurate, because other processes may be writing
// to the volume.
func (volume *Volume) AvailableSpace() (numBytes uint64) {
	volume.mutex.Lock()
	numBytes = volume.initialFree - volume.claimed
	volume.mutex.Unlock()
	volume.messageLog.Printf("[INFO] Storage volume has %d bytes available",
		numBytes)
	return numBytes
}

// Reserve requests that a number of bytes on disk be reserved for an
// upcoming operation, such as downloading and untarring a file.
// Reserving space does not have any effect on the file system. It
// simply allows the Volume struct to maintain some internal bookkeeping.
// Reserve will return an error if there is not enough free disk space to
// accomodate the requested number of bytes.
func (volume *Volume) Reserve(numBytes uint64) (err error) {
	available := volume.AvailableSpace()
	if numBytes >= available {
		err = errors.New("Requested %d bytes on volume, " +
			"but only %d are available", numBytes, available)
	} else  {
		volume.mutex.Lock()
		volume.claimed += numBytes
		volume.mutex.Unlock()
		volume.messageLog.Printf("[INFO] Reserved %d bytes on storage volume",
			numBytes)
	}
	return err
}

// Release tells the Volume struct that numBytes have been deleted from
// the underlying volume and are free to be reused later.
func (volume *Volume) Release(numBytes uint64) {
	if numBytes > volume.claimed {
		panic("Volume.claimed should not be less than zero!")
	}
	volume.mutex.Lock()
	volume.claimed = volume.claimed - numBytes
	volume.mutex.Unlock()
	volume.messageLog.Printf("[INFO] Freed %d bytes on storage volume",
		numBytes)
}
<commit_msg>Use fmt.Errorf instead of errors.New<commit_after>package bagman

import (
	"syscall"
	"sync"
	"log"
	"fmt"
)

// Volume tracks the amount of available space on a volume (disk),
// as well as the amount of space claimed for pending operations.
// The purpose is to allow the bag processor to try to determine
// ahead of time whether the underlying disk has enough space to
// accomodate the file it just pulled off the queue. We want to
// avoid downloading 100GB files when we know ahead of time that
// we don't have enough space to process them.
type Volume struct {
	path             string
	mutex            *sync.Mutex
	initialFree      uint64
	claimed          uint64
	messageLog       *log.Logger
}

// NewVolume creates a new Volume structure to track the amount
// of available space and claimed space on a volume (disk).
func NewVolume(path string, messageLog *log.Logger) (*Volume, error) {
	volume := new(Volume)
	volume.mutex = &sync.Mutex{}
	volume.path = path
	volume.claimed = 0
	volume.messageLog = messageLog
	initialFree, err := volume.currentFreeSpace()
	if err != nil {
		messageLog.Println("[ERROR] volume.go could not measure " +
			"free space on storage volume")
		return nil, err
	}
	volume.initialFree = initialFree
	messageLog.Printf("[INFO] Initial free space on storage volume = %d bytes",
		initialFree)
	return volume, nil
}

// InitialFreeSpace returns the number of bytes available to an
// unprivileged user on the volume at the time the Volume struct
// was initialized.
func (volume *Volume) InitialFreeSpace() (numBytes uint64) {
	return volume.initialFree
}

// Claimed space returns the number of bytes reserved for pending
// operations, including downloading and untarring bag archives.
func (volume *Volume) ClaimedSpace() (numBytes uint64) {
	return volume.claimed
}

// currentFreeSpace returns the number of bytes currently available
// to unprivileged users on the underlying volume. This number comes
// directly from the operating system's statfs call, and does not
// take into account the number of bytes reserved for pending operations.
func (volume *Volume)currentFreeSpace() (numBytes uint64, err error) {
	stat := &syscall.Statfs_t{}
	err = syscall.Statfs(volume.path, stat)
	if err != nil {
		return 0, err
	}
	freeBytes := uint64(stat.Bsize) * uint64(stat.Bavail)
	return freeBytes, nil
}

// AvailableSpace returns an approximate number of free bytes currently
// available to unprivileged users on the underlying volume, minus the
// number of bytes reserved for pending processes. The value returned
// will never be 100% accurate, because other processes may be writing
// to the volume.
func (volume *Volume) AvailableSpace() (numBytes uint64) {
	volume.mutex.Lock()
	numBytes = volume.initialFree - volume.claimed
	volume.mutex.Unlock()
	volume.messageLog.Printf("[INFO] Storage volume has %d bytes available",
		numBytes)
	return numBytes
}

// Reserve requests that a number of bytes on disk be reserved for an
// upcoming operation, such as downloading and untarring a file.
// Reserving space does not have any effect on the file system. It
// simply allows the Volume struct to maintain some internal bookkeeping.
// Reserve will return an error if there is not enough free disk space to
// accomodate the requested number of bytes.
func (volume *Volume) Reserve(numBytes uint64) (err error) {
	available := volume.AvailableSpace()
	if numBytes >= available {
		err = fmt.Errorf("Requested %d bytes on volume, " +
			"but only %d are available", numBytes, available)
	} else  {
		volume.mutex.Lock()
		volume.claimed += numBytes
		volume.mutex.Unlock()
		volume.messageLog.Printf("[INFO] Reserved %d bytes on storage volume",
			numBytes)
	}
	return err
}

// Release tells the Volume struct that numBytes have been deleted from
// the underlying volume and are free to be reused later.
func (volume *Volume) Release(numBytes uint64) {
	if numBytes > volume.claimed {
		panic("Volume.claimed should not be less than zero!")
	}
	volume.mutex.Lock()
	volume.claimed = volume.claimed - numBytes
	volume.mutex.Unlock()
	volume.messageLog.Printf("[INFO] Freed %d bytes on storage volume",
		numBytes)
}
<|endoftext|>


<commit_before>package audio

import "errors"

var (
	// ErrInvalidBuffer is a generic error returned when trying to read/write to an invalid buffer.
	ErrInvalidBuffer = errors.New("invalid buffer")
)

// Format is a high level representation of the underlying data.
type Format struct {
	// NumChannels is the number of channels contained in the data
	NumChannels int
	// SampleRate is the sampling rate in Hz
	SampleRate int
}

// Buffer is the representation of an audio buffer.
type Buffer interface {
	// PCMFormat is the format of buffer (describing the buffer content/format).
	PCMFormat() *Format
	// NumFrames returns the number of frames contained in the buffer.
	NumFrames() int
	// AsFloatBuffer returns a float 64 buffer from this buffer.
	AsFloatBuffer() *FloatBuffer
	// AsFloat32Buffer returns a float 32 buffer from this buffer.
	AsFloat32Buffer() *Float32Buffer
	// AsIntBuffer returns an int buffer from this buffer.
	AsIntBuffer() *IntBuffer
	// Clone creates a clean clone that can be modified without
	// changing the source buffer.
	Clone() Buffer
}

// FloatBuffer is an audio buffer with its PCM data formatted as float64.
type FloatBuffer struct {
	// Format is the representation of the underlying data format
	Format *Format
	// Data is the buffer PCM data as floats
	Data []float64
}

// PCMFormat returns the buffer format information.
func (buf *FloatBuffer) PCMFormat() *Format { return buf.Format }

// AsFloatBuffer implements the Buffer interface and returns itself.
func (buf *FloatBuffer) AsFloatBuffer() *FloatBuffer { return buf }

// AsFloat32Buffer implements the Buffer interface and returns a float 32 version of itself.
func (buf *FloatBuffer) AsFloat32Buffer() *Float32Buffer {
	newB := &Float32Buffer{}
	newB.Data = make([]float32, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float32(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsIntBuffer returns a copy of this buffer but with data truncated to Ints.
func (buf *FloatBuffer) AsIntBuffer() *IntBuffer {
	newB := &IntBuffer{}
	newB.Data = make([]int, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = int(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (buf *FloatBuffer) Clone() Buffer {
	if buf == nil {
		return nil
	}
	newB := &FloatBuffer{}
	newB.Data = make([]float64, len(buf.Data))
	copy(newB.Data, buf.Data)
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// NumFrames returns the number of frames contained in the buffer.
func (buf *FloatBuffer) NumFrames() int {
	if buf == nil || buf.Format == nil {
		return 0
	}
	numChannels := buf.Format.NumChannels
	if numChannels == 0 {
		numChannels = 1
	}

	return len(buf.Data) / numChannels
}

// Float32Buffer is an audio buffer with its PCM data formatted as float32.
type Float32Buffer struct {
	// Format is the representation of the underlying data format
	Format *Format
	// Data is the buffer PCM data as floats
	Data []float32
}

// PCMFormat returns the buffer format information.
func (buf *Float32Buffer) PCMFormat() *Format { return buf.Format }

// AsFloatBuffer implements the Buffer interface and returns a float64 version of itself.
func (buf *Float32Buffer) AsFloatBuffer() *FloatBuffer {
	newB := &FloatBuffer{}
	newB.Data = make([]float64, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float64(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsFloat32Buffer implements the Buffer interface and returns itself.
func (buf *Float32Buffer) AsFloat32Buffer() *Float32Buffer { return buf }

// AsIntBuffer returns a copy of this buffer but with data truncated to Ints.
func (buf *Float32Buffer) AsIntBuffer() *IntBuffer {
	newB := &IntBuffer{}
	newB.Data = make([]int, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = int(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (buf *Float32Buffer) Clone() Buffer {
	if buf == nil {
		return nil
	}
	newB := &Float32Buffer{}
	newB.Data = make([]float32, len(buf.Data))
	copy(newB.Data, buf.Data)
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// NumFrames returns the number of frames contained in the buffer.
func (buf *Float32Buffer) NumFrames() int {
	if buf == nil || buf.Format == nil {
		return 0
	}
	numChannels := buf.Format.NumChannels
	if numChannels == 0 {
		numChannels = 1
	}

	return len(buf.Data) / numChannels
}

// IntBuffer is an audio buffer with its PCM data formatted as int.
type IntBuffer struct {
	// Format is the representation of the underlying data format
	Format *Format
	// Data is the buffer PCM data as ints
	Data []int
}

// PCMFormat returns the buffer format information.
func (buf *IntBuffer) PCMFormat() *Format { return buf.Format }

// AsFloatBuffer returns a copy of this buffer but with data converted to floats.
func (buf *IntBuffer) AsFloatBuffer() *FloatBuffer {
	newB := &FloatBuffer{}
	newB.Data = make([]float64, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float64(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsFloat32Buffer returns a copy of this buffer but with data converted to float 32.
func (buf *IntBuffer) AsFloat32Buffer() *Float32Buffer {
	newB := &Float32Buffer{}
	newB.Data = make([]float32, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float32(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsIntBuffer implements the Buffer interface and returns itself.
func (buf *IntBuffer) AsIntBuffer() *IntBuffer { return buf }

// NumFrames returns the number of frames contained in the buffer.
func (buf *IntBuffer) NumFrames() int {
	if buf == nil || buf.Format == nil {
		return 0
	}
	numChannels := buf.Format.NumChannels
	if numChannels == 0 {
		numChannels = 1
	}

	return len(buf.Data) / numChannels
}

// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (buf *IntBuffer) Clone() Buffer {
	if buf == nil {
		return nil
	}
	newB := &IntBuffer{}
	newB.Data = make([]int, len(buf.Data))
	copy(newB.Data, buf.Data)
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}
<commit_msg>convert int to float32 on a -1/+1 scale<commit_after>package audio

import (
	"errors"
	"math"
)

var (
	// ErrInvalidBuffer is a generic error returned when trying to read/write to an invalid buffer.
	ErrInvalidBuffer = errors.New("invalid buffer")
)

// Format is a high level representation of the underlying data.
type Format struct {
	// NumChannels is the number of channels contained in the data
	NumChannels int
	// SampleRate is the sampling rate in Hz
	SampleRate int
}

// Buffer is the representation of an audio buffer.
type Buffer interface {
	// PCMFormat is the format of buffer (describing the buffer content/format).
	PCMFormat() *Format
	// NumFrames returns the number of frames contained in the buffer.
	NumFrames() int
	// AsFloatBuffer returns a float 64 buffer from this buffer.
	AsFloatBuffer() *FloatBuffer
	// AsFloat32Buffer returns a float 32 buffer from this buffer.
	AsFloat32Buffer() *Float32Buffer
	// AsIntBuffer returns an int buffer from this buffer.
	AsIntBuffer() *IntBuffer
	// Clone creates a clean clone that can be modified without
	// changing the source buffer.
	Clone() Buffer
}

// FloatBuffer is an audio buffer with its PCM data formatted as float64.
type FloatBuffer struct {
	// Format is the representation of the underlying data format
	Format *Format
	// Data is the buffer PCM data as floats
	Data []float64
}

// PCMFormat returns the buffer format information.
func (buf *FloatBuffer) PCMFormat() *Format { return buf.Format }

// AsFloatBuffer implements the Buffer interface and returns itself.
func (buf *FloatBuffer) AsFloatBuffer() *FloatBuffer { return buf }

// AsFloat32Buffer implements the Buffer interface and returns a float 32 version of itself.
func (buf *FloatBuffer) AsFloat32Buffer() *Float32Buffer {
	newB := &Float32Buffer{}
	newB.Data = make([]float32, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float32(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsIntBuffer returns a copy of this buffer but with data truncated to Ints.
func (buf *FloatBuffer) AsIntBuffer() *IntBuffer {
	newB := &IntBuffer{}
	newB.Data = make([]int, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = int(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (buf *FloatBuffer) Clone() Buffer {
	if buf == nil {
		return nil
	}
	newB := &FloatBuffer{}
	newB.Data = make([]float64, len(buf.Data))
	copy(newB.Data, buf.Data)
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// NumFrames returns the number of frames contained in the buffer.
func (buf *FloatBuffer) NumFrames() int {
	if buf == nil || buf.Format == nil {
		return 0
	}
	numChannels := buf.Format.NumChannels
	if numChannels == 0 {
		numChannels = 1
	}

	return len(buf.Data) / numChannels
}

// Float32Buffer is an audio buffer with its PCM data formatted as float32.
type Float32Buffer struct {
	// Format is the representation of the underlying data format
	Format *Format
	// Data is the buffer PCM data as floats
	Data []float32
}

// PCMFormat returns the buffer format information.
func (buf *Float32Buffer) PCMFormat() *Format { return buf.Format }

// AsFloatBuffer implements the Buffer interface and returns a float64 version of itself.
func (buf *Float32Buffer) AsFloatBuffer() *FloatBuffer {
	newB := &FloatBuffer{}
	newB.Data = make([]float64, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float64(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsFloat32Buffer implements the Buffer interface and returns itself.
func (buf *Float32Buffer) AsFloat32Buffer() *Float32Buffer { return buf }

// AsIntBuffer returns a copy of this buffer but with data truncated to Ints.
func (buf *Float32Buffer) AsIntBuffer() *IntBuffer {
	newB := &IntBuffer{}
	newB.Data = make([]int, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = int(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (buf *Float32Buffer) Clone() Buffer {
	if buf == nil {
		return nil
	}
	newB := &Float32Buffer{}
	newB.Data = make([]float32, len(buf.Data))
	copy(newB.Data, buf.Data)
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// NumFrames returns the number of frames contained in the buffer.
func (buf *Float32Buffer) NumFrames() int {
	if buf == nil || buf.Format == nil {
		return 0
	}
	numChannels := buf.Format.NumChannels
	if numChannels == 0 {
		numChannels = 1
	}

	return len(buf.Data) / numChannels
}

// IntBuffer is an audio buffer with its PCM data formatted as int.
type IntBuffer struct {
	// Format is the representation of the underlying data format
	Format *Format
	// Data is the buffer PCM data as ints
	Data []int
}

// PCMFormat returns the buffer format information.
func (buf *IntBuffer) PCMFormat() *Format { return buf.Format }

// AsFloatBuffer returns a copy of this buffer but with data converted to floats.
func (buf *IntBuffer) AsFloatBuffer() *FloatBuffer {
	newB := &FloatBuffer{}
	newB.Data = make([]float64, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float64(buf.Data[i])
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsFloat32Buffer returns a copy of this buffer but with data converted to float 32.
func (buf *IntBuffer) AsFloat32Buffer() *Float32Buffer {
	newB := &Float32Buffer{}
	newB.Data = make([]float32, len(buf.Data))
	for i := 0; i < len(buf.Data); i++ {
		newB.Data[i] = float32(float64(int16(buf.Data[i])) / math.Pow(2, 8*2-1))
	}
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}

// AsIntBuffer implements the Buffer interface and returns itself.
func (buf *IntBuffer) AsIntBuffer() *IntBuffer { return buf }

// NumFrames returns the number of frames contained in the buffer.
func (buf *IntBuffer) NumFrames() int {
	if buf == nil || buf.Format == nil {
		return 0
	}
	numChannels := buf.Format.NumChannels
	if numChannels == 0 {
		numChannels = 1
	}

	return len(buf.Data) / numChannels
}

// Clone creates a clean clone that can be modified without
// changing the source buffer.
func (buf *IntBuffer) Clone() Buffer {
	if buf == nil {
		return nil
	}
	newB := &IntBuffer{}
	newB.Data = make([]int, len(buf.Data))
	copy(newB.Data, buf.Data)
	newB.Format = &Format{
		NumChannels: buf.Format.NumChannels,
		SampleRate:  buf.Format.SampleRate,
	}
	return newB
}
<|endoftext|>


<commit_before>package bus

import (
	"fmt"
	"net"

	proto "github.com/huin/mqtt"
	"github.com/jeffallen/mqtt"
)

type TinyBus struct {
	mqtt          *mqtt.ClientConn
	subscriptions []*subscription
}

func ConnectTinyBus(host, id string) (*TinyBus, error) {
	conn, err := net.Dial("tcp", host)
	if err != nil {
		return nil, fmt.Errorf("Failed to connect to: %s", err)
	}
	mqtt := mqtt.NewClientConn(conn)
	mqtt.ClientId = id

	err = mqtt.Connect("", "")
	if err != nil {
		return nil, fmt.Errorf("MQTT Failed to connect to: %s", err)
	}

	bus := &TinyBus{
		mqtt:          mqtt,
		subscriptions: make([]*subscription, 0),
	}

	go func() {
		for m := range bus.mqtt.Incoming {
			bus.onIncoming(m)
		}
	}()

	return bus, nil
}

func (b *TinyBus) onIncoming(message *proto.Publish) {
	for _, sub := range b.subscriptions {
		if matches(sub.topic, message.TopicName) {
			go sub.callback(message.TopicName, []byte(message.Payload.(proto.BytesPayload)))
		}
	}
}

func (b *TinyBus) Publish(topic string, payload []byte) {

	b.mqtt.Publish(&proto.Publish{
		TopicName: topic,
		Payload:   proto.BytesPayload(payload),
	})

}

func (b *TinyBus) Subscribe(topic string, callback func(topic string, payload []byte)) (*subscription, error) {

	subscription := &subscription{
		topic:    topic,
		callback: callback,
	}

	err := b.subscribe(subscription)
	if err != nil {
		return nil, err
	}

	b.subscriptions = append(b.subscriptions, subscription)

	return subscription, nil
}

func (b *TinyBus) subscribe(subscription *subscription) error {
	_ = b.mqtt.Subscribe([]proto.TopicQos{proto.TopicQos{subscription.topic, proto.QosAtMostOnce}})
	//spew.Dump("subscription ack", ack)
	// TODO: Check ack
	return nil
}
<commit_msg>Revert "Revert "TinyBus - Add auto-reconnect + last will and testament""<commit_after>package bus

import (
	"fmt"
	"net"
	"sync"
	"time"

	proto "github.com/huin/mqtt"
	"github.com/jeffallen/mqtt"
	"github.com/ninjasphere/go-ninja/config"
)

type TinyBus struct {
	connecting    sync.WaitGroup
	mqtt          *mqtt.ClientConn
	subscriptions []*subscription
	host          string
	id            string
}

func ConnectTinyBus(host, id string) (*TinyBus, error) {

	bus := &TinyBus{
		subscriptions: make([]*subscription, 0),
		host:          host,
		id:            id,
	}

	bus.connect()

	return bus, nil
}

type wrappedConn struct {
	net.Conn
	done chan bool
}

func (c wrappedConn) Close() error {
	log.Warningf("Connection closed!")
	c.done <- true
	c.Conn.Close()
	return nil
}

func (b *TinyBus) connect() {

	b.connecting.Add(1)
	defer func() {
		b.connecting.Done()

		b.publish(&proto.Publish{
			Header: proto.Header{
				Retain: true,
			},
			TopicName: fmt.Sprintf("node/%s/module/%s/state/connected", config.Serial(), b.id),
			Payload:   proto.BytesPayload([]byte("true")),
		})
	}()

	var conn wrappedConn
	for {
		tcpConn, err := net.Dial("tcp", b.host)
		if err == nil {
			conn = wrappedConn{
				Conn: tcpConn,
				done: make(chan bool, 1),
			}
			break
		}

		//log.Warningf("Failed to connect to: %s", err)
		time.Sleep(time.Millisecond * 500)
	}

	if b.mqtt != nil {
		log.Infof("Reconnected to mqtt server")
	}

	mqtt := mqtt.NewClientConn(conn)
	mqtt.ClientId = b.id

	err := mqtt.ConnectCustom(&proto.Connect{
		WillFlag:    true,
		WillQos:     0,
		WillRetain:  true,
		WillTopic:   fmt.Sprintf("$node/%s/module/%s/state/connected", config.Serial(), b.id),
		WillMessage: "false",
	})

	if err != nil {
		log.Fatalf("MQTT Failed to connect to: %s", err)
	}

	b.mqtt = mqtt

	for _, s := range b.subscriptions {
		b.subscribe(s)
	}

	go func() {
		for m := range mqtt.Incoming {
			b.onIncoming(m)
		}
	}()

	go func() {
		<-conn.done
		b.connect()
	}()
}

func (b *TinyBus) onIncoming(message *proto.Publish) {
	for _, sub := range b.subscriptions {
		if matches(sub.topic, message.TopicName) {
			go sub.callback(message.TopicName, []byte(message.Payload.(proto.BytesPayload)))
		}
	}
}

func (b *TinyBus) Publish(topic string, payload []byte) {
	b.connecting.Wait()

	b.publish(&proto.Publish{
		TopicName: topic,
		Payload:   proto.BytesPayload(payload),
	})

}

func (b *TinyBus) publish(message *proto.Publish) {
	b.mqtt.Publish(message)
}

func (b *TinyBus) Subscribe(topic string, callback func(topic string, payload []byte)) (*subscription, error) {

	subscription := &subscription{
		topic:    topic,
		callback: callback,
	}

	err := b.subscribe(subscription)
	if err != nil {
		return nil, err
	}

	b.subscriptions = append(b.subscriptions, subscription)

	return subscription, nil
}

func (b *TinyBus) subscribe(subscription *subscription) error {
	_ = b.mqtt.Subscribe([]proto.TopicQos{proto.TopicQos{subscription.topic, proto.QosAtMostOnce}})
	//spew.Dump("subscription ack", ack)
	// TODO: Check ack
	return nil
}
<|endoftext|>