Skip to content

Commit 47fb789

Browse files
#874 added 'vs migrate' command
1 parent 04c0bec commit 47fb789

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

SoftLayer/CLI/dedicatedhost/detail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@click.option('--guests', is_flag=True, help='Show guests on dedicated host')
2020
@environment.pass_env
2121
def cli(env, identifier, price=False, guests=False):
22-
"""Get details for a virtual server."""
22+
"""Get details for a dedicated host."""
2323
dhost = SoftLayer.DedicatedHostManager(env.client)
2424

2525
table = formatting.KeyValueTable(['name', 'value'])

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
('virtual:credentials', 'SoftLayer.CLI.virt.credentials:cli'),
4747
('virtual:capacity', 'SoftLayer.CLI.virt.capacity:cli'),
4848
('virtual:placementgroup', 'SoftLayer.CLI.virt.placementgroup:cli'),
49+
('virtual:migrate', 'SoftLayer.CLI.virt.migrate:cli'),
4950

5051
('dedicatedhost', 'SoftLayer.CLI.dedicatedhost'),
5152
('dedicatedhost:list', 'SoftLayer.CLI.dedicatedhost.list:cli'),

SoftLayer/CLI/virt/migrate.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""Manage Migrations of Virtual Guests"""
2+
# :license: MIT, see LICENSE for more details.
3+
import click
4+
5+
import SoftLayer
6+
from SoftLayer.CLI import environment
7+
from SoftLayer.CLI import formatting
8+
from SoftLayer import utils
9+
10+
11+
@click.command()
12+
@click.option('--guest', '-g', type=click.INT, help="Guest ID to immediately migrate.")
13+
@click.option('--all', '-a', 'migrate_all', is_flag=True, default=False,
14+
help="Migrate ALL guests that require migration immediately.")
15+
@click.option('--host', '-h', type=click.INT,
16+
help="Dedicated Host ID to migrate to. Only works on guests that are already on a dedicated host.")
17+
@environment.pass_env
18+
def cli(env, guest, migrate_all, host):
19+
"""Manage VSIs that require migration. Can migrate Dedicated Host VSIs as well."""
20+
21+
vsi = SoftLayer.VSManager(env.client)
22+
pending_filter = {'virtualGuests': {'pendingMigrationFlag': {'operation': 1}}}
23+
dedicated_filer = {'virtualGuests': {'dedicatedHost': {'id': {'operation': 'not null'}}}}
24+
mask = """mask[
25+
id, hostname, domain, datacenter, pendingMigrationFlag, powerState,
26+
primaryIpAddress,primaryBackendIpAddress, dedicatedHost
27+
]"""
28+
29+
# No options, just print out a list of guests that can be migrated
30+
if not (guest or migrate_all):
31+
require_migration = vsi.list_instances(filter=pending_filter, mask=mask)
32+
require_table = formatting.Table(['id', 'hostname', 'domain', 'datacenter'], title="Require Migration")
33+
34+
for vsi_object in require_migration:
35+
require_table.add_row([
36+
vsi_object.get('id'),
37+
vsi_object.get('hostname'),
38+
vsi_object.get('domain'),
39+
utils.lookup(vsi_object, 'datacenter', 'name')
40+
])
41+
42+
if require_migration:
43+
env.fout(require_table)
44+
else:
45+
click.secho("No guests require migration at this time", fg='green')
46+
47+
migrateable = vsi.list_instances(filter=dedicated_filer, mask=mask)
48+
migrateable_table = formatting.Table(['id', 'hostname', 'domain', 'datacenter', 'Host Name', 'Host Id'],
49+
title="Dedicated Guests")
50+
for vsi_object in migrateable:
51+
migrateable_table.add_row([
52+
vsi_object.get('id'),
53+
vsi_object.get('hostname'),
54+
vsi_object.get('domain'),
55+
utils.lookup(vsi_object, 'datacenter', 'name'),
56+
utils.lookup(vsi_object, 'dedicatedHost', 'name'),
57+
utils.lookup(vsi_object, 'dedicatedHost', 'id')
58+
])
59+
env.fout(migrateable_table)
60+
# Migrate all guests with pendingMigrationFlag=True
61+
elif migrate_all:
62+
require_migration = vsi.list_instances(filter=pending_filter, mask="mask[id]")
63+
for vsi_object in require_migration:
64+
migrate(vsi, guest)
65+
# Just migrate based on the options
66+
else:
67+
migrate(vsi, guest, host)
68+
69+
70+
def migrate(vsi_manager, vsi_id, host_id=None):
71+
"""Handles actually migrating virtual guests and handling the exception"""
72+
73+
try:
74+
if host_id:
75+
vsi_manager.migrate_dedicated(vsi_id, host_id)
76+
else:
77+
vsi_manager.migrate(vsi_id)
78+
click.secho("Started a migration on {}".format(vsi_id), fg='green')
79+
except SoftLayer.exceptions.SoftLayerAPIError as ex:
80+
click.secho("Failed to migrate {}. {}".format(vsi_id, str(ex)), fg='red')

SoftLayer/managers/vs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,3 +1158,23 @@ def get_local_disks(self, instance_id):
11581158
"""
11591159
mask = 'mask[diskImage]'
11601160
return self.guest.getBlockDevices(mask=mask, id=instance_id)
1161+
1162+
def migrate(self, instance_id):
1163+
"""Calls SoftLayer_Virtual_Guest::migrate
1164+
1165+
Only actually does anything if the virtual server requires a migration.
1166+
Will return an exception otherwise.
1167+
1168+
:param int instance_id: Id of the virtual server
1169+
"""
1170+
return self.guest.migrate(id=instance_id)
1171+
1172+
def migrate_dedicated(self, instance_id, host_id):
1173+
"""Calls SoftLayer_Virtual_Guest::migrate
1174+
1175+
Only actually does anything if the virtual server requires a migration.
1176+
Will return an exception otherwise.
1177+
1178+
:param int instance_id: Id of the virtual server
1179+
"""
1180+
return self.guest.migrateDedicatedHost(host_id, id=instance_id)

0 commit comments

Comments
 (0)