init.py 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals, absolute_import

20
import logging
21

22
from django.contrib.auth.models import User, Group, Permission
23
from django.core.management.base import BaseCommand
24
from django.db.models import Q
25

26 27
from firewall.models import Vlan, VlanGroup, Domain, Firewall, Rule, Host
from firewall.fields import mac_custom
28
from storage.models import DataStore
29 30 31
from vm.models import Lease, Node
from dashboard.models import GroupProfile, Profile
from netaddr import IPAddress, EUI
32 33


34 35 36
logger = logging.getLogger(__name__)


37
class Command(BaseCommand):
38 39 40 41 42 43 44 45 46 47 48 49
    def add_arguments(self, parser):
        parser.add_argument('--force', action="store_true")
        parser.add_argument('--external-net')
        parser.add_argument('--management-net')
        parser.add_argument('--vm-net')
        parser.add_argument('--external-if')
        parser.add_argument('--management-if')
        parser.add_argument('--vm-if')
        parser.add_argument('--datastore-queue')
        parser.add_argument('--firewall-queue')
        parser.add_argument('--admin-user')
        parser.add_argument('--admin-pass')
50 51 52 53 54
        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")
55 56 57 58 59 60

    def create(self, model, field, **kwargs):
        value = kwargs[field]
        qs = model.objects.filter(**{field: value})[:1]
        if not qs.exists():
            obj = model.objects.create(**kwargs)
61 62
            logger.info('New %s: %s', model, obj)
            self.changed = True
63 64 65 66
            return obj
        else:
            return qs[0]

67
    # http://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html
68
    def print_state(self):
69
        self.stdout.write("\nchanged=%s" % ("yes" if self.changed else "no"))
70 71

    def handle(self, *args, **options):
72
        self.changed = False
73
    # from pdb import set_trace; set_trace()
74

75 76
        if (DataStore.objects.exists() and Vlan.objects.exists() and
                not options['force']):
77 78
            self.print_state()
            return
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

        admin = self.create(User, 'username', username=options['admin_user'],
                            is_superuser=True, is_staff=True)
        admin.set_password(options['admin_pass'])
        admin.save()

        self.create(DataStore, 'path', path='/datastore', name='default',
                    hostname=options['datastore_queue'])

        # leases
        self.create(Lease, 'name', name='lab',
                    suspend_interval_seconds=3600 * 5,
                    delete_interval_seconds=3600 * 24 * 7)

        self.create(Lease, 'name', name='project',
                    suspend_interval_seconds=3600 * 24 * 30,
                    delete_interval_seconds=3600 * 24 * 30 * 6)

        self.create(Lease, 'name', name='server',
                    suspend_interval_seconds=3600 * 24 * 365,
                    delete_interval_seconds=3600 * 24 * 365 * 3)

101 102 103 104 105 106
        net_domain = self.create(Domain, 'name', name='net.example.com',
                                 owner=admin)
        man_domain = self.create(Domain, 'name', name='man.example.com',
                                 owner=admin)
        vm_domain = self.create(Domain, 'name', name='vm.example.com',
                                owner=admin)
107 108

        # vlans
109 110
        net = self.create(Vlan, 'vid', name=options['external_if'], vid=4,
                          network4=options['external_net'], domain=net_domain)
111

112 113 114 115
        man = self.create(Vlan, 'vid', name=options['management_if'], vid=3,
                          dhcp_pool='manual',
                          network4=options['management_net'],
                          domain=man_domain,
116 117
                          snat_ip=options['external_net'].split('/')[0])
        man.snat_to.add(net)
118
        man.snat_to.add(man)
119

120 121 122
        vm = self.create(Vlan, 'vid', name=options['vm_if'], vid=2,
                         dhcp_pool='manual',
                         network4=options['vm_net'], domain=vm_domain,
123 124
                         snat_ip=options['external_net'].split('/')[0])
        vm.snat_to.add(net)
125
        vm.snat_to.add(vm)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

        # default vlan groups
        vg_all = self.create(VlanGroup, 'name', name='all')
        vg_all.vlans.add(vm, man, net)

        vg_pf = self.create(VlanGroup, 'name', name='portforward')
        vg_pf.vlans.add(vm, man, net)

        vg_net = self.create(VlanGroup, 'name', name='net')
        vg_net.vlans.add(net)

        # firewall rules
        fw = self.create(Firewall, 'name', name=options['firewall_queue'])

        self.create(Rule, 'description', description='default output rule',
                    direction='out', action='accept',
                    foreign_network=vg_all, firewall=fw)

144 145 146 147 148 149 150 151 152 153
        self.create(Rule, 'description', description='portal https',
                    direction='in', action='accept', proto='tcp', dport=443,
                    foreign_network=vg_all, firewall=fw)

        self.create(Rule, 'description', description='portal http',
                    direction='in', action='accept', proto='tcp', dport=80,
                    foreign_network=vg_all, firewall=fw)

        self.create(Rule, 'description', description='ssh',
                    direction='in', action='accept', proto='tcp', dport=22,
154 155 156 157 158 159 160 161 162 163
                    foreign_network=vg_all, firewall=fw)

        # vlan rules
        self.create(Rule, 'description', description='allow vm->net',
                    direction='out', action='accept',
                    foreign_network=vg_net, vlan=vm)

        self.create(Rule, 'description', description='allow man->net',
                    direction='out', action='accept',
                    foreign_network=vg_net, vlan=man)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        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
        admins = self.create(Group, 'name', name='Administrators')
        pusers = self.create(Group, 'name', name='Powerusers')
        users = self.create(Group, 'name', name='Users')

        # creating group profiles
        self.create(GroupProfile, 'group', group=admins)
        self.create(GroupProfile, 'group', group=pusers)
        self.create(GroupProfile, 'group', group=users)

        # specifying group permissions
        user_permissions = [
            'add_diskresizeaction',
            'change_diskresizeaction',
            'delete_diskresizeaction',
            'add_extendleaseaction',
            'change_extendleaseaction',
            'delete_extendleaseaction',
            'add_resourcechangeaction',
            'change_resourcechangeaction',
            'delete_resourcechangeaction',
            'add_templateaccessaction',
            'change_templateaccessaction',
            'delete_templateaccessaction',
            'add_templateaccesstype',
            'change_templateaccesstype',
            'delete_templateaccesstype',
            'create_vm',
            'config_ports',
        ]

        puser_permissions = [
            'add_extendleaseaction',
            'change_extendleaseaction',
            'delete_extendleaseaction',
            'config_ports',
            'create_vm',
            'create_empty_disk',
            'download_disk',
            'resize_disk',
            'access_console',
            'change_resources',
            'set_resources',
            'change_template_resources',
            'add_instancetemplate',
        ]

        admin_permissions = [
            'add_group',
            'change_group',
            'delete_group',
            'add_user',
            'change_user',
            'delete_user',
            'add_message',
            'change_message',
            'delete_message',
            'use_autocomplete',
            'add_group',
            'change_group',
            'delete_group',
            'create_empty_disk',
            'download_disk',
            'resize_disk',
            'access_console',
            'change_resources',
            'config_ports',
            'create_vm',
            'emergency_change_state',
            'recover',
            'redeploy',
            'set_resources',
            'change_template_resources',
            'create_base_template',
            'create_template',
            'create_leases',
        ]

        # set group permissions
        admins.permissions.set(self._get_permissions(admin_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=True, is_staff=False)
        poweruser.set_password("poweruser")
        poweruser.save()
        self.create(Profile, 'user', user=poweruser)

        adminuser = self.create(User, 'username', username="admin",
                                is_superuser=True, is_staff=False)
        adminuser.set_password("admin")
        adminuser.save()
        self.create(Profile, 'user', user=adminuser)

        # adding users o groups
        users.user_set.add(useruser)
        pusers.user_set.add(poweruser)
        admins.user_set.add(adminuser)

        # add groups to vm vlan
        vm.set_level(users, 'user')
        vm.set_level(pusers, 'user')
        vm.set_level(admins, 'user')

        # notify admin if there is no harware virtualization
        if options['kvm-present']:
            adminuser.profile.notify("hardware virtualization",
                                     "No hardware virtualization detected, "
                                     "your hardware does not support it or "
                                     "not enabled in BIOS.")
298
        self.print_state()
299 300 301 302 303 304

    def _get_permissions(self, code_names):
        query = Q()
        for cn in code_names:
            query |= Q(codename=cn)
        return Permission.objects.filter(query)