Advertisement
rockman1510

Mount_to_container.py

Jul 9th, 2020
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. # Using for mounting step that is from virtual board on cloud to container.
  2. # We are using for replacing mounting command lines on linux.
  3. # There are some parameters in this class.
  4. # -h    : for help.
  5. # -l [file path]    : List all the files in a directory, specifically, list all sub directories and files from mount point of board.
  6. # -m [root file path] ['file1, file2, ...'] [destination directory] [container name]    : Mount specific files to container.
  7. import os
  8. import sys
  9.  
  10. params = sys.argv   # Get parameters from command line
  11.  
  12.  
  13. # List system files function with parameters by printing them to screen.
  14. # source:   The path of directory that gets from command parameters (Ex: /home/cloud/boards)
  15. def list_files(source):
  16.     for path, dirs, files in os.walk(source):
  17.         print (path + ":")
  18.         for f in files:
  19.             print (f)
  20.  
  21.  
  22. # To sure that the inputted paths begin with '/'.
  23. # path  : Path of directory
  24. def smooth_path(path):
  25.     if not path[0] == '/':
  26.         path = '/' + path
  27.     return path
  28.  
  29.  
  30. # To sure that the path format of directory and file is correct after doing plus string.
  31. def fix_path(path):
  32.     return path.replace('//', '/')
  33.  
  34. str_mount_help = "Param '-m' [root_file_path] ['source_file1, source_file2, ...'] [dest_dir] [container_name]\t :Mount specific files to container with root path of file, sub directories and file, destination directory, container name."
  35.  
  36.  
  37. # Mount files function with.
  38. # root_path     : inputted root file path that is parent directory of mount point from board.
  39. # files     : a list of sub directories of file and files in string format inside root path.
  40. # dest_dir  : The new destination directory of mounting point.
  41. def mount_files(root_path, files, dest_dir):
  42.     if dest_dir == ''#Check dest_dir null or not
  43.         print("Error: Inputing destination directory not found!")
  44.         return False
  45.     elif not os.path.exists(dest_dir)# Check and create destination directory for mount point if it is not existed.
  46.         os.makedirs(smooth_path(dest_dir))
  47.     else:
  48.         print("Error: Destination directory is existed!")
  49.         return False
  50.     print("files: " + files)
  51.     if not os.path.exists(root_path):   # Check root path exists or not if not will alert and stop.
  52.         print("Error: Root directory doesn't exist!")
  53.         return False
  54.  
  55.     if len(files) > 0# Check string of files is null or not.
  56.         files_list = list(files.split(','))     # convert files from string to a list.
  57.         root_path = smooth_path(root_path)
  58.         dest_dir = smooth_path(dest_dir)
  59.         # access the files list.
  60.         for file_path in files_list:
  61.             file_path = file_path.replace(' ', '')
  62.             file_path = smooth_path(file_path)
  63.             source_file = fix_path(root_path + file_path)   # get source file.
  64.             print("Source File: " + source_file)
  65.             if os.path.isfile(source_file):     # check source file is existed or not
  66.                 path = fix_path(dest_dir + file_path.rsplit('/', 1) [0])    # a new sub directories of files for new mount point.
  67.                 if not os.path.exists(path):    # check new destination directory for mount point is existed or not.
  68.                     os.makedirs(path)   # execute creating new directory if doesn't exist.
  69.                 dest_file = dest_dir + smooth_path(file_path)   # new file path for mounting.              
  70.                 print("Destination File: " + dest_file + "\n")
  71.                 if not os.path.isfile(dest_file):   # check new file path is existed or not.
  72.                     os.mknod(dest_file)     # execute creating new file for mounting if doesn't exit.
  73.                     # execute mounting files to new file path with bind option.
  74.                     os.system("sudo mount --bind " + source_file + " " + dest_file)
  75.             else:   # print error if file doesn't exist
  76.                 print("Error: " + file_path + "\tFile doesn't exist!")
  77.         return True     # return true if mounting files successfully   
  78.     else:
  79.         print("Error: There is no input file!\n [file1, file2, file3,...]")
  80.         return False
  81.  
  82.  
  83. # Execute the program.
  84. if len(params) > 1: # check the number of inputting parameters.
  85.     if params[1] == '-l':   # do the case of list function.
  86.         if len(params) > 2: # check enough parameters or not.
  87.             list_files(params[2])   # list all files and directories with root path from parameter.
  88.         else:
  89.             print("Error: source path not found!")
  90.     elif params[1] == '-m': # do the case of mounting files.
  91.         if len(params) == 6:    # check enough parameters or not.
  92.             if mount_files(params[2], params[3], params[4]):    # do mounting files function with parameters.
  93.                 # After mounting files successfully, doing step mounting files from virtual board on cloud to a new container.
  94.                 os.system("sudo docker run -d -it --name " + params[5] + " --mount type=bind,source=" + params[4] + ",target=/home/" + params[5] + ",bind-propagation=rshared ubuntu") 
  95.             else:
  96.                 print("Mounting failed!")
  97.         else:
  98.             print("Error: You have to insert params!\n" + str_mount_help)
  99.     elif params[1] == '-h': # do the case of help.
  100.         print("Param '-l' [root_path] \t:List all sub directories and files.")
  101.         print(str_mount_help)
  102.     else:
  103.         print("Using '-h' for help!")
  104. else:
  105.     print("Using '-h' for help!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement