-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Karenchen/enable draftv2 #4651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Karenchen/enable draftv2 #4651
Changes from 33 commits
547441e
4aacf22
6cd1e00
48ec4bc
a15212e
1eceebf
6f775df
577a65a
cac9fc8
275ea32
29db02f
8b05aa4
6e9614c
525652a
c617cef
b6d5e0f
b9e52b5
8a643e8
3d103b2
e7e9c93
0ef773c
a52fedb
8cc6b05
6ebaf5e
448188f
c911b77
f68643b
1b872cf
61bdabf
e6ea3d0
daac6fc
fd314e5
df31dbf
2725913
85fb982
cc16119
90e216b
84e693a
6433574
76644f3
a3eab7e
3091bdc
dd5f2b7
9738354
d2febd1
dca6a1c
fe44121
bd06547
c458df9
93f3156
d51b743
bd4a8ef
ce33eca
5c355d9
6f40dc2
1836550
c6db3c1
c40d9b1
4247f00
c2e869a
b97861c
f8355d0
86ad0ac
9807f52
f7d6676
11baae4
4ae096f
c80cda3
74fbf95
eadc8ea
efe8c73
0532e51
472e2af
de30662
ecd10df
5abea0c
3e5a089
a6653e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| # pylint: disable=too-many-lines | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import sre_parse | ||
| from typing import List, Optional | ||
| import shutil | ||
| import subprocess | ||
| import requests | ||
| import os | ||
| import platform | ||
| from pathlib import Path | ||
| from knack.prompting import prompt_y_n | ||
|
|
||
|
|
||
| def aks_draft_app_init(deployment_path: str, | ||
| app_name: str, | ||
| language: str, | ||
| create_config: str, | ||
| dockerfile_only: str, | ||
| deployment_only: str) -> None: | ||
| file_path = _binary_pre_check() | ||
| if not file_path: | ||
| raise ValueError('Binary check was NOT executed successfully') | ||
|
|
||
| arguments = _build_arguments(app_name, language, create_config, dockerfile_only, deployment_only) | ||
| run_successful = _run(file_path, deployment_path, arguments) | ||
| if run_successful: | ||
| _cmd_finish() | ||
| else: | ||
| raise ValueError('\'az aks app init\' was NOT executed successfully') | ||
|
|
||
|
|
||
| # If setup is valid this method returns the correct binary path to execute | ||
| def _binary_pre_check() -> str: | ||
| print('The DraftV2 setup is in progress...') | ||
|
||
| draftv2_binary_path = _get_existing_path() | ||
|
|
||
| if draftv2_binary_path: # use found binary | ||
| return draftv2_binary_path | ||
| else: # prompt user to download binary | ||
| return _download_binary() | ||
|
|
||
|
|
||
| def _get_filename() -> Optional[str]: | ||
| operating_system = platform.system().lower() | ||
| architecture = platform.machine().lower() | ||
|
|
||
| if architecture == 'x86_64': | ||
| architecture = 'amd64' | ||
| if architecture not in ['arm64', 'amd64']: | ||
| print('Cannot find a suitable download for the current system architecture. Draft only supports AMD64 and ARM64.') | ||
| return None | ||
|
|
||
| return f'draftv2-{operating_system}-{architecture}' | ||
|
|
||
|
|
||
| # Returns path to existing draftv2 binary and None otherwise | ||
| def _get_existing_path() -> Optional[str]: | ||
| print('Checking if DraftV2 binary exists locally...') | ||
|
|
||
| filename = _get_filename() | ||
| if not filename: | ||
| return None | ||
|
|
||
| paths = _get_potential_paths() | ||
| if not paths: | ||
| print('List of potential DraftV2 paths is empty') | ||
| return None | ||
|
|
||
| for path in paths: | ||
| binary_file_path = path + '/' + filename | ||
| if os.path.exists(binary_file_path): | ||
| print('Existing binary found at: ' + binary_file_path) | ||
| return binary_file_path | ||
| return None | ||
|
|
||
|
|
||
| # Returns a list of potential draftV2 binary paths | ||
| def _get_potential_paths() -> List[str]: | ||
| result = [str(Path.home()) + '/' + '.aksapp'] | ||
| paths = os.environ['PATH'].split(':') | ||
karenychen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for path in paths: | ||
| if 'draftv2' in path: | ||
| result.append(path) | ||
| return result | ||
|
|
||
|
|
||
| def _build_arguments(app_name: str, | ||
| language: str, | ||
| create_config: str, | ||
| dockerfile_only: str, | ||
| deployment_only: str) -> List[str]: | ||
| options = { | ||
| 'app-name': app_name, | ||
| 'language': language, | ||
| 'create-config': create_config, | ||
| 'dockerfile-only': dockerfile_only, | ||
| 'deployment-only': deployment_only | ||
| } | ||
| args_list = [] | ||
| for arg, val in options.items(): | ||
| if val: | ||
| args_list.append(f'--{arg}={val}') | ||
| return args_list | ||
|
|
||
|
|
||
| def _run(binary_path: str, deployment_path: str, arguments: List[str]) -> bool: | ||
| if binary_path is None: | ||
| raise ValueError('The given Binary path was null or empty') | ||
|
|
||
| print('Running DraftV2 Binary ...') | ||
|
||
| cmd = [binary_path, 'create', deployment_path] + arguments | ||
| process = subprocess.Popen(cmd) | ||
| exit_code = process.wait() | ||
| return exit_code == 0 | ||
|
|
||
|
|
||
| def _cmd_finish(): | ||
karenychen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Clean up logic can go here if needed | ||
| print('Finishing running \'az aks app init\'') | ||
|
|
||
|
|
||
| def _download_binary() -> Optional[str]: | ||
| # prompt user to download binary. If users says no, we error out and tell them that this requires the binary | ||
| msg = 'The required binary was not found. Would you like us to download the required binary for you?' | ||
|
|
||
| if not prompt_y_n(msg, default='n'): | ||
| raise ValueError('`az aks app` requires the missing dependency') | ||
|
|
||
| print('Attempting to download dependency...') | ||
|
|
||
| filename = _get_filename() | ||
| if not filename: | ||
| return None | ||
|
|
||
| url = f'https://github.com/Azure/aks-app/releases/latest/download/{filename}' | ||
karenychen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| headers = {'Accept': 'application/octet-stream'} | ||
|
|
||
| dir_name = '.aksapp' | ||
| # Downloading the file by sending the request to the URL | ||
| req = requests.get(url, headers=headers) | ||
karenychen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| binary_path = str(Path.home()) + '/' + dir_name | ||
|
|
||
| # Directory | ||
| if os.path.exists(binary_path) is False: | ||
| os.chdir(str(Path.home())) | ||
| os.mkdir(dir_name) | ||
| print(f'Directory {dir_name} was created inside of your HOME directory') | ||
|
|
||
| if req.ok: | ||
| # Split URL to get the file name 'draftv2-darwin-amd64' | ||
| os.chdir(binary_path) | ||
| # Writing the file to the local file system | ||
| with open(filename, 'wb') as output_file: | ||
| output_file.write(req.content) | ||
| print('Download of DraftV2 binary was successful with a status code: ' + str(req.status_code)) | ||
| os.chmod(binary_path + '/' + filename, 0o755) | ||
| return binary_path + '/' + filename | ||
|
|
||
| print('Download of DraftV2 binary was unsuccessful with a status code: ' + str(req.status_code)) | ||
| return None | ||
Uh oh!
There was an error while loading. Please reload this page.