def processImageInfo(imageInspectOutput, imageInfo, nodeOSH, imageDict, OSHVResult): json = _JSONs() jsonOutput = json.loads(imageInspectOutput) inspectJsonObj = jsonOutput[0] imageName = imageInfo[0].strip() imageTag = imageInfo[1] imageIdShort = imageInfo[2] virtualSize = imageInfo[len(imageInfo) - 2] + imageInfo[len(imageInfo) - 1] imageId = inspectJsonObj['Id'] imageOSH = ObjectStateHolder('docker_image') if imageName == '<none>': imageOSH.setAttribute('name', 'Docker Image') else: imageOSH.setAttribute('name', imageName) imageOSH.setAttribute('docker_image_id', imageId) imageOSH.setAttribute('repository', imageName) imageOSH.setAttribute('tag', imageTag) imageOSH.setAttribute('virtual_size', virtualSize) imageOSH.setContainer(nodeOSH) OSHVResult.add(imageOSH) imageDict[imageId] = imageOSH
def processImageInfo(imageInspectOutput, imageInfo, nodeOSH, imageDict, OSHVResult): json = _JSONs() jsonOutput = json.loads(imageInspectOutput) inspectJsonObj = jsonOutput[0] imageName = imageInfo[0].strip() imageTag = imageInfo[1] imageIdShort = imageInfo[2] virtualSize = imageInfo[len(imageInfo) - 2] + imageInfo[len(imageInfo) - 1] imageId = inspectJsonObj['Id'] imageOSH = ObjectStateHolder('docker_image') if imageName == '<none>': imageOSH.setAttribute('name', 'Docker Image') else: imageOSH.setAttribute('name', imageName) imageOSH.setAttribute('docker_image_id', imageId) imageOSH.setAttribute('repository', imageName) imageOSH.setAttribute('tag', imageTag) imageOSH.setAttribute('virtual_size', virtualSize) imageOSH.setContainer(nodeOSH) OSHVResult.add(imageOSH) imageDict[imageId] = imageOSH
def processContainerInfo(shell, skipDockerVolume, filesystemDict, containerInspectOutput, containerInfo, imageDict, containerDict, containerLinks, dockerDaemonOSH, nodeOSH, client, Framework, OSHVResult): json = _JSONs() jsonOutput = json.loads(containerInspectOutput) inspectJsonObj = jsonOutput[0] imageName = containerInfo[1] containerName = containerInfo[2] if len(containerInfo) == 4: containerPorts = containerInfo[3] else: portsArray = [] ports = inspectJsonObj['NetworkSettings']['Ports'] port_keys = ports.keys() port_keys.sort() for port in port_keys: if ports[port]: if ports[port][0]['HostIp'] and ports[port][0]['HostPort']: portsArray.append(ports[port][0]['HostIp'] + ':' + ports[port][0]['HostPort'] + ' -> ' + port) else: portsArray.append(port) containerPorts = ', '.join(portsArray) # get container related image containerId = inspectJsonObj['Id'] imageId = inspectJsonObj['Image'] containerOSH = ObjectStateHolder('docker_container') containerOSH.setAttribute('name', containerName) containerOSH.setAttribute('docker_container_id', containerId) containerOSH.setAttribute('docker_image_id', imageId) containerOSH.setAttribute('docker_image', imageName) containerDict[containerId] = containerOSH # set container ports containerOSH.setAttribute('docker_container_ports', containerPorts) OSHVResult.add(containerOSH) containerOSH.setContainer(dockerDaemonOSH) daemonContainerLink = modeling.createLinkOSH('manage', dockerDaemonOSH, containerOSH) OSHVResult.add(daemonContainerLink) # get container links if inspectJsonObj['HostConfig']['Links'] is not None: for link in inspectJsonObj['HostConfig']['Links']: linkedContainer = link.split(':')[0].split('/')[1] containerInspectCMD = 'docker inspect -f {{.Id}} ' + linkedContainer linkedContainerId = shell.execCmd(containerInspectCMD) if shell.getLastCmdReturnCode() == 0: linkedContainerId = linkedContainerId.strip() containerLinks[containerId] = linkedContainerId else: Framework.reportError( ('Failed in command: docker inspect linked container <%s>.' % linkedContainer)) # link image and container logger.debug('imageOSH: ', imageDict[imageId]) logger.debug('containerOSH: ', containerOSH) imageContainerLink = modeling.createLinkOSH('realization', imageDict[imageId], containerOSH) OSHVResult.add(imageContainerLink) # get running software in container discoverRSinDockerContainer = Framework.getParameter('discoverRunningSW') if discoverRSinDockerContainer == 'true': topCmd = 'docker top ' + containerId topOutput = shell.execCmd(topCmd) logger.debug('docker top container: ', imageName) processList = [] topLines = None if shell.getLastCmdReturnCode() == 0: topLines = checkLastCmd(topOutput) else: Framework.reportWarning( ('Failed in command: docker top container <%s>.' % containerId)) if topLines: topcount = 0 for topLine in topLines: topcount += 1 if topcount == 1: continue topLine = topLine.strip() matcher = re.match( r'\s*(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(.+)\s+(.+)\s+(\d+):(\d+):(\d+)\s+(.+)', topLine) if matcher: owner = matcher.group(1) pid = matcher.group(2) commandLine = matcher.group(10) fullCommand = None argumentsLine = None if commandLine: tokens = re.split(r"\s+", commandLine, 1) fullCommand = tokens[0] if len(tokens) > 1: argumentsLine = tokens[1] commandName = fullCommand commandPath = None matcher = re.match(r"(.*/)([^/]+)$", fullCommand) if matcher: commandName = matcher.group(2) commandPath = fullCommand process = process_module.Process(commandName, pid, commandLine) logger.debug('process generated: ', process) process.argumentLine = argumentsLine process.owner = owner process.executablePath = commandPath processList.append(process) if len(processList) > 0: logger.debug('start apply to: ', containerOSH) appSign = applications.createApplicationSignature( Framework, client, shell) logger.debug('created ApplicationSignature: ', containerOSH) appSign.setProcessesManager( applications.ProcessesManager(processList, None)) logger.debug('ProcessesManager: ', containerOSH) appSign.getApplicationsTopology(containerOSH) logger.debug('finish apply to: ', containerOSH) # get container volumes if not skipDockerVolume: if inspectJsonObj.has_key('Mounts') and inspectJsonObj['Mounts']: mountResults = inspectJsonObj['Mounts'] for mountStr in mountResults: mount = mountStr dockerVolumeOSH = ObjectStateHolder('docker_volume') dockerVolumeOSH.setAttribute('name', 'Docker Volume') dockerVolumeOSH.setAttribute('dockervolume_source', mount['Source']) dockerVolumeOSH.setAttribute('dockervolume_destination', mount['Destination']) if mount['RW'] == 'true': dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'RW') else: dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'R') OSHVResult.add(dockerVolumeOSH) volumeContainerLink = modeling.createLinkOSH( 'usage', containerOSH, dockerVolumeOSH) OSHVResult.add(volumeContainerLink) linkDockerVolumeToLv(mount['Source'], filesystemDict, nodeOSH, dockerVolumeOSH, OSHVResult) elif inspectJsonObj.has_key('Volumes') and inspectJsonObj.has_key( 'VolumesRW') and inspectJsonObj['Volumes']: dockerVolumeOSH = ObjectStateHolder('docker_volume') dockerVolumeOSH.setAttribute('name', 'Docker Volume') OSHVResult.add(dockerVolumeOSH) volumeContainerLink = modeling.createLinkOSH( 'usage', containerOSH, dockerVolumeOSH) OSHVResult.add(volumeContainerLink) volumResults = inspectJsonObj['Volumes'] for (dst, src) in volumResults.items(): dockerVolumeOSH.setAttribute('dockervolume_source', src) dockerVolumeOSH.setAttribute('dockervolume_destination', dst) if inspectJsonObj['VolumesRW'][dst] == 'true': dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'RW') else: dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'R') linkDockerVolumeToLv(src, filesystemDict, nodeOSH, dockerVolumeOSH, OSHVResult) else: logger.debug('Skip Docker Volume since filesystem is not find!')
def processContainerInfo(shell, skipDockerVolume, filesystemDict, containerInspectOutput, containerInfo, imageDict, containerDict, containerLinks, dockerDaemonOSH, nodeOSH, client, Framework, OSHVResult): json = _JSONs() jsonOutput = json.loads(containerInspectOutput) inspectJsonObj = jsonOutput[0] imageName = containerInfo[1] containerName = containerInfo[2] if len(containerInfo) == 4: containerPorts = containerInfo[3] else: portsArray = [] ports = inspectJsonObj['NetworkSettings']['Ports'] port_keys = ports.keys() port_keys.sort() for port in port_keys: if ports[port]: if ports[port][0]['HostIp'] and ports[port][0]['HostPort']: portsArray.append(ports[port][0]['HostIp'] + ':' + ports[port][0]['HostPort'] + ' -> ' + port) else: portsArray.append(port) containerPorts = ', '.join(portsArray) # get container related image containerId = inspectJsonObj['Id'] imageId = inspectJsonObj['Image'] containerOSH = ObjectStateHolder('docker_container') containerOSH.setAttribute('name', containerName) containerOSH.setAttribute('docker_container_id', containerId) containerOSH.setAttribute('docker_image_id', imageId) containerOSH.setAttribute('docker_image', imageName) containerDict[containerId] = containerOSH # set container ports containerOSH.setAttribute('docker_container_ports', containerPorts) OSHVResult.add(containerOSH) containerOSH.setContainer(dockerDaemonOSH) daemonContainerLink = modeling.createLinkOSH('manage', dockerDaemonOSH, containerOSH) OSHVResult.add(daemonContainerLink) # get container links if inspectJsonObj['HostConfig']['Links'] is not None: for link in inspectJsonObj['HostConfig']['Links']: linkedContainer = link.split(':')[0].split('/')[1] containerInspectCMD = 'docker inspect -f {{.Id}} ' + linkedContainer linkedContainerId = shell.execCmd(containerInspectCMD) if shell.getLastCmdReturnCode() == 0: linkedContainerId = linkedContainerId.strip() containerLinks[containerId] = linkedContainerId else: Framework.reportError(('Failed in command: docker inspect linked container <%s>.' % linkedContainer)) # link image and container logger.debug('imageOSH: ', imageDict[imageId]) logger.debug('containerOSH: ', containerOSH) imageContainerLink = modeling.createLinkOSH('realization', imageDict[imageId], containerOSH) OSHVResult.add(imageContainerLink) # get running software in container discoverRSinDockerContainer = Framework.getParameter('discoverRunningSW') if discoverRSinDockerContainer == 'true': topCmd = 'docker top ' + containerId topOutput = shell.execCmd(topCmd) logger.debug('docker top container: ', imageName) processList = [] topLines = None if shell.getLastCmdReturnCode() == 0: topLines = checkLastCmd(topOutput) else: Framework.reportWarning(('Failed in command: docker top container <%s>.' % containerId)) if topLines: topcount = 0 for topLine in topLines: topcount += 1 if topcount == 1: continue topLine = topLine.strip() matcher = re.match(r'\s*(\w+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(.+)\s+(.+)\s+(\d+):(\d+):(\d+)\s+(.+)', topLine) if matcher: owner = matcher.group(1) pid = matcher.group(2) commandLine = matcher.group(10) fullCommand = None argumentsLine = None if commandLine: tokens = re.split(r"\s+", commandLine, 1) fullCommand = tokens[0] if len(tokens) > 1: argumentsLine = tokens[1] commandName = fullCommand commandPath = None matcher = re.match(r"(.*/)([^/]+)$", fullCommand) if matcher: commandName = matcher.group(2) commandPath = fullCommand process = process_module.Process(commandName, pid, commandLine) logger.debug('process generated: ', process) process.argumentLine = argumentsLine process.owner = owner process.executablePath = commandPath processList.append(process) if len(processList) > 0: logger.debug('start apply to: ', containerOSH) appSign = applications.createApplicationSignature(Framework, client, shell) logger.debug('created ApplicationSignature: ', containerOSH) appSign.setProcessesManager(applications.ProcessesManager(processList, None)) logger.debug('ProcessesManager: ', containerOSH) appSign.getApplicationsTopology(containerOSH) logger.debug('finish apply to: ', containerOSH) # get container volumes if not skipDockerVolume: if inspectJsonObj.has_key('Mounts') and inspectJsonObj['Mounts']: mountResults = inspectJsonObj['Mounts'] for mountStr in mountResults: mount = mountStr dockerVolumeOSH = ObjectStateHolder('docker_volume') dockerVolumeOSH.setAttribute('name', 'Docker Volume') dockerVolumeOSH.setAttribute('dockervolume_source', mount['Source']) dockerVolumeOSH.setAttribute('dockervolume_destination', mount['Destination']) if mount['RW'] == 'true': dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'RW') else: dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'R') OSHVResult.add(dockerVolumeOSH) volumeContainerLink = modeling.createLinkOSH('usage', containerOSH, dockerVolumeOSH) OSHVResult.add(volumeContainerLink) linkDockerVolumeToLv(mount['Source'], filesystemDict, nodeOSH, dockerVolumeOSH, OSHVResult) elif inspectJsonObj.has_key('Volumes') and inspectJsonObj.has_key('VolumesRW') and inspectJsonObj['Volumes']: dockerVolumeOSH = ObjectStateHolder('docker_volume') dockerVolumeOSH.setAttribute('name', 'Docker Volume') OSHVResult.add(dockerVolumeOSH) volumeContainerLink = modeling.createLinkOSH('usage', containerOSH, dockerVolumeOSH) OSHVResult.add(volumeContainerLink) volumResults = inspectJsonObj['Volumes'] for (dst, src) in volumResults.items(): dockerVolumeOSH.setAttribute('dockervolume_source', src) dockerVolumeOSH.setAttribute('dockervolume_destination', dst) if inspectJsonObj['VolumesRW'][dst] == 'true': dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'RW') else: dockerVolumeOSH.setAttribute('logicalvolume_accesstype', 'R') linkDockerVolumeToLv(src, filesystemDict, nodeOSH, dockerVolumeOSH, OSHVResult) else: logger.debug('Skip Docker Volume since filesystem is not find!')