|
1 | | -""" |
2 | | -Copyright (C) Microsoft Corporation. All rights reserved. |
3 | | - |
4 | | -Microsoft Corporation (“Microsoft”) grants you a nonexclusive, perpetual, |
5 | | -royalty-free right to use, copy, and modify the software code provided by us |
6 | | -("Software Code"). You may not sublicense the Software Code or any use of it |
7 | | -(except to your affiliates and to vendors to perform work on your behalf) |
8 | | -through distribution, network access, service agreement, lease, rental, or |
9 | | -otherwise. This license does not purport to express any claim of ownership over |
10 | | -data you may have shared with Microsoft in the creation of the Software Code. |
11 | | -Unless applicable law gives you more rights, Microsoft reserves all other |
12 | | -rights not expressly granted herein, whether by implication, estoppel or |
13 | | -otherwise. |
14 | | - |
15 | | -THE SOFTWARE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS |
16 | | -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | | -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | | -MICROSOFT OR ITS LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
19 | | -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | | -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
21 | | -BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
22 | | -IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
23 | | -ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE CODE, EVEN IF ADVISED OF THE |
24 | | -POSSIBILITY OF SUCH DAMAGE. |
25 | | -""" |
26 | | -import os, json, datetime, sys |
27 | | -from operator import attrgetter |
28 | | -from azureml.core import Workspace |
29 | | -from azureml.core.model import Model |
30 | | -from azureml.core.image import Image |
31 | | -from azureml.core.compute import AksCompute, ComputeTarget |
32 | | -from azureml.core.webservice import Webservice, AksWebservice |
33 | | -from azureml.core.authentication import AzureCliAuthentication |
34 | | - |
35 | | -cli_auth = AzureCliAuthentication() |
36 | | -# Get workspace |
37 | | -ws = Workspace.from_config(auth=cli_auth) |
38 | | - |
39 | | -# Get the Image to deploy details |
40 | | -try: |
41 | | - with open("aml_config/image.json") as f: |
42 | | - config = json.load(f) |
43 | | -except: |
44 | | - print("No new model, thus no deployment on ACI") |
45 | | - # raise Exception('No new model to register as production model perform better') |
46 | | - sys.exit(0) |
47 | | - |
48 | | -image_name = config["image_name"] |
49 | | -image_version = config["image_version"] |
50 | | - |
51 | | -images = Image.list(workspace=ws) |
52 | | -image, = (m for m in images if m.version == image_version and m.name == image_name) |
53 | | -print( |
54 | | - "From image.json, Image used to deploy webservice on ACI: {}\nImage Version: {}\nImage Location = {}".format( |
55 | | - image.name, image.version, image.image_location |
56 | | - ) |
57 | | -) |
58 | | - |
59 | | -# image = max(images, key=attrgetter('version')) |
60 | | -# print('From Max Version, Image used to deploy webservice on ACI: {}\nImage Version: {}\nImage Location = {}'.format(image.name, image.version, image.image_location)) |
61 | | - |
62 | | -# Check if AKS already Available |
63 | | -try: |
64 | | - with open("aml_config/aks_webservice.json") as f: |
65 | | - config = json.load(f) |
66 | | - aks_name = config["aks_name"] |
67 | | - aks_service_name = config["aks_service_name"] |
68 | | - compute_list = ws.compute_targets() |
69 | | - aks_target, = (c for c in compute_list if c.name == aks_name) |
70 | | - service = Webservice(name=aks_service_name, workspace=ws) |
71 | | - print( |
72 | | - "Updating AKS service {} with image: {}".format( |
73 | | - aks_service_name, image.image_location |
74 | | - ) |
75 | | - ) |
76 | | - service.update(image=image) |
77 | | -except: |
78 | | - aks_name = "aks" + datetime.datetime.now().strftime("%m%d%H") |
79 | | - aks_service_name = "akswebservice" + datetime.datetime.now().strftime("%m%d%H") |
80 | | - prov_config = AksCompute.provisioning_configuration( |
81 | | - agent_count=6, vm_size="Standard_F2", location="eastus" |
82 | | - ) |
83 | | - print( |
84 | | - "No AKS found in aks_webservice.json. Creating new Aks: {} and AKS Webservice: {}".format( |
85 | | - aks_name, aks_service_name |
86 | | - ) |
87 | | - ) |
88 | | - # Create the cluster |
89 | | - aks_target = ComputeTarget.create( |
90 | | - workspace=ws, name=aks_name, provisioning_configuration=prov_config |
91 | | - ) |
92 | | - |
93 | | - aks_target.wait_for_completion(show_output=True) |
94 | | - print(aks_target.provisioning_state) |
95 | | - print(aks_target.provisioning_errors) |
96 | | - |
97 | | - # Use the default configuration (can also provide parameters to customize) |
98 | | - aks_config = AksWebservice.deploy_configuration(enable_app_insights=True) |
99 | | - |
100 | | - service = Webservice.deploy_from_image( |
101 | | - workspace=ws, |
102 | | - name=aks_service_name, |
103 | | - image=image, |
104 | | - deployment_config=aks_config, |
105 | | - deployment_target=aks_target, |
106 | | - ) |
107 | | - |
108 | | - service.wait_for_deployment(show_output=True) |
109 | | - print(service.state) |
110 | | - print( |
111 | | - "Deployed AKS Webservice: {} \nWebservice Uri: {}".format( |
112 | | - service.name, service.scoring_uri |
113 | | - ) |
114 | | - ) |
115 | | - |
116 | | - |
117 | | -# Writing the AKS details to /aml_config/aks_webservice.json |
118 | | -aks_webservice = {} |
119 | | -aks_webservice["aks_name"] = aks_name |
120 | | -aks_webservice["aks_service_name"] = service.name |
121 | | -aks_webservice["aks_url"] = service.scoring_uri |
122 | | -aks_webservice["aks_keys"] = service.get_keys() |
123 | | -with open("aml_config/aks_webservice.json", "w") as outfile: |
124 | | - json.dump(aks_webservice, outfile) |
| 1 | +""" |
| 2 | +Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + |
| 4 | +Microsoft Corporation (“Microsoft”) grants you a nonexclusive, perpetual, |
| 5 | +royalty-free right to use, copy, and modify the software code provided by us |
| 6 | +("Software Code"). You may not sublicense the Software Code or any use of it |
| 7 | +(except to your affiliates and to vendors to perform work on your behalf) |
| 8 | +through distribution, network access, service agreement, lease, rental, or |
| 9 | +otherwise. This license does not purport to express any claim of ownership over |
| 10 | +data you may have shared with Microsoft in the creation of the Software Code. |
| 11 | +Unless applicable law gives you more rights, Microsoft reserves all other |
| 12 | +rights not expressly granted herein, whether by implication, estoppel or |
| 13 | +otherwise. |
| 14 | + |
| 15 | +THE SOFTWARE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | +MICROSOFT OR ITS LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 21 | +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 | +IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | +ARISING IN ANY WAY OUT OF THE USE OF THE SOFTWARE CODE, EVEN IF ADVISED OF THE |
| 24 | +POSSIBILITY OF SUCH DAMAGE. |
| 25 | +""" |
| 26 | +import os, json, datetime, sys |
| 27 | +from operator import attrgetter |
| 28 | +from azureml.core import Workspace |
| 29 | +from azureml.core.model import Model |
| 30 | +from azureml.core.image import Image |
| 31 | +from azureml.core.compute import AksCompute, ComputeTarget |
| 32 | +from azureml.core.webservice import Webservice, AksWebservice |
| 33 | +from azureml.core.authentication import AzureCliAuthentication |
| 34 | + |
| 35 | +cli_auth = AzureCliAuthentication() |
| 36 | +# Get workspace |
| 37 | +ws = Workspace.from_config(auth=cli_auth) |
| 38 | + |
| 39 | +# Get the Image to deploy details |
| 40 | +try: |
| 41 | + with open("aml_config/image.json") as f: |
| 42 | + config = json.load(f) |
| 43 | +except: |
| 44 | + print("No new model, thus no deployment on ACI") |
| 45 | + # raise Exception('No new model to register as production model perform better') |
| 46 | + sys.exit(0) |
| 47 | + |
| 48 | +image_name = config["image_name"] |
| 49 | +image_version = config["image_version"] |
| 50 | + |
| 51 | +images = Image.list(workspace=ws) |
| 52 | +image, = (m for m in images if m.version == image_version and m.name == image_name) |
| 53 | +print( |
| 54 | + "From image.json, Image used to deploy webservice on ACI: {}\nImage Version: {}\nImage Location = {}".format( |
| 55 | + image.name, image.version, image.image_location |
| 56 | + ) |
| 57 | +) |
| 58 | + |
| 59 | +# image = max(images, key=attrgetter('version')) |
| 60 | +# print('From Max Version, Image used to deploy webservice on ACI: {}\nImage Version: {}\nImage Location = {}'.format(image.name, image.version, image.image_location)) |
| 61 | + |
| 62 | +# Check if AKS already Available |
| 63 | +try: |
| 64 | + with open("aml_config/aks_webservice.json") as f: |
| 65 | + config = json.load(f) |
| 66 | + aks_name = config["aks_name"] |
| 67 | + aks_service_name = config["aks_service_name"] |
| 68 | + compute_list = ws.compute_targets() |
| 69 | + aks_target, = (c for c in compute_list if c.name == aks_name) |
| 70 | + service = Webservice(name=aks_service_name, workspace=ws) |
| 71 | + print( |
| 72 | + "Updating AKS service {} with image: {}".format( |
| 73 | + aks_service_name, image.image_location |
| 74 | + ) |
| 75 | + ) |
| 76 | + service.update(image=image) |
| 77 | +except: |
| 78 | + aks_name = "aks" + datetime.datetime.now().strftime("%m%d%H") |
| 79 | + aks_service_name = "akswebservice" + datetime.datetime.now().strftime("%m%d%H") |
| 80 | + prov_config = AksCompute.provisioning_configuration( |
| 81 | + agent_count=6, vm_size="Standard_F4s", location="eastus" |
| 82 | + ) |
| 83 | + print( |
| 84 | + "No AKS found in aks_webservice.json. Creating new Aks: {} and AKS Webservice: {}".format( |
| 85 | + aks_name, aks_service_name |
| 86 | + ) |
| 87 | + ) |
| 88 | + # Create the cluster |
| 89 | + aks_target = ComputeTarget.create( |
| 90 | + workspace=ws, name=aks_name, provisioning_configuration=prov_config |
| 91 | + ) |
| 92 | + |
| 93 | + aks_target.wait_for_completion(show_output=True) |
| 94 | + print(aks_target.provisioning_state) |
| 95 | + print(aks_target.provisioning_errors) |
| 96 | + |
| 97 | + # Use the default configuration (can also provide parameters to customize) |
| 98 | + aks_config = AksWebservice.deploy_configuration(enable_app_insights=True) |
| 99 | + |
| 100 | + service = Webservice.deploy_from_image( |
| 101 | + workspace=ws, |
| 102 | + name=aks_service_name, |
| 103 | + image=image, |
| 104 | + deployment_config=aks_config, |
| 105 | + deployment_target=aks_target, |
| 106 | + ) |
| 107 | + |
| 108 | + service.wait_for_deployment(show_output=True) |
| 109 | + print(service.state) |
| 110 | + print( |
| 111 | + "Deployed AKS Webservice: {} \nWebservice Uri: {}".format( |
| 112 | + service.name, service.scoring_uri |
| 113 | + ) |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +# Writing the AKS details to /aml_config/aks_webservice.json |
| 118 | +aks_webservice = {} |
| 119 | +aks_webservice["aks_name"] = aks_name |
| 120 | +aks_webservice["aks_service_name"] = service.name |
| 121 | +aks_webservice["aks_url"] = service.scoring_uri |
| 122 | +aks_webservice["aks_keys"] = service.get_keys() |
| 123 | +with open("aml_config/aks_webservice.json", "w") as outfile: |
| 124 | + json.dump(aks_webservice, outfile) |
0 commit comments