-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathSFTPServerButtonV2.tsx
More file actions
224 lines (211 loc) · 7.22 KB
/
SFTPServerButtonV2.tsx
File metadata and controls
224 lines (211 loc) · 7.22 KB
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
/**
@license
Copyright (c) 2015-2026 Lablup Inc. All rights reserved.
*/
import { SFTPServerButtonV2Fragment$key } from '../__generated__/SFTPServerButtonV2Fragment.graphql';
import {
useCurrentDomainValue,
useSuspendedBackendaiClient,
useWebUINavigate,
} from '../hooks';
import {
useCurrentProjectValue,
useResourceGroupsForCurrentProject,
} from '../hooks/useCurrentProject';
import { useDefaultSystemSSHImageWithFallback } from '../hooks/useDefaultImagesWithFallback';
import { useMergedAllowedStorageHostPermission } from '../hooks/useMergedAllowedStorageHostPermission';
import {
StartSessionWithDefaultValue,
useStartSession,
} from '../hooks/useStartSession';
import { openSFTPFailureModal } from './sftpFailureModal';
import { EllipsisOutlined } from '@ant-design/icons';
import { App, Dropdown, Image, Space, Tooltip, theme } from 'antd';
import {
BAIButton,
BAIButtonProps,
toLocalId,
useBAILogger,
useErrorMessageResolver,
} from 'backend.ai-ui';
import * as _ from 'lodash-es';
import { useTranslation } from 'react-i18next';
import { graphql, useFragment } from 'react-relay';
interface SFTPServerButtonV2Props extends BAIButtonProps {
showTitle?: boolean;
vfolderNodeFrgmt: SFTPServerButtonV2Fragment$key;
}
const SFTPServerButtonV2: React.FC<SFTPServerButtonV2Props> = ({
showTitle = true,
vfolderNodeFrgmt,
...buttonProps
}) => {
'use memo';
const { logger } = useBAILogger();
const { t } = useTranslation();
const { message, modal } = App.useApp();
const { token } = theme.useToken();
const webuiNavigate = useWebUINavigate();
const baiClient = useSuspendedBackendaiClient();
const currentDomain = useCurrentDomainValue();
const currentProject = useCurrentProjectValue();
if (!currentProject.id) {
throw new Error('Project ID is required for SFTPServerButtonV2');
}
const currentUserAccessKey = baiClient?._config?.accessKey;
const { unitedAllowedPermissionByVolume } =
useMergedAllowedStorageHostPermission(
currentDomain,
currentProject.id,
currentUserAccessKey,
);
const { vhostInfo: vhostInfoByCurrentProject } =
useResourceGroupsForCurrentProject();
const { getErrorMessage } = useErrorMessageResolver();
const { startSessionWithDefault, upsertSessionNotification } =
useStartSession();
const { systemSSHImage } = useDefaultSystemSSHImageWithFallback();
const vfolderNode = useFragment(
graphql`
fragment SFTPServerButtonV2Fragment on VFolder {
id
host
}
`,
vfolderNodeFrgmt,
);
// Verify that the current user has access to the volume of the vfolder.
// Check the project has SFTP scaling groups for the host of the vfolder.
const sftpScalingGroupByCurrentProject =
vhostInfoByCurrentProject?.volume_info[vfolderNode?.host || '']
?.sftp_scaling_groups;
// Verify that the current project has access to the volumes in the folder.
// Check the user has 'mount-in-session' permission united by domain, project, and keypair resource policy.
const hasAccessPermission = _.includes(
unitedAllowedPermissionByVolume[vfolderNode?.host ?? ''],
'mount-in-session',
);
const getTooltipTitle = () => {
if (!hasAccessPermission) {
return t('data.explorer.NoPermissionToMountFolder');
} else if (_.isEmpty(sftpScalingGroupByCurrentProject)) {
return t('data.explorer.NoSFTPSupportingScalingGroup');
} else if (!systemSSHImage) {
return t('data.explorer.NoImagesSupportingSystemSession');
} else if (!showTitle && systemSSHImage) {
return t('data.explorer.RunSSH/SFTPserver');
} else return '';
};
// Helper to create launcher value for SFTP session
const createSftpLauncherValue = (): StartSessionWithDefaultValue => ({
sessionName: `sftp-${toLocalId(vfolderNode?.id || '')}`,
sessionType: 'system',
...(baiClient._config?.systemSSHImage &&
baiClient._config?.allow_manual_image_name_for_session
? {
environments: {
manual: baiClient._config.systemSSHImage,
},
}
: {
environments: {
version: systemSSHImage || '',
},
}),
cluster_mode: 'single-node',
cluster_size: 1,
mount_ids: [toLocalId(vfolderNode?.id || '').replaceAll('-', '')],
resourceGroup: sftpScalingGroupByCurrentProject?.[0],
reuseIfExists: true,
});
return (
<Tooltip title={getTooltipTitle()}>
<Space.Compact>
<BAIButton
disabled={
_.isEmpty(sftpScalingGroupByCurrentProject) ||
!systemSSHImage ||
!hasAccessPermission
}
icon={
<Image
width="18px"
src="/resources/icons/sftp.png"
alt="SSH / SFTP"
preview={false}
/>
}
action={async () => {
const sftpSessionConf = createSftpLauncherValue();
await startSessionWithDefault(sftpSessionConf)
.then((results) => {
if (results?.fulfilled && results.fulfilled.length > 0) {
// set notification key for handling duplicate session creation
upsertSessionNotification(results.fulfilled, [
{
key: `sftp-${toLocalId(vfolderNode?.id || '')}`,
},
]);
}
if (results?.rejected && results.rejected.length > 0) {
const error = results.rejected[0].reason;
openSFTPFailureModal({
modal,
t,
token,
error,
getErrorMessage,
onGoToUploadSessions: () => {
webuiNavigate({
pathname: '/session',
search: '?type=system',
});
},
});
}
})
.catch((error) => {
logger.error(
'Unexpected error during session creation:',
error,
);
message.error(t('error.UnexpectedError'));
});
}}
{...buttonProps}
>
{showTitle && t('data.explorer.RunSSH/SFTPserver')}
</BAIButton>
<Dropdown
disabled={
_.isEmpty(sftpScalingGroupByCurrentProject) ||
!systemSSHImage ||
!hasAccessPermission
}
trigger={['click']}
menu={{
items: [
{
key: 'custom',
label: t('import.StartWithOptions'),
onClick: () => {
const launcherValue = createSftpLauncherValue();
const params = new URLSearchParams();
params.set('formValues', JSON.stringify(launcherValue));
params.set('step', '4');
webuiNavigate({
pathname: '/session/start',
search: params.toString(),
});
},
},
],
}}
>
<BAIButton icon={<EllipsisOutlined />} />
</Dropdown>
</Space.Compact>
</Tooltip>
);
};
export default SFTPServerButtonV2;