Skip to content

Commit

Permalink
purge_all() and get_all_items() added to see all resources and delete…
Browse files Browse the repository at this point in the history
… them all
  • Loading branch information
lee212 committed Sep 29, 2016
1 parent faecf71 commit 5d1fe87
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
Simple Azure
===============================================================================

Simple Azure supports cloud computing management in the Python Programming Language. It plans to support WIndows Azure, OpenStack, etc.
[Documentation](https://simple-azure.readthedocs.org/)
Simple Azure supports using Azure Virtual Machines with simple python functions like other cloud providers e.g. AWS. [Documentation](https://simple-azure.readthedocs.org/)

Caveats
-------------------------------------------------------------------------------

- Classic (legacy) Python SDK is used from https://github.com/Azure/azure-sdk-for-python/blob/master/azure-servicemanagement-legacy
- Virtual Machines, Cloud Services and Storage are only used in Microsoft Azure services.

Prerequisite
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -33,7 +38,6 @@ Create a VM on Windows Azure
from simpleazure.simpleazure import SimpleAzure as saz
azure = saz()
azure.get_config()
azure.create_vm()
```

Expand Down Expand Up @@ -72,7 +76,6 @@ cluster() function helps to deploy several VMs at once.

```
azure = saz()
azure.get_config()
azure.create_cluster()
```

Expand All @@ -93,7 +96,6 @@ Deploy 5 VMs with Azure Data Science Core at West Europe

```
azure = saz()
azure.get_config()
q = azure.get_registered_image(name="Azure-Data-Science-Core")
azure.set_image(image=q,refresh=True)
azure.set_location("West Europe")
Expand Down
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Three lines are required to deploy Window Azure Virtual Machine in Python.
from simpleazure.simpleazure import SimpleAzure as saz
azure = saz()
azure.get_config()
azure.create_vm()
.. raw:: html
Expand Down
3 changes: 0 additions & 3 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Deploying Azure Virtual Machine
from simpleazure.simpleazure import SimpleAzure as saz
azure = saz()
azure.get_config()
azure.create_vm()
You can change an operating system image by the ``set_image()`` function. For example, *Ubuntu 12.04 distribution* can be selected like as follows:
Expand All @@ -29,7 +28,6 @@ Deploying several machines
.. code-block:: python
azure = saz()
azure.get_config()
azure.create_cluster(num=5)
my-cluster-vm-0-87412
Expand All @@ -49,7 +47,6 @@ This example explains as how to deploy a virtual machine with the community imag
.. code-block:: python
azure = saz()
azure.get_config()
q = azure.get_registered_image(name="Azure-Data-Science-Core")
azure.set_image(image=q)
azure.create_vm()
Expand Down
75 changes: 73 additions & 2 deletions simpleazure/simpleazure.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_location(self):
return self.location

def set_role_size(self, size=config.DEFAULT_ROLE_SIZE):
"""Set a role size for the virtual machine among ExtraSmall, Small,
"""Set a role (flavor) size for the virtual machine among ExtraSmall, Small,
Medium, Large, ExtraLarge
:param size: the name of a role size to create
Expand Down Expand Up @@ -220,6 +220,8 @@ def connect_service(self, refresh=False):
:type refresh: bool.
"""
if not hasattr(self, 'cert'):
self.get_creds()
if not self.sms or refresh:
self.sms = ServiceManagementService(self.subscription_id, self.certificate_path)

Expand Down Expand Up @@ -404,7 +406,7 @@ def list_deployments(self):
def delete_vm(self, name=None):
"""Delete vm instance"""

res = self.sms.delete_deployment(name or self.get_name(), name or self.get_name())
res = self.sms.delete_deployment(name or self.get_name(), name or self.get_name(), delete_vhd=True)
return res

def set_image(self, name=None, image=None, refresh=False):
Expand Down Expand Up @@ -581,3 +583,72 @@ def get_username(self):

def get_pkey(self):
return self.private_key_path

def purge_all(self):
"""Delete all resources"""

res=None
self.connect_service()
# Delete hosted services
hosted_services = self.sms.list_hosted_services()
for i in hosted_services:
svc_props = self.sms.get_hosted_service_properties(i.service_name, True)
for j in svc_props.deployments:
self.delete_vm(j.name) #i.service_name)
# log("{0} (vm) deletion requested".format(i.service_name))
try:
res = self.sms.delete_hosted_service(i.service_name)
except:
print ("Failed to delete {0}".format(i.service_name))
pass
# log("{0} (cloud service) deletion requested".format(i.service_name))
# Delete storage account
storage = self.sms.list_storage_accounts()
for i in storage:
try:
res = self.sms.delete_storage_account(i.service_name)
# log("{0} (storage) deletion requested".format(i.service_name))
except:
print ("Failed to delete {0}".format(i.service_name))
pass

def get_all_items(self):
self.connect_service()
all_items = {}

# The following items are displayed:
# - cloud services
# - storage
# - virtual machines


supported = ['hosted_services', 'deployments', 'storage']
item = { 'count': 0, 'names': [] }

# initialize
for i in supported:
all_items[i] = item

# hosted services, deployments
hosted_services = self.sms.list_hosted_services()
all_items['hosted_services']['count'] = len(hosted_services)
for i in hosted_services:
svc_props = self.sms.get_hosted_service_properties(i.service_name, True)
all_items['deployments']['count'] += len(svc_props.deployments)
for j in svc_props.deployments:
all_items['deployments']['names'].append(j.name)
all_items['hosted_services']['names'].append(i.service_name)

storage = self.sms.list_storage_accounts()
all_items['storage']['count'] = len(storage)
for i in storage:
all_items['storage']['names'].append(i.service_name)
return all_items

# Tips
#
# 'hosted_service'
# returns like:
#
# {'url': u'https://management.core.windows.net/6b3cf2b5-2cc1-4828-b5e0-9f8be72e6e6f/services/hostedservices/sazvm-16203', 'service_name': u'sazvm-16203', 'hosted_service_properties': <azure.servicemanagement.models.HostedServiceProperties object at 0x7f9557815e90>, 'deployments': None}

0 comments on commit 5d1fe87

Please sign in to comment.