66 API
This commit is contained in:
parent
707ed651fb
commit
0964f0acff
1 changed files with 185 additions and 2 deletions
|
@ -130,7 +130,7 @@ def container_create():
|
|||
ssh_key='',
|
||||
)
|
||||
|
||||
# sshkey
|
||||
# SSH key
|
||||
if user_ssh.check():
|
||||
new_container['ssh_key'] = user_ssh.get()
|
||||
|
||||
|
@ -240,7 +240,7 @@ def container_info(container_id):
|
|||
container_details = containers.get_item(container_id)
|
||||
# print ControllerContainersStatisticsState().get(container_id)
|
||||
|
||||
statistics = []
|
||||
statistics = list()
|
||||
for s in ControllerContainersStatistics(container_id).size_get(1):
|
||||
created = s.created
|
||||
statistics.append({
|
||||
|
@ -311,3 +311,186 @@ def vms_list():
|
|||
:return:
|
||||
"""
|
||||
return jsonify(vms=ControllerVMS(g.user_id).get_items())
|
||||
|
||||
|
||||
@api.route('/vms', methods=['POST'])
|
||||
@auth.login_required
|
||||
def vms_create():
|
||||
"""
|
||||
curl -XPOST /api/v1/vms/ -u <email>:<secret> -d 'plan=<uuid>&datacenter=<uuid>'
|
||||
:return:
|
||||
"""
|
||||
user_balance = ControllerBilling().get(g.user_id)
|
||||
user_ssh = ControllerSSHKey(g.user_id)
|
||||
controller_plans = ControllerPlans()
|
||||
|
||||
# check user money
|
||||
if user_balance <= 0:
|
||||
return jsonify(message='no money')
|
||||
|
||||
new_vm = dict()
|
||||
new_vm['vm_id'] = str(uuid4())
|
||||
|
||||
# check exists plan
|
||||
if not controller_plans.exists(request.form.get('plan')):
|
||||
return jsonify(message='plan not exists')
|
||||
|
||||
# load plan details
|
||||
plan_details = controller_plans.plan_get(request.form.get('plan'))
|
||||
|
||||
new_vm['plan'] = request.form.get('plan')
|
||||
|
||||
# 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
|
||||
new_vm['password'] = ControllerCommon().generate_password(size=14)
|
||||
new_vm['hostname'] = ControllerCommon().generate_password(size=7)
|
||||
|
||||
new_vm['datacenter_id'] = str(select_ip.datacenter.id)
|
||||
new_vm['server_id'] = str(select_ip.server.id)
|
||||
|
||||
new_vm['platform'] = 'x86_64'
|
||||
new_vm['ipv4'] = select_ip.ipv4
|
||||
new_vm['ipv6'] = select_ip.ipv6
|
||||
|
||||
new_vm['ipv4_gateway'] = select_ip.ipv4_gateway
|
||||
new_vm['ipv6_gateway'] = select_ip.ipv6_gateway
|
||||
|
||||
# TODO: remove hardcore
|
||||
new_vm['dns1'] = '8.8.8.8'
|
||||
new_vm['dns2'] = '8.8.4.4'
|
||||
|
||||
new_vm['cores'] = plan_details.cores
|
||||
new_vm['storage'] = plan_details.storage
|
||||
new_vm['swap'] = plan_details.swap
|
||||
new_vm['memory'] = plan_details.memory
|
||||
|
||||
new_vm['os_name'] = 'ubuntu'
|
||||
new_vm['os_suite'] = 'trusty'
|
||||
|
||||
# sshkey
|
||||
new_vm['ssh_key'] = None
|
||||
if user_ssh.check():
|
||||
new_vm['ssh_key'] = user_ssh.get()
|
||||
|
||||
# create container record in database
|
||||
# status 4: creation
|
||||
status = 4
|
||||
container_create = ControllerVMS(g.user_id).create(
|
||||
vm_id=new_vm['vm_id'],
|
||||
datacenter_id=new_vm['datacenter_id'],
|
||||
server_id=new_vm['server_id'],
|
||||
hostname=new_vm['hostname'],
|
||||
ipv4=new_vm['ipv4'],
|
||||
ipv6=new_vm['ipv6'],
|
||||
plan=new_vm['plan'],
|
||||
platform=new_vm['platform'],
|
||||
os_name=new_vm['os_name'],
|
||||
os_suite=new_vm['os_suite'],
|
||||
status=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_vm['datacenter_id'],
|
||||
new_vm['server_id'],
|
||||
'vm_create',
|
||||
0,
|
||||
vm_id=new_vm['vm_id'],
|
||||
ipv4=new_vm['ipv4'],
|
||||
ipv4_gateway=new_vm['ipv4_gateway'],
|
||||
ipv6=new_vm['ipv6'],
|
||||
ipv6_gateway=new_vm['ipv6_gateway'],
|
||||
password=new_vm['password'],
|
||||
hostname=new_vm['hostname'],
|
||||
platform=new_vm['platform'],
|
||||
# TODO: remove hardcore
|
||||
dns1=new_vm['dns1'],
|
||||
dns2=new_vm['dns2'],
|
||||
|
||||
cores=new_vm['cores'],
|
||||
storage=new_vm['storage'],
|
||||
swap=new_vm['swap'],
|
||||
memory=new_vm['memory'],
|
||||
|
||||
os_name=new_vm['os_name'],
|
||||
os_suite=new_vm['os_suite'],
|
||||
|
||||
ssh_key=new_vm['ssh_key'],
|
||||
)
|
||||
|
||||
# send mail message with recovery code
|
||||
message_parts = []
|
||||
|
||||
if new_vm['ipv4']:
|
||||
message_parts.append(u"IPv4: %s" % new_vm['ipv4'])
|
||||
if new_vm['ipv6']:
|
||||
message_parts.append(u"IPv6: %s" % new_vm['ipv6'])
|
||||
message_parts.append(u"Пользователь: %s" % 'root')
|
||||
# message_parts.append(u"Пользователь: %s" % new_vm['username'])
|
||||
message_parts.append(u"Пароль: %s" % new_vm['password'])
|
||||
if new_vm['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(status=0, message='ok',
|
||||
user='root', password=new_vm['password'], ipv4=new_vm['ipv4'],
|
||||
public_ssh_key='added' if new_vm['ssh_key'] else 'empty')
|
||||
else:
|
||||
# mark ip as free
|
||||
ControllerIps().setfree(select_ip.id)
|
||||
return jsonify(status=1, message='fail')
|
||||
|
||||
|
||||
@api.route('/vms/<uuid:vm_id>', methods=['POST'])
|
||||
@auth.login_required
|
||||
def vm_actions(vm_id):
|
||||
# TODO:
|
||||
pass
|
||||
|
||||
|
||||
@api.route('/vms/<uuid:vm_id>', methods=['DELETE'])
|
||||
@auth.login_required
|
||||
def vm_delete(vm_id):
|
||||
#
|
||||
vms = ControllerVMS(g.user_id)
|
||||
tasks = ControllerTasks(g.user_id)
|
||||
# check the user have a selected rule
|
||||
# if user not have a container then redirect to the container list
|
||||
if not vms.exists(vm_id):
|
||||
return jsonify(message='not exists')
|
||||
|
||||
# get container details
|
||||
vm_details = vms.get(vm_id)
|
||||
|
||||
# Обновляем статус "5" для правила
|
||||
vms.set_status(vm_id, 5)
|
||||
# Создание задания
|
||||
tasks.create(
|
||||
datacenter_id=vm_details.datacenter.id,
|
||||
server_id=vm_details.server.id,
|
||||
task='vm_delete',
|
||||
status=0,
|
||||
vm_id=vm_id,
|
||||
# hostname=vm_details.hostname
|
||||
)
|
||||
|
||||
return jsonify(status=0, message='ok')
|
||||
|
|
Loading…
Add table
Reference in a new issue