1 // Copyright 2019 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
23 "github.com/golang/protobuf/proto"
24 "pigweed.dev/pw_target_runner"
26 pb "pigweed.dev/proto/pw_target_runner/exec_server_config_pb"
29 // ServerOptions contains command-line options for the server.
30 type ServerOptions struct {
31 // Path to a server configuration file.
34 // Port on which to run.
38 // configureServerFromFile sets up the server with workers specifyed in a
39 // config file. The file contains a pw.target_runner.ServerConfig protobuf
40 // message in canonical protobuf text format.
41 func configureServerFromFile(s *pw_target_runner.Server, filepath string) error {
42 content, err := ioutil.ReadFile(filepath)
47 var config pb.ServerConfig
48 if err := proto.UnmarshalText(string(content), &config); err != nil {
52 log.Printf("Parsed server configuration from %s\n", filepath)
54 runners := config.GetRunner()
59 // Create an exec worker for each of the runner messages listed in the
60 // config and register them with the server.
61 for i, runner := range runners {
62 // Build the complete command for the worker from its "command"
63 // and "args" fields in the proto message. The command is
64 // required; arguments are optional.
65 cmd := []string{runner.GetCommand()}
68 "ServerConfig.runner[%d] does not specify a command; skipping\n", i)
69 return errors.New(msg)
72 if args := runner.GetArgs(); args != nil {
73 cmd = append(cmd, args...)
76 worker := pw_target_runner.NewExecDeviceRunner(i, cmd)
77 s.RegisterWorker(worker)
80 "Registered ExecDeviceRunner %s with args %v\n",
89 configPtr := flag.String("config", "", "Path to server configuration file")
90 portPtr := flag.Int("port", 8080, "Server port")
94 server := pw_target_runner.NewServer()
97 if err := configureServerFromFile(server, *configPtr); err != nil {
98 log.Fatalf("Failed to parse config file %s: %v", *configPtr, err)
102 if err := server.Bind(*portPtr); err != nil {
106 if err := server.Serve(); err != nil {
107 log.Fatalf("Failed to start server: %v", err)