0

I have a problem with making vm`s images with additional disks attached. I use the capture method described here https://softlayer-python.readthedocs.io/en/v3.3.0/api/managers/cci.html. Although i have the additional_disks set to False, my images contain attachable devices as well. It affects all devices. Does anyone have any idea what can be wrong? Thanks in advance.

My script looks like followed:

import SoftLayer
import argparse
import sys
import datetime

def initialize_sl_connection():
    with open('/etc/default/sl_snapshots', 'r') as f:
        for line in f:
            if 'SL_USERNAME' in line:
                SL_USERNAME = line.rstrip().split('=')[1]
            if 'SL_API_KEY' in line:
                SL_API_KEY = line.rstrip().split('=')[1]
    client = SoftLayer.Client(username = SL_USERNAME, api_key = SL_API_KEY)
    mgr_vs = SoftLayer.VSManager(client)
    mgr_image = SoftLayer.ImageManager(client)
    return (mgr_vs, mgr_image)

def get_instance_id_by_hostname_and_datacenter(mgr_vs, hostname, datacenter):
    instance = (mgr_vs.list_instances(hostname = hostname, datacenter = datacenter)[0:]+[{}])[0]
    return instance['id'] if instance.has_key('id') else None

def capture_sl_snapshot(mgr_vs, hostname, additionaldisks, instance_id):
    name = hostname + '_' + str(datetime.date.today()).replace('-','_')
    mgr_vs.capture(instance_id = instance_id, name = name, additional_disks = additionaldisks, notes = None)

def clean_old_sl_snapshots(mgr_image, hostname):
    image_list = mgr_image.list_private_images(name = hostname+'*')
    number_of_snapshots = len(image_list)
    if number_of_snapshots > 4:
        for i in range(0, number_of_snapshots-4):
            mgr_image.delete_image(image_list[i]['id'])

def main():
    parser = argparse.ArgumentParser(
        description='Automate creating SL snapshots with Ansible.')
    parser.add_argument(
        '--hostname',
        dest='hostname',
        help='Servers hostname.',
        required=True)
    parser.add_argument(
        '--datacenter',
        dest='datacenter',
        help='Datacenters name.',
        required=True)
    parser.add_argument(
        '--additionaldisks',
        dest='additionaldisks',
        help='Create snapshot for any additional disk attached to the machine.',
        required=True)
    args = parser.parse_args()
    mgr_vs = initialize_sl_connection()[0]
    mgr_image = initialize_sl_connection()[1]
    instance_id = get_instance_id_by_hostname_and_datacenter(mgr_vs, args.hostname, args.datacenter)
    if instance_id:
        capture_sl_snapshot(mgr_vs, args.hostname, args.additionaldisks, instance_id)
        clean_old_sl_snapshots(mgr_image, args.hostname)

if __name__ == "__main__":
    main()

My call looks like this:

/usr/bin/python /opt/scripts/sl_take_snapshot.py --hostname my_servers_hostname --datacenter lon02 --additionaldisks False

/etc/defaults/sl_snapshots file has a following structure:

root@ip-10-0-3-84:~# cat /etc/default/sl_snapshots
SL_USERNAME=my_username
SL_API_KEY=my_key

Thank you in advance for any help provided.

Andrew S
  • 13
  • 2

1 Answers1

1

It seems that the issue is how you are parsing your values:

parser.add_argument(
        '--additionaldisks',
        dest='additionaldisks',
        help='Create snapshot for any additional disk attached to the machine.',
        required=True)

currently the value that you are parsign is an string not a booelan, so you are getting something like this:

additionaldisks = 'False'

For python empty strings are evaluate as False, but everything else is evaluated to True. So that's why you're coding is capturing all the disk.

If you do not want to capture all the disks you need to delete "--additionaldisks False" from your call. if you want to keep it in your call you neeed to change your parse code see this for more information Parsing boolean values with argparse

Regards

Community
  • 1
  • 1