Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Using for mounting step that is from virtual board on cloud to container.
- # We are using for replacing mounting command lines on linux.
- # There are some parameters in this class.
- # -h : for help.
- # -l [file path] : List all the files in a directory, specifically, list all sub directories and files from mount point of board.
- # -m [root file path] ['file1, file2, ...'] [destination directory] [container name] : Mount specific files to container.
- import os
- import sys
- params = sys.argv # Get parameters from command line
- # List system files function with parameters by printing them to screen.
- # source: The path of directory that gets from command parameters (Ex: /home/cloud/boards)
- def list_files(source):
- for path, dirs, files in os.walk(source):
- print (path + ":")
- for f in files:
- print (f)
- # To sure that the inputted paths begin with '/'.
- # path : Path of directory
- def smooth_path(path):
- if not path[0] == '/':
- path = '/' + path
- return path
- # To sure that the path format of directory and file is correct after doing plus string.
- def fix_path(path):
- return path.replace('//', '/')
- 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."
- # Mount files function with.
- # root_path : inputted root file path that is parent directory of mount point from board.
- # files : a list of sub directories of file and files in string format inside root path.
- # dest_dir : The new destination directory of mounting point.
- def mount_files(root_path, files, dest_dir):
- if dest_dir == '': #Check dest_dir null or not
- print("Error: Inputing destination directory not found!")
- return False
- elif not os.path.exists(dest_dir): # Check and create destination directory for mount point if it is not existed.
- os.makedirs(smooth_path(dest_dir))
- else:
- print("Error: Destination directory is existed!")
- return False
- print("files: " + files)
- if not os.path.exists(root_path): # Check root path exists or not if not will alert and stop.
- print("Error: Root directory doesn't exist!")
- return False
- if len(files) > 0: # Check string of files is null or not.
- files_list = list(files.split(',')) # convert files from string to a list.
- root_path = smooth_path(root_path)
- dest_dir = smooth_path(dest_dir)
- # access the files list.
- for file_path in files_list:
- file_path = file_path.replace(' ', '')
- file_path = smooth_path(file_path)
- source_file = fix_path(root_path + file_path) # get source file.
- print("Source File: " + source_file)
- if os.path.isfile(source_file): # check source file is existed or not
- path = fix_path(dest_dir + file_path.rsplit('/', 1) [0]) # a new sub directories of files for new mount point.
- if not os.path.exists(path): # check new destination directory for mount point is existed or not.
- os.makedirs(path) # execute creating new directory if doesn't exist.
- dest_file = dest_dir + smooth_path(file_path) # new file path for mounting.
- print("Destination File: " + dest_file + "\n")
- if not os.path.isfile(dest_file): # check new file path is existed or not.
- os.mknod(dest_file) # execute creating new file for mounting if doesn't exit.
- # execute mounting files to new file path with bind option.
- os.system("sudo mount --bind " + source_file + " " + dest_file)
- else: # print error if file doesn't exist
- print("Error: " + file_path + "\tFile doesn't exist!")
- return True # return true if mounting files successfully
- else:
- print("Error: There is no input file!\n [file1, file2, file3,...]")
- return False
- # Execute the program.
- if len(params) > 1: # check the number of inputting parameters.
- if params[1] == '-l': # do the case of list function.
- if len(params) > 2: # check enough parameters or not.
- list_files(params[2]) # list all files and directories with root path from parameter.
- else:
- print("Error: source path not found!")
- elif params[1] == '-m': # do the case of mounting files.
- if len(params) == 6: # check enough parameters or not.
- if mount_files(params[2], params[3], params[4]): # do mounting files function with parameters.
- # After mounting files successfully, doing step mounting files from virtual board on cloud to a new container.
- os.system("sudo docker run -d -it --name " + params[5] + " --mount type=bind,source=" + params[4] + ",target=/home/" + params[5] + ",bind-propagation=rshared ubuntu")
- else:
- print("Mounting failed!")
- else:
- print("Error: You have to insert params!\n" + str_mount_help)
- elif params[1] == '-h': # do the case of help.
- print("Param '-l' [root_path] \t:List all sub directories and files.")
- print(str_mount_help)
- else:
- print("Using '-h' for help!")
- else:
- print("Using '-h' for help!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement