create-activate phase 2

This commit is contained in:
deflax 2017-11-05 22:20:39 +02:00
parent 7bf97fc487
commit 9c085f4561
6 changed files with 32 additions and 19 deletions

View file

@ -96,7 +96,7 @@ def dashboard():
#current_app.logger.warning(str(inv_deploycubeids)) #current_app.logger.warning(str(inv_deploycubeids))
for unit_id in inv_deploycubeids: for unit_id in inv_deploycubeids:
rrd[unit_id] = {} rrd[unit_id] = {}
data = { 'user_id': int(unit_id), data = { 'unit_id': int(unit_id),
'type': 'kvm' } 'type': 'kvm' }
try: try:
query = contact_proxmaster(data, 'vmrrd') query = contact_proxmaster(data, 'vmrrd')

View file

@ -220,7 +220,7 @@ def contact_proxmaster(data, method):
try: try:
db_result = requests.post( url, data=data_json, headers={"content-type": "application/json"}, timeout=30 ) db_result = requests.post( url, data=data_json, headers={"content-type": "application/json"}, timeout=30 )
proxjson = db_result.json() proxjson = db_result.json()
#current_app.logger.info('grid> {}'.format(str(db_result))) current_app.logger.info('grid> {}'.format(str(proxjson)))
return proxjson return proxjson
except: except:
return { 'status': 'UNREACHABLE' } return { 'status': 'UNREACHABLE' }

View file

@ -4,6 +4,8 @@ from . import news
from functools import lru_cache from functools import lru_cache
import requests import requests
import json import json
from dateutil.parser import parse
from dateutil.tz import tzutc
from facepy import GraphAPI from facepy import GraphAPI
@lru_cache(maxsize=32) @lru_cache(maxsize=32)
@ -15,6 +17,13 @@ def get_fb_token():
#current_app.logger.info("FB Token: {}".format(fbtoken)) #current_app.logger.info("FB Token: {}".format(fbtoken))
return fbtoken return fbtoken
@news.app_template_filter('fbtime')
def fbtime(event_date):
UTC = tzutc()
dt = parse(event_date)
dt_utc = dt.astimezone(UTC) # Convert to UTC
return dt.strftime('%d %b %Y')
@news.route("/all", methods=['GET']) @news.route("/all", methods=['GET'])
def all(): def all():
graph = GraphAPI(get_fb_token()) graph = GraphAPI(get_fb_token())

View file

@ -5,8 +5,9 @@
<div class="row"> <div class="row">
<ul id="navigation"> <ul id="navigation">
{% for item in result %} {% for item in result %}
<li><a target="_blank" href="{{ item['link'] }}">{{ item['message'] }}</a> {{ item['created_time'] }}<br /> <li><a target="_blank" href="{{ item['link'] }}">{{ item['message'] }}</a></li>
</li><br /> <i>{{ item['created_time'] | fbtime }}</i>
<br /><br />
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View file

@ -1,7 +1,10 @@
{% block page_content %} {% block page_content %}
<h4><a href="/news/all">News</a></h4> <h4>News</h4>
{% for item in result %} {% for item in result %}
<p><span class="tweet_day">{{ item['created_time'] }}</span> <p>
<a target="_blank" href="{{ item['link'] }}">{{ item['message'] }}</a></p> <a target="_blank" href="{{ item['link'] }}">{{ item['message'] }}</a>
<span class="tweet_day">{{ item['created_time'] | fbtime }}</span>
</p>
{% endfor %} {% endfor %}
<a href="/news/all">>>></a>
{% endblock %} {% endblock %}

View file

@ -60,7 +60,7 @@ def createvm():
region_name = query['region'] region_name = query['region']
slave_name = query['slave'] slave_name = query['slave']
bridge_id = query['unit_id'] bridge_id = query['unit_id']
bridge_phyid = query['phyid'] bridge_phy_id = query['phy_id']
bridge = Bridge(user_id=int(current_user.pid), bridge_id=bridge_id) bridge = Bridge(user_id=int(current_user.pid), bridge_id=bridge_id)
db.session.add(bridge) db.session.add(bridge)
db.session.commit() db.session.commit()
@ -78,7 +78,7 @@ def createvm():
#machine will be installed where the switch physically is #machine will be installed where the switch physically is
region_name = query['region'] region_name = query['region']
slave_name = query['slave'] slave_name = query['slave']
bridge_phyid = query['phyid'] bridge_phy_id = query['phy_id']
else: else:
flash('Point found but cannot be used!') flash('Point found but cannot be used!')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
@ -94,7 +94,7 @@ def createvm():
'cpu': '1', 'cpu': '1',
'mem': '512', 'mem': '512',
'hdd': '20', 'hdd': '20',
'net0if': 'vmbr' + str(bridge_phyid) 'net0if': 'vmbr' + str(bridge_phy_id)
} }
try: try:
query = contact_proxmaster(data, 'create') query = contact_proxmaster(data, 'create')
@ -103,7 +103,7 @@ def createvm():
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
if query['status'] == 'kvm_created': if query['status'] == 'kvm_created':
deployment = Deployment(user_id=int(current_user.pid), machine_alias=, machine_id=query['unit_id'], machine_cpu=data['cpu'], machine_mem=data['mem'], machine_hdd=data['hdd'], enabled=True, protected=False, daysleft=15, warning=True, discount=0) deployment = Deployment(user_id=int(current_user.pid), machine_alias=str(form.servername.data), machine_id=query['unit_id'], machine_cpu=data['cpu'], machine_mem=data['mem'], machine_hdd=data['hdd'], enabled=True, protected=False, daysleft=15, warning=True, discount=0)
db.session.add(deployment) db.session.add(deployment)
db.session.commit() db.session.commit()
flash('New device created successfully in region "{}".'.format(str(selected_region.description))) flash('New device created successfully in region "{}".'.format(str(selected_region.description)))
@ -161,10 +161,10 @@ def activate(itemid=0):
current_app.logger.info('[{}] Charge deployment: {}'.format(owner.email, deploy.machine_id)) current_app.logger.info('[{}] Charge deployment: {}'.format(owner.email, deploy.machine_id))
#TODO: Filter routers for the selected region only. switch should return slave name #TODO: Filter routers for the selected region only. switch should return slave name
selected_router = current_user.inv_routers.filter_by(deleted=False).first() selected_router = owner.inv_routers.filter_by(deleted=False).first()
if selected_router is None: if selected_router is None:
#TODO: Filter bridges for the selected region only. switch should return slave name #TODO: Filter bridges for the selected region only. switch should return slave name
selected_bridge = current_user.inv_bridges.filter_by(deleted=False).first() selected_bridge = owner.inv_bridges.filter_by(deleted=False).first()
if selected_bridge is None: if selected_bridge is None:
flash('No bridge created yet. Cannot activate.') flash('No bridge created yet. Cannot activate.')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
@ -177,22 +177,22 @@ def activate(itemid=0):
#machine will be installed where the switch physically is #machine will be installed where the switch physically is
region_name = query['region'] region_name = query['region']
slave_name = query['slave'] slave_name = query['slave']
bridge_phyid = query['phyid'] bridge_phy_id = query['phy_id']
else: else:
flash('Point found but cannot be used!') flash('Point found but cannot be used!')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
#no router. creating... #no router. creating...
data = { 'clientid': str(current_user.pid), data = { 'clientid': str(owner.pid),
'clientemail': str(current_user.email), 'clientemail': str(owner.email),
'hostname': 'r' + str(selected_address.ip) + '-b' + str(bridge_phyid), 'hostname': 'c' + str(owner.pid) + '-rt' + str(selected_address.ip),
'region': str(region_name), 'region': str(region_name),
'slave': str(slave_name), 'slave': str(slave_name),
'type': 'lxc', 'type': 'lxc',
'cpu': '1', 'cpu': '1',
'mem': '128', 'mem': '128',
'hdd': '1', 'hdd': '1',
'net0if': 'vmbr' + bridge_phyid, 'net0if': 'vmbr' + bridge_phy_id,
'net0ip': '192.168.9.1', 'net0ip': '192.168.9.1',
'net1if': 'vmbr0', 'net1if': 'vmbr0',
'net1ip': str(selected_address.ip), 'net1ip': str(selected_address.ip),
@ -204,7 +204,7 @@ def activate(itemid=0):
flash('Region unreachable! Cannot create router. Please try again later...') flash('Region unreachable! Cannot create router. Please try again later...')
return redirect(url_for('main.dashboard')) return redirect(url_for('main.dashboard'))
if query is not None: if query is not None:
router = Router(user_id=int(current_user.pid), machine_id=query['cube']) router = Router(user_id=int(owner.pid), machine_id=query['cube'])
db.session.add(router) db.session.add(router)
db.session.commit() db.session.commit()