def create_tunnel(): """Create an SSH tunnel to the Datalab instance. This method blocks for as long as the connection is open. Raises: KeyboardInterrupt: When the end user kills the connection subprocess.CalledProcessError: If the connection dies on its own """ if utils.print_info_messages(args): print('Connecting to {0} via SSH').format(instance) cmd = ['ssh'] if args.zone: cmd.extend(['--zone', args.zone]) port_mapping = 'localhost:' + str(args.port) + ':localhost:8080' if os.name == 'posix': # The '-o' flag is not supported by all SSH clients (notably, # PuTTY does not support it). To avoid any potential issues # with it, we only add that flag when we believe it will # be supported. In particular, checking for an os name of # 'posix' works for both Linux and Mac OSX, which do support # that flag. cmd.extend( ['--ssh-flag=-o', '--ssh-flag=LogLevel=' + args.ssh_log_level]) cmd.extend([ '--ssh-flag=-4', '--ssh-flag=-N', '--ssh-flag=-L', '--ssh-flag=' + port_mapping ]) cmd.append('datalab@{0}'.format(instance)) gcloud_compute(args, cmd) return
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)
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
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
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 """ if utils.print_info_messages(args): 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
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
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
def maybe_start(args, gcloud_compute, instance, status): """Start the given Google Compute Engine VM if it is not running. Args: args: The Namespace instance returned by argparse gcloud_compute: Function that can be used to invoke `gcloud compute` instance: The name of the instance to check and (possibly) start status: The string describing the status of the instance Raises: subprocess.CalledProcessError: If one of the `gcloud` calls fail """ if status != 'RUNNING': if utils.print_info_messages(args): print('Restarting the instance {0} with status {1}'.format( instance, status)) start_cmd = ['instances', 'start'] if args.zone: start_cmd.extend(['--zone', args.zone]) start_cmd.extend([instance]) gcloud_compute(args, start_cmd) return
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
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
def create_tunnel(): """Create an SSH tunnel to the Datalab instance. This method blocks for as long as the connection is open. Raises: KeyboardInterrupt: When the end user kills the connection subprocess.CalledProcessError: If the connection dies on its own """ if utils.print_info_messages(args): print('Connecting to {0} via SSH').format(instance) cmd = ['ssh'] if args.zone: cmd.extend(['--zone', args.zone]) port_mapping = 'localhost:' + str(args.port) + ':localhost:8080' cmd.extend([ '--ssh-flag=-4', '--ssh-flag=-o', '--ssh-flag=LogLevel=' + args.ssh_log_level, '--ssh-flag=-N', '--ssh-flag=-L', '--ssh-flag=' + port_mapping ]) cmd.append('datalab@{0}'.format(instance)) gcloud_compute(args, cmd) return