Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created May 20, 2010 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save softlayer/407058 to your computer and use it in GitHub Desktop.
Save softlayer/407058 to your computer and use it in GitHub Desktop.
"""
Perform a server OS reload with a new operating system.
Reload a server with a new operating system, software, and a custom drive
partition scheme. In this case we're loading a server with Windows 2008 R2
Standard Edition, 64-bit with a 50G C: partition and a D: partition that fills
up the rest of the disk's free space. SoftLayer's standard
reloadOperatingSystem() method in the SoftLayer_Hardware_Server API service
<http://sldn.softlayer.com/reference/services/SoftLayer_Hardware_Server/reloadOperatingSystem>
takes care of the heavy lifting after you've defined your new software options
and drive partitions. Please perform server backups as necessary before
reloading your operating system and note that changing operating systems may
affect your monthly charges.
This script is written for Python 2.x and assumes the SoftLayer API Python
client <http://github.com/softlayer/softlayer-api-python-client> is installed.
If you're using Python 3.x then adjust print() and exception catching syntax
accordingly.
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
License: http://sldn.softlayer.com/article/License
"""
# So we can talk to the SoftLayer API:
import SoftLayer.API
# Grab these from the SoftLayer portal.
username = "set me!"
apiKey = "set me too!"
"""
The id of the server we wish to reload. Call the getHardware() method from the
SoftLayer_Hardware API service to get a list of hardware on your account,
including id numbers.
"""
serverId = 1234
# Create a connection to the SoftLayer_Hardware_Server API service.
client = SoftLayer.API.Client('SoftLayer_Hardware_Server', serverId, username, apiKey)
"""
Determine the operating system to load.
The operating system and software you wish to load during your reload such as
control panels and databases are retrieved from SoftLayer's product catalog.
Your server's billing item is associated with the server package that was used
to order the server. That package has associated items for everything available
to order with that server, be they RAM upgrades, extra hard drives, RAID cards,
operating systems, networking upgrades, and extra services to add to your
server. Each of those items have price records that note how much each of these
features cost. We need those price ids when building OS reload options.
Use an object mask to retrieve our server's billing item, product package,
package items, and item prices. Loop through these items to look for one with
the description of the operating system we want to load. While you're looping
through items you can pull out other items and prices for any other software
you wish to load during your reload.
Check the SoftLayer_Hardware_Server data type's manual page
<http://sldn.softlayer.com/wiki/index.php/SoftLayer_Hardware_Server_(type)> to
see what other properties are available to retrieve in an object mask.
"""
osPriceId = None
client.set_object_mask({
'billingItem' : {
'package' : {
'items' : {
'prices' : {}
}
}
}
})
try:
server = client.getObject()
except Exception, e:
print "Unable to retrieve server information. ", e
exit()
for item in server['billingItem']['package']['items']:
if item['description'] == 'Windows Server 2008 R2 Standard Edition (64bit)':
osPriceId = item['prices'][0]['id']
if osPriceId == None:
print "UInable to locate an operating system price record."
exit()
"""
Define the new OS reload configuration.
Partitions, software, and other custom reload options exist in a
SoftLayer_Container_Hardware_Server_Configuration object
<http://sldn.softlayer.com/wiki/index.php/SoftLayer_Container_Hardware_Server_Configuration>.
The hardDrives property in your configuration object models your drive's
partitions. Each partition must have at least its name and minimumSize defined.
Name defines your partition's mount point, and minimumSize specifies its size
in gigabytes.
UNIX partition schemes must have at least one swap partition, defined by the
partition name /swap0. Set the partition's grow property to 1 to indicate that
partition should expand to fill the remainder of the disk. You must have one
grow partition in your partition scheme.
Windows partitioning is a little simpler. Specify the C: partition with the
name /os, with drives letters D:, E:, F:, etc as /disk0, /disk1, /disk2, and
so on.
This example sets up the following partition scheme:
* C: 50G
* D: grow to fill free space
Specify the complex type of the hard drive object as a
SoftLayer_Hardware_Component_HardDrive type so the SoftLayer API will know to
look for partitioning information.
The configuration dictionary's itemPrices value contains the list of new
software you wish to install during your reload. You must specify at least an
operating system, but you may add other software options you wish to this list.
The SoftLayer API only needs the price's id populated to know what to install.
"""
configuration = {
'hardDrives' : [
{
'complexType' : 'SoftLayer_Hardware_Component_HardDrive',
'partitions': [
{
'name' : '/os',
'minimumSize' : '50',
},
{
'name' : '/disk0',
'minimumSize' : '1',
'grow' : '1',
},
]
}
],
'itemPrices' : [
{
'id' : osPriceId,
},
],
}
"""
Perform the OS reload. Normally the OS reload method retuns a confirmation
token, which you pass to a second call to reloadOperatingSystem(). Passing the
parameter 'FORCE' ignores the confirmation key step and bypasses the need to
make two API calls.
An OS reload may take between 30 and 60 minutes depending on the software
installed.
"""
try:
result = client.reloadOperatingSystem('FORCE', configuration)
print "OS reload started!"
except Exception, e:
print "Unable to reload server. ", e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment