<commit_before># Copyright 2013 <NAME> and contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import celery
from containers.models import Container
import utils

@celery.task
def check_protected_containers():
    print('Checking protected containers...')
    protected_containers = Container.objects.filter(protected=True)
    for c in protected_containers:
        # get host containers
        cnt_ids = [utils.get_short_id(x.get('Id'))
            for x in c.host.get_containers()]
        # check if container is still running
        if c.container_id not in cnt_ids:
            print('Recovering: {}'.format(c.description))
            host = c.host
            meta = c.get_meta()
            cfg = meta.get('Config')
            image = cfg.get('Image')
            command = ' '.join(cfg.get('Cmd'))
            # TODO: update port spec to specify the NAT'd port
            # to maintain connectivity
            ports = cfg.get('PortSpecs')
            env = cfg.get('Env')
            mem = cfg.get('Memory')
            description = c.description
            volumes = cfg.get('Volumes')
            volumes_from = cfg.get('VolumesFrom')
            privileged = cfg.get('Privileged')
            owner = c.owner
            c_id, status = host.create_container(image, command, ports, env, mem, description,
                volumes, volumes_from, privileged, owner)
            new_c = Container.objects.get(container_id=c_id)
            # mark new container as protected
            new_c.protected = True
            new_c.save()
            # TODO: add new container to any applications that
            # previous container belonged to

            # remove old container meta
            c.delete()
    return True
<commit_msg>refs #48: recover container with same port mapping as original<commit_after># Copyright 2013 <NAME> and contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import celery
from containers.models import Container
import utils

@celery.task
def check_protected_containers():
    print('Checking protected containers...')
    protected_containers = Container.objects.filter(protected=True)
    for c in protected_containers:
        # get host containers
        c.host.invalidate_cache()
        cnt_ids = [utils.get_short_id(x.get('Id'))
            for x in c.host.get_containers()]
        # check if container is still running
        if c.container_id not in cnt_ids:
            print('Recovering: {}'.format(c.description))
            host = c.host
            meta = c.get_meta()
            cfg = meta.get('Config')
            image = cfg.get('Image')
            command = ' '.join(cfg.get('Cmd'))
            # TODO: update port spec to specify the NAT'd port
            # to maintain connectivity
            port_mapping = meta.get('NetworkSettings').get('PortMapping')
            port_specs = []
            for x,y in port_mapping.items():
                for k,v in y.items():
                    port_specs.append('{}:{}/{}'.format(v,k,x.lower()))
            env = cfg.get('Env')
            mem = cfg.get('Memory')
            description = c.description
            volumes = cfg.get('Volumes')
            volumes_from = cfg.get('VolumesFrom')
            privileged = cfg.get('Privileged')
            owner = c.owner
            c_id, status = host.create_container(image, command, port_specs,
                env, mem, description, volumes, volumes_from, privileged, owner)
            new_c = Container.objects.get(container_id=c_id)
            # mark new container as protected
            new_c.protected = True
            new_c.save()
            # TODO: add new container to any applications that
            # previous container belonged to

            # remove old container meta
            c.delete()
    return True

{'fraction_non_alphanumeric': 0.09202326547569589, 'fraction_numerical': 0.003739094308267553, 'mean_word_length': 2.339112343966713, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7441093', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1238, 'n_words': 430}