File size: 4,508 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Service Management
==================

Usage
-----

Set-up certificates
~~~~~~~~~~~~~~~~~~~

You will need two certificates, one for the server (a .cer file) and one for
the client (a .pem file).

Using the Azure .PublishSettings certificate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can download your Azure publish settings file and use the certificate that
is embedded in that file to create the client certificate. The server
certificate already exists, so you won't need to upload one.

To do this, download your `publish settings <http://go.microsoft.com/fwlink/?LinkID=301775>`__
then use this code to create the .pem file.

.. code:: python

    from azure.servicemanagement import get_certificate_from_publish_settings

    subscription_id = get_certificate_from_publish_settings(
        publish_settings_path='MyAccount.PublishSettings',
        path_to_write_certificate='mycert.pem',
        subscription_id='00000000-0000-0000-0000-000000000000',
    )

The subscription id parameter is optional. If there are more than one
subscription in the publish settings, the first one will be used.

Creating and uploading new certificate with OpenSSL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To create the .pem file using `OpenSSL <http://www.openssl.org>`__, execute this:

.. code:: shell

    openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

To create the .cer certificate, execute this:

.. code:: shell

    openssl x509 -inform pem -in mycert.pem -outform der -out mycert.cer

After you have created the certificate, you will need to upload the .cer
file to Microsoft Azure via the "Upload" action of the "Settings" tab of
the `management portal <http://manage.windowsazure.com>`__.

Initialization
~~~~~~~~~~~~~~

To initialize the management service, pass in your subscription id and
the path to the .pem file.

.. code:: python

    from azure.servicemanagement import ServiceManagementService
    subscription_id = '00000000-0000-0000-0000-000000000000'
    cert_file = 'mycert.pem'
    sms = ServiceManagementService(subscription_id, cert_file)

List Available Locations
~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

    locations = sms.list_locations()
    for location in locations:
        print(location.name)

Create a Storage Service
~~~~~~~~~~~~~~~~~~~~~~~~

To create a storage service, you need a name for the service (between 3
and 24 lowercase characters and unique within Microsoft Azure), a label
(up to 100 characters, automatically encoded to base-64), and either a
location or an affinity group.

.. code:: python

    name = "mystorageservice"
    desc = name
    label = name
    location = 'West US'

    result = sms.create_storage_account(name, desc, label, location=location)

Create a Cloud Service
~~~~~~~~~~~~~~~~~~~~~~

A cloud service is also known as a hosted service (from earlier versions
of Microsoft Azure). The **create\_hosted\_service** method allows you
to create a new hosted service by providing a hosted service name (which
must be unique in Microsoft Azure), a label (automatically encoded to
base-64), and the location *or* the affinity group for your service.

.. code:: python

    name = "myhostedservice"
    desc = name
    label = name
    location = 'West US'

    result = sms.create_hosted_service(name, label, desc, location=location)

Create a Deployment
~~~~~~~~~~~~~~~~~~~

To make a new deployment to Azure you must store the package file in a
Microsoft Azure Blob Storage account under the same subscription as the
hosted service to which the package is being uploaded. You can create a
deployment package with the `Microsoft Azure PowerShell
cmdlets <https://docs.microsoft.com/en-us/powershell/azure/?view=azps-3.2.0>`__,
or with the `cspack commandline
tool <https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-model-and-package#servicepackagecspkg>`__.

.. code:: python

    service_name = "myhostedservice"
    deployment_name = "v1"
    slot = 'Production'
    package_url = "URL_for_.cspkg_file"
    configuration = base64.b64encode(open(file_path, 'rb').read('path_to_.cscfg_file'))
    label = service_name

    result = sms.create_deployment(service_name,
                         slot,
                         deployment_name,
                         package_url,
                         label,
                         configuration)

    operation = sms.get_operation_status(result.request_id)
    print('Operation status: ' + operation.status)