1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.arvados.org/arvados.git/lib/cloud"
22 "git.arvados.org/arvados.git/lib/dispatchcloud/test"
23 "git.arvados.org/arvados.git/sdk/go/arvados"
24 "github.com/sirupsen/logrus"
25 "golang.org/x/crypto/ssh"
28 // Driver is the loopback implementation of the cloud.Driver interface.
29 var Driver = cloud.DriverFunc(newInstanceSet)
32 errUnimplemented = errors.New("function not implemented by loopback driver")
33 errQuota = quotaError("loopback driver is always at quota")
36 type quotaError string
38 func (e quotaError) IsQuotaError() bool { return true }
39 func (e quotaError) Error() string { return string(e) }
41 type instanceSet struct {
42 instanceSetID cloud.InstanceSetID
43 logger logrus.FieldLogger
48 func newInstanceSet(config json.RawMessage, instanceSetID cloud.InstanceSetID, _ cloud.SharedResourceTags, logger logrus.FieldLogger) (cloud.InstanceSet, error) {
50 instanceSetID: instanceSetID,
56 func (is *instanceSet) Create(it arvados.InstanceType, _ cloud.ImageID, tags cloud.InstanceTags, _ cloud.InitCommand, pubkey ssh.PublicKey) (cloud.Instance, error) {
59 if len(is.instances) > 0 {
62 // A crunch-run process running in a previous instance may
63 // have marked the node as broken. In the loopback scenario a
64 // destroy+create cycle doesn't fix whatever was broken -- but
65 // nothing else will either, so the best we can do is remove
66 // the "broken" flag and try again.
67 if err := os.Remove("/var/lock/crunch-run-broken"); err == nil {
68 is.logger.Info("removed /var/lock/crunch-run-broken")
69 } else if !errors.Is(err, os.ErrNotExist) {
72 u, err := user.Current()
76 hostRSAKey, err := rsa.GenerateKey(rand.Reader, 1024)
80 hostKey, err := ssh.NewSignerFromKey(hostRSAKey)
84 hostPubKey, err := ssh.NewPublicKey(hostRSAKey.Public())
91 adminUser: u.Username,
93 hostPubKey: hostPubKey,
94 sshService: test.SSHService{
96 AuthorizedUser: u.Username,
97 AuthorizedKeys: []ssh.PublicKey{pubkey},
100 inst.sshService.Exec = inst.sshExecFunc
101 go inst.sshService.Start()
102 is.instances = []*instance{inst}
106 func (is *instanceSet) Instances(cloud.InstanceTags) ([]cloud.Instance, error) {
108 defer is.mtx.Unlock()
109 var ret []cloud.Instance
110 for _, inst := range is.instances {
111 ret = append(ret, inst)
116 func (is *instanceSet) Stop() {
118 defer is.mtx.Unlock()
119 for _, inst := range is.instances {
120 inst.sshService.Close()
124 type instance struct {
126 instanceType arvados.InstanceType
128 tags cloud.InstanceTags
129 hostPubKey ssh.PublicKey
130 sshService test.SSHService
133 func (i *instance) ID() cloud.InstanceID { return cloud.InstanceID(i.instanceType.ProviderType) }
134 func (i *instance) String() string { return i.instanceType.ProviderType }
135 func (i *instance) ProviderType() string { return i.instanceType.ProviderType }
136 func (i *instance) Address() string { return i.sshService.Address() }
137 func (i *instance) RemoteUser() string { return i.adminUser }
138 func (i *instance) Tags() cloud.InstanceTags { return i.tags }
139 func (i *instance) SetTags(tags cloud.InstanceTags) error {
143 func (i *instance) Destroy() error {
145 defer i.is.mtx.Unlock()
146 i.is.instances = i.is.instances[:0]
149 func (i *instance) VerifyHostKey(pubkey ssh.PublicKey, _ *ssh.Client) error {
150 if !bytes.Equal(pubkey.Marshal(), i.hostPubKey.Marshal()) {
151 return errors.New("host key mismatch")
155 func (i *instance) sshExecFunc(env map[string]string, command string, stdin io.Reader, stdout, stderr io.Writer) uint32 {
156 cmd := exec.Command("sh", "-c", strings.TrimPrefix(command, "sudo "))
160 for k, v := range env {
161 cmd.Env = append(cmd.Env, k+"="+v)
163 // Prevent child process from using our tty.
164 cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
168 } else if err, ok := err.(*exec.ExitError); !ok {
170 } else if code := err.ExitCode(); code < 0 {