<commit_before>package server

import (
	"fmt"
	"net"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"

	klatest "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
	kmaster "github.com/GoogleCloudPlatform/kubernetes/pkg/master"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
	etcdclient "github.com/coreos/go-etcd/etcd"
	"github.com/golang/glog"
	"github.com/spf13/cobra"

	"github.com/openshift/origin/pkg/cmd/flagtypes"
	"github.com/openshift/origin/pkg/cmd/server/etcd"
	"github.com/openshift/origin/pkg/cmd/server/kubernetes"
	"github.com/openshift/origin/pkg/cmd/server/origin"
	"github.com/openshift/origin/pkg/cmd/util"
	"github.com/openshift/origin/pkg/cmd/util/docker"
)

const longCommandDesc = `
Start an OpenShift server

This command helps you launch an OpenShift server. The default mode is all-in-one, which allows
you to run all of the components of an OpenShift system on a server with Docker. Running

    $ openshift start

will start OpenShift 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.

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 must pass a list of the node
      hostnames you want to watch (limitation).

    $ openshift start node --master masterIP

      Launches a new node and attempts to connect to the master on the provided IP.

You may also pass --etcd to connect to an external etcd server instead of running an integrated
instance.
`

// config is a struct that the command stores flag values into.
type config struct {
	Docker *docker.Helper

	MasterAddr flagtypes.Addr
	BindAddr   flagtypes.Addr
	EtcdAddr   flagtypes.Addr

	Hostname  string
	VolumeDir string

	EtcdDir string

	StorageVersion string

	NodeList flagtypes.StringList

	CORSAllowedOrigins flagtypes.StringList
}

func NewCommandStartServer(name string) *cobra.Command {
	cfg := &config{
		Docker:     docker.NewHelper(),
		MasterAddr: flagtypes.Addr{Value: "localhost:8080", DefaultScheme: "http", DefaultPort: 8080, AllowPrefix: true}.Default(),
		BindAddr:   flagtypes.Addr{Value: "0.0.0.0:8080", DefaultScheme: "http", DefaultPort: 8080, AllowPrefix: true}.Default(),
		EtcdAddr:   flagtypes.Addr{Value: "0.0.0.0:4001", DefaultScheme: "http", DefaultPort: 4001}.Default(),
		NodeList:   flagtypes.StringList{"127.0.0.1"},
	}

	cmd := &cobra.Command{
		Use:   fmt.Sprintf("%s [master|node]", name),
		Short: "Launch OpenShift",
		Long:  longCommandDesc,
		Run: func(c *cobra.Command, args []string) {
			if len(args) > 1 {
				glog.Fatalf("You may start an OpenShift all-in-one server with no arguments, or pass 'master' or 'node' to run in that role.")
			}

			var startEtcd, startNode, startMaster bool
			if len(args) == 1 {
				switch args[0] {
				case "master":
					startMaster = true
					startEtcd = !cfg.EtcdAddr.Provided
					defaultMasterAddress(cfg)
					glog.Infof("Starting an OpenShift master, reachable at %s (etcd: %s)", cfg.MasterAddr.String(), cfg.EtcdAddr.String())

				case "node":
					startNode = true
					defaultMasterAddress(cfg)
					glog.Infof("Starting an OpenShift node, connecting to %s (etcd: %s)", cfg.MasterAddr.String(), cfg.EtcdAddr.String())

				default:
					glog.Fatalf("You may start an OpenShift all-in-one server with no arguments, or pass 'master' or 'node' to run in that role.")
				}

			} else {
				startMaster = true
				startEtcd = !cfg.EtcdAddr.Provided
				startNode = true
				defaultMasterAddress(cfg)

				glog.Infof("Starting an OpenShift all-in-one, reachable at %s (etcd: %s)", cfg.MasterAddr.String(), cfg.EtcdAddr.String())
			}

			if startMaster {
				// update the node list to include the default node
				if len(cfg.Hostname) == 0 {
					cfg.Hostname = defaultHostname()
				}
				if len(cfg.NodeList) == 1 && cfg.NodeList[0] == "127.0.0.1" {
					cfg.NodeList[0] = cfg.Hostname
				}
				glog.Infof("Expecting %d nodes: %v", len(cfg.NodeList), cfg.NodeList)

				if startEtcd {
					etcdConfig := &etcd.Config{
						BindAddr:   cfg.EtcdAddr.URL.Host,
						MasterAddr: cfg.EtcdAddr.URL.Host,
						EtcdDir:    cfg.EtcdDir,
					}
					etcdConfig.Run()
				}

				// Connect and setup etcd interfaces
				etcdClient := getEtcdClient(cfg)
				etcdHelper, err := origin.NewEtcdHelper(cfg.StorageVersion, etcdClient)
				if err != nil {
					glog.Errorf("Error setting up server storage: %v", err)
				}
				ketcdHelper, err := kmaster.NewEtcdHelper(etcdClient.GetCluster(), klatest.Version)
				if err != nil {
					glog.Errorf("Error setting up Kubernetes server storage: %v", err)
				}

				assetAddr := net.JoinHostPort(cfg.MasterAddr.Host, "8091")

				osmaster := &origin.MasterConfig{
					BindAddr:   cfg.BindAddr.URL.Host,
					MasterAddr: cfg.MasterAddr.URL.String(),
					AssetAddr:  assetAddr,
					EtcdHelper: etcdHelper,
				}
				osmaster.EnsureKubernetesClient()
				osmaster.EnsureOpenShiftClient()
				osmaster.EnsureCORSAllowedOrigins(cfg.CORSAllowedOrigins)

				kmaster := &kubernetes.MasterConfig{
					NodeHosts:  cfg.NodeList,
					EtcdHelper: ketcdHelper,
					KubeClient: osmaster.KubeClient,
				}

				osmaster.RunAPI(kmaster)
				kmaster.RunScheduler()
				kmaster.RunReplicationController()
			}

			if startNode {
				etcdClient := getEtcdClient(cfg)

				hostname := cfg.Hostname
				if len(hostname) == 0 {
					hostname = defaultHostname()
				}

				nodeConfig := &kubernetes.NodeConfig{
					BindHost:   cfg.BindAddr.Host,
					NodeHost:   hostname,
					MasterHost: cfg.MasterAddr.URL.String(),

					VolumeDir: cfg.VolumeDir,

					NetworkContainerImage: env("KUBERNETES_NETWORK_CONTAINER_IMAGE", kubelet.NetworkContainerImage),

					EtcdClient: etcdClient,
				}

				nodeConfig.EnsureVolumeDir()<commit_msg>Added missing controllers in server start<commit_after>package server

import (
	"fmt"
	"net"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"time"

	klatest "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
	kmaster "github.com/GoogleCloudPlatform/kubernetes/pkg/master"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/tools"
	etcdclient "github.com/coreos/go-etcd/etcd"
	"github.com/golang/glog"
	"github.com/spf13/cobra"

	"github.com/openshift/origin/pkg/cmd/flagtypes"
	"github.com/openshift/origin/pkg/cmd/server/etcd"
	"github.com/openshift/origin/pkg/cmd/server/kubernetes"
	"github.com/openshift/origin/pkg/cmd/server/origin"
	"github.com/openshift/origin/pkg/cmd/util"
	"github.com/openshift/origin/pkg/cmd/util/docker"
)

const longCommandDesc = `
Start an OpenShift server

This command helps you launch an OpenShift server. The default mode is all-in-one, which allows
you to run all of the components of an OpenShift system on a server with Docker. Running

    $ openshift start

will start OpenShift 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.

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 must pass a list of the node
      hostnames you want to watch (limitation).

    $ openshift start node --master masterIP

      Launches a new node and attempts to connect to the master on the provided IP.

You may also pass --etcd to connect to an external etcd server instead of running an integrated
instance.
`

// config is a struct that the command stores flag values into.
type config struct {
	Docker *docker.Helper

	MasterAddr flagtypes.Addr
	BindAddr   flagtypes.Addr
	EtcdAddr   flagtypes.Addr

	Hostname  string
	VolumeDir string

	EtcdDir string

	StorageVersion string

	NodeList flagtypes.StringList

	CORSAllowedOrigins flagtypes.StringList
}

func NewCommandStartServer(name string) *cobra.Command {
	cfg := &config{
		Docker:     docker.NewHelper(),
		MasterAddr: flagtypes.Addr{Value: "localhost:8080", DefaultScheme: "http", DefaultPort: 8080, AllowPrefix: true}.Default(),
		BindAddr:   flagtypes.Addr{Value: "0.0.0.0:8080", DefaultScheme: "http", DefaultPort: 8080, AllowPrefix: true}.Default(),
		EtcdAddr:   flagtypes.Addr{Value: "0.0.0.0:4001", DefaultScheme: "http", DefaultPort: 4001}.Default(),
		NodeList:   flagtypes.StringList{"127.0.0.1"},
	}

	cmd := &cobra.Command{
		Use:   fmt.Sprintf("%s [master|node]", name),
		Short: "Launch OpenShift",
		Long:  longCommandDesc,
		Run: func(c *cobra.Command, args []string) {
			if len(args) > 1 {
				glog.Fatalf("You may start an OpenShift all-in-one server with no arguments, or pass 'master' or 'node' to run in that role.")
			}

			var startEtcd, startNode, startMaster bool
			if len(args) == 1 {
				switch args[0] {
				case "master":
					startMaster = true
					startEtcd = !cfg.EtcdAddr.Provided
					defaultMasterAddress(cfg)
					glog.Infof("Starting an OpenShift master, reachable at %s (etcd: %s)", cfg.MasterAddr.String(), cfg.EtcdAddr.String())

				case "node":
					startNode = true
					defaultMasterAddress(cfg)
					glog.Infof("Starting an OpenShift node, connecting to %s (etcd: %s)", cfg.MasterAddr.String(), cfg.EtcdAddr.String())

				default:
					glog.Fatalf("You may start an OpenShift all-in-one server with no arguments, or pass 'master' or 'node' to run in that role.")
				}

			} else {
				startMaster = true
				startEtcd = !cfg.EtcdAddr.Provided
				startNode = true
				defaultMasterAddress(cfg)

				glog.Infof("Starting an OpenShift all-in-one, reachable at %s (etcd: %s)", cfg.MasterAddr.String(), cfg.EtcdAddr.String())
			}

			if startMaster {
				// update the node list to include the default node
				if len(cfg.Hostname) == 0 {
					cfg.Hostname = defaultHostname()
				}
				if len(cfg.NodeList) == 1 && cfg.NodeList[0] == "127.0.0.1" {
					cfg.NodeList[0] = cfg.Hostname
				}
				glog.Infof("Expecting %d nodes: %v", len(cfg.NodeList), cfg.NodeList)

				if startEtcd {
					etcdConfig := &etcd.Config{
						BindAddr:   cfg.EtcdAddr.URL.Host,
						MasterAddr: cfg.EtcdAddr.URL.Host,
						EtcdDir:    cfg.EtcdDir,
					}
					etcdConfig.Run()
				}

				// Connect and setup etcd interfaces
				etcdClient := getEtcdClient(cfg)
				etcdHelper, err := origin.NewEtcdHelper(cfg.StorageVersion, etcdClient)
				if err != nil {
					glog.Errorf("Error setting up server storage: %v", err)
				}
				ketcdHelper, err := kmaster.NewEtcdHelper(etcdClient.GetCluster(), klatest.Version)
				if err != nil {
					glog.Errorf("Error setting up Kubernetes server storage: %v", err)
				}

				assetAddr := net.JoinHostPort(cfg.MasterAddr.Host, "8091")

				osmaster := &origin.MasterConfig{
					BindAddr:   cfg.BindAddr.URL.Host,
					MasterAddr: cfg.MasterAddr.URL.String(),
					AssetAddr:  assetAddr,
					EtcdHelper: etcdHelper,
				}
				osmaster.EnsureKubernetesClient()
				osmaster.EnsureOpenShiftClient()
				osmaster.EnsureCORSAllowedOrigins(cfg.CORSAllowedOrigins)

				kmaster := &kubernetes.MasterConfig{
					NodeHosts:  cfg.NodeList,
					EtcdHelper: ketcdHelper,
					KubeClient: osmaster.KubeClient,
				}

				osmaster.RunAPI(kmaster)
				osmaster.RunAssetServer()
				osmaster.RunBuildController()
				osmaster.RunDeploymentController()

				kmaster.RunScheduler()
				kmaster.RunReplicationController()
			}

			if startNode {
				etcdClient := getEtcdClient(cfg)

				hostname := cfg.Hostname
				if len(hostname) == 0 {
					hostname = defaultHostname()
				}

				nodeConfig := &kubernetes.NodeConfig{
					BindHost:   cfg.BindAddr.Host,
					NodeHost:   hostname,
					MasterHost: cfg.MasterAddr.URL.String(),

					VolumeDir: cfg.VolumeDir,

					NetworkContainerImage: env("KUBERNETES_NETWORK_CONTAINER_IMAGE", kubelet.NetworkContainerImage),

					EtcdClient: etcdClient,
				}

				nodeConfig.EnsureVolumeDir()
{'fraction_non_alphanumeric': 0.10948007216663933, 'fraction_numerical': 0.010004920452681646, 'mean_word_length': 3.596683000376932, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3479489', 'n_tokens_mistral': 4737, 'n_tokens_neox': 3925, 'n_words': 1019}