go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/cmd/agent/upstart.go (about)

     1  // Copyright 2018 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"os/exec"
    21  
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/common/logging"
    24  )
    25  
    26  // Ensure UpstartStrategy implements PlatformStrategy.
    27  var _ PlatformStrategy = &UpstartStrategy{}
    28  
    29  // UpstartStrategy is a Linux-specific PlatformStrategy using upstart.
    30  // Implements PlatformStrategy.
    31  type UpstartStrategy struct {
    32  	LinuxStrategy
    33  }
    34  
    35  // upstartCfg is the path to write the Swarming bot upstart config.
    36  const upstartCfg = "/etc/init/swarming-start-bot.conf"
    37  
    38  // upstartTmpl is the name of the Swarming bot upstart config template asset.
    39  const upstartTmpl = "swarming-start-bot.conf.tmpl"
    40  
    41  // upstartSrv is the name of the Swarming bot upstart service.
    42  const upstartSrv = "swarming-start-bot"
    43  
    44  // autostart configures the given Swarming bot code to be executed by the given
    45  // python on startup for the given user, then starts the Swarming bot process.
    46  // Implements PlatformStrategy.
    47  func (*UpstartStrategy) autostart(c context.Context, path, user string, python string) error {
    48  	subs := map[string]string{
    49  		"BotCode": path,
    50  		"User":    user,
    51  		"Python":  python,
    52  	}
    53  	s, err := substitute(c, string(GetAsset(upstartTmpl)), subs)
    54  	if err != nil {
    55  		return errors.Annotate(err, "failed to prepare template %q", upstartTmpl).Err()
    56  	}
    57  
    58  	logging.Infof(c, "installing: %s", upstartCfg)
    59  	// 0644 allows the upstart config to be read by all users.
    60  	// Useful when SSHing to the instance.
    61  	if err := os.WriteFile(upstartCfg, []byte(s), 0644); err != nil {
    62  		return errors.Annotate(err, "failed to write: %s", upstartCfg).Err()
    63  	}
    64  
    65  	logging.Infof(c, "starting %q", upstartSrv)
    66  	if err := exec.Command("initctl", "start", upstartSrv).Run(); err != nil {
    67  		return errors.Annotate(err, "failed to start service %q", upstartSrv).Err()
    68  	}
    69  	return nil
    70  }
    71  
    72  // canUseUpstart returns whether or not UpstartStrategy can be used.
    73  func canUseUpstart() bool {
    74  	return exec.Command("initctl", "--version").Run() == nil
    75  }