1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.arvados.org/arvados.git/lib/cloud"
21 "git.arvados.org/arvados.git/lib/dispatchcloud/test"
22 "git.arvados.org/arvados.git/sdk/go/arvados"
23 "github.com/sirupsen/logrus"
24 "golang.org/x/crypto/ssh"
27 // Driver is the loopback implementation of the cloud.Driver interface.
28 var Driver = cloud.DriverFunc(newInstanceSet)
31 errUnimplemented = errors.New("function not implemented by loopback driver")
32 errQuota = quotaError("loopback driver is always at quota")
35 type quotaError string
37 func (e quotaError) IsQuotaError() bool { return true }
38 func (e quotaError) Error() string { return string(e) }
40 type instanceSet struct {
41 instanceSetID cloud.InstanceSetID
42 logger logrus.FieldLogger
47 func newInstanceSet(config json.RawMessage, instanceSetID cloud.InstanceSetID, _ cloud.SharedResourceTags, logger logrus.FieldLogger) (cloud.InstanceSet, error) {
49 instanceSetID: instanceSetID,
55 func (is *instanceSet) Create(it arvados.InstanceType, _ cloud.ImageID, tags cloud.InstanceTags, _ cloud.InitCommand, pubkey ssh.PublicKey) (cloud.Instance, error) {
58 if len(is.instances) > 0 {
61 u, err := user.Current()
65 hostRSAKey, err := rsa.GenerateKey(rand.Reader, 1024)
69 hostKey, err := ssh.NewSignerFromKey(hostRSAKey)
73 hostPubKey, err := ssh.NewPublicKey(hostRSAKey.Public())
80 adminUser: u.Username,
82 hostPubKey: hostPubKey,
83 sshService: test.SSHService{
85 AuthorizedUser: u.Username,
86 AuthorizedKeys: []ssh.PublicKey{pubkey},
89 inst.sshService.Exec = inst.sshExecFunc
90 go inst.sshService.Start()
91 is.instances = []*instance{inst}
95 func (is *instanceSet) Instances(cloud.InstanceTags) ([]cloud.Instance, error) {
98 var ret []cloud.Instance
99 for _, inst := range is.instances {
100 ret = append(ret, inst)
105 func (is *instanceSet) Stop() {
107 defer is.mtx.Unlock()
108 for _, inst := range is.instances {
109 inst.sshService.Close()
113 type instance struct {
115 instanceType arvados.InstanceType
117 tags cloud.InstanceTags
118 hostPubKey ssh.PublicKey
119 sshService test.SSHService
122 func (i *instance) ID() cloud.InstanceID { return cloud.InstanceID(i.instanceType.ProviderType) }
123 func (i *instance) String() string { return i.instanceType.ProviderType }
124 func (i *instance) ProviderType() string { return i.instanceType.ProviderType }
125 func (i *instance) Address() string { return i.sshService.Address() }
126 func (i *instance) RemoteUser() string { return i.adminUser }
127 func (i *instance) Tags() cloud.InstanceTags { return i.tags }
128 func (i *instance) SetTags(tags cloud.InstanceTags) error {
132 func (i *instance) Destroy() error {
134 defer i.is.mtx.Unlock()
135 i.is.instances = i.is.instances[:0]
138 func (i *instance) VerifyHostKey(pubkey ssh.PublicKey, _ *ssh.Client) error {
139 if !bytes.Equal(pubkey.Marshal(), i.hostPubKey.Marshal()) {
140 return errors.New("host key mismatch")
144 func (i *instance) sshExecFunc(env map[string]string, command string, stdin io.Reader, stdout, stderr io.Writer) uint32 {
145 cmd := exec.Command("sh", "-c", strings.TrimPrefix(command, "sudo "))
149 for k, v := range env {
150 cmd.Env = append(cmd.Env, k+"="+v)
152 // Prevent child process from using our tty.
153 cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
157 } else if err, ok := err.(*exec.ExitError); !ok {
159 } else if code := err.ExitCode(); code < 0 {