238 lines
8.7 KiB
Python
238 lines
8.7 KiB
Python
# coding: utf-8
|
||
|
||
from uuid import uuid4
|
||
from flask import Blueprint, jsonify, request, g
|
||
from SWSCloudAPI.API import auth
|
||
from SWSCloudCore.controllers.datacenters import ControllerDataCenters
|
||
from SWSCloudCore.controllers.ips import ControllerIps
|
||
from SWSCloudCore.controllers.users import ControllerUsers
|
||
from SWSCloudCore.controllers.users import ControllerSSHKey
|
||
from SWSCloudCore.controllers.billing import ControllerBilling
|
||
from SWSCloudCore.controllers.common import ControllerCommon, ControllerMessagesEmail
|
||
from SWSCloudCore.controllers.tasks import ControllerTasks
|
||
from SWSCloudCore.controllers.containers import (
|
||
ControllerContainers, ControllerContainersStatistics, ControllerContainersStatisticsState)
|
||
|
||
api_v1_containers = Blueprint('containers', __name__, url_prefix='/api/v1/containers')
|
||
|
||
|
||
@api_v1_containers.route('/', methods=['GET'])
|
||
@auth.login_required
|
||
def containers_list():
|
||
"""
|
||
curl -X GET http://localhost:5000/api/v1/containers/ -u <email>:<secret>
|
||
:return:
|
||
"""
|
||
# get containers list
|
||
containers = ControllerContainers(g.user_id).get_items()
|
||
#
|
||
return jsonify(total=containers['total'], items=containers['items'])
|
||
|
||
|
||
@api_v1_containers.route('/', methods=['POST'])
|
||
@auth.login_required
|
||
def container_create():
|
||
"""
|
||
curl -X POST http://localhost:5000/api/v1/containers/ -u <email>:<secret> -d "datacenter_id=123"
|
||
:return:
|
||
"""
|
||
# Check exists data center
|
||
if not ControllerDataCenters().exists(request.form.get('datacenter')):
|
||
return jsonify(message='datacenter not exists')
|
||
# Check money
|
||
if ControllerBilling().get(g.user_id) <= 0:
|
||
return jsonify(message='no money')
|
||
|
||
# select server from selected region with available ip-addresses
|
||
# select IP
|
||
select_ip = ControllerIps().getfree(request.form.get('datacenter'))
|
||
# mark ip as busy (taken)
|
||
ControllerIps().setbusy(select_ip.id)
|
||
# generate password for container user
|
||
password = ControllerCommon().generate_password(size=14)
|
||
|
||
user_ssh = ControllerSSHKey(g.user_id)
|
||
|
||
new_container = dict(
|
||
container_id=str(uuid4()),
|
||
datacenter_id=str(select_ip.datacenter.id),
|
||
server_id=str(select_ip.server.id),
|
||
ipv4=select_ip.ipv4,
|
||
ipv6=select_ip.ipv6,
|
||
ipv4_gateway=select_ip.ipv4_gateway,
|
||
ipv6_gateway=select_ip.ipv6_gateway,
|
||
username='ubuntu',
|
||
password=password,
|
||
ssh_key='',
|
||
)
|
||
|
||
# SSH key
|
||
if user_ssh.check():
|
||
new_container['ssh_key'] = user_ssh.get()
|
||
|
||
# create container record in database
|
||
# status 4: creation
|
||
status = 4
|
||
container_create = ControllerContainers(g.user_id).create(
|
||
new_container['container_id'],
|
||
new_container['datacenter_id'],
|
||
new_container['server_id'],
|
||
new_container['ipv4'],
|
||
new_container['ipv6'],
|
||
status
|
||
)
|
||
# create default state data
|
||
ControllerContainersStatisticsState().set(new_container['container_id'], dict())
|
||
if container_create:
|
||
# create task for create new container
|
||
ControllerTasks(g.user_id).create(
|
||
new_container['datacenter_id'],
|
||
new_container['server_id'],
|
||
'container_create',
|
||
0,
|
||
container_id=new_container['container_id'],
|
||
ipv4=new_container['ipv4'],
|
||
ipv6=new_container['ipv6'],
|
||
ipv4_gateway=new_container['ipv4_gateway'],
|
||
ipv6_gateway=new_container['ipv6_gateway'],
|
||
username=new_container['username'],
|
||
password=new_container['password'],
|
||
ssh_key=new_container['ssh_key']
|
||
)
|
||
|
||
# send mail message with recovery code
|
||
message_parts = []
|
||
|
||
if new_container['ipv4']:
|
||
message_parts.append(u"IPv4: %s" % new_container['ipv4'])
|
||
if new_container['ipv6']:
|
||
message_parts.append(u"IPv6: %s" % new_container['ipv6'])
|
||
message_parts.append(u"Пользователь: %s" % new_container['username'])
|
||
message_parts.append(u"Пароль: %s" % new_container['password'])
|
||
if new_container['ssh_key']:
|
||
message_parts.append(u"SSH ключ: добавлен")
|
||
|
||
message = '<br/>\n'.join(message_parts)
|
||
subject = u'GoCloud.ru: Новый контейнер'
|
||
lead = u"""Поздравляем с новым контейнером."""
|
||
callout = u"""
|
||
Для входа в личный кабинет воспользуйтесь
|
||
<a href="https://gocloud.ru/account/login">страницей авторизации</a>.
|
||
"""
|
||
|
||
user_data = ControllerUsers(g.user_id).get()
|
||
|
||
email = ControllerMessagesEmail()
|
||
email.send(title=subject, to=user_data.email, lead=lead, message=message, callout=callout)
|
||
|
||
#
|
||
return jsonify(message='ok')
|
||
|
||
|
||
@api_v1_containers.route('/container/<uuid:container_id>', methods=['DELETE'])
|
||
@auth.login_required
|
||
def container_delete(container_id):
|
||
"""
|
||
curl -X DELETE http://gocloud.ru/api/v1/container/<uuid:container_id> -u <email>:<secret> -d "container_id=<uuid:container_id>"
|
||
:return:
|
||
"""
|
||
containers = ControllerContainers(g.user_id)
|
||
# check the user have a selected rule
|
||
# if user not have a container then redirect to the container list
|
||
if not containers.check_exists_item(container_id):
|
||
return jsonify(message='container_not_exists')
|
||
|
||
# get container details
|
||
container_details = ControllerContainers(g.user_id).get_item(container_id)
|
||
|
||
# Обновляем статус "5" для правила
|
||
containers.set_status(container_id, 5)
|
||
# Создание задания
|
||
ControllerTasks(g.user_id).create(
|
||
container_details.datacenter.id,
|
||
container_details.server.id,
|
||
'container_delete',
|
||
0,
|
||
container_id=container_id
|
||
)
|
||
# TODO: send email container was deleted about
|
||
return jsonify(status=0, message='container has been deleted')
|
||
|
||
|
||
@api_v1_containers.route('/<uuid:container_id>', methods=['GET'])
|
||
@auth.login_required
|
||
def container_info(container_id):
|
||
"""
|
||
curl -X GET http://localhost:5000/api/v1/container/<uuid:container_id> -u <email>:<secret>
|
||
:return:
|
||
"""
|
||
# init
|
||
containers = ControllerContainers(g.user_id)
|
||
# check the user have a selected rule
|
||
# if user not have a container then redirect to the container list
|
||
if not containers.check_exists_item(container_id):
|
||
return jsonify(message='container_not_found')
|
||
# get container details
|
||
container_details = containers.get_item(container_id)
|
||
# print ControllerContainersStatisticsState().get(container_id)
|
||
|
||
statistics = list()
|
||
for s in ControllerContainersStatistics(container_id).size_get(1):
|
||
created = s.created
|
||
statistics.append({
|
||
'year': created.strftime('%Y'),
|
||
'month': created.strftime('%m'),
|
||
'day': created.strftime('%d'),
|
||
'hour': created.strftime('%H'),
|
||
'minute': created.strftime('%M'),
|
||
'data': s.memory
|
||
})
|
||
# return
|
||
return jsonify(container=container_details, stats_memory=statistics)
|
||
|
||
|
||
@api_v1_containers.route('/<uuid:container_id>', methods=['POST'])
|
||
@auth.login_required
|
||
def container_actions(container_id):
|
||
"""
|
||
curl -X POST http://localhost:5000/api/v1/container/<uuid:container_id> -u <email>:<secret> -d "status=inactive"
|
||
curl -X POST http://localhost:5000/api/v1/container/<uuid:container_id> -u <email>:<secret> -d "action=active"
|
||
:return:
|
||
"""
|
||
containers = ControllerContainers(g.user_id)
|
||
# check the user have a selected rule
|
||
# if user not have a container then redirect to the container list
|
||
if not containers.check_exists_item(container_id):
|
||
return jsonify(message='container_not_found')
|
||
|
||
# get container details
|
||
container_details = containers.get_item(container_id)
|
||
|
||
if request.form.get('status') == "inactive":
|
||
containers.set_status(container_id, 3)
|
||
# Создание задания
|
||
ControllerTasks(g.user_id).create(
|
||
container_details.datacenter.id,
|
||
container_details.server.id,
|
||
'container_stop',
|
||
0,
|
||
container_id=container_id
|
||
)
|
||
elif request.form.get('status') == 'active':
|
||
balance = ControllerBilling().get(g.user_id)
|
||
|
||
if balance <= 0:
|
||
return jsonify(message='no money')
|
||
|
||
containers.set_status(container_id, 2)
|
||
# Создание задания
|
||
ControllerTasks(g.user_id).create(
|
||
container_details.datacenter.id,
|
||
container_details.server.id,
|
||
'container_start',
|
||
0,
|
||
container_id=container_details.id
|
||
)
|
||
return jsonify(message='ok')
|
||
else:
|
||
return jsonify(status=1, message='require action')
|