|
44 | 44 |
|
45 | 45 | _DATALAB_NOTEBOOKS_REPOSITORY = 'datalab-notebooks'
|
46 | 46 |
|
47 |
| -_DATALAB_STARTUP_SCRIPT = """#!/bin/bash |
| 47 | +_DATALAB_BASE_STARTUP_SCRIPT = """#!/bin/bash |
48 | 48 |
|
49 | 49 | PERSISTENT_DISK_DEV="/dev/disk/by-id/google-datalab-pd"
|
50 | 50 | MOUNT_DIR="/mnt/disks/datalab-pd"
|
|
148 | 148 | find "${{tmpdir}}/" -mindepth 1 -delete
|
149 | 149 | }}
|
150 | 150 |
|
| 151 | +""" |
| 152 | + |
| 153 | +_DATALAB_STARTUP_SCRIPT = _DATALAB_BASE_STARTUP_SCRIPT + """ |
151 | 154 | mount_and_prepare_disk
|
152 | 155 | configure_swap
|
153 | 156 | cleanup_tmp
|
@@ -524,38 +527,56 @@ def ensure_repo_exists(args, gcloud_repos, repo_name):
|
524 | 527 | raise RepositoryException(repo_name)
|
525 | 528 |
|
526 | 529 |
|
527 |
| -def run(args, gcloud_compute, gcloud_repos, |
528 |
| - email='', in_cloud_shell=False, **kwargs): |
529 |
| - """Implementation of the `datalab create` subcommand. |
| 530 | +def prepare(args, gcloud_compute, gcloud_repos): |
| 531 | + """Run preparation steps for VM creation. |
530 | 532 |
|
531 | 533 | Args:
|
532 | 534 | args: The Namespace instance returned by argparse
|
533 | 535 | gcloud_compute: Function that can be used to invoke `gcloud compute`
|
534 | 536 | gcloud_repos: Function that can be used to invoke
|
535 | 537 | `gcloud source repos`
|
536 |
| - email: The user's email address |
537 |
| - in_cloud_shell: Whether or not the command is being run in the |
538 |
| - Google Cloud Shell |
| 538 | + Returns: |
| 539 | + The disk config |
539 | 540 | Raises:
|
540 | 541 | subprocess.CalledProcessError: If a nested `gcloud` calls fails
|
541 | 542 | """
|
542 | 543 | ensure_network_exists(args, gcloud_compute)
|
543 | 544 | ensure_firewall_rule_exists(args, gcloud_compute)
|
544 | 545 |
|
545 |
| - instance = args.instance |
546 |
| - disk_name = args.disk_name or '{0}-pd'.format(instance) |
| 546 | + disk_name = args.disk_name or '{0}-pd'.format(args.instance) |
547 | 547 | ensure_disk_exists(args, gcloud_compute, disk_name)
|
| 548 | + disk_cfg = ( |
| 549 | + 'auto-delete=no,boot=no,device-name=datalab-pd,mode=rw,name=' + |
| 550 | + disk_name) |
548 | 551 |
|
549 | 552 | if not args.no_create_repository:
|
550 | 553 | ensure_repo_exists(args, gcloud_repos, _DATALAB_NOTEBOOKS_REPOSITORY)
|
551 | 554 |
|
552 |
| - print('Creating the instance {0}'.format(instance)) |
| 555 | + return disk_cfg |
| 556 | + |
| 557 | + |
| 558 | +def run(args, gcloud_compute, gcloud_repos, |
| 559 | + email='', in_cloud_shell=False, **kwargs): |
| 560 | + """Implementation of the `datalab create` subcommand. |
| 561 | +
|
| 562 | + Args: |
| 563 | + args: The Namespace instance returned by argparse |
| 564 | + gcloud_compute: Function that can be used to invoke `gcloud compute` |
| 565 | + gcloud_repos: Function that can be used to invoke |
| 566 | + `gcloud source repos` |
| 567 | + email: The user's email address |
| 568 | + in_cloud_shell: Whether or not the command is being run in the |
| 569 | + Google Cloud Shell |
| 570 | + Raises: |
| 571 | + subprocess.CalledProcessError: If a nested `gcloud` calls fails |
| 572 | + """ |
| 573 | + disk_cfg = prepare(args, gcloud_compute, gcloud_repos) |
| 574 | + |
| 575 | + print('Creating the instance {0}'.format(args.instance)) |
553 | 576 | cmd = ['instances', 'create']
|
554 | 577 | if args.zone:
|
555 | 578 | cmd.extend(['--zone', args.zone])
|
556 |
| - disk_cfg = ( |
557 |
| - 'auto-delete=no,boot=no,device-name=datalab-pd,mode=rw,name=' + |
558 |
| - disk_name) |
| 579 | + |
559 | 580 | enable_backups = "false" if args.no_backups else "true"
|
560 | 581 | console_log_level = args.log_level or "warn"
|
561 | 582 | user_email = args.for_user or email
|
@@ -602,7 +623,7 @@ def run(args, gcloud_compute, gcloud_repos,
|
602 | 623 | '--disk', disk_cfg,
|
603 | 624 | '--service-account', service_account,
|
604 | 625 | '--scopes', 'cloud-platform',
|
605 |
| - instance]) |
| 626 | + args.instance]) |
606 | 627 | gcloud_compute(args, cmd)
|
607 | 628 | finally:
|
608 | 629 | os.remove(startup_script_file.name)
|
|
0 commit comments