2022-07-11 14:11:26 +00:00
|
|
|
import json
|
2022-08-01 07:41:04 +00:00
|
|
|
import os
|
2022-07-11 14:11:26 +00:00
|
|
|
from dataclasses import dataclass
|
2022-08-01 07:41:04 +00:00
|
|
|
from typing import Optional
|
2022-07-11 14:11:26 +00:00
|
|
|
|
|
|
|
import requests
|
2022-08-01 07:41:04 +00:00
|
|
|
import yaml
|
2022-07-11 14:11:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2022-08-01 07:41:04 +00:00
|
|
|
class SberCloudConfig:
|
|
|
|
login: Optional[str] = None
|
|
|
|
password: Optional[str] = None
|
|
|
|
domain: Optional[str] = None
|
|
|
|
project_id: Optional[str] = None
|
|
|
|
iam_url: Optional[str] = None
|
2022-07-11 14:11:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-01 07:41:04 +00:00
|
|
|
def from_dict(config_dict: dict) -> 'SberCloudConfig':
|
|
|
|
return SberCloudConfig(**config_dict)
|
2022-07-11 14:11:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-08-01 07:41:04 +00:00
|
|
|
def from_yaml(config_path: str) -> 'SberCloudConfig':
|
|
|
|
with open(config_path) as file:
|
|
|
|
config_dict = yaml.load(file, Loader=yaml.FullLoader)
|
|
|
|
return SberCloudConfig.from_dict(config_dict["sbercloud"])
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_env() -> 'SberCloudConfig':
|
|
|
|
config_dict = {
|
|
|
|
"domain": os.getenv("SBERCLOUD_DOMAIN"),
|
|
|
|
"login": os.getenv("SBERCLOUD_LOGIN"),
|
|
|
|
"password": os.getenv("SBERCLOUD_PASSWORD"),
|
|
|
|
"project_id": os.getenv("SBERCLOUD_PROJECT_ID"),
|
|
|
|
"iam_url": os.getenv("SBERCLOUD_IAM_URL"),
|
|
|
|
}
|
|
|
|
return SberCloudConfig.from_dict(config_dict)
|
2022-07-11 14:11:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SberCloud:
|
2022-08-01 07:41:04 +00:00
|
|
|
def __init__(self, config: SberCloudConfig) -> None:
|
|
|
|
self.config = config
|
2022-07-11 14:11:26 +00:00
|
|
|
self.ecs_url = None
|
|
|
|
self.project_id = None
|
|
|
|
self.token = None
|
2022-08-01 07:41:04 +00:00
|
|
|
self._initialize()
|
|
|
|
self.ecs_nodes = self.get_ecs_nodes()
|
2022-07-11 14:11:26 +00:00
|
|
|
|
2022-08-01 07:41:04 +00:00
|
|
|
def _initialize(self) -> None:
|
2022-07-11 14:11:26 +00:00
|
|
|
data = {
|
|
|
|
'auth': {
|
|
|
|
'identity': {
|
|
|
|
'methods': ['password'],
|
|
|
|
'password': {
|
|
|
|
'user': {
|
|
|
|
'domain': {
|
2022-08-01 07:41:04 +00:00
|
|
|
'name': self.config.domain
|
2022-07-11 14:11:26 +00:00
|
|
|
},
|
2022-08-01 07:41:04 +00:00
|
|
|
'name': self.config.login,
|
|
|
|
'password': self.config.password
|
2022-07-11 14:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'scope': {
|
|
|
|
'project': {
|
2022-08-01 07:41:04 +00:00
|
|
|
'id': self.config.project_id
|
2022-07-11 14:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 07:41:04 +00:00
|
|
|
response = requests.post(
|
|
|
|
f'{self.config.iam_url}/v3/auth/tokens',
|
|
|
|
data=json.dumps(data),
|
|
|
|
headers={'Content-Type': 'application/json'}
|
|
|
|
)
|
|
|
|
self.ecs_url = [
|
|
|
|
catalog['endpoints'][0]['url']
|
|
|
|
for catalog in response.json()['token']['catalog'] if catalog['type'] == 'ecs'
|
|
|
|
][0]
|
2022-07-11 14:11:26 +00:00
|
|
|
self.project_id = self.ecs_url.split('/')[-1]
|
|
|
|
self.token = response.headers['X-Subject-Token']
|
|
|
|
|
2022-08-01 07:41:04 +00:00
|
|
|
def find_ecs_node_by_ip(self, ip: str, no_cache: bool = False) -> str:
|
|
|
|
if not self.ecs_nodes or no_cache:
|
|
|
|
self.ecs_nodes = self.get_ecs_nodes()
|
|
|
|
nodes_by_ip = [
|
|
|
|
node for node in self.ecs_nodes
|
|
|
|
if ip in [
|
|
|
|
node_ip['addr']
|
|
|
|
for node_ips in node['addresses'].values()
|
|
|
|
for node_ip in node_ips
|
|
|
|
]
|
|
|
|
]
|
|
|
|
assert len(nodes_by_ip) == 1
|
|
|
|
return nodes_by_ip[0]['id']
|
2022-07-11 14:11:26 +00:00
|
|
|
|
2022-08-01 07:41:04 +00:00
|
|
|
def get_ecs_nodes(self) -> list[dict]:
|
2022-07-11 14:11:26 +00:00
|
|
|
response = requests.get(f'{self.ecs_url}/cloudservers/detail',
|
|
|
|
headers={'X-Auth-Token': self.token}).json()
|
|
|
|
return response['servers']
|
|
|
|
|
2022-08-01 07:41:04 +00:00
|
|
|
def start_node(self, node_id: Optional[str] = None, node_ip: Optional[str] = None) -> None:
|
2022-07-11 14:11:26 +00:00
|
|
|
data = {
|
|
|
|
'os-start': {
|
|
|
|
'servers': [
|
|
|
|
{
|
2022-08-01 07:41:04 +00:00
|
|
|
'id': node_id or self.find_ecs_node_by_ip(node_ip)
|
2022-07-11 14:11:26 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 07:41:04 +00:00
|
|
|
response = requests.post(
|
|
|
|
f'{self.ecs_url}/cloudservers/action',
|
|
|
|
data=json.dumps(data),
|
|
|
|
headers={'Content-Type': 'application/json', 'X-Auth-Token': self.token}
|
|
|
|
)
|
|
|
|
assert response.status_code < 300, \
|
|
|
|
f'Status:{response.status_code}. Server not started: {response.json()}'
|
2022-07-11 14:11:26 +00:00
|
|
|
|
2022-08-01 07:41:04 +00:00
|
|
|
def stop_node(self, node_id: Optional[str] = None, node_ip: Optional[str] = None,
|
|
|
|
hard: bool = False) -> None:
|
2022-07-11 14:11:26 +00:00
|
|
|
data = {
|
|
|
|
'os-stop': {
|
|
|
|
'type': 'HARD' if hard else 'SOFT',
|
|
|
|
'servers': [
|
|
|
|
{
|
2022-08-01 07:41:04 +00:00
|
|
|
'id': node_id or self.find_ecs_node_by_ip(node_ip)
|
2022-07-11 14:11:26 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2022-08-01 07:41:04 +00:00
|
|
|
response = requests.post(
|
|
|
|
f'{self.ecs_url}/cloudservers/action',
|
|
|
|
data=json.dumps(data),
|
|
|
|
headers={'Content-Type': 'application/json', 'X-Auth-Token': self.token}
|
|
|
|
)
|
|
|
|
assert response.status_code < 300, \
|
|
|
|
f'Status:{response.status_code}. Server not stopped: {response.json()}'
|