|
|
@@ -6,14 +6,10 @@ import json
|
|
|
import random
|
|
|
from flask import Blueprint, render_template, url_for, request
|
|
|
import requests
|
|
|
-from math import ceil
|
|
|
-import re
|
|
|
-import socket
|
|
|
import time
|
|
|
|
|
|
-from werkzeug.datastructures import ImmutableMultiDict
|
|
|
-
|
|
|
from models import Database as db
|
|
|
+from models import Utils
|
|
|
from models.initialize import config
|
|
|
|
|
|
|
|
|
@@ -58,89 +54,6 @@ def show():
|
|
|
pages=ret['data']['pages'], last_page=ret['data']['last_page'])
|
|
|
|
|
|
|
|
|
-def create():
|
|
|
- host_url = request.host_url.rstrip('/')
|
|
|
-
|
|
|
- if request.method == 'POST':
|
|
|
- ability = request.form.get('ability')
|
|
|
- bandwidth = request.form.get('bandwidth', 0)
|
|
|
- bandwidth_unit = request.form.get('bandwidth_unit', 'm')
|
|
|
- os_template_image_id = request.form.get('os_template_image_id')
|
|
|
- quantity = request.form.get('quantity')
|
|
|
- password = request.form.get('password')
|
|
|
- remark = request.form.get('remark')
|
|
|
- allocation_by_random = 'allocation_by_random' in request.form
|
|
|
- node_id = request.form.get('node_id')
|
|
|
- ssh_keys_id = request.form.getlist('ssh_keys_id')
|
|
|
-
|
|
|
- if not isinstance(ability, basestring):
|
|
|
- pass
|
|
|
-
|
|
|
- m = re.search('^(\d+)c(\d+)g$', ability.lower())
|
|
|
- if m is None:
|
|
|
- pass
|
|
|
-
|
|
|
- cpu = m.groups()[0]
|
|
|
- memory = m.groups()[1]
|
|
|
-
|
|
|
- payload = {
|
|
|
- "cpu": int(cpu),
|
|
|
- "memory": int(memory),
|
|
|
- "bandwidth": int(bandwidth),
|
|
|
- "bandwidth_unit": bandwidth_unit,
|
|
|
- "os_template_image_id": int(os_template_image_id),
|
|
|
- "quantity": int(quantity),
|
|
|
- "remark": remark,
|
|
|
- "password": password,
|
|
|
- "ssh_keys_id": ssh_keys_id,
|
|
|
- "lease_term": 100
|
|
|
- }
|
|
|
-
|
|
|
- if not allocation_by_random:
|
|
|
- payload['node_id'] = node_id
|
|
|
-
|
|
|
- url = host_url + '/api/guest'
|
|
|
- headers = {'content-type': 'application/json'}
|
|
|
- r = requests.post(url, data=json.dumps(payload), headers=headers, cookies=request.cookies)
|
|
|
- j_r = json.loads(r.content)
|
|
|
-
|
|
|
- if j_r['state']['code'] != '200':
|
|
|
- return render_template('failure.html',
|
|
|
- go_back_url='/guests',
|
|
|
- timeout=10000, title='创建失败',
|
|
|
- message_title='创建实例失败',
|
|
|
- message=j_r['state']['sub']['zh-cn'])
|
|
|
-
|
|
|
- guests_url = host_url + url_for('api_guests.r_get_by_filter')
|
|
|
- guests_ret = requests.get(url=guests_url, cookies=request.cookies)
|
|
|
- guests_ret = json.loads(guests_ret.content)
|
|
|
- page_size = 10
|
|
|
- last_page = int(ceil(guests_ret['paging']['total'] / float(page_size)))
|
|
|
-
|
|
|
- return render_template('success.html',
|
|
|
- go_back_url='/guests?page={0}&page_size={1}'.format(last_page, page_size),
|
|
|
- timeout=10000, title='提交成功',
|
|
|
- message_title='创建实例的请求已被接受',
|
|
|
- message='您所提交的资源正在创建中。根据所提交资源的大小,需要等待几到十几分钟。页面将在10秒钟后自动跳转到实例列表页面!')
|
|
|
-
|
|
|
- else:
|
|
|
- hosts_url = host_url + url_for('api_hosts.r_get_by_filter', alive=True)
|
|
|
-
|
|
|
- hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
|
|
|
- hosts_ret = json.loads(hosts_ret.content)
|
|
|
-
|
|
|
- return render_template('guest_create.html', hosts_ret=hosts_ret)
|
|
|
-
|
|
|
-
|
|
|
-def port_is_opened(port):
|
|
|
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
- result = s.connect_ex(('0.0.0.0', port))
|
|
|
- if result == 0:
|
|
|
- return True
|
|
|
- else:
|
|
|
- return False
|
|
|
-
|
|
|
-
|
|
|
def vnc(uuid):
|
|
|
host_url = request.host_url.rstrip('/')
|
|
|
|
|
|
@@ -159,7 +72,7 @@ def vnc(uuid):
|
|
|
|
|
|
port = random.randrange(50000, 60000)
|
|
|
while True:
|
|
|
- if not port_is_opened(port=port):
|
|
|
+ if not Utils.port_is_opened(port=port):
|
|
|
break
|
|
|
|
|
|
port = random.randrange(50000, 60000)
|
|
|
@@ -215,10 +128,3 @@ def detail(uuid):
|
|
|
hosts_mapping_by_node_id=hosts_mapping_by_node_id,
|
|
|
disks_ret=disks_ret, config_ret=config_ret)
|
|
|
|
|
|
-
|
|
|
-def success():
|
|
|
- return render_template('success.html', go_back_url='/guests', timeout=10000, title='提交成功',
|
|
|
- message_title='创建实例的请求已被接受',
|
|
|
- message='您所提交的资源正在创建中。根据所提交资源的大小,需要等待几到十几分钟。页面将在10秒钟后自动跳转到实例列表页面!')
|
|
|
-
|
|
|
-
|