Michał Smereczyński | Cloudeon AS
Cloud Solution Architect
Aplikacje Python na platformie Microsoft
Azure
i platforma Microsoft Azure w aplikacjach
Python
@smereczynski
https://lnx.azurewebites.net
Michał Smereczyński
Linux Guy
DevOps
(czasami) Dev
Architekt
Konsultant
Aplikacje Python na platformie Microsoft Azure
Opcje:
IaaS
VM
Linux
Windows
PaaS
WebApp
Windows
Linux
Service Fabric
Guest exec
Container
Container Services
DC/OS
Swarm
Kubernetes
PaaS
WebApp
Windows
Linux
PaaS
WebApp
Windows
WSGI
Projekt
app.py
requirements.txt
runtime.txt
web.config
ptvs_virtualenv_proxy.py
WSGI Handler
Package Management
Virtual Env + Python version
App + Server config
Virtual Environment Proxy
App.py
def wsgi_app(environ, start_response):
status = '200 OK’
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
response_body = 'Hello World’
yield response_body.encode()
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('localhost', 5555, wsgi_app)
httpd.serve_forever()
requirements.txt
flask<1
requests==2.12.3
runtime.txt
python-3.4
web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="WSGI_ALT_VIRTUALENV_HANDLER" value="app.wsgi_app" />
<add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS" value="D:homesitewwwrootenvScriptspython.exe" />
<add key="WSGI_HANDLER" value="ptvs_virtualenv_proxy.get_venv_handler()" />
<add key="PYTHONPATH" value="D:homesitewwwroot" />
</appSettings>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="Python27_via_FastCGI" />
<remove name="Python34_via_FastCGI" />
<add name="Python FastCGI" path="handler.fcgi" verb="*" modules="FastCgiModule"
scriptProcessor="D:Python34python.exe|D:Python34Scriptswfastcgi.py" resourceType="Unspecified"
requireAccess="Script" />
</handlers>
</system.webServer>
</configuration>
ptvs_virtualenv_proxy.py
„The following script is used to retrieve the WSGI handler,
activate the virtual environment and log errors. It is
designed to be generic and used without modifications.”
Wdrożenie
push
1. KuduSync 2. Select Python version 3. Create virtual environment
4. Install packages5. Copy web.config6. Django collectstatic
aplikacja.azurewebsites.net
DEMO
Platforma Microsoft Azure w aplikacjach Python
Azure Resource Manager
CONSISTENT
MANAGEMENT
LAYER
AZURE RESOURCE MANAGER API
Azure SDK for Python
https://github.com/Azure/azure-sdk-for-python
pip install azure
from azure.mgmt.common import SubscriptionCloudCredentials
import azure.mgmt.compute
import azure.mgmt.network
import azure.mgmt.resource
import azure.mgmt.storage
import requests
def get_token_from_client_credentials(endpoint, client_id, client_secret):
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'resource': 'https://management.core.windows.net/'
}
response = requests.post(endpoint, data=payload).json()
return response['access_token']
auth_token = get_token_from_client_credentials(
endpoint='https://login.microsoftonline.com/160012ea-8f27-427f-9d00-030d66b90d0f/oauth2/token',
client_id='47bb44d3-c6da-4974-9df8-88598a10eb55',
client_secret='ijoijoufo2'
)
subscription_id = '420288c3-b6f0-45e4-a3f6-e70fe2c18f50'
creds = SubscriptionCloudCredentials(subscription_id, auth_token)
result = compute_client.virtual_machines.create_or_update(
GROUP_NAME,
azure.mgmt.compute.VirtualMachine(
location=REGION,
name=VM_NAME,
os_profile=azure.mgmt.compute.OSProfile(
admin_username=ADMIN_USERNAME,
admin_password=ADMIN_PASSWORD,
computer_name=COMPUTER_NAME,
),
hardware_profile=azure.mgmt.compute.HardwareProfile(
virtual_machine_size=azure.mgmt.compute.VirtualMachineSizeTypes.standard_a0
),
network_profile=azure.mgmt.compute.NetworkProfile(
network_interfaces=[
azure.mgmt.compute.NetworkInterfaceReference(
reference_uri=nic_id,
),
],
),
storage_profile=azure.mgmt.compute.StorageProfile(
os_disk=azure.mgmt.compute.OSDisk(
caching=azure.mgmt.compute.CachingTypes.none,
create_option=azure.mgmt.compute.DiskCreateOptionTypes.from_image,
name=OS_DISK_NAME,
virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.format(
STORAGE_NAME,
OS_DISK_NAME,
),
),
),
image_reference = azure.mgmt.compute.ImageReference(
publisher=IMAGE_PUBLISHER,
offer=IMAGE_OFFER,
sku=IMAGE_SKU,
version=IMAGE_VERSION,
),
),
),
)
TWORZENIEVM
result = network_client.public_ip_addresses.get(GROUP_NAME, PUBLIC_IP_NAME)
print('VM available at {}'.format(result.public_ip_address.ip_address))
Zasoby jako obiekty
DEMO

[PL] Code Europe 2016 - Python and Microsoft Azure