Little bit refactoring + Read env from ~/.ucloud.conf + disable tracebacks

This commit is contained in:
ahmadbilalkhalid 2019-11-17 23:54:39 +05:00
parent a07833d37a
commit 51e1130071
13 changed files with 331 additions and 102 deletions

View file

@ -4,6 +4,7 @@ url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
prospector = "*"
[packages]
requests = "*"

250
Pipfile.lock generated Executable file → Normal file
View file

@ -1,80 +1,272 @@
{
"_meta": {
"hash": {
"sha256": "ad97ea0bec676c536266766c718bd590b17e0e28d47728ab68cf21d09fbb07d4"
"sha256": "e3ca9beed0400b9be706e33007f7148c0a299bcb7881a3f501f9497ca9217b18"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.5"
},
"requires": {"python_version": "3.5"},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
{"name": "pypi", "url": "https://pypi.org/simple", "verify_ssl": true}
],
},
"default": {
"certifi": {
"hashes": [
"sha256:e4f3620cfea4f83eedc95b24abd9cd56f3c4b146dd0177e83a21b4eb49e21e50",
"sha256:fd7c7c74727ddcf00e9acd26bba8da604ffec95bf1c2144e67aff7a8b50e6cef"
"sha256:fd7c7c74727ddcf00e9acd26bba8da604ffec95bf1c2144e67aff7a8b50e6cef",
],
"version": "==2019.9.11"
"version": "==2019.9.11",
},
"chardet": {
"hashes": [
"sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae",
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"
"sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691",
],
"version": "==3.0.4"
"version": "==3.0.4",
},
"click": {
"hashes": [
"sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13",
"sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"
"sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7",
],
"index": "pypi",
"version": "==7.0"
"version": "==7.0",
},
"idna": {
"hashes": [
"sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407",
"sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"
"sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c",
],
"version": "==2.8"
"version": "==2.8",
},
"pyotp": {
"hashes": [
"sha256:c88f37fd47541a580b744b42136f387cdad481b560ef410c0d85c957eb2a2bc0",
"sha256:fc537e8acd985c5cbf51e11b7d53c42276fee017a73aec7c07380695671ca1a1"
"sha256:fc537e8acd985c5cbf51e11b7d53c42276fee017a73aec7c07380695671ca1a1",
],
"index": "pypi",
"version": "==2.3.0"
"version": "==2.3.0",
},
"python-decouple": {
"hashes": [
"sha256:1317df14b43efee4337a4aa02914bf004f010cd56d6c4bd894e6474ec8c4fe2d"
"sha256:55c546b85b0c47a15a47a4312d451a437f7344a9be3e001660bccd93b637de95"
],
"index": "pypi",
"version": "==3.1"
"version": "==3.3",
},
"requests": {
"hashes": [
"sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4",
"sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31"
"sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31",
],
"index": "pypi",
"version": "==2.22.0"
"version": "==2.22.0",
},
"urllib3": {
"hashes": [
"sha256:b246607a25ac80bedac05c6f282e3cdaf3afb65420fd024ac94435cabe6e18d1",
"sha256:dbe59173209418ae49d485b87d1681aefa36252ee85884c31346debd19463232"
"sha256:a8a318824cc77d1fd4b2bec2ded92646630d7fe8619497b142c84a9e6f5a7293",
"sha256:f3c5fd51747d450d4dcf6f923c81f78f811aab8205fda64b0aba34a4e48b0745",
],
"version": "==1.25.3"
}
"version": "==1.25.7",
},
},
"develop": {
"astroid": {
"hashes": [
"sha256:6560e1e1749f68c64a4b5dee4e091fce798d2f0d84ebe638cf0e0585a343acf4",
"sha256:b65db1bbaac9f9f4d190199bb8680af6f6f84fd3769a5ea883df8a91fe68b4c4",
],
"version": "==2.2.5",
},
"dodgy": {
"hashes": [
"sha256:65e13cf878d7aff129f1461c13cb5fd1bb6dfe66bb5327e09379c3877763280c"
],
"version": "==0.1.9",
},
"isort": {
"hashes": [
"sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1",
"sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd",
],
"version": "==4.3.21",
},
"lazy-object-proxy": {
"hashes": [
"sha256:0c4b206227a8097f05c4dbdd323c50edf81f15db3b8dc064d08c62d37e1a504d",
"sha256:194d092e6f246b906e8f70884e620e459fc54db3259e60cf69a4d66c3fda3449",
"sha256:1be7e4c9f96948003609aa6c974ae59830a6baecc5376c25c92d7d697e684c08",
"sha256:4677f594e474c91da97f489fea5b7daa17b5517190899cf213697e48d3902f5a",
"sha256:48dab84ebd4831077b150572aec802f303117c8cc5c871e182447281ebf3ac50",
"sha256:5541cada25cd173702dbd99f8e22434105456314462326f06dba3e180f203dfd",
"sha256:59f79fef100b09564bc2df42ea2d8d21a64fdcda64979c0fa3db7bdaabaf6239",
"sha256:8d859b89baf8ef7f8bc6b00aa20316483d67f0b1cbf422f5b4dc56701c8f2ffb",
"sha256:9254f4358b9b541e3441b007a0ea0764b9d056afdeafc1a5569eee1cc6c1b9ea",
"sha256:9651375199045a358eb6741df3e02a651e0330be090b3bc79f6d0de31a80ec3e",
"sha256:97bb5884f6f1cdce0099f86b907aa41c970c3c672ac8b9c8352789e103cf3156",
"sha256:9b15f3f4c0f35727d3a0fba4b770b3c4ebbb1fa907dbcc046a1d2799f3edd142",
"sha256:a2238e9d1bb71a56cd710611a1614d1194dc10a175c1e08d75e1a7bcc250d442",
"sha256:a6ae12d08c0bf9909ce12385803a543bfe99b95fe01e752536a60af2b7797c62",
"sha256:ca0a928a3ddbc5725be2dd1cf895ec0a254798915fb3a36af0964a0a4149e3db",
"sha256:cb2c7c57005a6804ab66f106ceb8482da55f5314b7fcb06551db1edae4ad1531",
"sha256:d74bb8693bf9cf75ac3b47a54d716bbb1a92648d5f781fc799347cfc95952383",
"sha256:d945239a5639b3ff35b70a88c5f2f491913eb94871780ebfabb2568bd58afc5a",
"sha256:eba7011090323c1dadf18b3b689845fd96a61ba0a1dfbd7f24b921398affc357",
"sha256:efa1909120ce98bbb3777e8b6f92237f5d5c8ea6758efea36a473e1d38f7d3e4",
"sha256:f3900e8a5de27447acbf900b4750b0ddfd7ec1ea7fbaf11dfa911141bc522af0",
],
"version": "==1.4.3",
},
"mccabe": {
"hashes": [
"sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42",
"sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f",
],
"version": "==0.6.1",
},
"pep8-naming": {
"hashes": [
"sha256:1b419fa45b68b61cd8c5daf4e0c96d28915ad14d3d5f35fcc1e7e95324a33a2e",
"sha256:4eedfd4c4b05e48796f74f5d8628c068ff788b9c2b08471ad408007fc6450e5a",
],
"version": "==0.4.1",
},
"prospector": {
"hashes": [
"sha256:aba551e53dc1a5a432afa67385eaa81d7b4cf4c162dc1a4d0ee00b3a0712ad90"
],
"index": "pypi",
"version": "==1.1.7",
},
"pycodestyle": {
"hashes": [
"sha256:cbc619d09254895b0d12c2c691e237b2e91e9b2ecf5e84c26b35400f93dcfb83",
"sha256:cbfca99bd594a10f674d0cd97a3d802a1fdef635d4361e1a2658de47ed261e3a",
],
"version": "==2.4.0",
},
"pydocstyle": {
"hashes": [
"sha256:04c84e034ebb56eb6396c820442b8c4499ac5eb94a3bda88951ac3dc519b6058",
"sha256:66aff87ffe34b1e49bff2dd03a88ce6843be2f3346b0c9814410d34987fbab59",
],
"version": "==4.0.1",
},
"pyflakes": {
"hashes": [
"sha256:08bd6a50edf8cffa9fa09a463063c425ecaaf10d1eb0335a7e8b1401aef89e6f",
"sha256:8d616a382f243dbf19b54743f280b80198be0bca3a5396f1d2e1fca6223e8805",
],
"version": "==1.6.0",
},
"pylint": {
"hashes": [
"sha256:5d77031694a5fb97ea95e828c8d10fc770a1df6eb3906067aaed42201a8a6a09",
"sha256:723e3db49555abaf9bf79dc474c6b9e2935ad82230b10c1138a71ea41ac0fff1",
],
"version": "==2.3.1",
},
"pylint-celery": {
"hashes": [
"sha256:41e32094e7408d15c044178ea828dd524beedbdbe6f83f712c5e35bde1de4beb"
],
"version": "==0.3",
},
"pylint-django": {
"hashes": [
"sha256:75c69d1ec2275918c37f175976da20e2f1e1e62e067098a685cd263ffa833dfd",
"sha256:c7cb6384ea7b33ea77052a5ae07358c10d377807390ef27b2e6ff997303fadb7",
],
"version": "==2.0.10",
},
"pylint-flask": {
"hashes": [
"sha256:f4d97de2216bf7bfce07c9c08b166e978fe9f2725de2a50a9845a97de7e31517"
],
"version": "==0.6",
},
"pylint-plugin-utils": {
"hashes": [
"sha256:2f30510e1c46edf268d3a195b2849bd98a1b9433229bb2ba63b8d776e1fc4d0a",
"sha256:57625dcca20140f43731311cd8fd879318bf45a8b0fd17020717a8781714a25a",
],
"version": "==0.6",
},
"pyyaml": {
"hashes": [
"sha256:0113bc0ec2ad727182326b61326afa3d1d8280ae1122493553fd6f4397f33df9",
"sha256:01adf0b6c6f61bd11af6e10ca52b7d4057dd0be0343eb9283c878cf3af56aee4",
"sha256:5124373960b0b3f4aa7df1707e63e9f109b5263eca5976c66e08b1c552d4eaf8",
"sha256:5ca4f10adbddae56d824b2c09668e91219bb178a1eee1faa56af6f99f11bf696",
"sha256:7907be34ffa3c5a32b60b95f4d95ea25361c951383a894fec31be7252b2b6f34",
"sha256:7ec9b2a4ed5cad025c2278a1e6a19c011c80a3caaac804fd2d329e9cc2c287c9",
"sha256:87ae4c829bb25b9fe99cf71fbb2140c448f534e24c998cc60f39ae4f94396a73",
"sha256:9de9919becc9cc2ff03637872a440195ac4241c80536632fffeb6a1e25a74299",
"sha256:a5a85b10e450c66b49f98846937e8cfca1db3127a9d5d1e31ca45c3d0bef4c5b",
"sha256:b0997827b4f6a7c286c01c5f60384d218dca4ed7d9efa945c3e1aa623d5709ae",
"sha256:b631ef96d3222e62861443cc89d6563ba3eeb816eeb96b2629345ab795e53681",
"sha256:bf47c0607522fdbca6c9e817a6e81b08491de50f3766a7a0e6a5be7905961b41",
"sha256:f81025eddd0327c7d4cfe9b62cf33190e1e736cc6e97502b3ec425f574b3e7a8",
],
"version": "==5.1.2",
},
"requirements-detector": {
"hashes": [
"sha256:9fbc4b24e8b7c3663aff32e3eba34596848c6b91bd425079b386973bd8d08931"
],
"version": "==0.6",
},
"setoptconf": {
"hashes": [
"sha256:5b0b5d8e0077713f5d5152d4f63be6f048d9a1bb66be15d089a11c898c3cf49c"
],
"version": "==0.2.0",
},
"six": {
"hashes": [
"sha256:1f1b7d42e254082a9db6279deae68afb421ceba6158efa6131de7b3003ee93fd",
"sha256:30f610279e8b2578cab6db20741130331735c781b56053c59c4076da27f06b66",
],
"version": "==1.13.0",
},
"snowballstemmer": {
"hashes": [
"sha256:209f257d7533fdb3cb73bdbd24f436239ca3b2fa67d56f6ff88e86be08cc5ef0",
"sha256:df3bac3df4c2c01363f3dd2cfa78cce2840a79b9f1c2d2de9ce8d31683992f52",
],
"version": "==2.0.0",
},
"typed-ast": {
"hashes": [
"sha256:1170afa46a3799e18b4c977777ce137bb53c7485379d9706af8a59f2ea1aa161",
"sha256:18511a0b3e7922276346bcb47e2ef9f38fb90fd31cb9223eed42c85d1312344e",
"sha256:262c247a82d005e43b5b7f69aff746370538e176131c32dda9cb0f324d27141e",
"sha256:2b907eb046d049bcd9892e3076c7a6456c93a25bebfe554e931620c90e6a25b0",
"sha256:354c16e5babd09f5cb0ee000d54cfa38401d8b8891eefa878ac772f827181a3c",
"sha256:48e5b1e71f25cfdef98b013263a88d7145879fbb2d5185f2a0c79fa7ebbeae47",
"sha256:4e0b70c6fc4d010f8107726af5fd37921b666f5b31d9331f0bd24ad9a088e631",
"sha256:630968c5cdee51a11c05a30453f8cd65e0cc1d2ad0d9192819df9978984529f4",
"sha256:66480f95b8167c9c5c5c87f32cf437d585937970f3fc24386f313a4c97b44e34",
"sha256:71211d26ffd12d63a83e079ff258ac9d56a1376a25bc80b1cdcdf601b855b90b",
"sha256:7954560051331d003b4e2b3eb822d9dd2e376fa4f6d98fee32f452f52dd6ebb2",
"sha256:838997f4310012cf2e1ad3803bce2f3402e9ffb71ded61b5ee22617b3a7f6b6e",
"sha256:95bd11af7eafc16e829af2d3df510cecfd4387f6453355188342c3e79a2ec87a",
"sha256:bc6c7d3fa1325a0c6613512a093bc2a2a15aeec350451cbdf9e1d4bffe3e3233",
"sha256:cc34a6f5b426748a507dd5d1de4c1978f2eb5626d51326e43280941206c209e1",
"sha256:d755f03c1e4a51e9b24d899561fec4ccaf51f210d52abdf8c07ee2849b212a36",
"sha256:d7c45933b1bdfaf9f36c579671fec15d25b06c8398f113dab64c18ed1adda01d",
"sha256:d896919306dd0aa22d0132f62a1b78d11aaf4c9fc5b3410d3c666b818191630a",
"sha256:fdc1c9bbf79510b76408840e009ed65958feba92a88833cdceecff93ae8fff66",
"sha256:ffde2fbfad571af120fcbfbbc61c72469e72f550d676c3342492a9dfdefb8f12",
],
"markers": "implementation_name == 'cpython'",
"version": "==1.4.0",
},
"wrapt": {
"hashes": [
"sha256:565a021fd19419476b9362b05eeaa094178de64f8361e44468f9e9d7843901e1"
],
"version": "==1.11.2",
},
},
"develop": {}
}

View file

@ -1 +0,0 @@
# ucloud-cli

0
commands/__init__ → __init__.py Executable file → Normal file
View file

0
commands/__init__.py Executable file
View file

View file

@ -1,11 +1,10 @@
import json
from commands.helper import OTPCredentials, load_dump_pretty
from decouple import config
import click
import requests
from .helper import OTPCredentials, load_dump_pretty
from config import env_vars
from os.path import join as join_path
@click.group()
def host():
@ -25,17 +24,15 @@ def create(name, realm, seed, hostname, cpu, ram, os_ssd, hdd):
data = {
**OTPCredentials(name, realm, seed).get_json(),
"hostname": hostname,
"specs": {
'cpu': cpu,
'ram': ram,
'os-ssd': os_ssd,
'hdd': hdd
},
"specs": {"cpu": cpu, "ram": ram, "os-ssd": os_ssd, "hdd": hdd},
}
r = requests.post("{}/host/create".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.post(
join_path(env_vars.get("UCLOUD_API_SERVER"), "host", "create"), json=data
)
print(load_dump_pretty(r.content))
@host.command("list")
def list_host():
r = requests.get("{}/host/list".format(config('UCLOUD_API_SERVER')))
r = requests.get(join_path(env_vars.get("UCLOUD_API_SERVER"), "host", "list"))
print(load_dump_pretty(r.content))

View file

@ -1,9 +1,11 @@
from commands.helper import load_dump_pretty
from decouple import config
from config import env_vars
from os.path import join as join_path
import click
import requests
@click.group()
def image():
pass
@ -11,10 +13,9 @@ def image():
@image.command("list")
@click.option("--public", is_flag=True)
@click.option("--private", is_flag=True)
def _list(public, private):
def _list(public):
if public:
r = requests.get("{}/image/list-public".format(config('UCLOUD_API_SERVER')))
r = requests.get(join_path(env_vars.get("UCLOUD_API_SERVER"), "image", "list-public"))
print(load_dump_pretty(r.content))
@ -24,5 +25,7 @@ def _list(public, private):
@click.option("--image-store-name", required=True)
def create_from_file(name, uuid, image_store_name):
data = {"name": name, "uuid": uuid, "image_store": image_store_name}
r = requests.post("{}/image/create".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.post(
join_path(env_vars.get("UCLOUD_API_SERVER"), "image", "create"), json=data
)
print(load_dump_pretty(r.content))

View file

@ -1,9 +1,11 @@
from commands.helper import load_dump_pretty, OTPCredentials
from decouple import config
from config import env_vars
from os.path import join as join_path
import click
import requests
@click.group()
def network():
pass
@ -21,7 +23,9 @@ def create(name, realm, seed, network_name, network_type, user):
**OTPCredentials(name, realm, seed).get_json(),
"network_name": network_name,
"type": network_type,
"user": user
"user": user,
}
r = requests.post("{}/network/create".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.post(
join_path(env_vars.get("UCLOUD_API_SERVER"), "network", "create"), json=data
)
print(load_dump_pretty(r.content))

View file

@ -1,5 +1,6 @@
from commands.helper import OTPCredentials, load_dump_pretty
from decouple import config
from config import env_vars
from os.path import join as join_path
import click
import requests
@ -16,7 +17,9 @@ def user():
@click.option("--seed", envvar="OTP_SEED", required=True)
def list_files(name, realm, seed):
data = OTPCredentials(name, realm, seed).get_json()
r = requests.get("{}/user/files".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.get(
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "files"), json=data
)
print(load_dump_pretty(r.content))
@ -26,7 +29,9 @@ def list_files(name, realm, seed):
@click.option("--seed", envvar="OTP_SEED", required=True)
def list_vms(name, realm, seed):
data = OTPCredentials(name, realm, seed).get_json()
r = requests.get("{}/user/vms".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.get(
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "vms"), json=data
)
print(load_dump_pretty(r.content))
@ -34,9 +39,11 @@ def list_vms(name, realm, seed):
@click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", envvar="OTP_SEED", required=True)
def list_files(name, realm, seed):
def list_networks(name, realm, seed):
data = OTPCredentials(name, realm, seed).get_json()
r = requests.get("{}/user/networks".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.get(
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "networks"), json=data
)
print(load_dump_pretty(r.content))
@ -48,12 +55,10 @@ def list_files(name, realm, seed):
@click.option("--key", required=True)
def add_ssh(name, realm, seed, key_name, key):
otp = OTPCredentials(name, realm, seed)
data = {
**otp.get_json(),
"key_name": key_name,
"key": key
}
r = requests.post("{}/user/add-ssh".format(config('UCLOUD_API_SERVER')), json=data)
data = {**otp.get_json(), "key_name": key_name, "key": key}
r = requests.post(
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "add-ssh"), json=data
)
print(load_dump_pretty(r.content))
@ -64,23 +69,22 @@ def add_ssh(name, realm, seed, key_name, key):
@click.option("--key-name", required=True)
def remove_ssh(name, realm, seed, key_name):
otp = OTPCredentials(name, realm, seed)
data = {
**otp.get_json(),
"key_name": key_name,
}
r = requests.get("{}/user/remove-ssh".format(config('UCLOUD_API_SERVER')), json=data)
data = {**otp.get_json(), "key_name": key_name}
r = requests.get(
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "remove-ssh"), json=data
)
print(load_dump_pretty(r.content))
@user.command("get-ssh")
@click.option("--name", envvar="OTP_NAME", required=True)
@click.option("--realm", envvar="OTP_REALM", required=True)
@click.option("--seed", envvar="OTP_SEED", required=True)
@click.option("--key-name", default="")
def add_ssh(name, realm, seed, key_name):
def get_ssh(name, realm, seed, key_name):
otp = OTPCredentials(name, realm, seed)
data = {
**otp.get_json(),
"key_name": key_name,
}
r = requests.get("{}/user/get-ssh".format(config('UCLOUD_API_SERVER')), json=data)
data = {**otp.get_json(), "key_name": key_name}
r = requests.get(
join_path(env_vars.get("UCLOUD_API_SERVER"), "user", "get-ssh"), json=data
)
print(load_dump_pretty(r.content))

View file

@ -1,15 +1,16 @@
import json
from commands.helper import OTPCredentials, load_dump_pretty
from decouple import config
import click
import requests
from commands.helper import OTPCredentials, load_dump_pretty
from config import env_vars
from os.path import join as join_path
def vm_command(command, otp, vm_name, **kwargs):
data = {**otp.get_json(), "vm_name": vm_name, "action": command, **kwargs}
r = requests.post("{}/vm/action".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.post(
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "action"), json=data
)
return r
@ -33,16 +34,13 @@ def create(name, realm, seed, vm_name, cpu, ram, os_ssd, hdd, image, network):
data = {
**OTPCredentials(name, realm, seed).get_json(),
"vm_name": vm_name,
"specs": {
'cpu': cpu,
'ram': ram,
'os-ssd': os_ssd,
'hdd': hdd
},
"specs": {"cpu": cpu, "ram": ram, "os-ssd": os_ssd, "hdd": hdd},
"network": network,
"image": image,
}
r = requests.post("{}/vm/create".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.post(
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "create"), json=data
)
print(load_dump_pretty(r.content))
@ -53,7 +51,9 @@ def create(name, realm, seed, vm_name, cpu, ram, os_ssd, hdd, image, network):
@click.option("--vm-name", required=True)
@click.option("--in_support_of")
def start(name, realm, seed, vm_name, in_support_of):
r = vm_command("start", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of)
r = vm_command(
"start", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of
)
print(load_dump_pretty(r.content))
@ -64,7 +64,9 @@ def start(name, realm, seed, vm_name, in_support_of):
@click.option("--vm-name", required=True)
@click.option("--in_support_of")
def stop(name, realm, seed, vm_name, in_support_of):
r = vm_command("stop", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of)
r = vm_command(
"stop", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of
)
print(load_dump_pretty(r.content))
@ -75,7 +77,12 @@ def stop(name, realm, seed, vm_name, in_support_of):
@click.option("--vm-name", required=True)
@click.option("--in_support_of")
def delete(name, realm, seed, vm_name, in_support_of):
r = vm_command("delete", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of)
r = vm_command(
"delete",
OTPCredentials(name, realm, seed),
vm_name,
in_support_of=in_support_of,
)
print(load_dump_pretty(r.content))
@ -88,7 +95,7 @@ def delete(name, realm, seed, vm_name, in_support_of):
def status(name, realm, seed, vm_name, in_support_of):
otp = OTPCredentials(name, realm, seed)
data = {**otp.get_json(), "vm_name": vm_name, "in_support_of": in_support_of}
r = requests.get("{}/vm/status".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.get(join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "status"), json=data)
print(load_dump_pretty(r.content))
@ -107,5 +114,7 @@ def vm_migration(name, realm, seed, vm_name, destination, in_support_of):
"destination": destination,
"in_support_of": in_support_of,
}
r = requests.post("{}/vm/migrate".format(config('UCLOUD_API_SERVER')), json=data)
r = requests.post(
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "migrate"), json=data
)
print(load_dump_pretty(r.content))

9
config.py Normal file
View file

@ -0,0 +1,9 @@
import sys
from os.path import expanduser
from decouple import Config, RepositoryEnv
try:
env_vars = Config(RepositoryEnv(expanduser("~/.ucloud.conf")))
except Exception as err:
print(err)
sys.exit(1)

9
helper.py Normal file
View file

@ -0,0 +1,9 @@
import os
import sys
def exception_handler(exception_type, exception, traceback):
if bool(os.getenv('DEBUG_UCLOUD')) is True:
sys.__excepthook__(exception_type, exception, traceback)
else:
print("%s: %s" % (exception_type.__name__, exception))

View file

@ -1,4 +1,5 @@
import click
import sys
from commands.vm import vm
from commands.user import user
@ -6,16 +7,17 @@ from commands.host import host
from commands.image import image
from commands.network import network
from helper import exception_handler
@click.group()
def entry_point():
pass
entry_point.add_command(vm)
entry_point.add_command(user)
entry_point.add_command(image)
entry_point.add_command(host)
entry_point.add_command(network)
if __name__ == "__main__":
sys.excepthook = exception_handler
entry_point.add_command(vm)
entry_point.add_command(user)
entry_point.add_command(image)
entry_point.add_command(host)
entry_point.add_command(network)
entry_point()