示例#1
0
// NewServer returns a new instance of Server.
func NewServer() (server *Server, err error) {
	if DisableAPIServerFlag.Get() {
		return nil, nil
	}
	server = new(Server)
	server.leverClient, err = leverapi.NewClient()
	if err != nil {
		return nil, err
	}
	// Pipe Lever calls into own host listener.
	server.leverClient.ForceHost =
		"127.0.0.1:" + host.EnvExtListenPortFlag.Get()
	server.httpServer = &http.Server{
		Addr:    ":" + ListenPortFlag.Get(),
		Handler: server,
	}
	go server.listenAndServe()
	return server, nil
}
示例#2
0
func TestMain(m *testing.M) {
	config.Initialize()
	leverutil.UpdateLoggingSettings()

	logger.Info("Deploying...")
	err := admin.DeployServiceDir(
		core.AdminEnvFlag.Get(), core.DefaultDevAliasFlag.Get(),
		core.DefaultDevEnvFlag.Get(), serviceName)
	if err != nil {
		logger.WithFields("err", err).Fatal("Error when deploying")
	}
	logger.Info("Deployed")
	client, err = api.NewClient()
	if err != nil {
		logger.WithFields("err", err).Fatal("Error getting client")
	}
	client.ForceHost = core.DefaultDevAliasFlag.Get()
	leverService = client.Service(core.DefaultDevEnvFlag.Get(), serviceName)
	logger.Info("Running tests...")
	os.Exit(m.Run())
}
示例#3
0
// DeployServiceDir deploys provided dir onto Lever, as a Lever service.
func DeployServiceDir(
	adminEnv string, host string, env string, srcDir string) error {
	client, err := api.NewClient()
	if err != nil {
		return err
	}
	if host != "" {
		client.ForceHost = host
	}

	info, err := os.Lstat(srcDir)
	if err != nil {
		return fmt.Errorf("Error getting info about source dir: %v", err)
	}
	if !info.IsDir() {
		return fmt.Errorf("Source is not a directory")
	}

	_, err = core.ReadLeverConfig(srcDir)
	if err != nil {
		return fmt.Errorf("Error reading lever.json: %v", err)
	}

	pipeReader, pipeWriter := io.Pipe()
	bufReader := bufio.NewReader(pipeReader)
	errCh := make(chan error)
	go func() {
		tarErr := leverutil.Tar(pipeWriter, srcDir)
		if tarErr != nil {
			errCh <- fmt.Errorf("Error trying to tar dir: %v", tarErr)
			return
		}
		tarErr = pipeWriter.Close()
		if tarErr != nil {
			errCh <- tarErr
		}
		close(errCh)
	}()

	endpoint := client.Service(adminEnv, "admin")
	stream, err := endpoint.InvokeChan("DeployServiceChan", env)
	if err != nil {
		return fmt.Errorf("Error invoking DeployServiceChan: %v", err)
	}

	chunk := make([]byte, 32*1024)
	for {
		var size int
		size, err = bufReader.Read(chunk)
		if err == io.EOF {
			break
		}
		if err != nil {
			stream.Close()
			return err
		}

		err = stream.Send(chunk[:size])
		if err != nil {
			stream.Close()
			return err
		}
	}
	err = stream.Close()
	if err != nil {
		return err
	}
	err, hasErr := <-errCh
	if hasErr && err != nil {
		return err
	}
	// Wait for remote to close stream.
	var msg interface{}
	err = stream.Receive(&msg)
	if err == nil {
		return fmt.Errorf("Unexpected message received")
	}
	if err != nil && err != io.EOF {
		return err
	}
	return nil
}
示例#4
0
func actionStream(ctx *cli.Context) {
	leverURL, err := core.ParseLeverURL(ctx.Args().Get(0))
	if err != nil {
		logger.WithFields("err", err).Fatal("Invalid lever URL")
	}
	if leverURL.Environment == "" {
		leverURL.Environment = flagEnv
	}

	client, err := leverapi.NewClient()
	if err != nil {
		logger.WithFields("err", err).Fatal("Error creating client")
	}
	if strings.HasSuffix(leverURL.Environment, ".lever") {
		client.ForceHost = detectLeverOSIPPort()
	}
	if flagHost != "" {
		client.ForceHost = flagHost
	}

	var args []interface{}
	for index := 1; index < ctx.NArg(); index++ {
		rawArg := ctx.Args().Get(index)
		var arg interface{}
		err = json.Unmarshal([]byte(rawArg), &arg)
		if err != nil {
			logger.WithFields("err", err, "jsonArg", rawArg).Fatal(
				"Error parsing JSON arg")
		}
		args = append(args, arg)
	}
	stream, err := client.InvokeChanURL(leverURL.String(), args...)
	if err != nil {
		logger.WithFields("err", err).Fatal("Invokation error")
	}

	receiveDone := make(chan struct{})
	go func() {
		stdout := bufio.NewWriter(os.Stdout)

		for {
			var msg interface{}
			err = stream.Receive(&msg)
			if err == io.EOF {
				break
			}
			if err != nil {
				logger.WithFields("err", err).Fatal(
					"Error receiving from server")
			}

			msgBytes, isBytes := msg.([]byte)
			if isBytes {
				_, err = stdout.Write(msgBytes)
				if err != nil {
					logger.WithFields("err", err).Fatal(
						"Error writing to stdout")
				}
			} else {
				var jsonMsg []byte
				if flagPrettyPrint {
					jsonMsg, err = json.MarshalIndent(msg, "", "  ")
				} else {
					jsonMsg, err = json.Marshal(msg)
				}
				if err != nil {
					logger.WithFields("err", err).Fatal(
						"Error turning receiving msg into JSON")
				}
				stdout.Write(jsonMsg)
				stdout.WriteString("\n")
			}
		}

		err = stdout.Flush()
		if err != nil {
			logger.WithFields("err", err).Fatal("Error flushing stdout")
		}
		close(receiveDone)
	}()

	stdin := bufio.NewReader(os.Stdin)
	if flagBytes {
		chunk := make([]byte, 32*1024)
		for {
			var size int
			size, err = stdin.Read(chunk)
			if err == io.EOF {
				break
			}
			if err != nil {
				logger.WithFields("err", err).Fatal("Error reading from stdin")
			}

			err = stream.Send(chunk[:size])
			if err != nil {
				logger.WithFields("err", err).Fatal("Error sending chunk")
			}
		}
	} else {
		for {
			var line string
			line, err = stdin.ReadString('\n')
			if err == io.EOF && line == "" {
				break
			}
			if err != nil && err != io.EOF {
				logger.WithFields("err", err).Fatal("Error reading from stdin")
			}

			var jsonMsg interface{}
			err = json.Unmarshal([]byte(line), &jsonMsg)
			if err != nil {
				logger.WithFields("err", err).Fatal(
					"Error parsing JSON from stdin")
			}

			err = stream.Send(jsonMsg)
			if err != nil {
				logger.WithFields("err", err).Fatal("Error sending JSON")
			}
		}
	}

	err = stream.Close()
	if err != nil {
		logger.WithFields("err", err).Fatal("Error trying to close stream")
	}
	<-receiveDone
}
示例#5
0
func actionInvoke(ctx *cli.Context) {
	leverURL, err := core.ParseLeverURL(ctx.Args().Get(0))
	if err != nil {
		logger.WithFields("err", err).Fatal("Invalid lever URL")
	}
	if leverURL.Environment == "" {
		leverURL.Environment = flagEnv
	}

	client, err := leverapi.NewClient()
	if err != nil {
		logger.WithFields("err", err).Fatal("Error creating client")
	}
	if strings.HasSuffix(leverURL.Environment, ".lever") {
		client.ForceHost = detectLeverOSIPPort()
	}
	if flagHost != "" {
		client.ForceHost = flagHost
	}

	if ctx.NArg() > 1 && ctx.Args().Get(1) == "--" {
		// Byte args case.
		// TODO: Read bytes arg from stdin.
		logger.Fatal("Bytes from stdin not yet implemented")
		return
	}

	// JSON args case.
	var args []interface{}
	for index := 1; index < ctx.NArg(); index++ {
		rawArg := ctx.Args().Get(index)
		var arg interface{}
		err = json.Unmarshal([]byte(rawArg), &arg)
		if err != nil {
			logger.WithFields("err", err, "jsonArg", rawArg).Fatal(
				"Error parsing JSON arg")
		}
		args = append(args, arg)
	}
	var reply interface{}
	err = client.InvokeURL(&reply, leverURL.String(), args...)
	if err != nil {
		logger.WithFields("err", err).Fatal("Invokation error")
	}
	bytes, isByteReply := reply.([]byte)
	if !isByteReply {
		if flagPrettyPrint {
			bytes, err = json.MarshalIndent(reply, "", "  ")
		} else {
			bytes, err = json.Marshal(reply)
		}
		if err != nil {
			logger.WithFields("err", err).Fatal(
				"Error turning reply into JSON form")
		}
	}

	stdout := bufio.NewWriter(os.Stdout)
	stdout.Write(bytes)
	if !isByteReply && flagPrettyPrint {
		stdout.WriteString("\n")
	}
	stdout.Flush()
}