File: client.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (238 lines) | stat: -rw-r--r-- 6,279 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package client

import (
	"io/fs"
	"net/rpc"
	"os"

	args "github.com/sylabs/singularity/v4/internal/pkg/runtime/engine/singularity/rpc"
	"golang.org/x/sys/unix"
)

// RPC holds the state necessary for remote procedure calls.
type RPC struct {
	Client *rpc.Client
	Name   string
}

// Mount calls the mount RPC using the supplied arguments.
func (t *RPC) Mount(source string, target string, filesystem string, flags uintptr, data string) error {
	arguments := &args.MountArgs{
		Source:     source,
		Target:     target,
		Filesystem: filesystem,
		Mountflags: flags,
		Data:       data,
	}

	var mountErr error

	err := t.Client.Call(t.Name+".Mount", arguments, &mountErr)
	// RPC communication will take precedence over mount error
	if err == nil {
		err = mountErr
	}

	return err
}

// Decrypt calls the DeCrypt RPC using the supplied arguments.
func (t *RPC) Decrypt(offset uint64, path string, key []byte, masterPid int) (string, error) {
	arguments := &args.CryptArgs{
		Offset:    offset,
		Loopdev:   path,
		Key:       key,
		MasterPid: masterPid,
	}

	var reply string
	err := t.Client.Call(t.Name+".Decrypt", arguments, &reply)

	return reply, err
}

// Mkdir calls the mkdir RPC using the supplied arguments.
func (t *RPC) Mkdir(path string, perm os.FileMode) error {
	arguments := &args.MkdirArgs{
		Path: path,
		Perm: perm,
	}
	return t.Client.Call(t.Name+".Mkdir", arguments, nil)
}

// Chroot calls the chroot RPC using the supplied arguments.
func (t *RPC) Chroot(root string, method string) (int, error) {
	arguments := &args.ChrootArgs{
		Root:   root,
		Method: method,
	}
	var reply int
	err := t.Client.Call(t.Name+".Chroot", arguments, &reply)
	return reply, err
}

// LoopDevice calls the loop device RPC using the supplied arguments.
func (t *RPC) LoopDevice(image string, mode int, info unix.LoopInfo64, maxDevices int, shared bool) (int, error) {
	arguments := &args.LoopArgs{
		Image:      image,
		Mode:       mode,
		Info:       info,
		MaxDevices: maxDevices,
		Shared:     shared,
	}
	var reply int
	err := t.Client.Call(t.Name+".LoopDevice", arguments, &reply)
	return reply, err
}

// SetHostname calls the sethostname RPC using the supplied arguments.
func (t *RPC) SetHostname(hostname string) (int, error) {
	arguments := &args.HostnameArgs{
		Hostname: hostname,
	}
	var reply int
	err := t.Client.Call(t.Name+".SetHostname", arguments, &reply)
	return reply, err
}

// Chdir calls the chdir RPC using the supplied arguments.
func (t *RPC) Chdir(dir string) (int, error) {
	arguments := &args.ChdirArgs{
		Dir: dir,
	}
	var reply int
	err := t.Client.Call(t.Name+".Chdir", arguments, &reply)
	return reply, err
}

// Stat calls the stat RPC using the supplied arguments.
func (t *RPC) Stat(path string) (os.FileInfo, error) {
	arguments := &args.StatArgs{
		Path: path,
	}
	var reply args.StatReply
	err := t.Client.Call(t.Name+".Stat", arguments, &reply)
	if err != nil {
		return nil, err
	}
	return reply.Fi, reply.Err
}

// Lstat calls the lstat RPC using the supplied arguments.
func (t *RPC) Lstat(path string) (os.FileInfo, error) {
	arguments := &args.StatArgs{
		Path: path,
	}
	var reply args.StatReply
	err := t.Client.Call(t.Name+".Lstat", arguments, &reply)
	if err != nil {
		return nil, err
	}
	return reply.Fi, reply.Err
}

// SendFuseFd calls the SendFuseFd RPC using the supplied arguments.
func (t *RPC) SendFuseFd(socket int, fds []int) error {
	arguments := &args.SendFuseFdArgs{
		Socket: socket,
		Fds:    fds,
	}
	var reply int
	err := t.Client.Call(t.Name+".SendFuseFd", arguments, &reply)
	return err
}

// Symlink calls the mkdir RPC using the supplied arguments.
func (t *RPC) Symlink(old string, new string) error {
	arguments := &args.SymlinkArgs{
		Old: old,
		New: new,
	}
	return t.Client.Call(t.Name+".Symlink", arguments, nil)
}

// ReadDir calls the readdir RPC using the supplied arguments.
func (t *RPC) ReadDir(dir string) ([]fs.DirEntry, error) {
	arguments := &args.ReadDirArgs{
		Dir: dir,
	}
	var reply args.ReadDirReply
	err := t.Client.Call(t.Name+".ReadDir", arguments, &reply)
	return reply.Files, err
}

// Chown calls the chown RPC using the supplied arguments.
func (t *RPC) Chown(name string, uid int, gid int) error {
	arguments := &args.ChownArgs{
		Name: name,
		UID:  uid,
		GID:  gid,
	}
	return t.Client.Call(t.Name+".Chown", arguments, nil)
}

// Lchown calls the lchown RPC using the supplied arguments.
func (t *RPC) Lchown(name string, uid int, gid int) error {
	arguments := &args.ChownArgs{
		Name: name,
		UID:  uid,
		GID:  gid,
	}
	return t.Client.Call(t.Name+".Lchown", arguments, nil)
}

// EvalRelative calls the evalrelative RPC using the supplied arguments.
func (t *RPC) EvalRelative(name string, root string) string {
	arguments := &args.EvalRelativeArgs{
		Name: name,
		Root: root,
	}
	var reply string
	t.Client.Call(t.Name+".EvalRelative", arguments, &reply)
	return reply
}

// Readlink calls the readlink RPC using the supplied arguments.
func (t *RPC) Readlink(name string) (string, error) {
	arguments := &args.ReadlinkArgs{
		Name: name,
	}
	var reply string
	err := t.Client.Call(t.Name+".Readlink", arguments, &reply)
	return reply, err
}

// Umask calls the umask RPC using the supplied arguments.
func (t *RPC) Umask(mask int) int {
	arguments := &args.UmaskArgs{
		Mask: mask,
	}
	var reply int
	t.Client.Call(t.Name+".Umask", arguments, &reply)
	return reply
}

// WriteFile calls the writefile RPC using the supplied arguments.
func (t *RPC) WriteFile(filename string, data []byte, perm os.FileMode) error {
	arguments := &args.WriteFileArgs{
		Filename: filename,
		Data:     data,
		Perm:     perm,
	}
	return t.Client.Call(t.Name+".WriteFile", arguments, nil)
}

// NvCCLI will call nvidia-container-cli to configure GPU(s) for the container.
func (t *RPC) NvCCLI(flags []string, rootFsPath string, userNS bool) error {
	arguments := &args.NvCCLIArgs{
		Flags:      flags,
		RootFsPath: rootFsPath,
		UserNS:     userNS,
	}
	return t.Client.Call(t.Name+".NvCCLI", arguments, nil)
}