|
| 1 | +Instances and Volumes |
| 2 | +===================== |
| 3 | + |
| 4 | +.. code-block:: python |
| 5 | +
|
| 6 | + import os |
| 7 | + from datacrunch import DataCrunchClient |
| 8 | +
|
| 9 | + # Get client secret from environment variable |
| 10 | + CLIENT_SECRET = os.environ['DATACRUNCH_CLIENT_SECRET'] |
| 11 | + CLIENT_ID = 'Ibk5bdxV64lKAWOqYnvSi' # Replace with your client ID |
| 12 | +
|
| 13 | + # Create datcrunch client |
| 14 | + datacrunch = DataCrunchClient(CLIENT_ID, CLIENT_SECRET) |
| 15 | +
|
| 16 | + # Get some volume type constants |
| 17 | + NVMe = datacrunch.constants.volume_types.NVMe |
| 18 | + HDD = datacrunch.constants.volume_types.HDD |
| 19 | +
|
| 20 | + EXISTING_OS_VOLUME_ID = '81e45bf0-5da2-412b-97d7-c20a7564fca0' |
| 21 | + EXAMPLE_VOLUME_ID = '225dde24-ae44-4787-9224-2b9f56f44394' |
| 22 | + EXAMPLE_INSTANCE_ID = '1eeabba4-caf7-4b4a-9143-0107034cc7f5' |
| 23 | +
|
| 24 | + # Get all SSH keys |
| 25 | + ssh_keys = datacrunch.ssh_keys.get() |
| 26 | +
|
| 27 | + # Create instance with extra attached volumes |
| 28 | + instance_with_extra_volumes = datacrunch.instances.create(instance_type='1V100.6V', |
| 29 | + image='fastai', |
| 30 | + ssh_key_ids=ssh_keys[0].id, |
| 31 | + hostname='example', |
| 32 | + description='example instance', |
| 33 | + volumes=[ |
| 34 | + {"type": HDD, "name": "volume-1", "size": 95}, |
| 35 | + {"type": NVMe, "name": "volume-2", "size": 95} |
| 36 | + ]) |
| 37 | +
|
| 38 | + # Create instance with existing OS volume as an image |
| 39 | + instance_with_existing_os_volume = datacrunch.instances.create(instance_type='1V100.6V', |
| 40 | + image=EXISTING_OS_VOLUME_ID, |
| 41 | + ssh_key_ids=ssh_keys[0].id, |
| 42 | + hostname='example', |
| 43 | + description='example instance') |
| 44 | +
|
| 45 | + # Delete instance AND OS volume (the rest of the volumes would be detached) |
| 46 | + datacrunch.instances.action(instance_id=EXAMPLE_INSTANCE_ID, |
| 47 | + action=datacrunch.constants.instance_actions.DELETE) |
| 48 | +
|
| 49 | + # Delete instance WITHOUT deleting the OS volume (will detach all volumes of the instance) |
| 50 | + datacrunch.instances.action(instance_id=EXAMPLE_INSTANCE_ID, |
| 51 | + action=datacrunch.constants.instance_actions.DELETE, |
| 52 | + volume_ids=[]) |
| 53 | +
|
| 54 | +
|
| 55 | + # Delete instance and one of it's volumes (will delete one volume, detach the rest) |
| 56 | + datacrunch.instances.action(instance_id=EXAMPLE_INSTANCE_ID, |
| 57 | + action=datacrunch.constants.instance_actions.DELETE, |
| 58 | + volume_ids=[EXAMPLE_VOLUME_ID]) |
0 commit comments