本文整理匯總了Python中cloudinstall.placement.controller.PlacementController.gen_defaults方法的典型用法代碼示例。如果您正苦於以下問題:Python PlacementController.gen_defaults方法的具體用法?Python PlacementController.gen_defaults怎麽用?Python PlacementController.gen_defaults使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cloudinstall.placement.controller.PlacementController
的用法示例。
在下文中一共展示了PlacementController.gen_defaults方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_gen_defaults_uses_only_ready
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import gen_defaults [as 別名]
def test_gen_defaults_uses_only_ready(self):
"""gen_defaults should only use ready machines"""
mock_maas_state = MagicMock()
mock_maas_state.machines.return_value = []
c = Config()
pc = PlacementController(config=c, maas_state=mock_maas_state)
# reset the mock to avoid looking at calls from
# PlacementController.__init__().
mock_maas_state.reset_mock()
pc.gen_defaults()
# we simply check the first call because we know that
# follow-on calls are from calls to get_assignments and do
# not affect machines used for defaults
self.assertEqual(mock_maas_state.machines.mock_calls[0],
call(MaasMachineStatus.READY))
示例2: __init__
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import gen_defaults [as 別名]
#.........這裏部分代碼省略.........
def initialize(self):
"""Authenticates against juju/maas and sets up placement controller."""
if getenv("FAKE_API_DATA"):
self.juju_state = FakeJujuState()
self.maas_state = FakeMaasState()
else:
self.authenticate_juju()
if self.config.is_multi():
creds = self.config.getopt('maascreds')
self.maas, self.maas_state = connect_to_maas(creds)
self.placement_controller = PlacementController(
self.maas_state, self.config)
if path.exists(self.config.placements_filename):
with open(self.config.placements_filename, 'r') as pf:
self.placement_controller.load(pf)
self.ui.status_info_message("Loaded placements from file.")
log.info("Loaded placements from "
"'{}'".format(self.config.placements_filename))
# If we have no machines (so we are a fresh install) but
# are reading a placements.yaml from a previous install,
# so it has no assignments, only deployments, tell the
# controller to use the deployments in the file as
# assignments:
if len(self.placement_controller.machines_pending()) == 0 and \
len(self.juju_state.machines()) == 0:
self.placement_controller.set_assignments_from_deployments()
log.info("Using deployments saved from previous install"
" as new assignments.")
else:
if self.config.is_multi():
def_assignments = self.placement_controller.gen_defaults()
else:
def_assignments = self.placement_controller.gen_single()
self.placement_controller.set_all_assignments(def_assignments)
pfn = self.config.placements_filename
self.placement_controller.set_autosave_filename(pfn)
self.placement_controller.do_autosave()
if self.config.is_single():
if self.config.getopt('headless'):
self.begin_deployment()
else:
self.begin_deployment_async()
return
if self.config.getopt('edit_placement') or \
not self.placement_controller.can_deploy():
self.config.setopt(
'current_state', ControllerState.PLACEMENT.value)
else:
if self.config.getopt('headless'):
self.begin_deployment()
else:
self.begin_deployment_async()
@utils.async
def wait_for_maas_async(self):
""" explicit async method
"""
self.wait_for_maas()
示例3: PlacementControllerTestCase
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import gen_defaults [as 別名]
class PlacementControllerTestCase(unittest.TestCase):
def setUp(self):
self.mock_maas_state = MagicMock()
with NamedTemporaryFile(mode='w+', encoding='utf-8') as tempf:
utils.spew(tempf.name, yaml.dump(dict()))
self.conf = Config({}, tempf.name)
self.pc = PlacementController(self.mock_maas_state,
self.conf)
self.mock_machine = MagicMock(name='machine1')
pmid = PropertyMock(return_value='fake-instance-id-1')
type(self.mock_machine).instance_id = pmid
self.mock_machine_2 = MagicMock(name='machine2')
pmid2 = PropertyMock(return_value='fake-instance-id-2')
type(self.mock_machine_2).instance_id = pmid2
self.mock_machines = [self.mock_machine, self.mock_machine_2]
self.mock_maas_state.machines.return_value = self.mock_machines
def test_get_assignments_atype(self):
self.assertEqual(0,
len(self.pc.get_assignments(CharmNovaCompute)))
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
md = self.pc.get_assignments(CharmNovaCompute)
self.assertEqual(1, len(md))
self.assertEqual(2, len(md[AssignmentType.LXC]))
def _do_test_simple_assign_type(self, assignment_type):
self.pc.assign(self.mock_machine, CharmNovaCompute, assignment_type)
print("assignments is {}".format(self.pc.assignments))
machines = self.pc.get_assignments(CharmNovaCompute)
print('machines for charm is {}'.format(machines))
self.assertEqual(machines,
{assignment_type: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[assignment_type], [CharmNovaCompute])
def test_simple_assign_bare(self):
self._do_test_simple_assign_type(AssignmentType.BareMetal)
def test_simple_assign_lxc(self):
self._do_test_simple_assign_type(AssignmentType.LXC)
def test_simple_assign_kvm(self):
self._do_test_simple_assign_type(AssignmentType.KVM)
def test_assign_nonmulti(self):
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.LXC)
self.assertEqual(self.pc.get_assignments(CharmKeystone),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.KVM)
self.assertEqual(self.pc.get_assignments(CharmKeystone),
{AssignmentType.KVM: [self.mock_machine]})
am = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(am[AssignmentType.KVM], [CharmKeystone])
self.assertEqual(am[AssignmentType.LXC], [])
def test_assign_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.assertEqual(self.pc.get_assignments(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.KVM)
self.assertEqual(self.pc.get_assignments(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine],
AssignmentType.KVM: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[AssignmentType.LXC], [CharmNovaCompute])
self.assertEqual(ma[AssignmentType.KVM], [CharmNovaCompute])
def test_remove_assignment_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine_2, CharmNovaCompute,
AssignmentType.LXC)
mfc = self.pc.get_assignments(CharmNovaCompute)
mfc_lxc = set(mfc[AssignmentType.LXC])
self.assertEqual(mfc_lxc, set(self.mock_machines))
self.pc.clear_assignments(self.mock_machine)
self.assertEqual(self.pc.get_assignments(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine_2]})
def test_gen_defaults(self):
satisfies_importstring = 'cloudinstall.placement.controller.satisfies'
with patch(satisfies_importstring) as mock_satisfies:
mock_satisfies.return_value = (True, )
defs = self.pc.gen_defaults(charm_classes=[CharmNovaCompute,
CharmKeystone],
maas_machines=[self.mock_machine,
#.........這裏部分代碼省略.........
示例4: __init__
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import gen_defaults [as 別名]
#.........這裏部分代碼省略.........
else:
self.authenticate_juju()
if self.config.is_multi():
creds = self.config.getopt('maascreds')
self.maas, self.maas_state = connect_to_maas(creds)
self.placement_controller = PlacementController(
self.maas_state, self.config)
if path.exists(self.config.placements_filename):
try:
with open(self.config.placements_filename, 'r') as pf:
self.placement_controller.load(pf)
except Exception:
log.exception("Exception loading placement")
raise Exception("Could not load "
"{}.".format(self.config.placements_filename))
self.ui.status_info_message("Loaded placements from file")
log.info("Loaded placements from "
"'{}'".format(self.config.placements_filename))
# If we have no machines (so we are a fresh install) but
# are reading a placements.yaml from a previous install,
# so it has no assignments, only deployments, tell the
# controller to use the deployments in the file as
# assignments:
if len(self.placement_controller.machines_pending()) == 0 and \
len(self.juju_state.machines()) == 0:
self.placement_controller.set_assignments_from_deployments()
log.info("Using deployments saved from previous install"
" as new assignments.")
else:
if self.config.is_multi():
def_assignments = self.placement_controller.gen_defaults()
else:
def_assignments = self.placement_controller.gen_single()
self.placement_controller.set_all_assignments(def_assignments)
pfn = self.config.placements_filename
self.placement_controller.set_autosave_filename(pfn)
self.placement_controller.do_autosave()
if self.config.is_single():
if self.config.getopt('headless'):
self.begin_deployment()
else:
async.submit(self.begin_deployment,
self.ui.show_exception_message)
return
if self.config.getopt('edit_placement') or \
not self.placement_controller.can_deploy():
self.config.setopt(
'current_state', ControllerState.PLACEMENT.value)
else:
if self.config.getopt('headless'):
self.begin_deployment()
else:
async.submit(self.begin_deployment,
self.ui.show_exception_message)
def commit_placement(self):
self.config.setopt('current_state', ControllerState.SERVICES.value)
self.ui.render_services_view(self.nodes, self.juju_state,
self.maas_state, self.config)
示例5: __init__
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import gen_defaults [as 別名]
class DisplayController:
""" Controller for displaying juju and maas state."""
def __init__(self, ui=None, opts=None):
self.ui = ui
self.opts = opts
self.config = Config()
self.juju_state = None
self.juju = None
self.maas = None
self.maas_state = None
self.nodes = None
self.placement_controller = None
self.current_state = ControllerState.INSTALL_WAIT
def authenticate_juju(self):
if not len(self.config.juju_env['state-servers']) > 0:
state_server = 'localhost:17070'
else:
state_server = self.config.juju_env['state-servers'][0]
self.juju = JujuClient(
url=path.join('wss://', state_server),
password=self.config.juju_api_password)
self.juju.login()
self.juju_state = JujuState(self.juju)
log.debug('Authenticated against juju api.')
def initialize(self):
"""Authenticates against juju/maas and sets up placement controller."""
if getenv("FAKE_API_DATA"):
self.juju_state = FakeJujuState()
self.maas_state = FakeMaasState()
else:
self.authenticate_juju()
if self.config.is_multi:
creds = self.config.maas_creds
self.maas, self.maas_state = connect_to_maas(creds)
self.placement_controller = PlacementController(
self.maas_state, self.opts)
if path.exists(self.config.placements_filename):
with open(self.config.placements_filename, 'r') as pf:
self.placement_controller.load(pf)
self.info_message("Loaded placements from file.")
else:
if self.config.is_multi:
def_assignments = self.placement_controller.gen_defaults()
else:
def_assignments = self.placement_controller.gen_single()
self.placement_controller.set_all_assignments(def_assignments)
pfn = self.config.placements_filename
self.placement_controller.set_autosave_filename(pfn)
self.placement_controller.do_autosave()
if self.config.is_single:
self.begin_deployment_async()
return
if self.opts.edit_placement or \
not self.placement_controller.can_deploy():
self.current_state = ControllerState.PLACEMENT
else:
self.begin_deployment_async()
def begin_deployment_async(self):
"""To be overridden in subclasses."""
pass
def begin_deployment(self):
"""To be overridden in subclasses."""
pass
# overlays
def step_info(self, message):
with dialog_context(self):
self.ui.show_step_info(message)
def show_password_input(self, title, cb):
with dialog_context(self):
self.ui.show_password_input(title, cb)
def show_maas_input(self, title, cb):
with dialog_context(self):
self.ui.show_maas_input(title, cb)
def show_landscape_input(self, title, cb):
with dialog_context(self):
self.ui.show_landscape_input(title, cb)
def show_selector_info(self, title, opts, cb):
with dialog_context(self):
self.ui.show_selector_info(title, opts, cb)
def show_selector_with_desc(self, title, install_types, cb):
#.........這裏部分代碼省略.........
示例6: PlacementControllerTestCase
# 需要導入模塊: from cloudinstall.placement.controller import PlacementController [as 別名]
# 或者: from cloudinstall.placement.controller.PlacementController import gen_defaults [as 別名]
class PlacementControllerTestCase(unittest.TestCase):
def setUp(self):
self.mock_maas_state = MagicMock()
self.mock_opts = MagicMock()
swopt = PropertyMock(return_value=False)
type(self.mock_opts).enable_swift = swopt
self.pc = PlacementController(self.mock_maas_state,
self.mock_opts)
self.mock_machine = MagicMock(name='machine1')
pmid = PropertyMock(return_value='fake-instance-id-1')
type(self.mock_machine).instance_id = pmid
self.mock_machine_2 = MagicMock(name='machine2')
pmid2 = PropertyMock(return_value='fake-instance-id-2')
type(self.mock_machine_2).instance_id = pmid2
self.mock_machines = [self.mock_machine, self.mock_machine_2]
self.mock_maas_state.machines.return_value = self.mock_machines
def test_machines_for_charm_atype(self):
self.assertEqual(0, len(self.pc.machines_for_charm(CharmNovaCompute)))
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
md = self.pc.machines_for_charm(CharmNovaCompute)
self.assertEqual(1, len(md))
self.assertEqual(2, len(md[AssignmentType.LXC]))
def _do_test_simple_assign_type(self, assignment_type):
self.pc.assign(self.mock_machine, CharmNovaCompute, assignment_type)
print("assignments is {}".format(self.pc.assignments))
machines = self.pc.machines_for_charm(CharmNovaCompute)
print('machines for charm is {}'.format(machines))
self.assertEqual(machines,
{assignment_type: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[assignment_type], [CharmNovaCompute])
def test_simple_assign_bare(self):
self._do_test_simple_assign_type(AssignmentType.BareMetal)
def test_simple_assign_lxc(self):
self._do_test_simple_assign_type(AssignmentType.LXC)
def test_simple_assign_kvm(self):
self._do_test_simple_assign_type(AssignmentType.KVM)
def test_assign_nonmulti(self):
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.LXC)
self.assertEqual(self.pc.machines_for_charm(CharmKeystone),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmKeystone, AssignmentType.KVM)
self.assertEqual(self.pc.machines_for_charm(CharmKeystone),
{AssignmentType.KVM: [self.mock_machine]})
am = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(am[AssignmentType.KVM], [CharmKeystone])
self.assertEqual(am[AssignmentType.LXC], [])
def test_assign_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.assertEqual(self.pc.machines_for_charm(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine]})
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.KVM)
self.assertEqual(self.pc.machines_for_charm(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine],
AssignmentType.KVM: [self.mock_machine]})
ma = self.pc.assignments_for_machine(self.mock_machine)
self.assertEqual(ma[AssignmentType.LXC], [CharmNovaCompute])
self.assertEqual(ma[AssignmentType.KVM], [CharmNovaCompute])
def test_remove_assignment_multi(self):
self.pc.assign(self.mock_machine, CharmNovaCompute, AssignmentType.LXC)
self.pc.assign(self.mock_machine_2, CharmNovaCompute,
AssignmentType.LXC)
mfc = self.pc.machines_for_charm(CharmNovaCompute)
mfc_lxc = set(mfc[AssignmentType.LXC])
self.assertEqual(mfc_lxc, set(self.mock_machines))
self.pc.clear_assignments(self.mock_machine)
self.assertEqual(self.pc.machines_for_charm(CharmNovaCompute),
{AssignmentType.LXC: [self.mock_machine_2]})
def test_gen_defaults(self):
satisfies_importstring = 'cloudinstall.placement.controller.satisfies'
with patch(satisfies_importstring) as mock_satisfies:
mock_satisfies.return_value = (True, )
defs = self.pc.gen_defaults(charm_classes=[CharmNovaCompute,
CharmKeystone],
maas_machines=[self.mock_machine,
self.mock_machine_2])
#.........這裏部分代碼省略.........