def _delete_legacy_release(self):
        """
        At some point the naming scheme for RStudio
        changed. This cause upgrade problems when
        an old release with the old release name is
        present.

        We're going to temporarily check/uninstall
        releases with the old name before installing
        the new release with the correct name.

        We can remove this once every user is on new naming
        scheme for RStudio.
        """
        if settings.EKS:
            old_release_name = f"{self.chart_name}-{self.user.slug}"
            if self.old_chart_name:
                # If an old_chart_name has been passed into the deployment, it
                # means the currently deployed instance of the tool is from a
                # different chart to the one for this tool. Therefore, it's
                # the old_chart_name that we should use for the old release
                # that needs deleting.
                old_release_name = f"{self.old_chart_name}-{self.user.slug}"
            if old_release_name in helm.list_releases(old_release_name,
                                                      self.k8s_namespace):
                helm.delete_eks(self.k8s_namespace, old_release_name)
        else:
            old_release_name = f"{self.user.slug}-{self.chart_name}"
            if old_release_name in helm.list_releases(old_release_name):
                helm.delete(old_release_name)
 def delete(self):
     aws.delete_role(self.user.iam_role_name)
     releases = helm.list_releases(namespace=self.k8s_namespace)
     # Delete all the user initialisation charts.
     releases.append(f"init-user-{self.user.slug}")
     releases.append(f"bootstrap-user-{self.user.slug}")
     releases.append(f"provision-user-{self.user.slug}")
     if settings.EKS:
         helm.delete_eks(self.k8s_namespace, *releases)
     else:
         helm.delete(*releases)
 def on_authenticate(self):
     """
     Run on each authenticated login on the control panel. Checks if the
     expected helm charts exist for the user. If not, will set things up
     properly. This function also checks if the user is ready to migrate
     and is logged into the new EKS infrastructure. If so, runs all the
     charts and AWS updates to cause the migration to be fulfilled.
     """
     init_chart_name = f"init-user-{self.user.slug}"
     bootstrap_chart_name = f"bootstrap-user-{self.user.slug}"
     provision_chart_name = f"provision-user-{self.user.slug}"
     releases = set(helm.list_releases(namespace=self.k8s_namespace))
     if settings.EKS:
         # On the new cluster, check if the bootstrap/provision helm
         # charts exist. If not, this is the user's first login to the new
         # platform. Run these helm charts to migrate the user to the new
         # platform. Ensure this is all stored in the database in case they
         # try to log into the control panel on the old infrastructure.
         has_charts = (bootstrap_chart_name in releases
                       and provision_chart_name in releases)
         is_migrated = (
             # This is an old user who has migrated.
             self.user.migration_state == self.user.COMPLETE or
             # This is a new user who has never used the old infra.
             self.user.migration_state == self.user.VOID)
         if not is_migrated:  # user requires one-off migration process.
             # Indicate the migration process is started for this user.
             self.user.migration_state = self.user.MIGRATING
             self.user.save()
             # Remove old infra's user init chart.
             helm.delete(self.k8s_namespace, init_chart_name)
             # Migrate the AWS roles for the user.
             aws.migrate_user_role(self.user)
             # Run the new charts to configure the user for EKS infra.
             self._init_user()
             # Update the user's state in the database.
             self.user.migration_state = self.user.COMPLETE
             self.user.save()
         elif not has_charts:  # user has charts deleted.
             # So recreate the user's charts.
             self._init_user()
     else:
         # On the old infrastructure...
         if init_chart_name not in releases:
             # The user has their charts deleted, so recreate.
             log.warning(f"Re-running init user chart for {self.user.slug}")
             self._init_user()
Ejemplo n.º 4
0
def test_list_releases_with_namespace():
    """
    Given a certain namespace, returns a list of the results.
    """
    mock_proc = MagicMock()
    mock_proc.stdout.read.return_value = "foo bar baz qux"
    mock_execute = MagicMock(return_value=mock_proc)
    with patch("controlpanel.api.helm._execute", mock_execute):
        result = helm.list_releases(namespace="some-ns")
        assert result == [
            "foo",
            "bar",
            "baz",
            "qux",
        ]
        mock_execute.assert_called_once_with("list", "-aq", "--namespace",
                                             "some-ns")