def get_container_logs(self, container, config, stderr=True, stdout=False, limit=100): wrapper = DockerWrapper(container.host.hostname, container.host.port, config) return wrapper.logs(container.id, stdout=stdout, stderr=stderr, tail=limit)
def update_host_containers(self, db, host, config): """ For a given host, update the application status and container state for all containers. """ wrapper = DockerWrapper(host.hostname, host.port, config) host_container_info = [] # Keep track of containers that go away previous_host_containers = dict([(container.id, container) for container in host.containers]) new_host_containers = set() try: host_container_info = wrapper.state_of_the_universe() except Exception as e: logging.error(e) host.status = 'down' db.add(host) for info in host_container_info: container = Container.get(db, info['id']) new_host_containers.add(info['id']) if container: logging.info("Found existing container {}, updating state to: {}".format(info['id'], info['state'])) container.state = info['state'] container.finished_at = parser.parse(info['finished_at']).astimezone(tz.tzlocal()).replace(tzinfo=None) db.add(container) else: logging.info("Got new container {}, setting state to: {}".format(info['id'], info['state'])) image_layer = info['image_id'][0:8] image = Image.get(db, image_layer) container = Container( id=info['id'], image_ref=info['image_ref'], state = info['state'], started_at = parser.parse(info['started_at']).astimezone(tz.tzlocal()).replace(tzinfo=None), finished_at = parser.parse(info['finished_at']).astimezone(tz.tzlocal()).replace(tzinfo=None) ) if 'command' in info: container.command = info['command'] if image: container.image = image host.containers.append(container) db.add(container) for c_id in previous_host_containers: if c_id not in new_host_containers: logging.info("Previous container {} not found on host, removing".format(c_id)) container = previous_host_containers[c_id] db.delete(container) db.add(host) db.commit()
def update_app_images(self, db, app): """ Update images for app from docker registry. If we know about existing containers that reference a newly fetched image, we attach the containers to the image """ logging.info("Fetching images for {} from {} ...".format( app.name, self._registry_url)) images = DockerWrapper.list_images(self._registry_url, app.name, self._registry_user, self._registry_password) for image_info in images: image = Image.get(db, image_info['layer']) if image: logging.info( "Found existing image {}, updating tag to {}".format( image_info['layer'], image_info['name'])) image.tag = image_info['name'] else: logging.info("Found new image {}, setting tag to {}".format( image_info['layer'], image_info['name'])) image = Image(id=image_info['layer'], tag=image_info['name']) for container in self.containers_for_app_image( db, app.name, image.tag): logging.info( "Found container {} with state: [{}], associated with image: {}, linking" .format(container.id, container.state, image.id)) image.containers.append(container) app.images.append(image) db.add(image) db.add(app) db.commit()
def update_app_images(self, db, app): """ Update images for app from docker registry. If we know about existing containers that reference a newly fetched image, we attach the containers to the image """ logging.info("Fetching images for {} from {} ...".format(app.name, self._registry_url)) images = DockerWrapper.list_images( self._registry_url, app.name, self._registry_user, self._registry_password) for image_info in images: image = Image.get(db, image_info['layer']) if image: logging.info("Found existing image {}, updating tag to {}".format(image_info['layer'], image_info['name'])) image.tag = image_info['name'] else: logging.info("Found new image {}, setting tag to {}".format(image_info['layer'], image_info['name'])) image = Image(id=image_info['layer'], tag=image_info['name']) for container in self.containers_for_app_image(db, app.name, image.tag): logging.info("Found container {} with state: [{}], associated with image: {}, linking".format( container.id, container.state, image.id )) image.containers.append(container) app.images.append(image) db.add(image) db.add(app) db.commit()
def launch_deployment(self, deployment_id, config): db = self.get_session(self.get_engine(config)) deployment = db.query(Deployment).get(deployment_id) encoded = deployment.encode_with_deps(db) logging.info("Launching deployment {}:{}/{}".format(deployment.app_name, deployment.image_tag, deployment.environment)) for host in deployment.hosts: logging.info("Launching deployment {}:{}/{} on {}".format(deployment.app_name, deployment.image_tag, deployment.environment, host.alias)) wrapper = DockerWrapper(host.hostname, host.port, config) wrapper.deploy_with_deps(encoded) try: self.update_deployment_containers(db, deployment, config) self.update_deployment_status(db, deployment) except Exception as e: logging.error(e) db.rollback() logging.info("Finished deployment {}:{}/{}".format(deployment.app_name, deployment.image_tag, deployment.environment))
def launch_deployment(self, deployment_id, config): db = self.get_session(self.get_engine(config)) deployment = db.query(Deployment).get(deployment_id) encoded = deployment.encode_with_deps(db) logging.info("Launching deployment {}:{}/{}".format( deployment.app_name, deployment.image_tag, deployment.environment)) for host in deployment.hosts: logging.info("Launching deployment {}:{}/{} on {}".format( deployment.app_name, deployment.image_tag, deployment.environment, host.alias)) wrapper = DockerWrapper(host.hostname, host.port, config) wrapper.deploy_with_deps(encoded) try: self.update_deployment_containers(db, deployment, config) self.update_deployment_status(db, deployment) except Exception as e: logging.error(e) db.rollback() logging.info("Finished deployment {}:{}/{}".format( deployment.app_name, deployment.image_tag, deployment.environment))
def update_host_containers(self, db, host, config): """ For a given host, update the application status and container state for all containers. """ wrapper = DockerWrapper(host.hostname, host.port, config) host_container_info = [] # Keep track of containers that go away previous_host_containers = dict([(container.id, container) for container in host.containers]) new_host_containers = set() try: host_container_info = wrapper.state_of_the_universe() except Exception as e: logging.error(e) host.status = 'down' db.add(host) for info in host_container_info: container = Container.get(db, info['id']) new_host_containers.add(info['id']) if container: logging.info( "Found existing container {}, updating state to: {}". format(info['id'], info['state'])) container.state = info['state'] container.finished_at = parser.parse( info['finished_at']).astimezone( tz.tzlocal()).replace(tzinfo=None) db.add(container) else: logging.info( "Got new container {}, setting state to: {}".format( info['id'], info['state'])) image_layer = info['image_id'][0:8] image = Image.get(db, image_layer) container = Container( id=info['id'], image_ref=info['image_ref'], state=info['state'], started_at=parser.parse(info['started_at']).astimezone( tz.tzlocal()).replace(tzinfo=None), finished_at=parser.parse(info['finished_at']).astimezone( tz.tzlocal()).replace(tzinfo=None)) if 'command' in info: container.command = info['command'] if image: container.image = image host.containers.append(container) db.add(container) for c_id in previous_host_containers: if c_id not in new_host_containers: logging.info( "Previous container {} not found on host, removing".format( c_id)) container = previous_host_containers[c_id] db.delete(container) db.add(host) db.commit()