|
| 1 | +"""Order/Create a scale group.""" |
| 2 | +# :license: MIT, see LICENSE for more details. |
| 3 | + |
| 4 | +import click |
| 5 | +from SoftLayer import utils |
| 6 | + |
| 7 | +import SoftLayer |
| 8 | +from SoftLayer.CLI import environment |
| 9 | +from SoftLayer.CLI import exceptions |
| 10 | +from SoftLayer.CLI import formatting |
| 11 | +from SoftLayer.CLI import helpers |
| 12 | +from SoftLayer.managers.autoscale import AutoScaleManager |
| 13 | + |
| 14 | + |
| 15 | +@click.command() |
| 16 | +@click.option('--name', required=True, prompt=True, help="Scale group's name.") |
| 17 | +@click.option('--cooldown', required=True, prompt=True, type=click.INT, |
| 18 | + help="The number of seconds this group will wait after lastActionDate before performing another action.") |
| 19 | +@click.option('--min', 'minimum', required=True, prompt=True, type=click.INT, help="Set the minimum number of guests") |
| 20 | +@click.option('--max', 'maximum', required=True, prompt=True, type=click.INT, help="Set the maximum number of guests") |
| 21 | +@click.option('--regional', required=True, prompt=True, type=click.INT, |
| 22 | + help="The identifier of the regional group this scaling group is assigned to.") |
| 23 | +@click.option('--postinstall', '-i', help="Post-install script to download") |
| 24 | +@click.option('--os', '-o', required=True, prompt=True, help="OS install code. Tip: you can specify <OS>_LATEST") |
| 25 | +@click.option('--datacenter', '-d', required=True, prompt=True, help="Datacenter shortname") |
| 26 | +@click.option('--hostname', '-H', required=True, prompt=True, help="Host portion of the FQDN") |
| 27 | +@click.option('--domain', '-D', required=True, prompt=True, help="Domain portion of the FQDN") |
| 28 | +@click.option('--cpu', required=True, prompt=True, type=click.INT, |
| 29 | + help="Number of CPUs for new guests (existing not effected") |
| 30 | +@click.option('--memory', required=True, prompt=True, type=click.INT, |
| 31 | + help="RAM in MB or GB for new guests (existing not effected") |
| 32 | +@click.option('--policy-relative', required=True, prompt=True, |
| 33 | + help="The type of scale to perform(ABSOLUTE, PERCENT, RELATIVE).") |
| 34 | +@click.option('--termination-policy', |
| 35 | + help="The termination policy for the group(CLOSEST_TO_NEXT_CHARGE=1, NEWEST=2, OLDEST=3).") |
| 36 | +@click.option('--policy-name', help="Collection of policies for this group. This can be empty.") |
| 37 | +@click.option('--policy-amount', help="The number to scale by. This number has different meanings based on type.") |
| 38 | +@click.option('--userdata', help="User defined metadata string") |
| 39 | +@helpers.multi_option('--key', '-k', help="SSH keys to add to the root user") |
| 40 | +@helpers.multi_option('--disk', required=True, prompt=True, help="Disk sizes") |
| 41 | +@environment.pass_env |
| 42 | +def cli(env, **args): |
| 43 | + """Order/Create a scale group. |
| 44 | +
|
| 45 | + E.g. |
| 46 | +
|
| 47 | + 'slcli autoscale create --name test --cooldown 3600 --min 1 --max 2 -o CENTOS_7_64 --datacenter dal10 |
| 48 | + --termination-policy 2 -H testvs -D test.com --cpu 2 --memory 1024 --policy-relative absolute |
| 49 | + --policy-name policytest --policy-amount 3 --regional 102 --disk 25 --disk 30 --disk 25' |
| 50 | +
|
| 51 | + """ |
| 52 | + scale = AutoScaleManager(env.client) |
| 53 | + network = SoftLayer.NetworkManager(env.client) |
| 54 | + |
| 55 | + datacenter = network.get_datacenter(args.get('datacenter')) |
| 56 | + |
| 57 | + ssh_keys = [] |
| 58 | + for key in args.get('key'): |
| 59 | + resolver = SoftLayer.SshKeyManager(env.client).resolve_ids |
| 60 | + key_id = helpers.resolve_id(resolver, key, 'SshKey') |
| 61 | + ssh_keys.append(key_id) |
| 62 | + scale_actions = [ |
| 63 | + { |
| 64 | + "amount": args['policy_amount'], |
| 65 | + "scaleType": args['policy_relative'] |
| 66 | + } |
| 67 | + ] |
| 68 | + policy_template = { |
| 69 | + 'name': args['policy_name'], |
| 70 | + 'policies': scale_actions |
| 71 | + |
| 72 | + } |
| 73 | + policies = [] |
| 74 | + |
| 75 | + block = [] |
| 76 | + number_disk = 0 |
| 77 | + for guest_disk in args['disk']: |
| 78 | + if number_disk == 1: |
| 79 | + number_disk = 2 |
| 80 | + disks = {'diskImage': {'capacity': guest_disk}, 'device': number_disk} |
| 81 | + block.append(disks) |
| 82 | + number_disk += 1 |
| 83 | + |
| 84 | + virt_template = { |
| 85 | + 'localDiskFlag': False, |
| 86 | + 'domain': args['domain'], |
| 87 | + 'hostname': args['hostname'], |
| 88 | + 'sshKeys': ssh_keys, |
| 89 | + 'postInstallScriptUri': args.get('postinstall'), |
| 90 | + 'operatingSystemReferenceCode': args['os'], |
| 91 | + 'maxMemory': args.get('memory'), |
| 92 | + 'datacenter': {'id': datacenter[0]['id']}, |
| 93 | + 'startCpus': args.get('cpu'), |
| 94 | + 'blockDevices': block, |
| 95 | + 'hourlyBillingFlag': True, |
| 96 | + 'privateNetworkOnlyFlag': False, |
| 97 | + 'networkComponents': [{'maxSpeed': 100}], |
| 98 | + 'typeId': 1, |
| 99 | + 'userData': [{ |
| 100 | + 'value': args.get('userdata') |
| 101 | + }], |
| 102 | + 'networkVlans': [], |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + order = { |
| 107 | + 'name': args['name'], |
| 108 | + 'cooldown': args['cooldown'], |
| 109 | + 'maximumMemberCount': args['maximum'], |
| 110 | + 'minimumMemberCount': args['minimum'], |
| 111 | + 'regionalGroupId': args['regional'], |
| 112 | + 'suspendedFlag': False, |
| 113 | + 'balancedTerminationFlag': False, |
| 114 | + 'virtualGuestMemberTemplate': virt_template, |
| 115 | + 'virtualGuestMemberCount': 0, |
| 116 | + 'policies': policies.append(utils.clean_dict(policy_template)), |
| 117 | + 'terminationPolicyId': args['termination_policy'] |
| 118 | + } |
| 119 | + |
| 120 | + if not (env.skip_confirmations or formatting.confirm( |
| 121 | + "This action will incur charges on your account. Continue?")): |
| 122 | + raise exceptions.CLIAbort('Aborting scale group order.') |
| 123 | + |
| 124 | + result = scale.create(order) |
| 125 | + |
| 126 | + table = formatting.KeyValueTable(['name', 'value']) |
| 127 | + vsi_table = formatting.KeyValueTable(['Id', 'Domain', 'Hostmane']) |
| 128 | + table.align['name'] = 'r' |
| 129 | + table.align['value'] = 'l' |
| 130 | + table.add_row(['Id', result['id']]) |
| 131 | + table.add_row(['Created', result['createDate']]) |
| 132 | + table.add_row(['Name', result['name']]) |
| 133 | + for vsi in result['virtualGuestMembers']: |
| 134 | + vsi_table.add_row([vsi['virtualGuest']['id'], vsi['virtualGuest']['domain'], vsi['virtualGuest']['hostname']]) |
| 135 | + |
| 136 | + table.add_row(['VirtualGuests', vsi_table]) |
| 137 | + output = table |
| 138 | + |
| 139 | + env.fout(output) |
0 commit comments