Skip to content

Commit

Permalink
wip: enable progress
Browse files Browse the repository at this point in the history
  • Loading branch information
orndorffgrant committed Jan 16, 2024
1 parent 8b8bdea commit b4292cf
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 76 deletions.
62 changes: 16 additions & 46 deletions features/api_enable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ Feature: u.pro.services.enable

Scenario Outline: u.pro.services.enable.v1 container services
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
<<<<<<< HEAD
When I apt update
And I apt install `jq`

Expand Down Expand Up @@ -34,14 +33,6 @@ Feature: u.pro.services.enable

# Basic enable
When I run shell command `pro api u.pro.services.enable.v1 --args service=esm-infra | jq .data.attributes` with sudo
=======
When I run `apt-get update` with sudo
And I apt install `jq`
And I attach `contract_token` with sudo and options `--no-auto-enable`
# Basic enable
And I run shell command `pro api u.pro.services.enable.v1 --args service=esm-infra | jq .data.attributes` with sudo
>>>>>>> 464f5f13 (api: add u.pro.services.enable.v1)
Then I will see the following on stdout:
"""
{
Expand All @@ -53,15 +44,7 @@ Feature: u.pro.services.enable
"reboot_required": false
}
"""
<<<<<<< HEAD
Then I verify that `esm-infra` is enabled
=======
When I run shell command `pro api u.pro.status.enabled_services.v1 | jq .data.attributes.enabled_services ` with sudo
Then stdout contains substring:
"""
esm-infra
"""
>>>>>>> 464f5f13 (api: add u.pro.services.enable.v1)
# Enable already enabled service succeeds
When I run shell command `pro api u.pro.services.enable.v1 --args service=esm-infra | jq .data.attributes` with sudo
Then I will see the following on stdout:
Expand Down Expand Up @@ -141,7 +124,6 @@ Feature: u.pro.services.enable
| release | machine_type |
| xenial | lxd-container |
| bionic | lxd-container |
<<<<<<< HEAD
Scenario Outline: u.pro.services.enable.v1 landscape
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
Expand All @@ -166,15 +148,6 @@ Feature: u.pro.services.enable
Scenario Outline: u.pro.services.enable.v1 vm services
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I apt update
=======
| focal | lxd-container |
| jammy | lxd-container |
Scenario Outline: u.pro.services.enable.v1 vm services
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I run `apt-get update` with sudo
>>>>>>> 464f5f13 (api: add u.pro.services.enable.v1)
And I apt install `jq`
And I attach `contract_token` with sudo and options `--no-auto-enable`
Expand Down Expand Up @@ -206,11 +179,7 @@ Feature: u.pro.services.enable
}
"""
# Enable with disable_incompatible works and variant works
<<<<<<< HEAD
When I run shell command `pro api u.pro.services.enable.v1 --data \"{\\\"service\\\": \\\"realtime-kernel\\\", \\\"variant\\\": \\\"intel-iotg\\\"}\" | jq .data.attributes` with sudo
=======
When I run shell command `pro api u.pro.services.enable.v1 --data \"{\\\"service\\\": \\\"realtime-kernel\\\", \\\"variant\\\": "intel-iotg"}\" | jq .data.attributes` with sudo
>>>>>>> 464f5f13 (api: add u.pro.services.enable.v1)
Then I will see the following on stdout:
"""
{
Expand All @@ -221,7 +190,6 @@ Feature: u.pro.services.enable
"realtime-kernel"
],
"messages": [],
<<<<<<< HEAD
"reboot_required": true
}
"""
Expand All @@ -239,20 +207,22 @@ Feature: u.pro.services.enable
Examples:
| release | machine_type |
| jammy | lxd-vm |
=======
"reboot_required": false
}
Scenario Outline: u.pro.services.enable.v1 with progress
Given a `<release>` `<machine_type>` machine with ubuntu-advantage-tools installed
When I run `apt-get update` with sudo
And I apt install `jq`
And I attach `contract_token` with sudo and options `--no-auto-enable`
# Basic enable
And I run shell command `pro api u.pro.services.enable.v1 --show-progress --args service=esm-infra` with sudo
Then I will see the following on stdout:
"""
When I run shell command `pro api u.pro.status.enabled_services.v1 | jq ".data.attributes.enabled_services | select(.name==\"realtime-kernel\")" ` with sudo
Then stdout contains substring:
{}
"""
{
"name": "realtime-kernel",
"variant_enabled": true,
"variant_name": "intel-iotg"
}
# Enabling multiple services shows steps correctly
When I run shell command `pro api u.pro.services.enable.v1 --show-progress --args service=ros` with sudo
Then I will see the following on stdout:
"""
{}
"""
Examples:
| release | machine_type |
| jammy | lxd-vm |
>>>>>>> 464f5f13 (api: add u.pro.services.enable.v1)
3 changes: 2 additions & 1 deletion uaclient/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import List, Optional # noqa: F401

from uaclient import (
api,
clouds,
config,
contract,
Expand Down Expand Up @@ -151,7 +152,7 @@ def enable_entitlement_by_name(
access_only=access_only,
extra_args=extra_args,
)
return entitlement.enable()
return entitlement.enable(api.ProgressWrapper())


def status(
Expand Down
56 changes: 56 additions & 0 deletions uaclient/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
import abc
import logging
from typing import Optional

# setup null handler for all API endpoints
logging.getLogger("ubuntupro").addHandler(logging.NullHandler())


class AbstractProgress(metaclass=abc.ABCMeta):
@abc.abstractmethod
def progress(
self,
*,
total_steps: int,
done_steps: int,
previous_step_message: Optional[str],
current_step_message: Optional[str]
):
pass


class NullProgress(AbstractProgress):
def progress(
self,
*,
total_steps: int,
done_steps: int,
previous_step_message: Optional[str],
current_step_message: Optional[str]
):
pass


class ProgressWrapper:
def __init__(self, progress_object: Optional[AbstractProgress] = None):
if progress_object is not None:
self.progress_object = progress_object
else:
self.progress_object = NullProgress()
self.done_steps = 0
self.total_steps = -1
self.previous_step_message = None # type: Optional[str]

def progress(self, message: str):
self.progress_object.progress(
total_steps=self.total_steps,
done_steps=self.done_steps,
previous_step_message=self.previous_step_message,
current_step_message=message,
)
self.previous_step_message = message
self.done_steps += 1

def start(self, total_steps: int, message: str):
self.total_steps = total_steps
self.progress(message)

def finish(self, message: str):
self.done_steps = self.total_steps
self.progress(message)
17 changes: 14 additions & 3 deletions uaclient/api/u/pro/services/enable/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import List, Optional

from uaclient import entitlements, event_logger, lock, messages, util
from uaclient.api import exceptions
from uaclient.api import AbstractProgress, ProgressWrapper, exceptions
from uaclient.api.api import APIEndpoint
from uaclient.api.data_types import AdditionalInfo
from uaclient.api.u.pro.status.enabled_services.v1 import _enabled_services
Expand Down Expand Up @@ -71,14 +71,19 @@ def _enabled_services_names(cfg: UAConfig) -> List[str]:
return [s.name for s in _enabled_services(cfg).enabled_services]


def enable(options: EnableOptions) -> EnableResult:
return _enable(options, UAConfig())
def enable(
options: EnableOptions, progress_object: Optional[AbstractProgress] = None
) -> EnableResult:
return _enable(options, UAConfig(), progress_object=progress_object)


def _enable(
options: EnableOptions,
cfg: UAConfig,
progress_object: Optional[AbstractProgress] = None,
) -> EnableResult:
progress = ProgressWrapper(progress_object)

event.set_event_mode(event_logger.EventLoggerMode.JSON)

if not util.we_are_currently_root():
Expand Down Expand Up @@ -111,6 +116,11 @@ def _enable(
access_only=options.access_only,
)

progress.start(
entitlement.calculate_total_enable_steps() + 1,
"Acquiring Ubuntu Pro lock file",
) # TODO i18n

success = False
fail_reason = None

Expand All @@ -120,6 +130,7 @@ def _enable(
lock_holder="u.pro.services.enable.v1",
):
success, fail_reason = entitlement.enable(
progress,
enable_required_services=options.enable_required_services,
disable_incompatible_services=options.disable_incompatible_services, # noqa: E501
api=True,
Expand Down
Loading

0 comments on commit b4292cf

Please sign in to comment.