Exemplo n.º 1
0
    def deploy(self, force=False, sys_path=None):
        """Run the CFNgin deploy action.

        Args:
            force (bool): Explicitly enable the action even if an environment
                file is not found.
            syspath (Optional[str]): Explicitly define a path to work in.
                If not provided, ``self.sys_path`` is used.

        """
        if self.should_skip(force):
            return
        if not sys_path:
            sys_path = self.sys_path
        config_file_names = self.find_config_files(sys_path=sys_path)

        with SafeHaven(environ=self.__ctx.env_vars,
                       sys_modules_exclude=['awacs', 'troposphere']):
            for config_name in config_file_names:
                LOGGER.info('%s: deploying...', os.path.basename(config_name))
                with SafeHaven(argv=['stacker', 'build', config_name],
                               sys_modules_exclude=['awacs', 'troposphere']):
                    ctx = self.load(config_name)
                    action = build.Action(
                        context=ctx,
                        provider_builder=self._get_provider_builder(
                            ctx.config.service_role))
                    action.execute(concurrency=self.concurrency,
                                   tail=self.tail)
Exemplo n.º 2
0
    def destroy(self, force=False, sys_path=None):
        """Run the CFNgin destroy action.

        Args:
            force (bool): Explicitly enable the action even if an environment
                file is not found.
            syspath (Optional[str]): Explicitly define a path to work in.
                If not provided, ``self.sys_path`` is used.

        """
        if self.should_skip(force):
            return
        if not sys_path:
            sys_path = self.sys_path
        config_file_names = self.find_config_files(sys_path=sys_path)
        # destroy should run in reverse to handle dependencies
        config_file_names.reverse()

        with SafeHaven(environ=self.__ctx.env_vars):
            for config_name in config_file_names:
                LOGGER.info('%s: destroying...', os.path.basename(config_name))
                with SafeHaven(argv=['stacker', 'destroy', config_name]):
                    ctx = self.load(config_name)
                    action = destroy.Action(
                        context=ctx,
                        provider_builder=self._get_provider_builder(
                            ctx.config.service_role))
                    action.execute(concurrency=self.concurrency,
                                   force=True,
                                   tail=self.tail)
Exemplo n.º 3
0
    def plan(self, force=False, sys_path=None):
        """Run the CFNgin plan action.

        Args:
            force (bool): Explicitly enable the action even if an environment
                file is not found.
            syspath (Optional[str]): Explicitly define a path to work in.
                If not provided, ``self.sys_path`` is used.

        """
        if self.should_skip(force):
            return
        if not sys_path:
            sys_path = self.sys_path
        config_file_names = self.find_config_files(sys_path=sys_path)
        with SafeHaven(environ=self.__ctx.env_vars):
            for config_name in config_file_names:
                LOGGER.info('%s: generating change sets...',
                            os.path.basename(config_name))
                with SafeHaven(argv=['stacker', 'diff', config_name]):
                    ctx = self.load(config_name)
                    action = diff.Action(
                        context=ctx,
                        provider_builder=self._get_provider_builder(
                            ctx.config.service_role))
                    action.execute()
Exemplo n.º 4
0
    def plan(self, force=False, sys_path=None):
        """Run the CFNgin plan action.

        Args:
            force (bool): Explicitly enable the action even if an environment
                file is not found.
            sys_path (Optional[str]): Explicitly define a path to work in.
                If not provided, ``self.sys_path`` is used.

        """
        if self.should_skip(force):
            return
        if not sys_path:
            sys_path = self.sys_path
        config_file_names = self.find_config_files(sys_path=sys_path)
        with SafeHaven(environ=self.__ctx.env_vars):
            for config_name in config_file_names:
                logger = PrefixAdaptor(os.path.basename(config_name), LOGGER)
                logger.notice("plan (in progress)")
                with SafeHaven(argv=["stacker", "diff", config_name]):
                    ctx = self.load(config_name)
                    action = diff.Action(
                        context=ctx,
                        provider_builder=self._get_provider_builder(
                            ctx.config.service_role),
                    )
                    action.execute()
                logger.success("plan (complete)")
Exemplo n.º 5
0
    def deploy(self, force=False, sys_path=None):
        """Run the CFNgin deploy action.

        Args:
            force (bool): Explicitly enable the action even if an environment
                file is not found.
            sys_path (Optional[str]): Explicitly define a path to work in.
                If not provided, ``self.sys_path`` is used.

        """
        if self.should_skip(force):
            return
        if not sys_path:
            sys_path = self.sys_path
        config_file_names = self.find_config_files(sys_path=sys_path)

        with SafeHaven(environ=self.__ctx.env_vars,
                       sys_modules_exclude=["awacs", "troposphere"]):
            for config_name in config_file_names:
                logger = PrefixAdaptor(os.path.basename(config_name), LOGGER)
                logger.notice("deploy (in progress)")
                with SafeHaven(
                        argv=["stacker", "build", config_name],
                        sys_modules_exclude=["awacs", "troposphere"],
                ):
                    ctx = self.load(config_name)
                    action = build.Action(
                        context=ctx,
                        provider_builder=self._get_provider_builder(
                            ctx.config.service_role),
                    )
                    action.execute(concurrency=self.concurrency,
                                   tail=self.tail)
                logger.success("deploy (complete)")
Exemplo n.º 6
0
    def test_context_manager_magic(self, caplog, monkeypatch):
        """Test init and the attributes it sets."""
        mock_reset_all = MagicMock()
        caplog.set_level(logging.DEBUG, 'runway.SafeHaven')
        monkeypatch.setattr(MODULE + '.os', MagicMock())
        monkeypatch.setattr(MODULE + '.sys', MagicMock())
        monkeypatch.setattr(SafeHaven, 'reset_all', mock_reset_all)

        with SafeHaven() as result:
            assert isinstance(result, SafeHaven)
        mock_reset_all.assert_called_once()
        assert caplog.messages == [
            'entering a safe haven...', 'leaving the safe haven...'
        ]
Exemplo n.º 7
0
    def test_sys_modules_exclude(self, monkeypatch):
        """Test sys.modules interations with excluded module."""
        monkeypatch.setattr(SafeHaven, 'reset_all', MagicMock())

        module = 'tests.fixtures.mock_hooks'

        assert module not in sys.modules
        with SafeHaven(sys_modules_exclude=module) as obj:
            from .fixtures import mock_hooks  # noqa pylint: disable=E,W,C
            assert module in sys.modules
            obj.reset_sys_modules()
        assert module in sys.modules
        # cleanup
        del sys.modules[module]
        assert module not in sys.modules
Exemplo n.º 8
0
    def test_sys_modules(self, caplog, monkeypatch):
        """Test sys.modules interactions."""
        caplog.set_level(logging.DEBUG, "runway.SafeHaven")
        monkeypatch.setattr(SafeHaven, "reset_all", MagicMock())

        # pylint: disable=unnecessary-comprehension
        orig_val = {k: v for k, v in sys.modules.items()}
        expected_logs = [
            "entering a safe haven...", "resetting sys.modules..."
        ]

        with SafeHaven() as obj:
            from .fixtures import mock_hooks  # noqa pylint: disable=E,W,C

            assert sys.modules != orig_val
            obj.reset_sys_modules()
        assert sys.modules == orig_val
        assert caplog.messages[:2] == expected_logs
        assert caplog.messages[-1] == "leaving the safe haven..."
Exemplo n.º 9
0
    def test_sys_path(self, provided, caplog, monkeypatch):
        """Test sys.path interactions."""
        caplog.set_level(logging.DEBUG, 'runway.SafeHaven')
        monkeypatch.setattr(SafeHaven, 'reset_all', MagicMock())

        orig_val = list(sys.path)
        expected_val = provided if isinstance(provided, list) else list(
            sys.path)
        expected_logs = [
            'entering a safe haven...',
            'resetting sys.path: %s' % orig_val, 'leaving the safe haven...'
        ]

        with SafeHaven(sys_path=provided) as obj:
            assert sys.path == expected_val
            sys.path.append('something-else')
            obj.reset_sys_path()
        assert sys.path == orig_val
        assert caplog.messages == expected_logs
Exemplo n.º 10
0
    def test_reset_all(self, caplog, monkeypatch):
        """Test reset_all."""
        mock_method = MagicMock()
        caplog.set_level(logging.DEBUG, 'runway.SafeHaven')
        monkeypatch.setattr(SafeHaven, 'reset_os_environ', mock_method)
        monkeypatch.setattr(SafeHaven, 'reset_sys_argv', mock_method)
        monkeypatch.setattr(SafeHaven, 'reset_sys_modules', mock_method)
        monkeypatch.setattr(SafeHaven, 'reset_sys_path', mock_method)

        expected_logs = [
            'entering a safe haven...', 'resetting all managed values...',
            'leaving the safe haven...', 'resetting all managed values...'
        ]

        with SafeHaven() as obj:
            obj.reset_all()
            assert mock_method.call_count == 4
        assert mock_method.call_count == 8  # called again on exit
        assert caplog.messages == expected_logs
Exemplo n.º 11
0
    def test_sys_path(self, provided, caplog, monkeypatch):
        """Test sys.path interactions."""
        caplog.set_level(logging.DEBUG, "runway.SafeHaven")
        monkeypatch.setattr(SafeHaven, "reset_all", MagicMock())

        orig_val = list(sys.path)
        expected_val = provided if isinstance(provided, list) else list(
            sys.path)
        expected_logs = [
            "entering a safe haven...",
            "resetting sys.path: %s" % json.dumps(orig_val),
            "leaving the safe haven...",
        ]

        with SafeHaven(sys_path=provided) as obj:
            assert sys.path == expected_val
            sys.path.append("something-else")
            obj.reset_sys_path()
        assert sys.path == orig_val
        assert caplog.messages == expected_logs
Exemplo n.º 12
0
    def test_os_environ(self, provided, caplog, monkeypatch):
        """Test os.environ interactions."""
        caplog.set_level(logging.DEBUG, 'runway.SafeHaven')
        monkeypatch.setattr(SafeHaven, 'reset_all', MagicMock())

        orig_val = dict(os.environ)
        expected_val = dict(os.environ)
        expected_logs = [
            'entering a safe haven...',
            'resetting os.environ: %s' % orig_val, 'leaving the safe haven...'
        ]

        if isinstance(provided, dict):
            expected_val.update(provided)

        with SafeHaven(environ=provided) as obj:
            assert os.environ == expected_val
            os.environ.update({'SOMETHING_ELSE': 'val'})
            obj.reset_os_environ()
        assert os.environ == orig_val
        assert caplog.messages == expected_logs
Exemplo n.º 13
0
    def test_os_environ(self, provided, caplog, monkeypatch):
        """Test os.environ interactions."""
        caplog.set_level(logging.DEBUG, "runway.SafeHaven")
        monkeypatch.setattr(SafeHaven, "reset_all", MagicMock())

        orig_val = dict(os.environ)
        expected_val = dict(os.environ)
        expected_logs = [
            "entering a safe haven...",
            "resetting os.environ: %s" % json.dumps(orig_val),
            "leaving the safe haven...",
        ]

        if isinstance(provided, dict):
            expected_val.update(provided)

        with SafeHaven(environ=provided) as obj:
            assert os.environ == expected_val
            os.environ.update({"SOMETHING_ELSE": "val"})
            obj.reset_os_environ()
        assert os.environ == orig_val
        assert caplog.messages == expected_logs