1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-24 15:15:22 +00:00

Merge pull request #154 from honza801/master

please merge new features userlist in grid, cloud-init interface
This commit is contained in:
Anatoliy Guskov 2018-06-15 19:14:21 +03:00 committed by GitHub
commit beea57189c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 369 additions and 18 deletions

View file

@ -5,6 +5,7 @@
* User can add SSH public key to root in Instance (Tested only Ubuntu)
* User can change root password in Instance (Tested only Ubuntu)
* Supports cloud-init datasource interface
### Warning!!!
@ -201,7 +202,7 @@ webvirtcloud RUNNING pid 24185, uptime 2:59:14
#### Apache mod_wsgi configuration
```
WSGIDaemonProcess webvirtcloud threads=2 maximum-requests=1000 display-name=webvirtcloud
WSGIScriptAlias / /srv/webvirtcloud/webvirtcloud/wsgi.py
WSGIScriptAlias / /srv/webvirtcloud/webvirtcloud/wsgi_custom.py
```
#### Install final required packages for libvirtd and others on Host Server
@ -219,6 +220,14 @@ login: admin
password: admin
</pre>
### Cloud-init
Currently supports only root ssh authorized keys and hostname. Example configuration of the cloud-init client follows.
```
datasource:
OpenStack:
metadata_urls: [ "http://webvirtcloud.domain.com/datasource" ]
```
### How To Update
```bash
git pull

View file

@ -0,0 +1,175 @@
{% extends "base.html" %}
{% load i18n %}
{% load staticfiles %}
{% block title %}{% trans "Users" %}{% endblock %}
{% block content %}
<!-- Page Heading -->
<div class="row">
<div class="col-lg-12">
{% include 'create_user_block.html' %}
<div class="pull-right search">
<input id="filter" class="form-control" type="text" placeholder="Search">
</div>
<h1 class="page-header">{% trans "Users" %}</h1>
</div>
</div>
<!-- /.row -->
{% include 'errors_block.html' %}
<div class="row">
{% if not users %}
<div class="col-lg-12">
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<i class="fa fa-exclamation-triangle"></i> <strong>{% trans "Warning:" %}</strong> {% trans "You don't have any User" %}
</div>
</div>
{% else %}
<div class="col-lg-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Username</th>
<th>Status</th>
<th>Staff</th>
<th>Superuser</th>
<th>Clone</th>
</tr>
</thead>
<tbody class="searchable">
{% for user in users %}
<tr class="{% if not user.is_active %}danger{% endif %}">
<td>
<a href="{% url 'account' user.id %}"><strong>{{ user.username }}</strong></a>
<a data-toggle="modal" href="#editUser{{ user.id }}" class="pull-right" title="{% trans "Edit" %}">
<span class="glyphicon glyphicon-cog"></span>
</a>
</td>
<td>
{% if user.is_active %}
{% trans "Active" %}
{% else %}
{% trans "Blocked" %}
{% endif %}
</td>
<td>{% if user.is_staff %}<span class="glyphicon glyphicon-ok"></span>{% endif %}</td>
<td>{% if user.is_superuser %}<span class="glyphicon glyphicon-ok"></span>{% endif %}</td>
<td>{% if user.userattributes.can_clone_instances %}<span class="glyphicon glyphicon-ok"></span>{% endif %}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% for user in users %}
<!-- Modal Edit -->
<div class="modal fade" id="editUser{{ user.id }}" tabindex="-1" role="dialog" aria-labelledby="editUserLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" method="post" role="form">{% csrf_token %}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">{% trans "Edit user info" %}</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Name" %}</label>
<div class="col-sm-6">
<input type="hidden" name="user_id" value="{{ user.id }}">
<input type="text" name="name" class="form-control" value="{{ user.username }}" disabled>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Password" %}</label>
<div class="col-sm-6">
<input type="password" name="user_pass" class="form-control" value="">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Is staff" %}</label>
<div class="col-sm-2">
<input type="checkbox" name="user_is_staff" {% if user.is_staff %}checked{% endif %}>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Is superuser" %}</label>
<div class="col-sm-2">
<input type="checkbox" name="user_is_superuser" {% if user.is_superuser %}checked{% endif %}>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Can clone instances" %}</label>
<div class="col-sm-2">
<input type="checkbox" name="userattributes_can_clone_instances" {% if user.userattributes.can_clone_instances %}checked{% endif %}>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max instances" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_instances" class="form-control" value="{{ user.userattributes.max_instances }}">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max cpus" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_cpus" class="form-control" value="{{ user.userattributes.max_cpus }}">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max memory (MB)" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_memory" class="form-control" value="{{ user.userattributes.max_memory }}">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Max disk size (GB)" %}</label>
<div class="col-sm-6">
<input type="text" name="userattributes_max_disk_size" class="form-control" value="{{ user.userattributes.max_disk_size }}">
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="pull-left btn btn-danger" name="delete">
{% trans "Delete" %}
</button>
{% if user.is_active %}
<button type="submit" class="pull-left btn btn-warning" name="block">
{% trans "Block" %}
</button>
{% else %}
<button type="submit" class="pull-left btn btn-success" name="unblock">
{% trans "Unblock" %}
</button>
{% endif %}
<button type="button" class="btn btn-default" data-dismiss="modal">
{% trans "Close" %}
</button>
<button type="submit" class="btn btn-primary" name="edit">
{% trans "Edit" %}
</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
{% endfor %}
</div>
{% endif %}
</div>
{% endblock %}
{% block script %}
<script>
function filter_table() {
var rex = new RegExp($(this).val(), 'i');
$('.searchable tr').hide();
$('.searchable tr').filter(function () {
return rex.test($(this).text());
}).show();
}
$(document).ready(function () {
(function ($) {
$('#filter').keyup(filter_table)
}(jQuery));
});
</script>
{% endblock %}

View file

@ -1,6 +1,6 @@
{% load i18n %}
{% if request.user.is_superuser %}
<a href="#AddUser" type="button" class="btn btn-success pull-right" data-toggle="modal">
<a href="#AddUser" type="button" class="btn btn-success btn-header pull-right" data-toggle="modal">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</a>
@ -8,12 +8,12 @@
<div class="modal fade" id="AddUser" tabindex="-1" role="dialog" aria-labelledby="AddUserLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">{% trans "Add New User" %}</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" method="post" action="" role="form">{% csrf_token %}
<form class="form-horizontal" method="post" action="" role="form">{% csrf_token %}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">{% trans "Add New User" %}</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="col-sm-4 control-label">{% trans "Name" %}</label>
<div class="col-sm-6">
@ -26,12 +26,12 @@
<input type="password" class="form-control" name="password" placeholder="*******" {% if not allow_empty_password %}required{% endif %}>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
<button type="submit" class="btn btn-primary" name="create">{% trans "Create" %}</button>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
<button type="submit" class="btn btn-primary" name="create">{% trans "Create" %}</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->

View file

@ -130,7 +130,10 @@ def accounts(request):
user_delete.delete()
return HttpResponseRedirect(request.get_full_path())
return render(request, 'accounts.html', locals())
accounts_template_file = 'accounts.html'
if settings.VIEW_ACCOUNTS_STYLE == "list":
accounts_template_file = 'accounts-list.html'
return render(request, accounts_template_file, locals())
@login_required

0
datasource/__init__.py Normal file
View file

3
datasource/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

3
datasource/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,6 @@
#cloud-config
{% if instance_keys %}
ssh_authorized_keys:
{% for key in instance_keys %} - {{ key }}{% endfor %}
{% endif %}

3
datasource/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
datasource/urls.py Normal file
View file

@ -0,0 +1,11 @@
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^openstack/$',
views.os_index, name='ds_openstack_index'),
url(r'^openstack/(?P<version>[\w\-\.]+)/meta_data.json$',
views.os_metadata_json, name='ds_openstack_metadata'),
url(r'^openstack/(?P<version>[\w\-\.]+)/user_data$',
views.os_userdata, name='ds_openstack_userdata'),
]

64
datasource/views.py Normal file
View file

@ -0,0 +1,64 @@
from django.shortcuts import render
from django.http import HttpResponse, Http404
from accounts.models import UserInstance, UserSSHKey
import json
import socket
OS_VERSIONS = [ 'latest', '' ]
OS_UUID = "iid-dswebvirtcloud"
def os_index(request):
response = '\n'.join(OS_VERSIONS)
return HttpResponse(response)
def os_metadata_json(request, version):
"""
:param request:
:param version:
:return:
"""
if version == 'latest':
ip = get_client_ip(request)
hostname = get_hostname_by_ip(ip)
response = { 'uuid': OS_UUID, 'hostname': hostname }
return HttpResponse(json.dumps(response))
else:
err = 'Invalid version: %s' % version
raise Http404(err)
def os_userdata(request, version):
"""
:param request:
:param version:
:return:
"""
if version == 'latest':
ip = get_client_ip(request)
hostname = get_hostname_by_ip(ip)
vname = hostname.split('.')[0]
instance_keys = []
userinstances = UserInstance.objects.filter(instance__name=vname)
for ui in userinstances:
keys = UserSSHKey.objects.filter(user=ui.user)
for k in keys:
instance_keys.append(k.keypublic)
return render(request, 'user_data', locals())
else:
err = 'Invalid version: %s' % version
raise Http404(err)
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[-1].strip()
else:
ip = request.META.get('REMOTE_ADDR')
return ip
def get_hostname_by_ip(ip):
addrs = socket.gethostbyaddr(ip)
return addrs[0]

View file

@ -712,6 +712,27 @@
</div>
</div>
</form>
<p>{% trans "To set console listen address, shutdown the instance." %}</p>
<form class="form-horizontal" method="post" role="form">{% csrf_token %}
<div class="form-group" id="console_listen_address_selection">
<label for="console_select_listen_address" class="col-sm-2 control-label">{% trans "Listen on" %}</label>
<div class="col-sm-6">
<select id="console_select_listen_address" class="form-control" name="console_listen_address">
<option value="" style="font-weight: bold">{% trans "please choose" %}</option>
{% for address, label in console_listen_addresses %}
<option value="{{ address }}">{{ label }}</option>
{% endfor %}
</select>
</div>
<div class="col-sm-3">
{% ifequal status 5 %}
<button type="submit" class="btn btn-success " name="set_console_listen_address">{% trans "Set" %}</button>
{% else %}
<button class="btn btn-success disabled" name="set_console_listen_address">{% trans "Set" %}</button>
{% endifequal %}
</div>
</div>
</form>
<p>{% trans "To create console password, shutdown the instance." %}</p>
<form class="form-horizontal" method="post" role="form">{% csrf_token %}
<div class="form-group">
@ -1273,6 +1294,13 @@
$("#console_select_type option[value='" + console_type + "']").prop('selected', true);
}
});
$(document).ready(function () {
// set current console listen address or fall back to default
var console_listen_address = "{{ console_listen_address }}"
if (console_listen_address != '') {
$("#console_select_listen_address option[value='" + console_listen_address + "']").prop('selected', true);
}
});
{% if not request.user.is_superuser %}
$('#select_clone_name').on('change', function () {
update_clone_disk_name($(this).val());

View file

@ -22,7 +22,6 @@ from vrtManager.connection import connection_manager
from vrtManager.create import wvmCreate
from vrtManager.util import randomPasswd
from libvirt import libvirtError, VIR_DOMAIN_XML_SECURE
from webvirtcloud.settings import QEMU_KEYMAPS, QEMU_CONSOLE_TYPES
from logs.views import addlogmsg
from django.conf import settings
@ -186,8 +185,9 @@ def instance(request, compute_id, vname):
computes_count = computes.count()
users = User.objects.all().order_by('username')
publickeys = UserSSHKey.objects.filter(user_id=request.user.id)
keymaps = QEMU_KEYMAPS
console_types = QEMU_CONSOLE_TYPES
keymaps = settings.QEMU_KEYMAPS
console_types = settings.QEMU_CONSOLE_TYPES
console_listen_addresses = settings.QEMU_CONSOLE_LISTEN_ADDRESSES
try:
userinstace = UserInstance.objects.get(instance__compute_id=compute_id,
instance__name=vname,
@ -617,6 +617,13 @@ def instance(request, compute_id, vname):
msg = _("Set VNC type")
addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(request.get_full_path() + '#vncsettings')
if 'set_console_listen_address' in request.POST:
console_type = request.POST.get('console_listen_address', '')
conn.set_console_listen_addr(console_type)
msg = _("Set VNC listen address")
addlogmsg(request.user.username, instance.name, msg)
return HttpResponseRedirect(request.get_full_path() + '#vncsettings')
if request.user.is_superuser:
if 'migrate' in request.POST:

View file

@ -455,6 +455,32 @@ class wvmInstance(wvmConnect):
return "127.0.0.1"
return listen_addr
def set_console_listen_addr(self, listen_addr):
xml = self._XMLDesc(VIR_DOMAIN_XML_SECURE)
root = ElementTree.fromstring(xml)
console_type = self.get_console_type()
try:
graphic = root.find("devices/graphics[@type='%s']" % console_type)
except SyntaxError:
# Little fix for old version ElementTree
graphic = root.find("devices/graphics")
if graphic is None:
return False
listen = graphic.find("listen[@type='address']")
if listen is None:
return False
if listen_addr:
graphic.set("listen", listen_addr)
listen.set("address", listen_addr)
else:
try:
graphic.attrib.pop("listen")
listen.attrib.pop("address")
except:
pass
newxml = ElementTree.tostring(root)
return self._defineXML(newxml)
def get_console_socket(self):
socket = util.get_xml_path(self._XMLDesc(0),
"/domain/devices/graphics/@socket")

View file

@ -29,6 +29,7 @@ INSTALLED_APPS = (
'logs',
'accounts',
'create',
'datasource',
)
MIDDLEWARE_CLASSES = (
@ -44,6 +45,7 @@ MIDDLEWARE_CLASSES = (
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
#'django.contrib.auth.backends.RemoteUserBackend',
#'accounts.backends.MyRemoteUserBackend',
)
@ -104,6 +106,12 @@ QEMU_CONSOLE_TYPES = ['vnc', 'spice']
# default console type
QEMU_CONSOLE_DEFAULT_TYPE = 'vnc'
# list of console listen addresses
QEMU_CONSOLE_LISTEN_ADDRESSES = (
('127.0.0.1', 'Localhost'),
('0.0.0.0', 'All interfaces'),
)
# list taken from http://qemu.weilnetz.de/qemu-doc.html#sec_005finvocation
QEMU_KEYMAPS = ['ar', 'da', 'de', 'de-ch', 'en-gb', 'en-us', 'es', 'et', 'fi',
'fo', 'fr', 'fr-be', 'fr-ca', 'fr-ch', 'hr', 'hu', 'is', 'it',
@ -123,6 +131,10 @@ ALLOW_EMPTY_PASSWORD = True
SHOW_ACCESS_ROOT_PASSWORD = False
SHOW_ACCESS_SSH_KEYS = False
SHOW_PROFILE_EDIT_PASSWORD = False
# available: default (grid), list
VIEW_ACCOUNTS_STYLE = 'grid'
INSTANCE_VOLUME_DEFAULT_FORMAT = 'qcow2'
INSTANCE_VOLUME_DEFAULT_BUS = 'virtio'
INSTANCE_VOLUME_DEFAULT_CACHE = 'directsync'

View file

@ -9,6 +9,7 @@ urlpatterns = patterns('',
url(r'^accounts/', include('accounts.urls')),
url(r'^computes/', include('computes.urls')),
url(r'^logs/', include('logs.urls')),
url(r'^datasource/', include('datasource.urls')),
url(r'^compute/(?P<compute_id>[0-9]+)/storages/$',
'storages.views.storages', name='storages'),