2015-12-01 02:43:10 +03:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
import uuid
|
2016-04-03 19:10:18 +03:00
|
|
|
|
from flask import Blueprint, g, redirect, render_template, request, session
|
|
|
|
|
from flask import url_for
|
2016-04-02 00:42:22 +03:00
|
|
|
|
|
|
|
|
|
from SWSCloudCore.controllers.billing import ControllerBilling
|
|
|
|
|
from SWSCloudCore.controllers.common import ControllerCommon
|
|
|
|
|
from SWSCloudCore.controllers.common import ControllerMessagesEmail
|
|
|
|
|
from SWSCloudCore.controllers.containers import ControllerContainers
|
|
|
|
|
from SWSCloudCore.controllers.containers import ControllerContainersStatistics
|
|
|
|
|
from SWSCloudCore.controllers.containers import ControllerContainersStatisticsState
|
|
|
|
|
from SWSCloudCore.controllers.datacenters import ControllerDataCenters
|
|
|
|
|
from SWSCloudCore.controllers.ips import ControllerIps
|
|
|
|
|
from SWSCloudCore.controllers.tasks import ControllerTasks
|
|
|
|
|
from SWSCloudCore.controllers.users import ControllerSSHKey
|
|
|
|
|
from SWSCloudCore.controllers.users import ControllerUsers
|
|
|
|
|
from SWSCloudCore import models
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
viewContainers = Blueprint('containers', __name__, url_prefix='/containers')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@viewContainers.route('/')
|
|
|
|
|
def index():
|
|
|
|
|
# check session
|
|
|
|
|
if not ControllerUsers().check_session():
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
# auth user
|
|
|
|
|
if not ControllerUsers().auth(session['email'], session['password']):
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
return render_template(
|
|
|
|
|
'default/containers/index.html',
|
2016-01-18 18:09:56 +03:00
|
|
|
|
# get containers list
|
|
|
|
|
containers=ControllerContainers(session['user_id']).get_items()
|
2015-12-01 02:43:10 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@viewContainers.route('/create', methods=['GET', 'POST'])
|
|
|
|
|
def create():
|
|
|
|
|
# check session
|
|
|
|
|
if not ControllerUsers().check_session():
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
# auth user
|
|
|
|
|
if not ControllerUsers().auth(session['email'], session['password']):
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
|
2015-12-12 17:39:48 +03:00
|
|
|
|
user_balance = ControllerBilling().get(session['user_id'])
|
2015-12-01 02:43:10 +03:00
|
|
|
|
user_ssh = ControllerSSHKey(session['user_id'])
|
|
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
|
# check user money
|
|
|
|
|
if user_balance <= 0:
|
2016-04-03 19:10:18 +03:00
|
|
|
|
flash(u'Недостаточно средств на аккаунте')
|
|
|
|
|
return redirect(url_for('containers.create'))
|
|
|
|
|
|
|
|
|
|
# select server from selected region with available ip-addresses
|
|
|
|
|
# select IP
|
|
|
|
|
select_ip = ControllerIps().getfree(request.form['datacenter'])
|
|
|
|
|
# mark ip as busy (taken)
|
|
|
|
|
ControllerIps().setbusy(select_ip.id)
|
|
|
|
|
# generate password for container user
|
|
|
|
|
password = ControllerCommon().generate_password(size=14)
|
|
|
|
|
|
|
|
|
|
new_container = {
|
|
|
|
|
'container_id': str(uuid.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': None,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# sshkey
|
|
|
|
|
if user_ssh.check():
|
|
|
|
|
new_container['ssh_key'] = user_ssh.get()
|
|
|
|
|
|
|
|
|
|
# create container record in database
|
|
|
|
|
# status 4: creation
|
|
|
|
|
status = 4
|
|
|
|
|
container_create = ControllerContainers(session['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(session['user_id']).create(
|
2015-12-01 02:43:10 +03:00
|
|
|
|
new_container['datacenter_id'],
|
|
|
|
|
new_container['server_id'],
|
2016-04-03 19:10:18 +03:00
|
|
|
|
'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']
|
2015-12-01 02:43:10 +03:00
|
|
|
|
)
|
|
|
|
|
|
2016-04-03 19:10:18 +03:00
|
|
|
|
# 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(session['user_id']).get()
|
|
|
|
|
|
|
|
|
|
email = ControllerMessagesEmail()
|
|
|
|
|
email.send(title=subject, to=user_data.email, lead=lead, message=message, callout=callout)
|
|
|
|
|
|
|
|
|
|
return redirect(url_for('containers.index'))
|
|
|
|
|
else:
|
|
|
|
|
# mark ip as free
|
|
|
|
|
ControllerIps().setfree(select_ip.id)
|
2015-12-01 02:43:10 +03:00
|
|
|
|
# Get datacenters list
|
|
|
|
|
datacenters = ControllerDataCenters().get()
|
2016-01-18 11:32:26 +03:00
|
|
|
|
#
|
2015-12-01 02:43:10 +03:00
|
|
|
|
return render_template(
|
|
|
|
|
'default/containers/create.html',
|
|
|
|
|
datacenters=datacenters
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2016-04-03 19:10:18 +03:00
|
|
|
|
@viewContainers.route('/delete/<uuid:container_id>.html', methods=['GET', 'POST'])
|
2015-12-01 02:43:10 +03:00
|
|
|
|
def delete(container_id):
|
|
|
|
|
# check session
|
|
|
|
|
if not ControllerUsers().check_session():
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
# auth user
|
|
|
|
|
if not ControllerUsers().auth(session['email'], session['password']):
|
|
|
|
|
return redirect(url_for("account.logout"))
|
2015-12-05 15:31:26 +03:00
|
|
|
|
#
|
|
|
|
|
containers = ControllerContainers(session['user_id'])
|
2015-12-01 02:43:10 +03:00
|
|
|
|
# check the user have a selected rule
|
|
|
|
|
# if user not have a container then redirect to the container list
|
2015-12-05 15:31:26 +03:00
|
|
|
|
if not containers.check_exists_item(container_id):
|
|
|
|
|
return redirect(url_for('containers.index'))
|
|
|
|
|
# get container details
|
|
|
|
|
container_details = ControllerContainers(session['user_id']).get_item(container_id)
|
|
|
|
|
# POST
|
2015-12-01 02:43:10 +03:00
|
|
|
|
if request.method == "POST":
|
|
|
|
|
# Обновляем статус "5" для правила
|
2015-12-05 15:31:26 +03:00
|
|
|
|
containers.set_status(container_id, 5)
|
|
|
|
|
# Создание задания
|
|
|
|
|
ControllerTasks(session['user_id']).create(
|
|
|
|
|
container_details.datacenter.id,
|
|
|
|
|
container_details.server.id,
|
|
|
|
|
'container_delete',
|
|
|
|
|
0,
|
2015-12-05 15:50:44 +03:00
|
|
|
|
container_id=container_id
|
2015-12-05 15:31:26 +03:00
|
|
|
|
)
|
|
|
|
|
# TODO: send email container was deleted about
|
2015-12-01 02:43:10 +03:00
|
|
|
|
# Редиректим на страницу со всеми правилами
|
2016-04-03 19:10:18 +03:00
|
|
|
|
flash(u'Контейнер был удалён')
|
2015-12-05 15:31:26 +03:00
|
|
|
|
return redirect(url_for('containers.index'))
|
2016-01-20 12:15:49 +03:00
|
|
|
|
return render_template(
|
|
|
|
|
'default/containers/delete.html',
|
|
|
|
|
container=container_details
|
|
|
|
|
)
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@viewContainers.route('/settings/<uuid:container_id>', methods=['GET', 'POST'])
|
|
|
|
|
def settings(container_id):
|
|
|
|
|
# check session
|
|
|
|
|
if not ControllerUsers().check_session():
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
# auth user
|
|
|
|
|
if not ControllerUsers().auth(session['email'], session['password']):
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
|
|
|
|
|
containers = ControllerContainers(session['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 redirect(url_for('containers.index'))
|
|
|
|
|
|
2015-12-05 20:04:02 +03:00
|
|
|
|
# get container details
|
|
|
|
|
container_details = containers.get_item(container_id)
|
|
|
|
|
|
2015-12-01 02:43:10 +03:00
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
if request.form['action'] == 'set_status':
|
|
|
|
|
if request.form['status'] == 'inactive':
|
2015-12-05 20:12:07 +03:00
|
|
|
|
containers.set_status(container_id, 3)
|
2015-12-05 20:04:02 +03:00
|
|
|
|
# Создание задания
|
|
|
|
|
ControllerTasks(session['user_id']).create(
|
|
|
|
|
container_details.datacenter.id,
|
|
|
|
|
container_details.server.id,
|
|
|
|
|
'container_stop',
|
2015-12-05 20:12:07 +03:00
|
|
|
|
0,
|
2015-12-05 20:04:02 +03:00
|
|
|
|
container_id=container_id
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for('containers.settings', container_id=container_id))
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
if request.form['status'] == 'active':
|
2016-04-03 19:10:18 +03:00
|
|
|
|
balance = ControllerBilling().get(session['user_id'])
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
if balance <= 0:
|
2016-04-03 19:10:18 +03:00
|
|
|
|
flash(u'Недостаточно средств на балансе.')
|
2015-12-05 20:04:02 +03:00
|
|
|
|
return redirect(url_for('containers.settings', container_id=container_id))
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2016-04-03 19:10:18 +03:00
|
|
|
|
containers.set_status(container_id, 2)
|
|
|
|
|
# Создание задания
|
|
|
|
|
ControllerTasks(session['user_id']).create(
|
|
|
|
|
container_details.datacenter.id,
|
|
|
|
|
container_details.server.id,
|
|
|
|
|
'container_start',
|
|
|
|
|
0,
|
|
|
|
|
container_id=container_details.id
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for('containers.settings', container_id=container_id))
|
|
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
|
'default/containers/settings.html',
|
|
|
|
|
container=container_details
|
|
|
|
|
)
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
|
2016-04-03 19:10:18 +03:00
|
|
|
|
@viewContainers.route('/stats/<uuid:container_id>.html')
|
2015-12-01 02:43:10 +03:00
|
|
|
|
def stats(container_id):
|
|
|
|
|
# check session
|
|
|
|
|
if not ControllerUsers().check_session():
|
|
|
|
|
return redirect(url_for("account.logout"))
|
|
|
|
|
# auth user
|
|
|
|
|
if not ControllerUsers().auth(session['email'], session['password']):
|
|
|
|
|
return redirect(url_for("account.logout"))
|
2016-01-20 12:15:49 +03:00
|
|
|
|
# init
|
|
|
|
|
containers = ControllerContainers(session['user_id'])
|
2015-12-01 02:43:10 +03:00
|
|
|
|
# check the user have a selected rule
|
|
|
|
|
# if user not have a container then redirect to the container list
|
2016-01-20 12:15:49 +03:00
|
|
|
|
if not containers.check_exists_item(container_id):
|
|
|
|
|
return redirect(url_for('containers.index'))
|
|
|
|
|
# get container details
|
|
|
|
|
container_details = containers.get_item(container_id)
|
2016-01-21 02:52:54 +03:00
|
|
|
|
# print ControllerContainersStatisticsState().get(container_id)
|
|
|
|
|
|
|
|
|
|
statistics = []
|
|
|
|
|
for s in ControllerContainersStatistics(container_id).size_get(1):
|
|
|
|
|
# print time.strftime(s.created, '%Y')
|
|
|
|
|
# print datetime.datetime.strptime(s.created, '%Y')
|
|
|
|
|
# print time.strftime("%B %d %Y", s.created)
|
|
|
|
|
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
|
2015-12-01 02:43:10 +03:00
|
|
|
|
})
|
2016-01-20 12:15:49 +03:00
|
|
|
|
# return
|
|
|
|
|
return render_template(
|
|
|
|
|
'default/containers/stats.html',
|
2016-01-21 02:52:54 +03:00
|
|
|
|
container=container_details,
|
|
|
|
|
stats_memory=statistics
|
2016-01-20 12:15:49 +03:00
|
|
|
|
)
|