Exemple #1
0
package models_test

import (
	"encoding/json"
	"fmt"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/cloudfoundry-incubator/bbs/models"
)

var _ = Describe("Actions", func() {
	itSerializes := func(actionPayload string, a *models.Action) {
		action := models.UnwrapAction(a)
		It("Action -> JSON for "+string(action.ActionType()), func() {
			marshalledAction := action
			json, err := json.Marshal(&marshalledAction)
			Expect(err).NotTo(HaveOccurred())
			Expect(json).To(MatchJSON(actionPayload))
		})
	}

	itDeserializes := func(actionPayload string, a *models.Action) {
		action := models.UnwrapAction(a)
		It("JSON -> Action for "+string(action.ActionType()), func() {
			wrappedJSON := fmt.Sprintf(`{"%s":%s}`, action.ActionType(), actionPayload)
			marshalledAction := new(models.Action)
			err := json.Unmarshal([]byte(wrappedJSON), marshalledAction)
			Expect(err).NotTo(HaveOccurred())
func (appRunner *appRunner) desireLrp(params CreateAppParams) error {
	primaryPort := route_helpers.GetPrimaryPort(params.Monitor.Port, params.ExposedPorts)

	private, public, err := appRunner.keygen.GenerateRSAKeyPair(2048)
	if err != nil {
		return err
	}

	routes := appRunner.buildRoutesWithDefaults(params, primaryPort)
	routes.DiegoSSHRoute = &route_helpers.DiegoSSHRoute{
		Port:       2222,
		PrivateKey: private,
	}

	vcapAppURIs := []string{}
	for _, route := range routes.AppRoutes {
		vcapAppURIs = append(vcapAppURIs, route.Hostnames...)
	}

	vcapApplication := struct {
		ApplicationName string   `json:"application_name"`
		ApplicationURIs []string `json:"application_uris"`
		Name            string   `json:"name"`
		URIs            []string `json:"uris"`
		Limits          struct {
			Disk   int `json:"disk,omitempty"`
			Memory int `json:"mem,omitempty"`
		} `json:"limits,omitempty"`
	}{}

	vcapApplication.ApplicationName = params.Name
	vcapApplication.Name = params.Name
	vcapApplication.ApplicationURIs = vcapAppURIs
	vcapApplication.URIs = vcapAppURIs
	vcapApplication.Limits.Disk = params.DiskMB
	vcapApplication.Limits.Memory = params.MemoryMB

	vcapAppBytes, err := json.Marshal(vcapApplication)
	if err != nil {
		return err
	}

	envVars := buildEnvironmentVariables(params.EnvironmentVariables)
	envVars = append(envVars, receptor.EnvironmentVariable{Name: "VCAP_APPLICATION", Value: string(vcapAppBytes)})
	envVars = append(envVars, receptor.EnvironmentVariable{Name: "PORT", Value: fmt.Sprintf("%d", primaryPort)})

	if _, exists := params.EnvironmentVariables["VCAP_SERVICES"]; !exists {
		envVars = append(envVars, receptor.EnvironmentVariable{Name: "VCAP_SERVICES", Value: "{}"})
	}

	setupAction := models.WrapAction(&models.SerialAction{
		Actions: []*models.Action{
			params.Setup,
			models.WrapAction(&models.DownloadAction{
				From: "http://file_server.service.dc1.consul:8080/v1/static/diego-sshd.tgz",
				To:   "/tmp",
				User: "******",
			}),
		},
	})

	hostKey, err := appRunner.keygen.GenerateRSAPrivateKey(2048)
	if err != nil {
		return err
	}

	req := receptor.DesiredLRPCreateRequest{
		ProcessGuid:          params.Name,
		Domain:               lrpDomain,
		RootFS:               params.RootFS,
		Instances:            params.Instances,
		Routes:               routes.RoutingInfo(),
		CPUWeight:            params.CPUWeight,
		MemoryMB:             params.MemoryMB,
		DiskMB:               params.DiskMB,
		Privileged:           params.Privileged,
		Ports:                append(params.ExposedPorts, 2222),
		LogGuid:              params.Name,
		LogSource:            "APP",
		MetricsGuid:          params.Name,
		EnvironmentVariables: envVars,
		Annotation:           params.Annotation,
		Setup:                models.UnwrapAction(setupAction),
		Action: &models.ParallelAction{
			Actions: []*models.Action{
				models.WrapAction(&models.RunAction{
					Path: "/tmp/diego-sshd",
					Args: []string{
						"-address=0.0.0.0:2222",
						fmt.Sprintf("-authorizedKey=%s", public),
						fmt.Sprintf("-hostKey=%s", hostKey),
					},
					Dir:  "/tmp",
					User: params.User,
				}),
				models.WrapAction(&models.RunAction{
					Path: params.StartCommand,
					Args: params.AppArgs,
					Dir:  params.WorkingDir,
					User: params.User,
				}),
			},
		},
	}

	var healthCheckArgs []string
	if params.Monitor.Timeout != 0 {
		healthCheckArgs = append(healthCheckArgs, "-timeout", fmt.Sprint(params.Monitor.Timeout))
	}
	switch params.Monitor.Method {
	case PortMonitor:
		req.Monitor = &models.RunAction{
			Path:      "/tmp/healthcheck",
			Args:      append(healthCheckArgs, "-port", fmt.Sprint(params.Monitor.Port)),
			LogSource: "HEALTH",
			User:      params.User,
		}
	case URLMonitor:
		req.Monitor = &models.RunAction{
			Path:      "/tmp/healthcheck",
			Args:      append(healthCheckArgs, "-port", fmt.Sprint(params.Monitor.Port), "-uri", params.Monitor.URI),
			LogSource: "HEALTH",
			User:      params.User,
		}
	case CustomMonitor:
		req.Monitor = &models.RunAction{
			Path:      "/bin/sh",
			Args:      []string{"-c", params.Monitor.CustomCommand},
			LogSource: "HEALTH",
			User:      params.User,
		}
	}

	return appRunner.receptorClient.CreateDesiredLRP(req)
}