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