<commit_before>package start

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	_ "net/http/pprof"
	"os"
	"path/filepath"
	"strings"

	"github.com/coreos/go-systemd/daemon"
	"github.com/golang/glog"
	"github.com/spf13/cobra"

	kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"

	"github.com/openshift/origin/pkg/cmd/server/admin"
	configapi "github.com/openshift/origin/pkg/cmd/server/api"
	configapilatest "github.com/openshift/origin/pkg/cmd/server/api/latest"
	"github.com/openshift/origin/pkg/cmd/server/api/validation"
	"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
	"github.com/openshift/origin/pkg/cmd/server/etcd"
	"github.com/openshift/origin/pkg/cmd/server/kubernetes"
	"github.com/openshift/origin/pkg/cmd/server/origin"
	cmdutil "github.com/openshift/origin/pkg/cmd/util"
)

type MasterOptions struct {
	MasterArgs *MasterArgs

	WriteConfigOnly bool
	ConfigFile      string
}

const longMasterCommandDesc = `
Start an OpenShift master

This command helps you launch an OpenShift master.  Running

    $ openshift start master

will start an OpenShift master listening on all interfaces, launch an etcd server to store
persistent data, and launch the Kubernetes system components. The server will run in the
foreground until you terminate the process.

Note: starting OpenShift without passing the --master address will attempt to find the IP
address that will be visible inside running Docker containers. This is not always successful,
so if you have problems tell OpenShift what public address it will be via --master=<ip>.

You may also pass an optional argument to the start command to start OpenShift in one of the
following roles:

    $ openshift start master --nodes=<host1,host2,host3,...>

      Launches the server and control plane for OpenShift. You may pass a list of the node
      hostnames you want to use, or create nodes via the REST API or 'openshift kube'.

You may also pass --etcd=<address> to connect to an external etcd server.

You may also pass --kubeconfig=<path> to connect to an external Kubernetes cluster.
`

// NewCommandStartMaster provides a CLI handler for 'start' command
func NewCommandStartMaster() (*cobra.Command, *MasterOptions) {
	options := &MasterOptions{}

	cmd := &cobra.Command{
		Use:   "master",
		Short: "Launch OpenShift master",
		Long:  longMasterCommandDesc,
		Run: func(c *cobra.Command, args []string) {
			if err := options.Complete(); err != nil {
				fmt.Println(err.Error())
				c.Help()
				return
			}

			if err := options.Validate(args); err != nil {
				fmt.Println(err.Error())
				c.Help()
				return
			}

			if err := options.StartMaster(); err != nil {
				glog.Fatal(err)
			}
		},
	}

	flags := cmd.Flags()

	flags.BoolVar(&options.WriteConfigOnly, "write-config", false, "Indicates that the command should build the configuration from command-line arguments, write it to the location specified by --config, and exit.")
	flags.StringVar(&options.ConfigFile, "config", "", "Location of the master configuration file to run from, or write to (when used with --write-config). When running from a configuration file, all other command-line arguments are ignored.")

	options.MasterArgs = NewDefaultMasterArgs()
	// make sure that KubeConnectionArgs and MasterArgs use the same CertArgs for this command
	options.MasterArgs.KubeConnectionArgs.CertArgs = options.MasterArgs.CertArgs

	BindMasterArgs(options.MasterArgs, flags, "")
	BindListenArg(options.MasterArgs.ListenArg, flags, "")
	BindPolicyArgs(options.MasterArgs.PolicyArgs, flags, "")
	BindImageFormatArgs(options.MasterArgs.ImageFormatArgs, flags, "")
	BindKubeConnectionArgs(options.MasterArgs.KubeConnectionArgs, flags, "")
	BindCertArgs(options.MasterArgs.CertArgs, flags, "")

	return cmd, options
}

func (o MasterOptions) Validate(args []string) error {
	if len(args) != 0 {
		return errors.New("no arguments are supported for start master")
	}
	if o.WriteConfigOnly {
		if len(o.ConfigFile) == 0 {
			return errors.New("--config is required if --write-config is true")
		}
	}

	if err := o.MasterArgs.Validate(); err != nil {
		return err
	}

	return nil
}

func (o MasterOptions) Complete() error {
	nodeList := util.NewStringSet()
	// take everything toLower
	for _, s := range o.MasterArgs.NodeList {
		nodeList.Insert(strings.ToLower(s))
	}

	o.MasterArgs.NodeList = nodeList.List()

	return nil
}

// StartMaster calls RunMaster and then waits forever
func (o MasterOptions) StartMaster() error {
	if err := o.RunMaster(); err != nil {
		return err
	}

	if o.WriteConfigOnly {
		return nil
	}

	select {}

	return nil
}

// RunMaster takes the options and:
// 1.  Creates certs if needed
// 2.  Reads fully specified master config OR builds a fully specified master config from the args
// 3.  Writes the fully specified master config and exits if needed
// 4.  Starts the master based on the fully specified config
func (o MasterOptions) RunMaster() error {
	startUsingConfigFile := !o.WriteConfigOnly && (len(o.ConfigFile) > 0)
	mintCerts := o.MasterArgs.CertArgs.CreateCerts && !startUsingConfigFile
	writeBootstrapPolicy := o.MasterArgs.PolicyArgs.CreatePolicyFile && !startUsingConfigFile

	if mintCerts {
		if err := o.CreateCerts(); err != nil {
			return nil
		}
	}
	if writeBootstrapPolicy {
		if err := o.CreateBootstrapPolicy(); err != nil {
			return nil
		}
	}

	var masterConfig *configapi.MasterConfig
	var err error
	if startUsingConfigFile {
		masterConfig, err = ReadMasterConfig(o.ConfigFile)
	} else {
		masterConfig, err = o.MasterArgs.BuildSerializeableMasterConfig()
	}
	if err != nil {
		return err
	}

	if o.WriteConfigOnly {
		// Resolve relative to CWD
		cwd, err := os.Getwd()
		if err != nil {
			return err
		}
		if err := configapi.ResolveMasterConfigPaths(masterConfig, cwd); err != nil {
			return err
		}

		// Relativize to config file dir
		base, err := cmdutil.MakeAbs(filepath.Dir(o.ConfigFile), cwd)
		if err != nil {
			return err
		}
		if err := configapi.RelativizeMasterConfigPaths(masterConfig, base); err != nil {
			return err
		}

		content, err := WriteMaster(masterConfig)
		if err != nil {
			return err
		}
		if err := ioutil.WriteFile(o.ConfigFile, content, 0644); err != nil {
			return err
		}
		return nil
	}

	errs := validation.ValidateMasterConfig(masterConfig)
	if len(errs) != 0 {
		return kerrors.NewInvalid("masterConfig", "", errs)
	}

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

	return nil
}

func (o MasterOptions) CreateBootstrapPolicy() error {
	writeBootstrapPolicy := admin.CreateBootstrapPolicyFileOptions{
		File: o.MasterArgs.PolicyArgs.PolicyFile,
		MasterAuthorizationNamespace:      bootstrappolicy.DefaultMasterAuthorizationNamespace,
		OpenShiftSharedResourcesNamespace: bootstrappolicy.DefaultOpenShiftSharedResourcesNamespace,
	}

	return writeBootstrapPolicy.CreateBootstrapPolicyFile()
}

func (o MasterOptions) CreateCerts() error {
	signerName := admin.DefaultSignerName()
	hostnames, err := o.MasterArgs.GetServerCertHostnames()
	if err != nil {
		return err
	}
	mintAllCertsOptions := admin.CreateAllCertsOptions{
		CertDir:    o.MasterArgs.CertArgs.CertDir,
		SignerName: signerName,
		Hostnames:  hostnames.List(),
		NodeList:   o.MasterArgs.NodeList,
	}
	if err := mintAllCertsOptions.CreateAllCerts(); err != nil {
		return err
	}

	rootCAFile := admin.DefaultRootCAFile(o.MasterArgs.CertArgs.CertDir)
	masterAddr, err := o.MasterArgs.GetMasterAddress()
	if err != nil {
		return err
	}
	publicMasterAddr, err := o.MasterArgs.GetMasterPublicAddress()
	if err != nil {
		return err
	}
	for _, clientCertInfo := range admin.DefaultClientCerts(o.MasterArgs.CertArgs.CertDir) {
		createKubeConfigOptions := admin.CreateKubeConfigOptions{
			APIServerURL:       masterAddr.String(),
			PublicAPIServerURL: publicMasterAddr.String(),
			APIServerCAFile:    rootCAFile,
			ServerNick:         "master",

			CertFile: clientCertInfo.CertLocation.CertFile,
			KeyFile:  clientCertInfo.CertLocation.KeyFile,
			UserNick: clientCertInfo.SubDir,

			KubeConfigFile: admin.DefaultKubeConfigFilename(o.MasterArgs.CertArgs.CertDir, clientCertInfo.SubDir),
		}

		if _, err := createKubeConfigOptions.CreateKubeConfig(); err != nil {
			return err
		}
	}

	return nil
}

func ReadMasterConfig(filename string) (*configapi.MasterConfig, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	config := &configapi.MasterConfig{}

	if err := configapilatest.Codec.DecodeInto(data, config); err != nil {
		return nil, err
	}

	base, err := cmdutil.MakeAbs(filepath.Dir(filename), "")
	if err != nil {
		return nil, err
	}
	if err := configapi.ResolveMasterConfigPaths(config, base); err != nil {
		return nil, err
	}

	return config, nil
}

func StartMaster(openshiftMasterConfig *configapi.MasterConfig) error {
	glog.Infof("Starting an OpenShift master, reachable at %s (etcd: %s)", openshiftMasterConfig.ServingInfo.BindAddress, openshiftMasterConfig.EtcdClientInfo.URL)
	glog.Infof("OpenShift master public address is %s", openshiftMasterConfig.AssetConfig.MasterPublicURL)

	if openshiftMasterConfig.EtcdConfig != nil {
		etcdConfig := &etcd.Config{
			BindAddr:     openshiftMasterConfig.EtcdConfig.ServingInfo.BindAddress,
			PeerBindAddr: openshiftMasterConfig.EtcdConfig.PeerAddress,
			MasterAddr:   openshiftMasterConfig.EtcdConfig.MasterAddress,
			EtcdDir:      openshiftMasterConfig.EtcdConfig.StorageDir,
		}

		etcdConfig.Run()
	}

	if cmdutil.Env("OPENSHIFT_PROFILE", "") == "web" {
		go func() {
			glog.Infof("Starting profiling endpoint at http://127.0.0.1:6060/debug/pprof/")
			glog.Fatal(http.ListenAndServe("127.0.0.1:6060", nil))
		}()
	}

	// Allow privileged containers
	// TODO: make this configurable and not the default https://github.com/openshift/origin/issues/662
	capabilities.Initialize(capabilities.Capabilities{
		AllowPrivileged: true,
	})

	openshiftConfig, err := origin.BuildMasterConfig(*openshiftMasterConfig)
	if err != nil {
		return err
	}
	//	 must start policy caching immediately
	openshiftConfig.RunPolicyCache()

	authConfig, err := origin.BuildAuthConfig(*openshiftMasterConfig)
	if err != nil {
		return err
	}

	if openshiftMasterConfig.KubernetesMasterConfig != nil {
		glog.Infof("Static Nodes: %v", openshiftMasterConfig.KubernetesMasterConfig.StaticNodeNames)

		kubeConfig, err := kubernetes.BuildKubernetesMasterConfig(*openshiftMasterConfig, openshiftConfig.RequestContextMapper, openshiftConfig.KubeClient())
		if err != nil {
			return err
		}
		kubeConfig.EnsurePortalFlags()

		openshiftConfig.Run([]origin.APIInstaller{kubeConfig}, []origin.APIInstaller{authConfig})
		go daemon.SdNotify("READY=1")

		kubeConfig.RunScheduler()
		kubeConfig.RunReplicationController()
		kubeConfig.RunEndpointController()
		kubeConfig.RunMinionController()
		kubeConfig.RunResourceQuotaManager()

	} else {
		_, kubeConfig, err := configapi.GetKubeClient(openshiftMasterConfig.MasterClients.KubernetesKubeConfig)
		if err != nil {
			return err
		}

		proxy := &kubernetes.ProxyConfig{
			ClientConfig: kubeConfig,
		}

		openshiftConfig.Run([]origin.APIInstaller{proxy}, []origin.APIInstaller{authConfig})
		go daemon.SdNotify("READY=1")
	}

	// TODO: recording should occur in individual components
	record.StartRecording(openshiftConfig.KubeClient().Events(""))

	glog.Infof("Using images from %q", openshiftConfig.ImageFor("<component>"))

	if openshiftMasterConfig.DNSConfig != nil {
		openshiftConfig.RunDNSServer()
	}
	if openshiftMasterConfig.AssetConfig != nil {
		openshiftConfig.RunAssetServer()
	}
	openshiftConfig.RunBuildController()
	openshiftConfig.RunBuildPodController()
	openshiftConfig.RunBuildImageChangeTriggerController()
	if err := openshiftConfig.RunDeploymentController(); err != nil {
		return err
	}
	openshiftConfig.RunDeployerPodController()
	openshiftConfig.RunDeploymentConfigController()
	openshiftConfig.RunDeploymentConfigChangeController()
	openshiftConfig.RunDeploymentImageChangeTriggerController()
	openshiftConfig.RunImageImportController()
	openshiftConfig.RunProjectAuthorizationCache()

	return nil
}
<commit_msg>pass master and public-master urls to create-all-certs<commit_after>package start

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	_ "net/http/pprof"
	"os"
	"path/filepath"
	"strings"

	"github.com/coreos/go-systemd/daemon"
	"github.com/golang/glog"
	"github.com/spf13/cobra"

	kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"

	"github.com/openshift/origin/pkg/cmd/server/admin"
	configapi "github.com/openshift/origin/pkg/cmd/server/api"
	configapilatest "github.com/openshift/origin/pkg/cmd/server/api/latest"
	"github.com/openshift/origin/pkg/cmd/server/api/validation"
	"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
	"github.com/openshift/origin/pkg/cmd/server/etcd"
	"github.com/openshift/origin/pkg/cmd/server/kubernetes"
	"github.com/openshift/origin/pkg/cmd/server/origin"
	cmdutil "github.com/openshift/origin/pkg/cmd/util"
)

type MasterOptions struct {
	MasterArgs *MasterArgs

	WriteConfigOnly bool
	ConfigFile      string
}

const longMasterCommandDesc = `
Start an OpenShift master

This command helps you launch an OpenShift master.  Running

    $ openshift start master

will start an OpenShift master listening on all interfaces, launch an etcd server to store
persistent data, and launch the Kubernetes system components. The server will run in the
foreground until you terminate the process.

Note: starting OpenShift without passing the --master address will attempt to find the IP
address that will be visible inside running Docker containers. This is not always successful,
so if you have problems tell OpenShift what public address it will be via --master=<ip>.

You may also pass an optional argument to the start command to start OpenShift in one of the
following roles:

    $ openshift start master --nodes=<host1,host2,host3,...>

      Launches the server and control plane for OpenShift. You may pass a list of the node
      hostnames you want to use, or create nodes via the REST API or 'openshift kube'.

You may also pass --etcd=<address> to connect to an external etcd server.

You may also pass --kubeconfig=<path> to connect to an external Kubernetes cluster.
`

// NewCommandStartMaster provides a CLI handler for 'start' command
func NewCommandStartMaster() (*cobra.Command, *MasterOptions) {
	options := &MasterOptions{}

	cmd := &cobra.Command{
		Use:   "master",
		Short: "Launch OpenShift master",
		Long:  longMasterCommandDesc,
		Run: func(c *cobra.Command, args []string) {
			if err := options.Complete(); err != nil {
				fmt.Println(err.Error())
				c.Help()
				return
			}

			if err := options.Validate(args); err != nil {
				fmt.Println(err.Error())
				c.Help()
				return
			}

			if err := options.StartMaster(); err != nil {
				glog.Fatal(err)
			}
		},
	}

	flags := cmd.Flags()

	flags.BoolVar(&options.WriteConfigOnly, "write-config", false, "Indicates that the command should build the configuration from command-line arguments, write it to the location specified by --config, and exit.")
	flags.StringVar(&options.ConfigFile, "config", "", "Location of the master configuration file to run from, or write to (when used with --write-config). When running from a configuration file, all other command-line arguments are ignored.")

	options.MasterArgs = NewDefaultMasterArgs()
	// make sure that KubeConnectionArgs and MasterArgs use the same CertArgs for this command
	options.MasterArgs.KubeConnectionArgs.CertArgs = options.MasterArgs.CertArgs

	BindMasterArgs(options.MasterArgs, flags, "")
	BindListenArg(options.MasterArgs.ListenArg, flags, "")
	BindPolicyArgs(options.MasterArgs.PolicyArgs, flags, "")
	BindImageFormatArgs(options.MasterArgs.ImageFormatArgs, flags, "")
	BindKubeConnectionArgs(options.MasterArgs.KubeConnectionArgs, flags, "")
	BindCertArgs(options.MasterArgs.CertArgs, flags, "")

	return cmd, options
}

func (o MasterOptions) Validate(args []string) error {
	if len(args) != 0 {
		return errors.New("no arguments are supported for start master")
	}
	if o.WriteConfigOnly {
		if len(o.ConfigFile) == 0 {
			return errors.New("--config is required if --write-config is true")
		}
	}

	if err := o.MasterArgs.Validate(); err != nil {
		return err
	}

	return nil
}

func (o MasterOptions) Complete() error {
	nodeList := util.NewStringSet()
	// take everything toLower
	for _, s := range o.MasterArgs.NodeList {
		nodeList.Insert(strings.ToLower(s))
	}

	o.MasterArgs.NodeList = nodeList.List()

	return nil
}

// StartMaster calls RunMaster and then waits forever
func (o MasterOptions) StartMaster() error {
	if err := o.RunMaster(); err != nil {
		return err
	}

	if o.WriteConfigOnly {
		return nil
	}

	select {}

	return nil
}

// RunMaster takes the options and:
// 1.  Creates certs if needed
// 2.  Reads fully specified master config OR builds a fully specified master config from the args
// 3.  Writes the fully specified master config and exits if needed
// 4.  Starts the master based on the fully specified config
func (o MasterOptions) RunMaster() error {
	startUsingConfigFile := !o.WriteConfigOnly && (len(o.ConfigFile) > 0)
	mintCerts := o.MasterArgs.CertArgs.CreateCerts && !startUsingConfigFile
	writeBootstrapPolicy := o.MasterArgs.PolicyArgs.CreatePolicyFile && !startUsingConfigFile

	if mintCerts {
		if err := o.CreateCerts(); err != nil {
			return nil
		}
	}
	if writeBootstrapPolicy {
		if err := o.CreateBootstrapPolicy(); err != nil {
			return nil
		}
	}

	var masterConfig *configapi.MasterConfig
	var err error
	if startUsingConfigFile {
		masterConfig, err = ReadMasterConfig(o.ConfigFile)
	} else {
		masterConfig, err = o.MasterArgs.BuildSerializeableMasterConfig()
	}
	if err != nil {
		return err
	}

	if o.WriteConfigOnly {
		// Resolve relative to CWD
		cwd, err := os.Getwd()
		if err != nil {
			return err
		}
		if err := configapi.ResolveMasterConfigPaths(masterConfig, cwd); err != nil {
			return err
		}

		// Relativize to config file dir
		base, err := cmdutil.MakeAbs(filepath.Dir(o.ConfigFile), cwd)
		if err != nil {
			return err
		}
		if err := configapi.RelativizeMasterConfigPaths(masterConfig, base); err != nil {
			return err
		}

		content, err := WriteMaster(masterConfig)
		if err != nil {
			return err
		}
		if err := ioutil.WriteFile(o.ConfigFile, content, 0644); err != nil {
			return err
		}
		return nil
	}

	errs := validation.ValidateMasterConfig(masterConfig)
	if len(errs) != 0 {
		return kerrors.NewInvalid("masterConfig", "", errs)
	}

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

	return nil
}

func (o MasterOptions) CreateBootstrapPolicy() error {
	writeBootstrapPolicy := admin.CreateBootstrapPolicyFileOptions{
		File: o.MasterArgs.PolicyArgs.PolicyFile,
		MasterAuthorizationNamespace:      bootstrappolicy.DefaultMasterAuthorizationNamespace,
		OpenShiftSharedResourcesNamespace: bootstrappolicy.DefaultOpenShiftSharedResourcesNamespace,
	}

	return writeBootstrapPolicy.CreateBootstrapPolicyFile()
}

func (o MasterOptions) CreateCerts() error {
	masterAddr, err := o.MasterArgs.GetMasterAddress()
	if err != nil {
		return err
	}
	publicMasterAddr, err := o.MasterArgs.GetMasterPublicAddress()
	if err != nil {
		return err
	}

	signerName := admin.DefaultSignerName()
	hostnames, err := o.MasterArgs.GetServerCertHostnames()
	if err != nil {
		return err
	}
	mintAllCertsOptions := admin.CreateAllCertsOptions{
		CertDir:            o.MasterArgs.CertArgs.CertDir,
		SignerName:         signerName,
		Hostnames:          hostnames.List(),
		NodeList:           o.MasterArgs.NodeList,
		APIServerURL:       masterAddr.String(),
		PublicAPIServerURL: publicMasterAddr.String(),
	}
	if err := mintAllCertsOptions.CreateAllCerts(); err != nil {
		return err
	}

	rootCAFile := admin.DefaultRootCAFile(o.MasterArgs.CertArgs.CertDir)
	for _, clientCertInfo := range admin.DefaultClientCerts(o.MasterArgs.CertArgs.CertDir) {
		createKubeConfigOptions := admin.CreateKubeConfigOptions{
			APIServerURL:       masterAddr.String(),
			PublicAPIServerURL: publicMasterAddr.String(),
			APIServerCAFile:    rootCAFile,
			ServerNick:         "master",

			CertFile: clientCertInfo.CertLocation.CertFile,
			KeyFile:  clientCertInfo.CertLocation.KeyFile,
			UserNick: clientCertInfo.SubDir,

			KubeConfigFile: admin.DefaultKubeConfigFilename(o.MasterArgs.CertArgs.CertDir, clientCertInfo.SubDir),
		}

		if _, err := createKubeConfigOptions.CreateKubeConfig(); err != nil {
			return err
		}
	}

	return nil
}

func ReadMasterConfig(filename string) (*configapi.MasterConfig, error) {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	config := &configapi.MasterConfig{}

	if err := configapilatest.Codec.DecodeInto(data, config); err != nil {
		return nil, err
	}

	base, err := cmdutil.MakeAbs(filepath.Dir(filename), "")
	if err != nil {
		return nil, err
	}
	if err := configapi.ResolveMasterConfigPaths(config, base); err != nil {
		return nil, err
	}

	return config, nil
}

func StartMaster(openshiftMasterConfig *configapi.MasterConfig) error {
	glog.Infof("Starting an OpenShift master, reachable at %s (etcd: %s)", openshiftMasterConfig.ServingInfo.BindAddress, openshiftMasterConfig.EtcdClientInfo.URL)
	glog.Infof("OpenShift master public address is %s", openshiftMasterConfig.AssetConfig.MasterPublicURL)

	if openshiftMasterConfig.EtcdConfig != nil {
		etcdConfig := &etcd.Config{
			BindAddr:     openshiftMasterConfig.EtcdConfig.ServingInfo.BindAddress,
			PeerBindAddr: openshiftMasterConfig.EtcdConfig.PeerAddress,
			MasterAddr:   openshiftMasterConfig.EtcdConfig.MasterAddress,
			EtcdDir:      openshiftMasterConfig.EtcdConfig.StorageDir,
		}

		etcdConfig.Run()
	}

	if cmdutil.Env("OPENSHIFT_PROFILE", "") == "web" {
		go func() {
			glog.Infof("Starting profiling endpoint at http://127.0.0.1:6060/debug/pprof/")
			glog.Fatal(http.ListenAndServe("127.0.0.1:6060", nil))
		}()
	}

	// Allow privileged containers
	// TODO: make this configurable and not the default https://github.com/openshift/origin/issues/662
	capabilities.Initialize(capabilities.Capabilities{
		AllowPrivileged: true,
	})

	openshiftConfig, err := origin.BuildMasterConfig(*openshiftMasterConfig)
	if err != nil {
		return err
	}
	//	 must start policy caching immediately
	openshiftConfig.RunPolicyCache()

	authConfig, err := origin.BuildAuthConfig(*openshiftMasterConfig)
	if err != nil {
		return err
	}

	if openshiftMasterConfig.KubernetesMasterConfig != nil {
		glog.Infof("Static Nodes: %v", openshiftMasterConfig.KubernetesMasterConfig.StaticNodeNames)

		kubeConfig, err := kubernetes.BuildKubernetesMasterConfig(*openshiftMasterConfig, openshiftConfig.RequestContextMapper, openshiftConfig.KubeClient())
		if err != nil {
			return err
		}
		kubeConfig.EnsurePortalFlags()

		openshiftConfig.Run([]origin.APIInstaller{kubeConfig}, []origin.APIInstaller{authConfig})
		go daemon.SdNotify("READY=1")

		kubeConfig.RunScheduler()
		kubeConfig.RunReplicationController()
		kubeConfig.RunEndpointController()
		kubeConfig.RunMinionController()
		kubeConfig.RunResourceQuotaManager()

	} else {
		_, kubeConfig, err := configapi.GetKubeClient(openshiftMasterConfig.MasterClients.KubernetesKubeConfig)
		if err != nil {
			return err
		}

		proxy := &kubernetes.ProxyConfig{
			ClientConfig: kubeConfig,
		}

		openshiftConfig.Run([]origin.APIInstaller{proxy}, []origin.APIInstaller{authConfig})
		go daemon.SdNotify("READY=1")
	}

	// TODO: recording should occur in individual components
	record.StartRecording(openshiftConfig.KubeClient().Events(""))

	glog.Infof("Using images from %q", openshiftConfig.ImageFor("<component>"))

	if openshiftMasterConfig.DNSConfig != nil {
		openshiftConfig.RunDNSServer()
	}
	if openshiftMasterConfig.AssetConfig != nil {
		openshiftConfig.RunAssetServer()
	}
	openshiftConfig.RunBuildController()
	openshiftConfig.RunBuildPodController()
	openshiftConfig.RunBuildImageChangeTriggerController()
	if err := openshiftConfig.RunDeploymentController(); err != nil {
		return err
	}
	openshiftConfig.RunDeployerPodController()
	openshiftConfig.RunDeploymentConfigController()
	openshiftConfig.RunDeploymentConfigChangeController()
	openshiftConfig.RunDeploymentImageChangeTriggerController()
	openshiftConfig.RunImageImportController()
	openshiftConfig.RunProjectAuthorizationCache()

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2018 The gVisor 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.

//go:build amd64
// +build amd64

package kvm

import (
	"gvisor.dev/gvisor/pkg/cpuid"
	"gvisor.dev/gvisor/pkg/ring0"
)

// userRegs represents KVM user registers.
//
// This mirrors kvm_regs.
type userRegs struct {
	RAX    uint64
	RBX    uint64
	RCX    uint64
	RDX    uint64
	RSI    uint64
	RDI    uint64
	RSP    uint64
	RBP    uint64
	R8     uint64
	R9     uint64
	R10    uint64
	R11    uint64
	R12    uint64
	R13    uint64
	R14    uint64
	R15    uint64
	RIP    uint64
	RFLAGS uint64
}

// systemRegs represents KVM system registers.
//
// This mirrors kvm_sregs.
type systemRegs struct {
	CS              segment
	DS              segment
	ES              segment
	FS              segment
	GS              segment
	SS              segment
	TR              segment
	LDT             segment
	GDT             descriptor
	IDT             descriptor
	CR0             uint64
	CR2             uint64
	CR3             uint64
	CR4             uint64
	CR8             uint64
	EFER            uint64
	apicBase        uint64
	interruptBitmap [(_KVM_NR_INTERRUPTS + 63) / 64]uint64
}

// segment is the expanded form of a segment register.
//
// This mirrors kvm_segment.
type segment struct {
	base     uint64
	limit    uint32
	selector uint16
	typ      uint8
	present  uint8
	DPL      uint8
	DB       uint8
	S        uint8
	L        uint8
	G        uint8
	AVL      uint8
	unusable uint8
	_        uint8
}

// Clear clears the segment and marks it unusable.
func (s *segment) Clear() {
	*s = segment{unusable: 1}
}

// selector is a segment selector.
type selector uint16

// tobool is a simple helper.
func tobool(x ring0.SegmentDescriptorFlags) uint8 {
	if x != 0 {
		return 1
	}
	return 0
}

// Load loads the segment described by d into the segment s.
//
// The argument sel is recorded as the segment selector index.
func (s *segment) Load(d *ring0.SegmentDescriptor, sel ring0.Selector) {
	flag := d.Flags()
	if flag&ring0.SegmentDescriptorPresent == 0 {
		s.Clear()
		return
	}
	s.base = uint64(d.Base())
	s.limit = d.Limit()
	s.typ = uint8((flag>>8)&0xF) | 1
	s.S = tobool(flag & ring0.SegmentDescriptorSystem)
	s.DPL = uint8(d.DPL())
	s.present = tobool(flag & ring0.SegmentDescriptorPresent)
	s.AVL = tobool(flag & ring0.SegmentDescriptorAVL)
	s.L = tobool(flag & ring0.SegmentDescriptorLong)
	s.DB = tobool(flag & ring0.SegmentDescriptorDB)
	s.G = tobool(flag & ring0.SegmentDescriptorG)
	if s.L != 0 {
		s.limit = 0xffffffff
	}
	s.unusable = 0
	s.selector = uint16(sel)
}

// descriptor describes a region of physical memory.
//
// It corresponds to the pseudo-descriptor used in the x86 LGDT and LIDT
// instructions, and mirrors kvm_dtable.
type descriptor struct {
	base  uint64
	limit uint16
	_     [3]uint16
}

// modelControlRegister is an MSR entry.
//
// This mirrors kvm_msr_entry.
type modelControlRegister struct {
	index uint32
	_     uint32
	data  uint64
}

// modelControlRegisers is a collection of MSRs.
//
// This mirrors kvm_msrs.
type modelControlRegisters struct {
	nmsrs   uint32
	_       uint32
	entries [16]modelControlRegister
}

// cpuidEntry is a single CPUID entry.
//
// This mirrors kvm_cpuid_entry2.
type cpuidEntry struct {
	function uint32
	index    uint32
	flags    uint32
	eax      uint32
	ebx      uint32
	ecx      uint32
	edx      uint32
	_        [3]uint32
}

// cpuidEntries is a collection of CPUID entries.
//
// This mirrors kvm_cpuid2.
type cpuidEntries struct {
	nr      uint32
	_       uint32
	entries [_KVM_NR_CPUID_ENTRIES]cpuidEntry
}

// Query implements cpuid.Function.Query.
func (c *cpuidEntries) Query(in cpuid.In) (out cpuid.Out) {
	for i := 0; i < int(c.nr); i++ {
		if c.entries[i].function == in.Eax && c.entries[i].index == in.Ecx {
			out.Eax = c.entries[i].eax
			out.Ebx = c.entries[i].ebx
			out.Ecx = c.entries[i].ecx
			out.Edx = c.entries[i].edx
			return
		}
	}
	return
}

// Set implements cpuid.ChangeableSet.Set.
func (c *cpuidEntries) Set(in cpuid.In, out cpuid.Out) {
	i := 0
	for ; i < int(c.nr); i++ {
		if c.entries[i].function == in.Eax && c.entries[i].index == in.Ecx {
			break
		}
	}
	if i == _KVM_NR_CPUID_ENTRIES {
		panic("exceeded KVM_NR_CPUID_ENTRIES")
	}

	c.entries[i].eax = out.Eax
	c.entries[i].ebx = out.Ebx
	c.entries[i].ecx = out.Ecx
	c.entries[i].edx = out.Edx
	if i == int(c.nr) {
		c.nr++
	}
}

// updateGlobalOnce does global initialization. It has to be called only once.
func updateGlobalOnce(fd int) error {
	err := updateSystemValues(int(fd))
	fs := cpuid.FeatureSet{
		Function: &cpuidSupported,
	}
	// Calculate whether guestPCID is supported.
	hasGuestPCID = fs.HasFeature(cpuid.X86FeaturePCID)
	// Create a static feature set from the KVM entries. Then, we
	// explicitly set OSXSAVE, since this does not come in the feature
	// entries, but can be provided when the relevant CR4 bit is set.
	s := &cpuidSupported
	cpuid.X86FeatureOSXSAVE.Set(s)
	// Explicitly disable nested virtualization. Since we don't provide
	// any virtualization APIs, there is no need to enable this feature.
	cpuid.X86FeatureVMX.Unset(s)
	cpuid.X86FeatureSVM.Unset(s)
	ring0.Init(cpuid.FeatureSet{
		Function: s,
	})
	physicalInit()
	return err
}
<commit_msg>kvm: handle errors of updateSystemValues<commit_after>// Copyright 2018 The gVisor 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.

//go:build amd64
// +build amd64

package kvm

import (
	"gvisor.dev/gvisor/pkg/cpuid"
	"gvisor.dev/gvisor/pkg/ring0"
)

// userRegs represents KVM user registers.
//
// This mirrors kvm_regs.
type userRegs struct {
	RAX    uint64
	RBX    uint64
	RCX    uint64
	RDX    uint64
	RSI    uint64
	RDI    uint64
	RSP    uint64
	RBP    uint64
	R8     uint64
	R9     uint64
	R10    uint64
	R11    uint64
	R12    uint64
	R13    uint64
	R14    uint64
	R15    uint64
	RIP    uint64
	RFLAGS uint64
}

// systemRegs represents KVM system registers.
//
// This mirrors kvm_sregs.
type systemRegs struct {
	CS              segment
	DS              segment
	ES              segment
	FS              segment
	GS              segment
	SS              segment
	TR              segment
	LDT             segment
	GDT             descriptor
	IDT             descriptor
	CR0             uint64
	CR2             uint64
	CR3             uint64
	CR4             uint64
	CR8             uint64
	EFER            uint64
	apicBase        uint64
	interruptBitmap [(_KVM_NR_INTERRUPTS + 63) / 64]uint64
}

// segment is the expanded form of a segment register.
//
// This mirrors kvm_segment.
type segment struct {
	base     uint64
	limit    uint32
	selector uint16
	typ      uint8
	present  uint8
	DPL      uint8
	DB       uint8
	S        uint8
	L        uint8
	G        uint8
	AVL      uint8
	unusable uint8
	_        uint8
}

// Clear clears the segment and marks it unusable.
func (s *segment) Clear() {
	*s = segment{unusable: 1}
}

// selector is a segment selector.
type selector uint16

// tobool is a simple helper.
func tobool(x ring0.SegmentDescriptorFlags) uint8 {
	if x != 0 {
		return 1
	}
	return 0
}

// Load loads the segment described by d into the segment s.
//
// The argument sel is recorded as the segment selector index.
func (s *segment) Load(d *ring0.SegmentDescriptor, sel ring0.Selector) {
	flag := d.Flags()
	if flag&ring0.SegmentDescriptorPresent == 0 {
		s.Clear()
		return
	}
	s.base = uint64(d.Base())
	s.limit = d.Limit()
	s.typ = uint8((flag>>8)&0xF) | 1
	s.S = tobool(flag & ring0.SegmentDescriptorSystem)
	s.DPL = uint8(d.DPL())
	s.present = tobool(flag & ring0.SegmentDescriptorPresent)
	s.AVL = tobool(flag & ring0.SegmentDescriptorAVL)
	s.L = tobool(flag & ring0.SegmentDescriptorLong)
	s.DB = tobool(flag & ring0.SegmentDescriptorDB)
	s.G = tobool(flag & ring0.SegmentDescriptorG)
	if s.L != 0 {
		s.limit = 0xffffffff
	}
	s.unusable = 0
	s.selector = uint16(sel)
}

// descriptor describes a region of physical memory.
//
// It corresponds to the pseudo-descriptor used in the x86 LGDT and LIDT
// instructions, and mirrors kvm_dtable.
type descriptor struct {
	base  uint64
	limit uint16
	_     [3]uint16
}

// modelControlRegister is an MSR entry.
//
// This mirrors kvm_msr_entry.
type modelControlRegister struct {
	index uint32
	_     uint32
	data  uint64
}

// modelControlRegisers is a collection of MSRs.
//
// This mirrors kvm_msrs.
type modelControlRegisters struct {
	nmsrs   uint32
	_       uint32
	entries [16]modelControlRegister
}

// cpuidEntry is a single CPUID entry.
//
// This mirrors kvm_cpuid_entry2.
type cpuidEntry struct {
	function uint32
	index    uint32
	flags    uint32
	eax      uint32
	ebx      uint32
	ecx      uint32
	edx      uint32
	_        [3]uint32
}

// cpuidEntries is a collection of CPUID entries.
//
// This mirrors kvm_cpuid2.
type cpuidEntries struct {
	nr      uint32
	_       uint32
	entries [_KVM_NR_CPUID_ENTRIES]cpuidEntry
}

// Query implements cpuid.Function.Query.
func (c *cpuidEntries) Query(in cpuid.In) (out cpuid.Out) {
	for i := 0; i < int(c.nr); i++ {
		if c.entries[i].function == in.Eax && c.entries[i].index == in.Ecx {
			out.Eax = c.entries[i].eax
			out.Ebx = c.entries[i].ebx
			out.Ecx = c.entries[i].ecx
			out.Edx = c.entries[i].edx
			return
		}
	}
	return
}

// Set implements cpuid.ChangeableSet.Set.
func (c *cpuidEntries) Set(in cpuid.In, out cpuid.Out) {
	i := 0
	for ; i < int(c.nr); i++ {
		if c.entries[i].function == in.Eax && c.entries[i].index == in.Ecx {
			break
		}
	}
	if i == _KVM_NR_CPUID_ENTRIES {
		panic("exceeded KVM_NR_CPUID_ENTRIES")
	}

	c.entries[i].eax = out.Eax
	c.entries[i].ebx = out.Ebx
	c.entries[i].ecx = out.Ecx
	c.entries[i].edx = out.Edx
	if i == int(c.nr) {
		c.nr++
	}
}

// updateGlobalOnce does global initialization. It has to be called only once.
func updateGlobalOnce(fd int) error {
	if err := updateSystemValues(int(fd)); err != nil {
		return err
	}
	fs := cpuid.FeatureSet{
		Function: &cpuidSupported,
	}
	// Calculate whether guestPCID is supported.
	hasGuestPCID = fs.HasFeature(cpuid.X86FeaturePCID)
	// Create a static feature set from the KVM entries. Then, we
	// explicitly set OSXSAVE, since this does not come in the feature
	// entries, but can be provided when the relevant CR4 bit is set.
	s := &cpuidSupported
	cpuid.X86FeatureOSXSAVE.Set(s)
	// Explicitly disable nested virtualization. Since we don't provide
	// any virtualization APIs, there is no need to enable this feature.
	cpuid.X86FeatureVMX.Unset(s)
	cpuid.X86FeatureSVM.Unset(s)
	ring0.Init(cpuid.FeatureSet{
		Function: s,
	})
	physicalInit()
	return nil
}
<|endoftext|>


<commit_before>package paza

import "testing"

func TestAll(t *testing.T) {
	set := NewSet()
	set.Add("a", set.Regex(`a`))
	set.Add("+", set.Regex(`\+`))
	// direct recursive
	set.AddRec("expr", set.OrdChoice(set.Concat("expr", "+", "a"), "a"))
	// TODO indirect recursive

	cases := []struct {
		text   []byte
		parser string
		ok     bool
		length int
	}{
		{[]byte(""), "a", false, 0},
		{[]byte(""), "+", false, 0},
		{[]byte(""), "expr", false, 0},
		{[]byte("a"), "a", true, 1},
		{[]byte("a "), "a", true, 1},
		{[]byte("b"), "a", false, 0},
		{[]byte("+"), "+", true, 1},
		{[]byte("+b"), "+", true, 1},
		{[]byte("b"), "+", false, 0},
		{[]byte("a"), "expr", true, 1},
		{[]byte("a+"), "expr", true, 1},
		{[]byte("a+a"), "expr", true, 3},
		{[]byte("a+a+a+a+a"), "expr", true, 9},
		{[]byte("a+a+a+a+a+"), "expr", true, 9},
		{[]byte("a+a+a+a+a+a"), "expr", true, 11},
	}

	for _, c := range cases {
		input := NewInput(c.text)
		ok, l := set.Call(c.parser, input, 0)
		if c.ok != ok || c.length != l {
			t.Fatalf("%v", c)
		}
	}
}

func TestCalc(t *testing.T) {
	set := NewSet()
	set.AddRec("expr", set.OrdChoice(
		set.Concat("expr", set.Rune('+'), "term"),
		set.Concat("expr", set.Rune('-'), "term"),
		"term",
	))
	set.AddRec("term", set.OrdChoice(
		set.Concat("term", set.Rune('*'), "factor"),
		set.Concat("term", set.Rune('/'), "factor"),
		"factor",
	))
	set.Add("factor", set.OrdChoice(
		set.Regex(`[0-9]+`),
		set.Concat(set.Rune('('), "expr", set.Rune(')')),
	))

	cases := []struct {
		text   []byte
		parser string
		ok     bool
		length int
	}{
		{[]byte("1"), "expr", true, 1},
		{[]byte("1+1"), "expr", true, 3},
		{[]byte("1-1"), "expr", true, 3},
		{[]byte("1*1"), "expr", true, 3},
		{[]byte("1/1"), "expr", true, 3},
		{[]byte("(1/1)"), "expr", true, 5},
		{[]byte("(1)/1"), "expr", true, 5},
		{[]byte("(1)/1*3"), "expr", true, 7},
		{[]byte("(1)/1*(3-2)"), "expr", true, 11},
		{[]byte("(1)/1**(3-2)"), "expr", true, 5},
		{[]byte("*(1)/1**(3-2)"), "expr", false, 0},
		{[]byte(""), "expr", false, 0},
	}

	for _, c := range cases {
		input := NewInput(c.text)
		ok, l := set.Call(c.parser, input, 0)
		if c.ok != ok || c.length != l {
			t.Fatalf("%v", c)
		}
	}
}
<commit_msg>add regular expression parser test<commit_after>package paza

import "testing"

func TestAll(t *testing.T) {
	set := NewSet()
	set.Add("a", set.Regex(`a`))
	set.Add("+", set.Regex(`\+`))
	set.AddRec("expr", set.OrdChoice(set.Concat("expr", "+", "a"), "a"))

	cases := []struct {
		text   []byte
		parser string
		ok     bool
		length int
	}{
		{[]byte(""), "a", false, 0},
		{[]byte(""), "+", false, 0},
		{[]byte(""), "expr", false, 0},
		{[]byte("a"), "a", true, 1},
		{[]byte("a "), "a", true, 1},
		{[]byte("b"), "a", false, 0},
		{[]byte("+"), "+", true, 1},
		{[]byte("+b"), "+", true, 1},
		{[]byte("b"), "+", false, 0},
		{[]byte("a"), "expr", true, 1},
		{[]byte("a+"), "expr", true, 1},
		{[]byte("a+a"), "expr", true, 3},
		{[]byte("a+a+a+a+a"), "expr", true, 9},
		{[]byte("a+a+a+a+a+"), "expr", true, 9},
		{[]byte("a+a+a+a+a+a"), "expr", true, 11},
	}

	for _, c := range cases {
		input := NewInput(c.text)
		ok, l := set.Call(c.parser, input, 0)
		if c.ok != ok || c.length != l {
			t.Fatalf("%v", c)
		}
	}
}

func TestCalc(t *testing.T) {
	set := NewSet()
	set.AddRec("expr", set.OrdChoice(
		set.Concat("expr", set.Rune('+'), "term"),
		set.Concat("expr", set.Rune('-'), "term"),
		"term",
	))
	set.AddRec("term", set.OrdChoice(
		set.Concat("term", set.Rune('*'), "factor"),
		set.Concat("term", set.Rune('/'), "factor"),
		"factor",
	))
	set.Add("factor", set.OrdChoice(
		set.Regex(`[0-9]+`),
		set.Concat(set.Rune('('), "expr", set.Rune(')')),
	))

	cases := []struct {
		text   []byte
		parser string
		ok     bool
		length int
	}{
		{[]byte("1"), "expr", true, 1},
		{[]byte("1+1"), "expr", true, 3},
		{[]byte("1-1"), "expr", true, 3},
		{[]byte("1*1"), "expr", true, 3},
		{[]byte("1/1"), "expr", true, 3},
		{[]byte("(1/1)"), "expr", true, 5},
		{[]byte("(1)/1"), "expr", true, 5},
		{[]byte("(1)/1*3"), "expr", true, 7},
		{[]byte("(1)/1*(3-2)"), "expr", true, 11},
		{[]byte("(1)/1**(3-2)"), "expr", true, 5},
		{[]byte("*(1)/1**(3-2)"), "expr", false, 0},
		{[]byte(""), "expr", false, 0},
	}

	for _, c := range cases {
		input := NewInput(c.text)
		ok, l := set.Call(c.parser, input, 0)
		if c.ok != ok || c.length != l {
			t.Fatalf("%v", c)
		}
	}
}

func TestRegex(t *testing.T) {
	/*
		<RE>	::=	<RE> "|" <simple-RE> | <simple-RE>
		<simple-RE>	::=	<simple-RE> <basic-RE> | <basic-RE>
		<basic-RE>	::=	<elementary-RE> "*" | <elementary-RE> "+" | <elementary-RE>
		<elementary-RE>	::=	"(" <RE> ")" | "." | "$" | [a-zA-Z0-9]
	*/
	set := NewSet()
	set.AddRec("re", set.OrdChoice(
		set.Concat("re", set.Rune('|'), "simple-re"),
		"simple-re",
	))
	set.AddRec("simple-re", set.OrdChoice(
		set.Concat("simple-re", "basic-re"),
		"basic-re",
	))
	set.Add("basic-re", set.OrdChoice(
		set.Concat("elementary-re", set.Rune('*')),
		set.Concat("elementary-re", set.Rune('+')),
		"elementary-re",
	))
	set.Add("elementary-re", set.OrdChoice(
		set.Concat(set.Rune('('), "re", set.Rune(')')),
		set.Rune('.'),
		set.Rune('$'),
		set.Regex(`[a-zA-Z0-9]`),
	))

	cases := []struct {
		text   []byte
		parser string
		ok     bool
		length int
	}{
		{[]byte(""), "re", false, 0},
		{[]byte("a"), "re", true, 1},
		{[]byte("a*"), "re", true, 2},
		{[]byte("a.*"), "re", true, 3},
		{[]byte("a(.*)"), "re", true, 5},
		{[]byte("a(.*)+"), "re", true, 6},
		{[]byte("a(.*)+$"), "re", true, 7},
		{[]byte("a(.*)+$b+"), "re", true, 9},
		{[]byte("a(.*)+$|b+"), "re", true, 10},
		{[]byte("a(.*)+$|*b+"), "re", true, 7},
	}

	for _, c := range cases {
		input := NewInput(c.text)
		ok, l := set.Call(c.parser, input, 0)
		if c.ok != ok || c.length != l {
			t.Fatalf("%v", c)
		}
	}
}
<|endoftext|>


<commit_before>package quota

import (
	"cf/api"
	"cf/command_metadata"
	"cf/configuration"
	"cf/errors"
	"cf/requirements"
	"cf/terminal"
	"fmt"
	"github.com/codegangsta/cli"
)

type DeleteQuota struct {
	ui        terminal.UI
	config    configuration.Reader
	quotaRepo api.QuotaRepository
	orgReq    requirements.OrganizationRequirement
}

func NewDeleteQuota(ui terminal.UI, config configuration.Reader, quotaRepo api.QuotaRepository) (cmd *DeleteQuota) {
	cmd = new(DeleteQuota)
	cmd.ui = ui
	cmd.config = config
	cmd.quotaRepo = quotaRepo
	return
}

func (command *DeleteQuota) Metadata() command_metadata.CommandMetadata {
	return command_metadata.CommandMetadata{
		Name:        "delete-quota",
		Description: "Delete a quota",
		Usage:       "CF_NAME delete-quota QUOTA [-f]",
		Flags: []cli.Flag{
			cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
		},
	}
}

func (cmd *DeleteQuota) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
	if len(c.Args()) != 1 {
		err = errors.New("Incorrect Usage")
		cmd.ui.FailWithUsage(c, "delete-quota")
		return
	}

	reqs = []requirements.Requirement{
		requirementsFactory.NewLoginRequirement(),
	}
	return
}

func (cmd *DeleteQuota) Run(c *cli.Context) {
	quotaName := c.Args()[0]

	if !c.Bool("f") {
		response := cmd.ui.ConfirmDelete("quota", quotaName)
		if !response {
			return
		}
	}

	cmd.ui.Say("Deleting quota %s as %s...",
		terminal.EntityNameColor(quotaName),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	quota, apiErr := cmd.quotaRepo.FindByName(quotaName)

	switch (apiErr).(type) {
	case nil: // no error
	case *errors.ModelNotFoundError:
		cmd.ui.Ok()
		cmd.ui.Warn("Quota %s does not exist", quotaName)
		return
	default:
		cmd.ui.Failed(apiErr.Error())
	}

	apiErr = cmd.quotaRepo.Delete(quota.Guid)
	if apiErr != nil {
		cmd.ui.Failed(apiErr.Error())
	}

	cmd.ui.Ok()
}
<commit_msg>Remove unused import<commit_after>package quota

import (
	"cf/api"
	"cf/command_metadata"
	"cf/configuration"
	"cf/errors"
	"cf/requirements"
	"cf/terminal"
	"github.com/codegangsta/cli"
)

type DeleteQuota struct {
	ui        terminal.UI
	config    configuration.Reader
	quotaRepo api.QuotaRepository
	orgReq    requirements.OrganizationRequirement
}

func NewDeleteQuota(ui terminal.UI, config configuration.Reader, quotaRepo api.QuotaRepository) (cmd *DeleteQuota) {
	cmd = new(DeleteQuota)
	cmd.ui = ui
	cmd.config = config
	cmd.quotaRepo = quotaRepo
	return
}

func (command *DeleteQuota) Metadata() command_metadata.CommandMetadata {
	return command_metadata.CommandMetadata{
		Name:        "delete-quota",
		Description: "Delete a quota",
		Usage:       "CF_NAME delete-quota QUOTA [-f]",
		Flags: []cli.Flag{
			cli.BoolFlag{Name: "f", Usage: "Force deletion without confirmation"},
		},
	}
}

func (cmd *DeleteQuota) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
	if len(c.Args()) != 1 {
		err = errors.New("Incorrect Usage")
		cmd.ui.FailWithUsage(c, "delete-quota")
		return
	}

	reqs = []requirements.Requirement{
		requirementsFactory.NewLoginRequirement(),
	}
	return
}

func (cmd *DeleteQuota) Run(c *cli.Context) {
	quotaName := c.Args()[0]

	if !c.Bool("f") {
		response := cmd.ui.ConfirmDelete("quota", quotaName)
		if !response {
			return
		}
	}

	cmd.ui.Say("Deleting quota %s as %s...",
		terminal.EntityNameColor(quotaName),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	quota, apiErr := cmd.quotaRepo.FindByName(quotaName)

	switch (apiErr).(type) {
	case nil: // no error
	case *errors.ModelNotFoundError:
		cmd.ui.Ok()
		cmd.ui.Warn("Quota %s does not exist", quotaName)
		return
	default:
		cmd.ui.Failed(apiErr.Error())
	}

	apiErr = cmd.quotaRepo.Delete(quota.Guid)
	if apiErr != nil {
		cmd.ui.Failed(apiErr.Error())
	}

	cmd.ui.Ok()
}
<|endoftext|>


<commit_before>package aws

import (
	"github.com/hashicorp/terraform/terraform"
)

type ResourceProvider struct {
}

func (p *ResourceProvider) Configure(map[string]interface{}) ([]string, error) {
	return nil, nil
}

func (p *ResourceProvider) Resources() []terraform.ResourceType {
	return nil
}
<commit_msg>providers/aws: pass tests<commit_after>package aws

import (
	"github.com/hashicorp/terraform/terraform"
)

type ResourceProvider struct {
}

func (p *ResourceProvider) Configure(map[string]interface{}) error {
	return nil
}

func (p *ResourceProvider) Diff(
	s *terraform.ResourceState,
	c map[string]interface{}) (*terraform.ResourceDiff, error) {
	return nil, nil
}

func (p *ResourceProvider) Resources() []terraform.ResourceType {
	return nil
}
<|endoftext|>


<commit_before>/*
Copyright 2016 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 network

import (
	"bytes"
	"fmt"
	"os/exec"
	"strconv"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/containernetworking/cni/pkg/ns"
	cnicurrent "github.com/containernetworking/cni/pkg/types/current"

	"github.com/Mirantis/virtlet/pkg/dhcp"
)

const (
	maxNetTestMembers = 32
	dhcpcdTimeout     = 2
)

type NetTester interface {
	Name() string
	Fg() bool
	Run(readyCh, stopCh chan struct{}) error
}

type NetTestGroup struct {
	wg, fg  sync.WaitGroup
	t       *testing.T
	timeout time.Duration
	stopCh  chan struct{}
	errCh   chan error
	nVacant int
}

func NewNetTestGroup(t *testing.T, timeout time.Duration) *NetTestGroup {
	return &NetTestGroup{
		t:       t,
		timeout: timeout,
		stopCh:  make(chan struct{}),
		errCh:   make(chan error, maxNetTestMembers),
		nVacant: maxNetTestMembers,
	}
}

func (g *NetTestGroup) Add(netNS ns.NetNS, tester NetTester) chan struct{} {
	if g.nVacant == 0 {
		// sending to errCh can possibly block if we add more members
		// than maxNetTestMembers
		g.t.Fatal("can't add more members to the test group")
	}
	doneCh := make(chan struct{})
	g.nVacant--
	readyCh := make(chan struct{})
	g.wg.Add(1)
	if tester.Fg() {
		g.fg.Add(1)
	}
	go func() {
		err := netNS.Do(func(ns.NetNS) (err error) {
			return tester.Run(readyCh, g.stopCh)
		})
		if err != nil {
			g.errCh <- fmt.Errorf("%s: %v", tester.Name(), err)
		}
		if tester.Fg() {
			g.fg.Done()
		}
		g.wg.Done()
		close(doneCh)
	}()
	select {
	case err := <-g.errCh:
		g.t.Fatal(err)
	case <-readyCh:
	}
	return doneCh
}

func (g *NetTestGroup) Stop() {
	if g.stopCh != nil {
		close(g.stopCh)
		g.stopCh = nil
		g.wg.Wait()
	}
}

func (g *NetTestGroup) Wait() {
	if g.stopCh == nil {
		g.t.Fatalf("test group already stopped")
	}

	var msgs []string
	fgDoneCh := make(chan struct{})
	go func() {
		g.fg.Wait()
		close(fgDoneCh)
	}()
	select {
	case <-fgDoneCh:
	case <-time.After(g.timeout):
		msgs = append(msgs, "test group timed out")
	}

	close(g.stopCh)
	g.stopCh = nil
	g.wg.Wait()
	for {
		select {
		case err := <-g.errCh:
			msgs = append(msgs, err.Error())
		default:
			if len(msgs) > 0 {
				g.t.Fatalf("test group failed:\n%s", strings.Join(msgs, "\n"))
			}
			return
		}
	}
}

type DhcpServerTester struct {
	config *cnicurrent.Result
}

func NewDhcpServerTester(config *cnicurrent.Result) *DhcpServerTester {
	return &DhcpServerTester{config}
}

func (d *DhcpServerTester) Name() string { return "dhcp server" }
func (d *DhcpServerTester) Fg() bool     { return false }

func (d *DhcpServerTester) Run(readyCh, stopCh chan struct{}) error {
	server := dhcp.NewServer(d.config)
	if err := server.SetupListener("0.0.0.0"); err != nil {
		return fmt.Errorf("failed to setup dhcp listener: %v", err)
	}

	close(readyCh)
	go func() {
		<-stopCh
		// If this happens before server.Serve() is executed,
		// Serve() will fail, but no race condition should happen
		server.Close()
	}()
	err := server.Serve()
	select {
	case <-stopCh:
		// skip 'use of closed network connection' error
		// if the server was stopped
		return nil
	default:
		return err
	}
}

type DhcpClient struct {
	expectedSubstrings []string
}

func NewDhcpClient(expectedSubstrings []string) *DhcpClient {
	return &DhcpClient{expectedSubstrings}
}

func (d *DhcpClient) Name() string { return "dhcp client" }
func (d *DhcpClient) Fg() bool     { return true }

func (d *DhcpClient) Run(readyCh, stopCh chan struct{}) error {
	args := []string{"-T", "-t", strconv.Itoa(dhcpcdTimeout)}
	cmd := exec.Command("dhcpcd", args...)
	var b bytes.Buffer
	cmd.Stdout = &b
	cmd.Stderr = &b
	if err := cmd.Start(); err != nil {
		return fmt.Errorf("error starting dhcpcd: %v", err)
	}
	close(readyCh)
	doneCh := make(chan struct{})
	go func() {
		select {
		case <-stopCh:
			cmd.Process.Kill()
		case <-doneCh:
		}
	}()
	err := cmd.Wait()
	close(doneCh)
	outStr := b.String()
	if err != nil {
		return fmt.Errorf("dhcpcd %s failed: %v\nout:\n%s", strings.Join(args, " "), err, outStr)
	}

	var missing []string
	for _, str := range d.expectedSubstrings {
		if !strings.Contains(outStr, str) {
			missing = append(missing, str)
		}
	}
	if len(missing) != 0 {
		return fmt.Errorf("some of the substrings are missing from dhcpcd output:\n%s\n--- Full output:\n%s",
			strings.Join(missing, "\n"), outStr)
	}
	return nil
}
<commit_msg>Fix network test failure handling<commit_after>/*
Copyright 2016 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 network

import (
	"bytes"
	"fmt"
	"os/exec"
	"strconv"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/containernetworking/cni/pkg/ns"
	cnicurrent "github.com/containernetworking/cni/pkg/types/current"

	"github.com/Mirantis/virtlet/pkg/dhcp"
)

const (
	maxNetTestMembers = 32
	dhcpcdTimeout     = 2
)

type NetTester interface {
	Name() string
	Fg() bool
	Run(readyCh, stopCh chan struct{}) error
}

type NetTestGroup struct {
	wg, fg  sync.WaitGroup
	t       *testing.T
	timeout time.Duration
	stopCh  chan struct{}
	errCh   chan error
	nVacant int
}

func NewNetTestGroup(t *testing.T, timeout time.Duration) *NetTestGroup {
	return &NetTestGroup{
		t:       t,
		timeout: timeout,
		stopCh:  make(chan struct{}),
		errCh:   make(chan error, maxNetTestMembers),
		nVacant: maxNetTestMembers,
	}
}

func (g *NetTestGroup) Add(netNS ns.NetNS, tester NetTester) chan struct{} {
	if g.nVacant == 0 {
		// sending to errCh can possibly block if we add more members
		// than maxNetTestMembers
		g.t.Fatal("can't add more members to the test group")
	}
	doneCh := make(chan struct{})
	g.nVacant--
	readyCh := make(chan struct{})
	g.wg.Add(1)
	if tester.Fg() {
		g.fg.Add(1)
	}
	go func() {
		err := netNS.Do(func(ns.NetNS) (err error) {
			return tester.Run(readyCh, g.stopCh)
		})
		if err != nil {
			g.errCh <- fmt.Errorf("%s: %v", tester.Name(), err)
		}
		if tester.Fg() {
			g.fg.Done()
		}
		g.wg.Done()
		close(doneCh)
	}()
	select {
	case err := <-g.errCh:
		close(g.stopCh)
		g.stopCh = nil
		g.t.Fatal(err)
	case <-readyCh:
	}
	return doneCh
}

func (g *NetTestGroup) Stop() {
	if g.stopCh != nil {
		close(g.stopCh)
		g.stopCh = nil
		g.wg.Wait()
	}
}

func (g *NetTestGroup) Wait() {
	if g.stopCh == nil {
		g.t.Fatalf("test group already stopped")
	}

	var msgs []string
	fgDoneCh := make(chan struct{})
	go func() {
		g.fg.Wait()
		close(fgDoneCh)
	}()
	select {
	case <-fgDoneCh:
	case <-time.After(g.timeout):
		msgs = append(msgs, "test group timed out")
	}

	close(g.stopCh)
	g.stopCh = nil
	g.wg.Wait()
	for {
		select {
		case err := <-g.errCh:
			msgs = append(msgs, err.Error())
		default:
			if len(msgs) > 0 {
				g.t.Fatalf("test group failed:\n%s", strings.Join(msgs, "\n"))
			}
			return
		}
	}
}

type DhcpServerTester struct {
	config *cnicurrent.Result
}

func NewDhcpServerTester(config *cnicurrent.Result) *DhcpServerTester {
	return &DhcpServerTester{config}
}

func (d *DhcpServerTester) Name() string { return "dhcp server" }
func (d *DhcpServerTester) Fg() bool     { return false }

func (d *DhcpServerTester) Run(readyCh, stopCh chan struct{}) error {
	server := dhcp.NewServer(d.config)
	if err := server.SetupListener("0.0.0.0"); err != nil {
		return fmt.Errorf("failed to setup dhcp listener: %v", err)
	}

	close(readyCh)
	go func() {
		<-stopCh
		// If this happens before server.Serve() is executed,
		// Serve() will fail, but no race condition should happen
		server.Close()
	}()
	err := server.Serve()
	select {
	case <-stopCh:
		// skip 'use of closed network connection' error
		// if the server was stopped
		return nil
	default:
		return err
	}
}

type DhcpClient struct {
	expectedSubstrings []string
}

func NewDhcpClient(expectedSubstrings []string) *DhcpClient {
	return &DhcpClient{expectedSubstrings}
}

func (d *DhcpClient) Name() string { return "dhcp client" }
func (d *DhcpClient) Fg() bool     { return true }

func (d *DhcpClient) Run(readyCh, stopCh chan struct{}) error {
	args := []string{"-T", "-t", strconv.Itoa(dhcpcdTimeout)}
	cmd := exec.Command("dhcpcd", args...)
	var b bytes.Buffer
	cmd.Stdout = &b
	cmd.Stderr = &b
	if err := cmd.Start(); err != nil {
		return fmt.Errorf("error starting dhcpcd: %v", err)
	}
	close(readyCh)
	doneCh := make(chan struct{})
	go func() {
		select {
		case <-stopCh:
			cmd.Process.Kill()
		case <-doneCh:
		}
	}()
	err := cmd.Wait()
	close(doneCh)
	outStr := b.String()
	if err != nil {
		return fmt.Errorf("dhcpcd %s failed: %v\nout:\n%s", strings.Join(args, " "), err, outStr)
	}

	var missing []string
	for _, str := range d.expectedSubstrings {
		if !strings.Contains(outStr, str) {
			missing = append(missing, str)
		}
	}
	if len(missing) != 0 {
		return fmt.Errorf("some of the substrings are missing from dhcpcd output:\n%s\n--- Full output:\n%s",
			strings.Join(missing, "\n"), outStr)
	}
	return nil
}
<|endoftext|>


<commit_before>package peco

import (
	"fmt"
	"sync"
	"testing"
	"time"

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

type interceptorArgs []interface{}
type interceptor struct {
	m      sync.Locker
	events map[string][]interceptorArgs
}

func newInterceptor() *interceptor {
	return &interceptor{
		newMutex(),
		make(map[string][]interceptorArgs),
	}
}

func (i *interceptor) reset() {
	i.m.Lock()
	defer i.m.Unlock()

	i.events = make(map[string][]interceptorArgs)
}

func (i *interceptor) record(name string, args []interface{}) {
	i.m.Lock()
	defer i.m.Unlock()

	events := i.events
	v, ok := events[name]
	if !ok {
		v = []interceptorArgs{}
	}

	events[name] = append(v, interceptorArgs(args))
}

func TestIDGen(t *testing.T) {
	lines := []*RawLine{}
	for i := 0; i < 1000000; i++ {
		lines = append(lines, NewRawLine(fmt.Sprintf("%d", i), false))
	}

	sel := NewSelection()
	for _, l := range lines {
		if sel.Has(l) {
			t.Errorf("Collision detected %d", l.ID())
		}
		sel.Add(l)
	}
}

func TestPeco(t *testing.T) {
	p := New()
	p.Argv = []string{"peco", "peco_test.go"}

	time.AfterFunc(time.Second, func() {
		p.Exit(nil)
	})
	if !assert.NoError(t, p.Run(), "p.Run() succeeds") {
		return
	}
}
<commit_msg>Use runtime.Caller to identify the file<commit_after>package peco

import (
	"fmt"
	"runtime"
	"sync"
	"testing"
	"time"

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

type interceptorArgs []interface{}
type interceptor struct {
	m      sync.Locker
	events map[string][]interceptorArgs
}

func newInterceptor() *interceptor {
	return &interceptor{
		newMutex(),
		make(map[string][]interceptorArgs),
	}
}

func (i *interceptor) reset() {
	i.m.Lock()
	defer i.m.Unlock()

	i.events = make(map[string][]interceptorArgs)
}

func (i *interceptor) record(name string, args []interface{}) {
	i.m.Lock()
	defer i.m.Unlock()

	events := i.events
	v, ok := events[name]
	if !ok {
		v = []interceptorArgs{}
	}

	events[name] = append(v, interceptorArgs(args))
}

func TestIDGen(t *testing.T) {
	lines := []*RawLine{}
	for i := 0; i < 1000000; i++ {
		lines = append(lines, NewRawLine(fmt.Sprintf("%d", i), false))
	}

	sel := NewSelection()
	for _, l := range lines {
		if sel.Has(l) {
			t.Errorf("Collision detected %d", l.ID())
		}
		sel.Add(l)
	}
}

func TestPeco(t *testing.T) {
	_, file, _, _ := runtime.Caller(0)
	p := New()
	p.Argv = []string{"peco", file}

	time.AfterFunc(time.Second, func() {
		p.Exit(nil)
	})
	if !assert.NoError(t, p.Run(), "p.Run() succeeds") {
		return
	}
}
<|endoftext|>


<commit_before>package main

import (
	"./kew"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	flag.Parse()
	path := flag.Arg(0)

	config := kew.NewConfig()

	// Load config
	if path != "" {
		file, err := ioutil.ReadFile(path)
		if err != nil {
			panic(fmt.Sprintf("kew: Error reading config file: %v", err))
			os.Exit(1)
		}

		err = json.Unmarshal(file, &config)
		if err != nil {
			panic(fmt.Sprintf("kew: Error parsing config file: %v", err))
		}
	}

	s := kew.NewServer(config)
	s.Store.Load()

	err := s.ListenAndServe()
	if err != nil {
		panic(fmt.Sprintf("main: %v", err))
	}

	fmt.Printf("Listening on http://localhost%s\n", s.Addr)

	shutdown := make(chan bool)
	<-shutdown
}
<commit_msg>Parallelize<commit_after>package main

import (
	"./kew"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"runtime"
)

func main() {
	flag.Parse()
	path := flag.Arg(0)

	runtime.GOMAXPROCS(runtime.NumCPU())

	config := kew.NewConfig()

	// Load config
	if path != "" {
		file, err := ioutil.ReadFile(path)
		if err != nil {
			panic(fmt.Sprintf("kew: Error reading config file: %v", err))
			os.Exit(1)
		}

		err = json.Unmarshal(file, &config)
		if err != nil {
			panic(fmt.Sprintf("kew: Error parsing config file: %v", err))
		}
	}

	s := kew.NewServer(config)
	s.Store.Load()

	err := s.ListenAndServe()
	if err != nil {
		panic(fmt.Sprintf("main: %v", err))
	}

	fmt.Printf("Listening on http://localhost%s\n", s.Addr)

	shutdown := make(chan bool)
	<-shutdown
}
<|endoftext|>


<commit_before>package core

import (
	"bytes"
	"encoding/gob"
	"errors"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strconv"
)

// DatasetEvaluatorType represents the type of the dataset evaluator
type DatasetEvaluatorType uint8

const (
	// OnlineEval dynamically parses the dataset values
	OnlineEval DatasetEvaluatorType = iota + 1
	// FileBasedEval returns the pre-computed values of an operator
	FileBasedEval
)

// DatasetEvaluator reflects the interface of an evaluator object.
type DatasetEvaluator interface {
	Evaluate(string) (float64, error)
}

// NewDatasetEvaluator returns a new DatasetEvaluator object
func NewDatasetEvaluator(evalType DatasetEvaluatorType,
	params map[string]string) (DatasetEvaluator, error) {
	if evalType == OnlineEval {
		eval := new(OnlineDatasetEvaluator)
		if _, ok := params["script"]; !ok {
			return nil, errors.New("Online evaluator needs script param")
		}
		eval.script = params["script"]
		if _, ok := params["testset"]; !ok {
			return nil, errors.New("Online evaluator needs testset param")
		}
		eval.testset = params["testset"]
		return eval, nil
	} else if evalType == FileBasedEval {
		eval := new(FileBasedEvaluator)
		if _, ok := params["scores"]; !ok {
			return nil, errors.New("File based evaluator needs scores file")
		}
		f, err := os.Open(params["scores"])
		if err != nil {
			return nil, err
		}
		scores := NewDatasetScores()
		buf, err := ioutil.ReadAll(f)
		if err != nil {
			return nil, err
		}
		err = scores.Deserialize(buf)
		if err != nil {
			return nil, err
		}
		eval.scores = *scores
		return eval, nil
	}
	return nil, errors.New("Not suitable evaluator found")
}

// OnlineDatasetEvaluator is responsible to execute the training script and fetch
// the model accuracy
type OnlineDatasetEvaluator struct {
	script  string
	testset string
}

// Evaluate evaluates a new dataset, based on its path
func (e *OnlineDatasetEvaluator) Evaluate(dataset string) (float64, error) {
	cmd := exec.Command(e.script, dataset, e.testset)
	out, err := cmd.CombinedOutput()
	if err != nil {
		log.Println(err)
		return -1, err
	}
	val, err := strconv.ParseFloat(string(out), 64)
	if err != nil {
		log.Println(err)
		return -1, err
	}
	return val, nil
}

// FileBasedEvaluator returns the scores of an operator based on a scores file.
type FileBasedEvaluator struct {
	scores DatasetScores
}

// Evaluate returns the score for a given dataset
func (e *FileBasedEvaluator) Evaluate(dataset string) (float64, error) {
	val, ok := e.scores.Scores[dataset]
	if !ok {
		return -1, errors.New("Dataset not found")
	}
	return val, nil
}

// DatasetScores is used to store the scores of a set of datasets
type DatasetScores struct {
	Scores map[string]float64
}

// NewDatasetScores initializes a new DatasetScores struct
func NewDatasetScores() *DatasetScores {
	o := new(DatasetScores)
	o.Scores = make(map[string]float64)
	return o
}

// Serialize returns a stream containing a DatasetScores object
func (s *DatasetScores) Serialize() ([]byte, error) {
	buf := new(bytes.Buffer)
	e := gob.NewEncoder(buf)
	err := e.Encode(s.Scores)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

// Deserialize constructs a DatasetScores strucy based on a byte array
func (s *DatasetScores) Deserialize(buf []byte) error {
	content := bytes.NewBuffer(buf)
	d := gob.NewDecoder(content)
	err := d.Decode(&s.Scores)
	return err
}
<commit_msg>removing spaces before parsing val<commit_after>package core

import (
	"bytes"
	"encoding/gob"
	"errors"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strconv"
	"strings"
)

// DatasetEvaluatorType represents the type of the dataset evaluator
type DatasetEvaluatorType uint8

const (
	// OnlineEval dynamically parses the dataset values
	OnlineEval DatasetEvaluatorType = iota + 1
	// FileBasedEval returns the pre-computed values of an operator
	FileBasedEval
)

// DatasetEvaluator reflects the interface of an evaluator object.
type DatasetEvaluator interface {
	Evaluate(string) (float64, error)
}

// NewDatasetEvaluator returns a new DatasetEvaluator object
func NewDatasetEvaluator(evalType DatasetEvaluatorType,
	params map[string]string) (DatasetEvaluator, error) {
	if evalType == OnlineEval {
		eval := new(OnlineDatasetEvaluator)
		if _, ok := params["script"]; !ok {
			return nil, errors.New("Online evaluator needs script param")
		}
		eval.script = params["script"]
		if _, ok := params["testset"]; !ok {
			return nil, errors.New("Online evaluator needs testset param")
		}
		eval.testset = params["testset"]
		return eval, nil
	} else if evalType == FileBasedEval {
		eval := new(FileBasedEvaluator)
		if _, ok := params["scores"]; !ok {
			return nil, errors.New("File based evaluator needs scores file")
		}
		f, err := os.Open(params["scores"])
		if err != nil {
			return nil, err
		}
		scores := NewDatasetScores()
		buf, err := ioutil.ReadAll(f)
		if err != nil {
			return nil, err
		}
		err = scores.Deserialize(buf)
		if err != nil {
			return nil, err
		}
		eval.scores = *scores
		return eval, nil
	}
	return nil, errors.New("Not suitable evaluator found")
}

// OnlineDatasetEvaluator is responsible to execute the training script and fetch
// the model accuracy
type OnlineDatasetEvaluator struct {
	script  string
	testset string
}

// Evaluate evaluates a new dataset, based on its path
func (e *OnlineDatasetEvaluator) Evaluate(dataset string) (float64, error) {
	cmd := exec.Command(e.script, dataset, e.testset)
	out, err := cmd.CombinedOutput()
	if err != nil {
		log.Println(err)
		return -1, err
	}
	val, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
	if err != nil {
		log.Println(err)
		return -1, err
	}
	return val, nil
}

// FileBasedEvaluator returns the scores of an operator based on a scores file.
type FileBasedEvaluator struct {
	scores DatasetScores
}

// Evaluate returns the score for a given dataset
func (e *FileBasedEvaluator) Evaluate(dataset string) (float64, error) {
	val, ok := e.scores.Scores[dataset]
	if !ok {
		return -1, errors.New("Dataset not found")
	}
	return val, nil
}

// DatasetScores is used to store the scores of a set of datasets
type DatasetScores struct {
	Scores map[string]float64
}

// NewDatasetScores initializes a new DatasetScores struct
func NewDatasetScores() *DatasetScores {
	o := new(DatasetScores)
	o.Scores = make(map[string]float64)
	return o
}

// Serialize returns a stream containing a DatasetScores object
func (s *DatasetScores) Serialize() ([]byte, error) {
	buf := new(bytes.Buffer)
	e := gob.NewEncoder(buf)
	err := e.Encode(s.Scores)
	if err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

// Deserialize constructs a DatasetScores strucy based on a byte array
func (s *DatasetScores) Deserialize(buf []byte) error {
	content := bytes.NewBuffer(buf)
	d := gob.NewDecoder(content)
	err := d.Decode(&s.Scores)
	return err
}
<|endoftext|>


<commit_before>package libkb

import (
	"code.google.com/p/go.crypto/openpgp"
	"encoding/hex"
	"fmt"
	"github.com/keybase/go-jsonw"
)

type PgpKeyBundle openpgp.Entity

const (
	PGP_FINGERPRINT_LEN = 20
)

type PgpFingerprint []byte

func PgpFingerprintFromHex(s string) (PgpFingerprint, error) {
	bv, err := hex.DecodeString(s)
	if err == nil && len(bv) != PGP_FINGERPRINT_LEN {
		err = fmt.Errorf("Bad fingerprint; wrong length: %d", len(bv))
		bv = nil
	}
	var ret PgpFingerprint
	if bv != nil {
		ret = PgpFingerprint(bv)
	}
	return ret, err
}

func (p PgpFingerprint) ToString() string {
	return hex.EncodeToString(p)
}

func GetPgpFingerprint(w *jsonw.Wrapper) (PgpFingerprint, error) {
	s, err := w.GetString()
	if err != nil {
		return nil, err
	}
	ret, err := PgpFingerprintFromHex(s)
	return ret, err
}

func GetPgpFingerprintVoid(w *jsonw.Wrapper, p *PgpFingerprint, e *error) {
	ret, err := GetPgpFingerprint(w)
	if err != nil {
		*e = err
	} else {
		*p = ret
	}
}

func (k PgpKeyBundle) toList() openpgp.EntityList {
	list := make(openpgp.EntityList, 1, 1)
	list[0] = (*openpgp.Entity)(&k)
	return list
}

func (k PgpKeyBundle) KeysById(id uint64) []openpgp.Key {
	return k.toList().KeysById(id)
}

func (k PgpKeyBundle) KeysByIdUsage(id uint64, usage byte) []openpgp.Key {
	return k.toList().KeysByIdUsage(id, usage)
}

func (k PgpKeyBundle) DecryptionKeys() []openpgp.Key {
	return k.toList().DecryptionKeys()
}

func (k PgpKeyBundle) MatchesKey(key *openpgp.Key) bool {
	return FastByteArrayEq(k.PrimaryKey.Fingerprint[:],
		key.PublicKey.Fingerprint[:])
}
<commit_msg>key: remove duplicate signatures<commit_after>package libkb

import (
	"code.google.com/p/go.crypto/openpgp"
	"encoding/hex"
	"fmt"
	"github.com/keybase/go-jsonw"
)

type PgpKeyBundle openpgp.Entity

const (
	PGP_FINGERPRINT_LEN = 20
)

type PgpFingerprint []byte

func PgpFingerprintFromHex(s string) (PgpFingerprint, error) {
	bv, err := hex.DecodeString(s)
	if err == nil && len(bv) != PGP_FINGERPRINT_LEN {
		err = fmt.Errorf("Bad fingerprint; wrong length: %d", len(bv))
		bv = nil
	}
	var ret PgpFingerprint
	if bv != nil {
		ret = PgpFingerprint(bv)
	}
	return ret, err
}

func (p PgpFingerprint) ToString() string {
	return hex.EncodeToString(p)
}

func GetPgpFingerprint(w *jsonw.Wrapper) (PgpFingerprint, error) {
	s, err := w.GetString()
	if err != nil {
		return nil, err
	}
	ret, err := PgpFingerprintFromHex(s)
	return ret, err
}

func GetPgpFingerprintVoid(w *jsonw.Wrapper, p *PgpFingerprint, e *error) {
	ret, err := GetPgpFingerprint(w)
	if err != nil {
		*e = err
	} else {
		*p = ret
	}
}
<|endoftext|>


<commit_before>// Package petrovich provides methods to inflect Russian first, last, and middle names.
package petrovich

//go:generate go run rules-generator/rules-generator.go

import (
	"strings"
	"unicode/utf8"
)

type Gender int

const (
	Androgynous Gender = iota
	Male
	Female
)

type Case int

const (
	Nomenative Case = -1 + iota
	Genitive
	Dative
	Accusative
	Instrumental
	Prepositional
)

// FirstName inflects first name depending on the given gender and case.
func FirstName(name string, g Gender, c Case) string {
	return inflect(name, g, c, allRules.firstName)
}

// MiddleName inflects middle name depending on the given gender and case.
func MiddleName(name string, g Gender, c Case) string {
	return inflect(name, g, c, allRules.middleName)
}

// LastName inflects last name depending on the given gender and case.
func LastName(name string, g Gender, c Case) string {
	return inflect(name, g, c, allRules.lastName)
}

func inflect(name string, g Gender, c Case, rg rulesGroup) string {
	if c == Nomenative {
		return name
	}

	ns := strings.Split(name, "-")
	r := make([]string, len(ns))

	for i, n := range ns {
		if e := checkException(n, g, c, rg); e != "" {
			r[i] = e
		} else {
			r[i] = findInRules(n, g, c, rg)
		}
	}

	return strings.Join(r, "-")
}

func checkException(name string, g Gender, c Case, rg rulesGroup) string {
	ln := strings.ToLower(name)

	for _, r := range rg.exceptions {
		if r.gender == Androgynous || r.gender == g {
			for _, t := range r.test {
				if t == ln && len(r.mods) > int(c) {
					return applyRule(name, r.mods[c])
				}
			}
		}
	}

	return ""
}

func findInRules(name string, g Gender, c Case, rg rulesGroup) string {
	for _, r := range rg.suffixes {
		if r.gender == Androgynous || r.gender == g {
			for _, t := range r.test {
				if strings.HasSuffix(name, t) && len(r.mods) > int(c) {
					return applyRule(name, r.mods[c])
				}
			}
		}
	}

	return name
}

func applyRule(name, mod string) string {
	if mod == "." {
		return name
	}

	if i := strings.LastIndex(mod, "-"); i >= 0 {
		l := utf8.RuneCountInString(name) - i - 1
		return string([]rune(name)[:l]) + mod[i+1:]
	}

	return name + mod
}
<commit_msg>Extend documentation<commit_after>// Package petrovich provides methods to inflect Russian first, last, and
// middle names.
//
// This is the Go port of https://github.com/petrovich. All implementations use
// common rules from https://github.com/petrovich/petrovich-rules.
//
// Usage:
//
//     n := petrovich.FirstName("Кузьма", petrovich.Male, petrovich.Genitive)
//     fmt.Print(n) // "Кузьмы"
//
//     n = petrovich.LastName("Петров-Водкин", petrovich.Male, petrovich.Prepositional)
//     fmt.Print(n) // "Петрове-Водкине"
//
package petrovich

//go:generate go run rules-generator/rules-generator.go

import (
	"strings"
	"unicode/utf8"
)

type Gender int

const (
	Androgynous Gender = iota
	Male
	Female
)

type Case int

const (
	Nomenative Case = -1 + iota
	Genitive
	Dative
	Accusative
	Instrumental
	Prepositional
)

// FirstName inflects first name depending on the given gender and case.
func FirstName(name string, g Gender, c Case) string {
	return inflect(name, g, c, allRules.firstName)
}

// MiddleName inflects middle name depending on the given gender and case.
func MiddleName(name string, g Gender, c Case) string {
	return inflect(name, g, c, allRules.middleName)
}

// LastName inflects last name depending on the given gender and case.
func LastName(name string, g Gender, c Case) string {
	return inflect(name, g, c, allRules.lastName)
}

func inflect(name string, g Gender, c Case, rg rulesGroup) string {
	if c == Nomenative {
		return name
	}

	ns := strings.Split(name, "-")
	r := make([]string, len(ns))

	for i, n := range ns {
		if e := checkException(n, g, c, rg); e != "" {
			r[i] = e
		} else {
			r[i] = findInRules(n, g, c, rg)
		}
	}

	return strings.Join(r, "-")
}

func checkException(name string, g Gender, c Case, rg rulesGroup) string {
	ln := strings.ToLower(name)

	for _, r := range rg.exceptions {
		if r.gender == Androgynous || r.gender == g {
			for _, t := range r.test {
				if t == ln && len(r.mods) > int(c) {
					return applyRule(name, r.mods[c])
				}
			}
		}
	}

	return ""
}

func findInRules(name string, g Gender, c Case, rg rulesGroup) string {
	for _, r := range rg.suffixes {
		if r.gender == Androgynous || r.gender == g {
			for _, t := range r.test {
				if strings.HasSuffix(name, t) && len(r.mods) > int(c) {
					return applyRule(name, r.mods[c])
				}
			}
		}
	}

	return name
}

func applyRule(name, mod string) string {
	if mod == "." {
		return name
	}

	if i := strings.LastIndex(mod, "-"); i >= 0 {
		l := utf8.RuneCountInString(name) - i - 1
		return string([]rune(name)[:l]) + mod[i+1:]
	}

	return name + mod
}
<|endoftext|>


<commit_before>package contnet

// Interest is a floating point value that represents the level of user's interest.
type Interest float64

// TopicInterest is a structure that represents the level of user's interest for specified topic.
type TopicInterest struct {
	Topic              Topic
	Interest           Interest
	CumulativeInterest Interest ``
}
type TopicInterestFactory struct{}

type TopicInterestDescription struct {
	Keyword1           Keyword
	Keyword2           Keyword
	Interest           Interest
	CumulativeInterest Interest
}

func (topicInterest *TopicInterest) Describe() *TopicInterestDescription {
	k1, k2 := topicInterest.Topic.GetKeywords()
	return &TopicInterestDescription{
		Keyword1:           k1,
		Keyword2:           k2,
		Interest:           topicInterest.Interest,
		CumulativeInterest: topicInterest.CumulativeInterest,
	}
}

func (factory *TopicInterestFactory) New(topic Topic, cumulativeInterest Interest) *TopicInterest {
	return &TopicInterest{
		Topic:              topic,
		CumulativeInterest: cumulativeInterest,
	}
}

func (topicInterest *TopicInterest) Clone() *TopicInterest {
	return Object.TopicInterest.New(topicInterest.Topic, topicInterest.Interest)
}

// Alias for a slice of pointers to topic interests.
type TopicInterests []*TopicInterest

func (topicInterests TopicInterests) Clone() TopicInterests {
	out := TopicInterests{}

	for i := 0; i < len(topicInterests); i++ {
		out = append(out, topicInterests[i].Clone())
	}

	return out
}

func (topicInterests TopicInterests) Describe() []*TopicInterestDescription {
	out := []*TopicInterestDescription{}

	for i := 0; i < len(topicInterests); i++ {
		out = append(out, topicInterests[i].Describe())
	}

	return out
}

const (
	__TRIM_INTEREST_LOWER_BOUND = 0.0001 // 0.001% interested
)

// value - between -1.0 and 1.0
func (topicInterests TopicInterests) Apply(topics Topics, value float64) TopicInterests {
	// foreach topic, add value to respective cumulative interest.
	// if no topic is registered, register it.
	// if cumulative interest falls below 0, remove interest
	interestSum := Interest(0.0)

	for i := 0; i < len(topics); i++ {
		found := false
		for j := 0; j < len(topicInterests); j++ {
			if topicInterests[j].Topic == *topics[i] {
				found = true
				topicInterests[j].CumulativeInterest += Interest(value)
				// if interest has become negative, remove interest (also if base interest is too low)
				if topicInterests[j].CumulativeInterest < 0 || topicInterests[j].Interest < __TRIM_INTEREST_LOWER_BOUND {
					topicInterests = append(topicInterests[:j], topicInterests[j+1:]...)
					break
				}

				interestSum += topicInterests[j].CumulativeInterest

				break
			}
		}
		// topic not found, create new interest if interest is positive, otherwise ignore it
		if !found && value > 0 {
			interest := Object.TopicInterest.New(*topics[i], Interest(value))
			topicInterests = append(topicInterests, interest)

			interestSum += Interest(value)
		}
	}

	// now recalculate base interests (proportion)
	for i := 0; i < len(topicInterests); i++ {
		topicInterests[i].Interest = topicInterests[i].CumulativeInterest / interestSum
	}

	return topicInterests
}
<commit_msg>Fairness factor<commit_after>package contnet

// Interest is a floating point value that represents the level of user's interest.
type Interest float64

// TopicInterest is a structure that represents the level of user's interest for specified topic.
type TopicInterest struct {
	Topic              Topic
	Interest           Interest
	CumulativeInterest Interest ``
}
type TopicInterestFactory struct{}

type TopicInterestDescription struct {
	Keyword1           Keyword
	Keyword2           Keyword
	Interest           Interest
	CumulativeInterest Interest
}

func (topicInterest *TopicInterest) Describe() *TopicInterestDescription {
	k1, k2 := topicInterest.Topic.GetKeywords()
	return &TopicInterestDescription{
		Keyword1:           k1,
		Keyword2:           k2,
		Interest:           topicInterest.Interest,
		CumulativeInterest: topicInterest.CumulativeInterest,
	}
}

func (factory *TopicInterestFactory) New(topic Topic, cumulativeInterest Interest) *TopicInterest {
	return &TopicInterest{
		Topic:              topic,
		CumulativeInterest: cumulativeInterest,
	}
}

func (topicInterest *TopicInterest) Clone() *TopicInterest {
	return Object.TopicInterest.New(topicInterest.Topic, topicInterest.Interest)
}

// Alias for a slice of pointers to topic interests.
type TopicInterests []*TopicInterest

func (topicInterests TopicInterests) Clone() TopicInterests {
	out := TopicInterests{}

	for i := 0; i < len(topicInterests); i++ {
		out = append(out, topicInterests[i].Clone())
	}

	return out
}

func (topicInterests TopicInterests) Describe() []*TopicInterestDescription {
	out := []*TopicInterestDescription{}

	for i := 0; i < len(topicInterests); i++ {
		out = append(out, topicInterests[i].Describe())
	}

	return out
}

const (
	__TRIM_INTEREST_LOWER_BOUND = 0.0001 // 0.001% interested
    __FAIRNESS_CONSTANT = 0.0015
)

// value - between -1.0 and 1.0
func (topicInterests TopicInterests) Apply(topics Topics, value float64) TopicInterests {
	// foreach topic, add value to respective cumulative interest.
	// if no topic is registered, register it.
	// if cumulative interest falls below 0, remove interest
	interestSum := Interest(0.0)

	for i := 0; i < len(topics); i++ {
		found := false
		for j := 0; j < len(topicInterests); j++ {
			if topicInterests[j].Topic == *topics[i] {
				found = true
				topicInterests[j].CumulativeInterest += Interest(value)
				// if interest has become negative, remove interest (also if base interest is too low)
				if topicInterests[j].CumulativeInterest < 0 || topicInterests[j].Interest < __TRIM_INTEREST_LOWER_BOUND {
					topicInterests = append(topicInterests[:j], topicInterests[j+1:]...)
					break
				}

				interestSum += topicInterests[j].CumulativeInterest

				break
			}
		}
		// topic not found, create new interest if interest is positive, otherwise ignore it
		if !found && value > 0 {
            // apply fairness factor to give newer entries a better chance for survival
            fair := value + __FAIRNESS_CONSTANT
			interest := Object.TopicInterest.New(*topics[i], Interest(fair))
			topicInterests = append(topicInterests, interest)

			interestSum += Interest(fair)
		}
	}

	// now recalculate base interests (proportion)
	for i := 0; i < len(topicInterests); i++ {
		topicInterests[i].Interest = topicInterests[i].CumulativeInterest / interestSum
	}

	return topicInterests
}
<|endoftext|>


<commit_before>package gofakes3

import (
	"encoding/base64"
	"fmt"
	"net/http"
	"strings"
)

// routeBase is a http.HandlerFunc that dispatches top level routes for
// GoFakeS3.
//
// URLs are assumed to break down into two common path segments, in the
// following format:
//   /<bucket>/<object>
//
// The operation for most of the core functionality is built around HTTP
// verbs, but outside the core functionality, the clean separation starts
// to degrade, especially around multipart uploads.
//
func (g *GoFakeS3) routeBase(w http.ResponseWriter, r *http.Request) {
	var (
		path   = strings.Trim(r.URL.Path, "/")
		parts  = strings.SplitN(path, "/", 2)
		bucket = parts[0]
		query  = r.URL.Query()
		object = ""
		err    error
	)

	hdr := w.Header()

	id := fmt.Sprintf("%016X", g.nextRequestID())
	hdr.Set("x-amz-id-2", base64.StdEncoding.EncodeToString([]byte(id+id+id+id))) // x-amz-id-2 is 48 bytes of random stuff
	hdr.Set("x-amz-request-id", id)
	hdr.Set("Server", "AmazonS3")

	if len(parts) == 2 {
		object = parts[1]
	}

	if uploadID := UploadID(query.Get("uploadId")); uploadID != "" {
		err = g.routeMultipartUpload(bucket, object, uploadID, w, r)

	} else if _, ok := query["uploads"]; ok {
		err = g.routeMultipartUploadBase(bucket, object, w, r)

	} else if _, ok := query["versioning"]; ok {
		err = g.routeVersioning(bucket, w, r)

	} else if _, ok := query["versions"]; ok {
		err = g.routeVersions(bucket, w, r)

	} else if versionID, ok := query["versionId"]; ok {
		err = g.routeVersion(bucket, object, VersionID(versionID[0]), w, r)

	} else if bucket != "" && object != "" {
		err = g.routeObject(bucket, object, w, r)

	} else if bucket != "" {
		err = g.routeBucket(bucket, w, r)

	} else if r.Method == "GET" {
		err = g.listBuckets(w, r)

	} else {
		http.NotFound(w, r)
		return
	}

	if err != nil {
		g.httpError(w, r, err)
	}
}

// routeObject oandles URLs that contain both a bucket path segment and an
// object path segment.
func (g *GoFakeS3) routeObject(bucket, object string, w http.ResponseWriter, r *http.Request) (err error) {
	switch r.Method {
	case "GET":
		return g.getObject(bucket, object, "", w, r)
	case "HEAD":
		return g.headObject(bucket, object, "", w, r)
	case "PUT":
		return g.createObject(bucket, object, w, r)
	case "DELETE":
		return g.deleteObject(bucket, object, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeBucket handles URLs that contain only a bucket path segment, not an
// object path segment.
func (g *GoFakeS3) routeBucket(bucket string, w http.ResponseWriter, r *http.Request) (err error) {
	switch r.Method {
	case "GET":
		return g.listBucket(bucket, w, r)
	case "PUT":
		return g.createBucket(bucket, w, r)
	case "DELETE":
		return g.deleteBucket(bucket, w, r)
	case "HEAD":
		return g.headBucket(bucket, w, r)
	case "POST":
		if _, ok := r.URL.Query()["delete"]; ok {
			return g.deleteMulti(bucket, w, r)
		} else {
			return g.createObjectBrowserUpload(bucket, w, r)
		}
	default:
		return ErrMethodNotAllowed
	}
}

// routeMultipartUploadBase operates on routes that contain '?uploads' in the
// query string. These routes may or may not have a value for bucket or object;
// this is validated and handled in the target handler functions.
func (g *GoFakeS3) routeMultipartUploadBase(bucket, object string, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.listMultipartUploads(bucket, w, r)
	case "POST":
		return g.initiateMultipartUpload(bucket, object, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeVersioningBase operates on routes that contain '?versioning' in the
// query string. These routes may or may not have a value for bucket; this is
// validated and handled in the target handler functions.
func (g *GoFakeS3) routeVersioning(bucket string, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.getBucketVersioning(bucket, w, r)
	case "PUT":
		return g.putBucketVersioning(bucket, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeVersions operates on routes that contain '?versions' in the query string.
func (g *GoFakeS3) routeVersions(bucket string, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.listBucketVersions(bucket, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeVersion operates on routes that contain '?versionId=<id>' in the
// query string.
func (g *GoFakeS3) routeVersion(bucket, object string, versionID VersionID, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.getObject(bucket, object, versionID, w, r)
	case "HEAD":
		return g.headObject(bucket, object, versionID, w, r)
	case "DELETE":
		return g.deleteObjectVersion(bucket, object, versionID, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeMultipartUpload operates on routes that contain '?uploadId=<id>' in the
// query string.
func (g *GoFakeS3) routeMultipartUpload(bucket, object string, uploadID UploadID, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.listMultipartUploadParts(bucket, object, uploadID, w, r)
	case "PUT":
		return g.putMultipartUploadPart(bucket, object, uploadID, w, r)
	case "DELETE":
		return g.abortMultipartUpload(bucket, object, uploadID, w, r)
	case "POST":
		return g.completeMultipartUpload(bucket, object, uploadID, w, r)
	default:
		return ErrMethodNotAllowed
	}
}
<commit_msg>Handle 'null' version ID input<commit_after>package gofakes3

import (
	"encoding/base64"
	"fmt"
	"net/http"
	"strings"
)

// routeBase is a http.HandlerFunc that dispatches top level routes for
// GoFakeS3.
//
// URLs are assumed to break down into two common path segments, in the
// following format:
//   /<bucket>/<object>
//
// The operation for most of the core functionality is built around HTTP
// verbs, but outside the core functionality, the clean separation starts
// to degrade, especially around multipart uploads.
//
func (g *GoFakeS3) routeBase(w http.ResponseWriter, r *http.Request) {
	var (
		path   = strings.Trim(r.URL.Path, "/")
		parts  = strings.SplitN(path, "/", 2)
		bucket = parts[0]
		query  = r.URL.Query()
		object = ""
		err    error
	)

	hdr := w.Header()

	id := fmt.Sprintf("%016X", g.nextRequestID())
	hdr.Set("x-amz-id-2", base64.StdEncoding.EncodeToString([]byte(id+id+id+id))) // x-amz-id-2 is 48 bytes of random stuff
	hdr.Set("x-amz-request-id", id)
	hdr.Set("Server", "AmazonS3")

	if len(parts) == 2 {
		object = parts[1]
	}

	if uploadID := UploadID(query.Get("uploadId")); uploadID != "" {
		err = g.routeMultipartUpload(bucket, object, uploadID, w, r)

	} else if _, ok := query["uploads"]; ok {
		err = g.routeMultipartUploadBase(bucket, object, w, r)

	} else if _, ok := query["versioning"]; ok {
		err = g.routeVersioning(bucket, w, r)

	} else if _, ok := query["versions"]; ok {
		err = g.routeVersions(bucket, w, r)

	} else if versionID := versionFromQuery(query["versionId"]); versionID != "" {
		err = g.routeVersion(bucket, object, VersionID(versionID), w, r)

	} else if bucket != "" && object != "" {
		err = g.routeObject(bucket, object, w, r)

	} else if bucket != "" {
		err = g.routeBucket(bucket, w, r)

	} else if r.Method == "GET" {
		err = g.listBuckets(w, r)

	} else {
		http.NotFound(w, r)
		return
	}

	if err != nil {
		g.httpError(w, r, err)
	}
}

// routeObject oandles URLs that contain both a bucket path segment and an
// object path segment.
func (g *GoFakeS3) routeObject(bucket, object string, w http.ResponseWriter, r *http.Request) (err error) {
	switch r.Method {
	case "GET":
		return g.getObject(bucket, object, "", w, r)
	case "HEAD":
		return g.headObject(bucket, object, "", w, r)
	case "PUT":
		return g.createObject(bucket, object, w, r)
	case "DELETE":
		return g.deleteObject(bucket, object, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeBucket handles URLs that contain only a bucket path segment, not an
// object path segment.
func (g *GoFakeS3) routeBucket(bucket string, w http.ResponseWriter, r *http.Request) (err error) {
	switch r.Method {
	case "GET":
		return g.listBucket(bucket, w, r)
	case "PUT":
		return g.createBucket(bucket, w, r)
	case "DELETE":
		return g.deleteBucket(bucket, w, r)
	case "HEAD":
		return g.headBucket(bucket, w, r)
	case "POST":
		if _, ok := r.URL.Query()["delete"]; ok {
			return g.deleteMulti(bucket, w, r)
		} else {
			return g.createObjectBrowserUpload(bucket, w, r)
		}
	default:
		return ErrMethodNotAllowed
	}
}

// routeMultipartUploadBase operates on routes that contain '?uploads' in the
// query string. These routes may or may not have a value for bucket or object;
// this is validated and handled in the target handler functions.
func (g *GoFakeS3) routeMultipartUploadBase(bucket, object string, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.listMultipartUploads(bucket, w, r)
	case "POST":
		return g.initiateMultipartUpload(bucket, object, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeVersioningBase operates on routes that contain '?versioning' in the
// query string. These routes may or may not have a value for bucket; this is
// validated and handled in the target handler functions.
func (g *GoFakeS3) routeVersioning(bucket string, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.getBucketVersioning(bucket, w, r)
	case "PUT":
		return g.putBucketVersioning(bucket, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeVersions operates on routes that contain '?versions' in the query string.
func (g *GoFakeS3) routeVersions(bucket string, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.listBucketVersions(bucket, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeVersion operates on routes that contain '?versionId=<id>' in the
// query string.
func (g *GoFakeS3) routeVersion(bucket, object string, versionID VersionID, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.getObject(bucket, object, versionID, w, r)
	case "HEAD":
		return g.headObject(bucket, object, versionID, w, r)
	case "DELETE":
		return g.deleteObjectVersion(bucket, object, versionID, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

// routeMultipartUpload operates on routes that contain '?uploadId=<id>' in the
// query string.
func (g *GoFakeS3) routeMultipartUpload(bucket, object string, uploadID UploadID, w http.ResponseWriter, r *http.Request) error {
	switch r.Method {
	case "GET":
		return g.listMultipartUploadParts(bucket, object, uploadID, w, r)
	case "PUT":
		return g.putMultipartUploadPart(bucket, object, uploadID, w, r)
	case "DELETE":
		return g.abortMultipartUpload(bucket, object, uploadID, w, r)
	case "POST":
		return g.completeMultipartUpload(bucket, object, uploadID, w, r)
	default:
		return ErrMethodNotAllowed
	}
}

func versionFromQuery(qv []string) string {
	// The versionId subresource may be the string 'null'; this has been
	// observed coming in via Boto. The S3 documentation for the "DELETE
	// object" endpoint describes a 'null' version explicitly, but we don't
	// want backend implementers to have to special-case this string, so
	// let's hide it in here:
	if len(qv) > 0 && qv[0] != "" && qv[0] != "null" {
		return qv[0]
	}
	return ""
}
<|endoftext|>


<commit_before>package main

import "fmt"
import "github.com/stianeikeland/go-rpio"
import "os"
import "time"

var pin = rpio.Pin(4)

func main() {
	fmt.Println("Opening rpio access")

	var err = rpio.Open()

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer rpio.Close()

	fmt.Println("Pin as output")
	pin.Output()

	fmt.Println("Number of blinks?")
	var num int
	_, err := fmt.Scanf("%d", &i)
	if err != nil {
		fmt.Println("not a number")
	} else {
		fmt.Print(i)
		fmt.Println(" is a number")
	}

	for i := 0; i < i; i++ {
		fmt.Println("Toggle")
		pin.Toggle()
		time.Sleep(time.Second)
	}
}
<commit_msg>Fixed typo<commit_after>package main

import "fmt"
import "github.com/stianeikeland/go-rpio"
import "os"
import "time"

var pin = rpio.Pin(4)

func main() {
	fmt.Println("Opening rpio access")

	var err = rpio.Open()

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer rpio.Close()

	fmt.Println("Pin as output")
	pin.Output()

	fmt.Println("Number of blinks?")
	var num int
	_, err := fmt.Scanf("%d", &num)
	if err != nil {
		fmt.Println("not a number")
	} else {
		fmt.Print(i)
		fmt.Println(" is a number")
	}

	for i := 0; i < num; i++ {
		fmt.Println("Toggle")
		pin.Toggle()
		time.Sleep(time.Second)
	}
}
<|endoftext|>