Beispiel #1
0
def create_stack_extended(ctx, config_source, working_directory, **kwargs):

    config_source = os.path.expandvars(os.path.expanduser(config_source))
    if not os.path.exists(config_source):
        click.echo('Config source does not exist', file=sys.stderr)
        ctx.abort()

    if not (config_source.endswith(".zip") and os.path.isfile(config_source)
            and zipfile.is_zipfile(config_source)):
        click.echo('Config source must be a .zip file.', file=sys.stderr)
        ctx.abort()

    send_value = create_base64encoded_zip(config_source)
    if not send_value:
        click.echo('Internal error: Unable to generate encoded zip',
                   file=sys.stderr)
        ctx.abort()

    kwargs['config_source'] = {
        'configSourceType':
        oci.resource_manager.models.ConfigSource.CONFIG_SOURCE_TYPE_ZIP_UPLOAD,
        'zipFileBase64Encoded': send_value
    }

    if working_directory is not None:
        kwargs['config_source']['workingDirectory'] = working_directory

    json_skeleton_utils.remove_json_skeleton_params_from_dict(kwargs)
    ctx.invoke(resourcemanager_cli.create_stack, **kwargs)
Beispiel #2
0
def create_volume_extended(ctx, **kwargs):
    if kwargs['source_volume_id'] and kwargs['volume_backup_id']:
        raise click.UsageError(
            'You cannot specify both the --volume-backup-id and --source-volume-id options'
        )

    if not kwargs['source_volume_id'] and not kwargs['availability_domain']:
        raise click.UsageError(
            'An availability domain must be specified when creating an empty volume or restoring a volume from a backup'
        )

    if not kwargs['source_volume_id'] and not kwargs[
            'volume_backup_id'] and not kwargs['compartment_id']:
        raise click.UsageError(
            'A compartment ID must be specified when creating an empty volume')

    if kwargs['size_in_mbs'] and kwargs['size_in_gbs']:
        raise click.UsageError(
            'You cannot specify both --size-in-mbs and --size-in-gbs')

    client = cli_util.build_client('core', 'blockstorage', ctx)

    if kwargs['source_volume_id']:
        source_volume = client.get_volume(volume_id=kwargs['source_volume_id'])
        if not kwargs['availability_domain']:
            kwargs[
                'availability_domain'] = source_volume.data.availability_domain
        if not kwargs['compartment_id']:
            kwargs['compartment_id'] = source_volume.data.compartment_id

    if kwargs['volume_backup_id']:
        source_backup = client.get_volume_backup(
            volume_backup_id=kwargs['volume_backup_id'])
        if not kwargs['compartment_id']:
            kwargs['compartment_id'] = source_backup.data.compartment_id

    if kwargs['source_volume_id'] or kwargs['volume_backup_id']:
        if kwargs['volume_backup_id']:
            source_details = {
                'type': 'volumeBackup',
                'id': kwargs['volume_backup_id']
            }
        else:
            source_details = {
                'type': 'volume',
                'id': kwargs['source_volume_id']
            }

        kwargs['source_details'] = json.dumps(source_details)

    kwargs.pop('source_volume_id', None)
    kwargs.pop('volume_backup_id', None)

    json_skeleton_utils.remove_json_skeleton_params_from_dict(kwargs)

    ctx.invoke(blockstorage_cli.create_volume, **kwargs)
Beispiel #3
0
def create_boot_volume_extended(ctx, **kwargs):
    if kwargs['source_boot_volume_id'] and kwargs['boot_volume_backup_id']:
        raise click.UsageError(
            'You cannot specify both the --boot-volume-backup-id and --source-boot-volume-id options'
        )

    if not kwargs['source_boot_volume_id'] and not kwargs[
            'boot_volume_backup_id']:
        raise click.UsageError(
            'An empty boot volume cannot be created. Please specify either --boot-volume-backup-id or --source-boot-volume-id'
        )

    if not kwargs['source_boot_volume_id'] and not kwargs[
            'availability_domain']:
        raise click.UsageError(
            'An availability domain must be specified when restoring a boot volume from backup'
        )

    client = cli_util.build_client('core', 'blockstorage', ctx)

    if kwargs['source_boot_volume_id']:
        source_boot_volume = client.get_boot_volume(
            boot_volume_id=kwargs['source_boot_volume_id'])
        if not kwargs['availability_domain']:
            kwargs[
                'availability_domain'] = source_boot_volume.data.availability_domain
        if not kwargs['compartment_id']:
            kwargs['compartment_id'] = source_boot_volume.data.compartment_id

    if kwargs['boot_volume_backup_id']:
        source_backup = client.get_boot_volume_backup(
            boot_volume_backup_id=kwargs['boot_volume_backup_id'])
        if not kwargs['compartment_id']:
            kwargs['compartment_id'] = source_backup.data.compartment_id

    if kwargs['source_boot_volume_id'] or kwargs['boot_volume_backup_id']:
        if kwargs['boot_volume_backup_id']:
            source_details = {
                'type': 'bootVolumeBackup',
                'id': kwargs['boot_volume_backup_id']
            }
        else:
            source_details = {
                'type': 'bootVolume',
                'id': kwargs['source_boot_volume_id']
            }

        kwargs['source_details'] = json.dumps(source_details)

    kwargs.pop('source_boot_volume_id', None)
    kwargs.pop('boot_volume_backup_id', None)

    json_skeleton_utils.remove_json_skeleton_params_from_dict(kwargs)

    ctx.invoke(blockstorage_cli.create_boot_volume, **kwargs)
def create_instance_console_connection(ctx, from_json, instance_id, ssh_public_key_file, wait_for_state, max_wait_seconds, wait_interval_seconds, defined_tags, freeform_tags):
    # Empirically, if the public key file contains multiple entires this is accepted but the serial console
    # will use the first key in the file
    kwargs = {
        'instance_id': instance_id,
        'public_key': ssh_public_key_file.read(),
        'wait_for_state': wait_for_state,
        'max_wait_seconds': max_wait_seconds,
        'wait_interval_seconds': wait_interval_seconds,
        'defined_tags': defined_tags,
        'freeform_tags': freeform_tags
    }

    json_skeleton_utils.remove_json_skeleton_params_from_dict(kwargs)

    ctx.invoke(compute_cli.create_instance_console_connection, **kwargs)
Beispiel #5
0
def launch_instance_extended(ctx, **kwargs):
    metadata = {}
    if kwargs['metadata']:
        metadata = cli_util.parse_json_parameter("metadata",
                                                 kwargs['metadata'])

    user_data_file = kwargs['user_data_file']
    if user_data_file:
        if 'user_data' in metadata:
            raise click.UsageError(
                'Cannot specify CloudInit user-data as part of both --user-data-file and --metadata.'
            )
        else:
            content = b64encode(user_data_file.read()).decode('utf-8')
            metadata['user_data'] = content

    ssh_authorized_keys_file = kwargs['ssh_authorized_keys_file']
    if ssh_authorized_keys_file:
        if 'ssh_authorized_keys' in metadata:
            raise click.UsageError(
                'Cannot specify ssh-authorized-keys as part of both --ssh-authorized-keys-file and --metadata.'
            )
        else:
            metadata['ssh_authorized_keys'] = ssh_authorized_keys_file.read()

    if kwargs.get('source_details') and (
            kwargs.get('image_id') or kwargs.get('source_boot_volume_id')
            or kwargs.get('boot_volume_size_in_gbs')):
        raise click.UsageError(
            'Cannot specify --source-details with any of: --image-id, --source-boot-volume-id, or --boot-volume-size-in-gbs'
        )
    if kwargs.get('image_id') and kwargs.get('source_boot_volume_id'):
        raise click.UsageError(
            'Cannot specify both an --image-id and a --source-boot-volume-id to be used to boot the instance'
        )
    if kwargs.get('boot_volume_size_in_gbs') and kwargs.get(
            'source_boot_volume_id'):
        raise click.UsageError(
            'Cannot specify both a --source-boot-volume-id and a --boot-volume-size-in-gbs to be used to boot the instance'
        )

    kwargs['metadata'] = json.dumps(metadata)

    create_vnic_details = {}
    if 'assign_public_ip' in kwargs and kwargs['assign_public_ip'] is not None:
        create_vnic_details['assignPublicIp'] = kwargs['assign_public_ip']

    if 'skip_source_dest_check' in kwargs and kwargs[
            'skip_source_dest_check'] is not None:
        create_vnic_details['skipSourceDestCheck'] = kwargs[
            'skip_source_dest_check']

    if kwargs['hostname_label']:
        create_vnic_details['hostnameLabel'] = kwargs['hostname_label']

    if kwargs['private_ip']:
        create_vnic_details['privateIp'] = kwargs['private_ip']

    if kwargs['subnet_id']:
        create_vnic_details['subnetId'] = kwargs['subnet_id']

    if kwargs['vnic_display_name']:
        create_vnic_details['displayName'] = kwargs['vnic_display_name']

    if len(create_vnic_details) > 0:
        kwargs['create_vnic_details'] = json.dumps(create_vnic_details)

    if kwargs.get('image_id'):
        source_details = {'sourceType': 'image', 'imageId': kwargs['image_id']}
        if kwargs.get('boot_volume_size_in_gbs'):
            source_details['bootVolumeSizeInGBs'] = kwargs.get(
                'boot_volume_size_in_gbs')

        kwargs['source_details'] = json.dumps(source_details)
    if kwargs.get('source_boot_volume_id'):
        kwargs['source_details'] = json.dumps({
            'sourceType':
            'bootVolume',
            'bootVolumeId':
            kwargs['source_boot_volume_id']
        })

    # delete additional kwargs because launch_instance will not recognize them
    del kwargs['assign_public_ip']
    del kwargs['hostname_label']
    del kwargs['private_ip']
    del kwargs['ssh_authorized_keys_file']
    del kwargs['subnet_id']
    del kwargs['user_data_file']
    del kwargs['vnic_display_name']
    del kwargs['skip_source_dest_check']

    # Remove the source_boot_volume_id and boot_volume_size_in_gbs parameters. image_id is an existing parameter so the underlying
    # CLI operation will accept it
    kwargs.pop('boot_volume_size_in_gbs', None)
    kwargs.pop('source_boot_volume_id', None)

    json_skeleton_utils.remove_json_skeleton_params_from_dict(kwargs)

    ctx.invoke(compute_cli.launch_instance, **kwargs)
Beispiel #6
0
def create_stack_extended(ctx, from_json, wait_for_state, max_wait_seconds,
                          wait_interval_seconds, compartment_id, config_source,
                          display_name, description, variables,
                          terraform_version, freeform_tags, defined_tags,
                          working_directory, **kwargs):

    config_source = os.path.expandvars(os.path.expanduser(config_source))
    if not os.path.exists(config_source):
        click.echo('Config source does not exist', file=sys.stderr)
        ctx.abort()

    if not (config_source.endswith(".zip") and os.path.isfile(config_source)
            and zipfile.is_zipfile(config_source)):
        click.echo('Config source must be a .zip file.', file=sys.stderr)
        ctx.abort()

    send_value = create_base64encoded_zip(config_source)
    if not send_value:
        click.echo('Internal error: Unable to generate encoded zip',
                   file=sys.stderr)
        ctx.abort()

    config_source = {
        'configSourceType':
        oci.resource_manager.models.ConfigSource.CONFIG_SOURCE_TYPE_ZIP_UPLOAD,
        'zipFileBase64Encoded': send_value
    }

    if working_directory is not None:
        config_source['workingDirectory'] = working_directory

    json_skeleton_utils.remove_json_skeleton_params_from_dict(kwargs)

    kwargs = {}
    kwargs['opc_request_id'] = cli_util.use_or_generate_request_id(
        ctx.obj['request_id'])

    _details = {}
    _details['compartmentId'] = compartment_id
    _details['configSource'] = cli_util.parse_json_parameter(
        "config_source", config_source)

    if display_name is not None:
        _details['displayName'] = display_name

    if description is not None:
        _details['description'] = description

    if variables is not None:
        _details['variables'] = cli_util.parse_json_parameter(
            "variables", variables)

    if terraform_version is not None:
        _details['terraformVersion'] = terraform_version

    if freeform_tags is not None:
        _details['freeformTags'] = cli_util.parse_json_parameter(
            "freeform_tags", freeform_tags)

    if defined_tags is not None:
        _details['definedTags'] = cli_util.parse_json_parameter(
            "defined_tags", defined_tags)

    client = cli_util.build_client('resource_manager', 'resource_manager', ctx)
    result = client.create_stack(create_stack_details=_details, **kwargs)
    if wait_for_state:

        if hasattr(client, 'get_stack') and callable(
                getattr(client, 'get_stack')):
            try:
                wait_period_kwargs = {}
                if max_wait_seconds is not None:
                    wait_period_kwargs['max_wait_seconds'] = max_wait_seconds
                if wait_interval_seconds is not None:
                    wait_period_kwargs[
                        'max_interval_seconds'] = wait_interval_seconds

                click.echo(
                    'Action completed. Waiting until the work request has entered state: {}'
                    .format(wait_for_state),
                    file=sys.stderr)
                result = oci.wait_until(client,
                                        client.get_stack(result.data.id),
                                        'lifecycle_state', wait_for_state,
                                        **wait_period_kwargs)
            except oci.exceptions.MaximumWaitTimeExceeded as e:
                # If we fail, we should show an error, but we should still provide the information to the customer
                click.echo(
                    'Failed to wait until the work request entered the specified state. Outputting last known resource state',
                    file=sys.stderr)
                cli_util.render_response(result, ctx)
                sys.exit(2)
            except Exception:
                click.echo(
                    'Encountered error while waiting for work request to enter the specified state. Outputting last known resource state',
                    file=sys.stderr)
                cli_util.render_response(result, ctx)
                raise
        else:
            click.echo(
                'Unable to wait for the work request to enter the specified state',
                file=sys.stderr)
    cli_util.render_response(result, ctx)