Merge "firstapp - move libcloud sec5 to sample file"
This commit is contained in:
commit
248797c521
49
firstapp/samples/libcloud/block_storage.py
Normal file
49
firstapp/samples/libcloud/block_storage.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# step-1
|
||||||
|
from libcloud.compute.types import Provider
|
||||||
|
from libcloud.compute.providers import get_driver
|
||||||
|
|
||||||
|
auth_username = 'your_auth_username'
|
||||||
|
auth_password = 'your_auth_password'
|
||||||
|
auth_url = 'http://controller:5000'
|
||||||
|
project_name = 'your_project_name_or_id'
|
||||||
|
region_name = 'your_region_name'
|
||||||
|
|
||||||
|
provider = get_driver(Provider.OPENSTACK)
|
||||||
|
connection = provider(auth_username,
|
||||||
|
auth_password,
|
||||||
|
ex_force_auth_url=auth_url,
|
||||||
|
ex_force_auth_version='2.0_password',
|
||||||
|
ex_tenant_name=project_name,
|
||||||
|
ex_force_service_region=region_name)
|
||||||
|
|
||||||
|
# step-2
|
||||||
|
volume = connection.create_volume(1, 'test')
|
||||||
|
print(volume)
|
||||||
|
|
||||||
|
# step-3
|
||||||
|
volumes = connection.list_volumes()
|
||||||
|
print(volumes)
|
||||||
|
|
||||||
|
|
||||||
|
# step-4
|
||||||
|
db_group = connection.ex_create_security_group('database', 'for database service')
|
||||||
|
connection.ex_create_security_group_rule(db_group, 'TCP', 3306, 3306)
|
||||||
|
instance = connection.create_node(name='app-database',
|
||||||
|
image=image,
|
||||||
|
size=flavor,
|
||||||
|
ex_keyname=keypair_name,
|
||||||
|
ex_security_groups=[db_group])
|
||||||
|
|
||||||
|
# step-5
|
||||||
|
volume = connection.ex_get_volume('755ab026-b5f2-4f53-b34a-6d082fb36689')
|
||||||
|
connection.attach_volume(instance, volume, '/dev/vdb')
|
||||||
|
|
||||||
|
# step-6
|
||||||
|
connection.detach_volume(volume)
|
||||||
|
connection.destroy_volume(volume)
|
||||||
|
|
||||||
|
# step-7
|
||||||
|
snapshot_name = 'test_backup_1'
|
||||||
|
connection.create_volume_snapshot('test', name=snapshot_name)
|
||||||
|
|
||||||
|
# step-8
|
@ -68,24 +68,10 @@ Connect to the API endpoint:
|
|||||||
|
|
||||||
.. only:: libcloud
|
.. only:: libcloud
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../samples/libcloud/block_storage.py
|
||||||
|
:language: python
|
||||||
from libcloud.compute.types import Provider
|
:start-after: step-1
|
||||||
from libcloud.compute.providers import get_driver
|
:end-before: step-2
|
||||||
|
|
||||||
auth_username = 'your_auth_username'
|
|
||||||
auth_password = 'your_auth_password'
|
|
||||||
auth_url = 'http://controller:5000'
|
|
||||||
project_name = 'your_project_name_or_id'
|
|
||||||
region_name = 'your_region_name'
|
|
||||||
|
|
||||||
provider = get_driver(Provider.OPENSTACK)
|
|
||||||
connection = provider(auth_username,
|
|
||||||
auth_password,
|
|
||||||
ex_force_auth_url=auth_url,
|
|
||||||
ex_force_auth_version='2.0_password',
|
|
||||||
ex_tenant_name=project_name,
|
|
||||||
ex_force_service_region=region_name)
|
|
||||||
|
|
||||||
.. only:: shade
|
.. only:: shade
|
||||||
|
|
||||||
@ -98,10 +84,10 @@ To try it out, make a 1GB volume called :test'.
|
|||||||
|
|
||||||
.. only:: libcloud
|
.. only:: libcloud
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../samples/libcloud/block_storage.py
|
||||||
|
:language: python
|
||||||
volume = connection.create_volume(1, 'test')
|
:start-after: step-2
|
||||||
print(volume)
|
:end-before: step-3
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
@ -120,10 +106,10 @@ To see if the volume creation was successful, list all volumes:
|
|||||||
|
|
||||||
.. only:: libcloud
|
.. only:: libcloud
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../samples/libcloud/block_storage.py
|
||||||
|
:language: python
|
||||||
volumes = connection.list_volumes()
|
:start-after: step-3
|
||||||
print(volumes)
|
:end-before: step-4
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
@ -150,15 +136,10 @@ MySQL, port 3306) from the network:
|
|||||||
|
|
||||||
.. only:: libcloud
|
.. only:: libcloud
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../samples/libcloud/block_storage.py
|
||||||
|
:language: python
|
||||||
db_group = connection.ex_create_security_group('database', 'for database service')
|
:start-after: step-4
|
||||||
connection.ex_create_security_group_rule(db_group, 'TCP', 3306, 3306)
|
:end-before: step-5
|
||||||
instance = connection.create_node(name='app-database',
|
|
||||||
image=image,
|
|
||||||
size=flavor,
|
|
||||||
ex_keyname=keypair_name,
|
|
||||||
ex_security_groups=[db_group])
|
|
||||||
|
|
||||||
.. only:: shade
|
.. only:: shade
|
||||||
|
|
||||||
@ -173,10 +154,10 @@ attach the volume to it at :code:`/dev/vdb`:
|
|||||||
|
|
||||||
.. only:: libcloud
|
.. only:: libcloud
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../samples/libcloud/block_storage.py
|
||||||
|
:language: python
|
||||||
volume = connection.ex_get_volume('755ab026-b5f2-4f53-b34a-6d082fb36689')
|
:start-after: step-5
|
||||||
connection.attach_volume(instance, volume, '/dev/vdb')
|
:end-before: step-6
|
||||||
|
|
||||||
.. only:: shade
|
.. only:: shade
|
||||||
|
|
||||||
@ -243,18 +224,14 @@ To detach and delete a volume:
|
|||||||
|
|
||||||
.. only:: libcloud
|
.. only:: libcloud
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../samples/libcloud/block_storage.py
|
||||||
|
:start-after: step-6
|
||||||
connection.detach_volume(volume)
|
:end-before: step-7
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
True
|
True
|
||||||
|
|
||||||
.. code-block:: python
|
|
||||||
|
|
||||||
connection.destroy_volume(volume)
|
|
||||||
|
|
||||||
.. note:: :code:`detach_volume` and :code:`destroy_volume` take a
|
.. note:: :code:`detach_volume` and :code:`destroy_volume` take a
|
||||||
volume object, not a name.
|
volume object, not a name.
|
||||||
|
|
||||||
@ -269,10 +246,10 @@ To detach and delete a volume:
|
|||||||
|
|
||||||
Other features, such as creating volume snapshots, are useful for backups:
|
Other features, such as creating volume snapshots, are useful for backups:
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../samples/libcloud/block_storage.py
|
||||||
|
:language: python
|
||||||
snapshot_name = 'test_backup_1'
|
:start-after: step-7
|
||||||
connection.create_volume_snapshot('test', name=snapshot_name)
|
:end-before: step-8
|
||||||
|
|
||||||
.. todo:: Do we need a note here to mention that 'test' is the
|
.. todo:: Do we need a note here to mention that 'test' is the
|
||||||
volume name and not the volume object?
|
volume name and not the volume object?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user