示例#1
0
文件: create.py 项目: zhengr/datalab
def create_repo(args, gcloud_repos, repo_name):
    """Create the given repository.

    Args:
      args: The Namespace returned by argparse
      gcloud_repos: Function that can be used for invoking
        `gcloud alpha source repos`
      repo_name: The name of the repository to create
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    create_cmd = ['create', repo_name]
    utils.call_gcloud_quietly(args, gcloud_repos, create_cmd)
示例#2
0
def create_network(args, gcloud_compute):
    """Create the `datalab-network` network.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the network {0}'.format(_DATALAB_NETWORK))
    create_cmd = [
        'networks', 'create', _DATALAB_NETWORK,
        '--description', _DATALAB_NETWORK_DESCRIPTION]
    utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
    return
示例#3
0
def create_repo(args, gcloud_repos, repo_name):
    """Create the given repository.

    Args:
      args: The Namespace returned by argparse
      gcloud_repos: Function that can be used for invoking
        `gcloud source repos`
      repo_name: The name of the repository to create
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the repository {0}'.format(repo_name))

    create_cmd = ['create', repo_name]
    utils.call_gcloud_quietly(args, gcloud_repos, create_cmd)
示例#4
0
def create_repo(args, gcloud_repos, repo_name):
    """Create the given repository.

    Args:
      args: The Namespace returned by argparse
      gcloud_repos: Function that can be used for invoking
        `gcloud source repos`
      repo_name: The name of the repository to create
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the repository {0}'.format(repo_name))

    create_cmd = ['create', repo_name]
    utils.call_gcloud_quietly(args, gcloud_repos, create_cmd)
示例#5
0
def create_firewall_rule(args, gcloud_compute):
    """Create the `datalab-network-allow-ssh` firewall rule.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    print('Creating the firewall rule {0}'.format(_DATALAB_FIREWALL_RULE))
    create_cmd = [
        'firewall-rules', 'create', _DATALAB_FIREWALL_RULE, '--allow',
        'tcp:22', '--network', _DATALAB_NETWORK, '--description',
        _DATALAB_FIREWALL_RULE_DESCRIPTION
    ]
    utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
    return
示例#6
0
def ensure_network_exists(args, gcloud_compute):
    """Create the `datalab-network` network if it does not already exist.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    get_cmd = ['networks', 'describe', '--format', 'value(name)',
               _DATALAB_NETWORK]
    try:
        utils.call_gcloud_quietly(
            args, gcloud_compute, get_cmd, report_errors=False)
    except subprocess.CalledProcessError:
        create_network(args, gcloud_compute)
    return
示例#7
0
def create_network(args, gcloud_compute, network_name):
    """Create the specified network.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      network_name: The name of the network
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the network {0}'.format(network_name))
    create_cmd = [
        'networks', 'create', network_name,
        '--description', _DATALAB_NETWORK_DESCRIPTION]
    utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
    return
示例#8
0
def ensure_network_exists(args, gcloud_compute, network_name):
    """Create the specified network if it does not already exist.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      network_name: The name of the network
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    get_cmd = ['networks', 'describe', '--format', 'value(name)', network_name]
    try:
        utils.call_gcloud_quietly(
            args, gcloud_compute, get_cmd, report_errors=False)
    except subprocess.CalledProcessError:
        create_network(args, gcloud_compute, network_name)
    return
示例#9
0
def ensure_firewall_rule_exists(args, gcloud_compute):
    """Create the `datalab-network-allow-ssh` firewall rule if it necessary.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    get_cmd = [
        'firewall-rules', 'describe', _DATALAB_FIREWALL_RULE,
        '--format', 'value(name)']
    try:
        utils.call_gcloud_quietly(
            args, gcloud_compute, get_cmd, report_errors=False)
    except subprocess.CalledProcessError:
        create_firewall_rule(args, gcloud_compute)
    return
示例#10
0
def ensure_firewall_rule_exists(args, gcloud_compute, network_name):
    """Create a firewall rule to allow SSH access if necessary.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      network_name: The name of the network on which to allow SSH access
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    rule_name = _DATALAB_FIREWALL_RULE_TEMPLATE.format(network_name)
    get_cmd = [
        'firewall-rules', 'describe', rule_name, '--format', 'value(name)']
    try:
        utils.call_gcloud_quietly(
            args, gcloud_compute, get_cmd, report_errors=False)
    except subprocess.CalledProcessError:
        create_firewall_rule(args, gcloud_compute, network_name, rule_name)
    return
示例#11
0
def create_firewall_rule(args, gcloud_compute, network_name, rule_name):
    """Create the specified firewall rule to allow SSH access.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      network_name: The name of the network on which to allow SSH access
      rule_name: The name of the firewall rule
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the firewall rule {0}'.format(rule_name))
    create_cmd = [
        'firewall-rules', 'create', rule_name, '--allow', 'tcp:22',
        '--network', network_name, '--description',
        _DATALAB_FIREWALL_RULE_DESCRIPTION
    ]
    utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
    return
示例#12
0
def ensure_disk_exists(args, gcloud_compute, disk_name):
    """Create the given persistent disk if it does not already exist.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      disk_name: The name of the persistent disk
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    get_cmd = [
        'disks', 'describe', disk_name, '--format', 'value(name)']
    if args.zone:
        get_cmd.extend(['--zone', args.zone])
    try:
        utils.call_gcloud_quietly(
            args, gcloud_compute, get_cmd, report_errors=False)
    except subprocess.CalledProcessError:
        create_disk(args, gcloud_compute, disk_name)
    return
示例#13
0
def create_firewall_rule(args, gcloud_compute, network_name, rule_name):
    """Create the specified firewall rule to allow SSH access.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      network_name: The name of the network on which to allow SSH access
      rule_name: The name of the firewall rule
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the firewall rule {0}'.format(rule_name))
    create_cmd = [
        'firewall-rules', 'create', rule_name,
        '--allow', 'tcp:22',
        '--network', network_name,
        '--description', _DATALAB_FIREWALL_RULE_DESCRIPTION]
    utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
    return
示例#14
0
def ensure_disk_exists(args, gcloud_compute, disk_name):
    """Create the given persistent disk if it does not already exist.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      disk_name: The name of the persistent disk
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    get_cmd = ['disks', 'describe', disk_name, '--format', 'value(name)']
    if args.zone:
        get_cmd.extend(['--zone', args.zone])
    try:
        utils.call_gcloud_quietly(args,
                                  gcloud_compute,
                                  get_cmd,
                                  report_errors=False)
    except subprocess.CalledProcessError:
        create_disk(args, gcloud_compute, disk_name)
    return
示例#15
0
def create_disk(args, gcloud_compute, disk_name):
    """Create the user's persistent disk.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      disk_name: The name of the persistent disk to create
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the disk {0}'.format(disk_name))
    create_cmd = ['disks', 'create']
    if args.zone:
        create_cmd.extend(['--zone', args.zone])
    create_cmd.extend([
        '--size', str(args.disk_size_gb) + 'GB',
        '--description', _DATALAB_DISK_DESCRIPTION,
        disk_name])
    utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
    return
示例#16
0
def ensure_disk_exists(args, gcloud_compute, disk_name, report_errors=False):
    """Create the given persistent disk if it does not already exist.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      disk_name: The name of the persistent disk
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    get_cmd = ['disks', 'describe', disk_name, '--format', 'value(name)']
    if args.zone:
        get_cmd.extend(['--zone', args.zone])
    try:
        utils.call_gcloud_quietly(args,
                                  gcloud_compute,
                                  get_cmd,
                                  report_errors=False)
    except subprocess.CalledProcessError:
        try:
            create_disk(args, gcloud_compute, disk_name, report_errors)
        except:
            if (not args.zone) and (not args.quiet):
                # We take this failure as a sign that gcloud might need
                # to prompt for a zone. As such, we do that prompting
                # for it, and then try again.
                args.zone = utils.prompt_for_zone(args, gcloud_compute)
                ensure_disk_exists(args,
                                   gcloud_compute,
                                   disk_name,
                                   report_errors=True)
            elif not report_errors:
                # We know the command failed (and will almost certainly
                # fail again), but we did not forward the errors that
                # it reported to the user. To work around this, we
                # re-run the command with 'report_errors' set to True
                create_disk(args, gcloud_compute, disk_name, True)
            else:
                raise
    return
示例#17
0
def create_disk(args, gcloud_compute, disk_name):
    """Create the user's persistent disk.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      disk_name: The name of the persistent disk to create
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    if utils.print_info_messages(args):
        print('Creating the disk {0}'.format(disk_name))
    create_cmd = ['disks', 'create']
    if args.zone:
        create_cmd.extend(['--zone', args.zone])
    create_cmd.extend([
        '--size',
        str(args.disk_size_gb) + 'GB', '--description',
        _DATALAB_DISK_DESCRIPTION, disk_name
    ])
    utils.call_gcloud_quietly(args, gcloud_compute, create_cmd)
    return
示例#18
0
def ensure_firewall_rule_exists(args, gcloud_compute, network_name):
    """Create a firewall rule to allow SSH access if necessary.

    Args:
      args: The Namespace returned by argparse
      gcloud_compute: Function that can be used for invoking `gcloud compute`
      network_name: The name of the network on which to allow SSH access
    Raises:
      subprocess.CalledProcessError: If the `gcloud` command fails
    """
    rule_name = _DATALAB_FIREWALL_RULE_TEMPLATE.format(network_name)
    get_cmd = [
        'firewall-rules', 'describe', rule_name, '--format', 'value(name)'
    ]
    try:
        utils.call_gcloud_quietly(args,
                                  gcloud_compute,
                                  get_cmd,
                                  report_errors=False)
    except subprocess.CalledProcessError:
        create_firewall_rule(args, gcloud_compute, network_name, rule_name)
    return