Coverage for mlos_bench/mlos_bench/environments/remote/host_env.py: 50%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 01:18 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Remote host Environment.""" 

6 

7import logging 

8from typing import Optional 

9 

10from mlos_bench.environments.base_environment import Environment 

11from mlos_bench.services.base_service import Service 

12from mlos_bench.services.types.host_provisioner_type import SupportsHostProvisioning 

13from mlos_bench.tunables.tunable_groups import TunableGroups 

14 

15_LOG = logging.getLogger(__name__) 

16 

17 

18class HostEnv(Environment): 

19 """Remote host environment.""" 

20 

21 def __init__( # pylint: disable=too-many-arguments 

22 self, 

23 *, 

24 name: str, 

25 config: dict, 

26 global_config: Optional[dict] = None, 

27 tunables: Optional[TunableGroups] = None, 

28 service: Optional[Service] = None, 

29 ): 

30 """ 

31 Create a new environment for host operations. 

32 

33 Parameters 

34 ---------- 

35 name: str 

36 Human-readable name of the environment. 

37 config : dict 

38 Free-format dictionary that contains the benchmark environment 

39 configuration. Each config must have at least the "tunable_params" 

40 and the "const_args" sections. 

41 global_config : dict 

42 Free-format dictionary of global parameters (e.g., security credentials) 

43 to be mixed in into the "const_args" section of the local config. 

44 tunables : TunableGroups 

45 A collection of tunable parameters for *all* environments. 

46 service: Service 

47 An optional service object (e.g., providing methods to 

48 deploy or reboot a VM/host, etc.). 

49 """ 

50 super().__init__( 

51 name=name, 

52 config=config, 

53 global_config=global_config, 

54 tunables=tunables, 

55 service=service, 

56 ) 

57 

58 assert self._service is not None and isinstance( 

59 self._service, SupportsHostProvisioning 

60 ), "HostEnv requires a service that supports host provisioning operations" 

61 self._host_service: SupportsHostProvisioning = self._service 

62 

63 def setup(self, tunables: TunableGroups, global_config: Optional[dict] = None) -> bool: 

64 """ 

65 Check if host is ready. (Re)provision and start it, if necessary. 

66 

67 Parameters 

68 ---------- 

69 tunables : TunableGroups 

70 A collection of groups of tunable parameters along with the 

71 parameters' values. HostEnv tunables are variable parameters that, 

72 together with the HostEnv configuration, are sufficient to provision 

73 and start a Host. 

74 global_config : dict 

75 Free-format dictionary of global parameters of the environment 

76 that are not used in the optimization process. 

77 

78 Returns 

79 ------- 

80 is_success : bool 

81 True if operation is successful, false otherwise. 

82 """ 

83 _LOG.info("Host set up: %s :: %s", self, tunables) 

84 if not super().setup(tunables, global_config): 

85 return False 

86 

87 (status, params) = self._host_service.provision_host(self._params) 

88 if status.is_pending(): 

89 (status, _) = self._host_service.wait_host_deployment(params, is_setup=True) 

90 

91 self._is_ready = status.is_succeeded() 

92 return self._is_ready 

93 

94 def teardown(self) -> None: 

95 """Shut down the Host and release it.""" 

96 _LOG.info("Host tear down: %s", self) 

97 (status, params) = self._host_service.deprovision_host(self._params) 

98 if status.is_pending(): 

99 (status, _) = self._host_service.wait_host_deployment(params, is_setup=False) 

100 

101 super().teardown() 

102 _LOG.debug("Final status of Host deprovisioning: %s :: %s", self, status)