43
43
def schedule_run_instance(self, context, instance_id, *_args, **_kwargs):
44
44
"""Picks a host that is up and has the fewest running instances."""
45
45
instance_ref = db.instance_get(context, instance_id)
46
if instance_ref['availability_zone'] and context.is_admin:
47
zone, _x, host = instance_ref['availability_zone'].partition(':')
48
service = db.service_get_by_args(context.elevated(), host,
50
if not self.service_is_up(service):
51
raise driver.WillNotSchedule(_("Host %s is not alive") % host)
53
# TODO(vish): this probably belongs in the manager, if we
54
# can generalize this somehow
55
now = datetime.datetime.utcnow()
56
db.instance_update(context, instance_id, {'host': host,
46
59
results = db.service_get_all_compute_sorted(context)
47
60
for result in results:
48
61
(service, instance_cores) = result
49
62
if instance_cores + instance_ref['vcpus'] > FLAGS.max_cores:
50
raise driver.NoValidHost("All hosts have too many cores")
63
raise driver.NoValidHost(_("All hosts have too many cores"))
51
64
if self.service_is_up(service):
52
65
# NOTE(vish): this probably belongs in the manager, if we
53
66
# can generalize this somehow
57
70
{'host': service['host'],
58
71
'scheduled_at': now})
59
72
return service['host']
60
raise driver.NoValidHost("No hosts found")
73
raise driver.NoValidHost(_("No hosts found"))
62
75
def schedule_create_volume(self, context, volume_id, *_args, **_kwargs):
63
76
"""Picks a host that is up and has the fewest volumes."""
64
77
volume_ref = db.volume_get(context, volume_id)
78
if (':' in volume_ref['availability_zone']) and context.is_admin:
79
zone, _x, host = volume_ref['availability_zone'].partition(':')
80
service = db.service_get_by_args(context.elevated(), host,
82
if not self.service_is_up(service):
83
raise driver.WillNotSchedule(_("Host %s not available") % host)
85
# TODO(vish): this probably belongs in the manager, if we
86
# can generalize this somehow
87
now = datetime.datetime.utcnow()
88
db.volume_update(context, volume_id, {'host': host,
65
91
results = db.service_get_all_volume_sorted(context)
66
92
for result in results:
67
93
(service, volume_gigabytes) = result
68
94
if volume_gigabytes + volume_ref['size'] > FLAGS.max_gigabytes:
69
raise driver.NoValidHost("All hosts have too many gigabytes")
95
raise driver.NoValidHost(_("All hosts have too many "
70
97
if self.service_is_up(service):
71
98
# NOTE(vish): this probably belongs in the manager, if we
72
99
# can generalize this somehow
76
103
{'host': service['host'],
77
104
'scheduled_at': now})
78
105
return service['host']
79
raise driver.NoValidHost("No hosts found")
106
raise driver.NoValidHost(_("No hosts found"))
81
108
def schedule_set_network_host(self, context, *_args, **_kwargs):
82
109
"""Picks a host that is up and has the fewest networks."""
85
112
for result in results:
86
113
(service, instance_count) = result
87
114
if instance_count >= FLAGS.max_networks:
88
raise driver.NoValidHost("All hosts have too many networks")
115
raise driver.NoValidHost(_("All hosts have too many networks"))
89
116
if self.service_is_up(service):
90
117
return service['host']
91
raise driver.NoValidHost("No hosts found")
118
raise driver.NoValidHost(_("No hosts found"))