def start_container(): client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== containerName = request.form['containerName'] container = client.containers.get(containerName) try: container.start(wait = True) except Exception as e: response = Response({'success': False, 'message':' Container <{}> : '.format(containerName) + str(e) }) return response.success() IP_ADDR = "N/A" try: net_key = [key for key in container.state().network.keys() if key != 'lo'][0]#Exclude the LOOPBACK IP_ADDR = str(container.state().network[net_key]['addresses'][0]['address']) except: pass data = {'success': True, 'message':'Container ' +containerName+ ' successfully started', 'container_name' : containerName, 'ip' : IP_ADDR } response = Response(data) return response.success()
def network_post(): bridge_net = BridgeNetwork() result = bridge_net.validate_form(request.form) if result['error']: flash( result['message'] ,'error') else: lxc_terminal_tasks = bridge_net._form_to_LXC_SET_TASK(result['validated_data']) executed = bridge_net._execute_LXC_NETWORK_TERMINAL( lxc_terminal_tasks ) flash( str(executed['spitout']) ,'success') #restart all containers main_config = bridge_net.get_lxd_main_bridge_config() if 'used_by' in main_config: if len(main_config['used_by']) > 0: #====================== client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== for cnt_name in main_config['used_by']: container = client.containers.get(cnt_name) print(">> Restarting container <{}> !".format(cnt_name)) container.restart() return redirect(url_for('network_get'))
def delete_container(): client = connect() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== container = None try: containerName = request.form['containerName'] container = client.containers.get(containerName) except Exception, e: data = {'success': False, 'payload': 'There is no such container by name <{}> !'.format(containerName)} response = Response(data) return response.bad_request()
def local_images(): rez = list_of_local_images() if rez['error']: response = Response(rez) return response.bad_request() data = {'success': True, 'payload': rez['list']} response = Response(data) return response.success()
def delete_local_image(): client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== img_alias = request.form['image_alias'] try: local_image = client.images.get_by_alias(img_alias) local_image.delete(wait = True) data = {'success': True, 'payload': 'Image with alias <{}> has been successfully deleted !'.format(img_alias) , 'img_alias' : img_alias} response = Response(data) return response.success() except: data = {'success': False, 'payload': 'Image with alias <{}> has not found !'.format(img_alias)} response = Response(data) return response.bad_request()
def launch_container(): #============= img_alias = request.form['img_alias'] containerName = request.form['containerName'] #============= config = {'name': containerName, 'ephemeral': False,#'config': {'limits.cpu': '2', 'limits.memory': '1GB'}, 'source': {'type': 'image', 'alias': img_alias } } #============= client = connect() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== #pre-check container name if client.containers.exists(containerName): data = {'success': False, 'payload': "Container with name <{0}> can not be created because it already exists !".format( containerName ), 'move_next' : True } response = Response(data) return response.success() local_cached_OS = None try: local_cached_OS = client.images.get_by_alias( img_alias ) except Exception, e: if str(e).lower() == "not found" : #>>START download it locally try: linux_repo = Client(endpoint='https://images.linuxcontainers.org') except Exception, e: print str(e) #NO INTERNET data = {'success': False, 'payload': "NO INTERNET CONNECTION to download the LXC Image <{}> !".format( img_alias ), 'move_next' : False } response = Response(data) return response.success() try: selected_image = linux_repo.images.get_by_alias( img_alias ) except Exception, e: data = {'success': False, 'payload': "There is no LXC IMAGE from the LXC Linux official repo, identified by this alias <{0}> !".format( img_alias ), 'move_next' : False } response = Response(data) return response.success()
def stop_container(): client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== containerName = request.form['containerName'] container = client.containers.get(containerName) container.stop(wait = True) data = {'success': True, 'message': 'Container ' +containerName+ ' successfully stopped !', 'container_name' : containerName} response = Response(data) return response.success()
def delete_container(): client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== container = None try: containerName = request.form['containerName'] container = client.containers.get(containerName) except Exception as e: data = {'success': False, 'payload': 'There is no such container by name <{}> !'.format(containerName)} response = Response(data) return response.bad_request() if container.status.lower() == 'running': container.stop(wait = True) container.delete(wait=True) data = {'success': True, 'message': 'Container <{}> has been successfully deleted !'.format(containerName), "container_name" : containerName } response = Response(data) return response.success()
def start_container(): client = connect() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== containerName = request.form['containerName'] container = client.containers.get(containerName) try: container.start(wait = True) except Exception, e: response = Response({'success': False, 'message':' Container <{}> : '.format(containerName) + str(e) }) return response.success()
def check_cached_image(): img_alias = request.form['img_alias'] #------------------------------------ client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== try: target_img = client.images.get_by_alias(img_alias) data = {'success': True, 'image_exists' : True } response = Response(data) return response.success() except Exception as e: data = {'success': True, 'image_exists' : False } response = Response(data) return response.success()
def container_ip(container_name): client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== try: container = client.containers.get(container_name) data = { 'error' : False, "container_name" : container_name } loop_times = 8 while( (container.status == 'Running') and (loop_times > 0) ): arr_of_dicts = container.state().network.get('eth0')['addresses'] for item in arr_of_dicts: if item['family'].lower() == 'inet': data['IP'] = item['address'] #break response = Response(data) return response.success() loop_times -= 1 time.sleep(0.3) #out of the loop data['IP'] = "N/A" response = Response(data) return response.success() except Exception as e: data = { "error": True, "container_name" : container_name , "message": str(e).upper() } response = Response(data) return response.success()
def container_details(container_name=None): client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== try: container = client.containers.get(container_name) MAC_ADDR = 'N/A' PID = 'N/A' PROCESSES = 'N/A' NETWORK = 'N/A' RAM_MEM = 'N/A' CPU_USG = 'N/A' if container.status == "Running": PID = container.state().pid PROCESSES = container.state().processes NETWORK = container.state().network RAM_MEM = container.state().memory CPU_USG = container.state().cpu #rectify lo if 'lo' in NETWORK: del NETWORK['lo'] MAC_HWADDR_KEY = "volatile.eth0.hwaddr" for key in container.expanded_config.keys(): if "hwaddr" == key[-6:]: MAC_HWADDR_KEY = key if MAC_HWADDR_KEY in container.expanded_config.keys(): MAC_ADDR = str(container.expanded_config[MAC_HWADDR_KEY]).upper() IMAGE_OS = None if "image.distribution" in container.expanded_config.keys(): IMAGE_OS = str(container.expanded_config["image.distribution"]).capitalize() else: IMAGE_OS = str(container.expanded_config["image.os"]).capitalize() if "image.release" in container.expanded_config.keys(): IMAGE_REL = container.expanded_config["image.release"].capitalize() else: IMAGE_REL = str(container.expanded_config["image.version"]) OS_METADATA = {"name" : IMAGE_OS, "release" : IMAGE_REL, "architecture" : str(container.expanded_config["image.architecture"])} CONTAINER_RESOURCE_CONSTRAINS = [] for ex_c_k in container.expanded_config.keys(): if ex_c_k[:6] == 'limits': CONTAINER_RESOURCE_CONSTRAINS.append({ str(ex_c_k[7:]) : str(container.expanded_config[ex_c_k]) }) rez = { 'name': container.name, 'memory' : RAM_MEM, 'cpu_usage' : CPU_USG, 'OS' : OS_METADATA, 'architecture': container.architecture, 'created_at': container.created_at, 'status': str(container.status).upper(), 'profiles': container.profiles, 'MAC_addr': MAC_ADDR, 'pid': PID, 'process_count': PROCESSES, 'network_interfaces': NETWORK, 'resource_constrains' : CONTAINER_RESOURCE_CONSTRAINS} data_rez = {"error" : False, "result" : rez} return render_template('container-details.html', auto_refresh = "auto_refresh" in request.args, data_rez = data_rez, currentpage = "container / "+container_name ) except Exception as e: data_rez = {"error": True, "message": str(e).upper(), "c_name" : container_name} return render_template('container-details.html', data_rez = data_rez, currentpage = "container / "+container_name)
def launch_container(): #============= img_alias = request.form['img_alias'] containerName = request.form['containerName'] #============= config = {'name': containerName, 'ephemeral': False,#'config': {'limits.cpu': '2', 'limits.memory': '1GB'}, 'source': {'type': 'image', 'alias': img_alias } } #============= client = connector.remote() if client['error']: response = Response(client) return response.bad_request() client = client['conn'] #====================== #pre-check container name if client.containers.exists(containerName): data = {'success': False, 'payload': "Container with name <{0}> can not be created because it already exists !".format( containerName ), 'move_next' : True } response = Response(data) return response.success() local_cached_OS = None try: local_cached_OS = client.images.get_by_alias( img_alias ) except Exception as e: if str(e).lower() == "not found" : #>>START download it locally try: linux_repo = Client(endpoint='https://images.linuxcontainers.org') except Exception as e: print(str(e)) #NO INTERNET data = {'success': False, 'payload': "NO INTERNET CONNECTION to download the LXC Image <{}> !".format( img_alias ), 'move_next' : False } response = Response(data) return response.success() try: selected_image = linux_repo.images.get_by_alias( img_alias ) except Exception as e: data = {'success': False, 'payload': "There is no LXC IMAGE from the LXC Linux official repo, identified by this alias <{0}> !".format( img_alias ), 'move_next' : False } response = Response(data) return response.success() new_local_image = selected_image.copy(client, auto_update=False, public=False, wait=True) new_local_image.add_alias( img_alias, str(selected_image.properties["description"])) local_cached_OS = new_local_image #<<END download it locally #move on creating from LOCAL OS cached image new_container = client.containers.create( config, wait=True) try: new_container.start(wait = True) except Exception as e: response = Response({'success': False, 'payload':' Container <{}> : '.format(containerName) + str(e), 'move_next' : True}) return response.success() data = {'success': True, 'payload': "Container <{0}> has been successfully launched from image <{1}> !".format( containerName ,img_alias), 'container_name' : containerName, 'move_next' : True} response = Response(data) return response.success()