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

     1  // Copyright 2019 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 SystemdStrategy implements PlatformStrategy.
    27  var _ PlatformStrategy = &SystemdStrategy{}
    28  
    29  // SystemdStrategy is a Linux-specific PlatformStrategy using systemd.
    30  // Implements PlatformStrategy.
    31  type SystemdStrategy struct {
    32  	LinuxStrategy
    33  }
    34  
    35  // systemdCfg is the path to write the Swarming bot systemd config.
    36  const systemdCfg = "/etc/systemd/system/swarming-start-bot.service"
    37  
    38  // systemdTmpl is the name of the Swarming bot systemd config template asset.
    39  const systemdTmpl = "swarming-start-bot.service.tmpl"
    40  
    41  // systemdSrv is the name of the Swarming bot systemd service.
    42  const systemdSrv = "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 (*SystemdStrategy) 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(systemdTmpl)), subs)
    54  	if err != nil {
    55  		return errors.Annotate(err, "failed to prepare template %q", systemdTmpl).Err()
    56  	}
    57  
    58  	logging.Infof(c, "installing: %s", systemdCfg)
    59  	// 0644 allows the systemd config to be read by all users.
    60  	// Useful when SSHing to the instance.
    61  	if err := os.WriteFile(systemdCfg, []byte(s), 0644); err != nil {
    62  		return errors.Annotate(err, "failed to write: %s", systemdCfg).Err()
    63  	}
    64  
    65  	// Enable the service so it starts on next boot.
    66  	logging.Infof(c, "enabling %q", systemdSrv)
    67  	if err := exec.Command("systemctl", "enable", systemdSrv).Run(); err != nil {
    68  		return errors.Annotate(err, "failed to enable service %q", systemdSrv).Err()
    69  	}
    70  
    71  	// Start the service right now.
    72  	logging.Infof(c, "starting %q", systemdSrv)
    73  	if err := exec.Command("systemctl", "start", systemdSrv).Run(); err != nil {
    74  		return errors.Annotate(err, "failed to start service %q", systemdSrv).Err()
    75  	}
    76  	return nil
    77  }