@@ -699,7 +699,7 @@ class BootstrapManager:
699
699
def __init__ (self , temp_env_name , client , tear_down_client , bootstrap_host ,
700
700
machines , series , agent_url , agent_stream , region , log_dir ,
701
701
keep_env , permanent , jes_enabled , controller_strategy = None ,
702
- logged_exception_exit = True ):
702
+ logged_exception_exit = True , existing_controller = None ):
703
703
"""Constructor.
704
704
705
705
Please see see `BootstrapManager` for argument descriptions.
@@ -728,6 +728,10 @@ def __init__(self, temp_env_name, client, tear_down_client, bootstrap_host,
728
728
self .has_controller = False
729
729
self .resource_details = None
730
730
731
+ # We can probably just make this just 1 state.
732
+ self .existing_controller = existing_controller is not None
733
+ self .controller_id = existing_controller
734
+
731
735
def ensure_cleanup (self ):
732
736
"""
733
737
Ensure any required cleanup for the current substrate is done.
@@ -775,6 +779,9 @@ def tear_down_client(self):
775
779
776
780
@classmethod
777
781
def from_args (cls , args ):
782
+ if 'existing' in args and args .existing :
783
+ return cls ._from_existing_controller (args )
784
+
778
785
if not args .logs :
779
786
args .logs = generate_default_clean_dir (args .temp_env_name )
780
787
@@ -791,7 +798,11 @@ def from_args(cls, args):
791
798
return cls .from_client (args , client )
792
799
793
800
@classmethod
794
- def from_existing_controller (cls , args ):
801
+ def _from_existing_controller (cls , args ):
802
+ if 'existing' not in args :
803
+ raise RuntimeError (
804
+ 'Attempting to use existing controller without providing '
805
+ 'controller id.' )
795
806
try :
796
807
juju_home = os .environ ['JUJU_DATA' ]
797
808
except KeyError :
@@ -817,17 +828,18 @@ def from_existing_controller(cls, args):
817
828
controller_name = controller ,
818
829
model_name = model )
819
830
client .has_controller = True
820
- return cls .from_client_existing (args , client )
831
+ return cls .from_client_existing (args , client , args . existing )
821
832
822
833
@classmethod
823
- def from_client_existing (cls , args , client ):
834
+ def from_client_existing (cls , args , client , existing_controller ):
824
835
jes_enabled = client .is_jes_enabled ()
825
836
controller_strategy = ExistingController (client )
826
837
return cls (
827
838
args .temp_env_name , client , client , args .bootstrap_host ,
828
839
args .machine , args .series , args .agent_url , args .agent_stream ,
829
840
args .region , args .logs , args .keep_env , permanent = jes_enabled ,
830
- jes_enabled = jes_enabled , controller_strategy = controller_strategy )
841
+ jes_enabled = jes_enabled , controller_strategy = controller_strategy ,
842
+ existing_controller = existing_controller )
831
843
832
844
@classmethod
833
845
def from_client (cls , args , client ):
@@ -1108,22 +1120,26 @@ def booted_context(self, upload_tools, **kwargs):
1108
1120
:param **kwargs: All remaining keyword arguments are passed to the
1109
1121
client's bootstrap.
1110
1122
"""
1111
- try :
1112
- with self .top_context () as machines :
1113
- with self .bootstrap_context (
1114
- machines , omit_config = self .client .bootstrap_replaces ):
1115
- self .controller_strategy .create_initial_model (
1116
- upload_tools , self .series , kwargs )
1117
- with self .runtime_context (machines ):
1118
- self .client .list_controllers ()
1119
- self .client .list_models ()
1120
- for m_client in self .client .iter_model_clients ():
1121
- m_client .show_status ()
1122
- yield machines
1123
- except LoggedException :
1124
- if self .logged_exception_exit :
1125
- sys .exit (1 )
1126
- raise
1123
+ if self .existing_controller :
1124
+ yield self .existing_context (upload_tools , ** kwargs )
1125
+ else :
1126
+ try :
1127
+ with self .top_context () as machines :
1128
+ with self .bootstrap_context (
1129
+ machines ,
1130
+ omit_config = self .client .bootstrap_replaces ):
1131
+ self .controller_strategy .create_initial_model (
1132
+ upload_tools , self .series , kwargs )
1133
+ with self .runtime_context (machines ):
1134
+ self .client .list_controllers ()
1135
+ self .client .list_models ()
1136
+ for m_client in self .client .iter_model_clients ():
1137
+ m_client .show_status ()
1138
+ yield machines
1139
+ except LoggedException :
1140
+ if self .logged_exception_exit :
1141
+ sys .exit (1 )
1142
+ raise
1127
1143
1128
1144
@contextmanager
1129
1145
def existing_booted_context (self , upload_tools , ** kwargs ):
@@ -1142,13 +1158,15 @@ def existing_booted_context(self, upload_tools, **kwargs):
1142
1158
sys .exit (1 )
1143
1159
1144
1160
@contextmanager
1145
- def existing_context (self , upload_tools , controller_id ):
1161
+ def existing_context (self , upload_tools ):
1162
+ if controller_id is None :
1163
+ raise RuntimeError () # Lets make it so this isn't possible.
1146
1164
try :
1147
1165
with self .top_context () as machines :
1148
1166
with self .runtime_context (machines ):
1149
1167
self .has_controller = True
1150
- if controller_id != 'current' :
1151
- self .controller_strategy .prepare (controller_id )
1168
+ if self . controller_id != 'current' :
1169
+ self .controller_strategy .prepare (self . controller_id )
1152
1170
self .controller_strategy .create_initial_model ()
1153
1171
yield machines
1154
1172
except LoggedException :
@@ -1265,15 +1283,3 @@ def wait_for_state_server_to_shutdown(host, client, instance_id, timeout=60):
1265
1283
else :
1266
1284
raise Exception (
1267
1285
'{} was not deleted:' .format (instance_id ))
1268
-
1269
-
1270
- def test_on_controller (test , args ):
1271
- if args .existing :
1272
- bs_manager = BootstrapManager .from_existing_controller (args )
1273
- with bs_manager .existing_context (args .upload_tools ,
1274
- args .existing ):
1275
- test (bs_manager .client )
1276
- else :
1277
- bs_manager = BootstrapManager .from_args (args )
1278
- with bs_manager .booted_context (args .upload_tools ):
1279
- test (bs_manager .client )
0 commit comments