<commit_before>/*
Copyright 2014 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package handlers

import (
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/golang/glog"

	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/api/meta"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apiserver/pkg/admission"
	"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
	"k8s.io/apiserver/pkg/endpoints/metrics"
	"k8s.io/apiserver/pkg/endpoints/request"
	"k8s.io/apiserver/pkg/registry/rest"
)

// RequestScope encapsulates common fields across all RESTful handler methods.
type RequestScope struct {
	Namer ScopeNamer
	ContextFunc

	Serializer runtime.NegotiatedSerializer
	runtime.ParameterCodec

	Creater         runtime.ObjectCreater
	Convertor       runtime.ObjectConvertor
	Defaulter       runtime.ObjectDefaulter
	Typer           runtime.ObjectTyper
	UnsafeConvertor runtime.ObjectConvertor

	TableConvertor rest.TableConvertor

	Resource    schema.GroupVersionResource
	Kind        schema.GroupVersionKind
	Subresource string

	MetaGroupVersion schema.GroupVersion
}

func (scope *RequestScope) err(err error, w http.ResponseWriter, req *http.Request) {
	ctx := scope.ContextFunc(req)
	responsewriters.ErrorNegotiated(ctx, err, scope.Serializer, scope.Kind.GroupVersion(), w, req)
}

func (scope *RequestScope) AllowsConversion(gvk schema.GroupVersionKind) bool {
	// TODO: this is temporary, replace with an abstraction calculated at endpoint installation time
	if gvk.GroupVersion() == metav1beta1.SchemeGroupVersion {
		switch gvk.Kind {
		case "Table":
			return scope.TableConvertor != nil
		case "PartialObjectMetadata", "PartialObjectMetadataList":
			// TODO: should delineate between lists and non-list endpoints
			return true
		default:
			return false
		}
	}
	return false
}

func (scope *RequestScope) AllowsServerVersion(version string) bool {
	return version == scope.MetaGroupVersion.Version
}

func (scope *RequestScope) AllowsStreamSchema(s string) bool {
	return s == "watch"
}

// MaxRetryWhenPatchConflicts is the maximum number of conflicts retry during a patch operation before returning failure
const MaxRetryWhenPatchConflicts = 5

// ConnectResource returns a function that handles a connect request on a rest.Storage object.
func ConnectResource(connecter rest.Connecter, scope RequestScope, admit admission.Interface, restPath string, isSubresource bool) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		namespace, name, err := scope.Namer.Name(req)
		if err != nil {
			scope.err(err, w, req)
			return
		}
		ctx := scope.ContextFunc(req)
		ctx = request.WithNamespace(ctx, namespace)
		opts, subpath, subpathKey := connecter.NewConnectOptions()
		if err := getRequestOptions(req, scope, opts, subpath, subpathKey, isSubresource); err != nil {
			err = errors.NewBadRequest(err.Error())
			scope.err(err, w, req)
			return
		}
		if admit != nil && admit.Handles(admission.Connect) {
			connectRequest := &rest.ConnectRequest{
				Name:         name,
				Options:      opts,
				ResourcePath: restPath,
			}
			userInfo, _ := request.UserFrom(ctx)
			// TODO: remove the mutating admission here as soon as we have ported all plugin that handle CONNECT
			if mutatingAdmit, ok := admit.(admission.MutationInterface); ok {
				err = mutatingAdmit.Admit(admission.NewAttributesRecord(connectRequest, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Connect, userInfo))
				if err != nil {
					scope.err(err, w, req)
					return
				}
			}
			if mutatingAdmit, ok := admit.(admission.ValidationInterface); ok {
				err = mutatingAdmit.Validate(admission.NewAttributesRecord(connectRequest, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Connect, userInfo))
				if err != nil {
					scope.err(err, w, req)
					return
				}
			}
		}
		requestInfo, _ := request.RequestInfoFrom(ctx)
		metrics.RecordLongRunning(req, requestInfo, func() {
			handler, err := connecter.Connect(ctx, name, opts, &responder{scope: scope, req: req, w: w})
			if err != nil {
				scope.err(err, w, req)
				return
			}
			handler.ServeHTTP(w, req)
		})
	}
}

// responder implements rest.Responder for assisting a connector in writing objects or errors.
type responder struct {
	scope RequestScope
	req   *http.Request
	w     http.ResponseWriter
}

func (r *responder) Object(statusCode int, obj runtime.Object) {
	ctx := r.scope.ContextFunc(r.req)
	responsewriters.WriteObject(ctx, statusCode, r.scope.Kind.GroupVersion(), r.scope.Serializer, obj, r.w, r.req)
}

func (r *responder) Error(err error) {
	r.scope.err(err, r.w, r.req)
}

// resultFunc is a function that returns a rest result and can be run in a goroutine
type resultFunc func() (runtime.Object, error)

// finishRequest makes a given resultFunc asynchronous and handles errors returned by the response.
// An api.Status object with status != success is considered an "error", which interrupts the normal response flow.
func finishRequest(timeout time.Duration, fn resultFunc) (result runtime.Object, err error) {
	// these channels need to be buffered to prevent the goroutine below from hanging indefinitely
	// when the select statement reads something other than the one the goroutine sends on.
	ch := make(chan runtime.Object, 1)
	errCh := make(chan error, 1)
	panicCh := make(chan interface{}, 1)
	go func() {
		// panics don't cross goroutine boundaries, so we have to handle ourselves
		defer utilruntime.HandleCrash(func(panicReason interface{}) {
			// Propagate to parent goroutine
			panicCh <- panicReason
		})

		if result, err := fn(); err != nil {
			errCh <- err
		} else {
			ch <- result
		}
	}()

	select {
	case result = <-ch:
		if status, ok := result.(*metav1.Status); ok {
			if status.Status != metav1.StatusSuccess {
				return nil, errors.FromObject(status)
			}
		}
		return result, nil
	case err = <-errCh:
		return nil, err
	case p := <-panicCh:
		panic(p)
	case <-time.After(timeout):
		return nil, errors.NewTimeoutError("request did not complete within allowed duration", 0)
	}
}

// transformDecodeError adds additional information when a decode fails.
func transformDecodeError(typer runtime.ObjectTyper, baseErr error, into runtime.Object, gvk *schema.GroupVersionKind, body []byte) error {
	objGVKs, _, err := typer.ObjectKinds(into)
	if err != nil {
		return err
	}
	objGVK := objGVKs[0]
	if gvk != nil && len(gvk.Kind) > 0 {
		return errors.NewBadRequest(fmt.Sprintf("%s in version %q cannot be handled as a %s: %v", gvk.Kind, gvk.Version, objGVK.Kind, baseErr))
	}
	summary := summarizeData(body, 30)
	return errors.NewBadRequest(fmt.Sprintf("the object provided is unrecognized (must be of type %s): %v (%s)", objGVK.Kind, baseErr, summary))
}

// setSelfLink sets the self link of an object (or the child items in a list) to the base URL of the request
// plus the path and query generated by the provided linkFunc
func setSelfLink(obj runtime.Object, requestInfo *request.RequestInfo, namer ScopeNamer) error {
	// TODO: SelfLink generation should return a full URL?
	uri, err := namer.GenerateLink(requestInfo, obj)
	if err != nil {
		return nil
	}

	return namer.SetSelfLink(obj, uri)
}

func hasUID(obj runtime.Object) (bool, error) {
	if obj == nil {
		return false, nil
	}
	accessor, err := meta.Accessor(obj)
	if err != nil {
		return false, errors.NewInternalError(err)
	}
	if len(accessor.GetUID()) == 0 {
		return false, nil
	}
	return true, nil
}

// checkName checks the provided name against the request
func checkName(obj runtime.Object, name, namespace string, namer ScopeNamer) error {
	objNamespace, objName, err := namer.ObjectName(obj)
	if err != nil {
		return errors.NewBadRequest(fmt.Sprintf(
			"the name of the object (%s based on URL) was undeterminable: %v", name, err))
	}
	if objName != name {
		return errors.NewBadRequest(fmt.Sprintf(
			"the name of the object (%s) does not match the name on the URL (%s)", objName, name))
	}
	if len(namespace) > 0 {
		if len(objNamespace) > 0 && objNamespace != namespace {
			return errors.NewBadRequest(fmt.Sprintf(
				"the namespace of the object (%s) does not match the namespace on the request (%s)", objNamespace, namespace))
		}
	}

	return nil
}

// setListSelfLink sets the self link of a list to the base URL, then sets the self links
// on all child objects returned. Returns the number of items in the list.
func setListSelfLink(obj runtime.Object, ctx request.Context, req *http.Request, namer ScopeNamer) (int, error) {
	if !meta.IsListType(obj) {
		return 0, nil
	}

	uri, err := namer.GenerateListLink(req)
	if err != nil {
		return 0, err
	}
	if err := namer.SetSelfLink(obj, uri); err != nil {
		glog.V(4).Infof("Unable to set self link on object: %v", err)
	}
	requestInfo, ok := request.RequestInfoFrom(ctx)
	if !ok {
		return 0, fmt.Errorf("missing requestInfo")
	}

	count := 0
	err = meta.EachListItem(obj, func(obj runtime.Object) error {
		count++
		return setSelfLink(obj, requestInfo, namer)
	})
	return count, err
}

func summarizeData(data []byte, maxLength int) string {
	switch {
	case len(data) == 0:
		return "<empty>"
	case data[0] == '{':
		if len(data) > maxLength {
			return string(data[:maxLength]) + " ..."
		}
		return string(data)
	default:
		if len(data) > maxLength {
			return hex.EncodeToString(data[:maxLength]) + " ..."
		}
		return hex.EncodeToString(data)
	}
}

func readBody(req *http.Request) ([]byte, error) {
	defer req.Body.Close()
	return ioutil.ReadAll(req.Body)
}

func parseTimeout(str string) time.Duration {
	if str != "" {
		timeout, err := time.ParseDuration(str)
		if err == nil {
			return timeout
		}
		glog.Errorf("Failed to parse %q: %v", str, err)
	}
	return 30 * time.Second
}
<commit_msg>fix typo: mutating validating admission should be distinguished<commit_after>/*
Copyright 2014 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package handlers

import (
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/golang/glog"

	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/api/meta"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apiserver/pkg/admission"
	"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
	"k8s.io/apiserver/pkg/endpoints/metrics"
	"k8s.io/apiserver/pkg/endpoints/request"
	"k8s.io/apiserver/pkg/registry/rest"
)

// RequestScope encapsulates common fields across all RESTful handler methods.
type RequestScope struct {
	Namer ScopeNamer
	ContextFunc

	Serializer runtime.NegotiatedSerializer
	runtime.ParameterCodec

	Creater         runtime.ObjectCreater
	Convertor       runtime.ObjectConvertor
	Defaulter       runtime.ObjectDefaulter
	Typer           runtime.ObjectTyper
	UnsafeConvertor runtime.ObjectConvertor

	TableConvertor rest.TableConvertor

	Resource    schema.GroupVersionResource
	Kind        schema.GroupVersionKind
	Subresource string

	MetaGroupVersion schema.GroupVersion
}

func (scope *RequestScope) err(err error, w http.ResponseWriter, req *http.Request) {
	ctx := scope.ContextFunc(req)
	responsewriters.ErrorNegotiated(ctx, err, scope.Serializer, scope.Kind.GroupVersion(), w, req)
}

func (scope *RequestScope) AllowsConversion(gvk schema.GroupVersionKind) bool {
	// TODO: this is temporary, replace with an abstraction calculated at endpoint installation time
	if gvk.GroupVersion() == metav1beta1.SchemeGroupVersion {
		switch gvk.Kind {
		case "Table":
			return scope.TableConvertor != nil
		case "PartialObjectMetadata", "PartialObjectMetadataList":
			// TODO: should delineate between lists and non-list endpoints
			return true
		default:
			return false
		}
	}
	return false
}

func (scope *RequestScope) AllowsServerVersion(version string) bool {
	return version == scope.MetaGroupVersion.Version
}

func (scope *RequestScope) AllowsStreamSchema(s string) bool {
	return s == "watch"
}

// MaxRetryWhenPatchConflicts is the maximum number of conflicts retry during a patch operation before returning failure
const MaxRetryWhenPatchConflicts = 5

// ConnectResource returns a function that handles a connect request on a rest.Storage object.
func ConnectResource(connecter rest.Connecter, scope RequestScope, admit admission.Interface, restPath string, isSubresource bool) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		namespace, name, err := scope.Namer.Name(req)
		if err != nil {
			scope.err(err, w, req)
			return
		}
		ctx := scope.ContextFunc(req)
		ctx = request.WithNamespace(ctx, namespace)
		opts, subpath, subpathKey := connecter.NewConnectOptions()
		if err := getRequestOptions(req, scope, opts, subpath, subpathKey, isSubresource); err != nil {
			err = errors.NewBadRequest(err.Error())
			scope.err(err, w, req)
			return
		}
		if admit != nil && admit.Handles(admission.Connect) {
			connectRequest := &rest.ConnectRequest{
				Name:         name,
				Options:      opts,
				ResourcePath: restPath,
			}
			userInfo, _ := request.UserFrom(ctx)
			// TODO: remove the mutating admission here as soon as we have ported all plugin that handle CONNECT
			if mutatingAdmission, ok := admit.(admission.MutationInterface); ok {
				err = mutatingAdmission.Admit(admission.NewAttributesRecord(connectRequest, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Connect, userInfo))
				if err != nil {
					scope.err(err, w, req)
					return
				}
			}
			if validatingAdmission, ok := admit.(admission.ValidationInterface); ok {
				err = validatingAdmission.Validate(admission.NewAttributesRecord(connectRequest, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Connect, userInfo))
				if err != nil {
					scope.err(err, w, req)
					return
				}
			}
		}
		requestInfo, _ := request.RequestInfoFrom(ctx)
		metrics.RecordLongRunning(req, requestInfo, func() {
			handler, err := connecter.Connect(ctx, name, opts, &responder{scope: scope, req: req, w: w})
			if err != nil {
				scope.err(err, w, req)
				return
			}
			handler.ServeHTTP(w, req)
		})
	}
}

// responder implements rest.Responder for assisting a connector in writing objects or errors.
type responder struct {
	scope RequestScope
	req   *http.Request
	w     http.ResponseWriter
}

func (r *responder) Object(statusCode int, obj runtime.Object) {
	ctx := r.scope.ContextFunc(r.req)
	responsewriters.WriteObject(ctx, statusCode, r.scope.Kind.GroupVersion(), r.scope.Serializer, obj, r.w, r.req)
}

func (r *responder) Error(err error) {
	r.scope.err(err, r.w, r.req)
}

// resultFunc is a function that returns a rest result and can be run in a goroutine
type resultFunc func() (runtime.Object, error)

// finishRequest makes a given resultFunc asynchronous and handles errors returned by the response.
// An api.Status object with status != success is considered an "error", which interrupts the normal response flow.
func finishRequest(timeout time.Duration, fn resultFunc) (result runtime.Object, err error) {
	// these channels need to be buffered to prevent the goroutine below from hanging indefinitely
	// when the select statement reads something other than the one the goroutine sends on.
	ch := make(chan runtime.Object, 1)
	errCh := make(chan error, 1)
	panicCh := make(chan interface{}, 1)
	go func() {
		// panics don't cross goroutine boundaries, so we have to handle ourselves
		defer utilruntime.HandleCrash(func(panicReason interface{}) {
			// Propagate to parent goroutine
			panicCh <- panicReason
		})

		if result, err := fn(); err != nil {
			errCh <- err
		} else {
			ch <- result
		}
	}()

	select {
	case result = <-ch:
		if status, ok := result.(*metav1.Status); ok {
			if status.Status != metav1.StatusSuccess {
				return nil, errors.FromObject(status)
			}
		}
		return result, nil
	case err = <-errCh:
		return nil, err
	case p := <-panicCh:
		panic(p)
	case <-time.After(timeout):
		return nil, errors.NewTimeoutError("request did not complete within allowed duration", 0)
	}
}

// transformDecodeError adds additional information when a decode fails.
func transformDecodeError(typer runtime.ObjectTyper, baseErr error, into runtime.Object, gvk *schema.GroupVersionKind, body []byte) error {
	objGVKs, _, err := typer.ObjectKinds(into)
	if err != nil {
		return err
	}
	objGVK := objGVKs[0]
	if gvk != nil && len(gvk.Kind) > 0 {
		return errors.NewBadRequest(fmt.Sprintf("%s in version %q cannot be handled as a %s: %v", gvk.Kind, gvk.Version, objGVK.Kind, baseErr))
	}
	summary := summarizeData(body, 30)
	return errors.NewBadRequest(fmt.Sprintf("the object provided is unrecognized (must be of type %s): %v (%s)", objGVK.Kind, baseErr, summary))
}

// setSelfLink sets the self link of an object (or the child items in a list) to the base URL of the request
// plus the path and query generated by the provided linkFunc
func setSelfLink(obj runtime.Object, requestInfo *request.RequestInfo, namer ScopeNamer) error {
	// TODO: SelfLink generation should return a full URL?
	uri, err := namer.GenerateLink(requestInfo, obj)
	if err != nil {
		return nil
	}

	return namer.SetSelfLink(obj, uri)
}

func hasUID(obj runtime.Object) (bool, error) {
	if obj == nil {
		return false, nil
	}
	accessor, err := meta.Accessor(obj)
	if err != nil {
		return false, errors.NewInternalError(err)
	}
	if len(accessor.GetUID()) == 0 {
		return false, nil
	}
	return true, nil
}

// checkName checks the provided name against the request
func checkName(obj runtime.Object, name, namespace string, namer ScopeNamer) error {
	objNamespace, objName, err := namer.ObjectName(obj)
	if err != nil {
		return errors.NewBadRequest(fmt.Sprintf(
			"the name of the object (%s based on URL) was undeterminable: %v", name, err))
	}
	if objName != name {
		return errors.NewBadRequest(fmt.Sprintf(
			"the name of the object (%s) does not match the name on the URL (%s)", objName, name))
	}
	if len(namespace) > 0 {
		if len(objNamespace) > 0 && objNamespace != namespace {
			return errors.NewBadRequest(fmt.Sprintf(
				"the namespace of the object (%s) does not match the namespace on the request (%s)", objNamespace, namespace))
		}
	}

	return nil
}

// setListSelfLink sets the self link of a list to the base URL, then sets the self links
// on all child objects returned. Returns the number of items in the list.
func setListSelfLink(obj runtime.Object, ctx request.Context, req *http.Request, namer ScopeNamer) (int, error) {
	if !meta.IsListType(obj) {
		return 0, nil
	}

	uri, err := namer.GenerateListLink(req)
	if err != nil {
		return 0, err
	}
	if err := namer.SetSelfLink(obj, uri); err != nil {
		glog.V(4).Infof("Unable to set self link on object: %v", err)
	}
	requestInfo, ok := request.RequestInfoFrom(ctx)
	if !ok {
		return 0, fmt.Errorf("missing requestInfo")
	}

	count := 0
	err = meta.EachListItem(obj, func(obj runtime.Object) error {
		count++
		return setSelfLink(obj, requestInfo, namer)
	})
	return count, err
}

func summarizeData(data []byte, maxLength int) string {
	switch {
	case len(data) == 0:
		return "<empty>"
	case data[0] == '{':
		if len(data) > maxLength {
			return string(data[:maxLength]) + " ..."
		}
		return string(data)
	default:
		if len(data) > maxLength {
			return hex.EncodeToString(data[:maxLength]) + " ..."
		}
		return hex.EncodeToString(data)
	}
}

func readBody(req *http.Request) ([]byte, error) {
	defer req.Body.Close()
	return ioutil.ReadAll(req.Body)
}

func parseTimeout(str string) time.Duration {
	if str != "" {
		timeout, err := time.ParseDuration(str)
		if err == nil {
			return timeout
		}
		glog.Errorf("Failed to parse %q: %v", str, err)
	}
	return 30 * time.Second
}
<|endoftext|>


<commit_before>/*
Copyright 2018 Mirantis

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 e2e

import (
	"context"
	"io/ioutil"
	"os"
	"strings"
	"time"

	. "github.com/onsi/gomega"
	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/Mirantis/virtlet/tests/e2e/framework"
	. "github.com/Mirantis/virtlet/tests/e2e/ginkgo-ext"
)

const (
	gendocTmpDir = "/tmp"
)

var _ = Describe("virtletctl", func() {
	It("Should display usage info on help subcommand", func(done Done) {
		defer close(done)

		ctx, closeFunc := context.WithCancel(context.Background())
		defer closeFunc()
		localExecutor := framework.LocalExecutor(ctx)

		By("Calling virtletctl help")
		callVirtletctl(localExecutor, "help")
	}, 10)

	Context("Tests depending on spawning VM", func() {
		var (
			vm           *framework.VMInterface
			tempfileName string
		)

		BeforeAll(func() {
			cm := &v1.ConfigMap{
				ObjectMeta: metav1.ObjectMeta{
					Name: "sshkey",
				},
				Data: map[string]string{
					"authorized_keys": SshPublicKey,
				},
			}
			_, err := controller.ConfigMaps().Create(cm)
			Expect(err).NotTo(HaveOccurred())

			vm = controller.VM("virtletctl-cirros-vm")
			Expect(vm.CreateAndWait(VMOptions{
				SSHKeySource: "configmap/sshkey",
			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())

			waitSSH(vm)

			tempfile, err := ioutil.TempFile("", "")
			Expect(err).NotTo(HaveOccurred())
			defer tempfile.Close()
			tempfileName = tempfile.Name()

			strippedKey := strings.Replace(SshPrivateKey, "\t", "", -1)
			_, err = tempfile.Write([]byte(strippedKey))
			Expect(err).NotTo(HaveOccurred())
			Expect(os.Chmod(tempfileName, 0600)).To(Succeed())
		})

		AfterAll(func() {
			deleteVM(vm)
			controller.ConfigMaps().Delete("sshkey", nil)
			os.Remove(tempfileName)
		})

		It("Should be able to access the pod via ssh using ssh subcommand", func(done Done) {
			By("Calling virtletctl ssh cirros@cirros-vm hostname")
			defer close(done)

			ctx, closeFunc := context.WithCancel(context.Background())
			defer closeFunc()
			localExecutor := framework.LocalExecutor(ctx)

			output := callVirtletctl(localExecutor, "ssh", "--namespace", controller.Namespace(), "cirros@virtletctl-cirros-vm", "--", "-i", tempfileName, "hostname")
			Expect(output).To(Equal("virtletctl-cirros-vm"))
		}, 60)

		It("Should dump Virtlet diagnostics on diag dump subcommand", func(done Done) {
			defer close(done)

			ctx, closeFunc := context.WithCancel(context.Background())
			defer closeFunc()
			localExecutor := framework.LocalExecutor(ctx)

			By("Calling virtletctl diag dump")
			output := callVirtletctl(localExecutor, "diag", "dump", "--json")
			Expect(output).To(ContainSubstring("cirros-vm"))
		}, 60)

	})

	It("Should return libvirt version on virsh subcommand", func(done Done) {
		defer close(done)

		ctx, closeFunc := context.WithCancel(context.Background())
		defer closeFunc()
		localExecutor := framework.LocalExecutor(ctx)

		By("Calling virtletctl virsh version")
		output := callVirtletctl(localExecutor, "virsh", "version")
		Expect(output).To(ContainSubstring("Compiled against library:"))
		Expect(output).To(ContainSubstring("Using library:"))
	}, 60)

	It("Should install itself as a kubectl plugin on install subcommand [unsafe]", func() {
		includeUnsafe()
		ctx, closeFunc := context.WithCancel(context.Background())
		defer closeFunc()
		localExecutor := framework.LocalExecutor(ctx)

		By("Calling virtletctl install")
		callVirtletctl(localExecutor, "install")

		By("Calling kubectl plugin virt help")
		_, err := framework.RunSimple(localExecutor, "kubectl", "plugin", "virt", "help")
		Expect(err).NotTo(HaveOccurred())
	}, 60)
})

func callVirtletctl(executor framework.Executor, args ...string) string {
	args = append([]string{"_output/virtletctl", "-s", *framework.ClusterURL}, args...)
	output, err := framework.RunSimple(executor, args...)
	Expect(err).NotTo(HaveOccurred())
	return output
}
<commit_msg>Improve virtletctl diag e2e<commit_after>/*
Copyright 2018 Mirantis

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 e2e

import (
	"context"
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"time"

	. "github.com/onsi/gomega"
	"k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	"github.com/Mirantis/virtlet/tests/e2e/framework"
	. "github.com/Mirantis/virtlet/tests/e2e/ginkgo-ext"
)

const (
	gendocTmpDir = "/tmp"
)

var _ = Describe("virtletctl", func() {
	It("Should display usage info on help subcommand", func(done Done) {
		defer close(done)

		ctx, closeFunc := context.WithCancel(context.Background())
		defer closeFunc()
		localExecutor := framework.LocalExecutor(ctx)

		By("Calling virtletctl help")
		callVirtletctl(localExecutor, "help")
	}, 10)

	Context("Tests depending on spawning VM", func() {
		var (
			vm           *framework.VMInterface
			tempfileName string
		)

		BeforeAll(func() {
			cm := &v1.ConfigMap{
				ObjectMeta: metav1.ObjectMeta{
					Name: "sshkey",
				},
				Data: map[string]string{
					"authorized_keys": SshPublicKey,
				},
			}
			_, err := controller.ConfigMaps().Create(cm)
			Expect(err).NotTo(HaveOccurred())

			vm = controller.VM("virtletctl-cirros-vm")
			Expect(vm.CreateAndWait(VMOptions{
				SSHKeySource: "configmap/sshkey",
			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())

			waitSSH(vm)

			tempfile, err := ioutil.TempFile("", "")
			Expect(err).NotTo(HaveOccurred())
			defer tempfile.Close()
			tempfileName = tempfile.Name()

			strippedKey := strings.Replace(SshPrivateKey, "\t", "", -1)
			_, err = tempfile.Write([]byte(strippedKey))
			Expect(err).NotTo(HaveOccurred())
			Expect(os.Chmod(tempfileName, 0600)).To(Succeed())
		})

		AfterAll(func() {
			deleteVM(vm)
			controller.ConfigMaps().Delete("sshkey", nil)
			os.Remove(tempfileName)
		})

		It("Should be able to access the pod via ssh using ssh subcommand", func(done Done) {
			By("Calling virtletctl ssh cirros@cirros-vm hostname")
			defer close(done)

			ctx, closeFunc := context.WithCancel(context.Background())
			defer closeFunc()
			localExecutor := framework.LocalExecutor(ctx)

			output := callVirtletctl(localExecutor, "ssh", "--namespace", controller.Namespace(), "cirros@virtletctl-cirros-vm", "--", "-i", tempfileName, "hostname")
			Expect(output).To(Equal("virtletctl-cirros-vm"))
		}, 60)

		It("Should dump Virtlet diagnostics on diag dump subcommand", func(done Done) {
			defer close(done)

			ctx, closeFunc := context.WithCancel(context.Background())
			defer closeFunc()
			localExecutor := framework.LocalExecutor(ctx)

			By("Calling virtletctl diag dump")
			tempDir, err := ioutil.TempDir("", "diag-out")
			Expect(err).NotTo(HaveOccurred())
			defer func() {
				os.RemoveAll(tempDir)
			}()
			callVirtletctl(localExecutor, "diag", "dump", tempDir)
			metadataFiles, err := filepath.Glob(filepath.Join(tempDir, "nodes/*/metadata.txt"))
			Expect(err).NotTo(HaveOccurred())
			Expect(metadataFiles).NotTo(BeEmpty())
			content, err := ioutil.ReadFile(metadataFiles[0])
			Expect(err).NotTo(HaveOccurred())
			Expect(string(content)).To(ContainSubstring("Sandboxes:"))

			By("Calling virtletctl diag dump --json")
			output := callVirtletctl(localExecutor, "diag", "dump", "--json")
			var decoded map[string]interface{}
			Expect(json.Unmarshal([]byte(output), &decoded)).To(Succeed())
			Expect(decoded["name"]).To(Equal("nodes"))
			Expect(decoded["children"]).To(BeAssignableToTypeOf(map[string]interface{}{}))
			m := decoded["children"].(map[string]interface{})
			nodeName := ""
			for name := range m {
				nodeName = name
				break
			}
			Expect(nodeName).NotTo(BeEmpty())
			Expect(m[nodeName]).To(BeAssignableToTypeOf(map[string]interface{}{}))
			m = m[nodeName].(map[string]interface{})
			Expect(m["children"]).To(BeAssignableToTypeOf(map[string]interface{}{}))
			m = m["children"].(map[string]interface{})
			Expect(m["metadata"]).To(BeAssignableToTypeOf(map[string]interface{}{}))
			m = m["metadata"].(map[string]interface{})
			Expect(m["data"]).To(ContainSubstring("Sandboxes:"))
		}, 60)
	})

	It("Should return libvirt version on virsh subcommand", func(done Done) {
		defer close(done)

		ctx, closeFunc := context.WithCancel(context.Background())
		defer closeFunc()
		localExecutor := framework.LocalExecutor(ctx)

		By("Calling virtletctl virsh version")
		output := callVirtletctl(localExecutor, "virsh", "version")
		Expect(output).To(ContainSubstring("Compiled against library:"))
		Expect(output).To(ContainSubstring("Using library:"))
	}, 60)

	It("Should install itself as a kubectl plugin on install subcommand [unsafe]", func() {
		includeUnsafe()
		ctx, closeFunc := context.WithCancel(context.Background())
		defer closeFunc()
		localExecutor := framework.LocalExecutor(ctx)

		By("Calling virtletctl install")
		callVirtletctl(localExecutor, "install")

		By("Calling kubectl plugin virt help")
		_, err := framework.RunSimple(localExecutor, "kubectl", "plugin", "virt", "help")
		Expect(err).NotTo(HaveOccurred())
	}, 60)
})

func callVirtletctl(executor framework.Executor, args ...string) string {
	args = append([]string{"_output/virtletctl", "-s", *framework.ClusterURL}, args...)
	output, err := framework.RunSimple(executor, args...)
	Expect(err).NotTo(HaveOccurred())
	return output
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"math/rand"
	"net"
	"net/http"
	"time"

	"github.com/zserge/webview"
)

const (
	windowWidth  = 480
	windowHeight = 320
)

var indexHTML = fmt.Sprintf(`
<!doctype html>
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
	</head>
	<body>
		<canvas id="canvas" width="%d" height="%d">
			Your browser doesn't support HTML5 canvas element.
		</canvas>
		<script type="text/javascript">
			window.drawData = {};
			function draw() {
				window.external.invoke_('draw');
				var canvas = document.getElementById('canvas');
				var ctx = canvas.getContext('2d');
				ctx.clearRect(0, 0, canvas.width, canvas.height);
				ctx.beginPath();
				ctx.moveTo(drawData.x1, drawData.y1);
				ctx.lineTo(drawData.x2, drawData.y2);
				ctx.stroke();
				window.requestAnimationFrame(draw);
			}
			draw();
		</script>
	</body>
</html>
`, windowWidth, windowHeight)

func startServer() string {
	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		log.Fatal(err)
	}
	go func() {
		defer ln.Close()
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte(indexHTML))
		})
		log.Fatal(http.Serve(ln, nil))
	}()
	return "http://" + ln.Addr().String()
}

var (
	numFrames int
	prevTime  time.Time
	totalTime time.Duration
)

func handleRPC(w webview.WebView, data string) {
	numFrames++
	if !prevTime.IsZero() {
		totalTime = totalTime + time.Since(prevTime)
	}
	prevTime = time.Now()
	if numFrames%100 == 0 {
		d := totalTime / time.Duration(numFrames)
		log.Println("time per frame:", d, " fps:", int(time.Second/d))
	}
	s := fmt.Sprintf(`drawData = {x1:%d,y1:%d,x2:%d,y2:%d}`,
		rand.Intn(windowWidth), rand.Intn(windowHeight),
		rand.Intn(windowWidth), rand.Intn(windowHeight))
	w.Eval(s)
}

func main() {
	url := startServer()
	w := webview.New(webview.Settings{
		Width:  windowWidth,
		Height: windowHeight,
		Title:  "Simple canvas demo",
		URL:    url,
		ExternalInvokeCallback: handleRPC,
	})
	defer w.Exit()
	w.Run()
}
<commit_msg>examples: add css reset<commit_after>package main

import (
	"fmt"
	"log"
	"math/rand"
	"net"
	"net/http"
	"time"

	"github.com/zserge/webview"
)

const (
	windowWidth  = 480
	windowHeight = 320
)

var indexHTML = fmt.Sprintf(`
<!doctype html>
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<style>* { margin: 0; padding: 0; box-sizing: border-box; }</style>
	</head>
	<body>
		<canvas id="canvas" width="%d" height="%d">
			Your browser doesn't support HTML5 canvas element.
		</canvas>
		<script type="text/javascript">
			window.drawData = {};
			function draw() {
				window.external.invoke_('draw');
				var canvas = document.getElementById('canvas');
				var ctx = canvas.getContext('2d');
				ctx.clearRect(0, 0, canvas.width, canvas.height);
				ctx.beginPath();
				ctx.moveTo(drawData.x1, drawData.y1);
				ctx.lineTo(drawData.x2, drawData.y2);
				ctx.stroke();
				window.requestAnimationFrame(draw);
			}
			draw();
		</script>
	</body>
</html>
`, windowWidth, windowHeight)

func startServer() string {
	ln, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		log.Fatal(err)
	}
	go func() {
		defer ln.Close()
		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte(indexHTML))
		})
		log.Fatal(http.Serve(ln, nil))
	}()
	return "http://" + ln.Addr().String()
}

var (
	numFrames int
	prevTime  time.Time
	totalTime time.Duration
)

func handleRPC(w webview.WebView, data string) {
	numFrames++
	if !prevTime.IsZero() {
		totalTime = totalTime + time.Since(prevTime)
	}
	prevTime = time.Now()
	if numFrames%100 == 0 {
		d := totalTime / time.Duration(numFrames)
		log.Println("time per frame:", d, " fps:", int(time.Second/d))
	}
	s := fmt.Sprintf(`drawData = {x1:%d,y1:%d,x2:%d,y2:%d}`,
		rand.Intn(windowWidth), rand.Intn(windowHeight),
		rand.Intn(windowWidth), rand.Intn(windowHeight))
	w.Eval(s)
}

func main() {
	url := startServer()
	w := webview.New(webview.Settings{
		Width:  windowWidth,
		Height: windowHeight,
		Title:  "Simple canvas demo",
		URL:    url,
		ExternalInvokeCallback: handleRPC,
	})
	defer w.Exit()
	w.Run()
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"net/http"
	"text/template"
	"os"
	"io"
	"github.com/gorilla/mux"
	"github.com/twinj/uuid"
	_ "bufio"
	_ "io/ioutil"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func hello(w http.ResponseWriter, r *http.Request) {
	templatePath := "index.html"
	template, _ := template.ParseFiles(templatePath)
	template.Execute(w, nil)
	fmt.Println("Hit index.html")
}

func game(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	gameId := vars["gameId"]
	fmt.Println(gameId)

	gameMap := make(map[string]string)
	gameMap["GameId"] = gameId

	templatePath := "game.html"
	template, _ := template.ParseFiles(templatePath)
	template.Execute(w, gameMap)
	fmt.Println("Hit game.html")
}

func newGame(w http.ResponseWriter, r *http.Request) {
	// The UUID for this game which will be used everywhere
	// as this game's identifier
	u := uuid.NewV4()
	io.WriteString(w, u.String())

	// Create a file with this name and initialize it
	f, err := os.Create("./" + u.String() + ".json")
    check(err)
    defer f.Close()
}

func main() {
	// The default UUID format has gross curly brackets in it!
	uuid.SwitchFormat(uuid.CleanHyphen)

	// Setup all routes and serve
    r := mux.NewRouter()
    r.HandleFunc("/", hello)
    r.HandleFunc("/newgame", newGame)
    r.HandleFunc("/game/{gameId}", game)
    r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
    http.Handle("/", r)
    http.ListenAndServe(":8000", nil)
}

// Misc notes:
/*
	Games should probably be saved as individual json files, <UUID>.json
	Those files should have a history of all moves that were made in this game

	Maybe there should also be a copy of the board as it was at the very end?
	This SHOULD be figure-out-able given just the history of moves, but
	people making very primitive AIs shouldn't have to roll their own board
	generator.

*/
<commit_msg>Added actual JSON encoding/decoding of games and moves<commit_after>package main

import (
	"fmt"
	"net/http"
	"text/template"
	"os"
	"io"
	"github.com/gorilla/mux"
	"github.com/twinj/uuid"
	"encoding/json"
	_ "bufio"
	_ "io/ioutil"
)

type Move struct {
	// A Move is just a coordinate but if it has the special value (-1, -1)
	// it represents a skipped move
	X int
	Y int
}
type Game struct {
    Uuid string
    Player1 string
    Player2 string
    Moves []Move
}

/*  Custom marshal and unmarshal methods are needed because without them, the
    list of moves looks like this:
		[{"X":8,"Y":4}, ...]
	With the custom marshalling, the list of moves looks like this:
		[[8,4], ...]
	Which uses 5 bytes per move instead of 13
*/
func (m Move) MarshalJSON() ([]byte, error) {
    return json.Marshal([]int{m.X, m.Y})
}
func (m *Move) UnmarshalJSON(data []byte) error {
    var coordinate [2]int
    json.Unmarshal(data, &coordinate)
    m.X = coordinate[0]
    m.Y = coordinate[1]
    return nil
}

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func hello(w http.ResponseWriter, r *http.Request) {
	templatePath := "index.html"
	template, _ := template.ParseFiles(templatePath)
	template.Execute(w, nil)
	fmt.Println("Hit index.html")
}

func game(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	gameId := vars["gameId"]
	fmt.Println(gameId)

	gameMap := make(map[string]string)
	gameMap["GameId"] = gameId

	templatePath := "game.html"
	template, _ := template.ParseFiles(templatePath)
	template.Execute(w, gameMap)
	fmt.Println("Hit game.html")
}

func newGame(w http.ResponseWriter, r *http.Request) {
	// The UUID for this game which will be used everywhere
	// as this game's identifier
	u := uuid.NewV4()
	io.WriteString(w, u.String())

	var g Game
	g.Uuid = u.String()
	g.Player1 = "foo"
	g.Player2 = "bar"
	g.Moves = append(g.Moves, Move{5, 5})
	g.Moves = append(g.Moves, Move{5, 6})

	b, _ := json.Marshal(g)

	// Create a file with this name and initialize it
	f, err := os.Create("./" + u.String() + ".json")
    check(err)
    defer f.Close()
	f.Write(b)
	f.WriteString("\n")
}

func main() {
	// The default UUID format has gross curly brackets in it!
	uuid.SwitchFormat(uuid.CleanHyphen)

	// Setup all routes and serve
    r := mux.NewRouter()
    r.HandleFunc("/", hello)
    r.HandleFunc("/newgame", newGame)
    r.HandleFunc("/game/{gameId}", game)
    r.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
    http.Handle("/", r)
    http.ListenAndServe(":8000", nil)
}

// Misc notes:
/*
	Games should probably be saved as individual json files, <UUID>.json
	Those files should have a history of all moves that were made in this game

	Maybe there should also be a copy of the board as it was at the very end?
	This SHOULD be figure-out-able given just the history of moves, but
	people making very primitive AIs shouldn't have to roll their own board
	generator.

*/
<|endoftext|>


<commit_before>package main

import (
	"cli"
	"fmt"
	"github.com/qiniu/log"
	"os"
)

var debugMode = false

var supportedCmds = map[string]cli.CliFunc{
	"account":       cli.Account,
	"dircache":      cli.DirCache,
	"listbucket":    cli.ListBucket,
	"alilistbucket": cli.AliListBucket,
	"prefop":        cli.Prefop,
	"stat":          cli.Stat,
	"delete":        cli.Delete,
	"move":          cli.Move,
	"copy":          cli.Copy,
	"chgm":          cli.Chgm,
	"fetch":         cli.Fetch,
	"prefetch":      cli.Prefetch,
	"batchstat":     cli.BatchStat,
	"batchdelete":   cli.BatchDelete,
	"batchchgm":     cli.BatchChgm,
	"batchrename":   cli.BatchRename,
	"batchcopy":     cli.BatchCopy,
	"batchmove":     cli.BatchMove,
	"checkqrsync":   cli.CheckQrsync,
	"fput":          cli.FormPut,
	"qupload":       cli.QiniuUpload,
	"qdownload":     cli.QiniuDownload,
	"rput":          cli.ResumablePut,
	"b64encode":     cli.Base64Encode,
	"b64decode":     cli.Base64Decode,
	"urlencode":     cli.Urlencode,
	"urldecode":     cli.Urldecode,
	"ts2d":          cli.Timestamp2Date,
	"tns2d":         cli.TimestampNano2Date,
	"tms2d":         cli.TimestampMilli2Date,
	"d2ts":          cli.Date2Timestamp,
	"ip":            cli.IpQuery,
	"qetag":         cli.Qetag,
	"help":          cli.Help,
	"unzip":         cli.Unzip,
	"privateurl":    cli.PrivateUrl,
	"saveas":        cli.Saveas,
	"reqid":         cli.ReqId,
	"m3u8delete":    cli.M3u8Delete,
	"buckets":       cli.GetBuckets,
	"domains":       cli.GetDomainsOfBucket,
}

func main() {
	args := os.Args
	argc := len(args)
	log.SetOutputLevel(log.Linfo)
	if argc > 1 {
		cmd := ""
		params := []string{}
		option := args[1]
		if option == "-d" {
			if argc > 2 {
				cmd = args[2]
				if argc > 3 {
					params = args[3:]
				}
			}
			log.SetOutputLevel(log.Ldebug)
		} else if option == "-v" {
			cli.Version()
			return
		} else if option == "-h" {
			cli.Help("help")
			return
		} else {
			cmd = args[1]
			if argc > 2 {
				params = args[2:]
			}
		}
		if cmd == "" {
			fmt.Println("Error: no subcommand specified")
			return
		}
		hit := false
		for cmdName, cliFunc := range supportedCmds {
			if cmdName == cmd {
				cliFunc(cmd, params...)
				hit = true
				break
			}
		}
		if !hit {
			fmt.Println(fmt.Sprintf("Error: unknown cmd `%s'", cmd))
		}
	} else {
		fmt.Println("Use help or help [cmd1 [cmd2 [cmd3 ...]]] to see supported commands.")
	}
}
<commit_msg>Fix log output problem.<commit_after>package main

import (
	"cli"
	"fmt"
	"github.com/qiniu/log"
	"os"
)

var debugMode = false

var supportedCmds = map[string]cli.CliFunc{
	"account":       cli.Account,
	"dircache":      cli.DirCache,
	"listbucket":    cli.ListBucket,
	"alilistbucket": cli.AliListBucket,
	"prefop":        cli.Prefop,
	"stat":          cli.Stat,
	"delete":        cli.Delete,
	"move":          cli.Move,
	"copy":          cli.Copy,
	"chgm":          cli.Chgm,
	"fetch":         cli.Fetch,
	"prefetch":      cli.Prefetch,
	"batchstat":     cli.BatchStat,
	"batchdelete":   cli.BatchDelete,
	"batchchgm":     cli.BatchChgm,
	"batchrename":   cli.BatchRename,
	"batchcopy":     cli.BatchCopy,
	"batchmove":     cli.BatchMove,
	"checkqrsync":   cli.CheckQrsync,
	"fput":          cli.FormPut,
	"qupload":       cli.QiniuUpload,
	"qdownload":     cli.QiniuDownload,
	"rput":          cli.ResumablePut,
	"b64encode":     cli.Base64Encode,
	"b64decode":     cli.Base64Decode,
	"urlencode":     cli.Urlencode,
	"urldecode":     cli.Urldecode,
	"ts2d":          cli.Timestamp2Date,
	"tns2d":         cli.TimestampNano2Date,
	"tms2d":         cli.TimestampMilli2Date,
	"d2ts":          cli.Date2Timestamp,
	"ip":            cli.IpQuery,
	"qetag":         cli.Qetag,
	"help":          cli.Help,
	"unzip":         cli.Unzip,
	"privateurl":    cli.PrivateUrl,
	"saveas":        cli.Saveas,
	"reqid":         cli.ReqId,
	"m3u8delete":    cli.M3u8Delete,
	"buckets":       cli.GetBuckets,
	"domains":       cli.GetDomainsOfBucket,
}

func main() {
	args := os.Args
	argc := len(args)
	log.SetOutputLevel(log.Linfo)
	log.SetOutput(os.Stdout)
	if argc > 1 {
		cmd := ""
		params := []string{}
		option := args[1]
		if option == "-d" {
			if argc > 2 {
				cmd = args[2]
				if argc > 3 {
					params = args[3:]
				}
			}
			log.SetOutputLevel(log.Ldebug)
		} else if option == "-v" {
			cli.Version()
			return
		} else if option == "-h" {
			cli.Help("help")
			return
		} else {
			cmd = args[1]
			if argc > 2 {
				params = args[2:]
			}
		}
		if cmd == "" {
			fmt.Println("Error: no subcommand specified")
			return
		}
		hit := false
		for cmdName, cliFunc := range supportedCmds {
			if cmdName == cmd {
				cliFunc(cmd, params...)
				hit = true
				break
			}
		}
		if !hit {
			fmt.Println(fmt.Sprintf("Error: unknown cmd `%s'", cmd))
		}
	} else {
		fmt.Println("Use help or help [cmd1 [cmd2 [cmd3 ...]]] to see supported commands.")
	}
}
<|endoftext|>


<commit_before>package webservice

import (
	restful "github.com/emicklei/go-restful"
	"github.com/wtsi-hgi/hgi-web/go-backend/model"
	"github.com/wtsi-hgi/hgi-web/go-backend/application"
	"net/http"
//	"log"
)

func Project() *restful.WebService {
	ws := new(restful.WebService)

	ws.Path("/projects").
		Consumes(restful.MIME_XML, restful.MIME_JSON).
		Produces(restful.MIME_XML, restful.MIME_JSON)

	ws.Route(ws.GET("/").To(listProjects).
		Doc("Get a list of projects").
		Writes(model.Projects{}))

	ws.Route(ws.POST("/").To(createProject).
		Doc("Create a new project").
		Reads(model.Project{}).
		Writes(model.Project{}))

	ws.Route(ws.GET("/{id}").To(getProject).
		Doc("Get the project by its id").
		Param(ws.PathParameter("id" , "the identifier for the project")).
		Writes(model.Project{}))

	ws.Route(ws.PUT("/{id}").To(putProject).
		Doc("Replace the project given by id with the given data").
		Param(ws.PathParameter("id" , "the identifier for the project")).
		Reads(model.Project{}))

	ws.Route(ws.DELETE("/{id}").To(deleteProject).
		Doc("Delete the project given by id").
		Param(ws.PathParameter("id" , "the identifier for the project")))
//		Param(ws.QueryParameter("environment" , "the scope in which the applcation node lives")).

	return ws
}

func listProjects(request *restful.Request, response *restful.Response) {
	projects, err := application.SharedLogic.AllProjects()
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}
	response.WriteEntity(projects)
}

func createProject(request *restful.Request, response *restful.Response) {

	project := new(model.Project)
	err := request.ReadEntity(&project)
	if err != nil {
		response.WriteError(http.StatusBadRequest, err)
		return
	}

	// TODO: validate?
	dbid, err := application.SharedLogic.SaveProject(project)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
	}

	response.WriteEntity(dbid)
}

func getProject(request *restful.Request, response *restful.Response) {
	id := request.PathParameter("id")
	//env := request.QueryParameter("environment")
	// response.AddHeader("X-Something","other")
	// response.WriteEntity(anApp) , uses Accept header to detect XML/JSON
	// response.WriterError(http.StatusInternal
	project, err := application.SharedLogic.GetProject(id)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	response.WriteEntity(project)
}

func putProject(request *restful.Request, response *restful.Response) {

}

func deleteProject(request *restful.Request, response *restful.Response) {

}

<commit_msg>Changed CORS policy to allow all hosts as origin for RESTful project backend.<commit_after>package webservice

import (
	restful "github.com/emicklei/go-restful"
	"github.com/wtsi-hgi/hgi-web/go-backend/model"
	"github.com/wtsi-hgi/hgi-web/go-backend/application"
	"net/http"
//	"log"
)

func Project() *restful.WebService {
	ws := new(restful.WebService)

	ws.Path("/projects").
		Consumes(restful.MIME_XML, restful.MIME_JSON).
		Produces(restful.MIME_XML, restful.MIME_JSON)

	ws.Route(ws.GET("/").To(listProjects).
		Doc("Get a list of projects").
		Writes(model.Projects{}))

	ws.Route(ws.POST("/").To(createProject).
		Doc("Create a new project").
		Reads(model.Project{}).
		Writes(model.Project{}))

	ws.Route(ws.GET("/{id}").To(getProject).
		Doc("Get the project by its id").
		Param(ws.PathParameter("id" , "the identifier for the project")).
		Writes(model.Project{}))

	ws.Route(ws.PUT("/{id}").To(putProject).
		Doc("Replace the project given by id with the given data").
		Param(ws.PathParameter("id" , "the identifier for the project")).
		Reads(model.Project{}))

	ws.Route(ws.DELETE("/{id}").To(deleteProject).
		Doc("Delete the project given by id").
		Param(ws.PathParameter("id" , "the identifier for the project")))
//		Param(ws.QueryParameter("environment" , "the scope in which the applcation node lives")).

	return ws
}

func addCommonHeaders(response *restful.Response) {
	response.AddHeader("Access-Control-Allow-Origin", "*")
	response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
}

func listProjects(request *restful.Request, response *restful.Response) {
	projects, err := application.SharedLogic.AllProjects()
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}
	addCommonHeaders(response)
	response.WriteEntity(projects)
}

func createProject(request *restful.Request, response *restful.Response) {

	project := new(model.Project)
	err := request.ReadEntity(&project)
	if err != nil {
		response.WriteError(http.StatusBadRequest, err)
		return
	}

	// TODO: validate?
	dbid, err := application.SharedLogic.SaveProject(project)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
	}

	addCommonHeaders(response)
	response.WriteEntity(dbid)
}

func getProject(request *restful.Request, response *restful.Response) {
	id := request.PathParameter("id")
	//env := request.QueryParameter("environment")
	// response.AddHeader("X-Something","other")
	// response.WriteEntity(anApp) , uses Accept header to detect XML/JSON
	// response.WriterError(http.StatusInternal
	project, err := application.SharedLogic.GetProject(id)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	addCommonHeaders(response)
	response.WriteEntity(project)
}

func putProject(request *restful.Request, response *restful.Response) {

	addCommonHeaders(response)

}

func deleteProject(request *restful.Request, response *restful.Response) {

	addCommonHeaders(response)
}

<|endoftext|>


<commit_before>package main

import (
	"encoding/base64"
	"errors"
	"fmt"
	"io"
	"os"
	"reflect"
	"regexp"
	"strconv"
	"time"

	"flag"
	"io/ioutil"
	"koding/newkite/kite"
	"koding/newkite/protocol"
	"path/filepath"
)

type Os struct{}

var port = flag.String("port", "", "port to bind itself")

func main() {
	flag.Parse()
	o := &protocol.Options{Username: "fatih", Kitename: "os", Version: "1", Port: *port}
	k := kite.New(o, new(Os))
	k.Start()
}

func (Os) ReadDirectory(r *protocol.KiteRequest, result *[]string) error {
	path := r.Args.(string)
	files, err := ReadDirectory(path)
	if err != nil {
		return err
	}

	*result = files
	return nil
}

func (Os) Glob(r *protocol.KiteRequest, result *[]string) error {
	glob := r.Args.(string)
	files, err := Glob(glob)
	if err != nil {
		return err
	}

	*result = files

	return nil
}

func (Os) ReadFile(r *protocol.KiteRequest, result *map[string]interface{}) error {
	path := r.Args.(string)
	buf, err := ReadFile(path)
	if err != nil {
		return err
	}

	*result = map[string]interface{}{"content": buf}
	return nil
}

func (Os) WriteFile(r *protocol.KiteRequest, result *string) error {
	// TODO: write an Unmarshaller from interface{} to a given struct
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}
	content, ok := params["content"].(string)
	if !ok {
		return errors.New("content argument missing")
	}
	doNotOverwrite, _ := params["doNotOverwrite"].(bool)
	appendTo, _ := params["append"].(bool)

	buf, err := base64.StdEncoding.DecodeString(content)
	if err != nil {
		return err
	}

	err = WriteFile(path, buf, doNotOverwrite, appendTo)
	if err != nil {
		return err
	}

	*result = fmt.Sprintf("content written to %s", path)
	return nil
}

func (Os) EnsureNonexistentPath(r *protocol.KiteRequest, result *string) error {
	name := r.Args.(string)
	name, err := EnsureNonexistentPath(name)
	if err != nil {
		return err
	}

	*result = name
	return nil
}

func (Os) GetInfo(r *protocol.KiteRequest, result *FileEntry) error {
	path := r.Args.(string)
	fileEntry, err := GetInfo(path)
	if err != nil {
		return err
	}

	*result = *fileEntry
	return nil
}

func (Os) SetPermissions(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}
	mode, ok := params["mode"].(int)
	if !ok {
		return errors.New("mode argument missing")
	}
	recursive, ok := params["recursive"].(bool)
	if !ok {
		return errors.New("recursive argument missing")
	}

	err := SetPermissions(path, os.FileMode(mode), recursive)
	if err != nil {
		return err
	}

	*result = true
	return nil

}

func (Os) Remove(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}

	err := Remove(path)
	if err != nil {
		return err
	}

	*result = true
	return nil
}

func (Os) Rename(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	oldPath, ok := params["oldPath"].(string)
	if !ok {
		return errors.New("oldPath argument missing")
	}

	newPath, ok := params["newPath"].(string)
	if !ok {
		return errors.New("newPath argument missing")
	}

	err := Rename(oldPath, newPath)
	if err != nil {
		return err
	}

	*result = true
	return nil
}

func (Os) CreateDirectory(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}
	recursive, ok := params["recursive"].(bool)
	if !ok {
		return errors.New("recursive argument missing")
	}

	err := CreateDirectory(path, recursive)
	if err != nil {
		return err
	}
	*result = true
	return nil
}

/****************************************
*
* Make the functions below to a seperate package
*
*****************************************/
func unmarshal(a, s interface{}) {
	t := reflect.TypeOf(s)
	if t.Kind() != reflect.Struct {
		fmt.Printf("%v type can't have attributes inspected\n", t.Kind())
		return
	}

	params := make(map[string]reflect.Type)
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		params[field.Name] = field.Type
	}

	x := reflect.TypeOf(a)
	if x.Kind() != reflect.Map {
		fmt.Printf("%v type can't have attributes inspected\n", x.Kind())
		return
	}

	for _, value := range reflect.ValueOf(a).MapKeys() {
		v := reflect.ValueOf(a).MapIndex(value)
		fmt.Println(v.Kind().String())
	}
}

func ReadDirectory(path string) ([]string, error) {
	files, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, err
	}

	ls := make([]string, len(files))
	for i, file := range files {
		ls[i] = file.Name()
	}

	return ls, nil
}

func Glob(glob string) ([]string, error) {
	files, err := filepath.Glob(glob)
	if err != nil {
		return nil, err
	}

	return files, nil
}

func ReadFile(path string) ([]byte, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	fi, err := file.Stat()
	if err != nil {
		return nil, err
	}

	if fi.Size() > 10*1024*1024 {
		return nil, fmt.Errorf("File larger than 10MiB.")
	}

	buf := make([]byte, fi.Size())
	if _, err := io.ReadFull(file, buf); err != nil {
		return nil, err
	}

	return buf, nil
}

func WriteFile(filename string, data []byte, DoNotOverwrite, Append bool) error {
	flags := os.O_RDWR | os.O_CREATE
	if DoNotOverwrite {
		flags |= os.O_EXCL
	}

	if !Append {
		flags |= os.O_TRUNC
	} else {
		flags |= os.O_APPEND
	}

	file, err := os.OpenFile(filename, flags, 0666)
	if err != nil {
		return err
	}
	defer file.Close()

	_, err = file.Write(data)
	if err != nil {
		return err
	}

	return nil
}

var suffixRegexp = regexp.MustCompile(`.((_\d+)?)(\.\w*)?$`)

func EnsureNonexistentPath(name string) (string, error) {
	index := 1
	for {
		_, err := os.Stat(name)
		if err != nil {
			if os.IsNotExist(err) {
				break
			}
			return "", err
		}

		loc := suffixRegexp.FindStringSubmatchIndex(name)
		name = name[:loc[2]] + "_" + strconv.Itoa(index) + name[loc[3]:]
		index++
	}

	return name, nil
}

func GetInfo(path string) (*FileEntry, error) {
	fi, err := os.Stat(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, errors.New("file does not exist")
		}
		return nil, err
	}

	return &FileEntry{
		Name:     fi.Name(),
		FullPath: path,
		IsDir:    fi.IsDir(),
		Size:     fi.Size(),
		Mode:     fi.Mode(),
		Time:     fi.ModTime(),
	}, nil
}

type FileEntry struct {
	Name     string      `json:"name"`
	FullPath string      `json:"fullPath"`
	IsDir    bool        `json:"isDir"`
	Size     int64       `json:"size"`
	Mode     os.FileMode `json:"mode"`
	Time     time.Time   `json:"time"`
}

func SetPermissions(name string, mode os.FileMode, recursive bool) error {
	var doChange func(name string) error

	doChange = func(name string) error {
		if err := os.Chmod(name, mode); err != nil {
			return err
		}

		if !recursive {
			return nil
		}

		fi, err := os.Stat(name)
		if err != nil {
			return err
		}

		if !fi.IsDir() {
			return nil
		}

		dir, err := os.Open(name)
		if err != nil {
			return err
		}
		defer dir.Close()

		entries, err := dir.Readdirnames(0)
		if err != nil {
			return err
		}
		var firstErr error
		for _, entry := range entries {
			err := doChange(name + "/" + entry)
			if err != nil && firstErr == nil {
				firstErr = err
			}
		}
		return firstErr
	}

	return doChange(name)
}

func Remove(path string) error {
	return os.Remove(path)
}

func Rename(oldname, newname string) error {
	return os.Rename(oldname, newname)
}

func CreateDirectory(name string, recursive bool) error {
	if recursive {
		return os.MkdirAll(name, 0755)
	}

	return os.Mkdir(name, 0755)
}
<commit_msg>Fixes<commit_after>package main

import (
	"encoding/base64"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"koding/newkite/kite"
	"koding/newkite/protocol"
	"os"
	"path"
	"path/filepath"
	"reflect"
	"regexp"
	"strconv"

	"time"
)

type Os struct{}

var port = flag.String("port", "", "port to bind itself")

func main() {
	flag.Parse()
	o := &protocol.Options{Username: "fatih", Kitename: "os-local", Version: "1", Port: *port}
	k := kite.New(o, new(Os))

	k.Start()
}

func (Os) ReadDirectory(r *protocol.KiteRequest, result *map[string]interface{}) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}

	response := make(map[string]interface{})
	files, err := ReadDirectory(path)
	if err != nil {
		return err
	}

	response["files"] = files
	*result = response
	return nil
}

func (Os) Glob(r *protocol.KiteRequest, result *[]string) error {
	params := r.Args.(map[string]interface{})
	glob, ok := params["pattern"].(string)
	if !ok {
		return errors.New("pattern argument missing")
	}

	files, err := Glob(glob)
	if err != nil {
		return err
	}

	*result = files

	return nil
}

func (Os) ReadFile(r *protocol.KiteRequest, result *map[string]interface{}) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}
	buf, err := ReadFile(path)
	if err != nil {
		return err
	}

	*result = map[string]interface{}{"content": buf}
	return nil
}

func (Os) WriteFile(r *protocol.KiteRequest, result *string) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}
	content, ok := params["content"].(string)
	if !ok {
		return errors.New("content argument missing")
	}
	doNotOverwrite, _ := params["doNotOverwrite"].(bool)
	appendTo, _ := params["append"].(bool)

	buf, err := base64.StdEncoding.DecodeString(content)
	if err != nil {
		return err
	}

	err = WriteFile(path, buf, doNotOverwrite, appendTo)
	if err != nil {
		return err
	}

	*result = fmt.Sprintf("content written to %s", path)
	return nil
}

func (Os) EnsureNonexistentPath(r *protocol.KiteRequest, result *string) error {
	name := r.Args.(string)
	name, err := EnsureNonexistentPath(name)
	if err != nil {
		return err
	}

	*result = name
	return nil
}

func (Os) GetInfo(r *protocol.KiteRequest, result *FileEntry) error {
	path := r.Args.(string)
	fileEntry, err := GetInfo(path)
	if err != nil {
		return err
	}

	*result = *fileEntry
	return nil
}

func (Os) SetPermissions(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}
	mode, ok := params["mode"].(int)
	if !ok {
		return errors.New("mode argument missing")
	}
	recursive, ok := params["recursive"].(bool)
	if !ok {
		return errors.New("recursive argument missing")
	}

	err := SetPermissions(path, os.FileMode(mode), recursive)
	if err != nil {
		return err
	}

	*result = true
	return nil

}

func (Os) Remove(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}

	err := Remove(path)
	if err != nil {
		return err
	}

	*result = true
	return nil
}

func (Os) Rename(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	oldPath, ok := params["oldPath"].(string)
	if !ok {
		return errors.New("oldPath argument missing")
	}

	newPath, ok := params["newPath"].(string)
	if !ok {
		return errors.New("newPath argument missing")
	}

	err := Rename(oldPath, newPath)
	if err != nil {
		return err
	}

	*result = true
	return nil
}

func (Os) CreateDirectory(r *protocol.KiteRequest, result *bool) error {
	params := r.Args.(map[string]interface{})
	path, ok := params["path"].(string)
	if !ok {
		return errors.New("path argument missing")
	}
	recursive, ok := params["recursive"].(bool)
	if !ok {
		return errors.New("recursive argument missing")
	}

	err := CreateDirectory(path, recursive)
	if err != nil {
		return err
	}
	*result = true
	return nil
}

/****************************************
*
* Make the functions below to a seperate package
*
*****************************************/
func unmarshal(a, s interface{}) {
	t := reflect.TypeOf(s)
	if t.Kind() != reflect.Struct {
		fmt.Printf("%v type can't have attributes inspected\n", t.Kind())
		return
	}

	params := make(map[string]reflect.Type)
	for i := 0; i < t.NumField(); i++ {
		field := t.Field(i)
		params[field.Name] = field.Type
	}

	x := reflect.TypeOf(a)
	if x.Kind() != reflect.Map {
		fmt.Printf("%v type can't have attributes inspected\n", x.Kind())
		return
	}

	for _, value := range reflect.ValueOf(a).MapKeys() {
		v := reflect.ValueOf(a).MapIndex(value)
		fmt.Println(v.Kind().String())
	}
}

func ReadDirectory(p string) ([]FileEntry, error) {
	files, err := ioutil.ReadDir(p)
	if err != nil {
		return nil, err
	}

	ls := make([]FileEntry, len(files))
	for i, info := range files {
		ls[i] = makeFileEntry(path.Join(p, info.Name()), info)
	}

	return ls, nil
}

func Glob(glob string) ([]string, error) {
	files, err := filepath.Glob(glob)
	if err != nil {
		return nil, err
	}

	return files, nil
}

func ReadFile(path string) ([]byte, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	fi, err := file.Stat()
	if err != nil {
		return nil, err
	}

	if fi.Size() > 10*1024*1024 {
		return nil, fmt.Errorf("File larger than 10MiB.")
	}

	buf := make([]byte, fi.Size())
	if _, err := io.ReadFull(file, buf); err != nil {
		return nil, err
	}

	return buf, nil
}

func WriteFile(filename string, data []byte, DoNotOverwrite, Append bool) error {
	flags := os.O_RDWR | os.O_CREATE
	if DoNotOverwrite {
		flags |= os.O_EXCL
	}

	if !Append {
		flags |= os.O_TRUNC
	} else {
		flags |= os.O_APPEND
	}

	file, err := os.OpenFile(filename, flags, 0666)
	if err != nil {
		return err
	}
	defer file.Close()

	_, err = file.Write(data)
	if err != nil {
		return err
	}

	return nil
}

var suffixRegexp = regexp.MustCompile(`.((_\d+)?)(\.\w*)?$`)

func EnsureNonexistentPath(name string) (string, error) {
	index := 1
	for {
		_, err := os.Stat(name)
		if err != nil {
			if os.IsNotExist(err) {
				break
			}
			return "", err
		}

		loc := suffixRegexp.FindStringSubmatchIndex(name)
		name = name[:loc[2]] + "_" + strconv.Itoa(index) + name[loc[3]:]
		index++
	}

	return name, nil
}

func GetInfo(path string) (*FileEntry, error) {
	fi, err := os.Stat(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, errors.New("file does not exist")
		}
		return nil, err
	}

	fileEntry := makeFileEntry(path, fi)

	return &fileEntry, nil
}

func makeFileEntry(fullPath string, fi os.FileInfo) FileEntry {
	entry := FileEntry{
		Name:     fi.Name(),
		FullPath: fullPath,
		IsDir:    fi.IsDir(),
		Size:     fi.Size(),
		Mode:     fi.Mode(),
		Time:     fi.ModTime(),
	}

	if fi.Mode()&os.ModeSymlink != 0 {
		symlinkInfo, err := os.Stat(path.Dir(fullPath) + "/" + fi.Name())
		if err != nil {
			entry.IsBroken = true
			return entry
		}
		entry.IsDir = symlinkInfo.IsDir()
		entry.Size = symlinkInfo.Size()
		entry.Mode = symlinkInfo.Mode()
		entry.Time = symlinkInfo.ModTime()
	}

	return entry
}

type FileEntry struct {
	Name     string      `json:"name"`
	FullPath string      `json:"fullPath"`
	IsDir    bool        `json:"isDir"`
	Size     int64       `json:"size"`
	Mode     os.FileMode `json:"mode"`
	Time     time.Time   `json:"time"`
	IsBroken bool        `json:"isBroken"`
	Readable bool        `json:"readable"`
	Writable bool        `json:"writable"`
}

func SetPermissions(name string, mode os.FileMode, recursive bool) error {
	var doChange func(name string) error

	doChange = func(name string) error {
		if err := os.Chmod(name, mode); err != nil {
			return err
		}

		if !recursive {
			return nil
		}

		fi, err := os.Stat(name)
		if err != nil {
			return err
		}

		if !fi.IsDir() {
			return nil
		}

		dir, err := os.Open(name)
		if err != nil {
			return err
		}
		defer dir.Close()

		entries, err := dir.Readdirnames(0)
		if err != nil {
			return err
		}
		var firstErr error
		for _, entry := range entries {
			err := doChange(name + "/" + entry)
			if err != nil && firstErr == nil {
				firstErr = err
			}
		}
		return firstErr
	}

	return doChange(name)
}

func Remove(path string) error {
	return os.Remove(path)
}

func Rename(oldname, newname string) error {
	return os.Rename(oldname, newname)
}

func CreateDirectory(name string, recursive bool) error {
	if recursive {
		return os.MkdirAll(name, 0755)
	}

	return os.Mkdir(name, 0755)
}

// https://groups.google.com/forum/#!topic/golang-nuts/7tn9vSEe0ww
// func IsReadable(info os.FileInfo) bool {
//
// 	fm := info.FileMode()
// 	if fm&(1<<2) != 0 {
// 		// yes
// 	} else if (fm & (1 << 5)) && (os.Getegid() ==
// 		int(info.Sys().(syscall.Stat_t).Gid)) {
// 		// yes
// 	} else if (fm & (1 << 8)) && (os.Geteuid() ==
// 		int(info.Sys().(syscall.Stat_t).Uid)) {
// 		//yes
// 	}
//
// }
<|endoftext|>


<commit_before><commit_msg>remove '-' in test_1 plugin help sample<commit_after><|endoftext|>


<commit_before>package llb

import (
	"strings"

	"github.com/moby/buildkit/solver-next"
	"github.com/moby/buildkit/solver/pb"
	"github.com/moby/buildkit/source"
	digest "github.com/opencontainers/go-digest"
	"github.com/pkg/errors"
)

type vertex struct {
	sys     interface{}
	options solver.VertexOptions
	inputs  []solver.Edge
	digest  digest.Digest
	name    string
}

func (v *vertex) Digest() digest.Digest {
	return v.digest
}

func (v *vertex) Sys() interface{} {
	return v.sys
}

func (v *vertex) Options() solver.VertexOptions {
	return v.options
}

func (v *vertex) Inputs() []solver.Edge {
	return v.inputs
}

func (v *vertex) Name() string {
	return v.name
}

func Load(def *pb.Definition) (solver.Edge, error) {
	return loadLLB(def, func(dgst digest.Digest, pbOp *pb.Op, load func(digest.Digest) (solver.Vertex, error)) (solver.Vertex, error) {
		opMetadata := def.Metadata[dgst]
		vtx, err := newVertex(dgst, pbOp, &opMetadata, load)
		if err != nil {
			return nil, err
		}
		return vtx, nil
	})
}

func newVertex(dgst digest.Digest, op *pb.Op, opMeta *pb.OpMetadata, load func(digest.Digest) (solver.Vertex, error)) (*vertex, error) {
	opt := solver.VertexOptions{}
	if opMeta != nil {
		opt.IgnoreCache = opMeta.IgnoreCache
		opt.Description = opMeta.Description
	}
	vtx := &vertex{sys: op.Op, options: opt, digest: dgst, name: llbOpName(op)}
	for _, in := range op.Inputs {
		sub, err := load(in.Digest)
		if err != nil {
			return nil, err
		}
		vtx.inputs = append(vtx.inputs, solver.Edge{Index: solver.Index(in.Index), Vertex: sub})
	}
	return vtx, nil
}

// loadLLB loads LLB.
// fn is executed sequentially.
func loadLLB(def *pb.Definition, fn func(digest.Digest, *pb.Op, func(digest.Digest) (solver.Vertex, error)) (solver.Vertex, error)) (solver.Edge, error) {
	if len(def.Def) == 0 {
		return solver.Edge{}, errors.New("invalid empty definition")
	}

	allOps := make(map[digest.Digest]*pb.Op)

	var dgst digest.Digest

	for _, dt := range def.Def {
		var op pb.Op
		if err := (&op).Unmarshal(dt); err != nil {
			return solver.Edge{}, errors.Wrap(err, "failed to parse llb proto op")
		}
		dgst = digest.FromBytes(dt)
		allOps[dgst] = &op
	}

	lastOp := allOps[dgst]
	delete(allOps, dgst)
	dgst = lastOp.Inputs[0].Digest

	cache := make(map[digest.Digest]solver.Vertex)

	var rec func(dgst digest.Digest) (solver.Vertex, error)
	rec = func(dgst digest.Digest) (solver.Vertex, error) {
		if v, ok := cache[dgst]; ok {
			return v, nil
		}
		v, err := fn(dgst, allOps[dgst], rec)
		if err != nil {
			return nil, err
		}
		cache[dgst] = v
		return v, nil
	}

	v, err := rec(dgst)
	if err != nil {
		return solver.Edge{}, err
	}
	return solver.Edge{Vertex: v, Index: solver.Index(lastOp.Inputs[0].Index)}, nil
}

func llbOpName(op *pb.Op) string {
	switch op := op.Op.(type) {
	case *pb.Op_Source:
		if id, err := source.FromLLB(op); err == nil {
			if id, ok := id.(*source.LocalIdentifier); ok {
				if len(id.IncludePatterns) == 1 {
					return op.Source.Identifier + " (" + id.IncludePatterns[0] + ")"
				}
			}
		}
		return op.Source.Identifier
	case *pb.Op_Exec:
		return strings.Join(op.Exec.Meta.Args, " ")
	case *pb.Op_Build:
		return "build"
	default:
		return "unknown"
	}
}
<commit_msg>solver: allow setting cache source on llb load<commit_after>package llb

import (
	"strings"

	"github.com/moby/buildkit/solver-next"
	"github.com/moby/buildkit/solver/pb"
	"github.com/moby/buildkit/source"
	digest "github.com/opencontainers/go-digest"
	"github.com/pkg/errors"
)

type vertex struct {
	sys     interface{}
	options solver.VertexOptions
	inputs  []solver.Edge
	digest  digest.Digest
	name    string
}

func (v *vertex) Digest() digest.Digest {
	return v.digest
}

func (v *vertex) Sys() interface{} {
	return v.sys
}

func (v *vertex) Options() solver.VertexOptions {
	return v.options
}

func (v *vertex) Inputs() []solver.Edge {
	return v.inputs
}

func (v *vertex) Name() string {
	return v.name
}

type LoadOpt func(*solver.VertexOptions)

func WithCacheSource(cm solver.CacheManager) LoadOpt {
	return func(opt *solver.VertexOptions) {
		opt.CacheSource = cm
	}
}

func Load(def *pb.Definition, opts ...LoadOpt) (solver.Edge, error) {
	return loadLLB(def, func(dgst digest.Digest, pbOp *pb.Op, load func(digest.Digest) (solver.Vertex, error)) (solver.Vertex, error) {
		opMetadata := def.Metadata[dgst]
		vtx, err := newVertex(dgst, pbOp, &opMetadata, load, opts...)
		if err != nil {
			return nil, err
		}
		return vtx, nil
	})
}

func newVertex(dgst digest.Digest, op *pb.Op, opMeta *pb.OpMetadata, load func(digest.Digest) (solver.Vertex, error), opts ...LoadOpt) (*vertex, error) {
	opt := solver.VertexOptions{}
	if opMeta != nil {
		opt.IgnoreCache = opMeta.IgnoreCache
		opt.Description = opMeta.Description
	}
	for _, fn := range opts {
		fn(&opt)
	}
	vtx := &vertex{sys: op.Op, options: opt, digest: dgst, name: llbOpName(op)}
	for _, in := range op.Inputs {
		sub, err := load(in.Digest)
		if err != nil {
			return nil, err
		}
		vtx.inputs = append(vtx.inputs, solver.Edge{Index: solver.Index(in.Index), Vertex: sub})
	}
	return vtx, nil
}

// loadLLB loads LLB.
// fn is executed sequentially.
func loadLLB(def *pb.Definition, fn func(digest.Digest, *pb.Op, func(digest.Digest) (solver.Vertex, error)) (solver.Vertex, error)) (solver.Edge, error) {
	if len(def.Def) == 0 {
		return solver.Edge{}, errors.New("invalid empty definition")
	}

	allOps := make(map[digest.Digest]*pb.Op)

	var dgst digest.Digest

	for _, dt := range def.Def {
		var op pb.Op
		if err := (&op).Unmarshal(dt); err != nil {
			return solver.Edge{}, errors.Wrap(err, "failed to parse llb proto op")
		}
		dgst = digest.FromBytes(dt)
		allOps[dgst] = &op
	}

	lastOp := allOps[dgst]
	delete(allOps, dgst)
	dgst = lastOp.Inputs[0].Digest

	cache := make(map[digest.Digest]solver.Vertex)

	var rec func(dgst digest.Digest) (solver.Vertex, error)
	rec = func(dgst digest.Digest) (solver.Vertex, error) {
		if v, ok := cache[dgst]; ok {
			return v, nil
		}
		v, err := fn(dgst, allOps[dgst], rec)
		if err != nil {
			return nil, err
		}
		cache[dgst] = v
		return v, nil
	}

	v, err := rec(dgst)
	if err != nil {
		return solver.Edge{}, err
	}
	return solver.Edge{Vertex: v, Index: solver.Index(lastOp.Inputs[0].Index)}, nil
}

func llbOpName(op *pb.Op) string {
	switch op := op.Op.(type) {
	case *pb.Op_Source:
		if id, err := source.FromLLB(op); err == nil {
			if id, ok := id.(*source.LocalIdentifier); ok {
				if len(id.IncludePatterns) == 1 {
					return op.Source.Identifier + " (" + id.IncludePatterns[0] + ")"
				}
			}
		}
		return op.Source.Identifier
	case *pb.Op_Exec:
		return strings.Join(op.Exec.Meta.Args, " ")
	case *pb.Op_Build:
		return "build"
	default:
		return "unknown"
	}
}
<|endoftext|>


<commit_before>// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license

package irc

import "github.com/DanielOaks/girc-go/ircmsg"

// Command represents a command accepted from a client.
type Command struct {
	handler           func(server *Server, client *Client, msg ircmsg.IrcMessage) bool
	oper              bool
	usablePreReg      bool
	leaveClientActive bool // if true, leaves the client active time alone. reversed because we can't default a struct element to True
	leaveClientIdle   bool
	minParams         int
	capabs            []string
}

// Run runs this command with the given client/message.
func (cmd *Command) Run(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
	if !client.registered && !cmd.usablePreReg {
		// command silently ignored
		return false
	}
	if cmd.oper && !client.flags[Operator] {
		client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, "Permission Denied - You're not an IRC operator")
		return false
	}
	if len(cmd.capabs) > 0 && !client.HasCapabs(cmd.capabs...) {
		client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, "Permission Denied")
		return false
	}
	if len(msg.Params) < cmd.minParams {
		client.Send(nil, server.name, ERR_NEEDMOREPARAMS, client.nick, msg.Command, "Not enough parameters")
		return false
	}
	if !cmd.leaveClientActive {
		client.Active()
	}
	if !cmd.leaveClientIdle {
		client.Touch()
	}
	exiting := cmd.handler(server, client, msg)

	// after each command, see if we can send registration to the client
	if !client.registered {
		server.tryRegister(client)
	}

	return exiting
}

// Commands holds all commands executable by a client connected to us.
var Commands = map[string]Command{
	"AMBIANCE": {
		handler:   sceneHandler,
		minParams: 2,
	},
	"AUTHENTICATE": {
		handler:      authenticateHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"AWAY": {
		handler:   awayHandler,
		minParams: 0,
	},
	"CAP": {
		handler:      capHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"DEBUG": {
		handler:   debugHandler,
		minParams: 1,
	},
	"DLINE": {
		handler:   dlineHandler,
		minParams: 1,
		oper:      true,
	},
	"HELP": {
		handler:   helpHandler,
		minParams: 0,
	},
	"INVITE": {
		handler:   inviteHandler,
		minParams: 2,
	},
	"ISON": {
		handler:   isonHandler,
		minParams: 1,
	},
	"JOIN": {
		handler:   joinHandler,
		minParams: 1,
	},
	"KICK": {
		handler:   kickHandler,
		minParams: 2,
	},
	"KILL": {
		handler:   killHandler,
		minParams: 1,
		oper:      true,
		capabs:    []string{"oper:local_kill"}, //TODO(dan): when we have S2S, this will be checked in the command handler itself
	},
	"LIST": {
		handler:   listHandler,
		minParams: 0,
	},
	"MODE": {
		handler:   modeHandler,
		minParams: 1,
	},
	"MONITOR": {
		handler:   monitorHandler,
		minParams: 1,
	},
	"MOTD": {
		handler:   motdHandler,
		minParams: 0,
	},
	"NAMES": {
		handler:   namesHandler,
		minParams: 0,
	},
	"NICK": {
		handler:      nickHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"NOTICE": {
		handler:   noticeHandler,
		minParams: 2,
	},
	"NPC": {
		handler:   npcHandler,
		minParams: 3,
	},
	"NPCA": {
		handler:   npcaHandler,
		minParams: 3,
	},
	"OPER": {
		handler:   operHandler,
		minParams: 2,
	},
	"PART": {
		handler:   partHandler,
		minParams: 1,
	},
	"PASS": {
		handler:      passHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"PING": {
		handler:           pingHandler,
		usablePreReg:      true,
		minParams:         1,
		leaveClientActive: true,
	},
	"PONG": {
		handler:           pongHandler,
		usablePreReg:      true,
		minParams:         1,
		leaveClientActive: true,
	},
	"PRIVMSG": {
		handler:   privmsgHandler,
		minParams: 2,
	},
	"SANICK": {
		handler:   sanickHandler,
		minParams: 2,
		oper:      true,
	},
	"SCENE": {
		handler:   sceneHandler,
		minParams: 2,
	},
	"QUIT": {
		handler:      quitHandler,
		usablePreReg: true,
		minParams:    0,
	},
	"REG": {
		handler:   regHandler,
		minParams: 3,
	},
	"REHASH": {
		handler:   rehashHandler,
		minParams: 0,
		oper:      true,
		capabs:    []string{"oper:rehash"},
	},
	"TIME": {
		handler:   timeHandler,
		minParams: 0,
	},
	"TOPIC": {
		handler:   topicHandler,
		minParams: 1,
	},
	"UNDLINE": {
		handler:   unDLineHandler,
		minParams: 1,
		oper:      true,
	},
	"USER": {
		handler:      userHandler,
		usablePreReg: true,
		minParams:    4,
	},
	"VERSION": {
		handler:   versionHandler,
		minParams: 0,
	},
	"WHO": {
		handler:   whoHandler,
		minParams: 0,
	},
	"WHOIS": {
		handler:   whoisHandler,
		minParams: 1,
	},
	"WHOWAS": {
		handler:   whowasHandler,
		minParams: 1,
	},
}
<commit_msg>commands: Use ERR_NOTREGISTERED when appropriate<commit_after>// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license

package irc

import "github.com/DanielOaks/girc-go/ircmsg"

// Command represents a command accepted from a client.
type Command struct {
	handler           func(server *Server, client *Client, msg ircmsg.IrcMessage) bool
	oper              bool
	usablePreReg      bool
	leaveClientActive bool // if true, leaves the client active time alone. reversed because we can't default a struct element to True
	leaveClientIdle   bool
	minParams         int
	capabs            []string
}

// Run runs this command with the given client/message.
func (cmd *Command) Run(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
	if !client.registered && !cmd.usablePreReg {
		client.Send(nil, server.name, ERR_NOTREGISTERED, client.nick, "You need to register before you can use that command")
		return false
	}
	if cmd.oper && !client.flags[Operator] {
		client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, "Permission Denied - You're not an IRC operator")
		return false
	}
	if len(cmd.capabs) > 0 && !client.HasCapabs(cmd.capabs...) {
		client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, "Permission Denied")
		return false
	}
	if len(msg.Params) < cmd.minParams {
		client.Send(nil, server.name, ERR_NEEDMOREPARAMS, client.nick, msg.Command, "Not enough parameters")
		return false
	}
	if !cmd.leaveClientActive {
		client.Active()
	}
	if !cmd.leaveClientIdle {
		client.Touch()
	}
	exiting := cmd.handler(server, client, msg)

	// after each command, see if we can send registration to the client
	if !client.registered {
		server.tryRegister(client)
	}

	return exiting
}

// Commands holds all commands executable by a client connected to us.
var Commands = map[string]Command{
	"AMBIANCE": {
		handler:   sceneHandler,
		minParams: 2,
	},
	"AUTHENTICATE": {
		handler:      authenticateHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"AWAY": {
		handler:   awayHandler,
		minParams: 0,
	},
	"CAP": {
		handler:      capHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"DEBUG": {
		handler:   debugHandler,
		minParams: 1,
	},
	"DLINE": {
		handler:   dlineHandler,
		minParams: 1,
		oper:      true,
	},
	"HELP": {
		handler:   helpHandler,
		minParams: 0,
	},
	"INVITE": {
		handler:   inviteHandler,
		minParams: 2,
	},
	"ISON": {
		handler:   isonHandler,
		minParams: 1,
	},
	"JOIN": {
		handler:   joinHandler,
		minParams: 1,
	},
	"KICK": {
		handler:   kickHandler,
		minParams: 2,
	},
	"KILL": {
		handler:   killHandler,
		minParams: 1,
		oper:      true,
		capabs:    []string{"oper:local_kill"}, //TODO(dan): when we have S2S, this will be checked in the command handler itself
	},
	"LIST": {
		handler:   listHandler,
		minParams: 0,
	},
	"MODE": {
		handler:   modeHandler,
		minParams: 1,
	},
	"MONITOR": {
		handler:   monitorHandler,
		minParams: 1,
	},
	"MOTD": {
		handler:   motdHandler,
		minParams: 0,
	},
	"NAMES": {
		handler:   namesHandler,
		minParams: 0,
	},
	"NICK": {
		handler:      nickHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"NOTICE": {
		handler:   noticeHandler,
		minParams: 2,
	},
	"NPC": {
		handler:   npcHandler,
		minParams: 3,
	},
	"NPCA": {
		handler:   npcaHandler,
		minParams: 3,
	},
	"OPER": {
		handler:   operHandler,
		minParams: 2,
	},
	"PART": {
		handler:   partHandler,
		minParams: 1,
	},
	"PASS": {
		handler:      passHandler,
		usablePreReg: true,
		minParams:    1,
	},
	"PING": {
		handler:           pingHandler,
		usablePreReg:      true,
		minParams:         1,
		leaveClientActive: true,
	},
	"PONG": {
		handler:           pongHandler,
		usablePreReg:      true,
		minParams:         1,
		leaveClientActive: true,
	},
	"PRIVMSG": {
		handler:   privmsgHandler,
		minParams: 2,
	},
	"SANICK": {
		handler:   sanickHandler,
		minParams: 2,
		oper:      true,
	},
	"SCENE": {
		handler:   sceneHandler,
		minParams: 2,
	},
	"QUIT": {
		handler:      quitHandler,
		usablePreReg: true,
		minParams:    0,
	},
	"REG": {
		handler:   regHandler,
		minParams: 3,
	},
	"REHASH": {
		handler:   rehashHandler,
		minParams: 0,
		oper:      true,
		capabs:    []string{"oper:rehash"},
	},
	"TIME": {
		handler:   timeHandler,
		minParams: 0,
	},
	"TOPIC": {
		handler:   topicHandler,
		minParams: 1,
	},
	"UNDLINE": {
		handler:   unDLineHandler,
		minParams: 1,
		oper:      true,
	},
	"USER": {
		handler:      userHandler,
		usablePreReg: true,
		minParams:    4,
	},
	"VERSION": {
		handler:   versionHandler,
		minParams: 0,
	},
	"WHO": {
		handler:   whoHandler,
		minParams: 0,
	},
	"WHOIS": {
		handler:   whoisHandler,
		minParams: 1,
	},
	"WHOWAS": {
		handler:   whowasHandler,
		minParams: 1,
	},
}
<|endoftext|>


<commit_before>package cluster

import (
	"context"
	"sync"
	"time"

	lxd "github.com/lxc/lxd/client"
	"github.com/lxc/lxd/lxd/db"
	"github.com/lxc/lxd/lxd/endpoints"
	"github.com/lxc/lxd/lxd/task"
	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/api"
	"github.com/lxc/lxd/shared/logger"
)

var listeners = map[string]*lxd.EventListener{}
var listenersLock sync.Mutex

// Events starts a task that continuously monitors the list of cluster nodes and
// maintains a pool of websocket connections against all of them, in order to
// get notified about events.
//
// Whenever an event is received the given callback is invoked.
func Events(endpoints *endpoints.Endpoints, cluster *db.Cluster, serverCert func() *shared.CertInfo, f func(int64, api.Event)) (task.Func, task.Schedule) {
	// Update our pool of event listeners. Since database queries are
	// blocking, we spawn the actual logic in a goroutine, to abort
	// immediately when we receive the stop signal.
	update := func(ctx context.Context) {
		ch := make(chan struct{})
		go func() {
			eventsUpdateListeners(endpoints, cluster, serverCert, f)
			ch <- struct{}{}
		}()
		select {
		case <-ch:
		case <-ctx.Done():
		}
	}

	schedule := task.Every(time.Second)

	return update, schedule
}

func eventsUpdateListeners(endpoints *endpoints.Endpoints, cluster *db.Cluster, serverCert func() *shared.CertInfo, f func(int64, api.Event)) {
	// Get the current cluster nodes.
	var nodes []db.NodeInfo
	var offlineThreshold time.Duration

	err := cluster.Transaction(func(tx *db.ClusterTx) error {
		var err error

		nodes, err = tx.GetNodes()
		if err != nil {
			return err
		}

		offlineThreshold, err = tx.GetNodeOfflineThreshold()
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		logger.Warnf("Failed to get current cluster nodes: %v", err)
		return
	}
	if len(nodes) == 1 {
		return // Either we're not clustered or this is a single-node cluster
	}

	address := endpoints.NetworkAddress()

	addresses := make([]string, len(nodes))
	for i, node := range nodes {
		addresses[i] = node.Address

		// Don't bother trying to connect to offline nodes, or to ourselves.
		if node.IsOffline(offlineThreshold) || node.Address == address {
			continue
		}

		listenersLock.Lock()
		listener, ok := listeners[node.Address]

		// The node has already a listener associated to it.
		if ok {
			// Double check that the listener is still
			// connected. If it is, just move on, other
			// we'll try to connect again.
			if listeners[node.Address].IsActive() {
				listenersLock.Unlock()
				continue
			}

			delete(listeners, node.Address)
		}
		listenersLock.Unlock()

		listener, err := eventsConnect(node.Address, endpoints.NetworkCert(), serverCert())
		if err != nil {
			logger.Warnf("Failed to get events from node %s: %v", node.Address, err)
			continue
		}
		logger.Debugf("Listening for events on node %s", node.Address)
		listener.AddHandler(nil, func(event api.Event) { f(node.ID, event) })

		listenersLock.Lock()
		listeners[node.Address] = listener
		listenersLock.Unlock()
	}

	listenersLock.Lock()
	for address, listener := range listeners {
		if !shared.StringInSlice(address, addresses) {
			listener.Disconnect()
			delete(listeners, address)
		}
	}
	listenersLock.Unlock()
}

// Establish a client connection to get events from the given node.
func eventsConnect(address string, networkCert *shared.CertInfo, serverCert *shared.CertInfo) (*lxd.EventListener, error) {
	client, err := Connect(address, networkCert, serverCert, true)
	if err != nil {
		return nil, err
	}

	// Set the project to the special wildcard in order to get notified
	// about all events across all projects.
	client = client.UseProject("*")

	return client.GetEvents()
}
<commit_msg>lxd/cluster/events: Improve logging consistency in eventsUpdateListeners<commit_after>package cluster

import (
	"context"
	"sync"
	"time"

	lxd "github.com/lxc/lxd/client"
	"github.com/lxc/lxd/lxd/db"
	"github.com/lxc/lxd/lxd/endpoints"
	"github.com/lxc/lxd/lxd/task"
	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/api"
	log "github.com/lxc/lxd/shared/log15"
	"github.com/lxc/lxd/shared/logger"
)

var listeners = map[string]*lxd.EventListener{}
var listenersLock sync.Mutex

// Events starts a task that continuously monitors the list of cluster nodes and
// maintains a pool of websocket connections against all of them, in order to
// get notified about events.
//
// Whenever an event is received the given callback is invoked.
func Events(endpoints *endpoints.Endpoints, cluster *db.Cluster, serverCert func() *shared.CertInfo, f func(int64, api.Event)) (task.Func, task.Schedule) {
	// Update our pool of event listeners. Since database queries are
	// blocking, we spawn the actual logic in a goroutine, to abort
	// immediately when we receive the stop signal.
	update := func(ctx context.Context) {
		ch := make(chan struct{})
		go func() {
			eventsUpdateListeners(endpoints, cluster, serverCert, f)
			ch <- struct{}{}
		}()
		select {
		case <-ch:
		case <-ctx.Done():
		}
	}

	schedule := task.Every(time.Second)

	return update, schedule
}

func eventsUpdateListeners(endpoints *endpoints.Endpoints, cluster *db.Cluster, serverCert func() *shared.CertInfo, f func(int64, api.Event)) {
	// Get the current cluster nodes.
	var nodes []db.NodeInfo
	var offlineThreshold time.Duration

	err := cluster.Transaction(func(tx *db.ClusterTx) error {
		var err error

		nodes, err = tx.GetNodes()
		if err != nil {
			return err
		}

		offlineThreshold, err = tx.GetNodeOfflineThreshold()
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		logger.Warn("Failed to get current cluster members", log.Ctx{"err": err})
		return
	}
	if len(nodes) == 1 {
		return // Either we're not clustered or this is a single-node cluster
	}

	address := endpoints.NetworkAddress()

	addresses := make([]string, len(nodes))
	for i, node := range nodes {
		addresses[i] = node.Address

		// Don't bother trying to connect to offline nodes, or to ourselves.
		if node.IsOffline(offlineThreshold) || node.Address == address {
			continue
		}

		listenersLock.Lock()
		listener, ok := listeners[node.Address]

		// The node has already a listener associated to it.
		if ok {
			// Double check that the listener is still
			// connected. If it is, just move on, other
			// we'll try to connect again.
			if listeners[node.Address].IsActive() {
				listenersLock.Unlock()
				continue
			}

			delete(listeners, node.Address)
		}
		listenersLock.Unlock()

		listener, err := eventsConnect(node.Address, endpoints.NetworkCert(), serverCert())
		if err != nil {
			logger.Warn("Failed to get events from member", log.Ctx{"address": node.Address, "err": err})
			continue
		}
		logger.Debug("Listening for events on member", log.Ctx{"address": node.Address})
		listener.AddHandler(nil, func(event api.Event) { f(node.ID, event) })

		listenersLock.Lock()
		listeners[node.Address] = listener
		listenersLock.Unlock()
	}

	listenersLock.Lock()
	for address, listener := range listeners {
		if !shared.StringInSlice(address, addresses) {
			listener.Disconnect()
			delete(listeners, address)
		}
	}
	listenersLock.Unlock()
}

// Establish a client connection to get events from the given node.
func eventsConnect(address string, networkCert *shared.CertInfo, serverCert *shared.CertInfo) (*lxd.EventListener, error) {
	client, err := Connect(address, networkCert, serverCert, true)
	if err != nil {
		return nil, err
	}

	// Set the project to the special wildcard in order to get notified
	// about all events across all projects.
	client = client.UseProject("*")

	return client.GetEvents()
}
<|endoftext|>