2020package com .cloud .resource ;
2121
2222import java .io .File ;
23+ import java .io .FileWriter ;
24+ import java .io .IOException ;
2325import java .io .PrintWriter ;
2426import java .io .StringWriter ;
2527import java .net .NetworkInterface ;
@@ -64,6 +66,7 @@ public abstract class ServerResourceBase implements ServerResource {
6466 protected NetworkInterface storageNic ;
6567 protected NetworkInterface storageNic2 ;
6668 protected IAgentControl agentControl ;
69+ protected static final String DEFAULT_LOCAL_STORAGE_PATH = "/var/lib/libvirt/images/" ;
6770
6871 @ Override
6972 public String getName () {
@@ -178,27 +181,27 @@ protected Answer listHostDevices() {
178181 return new ListHostDeviceAnswer (true , hostDevicesText );
179182 }
180183
181- protected Answer createImageRbd (String names , long sizes , String poolPath ) {
182- sizes = ( sizes * 1024 );
183- String cmdout = Script .runSimpleBashScript ("rbd -p " + poolPath + " create -s " + sizes + " " + names );
184+ protected Answer createImageRbd (String poolUuid , String skey , String authUserName , String host , String names , long sizes , String poolPath ) {
185+ createRBDSecretKeyFileIfNoExist ( poolUuid , DEFAULT_LOCAL_STORAGE_PATH , skey );
186+ String cmdout = Script .runSimpleBashScript ("rbd -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid + " create -s " + ( sizes * 1024 ) + " " + names );
184187 if (cmdout == null ) {
185188 logger .debug (cmdout );
186189 }else {
187190 }
188191 return new ListRbdObjectsAnswer (true , names );
189192 }
190193
191- protected Answer deleteImageRbd (String name , String poolPath ) {
192-
193- String cmdout = Script .runSimpleBashScript ("rbd -p " + poolPath + " rm " + name );
194+ protected Answer deleteImageRbd (String poolUuid , String skey , String authUserName , String host , String name , String poolPath ) {
195+ createRBDSecretKeyFileIfNoExist ( poolUuid , DEFAULT_LOCAL_STORAGE_PATH , skey );
196+ String cmdout = Script .runSimpleBashScript ("rbd -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid + " rm " + name );
194197 if (cmdout == null ) {
195198 logger .debug (cmdout );
196199 }else {
197200 }
198201 return new ListRbdObjectsAnswer (true , name );
199202 }
200203
201- protected Answer listRbdFilesAtPath (int startIndex , int pageSize , String poolPath , String keyword ) {
204+ protected Answer listRbdFilesAtPath (String poolUuid , String skey , String authUserName , String host , int startIndex , int pageSize , String poolPath , String keyword ) {
202205 int count = 0 ;
203206 List <String > names = new ArrayList <>();
204207 List <String > paths = new ArrayList <>();
@@ -207,13 +210,15 @@ protected Answer listRbdFilesAtPath(int startIndex, int pageSize, String poolPat
207210 List <Long > sizes = new ArrayList <>();
208211 List <Long > modifiedList = new ArrayList <>();
209212
213+ createRBDSecretKeyFileIfNoExist (poolUuid , DEFAULT_LOCAL_STORAGE_PATH , skey );
214+
210215 Script listCommand = new Script ("/bin/bash" , logger );
211216 listCommand .add ("-c" );
212217
213218 if (keyword != null && !keyword .isEmpty ()) {
214- listCommand .add ("rbd -p " + poolPath + " ls | grep " + keyword );
219+ listCommand .add ("rbd ls -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid + " | grep " + keyword );
215220 } else {
216- listCommand .add ("rbd -p " + poolPath + " ls" );
221+ listCommand .add ("rbd ls -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid );
217222 }
218223 OutputInterpreter .AllLinesParser listParser = new OutputInterpreter .AllLinesParser ();
219224 String listResult = listCommand .execute (listParser );
@@ -231,6 +236,9 @@ protected Answer listRbdFilesAtPath(int startIndex, int pageSize, String poolPat
231236
232237 Script infoCommand = new Script ("rbd" );
233238 infoCommand .add ("-p" , poolPath );
239+ infoCommand .add ("--id" , authUserName );
240+ infoCommand .add ("-m" , host );
241+ infoCommand .add ("-K" , DEFAULT_LOCAL_STORAGE_PATH + poolUuid );
234242 infoCommand .add ("info" , imageName .trim ());
235243 OutputInterpreter .AllLinesParser infoParser = new OutputInterpreter .AllLinesParser ();
236244 String infoResult = infoCommand .execute (infoParser );
@@ -265,49 +273,65 @@ protected Answer listRbdFilesAtPath(int startIndex, int pageSize, String poolPat
265273 return new ListDataStoreObjectsAnswer (true , count , names , paths , absPaths , isDirs , sizes , modifiedList );
266274 }
267275
276+ public void createRBDSecretKeyFileIfNoExist (String uuid , String localPath , String skey ) {
277+ File file = new File (localPath + File .separator + uuid );
278+ try {
279+ // 파일이 존재하지 않을 때만 생성
280+ if (!file .exists ()) {
281+ boolean isCreated = file .createNewFile ();
282+ if (isCreated ) {
283+ // 파일 생성 후 내용 작성
284+ FileWriter writer = new FileWriter (file );
285+ writer .write (skey );
286+ writer .close ();
287+ }
288+ }
289+ } catch (IOException e ) {}
290+ }
268291
269- protected Answer listFilesAtPath (String nfsMountPoint , String relativePath , int startIndex , int pageSize , String keyword ) {
270- int count = 0 ;
271- File file = new File (nfsMountPoint , relativePath );
272- List <String > names = new ArrayList <>();
273- List <String > paths = new ArrayList <>();
274- List <String > absPaths = new ArrayList <>();
275- List <Boolean > isDirs = new ArrayList <>();
276- List <Long > sizes = new ArrayList <>();
277- List <Long > modifiedList = new ArrayList <>();
278- if (file .isFile ()) {
279- count = 1 ;
280- names .add (file .getName ());
281- paths .add (file .getPath ().replace (nfsMountPoint , "" ));
282- absPaths .add (file .getPath ());
283- isDirs .add (file .isDirectory ());
284- sizes .add (file .length ());
285- modifiedList .add (file .lastModified ());
286- } else if (file .isDirectory ()) {
287- String [] files = file .list ();
288- List <String > filteredFiles = new ArrayList <>();
289- if (keyword != null && !"" .equals (keyword )) {
290- for (String fileName : files ) {
291- if (fileName .contains (keyword )) {
292- filteredFiles .add (fileName );
292+ protected Answer listFilesAtPath (String nfsMountPoint , String relativePath , int startIndex , int pageSize , String keyword ) {
293+ int count = 0 ;
294+ File file = new File (nfsMountPoint , relativePath );
295+ List <String > names = new ArrayList <>();
296+ List <String > paths = new ArrayList <>();
297+ List <String > absPaths = new ArrayList <>();
298+ List <Boolean > isDirs = new ArrayList <>();
299+ List <Long > sizes = new ArrayList <>();
300+ List <Long > modifiedList = new ArrayList <>();
301+ if (file .isFile ()) {
302+ count = 1 ;
303+ names .add (file .getName ());
304+ paths .add (file .getPath ().replace (nfsMountPoint , "" ));
305+ absPaths .add (file .getPath ());
306+ isDirs .add (file .isDirectory ());
307+ sizes .add (file .length ());
308+ modifiedList .add (file .lastModified ());
309+ } else if (file .isDirectory ()) {
310+ String [] files = file .list ();
311+ List <String > filteredFiles = new ArrayList <>();
312+ if (keyword != null && !"" .equals (keyword )) {
313+ for (String fileName : files ) {
314+ if (fileName .contains (keyword )) {
315+ filteredFiles .add (fileName );
316+ }
293317 }
318+ } else {
319+ filteredFiles .addAll (Arrays .asList (files ));
320+ }
321+ count = filteredFiles .size ();
322+ for (int i = startIndex ; i < startIndex + pageSize && i < count ; i ++) {
323+ File f = new File (nfsMountPoint , relativePath + '/' + filteredFiles .get (i ));
324+ names .add (f .getName ());
325+ paths .add (f .getPath ().replace (nfsMountPoint , "" ));
326+ absPaths .add (f .getPath ());
327+ isDirs .add (f .isDirectory ());
328+ sizes .add (f .length ());
329+ modifiedList .add (f .lastModified ());
294330 }
295- } else {
296- filteredFiles .addAll (Arrays .asList (files ));
297- }
298- count = filteredFiles .size ();
299- for (int i = startIndex ; i < startIndex + pageSize && i < count ; i ++) {
300- File f = new File (nfsMountPoint , relativePath + '/' + filteredFiles .get (i ));
301- names .add (f .getName ());
302- paths .add (f .getPath ().replace (nfsMountPoint , "" ));
303- absPaths .add (f .getPath ());
304- isDirs .add (f .isDirectory ());
305- sizes .add (f .length ());
306- modifiedList .add (f .lastModified ());
307331 }
332+ return new ListDataStoreObjectsAnswer (file .exists (), count , names , paths , absPaths , isDirs , sizes , modifiedList );
308333 }
309- return new ListDataStoreObjectsAnswer (file .exists (), count , names , paths , absPaths , isDirs , sizes , modifiedList );
310- }
334+
311335 protected Answer listFilesAtPath (String nfsMountPoint , String relativePath , int startIndex , int pageSize ) {
312336 int count = 0 ;
313337 File file = new File (nfsMountPoint , relativePath );
@@ -340,6 +364,7 @@ protected Answer listFilesAtPath(String nfsMountPoint, String relativePath, int
340364 }
341365 return new ListDataStoreObjectsAnswer (file .exists (), count , names , paths , absPaths , isDirs , sizes , modifiedList );
342366 }
367+
343368 protected void fillNetworkInformation (final StartupCommand cmd ) {
344369 String [] info = null ;
345370 if (privateNic != null ) {
0 commit comments