def files_rm(self, user_container_name, file, recursive=False): check_containername(user_container_name, 'user_container_name') check_pathname(file, 'file') container = data_container_name(user_container_name) filetorm = absolute_userpath(file) filemanager.rm(container, filetorm, recursive)
def files_ls(self, user_container_name, dir, recursive=False): check_containername(user_container_name, 'user_container_name') check_pathname(dir, 'dir') container = data_container_name(user_container_name) file = absolute_userpath(dir) return filemanager.listfiles(container, file, recursive)
def files_exists(self, user_container_name, file): check_containername(user_container_name, 'user_container_name') check_pathname(file, 'file') container = data_container_name(user_container_name) checkexisting = absolute_userpath(file) return filemanager.exists(container, checkexisting)
def files_mkdir(self, user_container_name, dir): check_containername(user_container_name, 'user_container_name') check_pathname(dir, 'dir') container = data_container_name(user_container_name) file = absolute_userpath(dir) filemanager.mkdir(container, file, True, 1000)
def files_readsecret(self, user_container_name): check_containername(user_container_name, 'user_container_name') container = data_container_name(user_container_name) data = StringIO.StringIO() filemanager.fromcontainer(container, '/etc/rosauth/secret', data) return data.getvalue()
def files_tocontainer(self, user_container_name, data, targetfile): check_containername(user_container_name, 'user_container_name') check_pathname(targetfile, 'targetfile') container = data_container_name(user_container_name) file = absolute_userpath(targetfile) filemanager.tocontainer(container, to_deb64_stream(data), file, 1000)
def files_fromcontainer(self, user_container_name, sourcefile): check_containername(user_container_name, 'user_container_name') check_pathname(sourcefile, 'sourcefile') container = data_container_name(user_container_name) file = absolute_userpath(sourcefile) data = StringIO.StringIO() filemanager.fromcontainer(container, file, data) return base64.b64encode(data.getvalue())
def create_user_data_container(self, container_name): try: all_containers = self.__client.containers(all=True) user_data_container = data_container_name(container_name) if self.__get_container(user_data_container, all_containers) is None: sysout("Creating "+user_data_container+" container.") self.__client.create_container('knowrob/user_data', detach=True, tty=True, name=user_data_container, volumes=['/etc/rosauth'], entrypoint='true') self.__client.start(user_data_container) return True except (APIError, DockerException), e: sysout("Error in create_user_data_container: " + str(e.message)) traceback.print_exc()
def start_user_container(self, application_image, user_container_name, limit_resources=True): try: all_containers = self.__client.containers(all=True) # Make sure common containers are up and running self.__start_common_container__(all_containers) # Stop user container if running self.__stop_container__(user_container_name, all_containers) user_home_dir = absolute_userpath('') ros_version = os.getenv('OPENEASE_DOCKER_ROS_DISTRO', 'hydro') sysout('DOCKER ROS DISTRO: ' + ros_version) sysout("Creating user container " + user_container_name) env = {"VIRTUAL_HOST": user_container_name, "VIRTUAL_PORT": '9090', "ROS_PACKAGE_PATH": ":".join([ "/home/ros/src", "/opt/ros/"+ros_version+"/share", "/opt/ros/"+ros_version+"/stacks", user_home_dir ])} if limit_resources: mem_limit = 256 * 1024 * 1024 # default is 1024, meaning that 4 of these containers will receive the same cpu time as one default # container. decrease this further if you want to increase the maximum amount of users on the host. cpu_shares = 256 else: mem_limit = 0 cpu_shares = 1024 # Default value self.__client.create_container(application_image, detach=True, tty=True, environment=env, name=user_container_name, mem_limit=mem_limit, cpu_shares=cpu_shares, memswap_limit=mem_limit*4, entrypoint=['/opt/ros/'+ros_version+'/bin/roslaunch', 'rs_kbreasoning', 'knowrob_robosherlock.launch']) # Read links and volumes from webapp_container ENV inspect = self.__client.inspect_image(application_image) env = dict(map(lambda x: x.split("="), inspect['Config']['Env'])) links = map(lambda x: tuple(x.split(':')), env['DOCKER_LINKS'].split(' ')) volumes = env['DOCKER_VOLUMES'].split(' ') volumes.append(data_container_name(user_container_name)) sysout("Starting user container " + user_container_name) self.__client.start(user_container_name, port_bindings={9090: ('127.0.0.1',)}, links=links, volumes_from=volumes) except Exception, e: sysout("Error in start_user_container: " + str(e.message)) traceback.print_exc()
def start_user_container(self, application_image, user_container_name, limit_resources=True): try: all_containers = self.__client.containers(all=True) # Make sure common containers are up and running self.__start_common_container__(all_containers) # Stop user container if running self.__stop_container__(user_container_name, all_containers) user_home_dir = absolute_userpath('') sysout("Creating user container " + user_container_name) env = {"VIRTUAL_HOST": user_container_name, "VIRTUAL_PORT": '9090', "ROS_PACKAGE_PATH": ":".join([ "/home/ros/src", "/opt/ros/hydro/share", "/opt/ros/hydro/stacks", user_home_dir ])} if limit_resources: mem_limit = 256 * 1024 * 1024 # default is 1024, meaning that 4 of these containers will receive the same cpu time as one default # container. decrease this further if you want to increase the maximum amount of users on the host. cpu_shares = 256 else: mem_limit = 0 cpu_shares = 1024 # Default value self.__client.create_container(application_image, detach=True, tty=True, environment=env, name=user_container_name, mem_limit=mem_limit, cpu_shares=cpu_shares, memswap_limit=mem_limit*4, entrypoint=['/opt/ros/hydro/bin/roslaunch', 'knowrob_roslog_launch', 'knowrob_ease.launch']) # Read links and volumes from webapp_container ENV inspect = self.__client.inspect_image(application_image) env = dict(map(lambda x: x.split("="), inspect['Config']['Env'])) links = map(lambda x: tuple(x.split(':')), env['DOCKER_LINKS'].split(' ')) volumes = env['DOCKER_VOLUMES'].split(' ') volumes.append(data_container_name(user_container_name)) sysout("Starting user container " + user_container_name) self.__client.start(user_container_name, port_bindings={9090: ('127.0.0.1',)}, links=links, volumes_from=volumes) except Exception, e: sysout("Error in start_user_container: " + str(e.message)) traceback.print_exc()
def files_writesecret(self, user_container_name, secret): check_containername(user_container_name, 'user_container_name') container = data_container_name(user_container_name) data = StringIO.StringIO(secret) filemanager.tocontainer(container, data, '/etc/rosauth/secret')
def __largecopy(self, user_container_name, src, tgt): check_containername(user_container_name, 'user_container_name') container = data_container_name(user_container_name) filemanager.copy_with_lft(container, src, tgt, 1000)
def start_user_container(self, application_image, user_container_name, ros_distribution, limit_resources=True): try: all_containers = self.__client.containers(all=True) # Stop user container if running self.__stop_container__(user_container_name, all_containers) user_home_dir = absolute_userpath('') sysout("Creating user container " + user_container_name) env = { "VIRTUAL_HOST": user_container_name, "VIRTUAL_PORT": '9090', "MONGO_PORT_27017_TCP_ADDR": 'mongo', "MONGO_PORT_27017_TCP_PORT": '27017', "ROS_PACKAGE_PATH": ":".join([ "/home/ros/src", "/opt/ros/" + ros_distribution + "/share", "/opt/ros/" + ros_distribution + "/stacks", user_home_dir ]) } links = [] if limit_resources: mem_limit = 256 * 1024 * 1024 # default is 1024, meaning that 4 of these containers will receive the same cpu time as one default # container. decrease this further if you want to increase the maximum amount of users on the host. cpu_shares = 256 else: mem_limit = 0 cpu_shares = 1024 # Default value volumes = ['/episodes'] volume_bindings = { os.environ['OPENEASE_EPISODE_DATA']: { 'bind': '/episodes', 'mode': 'ro' } } host_config = self.__client.create_host_config( binds=volume_bindings, mem_limit=mem_limit, memswap_limit=mem_limit * 4, ) self.__client.create_container( application_image, detach=True, tty=True, environment=env, name=user_container_name, cpu_shares=cpu_shares, volumes=volumes, host_config=host_config, entrypoint=[ '/opt/ros/' + ros_distribution + '/bin/roslaunch', 'knowrob_roslog_launch', 'knowrob_ease.launch' ]) for network_name in self.network_names: self.__client.connect_container_to_network( user_container_name, network_name) # Read links and volumes from webapp_container ENV inspect = self.__client.inspect_image(application_image) env = dict(map(lambda x: x.split("="), inspect['Config']['Env'])) volumes_from = [data_container_name(user_container_name)] sysout("Starting user container " + user_container_name) self.__client.start(user_container_name, port_bindings={9090: ('127.0.0.1', )}, volumes_from=volumes_from) except Exception, e: sysout("Error in start_user_container: " + str(e.message)) traceback.print_exc()