Commit aa92a44d by Czémán Arnold

Merge branch 'easy_setup' into 'master'

Easy setup

See merge request !397
parents 3f1b8167 d8606201
Pipeline #621 passed with stage
in 0 seconds
...@@ -19,12 +19,16 @@ from __future__ import unicode_literals, absolute_import ...@@ -19,12 +19,16 @@ from __future__ import unicode_literals, absolute_import
import logging import logging
from django.contrib.auth.models import User from django.contrib.auth.models import User, Group, Permission
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.db.models import Q
from firewall.models import Vlan, VlanGroup, Domain, Firewall, Rule from firewall.models import Vlan, VlanGroup, Domain, Firewall, Rule, Host
from firewall.fields import mac_custom
from storage.models import DataStore from storage.models import DataStore
from vm.models import Lease from vm.models import Lease, Node
from dashboard.models import GroupProfile, Profile
from netaddr import IPAddress, EUI
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -43,6 +47,11 @@ class Command(BaseCommand): ...@@ -43,6 +47,11 @@ class Command(BaseCommand):
parser.add_argument('--firewall-queue') parser.add_argument('--firewall-queue')
parser.add_argument('--admin-user') parser.add_argument('--admin-user')
parser.add_argument('--admin-pass') parser.add_argument('--admin-pass')
parser.add_argument('--node-hostname')
parser.add_argument('--node-mac')
parser.add_argument('--node-ip')
parser.add_argument('--node-name')
parser.add_argument('--kvm-present', action="store_true")
def create(self, model, field, **kwargs): def create(self, model, field, **kwargs):
value = kwargs[field] value = kwargs[field]
...@@ -55,12 +64,13 @@ class Command(BaseCommand): ...@@ -55,12 +64,13 @@ class Command(BaseCommand):
else: else:
return qs[0] return qs[0]
# http://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html # http://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html
def print_state(self): def print_state(self):
self.stdout.write("\nchanged=%s" % ("yes" if self.changed else "no")) self.stdout.write("\nchanged=%s" % ("yes" if self.changed else "no"))
def handle(self, *args, **options): def handle(self, *args, **options):
self.changed = False self.changed = False
# from pdb import set_trace; set_trace()
if (DataStore.objects.exists() and Vlan.objects.exists() and if (DataStore.objects.exists() and Vlan.objects.exists() and
not options['force']): not options['force']):
...@@ -71,6 +81,7 @@ class Command(BaseCommand): ...@@ -71,6 +81,7 @@ class Command(BaseCommand):
is_superuser=True, is_staff=True) is_superuser=True, is_staff=True)
admin.set_password(options['admin_pass']) admin.set_password(options['admin_pass'])
admin.save() admin.save()
self.create(Profile, 'user', user=admin)
self.create(DataStore, 'path', path='/datastore', name='default', self.create(DataStore, 'path', path='/datastore', name='default',
hostname=options['datastore_queue']) hostname=options['datastore_queue'])
...@@ -151,5 +162,112 @@ class Command(BaseCommand): ...@@ -151,5 +162,112 @@ class Command(BaseCommand):
self.create(Rule, 'description', description='allow man->net', self.create(Rule, 'description', description='allow man->net',
direction='out', action='accept', direction='out', action='accept',
foreign_network=vg_net, vlan=man) foreign_network=vg_net, vlan=man)
node_ip = IPAddress(options['node_ip'])
node_mac = EUI(options['node_mac'], dialect=mac_custom)
node_host = Host.objects.filter(ipv4=node_ip).first()
if node_host is None:
node_host = self.create(Host, 'mac', mac=node_mac,
hostname=options['node_hostname'],
ipv4=node_ip, vlan=man, owner=admin)
else:
Host.objects.filter(pk=node_host.pk).update(
mac=node_mac, hostname=options['node_hostname'],
ipv4=node_ip, vlan=man, owner=admin)
node_host.refresh_from_db()
self.create(Node, 'name', name=options['node_name'], host=node_host,
priority=1, enabled=True, schedule_enabled=True)
# creating groups
susers = self.create(Group, 'name', name='Superusers')
pusers = self.create(Group, 'name', name='Powerusers')
users = self.create(Group, 'name', name='Users')
# creating group profiles
self.create(GroupProfile, 'group', group=susers)
self.create(GroupProfile, 'group', group=pusers)
self.create(GroupProfile, 'group', group=users)
# specifying group permissions
user_permissions = [
'create_vm',
'config_ports',
]
puser_permissions = [
'use_autocomplete',
'config_ports',
'create_vm',
'create_empty_disk',
'download_disk',
'resize_disk',
'access_console',
'change_resources',
'set_resources',
'change_template_resources',
'create_template',
]
suser_permissions = [
'add_group',
'use_autocomplete',
'create_empty_disk',
'download_disk',
'access_console',
'change_resources',
'config_ports',
'create_vm',
'recover',
'set_resources',
'change_template_resources',
'create_base_template',
'create_template'
]
# set group permissions
susers.permissions.set(self._get_permissions(suser_permissions))
pusers.permissions.set(self._get_permissions(puser_permissions))
users.permissions.set(self._get_permissions(user_permissions))
# creating users and their profiles
useruser = self.create(User, 'username', username='user',
is_superuser=False, is_staff=False)
useruser.set_password("user")
useruser.save()
self.create(Profile, 'user', user=useruser)
poweruser = self.create(User, 'username', username="poweruser",
is_superuser=False, is_staff=False)
poweruser.set_password("poweruser")
poweruser.save()
self.create(Profile, 'user', user=poweruser)
superuser = self.create(User, 'username', username="superuser",
is_superuser=False, is_staff=False)
superuser.set_password("superuser")
superuser.save()
self.create(Profile, 'user', user=superuser)
# adding users o groups
users.user_set.add(useruser)
pusers.user_set.add(poweruser)
susers.user_set.add(superuser)
# add groups to vm vlan
vm.set_level(users, 'user')
vm.set_level(pusers, 'user')
vm.set_level(susers, 'user')
# notify admin if there is no harware virtualization
if not options['kvm_present']:
admin.profile.notify("hardware virtualization",
"No hardware virtualization detected, "
"your hardware does not support it or "
"not enabled in BIOS.")
self.print_state() self.print_state()
def _get_permissions(self, code_names):
query = Q()
for cn in code_names:
query |= Q(codename=cn)
return Permission.objects.filter(query)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment