def _add_service(services, definition, volume_map, host_log, high_priority_env): realname = definition[CONTAINER_DEF_NAME_KEY] img = definition[CONTAINER_DEF_IMG_KEY] links = definition.get(CONTAINER_DEF_LINKS_KEY, []) command = definition.get(CONTAINER_DEF_CMD_KEY, []) dockerrun_port_mappings = definition.get(CONTAINER_DEF_PORT_MAPPINGS_KEY, []) ports = _get_port_maps(dockerrun_port_mappings) remote_mountpoints = definition.get(MOUNT_POINTS, []) definition_env = EnvvarCollector(_get_definition_envvars(definition)) merged_envvars = definition_env.merge(high_priority_env).filtered().map privileged = definition.get(CONTAINER_DEF_PRIVILEGED_KEY, None) service = {COMPOSE_IMG_KEY: img} if command: service[COMPOSE_CMD_KEY] = command if ports: service[COMPOSE_PORTS_KEY] = ports if links: service[COMPOSE_LINKS_KEY] = [ '{}:{}'.format(_fakename(n), n) for n in links ] if merged_envvars: service[COMPOSE_ENV_KEY] = merged_envvars if privileged is not None: service[CONTAINER_DEF_PRIVILEGED_KEY] = privileged volumes = [] for mp in remote_mountpoints: src_vol = mp[SOURCE_VOLUME_KEY] container_path = mp[CONTAINER_PATH_KEY] read_only = mp.get(READ_ONLY_KEY) if src_vol in volume_map: src_path = volume_map[src_vol] elif src_vol.startswith(AWSEB_LOGS): dirname = src_vol[len(AWSEB_LOGS):] src_path = os.path.join(host_log, dirname) os.makedirs(src_path) else: continue volume = '{}:{}'.format(src_path, container_path) if read_only: volume += READ_ONLY_VOLUME volumes.append(volume) if volumes: service[COMPOSE_VOLUMES_KEY] = volumes # alias the container names because '-' character is not allowed for service # names in docker-compose.yml services[_fakename(realname)] = service
def setUp(self): self.soln_stk = dummy.get_soln_stk() self.fs_handler = dummy.get_multicontainer_fs_handler() self.pathconfig = self.fs_handler.pathconfig self.fs_handler.make_docker_compose = Mock() self.fs_handler.dockerrun = { dockerrun.VERSION_KEY: dockerrun.VERSION_TWO } self.fs_handler.get_setenv_env.return_value = EnvvarCollector({ 'a': '3', 'z': '0' }) self.env = EnvvarCollector({'a': '1', 'b': '2', 'c': '5'}) self.multicontainer = MultiContainer(fs_handler=self.fs_handler, soln_stk=self.soln_stk, opt_env=self.env)
def setenv(var_list, pathconfig=PathConfig): setenv_env = LocalState.get_envvarcollector(pathconfig.local_state_path()) opt_env = EnvvarCollector.from_str(','.join(var_list)) merged_env = setenv_env.merge(opt_env) LocalState.save_envvarcollector(merged_env.filtered(), pathconfig.local_state_path())
def test_merge_overlapping_and_vars_to_remove(self): env0 = EnvvarCollector({'a': '0', 'd': '1'}) env1 = EnvvarCollector({'a': '5', 'd': '5'}, {'d', 'c'}) self.assertEqual({'a': '5'}, env0.merge(env1).filtered().map) self.assertEqual({'a': '0'}, env1.merge(env0).filtered().map) self.assertSetEqual({'d', 'c'}, env0.merge(env1).to_remove) self.assertSetEqual({'d', 'c'}, env1.merge(env0).to_remove)
def test_merge_non_overlapping_envs(self): env0 = EnvvarCollector({'a': '0', 'b': '1'}) env1 = EnvvarCollector({'c': '3', 'd': '4'}) expected_envvars = {'a': '0', 'b': '1', 'c': '3', 'd': '4'} self.assertDictEqual(expected_envvars, env0.merge(env1).filtered().map) self.assertDictEqual(expected_envvars, env1.merge(env0).filtered().map) self.assertSetEqual(set(), env0.merge(env1).to_remove) self.assertSetEqual(set(), env1.merge(env0).to_remove)
def make_container(envvars_str=None, host_port=None, allow_insecure_ssl=False, pathconfig=PathConfig): """ Factory function for making a container or multicontainer. :param envvars_str: str: key=val str of environment variables :param host_port: str: optional host port mapped to container port :param allow_insecure_ssl: bool: allow insecure connection to docker registry :param pathconfig: PathConfig: Holds path/existence info :return Container/MultiContainer """ soln_stk = _determine_platform() container_cfg = containerops.get_configuration() opt_env = EnvvarCollector.from_str(envvars_str) if containerops.is_multi(soln_stk, container_cfg): return MultiContainer( fs_handler=make_multicontainer_fs_handler(pathconfig), opt_env=opt_env, allow_insecure_ssl=allow_insecure_ssl, soln_stk=soln_stk) elif containerops.is_generic(soln_stk, container_cfg): return GenericContainer( fs_handler=make_container_fs_handler(pathconfig), soln_stk=soln_stk, container_cfg=container_cfg, opt_env=opt_env, host_port=host_port) elif containerops.is_preconfigured(soln_stk, container_cfg): return PreconfiguredContainer( fs_handler=make_container_fs_handler(pathconfig), soln_stk=soln_stk, container_cfg=container_cfg, opt_env=opt_env, host_port=host_port) else: raise NotSupportedError(strings['local.unsupported'])
def test_setenv_delete_only(self, LocalState): setenv_env = EnvvarCollector({'a': '1', 'b': '2'}) var_list = ['a=', 'b='] expected_envvars = {} self._test_setenv(setenv_env, var_list, expected_envvars, LocalState)
def test_setenv_delete_and_overwrite(self, LocalState): setenv_env = EnvvarCollector({'a': '1', 'b': '2'}) var_list = ['a=', 'b=3', 'c=55'] expected_envvars = {'b': '3', 'c': '55'} self._test_setenv(setenv_env, var_list, expected_envvars, LocalState)
# or in the "license" file accompanying this file. This file 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 sys import unittest from mock import patch from unittest import TestCase from ebcli.containers import compose from ebcli.containers.envvarcollector import EnvvarCollector DOCKER_PROJ_PATH = '/a/b/c' HOST_LOG = '/a/b/c/.elasticbeanstalk/logs/local/12345_6789' ENV_COLLECTOR = EnvvarCollector({'a': 'b', 'c': 'd'}) class TestCompose(TestCase): def test_iter_services_simple(self): simple_compose = _get_expected_multicontainer_compose_dict_simple() actual_services = compose.iter_services(simple_compose) expected_services = ('nginxproxy', 'phpapp') self.assertListEqual(sorted(expected_services), sorted(actual_services)) def test_iter_services_complex(self): complex_compose = _get_expected_multicontainer_compose_dict_complex() actual_services = compose.iter_services(complex_compose) expected_services = ('nginxproxy', 'nodeapp', 'tomcatapp')
def loads(cls, path): try: data = fileoperations.read_from_data_file(path) return cPickle.loads(data) except IOError: # file doesn't exist, so no local state return cls(EnvvarCollector())
def test_empty_environment(self): self.assertDictEqual({}, EnvvarCollector().map) self.assertSetEqual(set(), EnvvarCollector().to_remove)
def loads(cls, path): try: data = fileoperations.read_from_data_file(path) return cPickle.loads(data) except IOError: return cls(EnvvarCollector())
def test_fitered_removed_no_envvars(self): envvars = {'a': '5', 'd': '5'} env = EnvvarCollector(envvars) result = env.filtered() self.assertDictEqual(envvars, result.map) self.assertSetEqual(set(), result.to_remove)
def test_fitered_removed_some_envvars(self): env = EnvvarCollector({'a': '5', 'd': '5'}, {'a'}) result = env.filtered() self.assertDictEqual({'d': '5'}, result.map) self.assertSetEqual(set(), result.to_remove)
def test_setenv_overwrite_only(self, LocalState): setenv_env = EnvvarCollector({'a': '1', 'b': '2'}) var_list = ['a=6', 'b=7'] expected_envvars = {'a': '6', 'b': '7'} self._test_setenv(setenv_env, var_list, expected_envvars, LocalState)
def setUp(self): self.envvarcollector = EnvvarCollector({'a': 'b', 'c': 'd'}) self.localstate = LocalState(self.envvarcollector)
# http://aws.amazon.com/apache2.0/ # # or in the 'license' file accompanying this file. This file 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. from unittest import TestCase from mock import patch, Mock from ebcli.containers.envvarcollector import EnvvarCollector from ebcli.containers.preconfigured_container import PreconfiguredContainer from tests.unit.containers import dummy OPT_ENV = EnvvarCollector({'a': '1', 'b': '555'}) SETENV_ENV = EnvvarCollector({'a': '350'}) EXPECTED_ENVVARS_MAP = {'a': '1', 'b': '555'} IMG_ID = '12345' class TestAbstractContainer(TestCase): def setUp(self): self.fs_handler = dummy.get_container_fs_handler() self.pathconfig = self.fs_handler.pathconfig self.cnt = PreconfiguredContainer(fs_handler=self.fs_handler, soln_stk=Mock(), container_cfg={}, opt_env=OPT_ENV) self.cnt._get_log_volume_map = Mock(return_value=None) self.cnt._containerize = Mock(return_value=False)