def shutdown_container(self, user_id, kill=False, remove=False): container_id = self.get_user_container_id(user_id) container_port = self.get_user_port(user_id) try: if container_port: # Graceful shutdown sync_url = 'http://127.0.0.1:%s/sync' % container_port try: json_get(sync_url) time.sleep(2) except: pass try: exit_url = 'http://127.0.0.1:%s/exit' % container_port json_get(exit_url) except: pass if container_id: if kill: # Wait until process finishes, then kill it #self.docker(['wait', container_id]) self.docker(['kill', container_id]) if remove: self.docker(['rm', '-f', container_id]) except Exception, e: print 'Error shutting down container: %s' % e
def call_container(cmd, data=None, port=None, timeout=5, **kwargs): base_host = get_container_host() # from flask.ext.login import current_user # if current_user.is_anonymous: # raise Exception('Authentication required') # if not port: # port = current_user.get_port() # if not port or type(port) is not int: # raise Exception('Container not available') result = None if cmd == 'get_root_context_id': result, status_code = json_get('%s/get_root_context_id' % base_host, return_code=True, timeout=timeout) elif cmd == 'init_root_context_id': result, status_code = json_get('%s/init_root_context_id' % base_host, return_code=True, timeout=timeout) elif cmd == 'get_id_block': result, status_code = json_get('%s/get_id_block' % base_host, return_code=True, timeout=timeout) elif cmd == 'get_last_id': result, status_code = json_get('%s/get_last_id' % base_host, return_code=True, timeout=timeout) elif cmd == 'get_contexts': result, status_code = json_get('%s/data/contexts/%s' % (base_host, data), return_code=True, timeout=timeout) elif cmd == 'get_points': result, status_code = json_get('%s/data/points/%s' % (base_host, data), return_code=True, timeout=timeout) elif cmd == 'get_associations': result, status_code = json_get('%s/data/associations/%s' % (base_host, data), return_code=True, timeout=timeout) elif cmd == 'data_query': timeout = 15 if data: url = '%s/data/query/%s' % (base_host, data) else: url = '%s/data/query' % base_host result, status_code = json_get(url, return_code=True, timeout=timeout) if is_error(result): raise Exception('Container call error: %s' % result) elif status_code != 200: raise Exception('HTTP error response') return result
def verify_container(self, user_id): container_port = self.get_user_port(user_id) ready = False if container_port: ping_url = 'http://127.0.0.1:%s/ping' % container_port tries = 0 while not ready and tries < 10: result = json_get(ping_url, timeout=0.5) if result and result.get('Success'): ready = True break time.sleep(0.4) tries += 1 return ready