ssh subcommand added for vm command + config file added

This commit is contained in:
ahmadbilalkhalid 2019-12-23 18:23:18 +05:00
parent 89b48c5f06
commit 267828e85a
5 changed files with 34 additions and 3 deletions

5
.gitignore vendored
View file

@ -2,4 +2,7 @@
.vscode
__pycache__
ucloud_cli.egg-info
ucloud_cli.egg-info
build/
dist/

4
conf/ucloud-cli.conf Normal file
View file

@ -0,0 +1,4 @@
OTP_NAME=replace_me
OTP_REALM=replace_me
OTP_SEED=replace_me
UCLOUD_API_SERVER=http://[::]:5000

View file

@ -1,3 +1,5 @@
import os
from setuptools import setup, find_packages
with open("README.md", "r") as fh:
@ -25,4 +27,5 @@ setup(name='ucloud_cli',
'click'
],
scripts=['bin/ucloud-cli'],
zip_safe=False)
data_files=[(os.path.expanduser('~/ucloud/'), ['conf/ucloud-cli.conf'])],
zip_safe=False)

View file

@ -1,5 +1,7 @@
import click
import json
import requests
import subprocess as sp
from ucloud_cli.commands.helper import OTPCredentials, load_dump_pretty
from ucloud_cli.config import env_vars
@ -118,3 +120,22 @@ def vm_migration(name, realm, seed, vm_name, destination, in_support_of):
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "migrate"), json=data
)
print(load_dump_pretty(r.content))
@vm.command("ssh")
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
@click.option("--vm-name", required=True)
@click.option("--in_support_of")
def ssh(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(join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "status"), json=data)
try:
_json = json.loads(r.content)
sp.run(['ssh', '-o', 'ConnectTimeout=10',
'root@{}'.format(_json['ip'][0])])
except Exception as err:
print("Some error occurred while accessing VM."
"Make sure VM is running", err)

View file

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