<commit_before># Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import urlparse

from ostrich import steps
from ostrich import utils

def get_steps(r):
    """Do all the configuration we do before bootstrapping."""

    nextsteps = []

    p = urlparse.urlparse(r.complete['git-mirror-github'])
    mirror_host_github = p.netloc.split(':')[0]
    p = urlparse.urlparse(r.complete['git-mirror-openstack'])
    mirror_host_openstack = p.netloc.split(':')[0]

    nextsteps.append(
        steps.SimpleCommandStep(
            'git-mirror-host-keys',
            ('ssh-keyscan -H %s >> /etc/ssh/ssh_known_hosts'
             % mirror_host_openstack),
            **r.kwargs)
        )

    if mirror_host_github != mirror_host_openstack:
        nextsteps.append(
            steps.SimpleCommandStep(
                'git-mirror-host-keys-github',
                ('ssh-keyscan -H %s >> /etc/ssh/ssh_known_hosts'
                 % mirror_host_github),
                **r.kwargs)
            )

    if r.complete['enable-ceph'] == 'yes':
        if r.complete['osa-branch'] in ['stable/mitaka',
                                        'stable/newton']:
            # This isn't implemented for these releases
            pass
        else:
            nextsteps.append(
                steps.YamlAddElementStep(
                    'enable-ceph-aio',
                    'tests/bootstrap-aio.yml',
                    [0, 'vars', 'confd_overrides', 'aio'],
                    'name: ceph.yml.aio',
                    **r.kwargs
                )
            )

    if utils.is_ironic(r):
        if r.complete['osa-branch'] == 'stable/mitaka':
            nextsteps.append(steps.PatchStep('ironic-aio-mitaka', **r.kwargs))
        else:
            nextsteps.append(
                steps.YamlAddElementStep(
                    'enable-ironic-aio',
                    'tests/bootstrap-aio.yml',
                    [0, 'vars', 'confd_overrides', 'aio'],
                    'name: ironic.yml.aio',
                    **r.kwargs
                )
            )

        nextsteps.append(
            steps.FileAppendStep(
                'group-vars-ironic_service_user_name',
                'playbooks/inventory/group_vars/all.yml',
                '\n\nironic_service_user_name: ironic',
                **r.kwargs)
            )

    if r.complete['osa-branch'] != 'stable/mitaka':
        nextsteps.append(
            steps.RegexpEditorStep(
                'ansible-no-loopback-swap',
                ('/opt/openstack-ansible/tests/roles/bootstrap-host/'
                 'tasks/prepare_loopback_swap.yml'),
                'command: grep /openstack/swap.img /proc/swaps',
                'command: /bin/true',
                **r.kwargs)
            )

    nextsteps.append(
        steps.RegexpEditorStep(
            'lxc-cachable-downloads',
            '/usr/share/lxc/templates/lxc-download',
            'wget_wrapper -T 30 -q https?://',
            'wget_wrapper -T 30 -q --no-hsts http://',
            **r.kwargs)
        )

    nextsteps.append(
        steps.SimpleCommandStep(
            'archive-upper-constraints',
            ('curl https://git.openstack.org/cgit/openstack/requirements/'
             'plain/upper-constraints.txt?id='
             '$(awk \'/requirements_git_install_branch:/ {print $2}\' '
             '/opt/openstack-ansible/playbooks/defaults/repo_packages/'
             'openstack_services.yml) -o ~/.ostrich/upper-contraints.txt'),
            **r.kwargs)
        )

    return nextsteps
<commit_msg>Its a list of dicts!<commit_after># Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import urlparse

from ostrich import steps
from ostrich import utils

def get_steps(r):
    """Do all the configuration we do before bootstrapping."""

    nextsteps = []

    p = urlparse.urlparse(r.complete['git-mirror-github'])
    mirror_host_github = p.netloc.split(':')[0]
    p = urlparse.urlparse(r.complete['git-mirror-openstack'])
    mirror_host_openstack = p.netloc.split(':')[0]

    nextsteps.append(
        steps.SimpleCommandStep(
            'git-mirror-host-keys',
            ('ssh-keyscan -H %s >> /etc/ssh/ssh_known_hosts'
             % mirror_host_openstack),
            **r.kwargs)
        )

    if mirror_host_github != mirror_host_openstack:
        nextsteps.append(
            steps.SimpleCommandStep(
                'git-mirror-host-keys-github',
                ('ssh-keyscan -H %s >> /etc/ssh/ssh_known_hosts'
                 % mirror_host_github),
                **r.kwargs)
            )

    if r.complete['enable-ceph'] == 'yes':
        if r.complete['osa-branch'] in ['stable/mitaka',
                                        'stable/newton']:
            # This isn't implemented for these releases
            pass
        else:
            nextsteps.append(
                steps.YamlAddElementStep(
                    'enable-ceph-aio',
                    'tests/bootstrap-aio.yml',
                    [0, 'vars', 'confd_overrides', 'aio'],
                    {'name': 'ceph.yml.aio'},
                    **r.kwargs
                )
            )

    if utils.is_ironic(r):
        if r.complete['osa-branch'] == 'stable/mitaka':
            nextsteps.append(steps.PatchStep('ironic-aio-mitaka', **r.kwargs))
        else:
            nextsteps.append(
                steps.YamlAddElementStep(
                    'enable-ironic-aio',
                    'tests/bootstrap-aio.yml',
                    [0, 'vars', 'confd_overrides', 'aio'],
                    {'name': 'ironic.yml.aio'},
                    **r.kwargs
                )
            )

        nextsteps.append(
            steps.FileAppendStep(
                'group-vars-ironic_service_user_name',
                'playbooks/inventory/group_vars/all.yml',
                '\n\nironic_service_user_name: ironic',
                **r.kwargs)
            )

    if r.complete['osa-branch'] != 'stable/mitaka':
        nextsteps.append(
            steps.RegexpEditorStep(
                'ansible-no-loopback-swap',
                ('/opt/openstack-ansible/tests/roles/bootstrap-host/'
                 'tasks/prepare_loopback_swap.yml'),
                'command: grep /openstack/swap.img /proc/swaps',
                'command: /bin/true',
                **r.kwargs)
            )

    nextsteps.append(
        steps.RegexpEditorStep(
            'lxc-cachable-downloads',
            '/usr/share/lxc/templates/lxc-download',
            'wget_wrapper -T 30 -q https?://',
            'wget_wrapper -T 30 -q --no-hsts http://',
            **r.kwargs)
        )

    nextsteps.append(
        steps.SimpleCommandStep(
            'archive-upper-constraints',
            ('curl https://git.openstack.org/cgit/openstack/requirements/'
             'plain/upper-constraints.txt?id='
             '$(awk \'/requirements_git_install_branch:/ {print $2}\' '
             '/opt/openstack-ansible/playbooks/defaults/repo_packages/'
             'openstack_services.yml) -o ~/.ostrich/upper-contraints.txt'),
            **r.kwargs)
        )

    return nextsteps
<|endoftext|>


<commit_before>from django.views.generic import RedirectView

try:
    from django.core.urlresolvers import reverse
except ModuleNotFoundError:
    from django.urls import reverse

from django.views.static import serve
from django.conf import settings
from django.contrib.auth.decorators import login_required
from functools import wraps
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.auth.views import login
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.utils.translation import ugettext as _

def superuser_required(view_func):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, displaying the login page if necessary.
    """
    @wraps(view_func)
    def _checklogin(request, *args, **kwargs):
        if request.user.is_active and request.user.is_superuser:
            # The user is valid. Continue to the admin page.
            return view_func(request, *args, **kwargs)

        assert hasattr(request, 'session'), "The Django admin requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
        defaults = {
            'template_name': 'admin/login.html',
            'authentication_form': AdminAuthenticationForm,
            'extra_context': {
                'title': _('Log in'),
                'app_path': request.get_full_path(),
                REDIRECT_FIELD_NAME: request.get_full_path(),
                },
            }
        return login(request, **defaults)
    return _checklogin

def public(function=None):
    """
    Dummy decorator that doesn't check anything.
    """
    return function

class DocsAccessSettingError(ValueError):
    pass

class DocsRootSettingError(ValueError):
    pass

DOCS_ACCESS_CHOICES = (
    'public',
    'login_required',
    'staff',
    'superuser',
)
DOCS_ROOT = getattr(settings, 'DOCS_ROOT', None)
DOCS_ACCESS = getattr(settings, 'DOCS_ACCESS', DOCS_ACCESS_CHOICES[0])

if DOCS_ACCESS == 'public':
    decorator = public
elif DOCS_ACCESS == 'login_required':
    decorator = login_required
elif DOCS_ACCESS == 'staff':
    decorator = staff_member_required
elif DOCS_ACCESS == 'superuser':
    decorator = superuser_required
else:
    decorator = public

@decorator
def serve_docs(request, path, **kwargs):
    if DOCS_ACCESS not in DOCS_ACCESS_CHOICES:
        raise DocsAccessSettingError('DOCS_ACCESS setting value is incorrect: %s (choises are: %s)' % (
            DOCS_ACCESS,
            DOCS_ACCESS_CHOICES
        ))
    if 'document_root' not in kwargs and not DOCS_ROOT:
        raise DocsRootSettingError('DOCS_ROOT setting value is incorrect: %s (must be a valid path)' % DOCS_ROOT)
    if 'document_root' not in kwargs and DOCS_ROOT:
        kwargs['document_root'] = DOCS_ROOT
    return serve(request, path, **kwargs)

class DocsRootView(RedirectView):
    def get_redirect_url(self, **kwargs):
        return reverse('docs_files', kwargs={'path': 'index.html'})

<commit_msg>Django 2.0 support<commit_after>from django.views.generic import RedirectView

try:
    from django.core.urlresolvers import reverse
except ImportError:
    from django.urls import reverse

from django.views.static import serve
from django.conf import settings
from django.contrib.auth.decorators import login_required
from functools import wraps
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.auth.views import login
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.utils.translation import ugettext as _

def superuser_required(view_func):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, displaying the login page if necessary.
    """
    @wraps(view_func)
    def _checklogin(request, *args, **kwargs):
        if request.user.is_active and request.user.is_superuser:
            # The user is valid. Continue to the admin page.
            return view_func(request, *args, **kwargs)

        assert hasattr(request, 'session'), "The Django admin requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
        defaults = {
            'template_name': 'admin/login.html',
            'authentication_form': AdminAuthenticationForm,
            'extra_context': {
                'title': _('Log in'),
                'app_path': request.get_full_path(),
                REDIRECT_FIELD_NAME: request.get_full_path(),
                },
            }
        return login(request, **defaults)
    return _checklogin

def public(function=None):
    """
    Dummy decorator that doesn't check anything.
    """
    return function

class DocsAccessSettingError(ValueError):
    pass

class DocsRootSettingError(ValueError):
    pass

DOCS_ACCESS_CHOICES = (
    'public',
    'login_required',
    'staff',
    'superuser',
)
DOCS_ROOT = getattr(settings, 'DOCS_ROOT', None)
DOCS_ACCESS = getattr(settings, 'DOCS_ACCESS', DOCS_ACCESS_CHOICES[0])

if DOCS_ACCESS == 'public':
    decorator = public
elif DOCS_ACCESS == 'login_required':
    decorator = login_required
elif DOCS_ACCESS == 'staff':
    decorator = staff_member_required
elif DOCS_ACCESS == 'superuser':
    decorator = superuser_required
else:
    decorator = public

@decorator
def serve_docs(request, path, **kwargs):
    if DOCS_ACCESS not in DOCS_ACCESS_CHOICES:
        raise DocsAccessSettingError('DOCS_ACCESS setting value is incorrect: %s (choises are: %s)' % (
            DOCS_ACCESS,
            DOCS_ACCESS_CHOICES
        ))
    if 'document_root' not in kwargs and not DOCS_ROOT:
        raise DocsRootSettingError('DOCS_ROOT setting value is incorrect: %s (must be a valid path)' % DOCS_ROOT)
    if 'document_root' not in kwargs and DOCS_ROOT:
        kwargs['document_root'] = DOCS_ROOT
    return serve(request, path, **kwargs)

class DocsRootView(RedirectView):
    def get_redirect_url(self, **kwargs):
        return reverse('docs_files', kwargs={'path': 'index.html'})

<|endoftext|>


<commit_before># encoding: utf-8

"""
The |Table| object and related proxy classes.
"""

from __future__ import absolute_import, print_function, unicode_literals

from .blkcntnr import BlockItemContainer
from .oxml.simpletypes import ST_Merge
from .shared import lazyproperty, Parented

class Table(Parented):
    """
    Proxy class for a WordprocessingML ``<w:tbl>`` element.
    """
    def __init__(self, tbl, parent):
        super(Table, self).__init__(parent)
        self._tbl = tbl

    def add_column(self):
        """
        Return a |_Column| instance, newly added rightmost to the table.
        """
        tblGrid = self._tbl.tblGrid
        gridCol = tblGrid.add_gridCol()
        for tr in self._tbl.tr_lst:
            tr.add_tc()
        return _Column(gridCol, self._tbl, self)

    def add_row(self):
        """
        Return a |_Row| instance, newly added bottom-most to the table.
        """
        tbl = self._tbl
        tr = tbl.add_tr()
        for gridCol in tbl.tblGrid.gridCol_lst:
            tr.add_tc()
        return _Row(tr, self)

    @property
    def autofit(self):
        """
        |True| if column widths can be automatically adjusted to improve the
        fit of cell contents. |False| if table layout is fixed. Column widths
        are adjusted in either case if total column width exceeds page width.
        Read/write boolean.
        """
        return self._tblPr.autofit

    @autofit.setter
    def autofit(self, value):
        self._tblPr.autofit = value

    def cell(self, row_idx, col_idx):
        """
        Return |_Cell| instance correponding to table cell at *row_idx*,
        *col_idx* intersection, where (0, 0) is the top, left-most cell.
        """
        row = self.rows[row_idx]
        return row.cells[col_idx]

    def column_cells(self, column_idx):
        """
        Sequence of cells in the column at *column_idx* in this table.
        """
        cells = self._cells
        idxs = range(column_idx, len(cells), self._column_count)
        return [cells[idx] for idx in idxs]

    @lazyproperty
    def columns(self):
        """
        |_Columns| instance containing the sequence of rows in this table.
        """
        return _Columns(self._tbl, self)

    def row_cells(self, row_idx):
        """
        Sequence of cells in the row at *row_idx* in this table.
        """
        column_count = self._column_count
        start = row_idx * column_count
        end = start + column_count
        return self._cells[start:end]

    @lazyproperty
    def rows(self):
        """
        |_Rows| instance containing the sequence of rows in this table.
        """
        return _Rows(self._tbl, self)

    @property
    def style(self):
        """
        String name of style to be applied to this table, e.g.
        'LightShading-Accent1'. Name is derived by removing spaces from the
        table style name displayed in the Word UI.
        """
        return self._tblPr.style

    @style.setter
    def style(self, value):
        self._tblPr.style = value

    @property
    def table(self):
        """
        Provide child objects with reference to the |Table| object they
        belong to, without them having to know their direct parent is
        a |Table| object. This is the terminus of a series of `parent._table`
        calls from an arbitrary child through its ancestors.
        """
        return self

    @property
    def _cells(self):
        """
        A sequence of |_Cell| objects, one for each cell of the layout grid.
        If the table contains a span, one or more |_Cell| object references
        are repeated.
        """
        col_count = self._column_count
        cells = []
        for tc in self._tbl.iter_tcs():
            for grid_span_idx in range(tc.grid_span):
                if tc.vMerge == ST_Merge.CONTINUE:
                    cells.append(cells[-col_count])
                elif grid_span_idx > 0:
                    cells.append(cells[-1])
                else:
                    cells.append(_Cell(tc, self))
        return cells

    @property
    def _column_count(self):
        """
        The number of grid columns in this table.
        """
        return self._tbl.col_count

    @property
    def _tblPr(self):
        return self._tbl.tblPr

class _Cell(BlockItemContainer):
    """
    Table cell
    """
    def __init__(self, tc, parent):
        super(_Cell, self).__init__(tc, parent)
        self._tc = tc

    def add_paragraph(self, text='', style=None):
        """
        Return a paragraph newly added to the end of the content in this
        cell. If present, *text* is added to the paragraph in a single run.
        If specified, the paragraph style *style* is applied. If *style* is
        not specified or is |None|, the result is as though the 'Normal'
        style was applied. Note that the formatting of text in a cell can be
        influenced by the table style. *text* can contain tab (``\\t``)
        characters, which are converted to the appropriate XML form for
        a tab. *text* can also include newline (``\\n``) or carriage return
        (``\\r``) characters, each of which is converted to a line break.
        """
        return super(_Cell, self).add_paragraph(text, style)

    def add_table(self, rows, cols):
        """
        Return a table newly added to this cell after any existing cell
        content, having *rows* rows and *cols* columns. An empty paragraph is
        added after the table because Word requires a paragraph element as
        the last element in every cell.
        """
        new_table = super(_Cell, self).add_table(rows, cols)
        self.add_paragraph()
        return new_table

    @property
    def paragraphs(self):
        """
        List of paragraphs in the cell. A table cell is required to contain
        at least one block-level element and end with a paragraph. By
        default, a new cell contains a single paragraph. Read-only
        """
        return super(_Cell, self).paragraphs

    @property
    def tables(self):
        """
        List of tables in the cell, in the order they appear. Read-only.
        """
        return super(_Cell, self).tables

    @property
    def text(self):
        """
        The entire contents of this cell as a string of text. Assigning
        a string to this property replaces all existing content with a single
        paragraph containing the assigned text in a single run.
        """
        return '\n'.join(p.text for p in self.paragraphs)

    @text.setter
    def text(self, text):
        """
        Write-only. Set entire contents of cell to the string *text*. Any
        existing content or revisions are replaced.
        """
        tc = self._tc
        tc.clear_content()
        p = tc.add_p()
        r = p.add_r()
        r.text = text

    @property
    def width(self):
        """
        The width of this cell in EMU, or |None| if no explicit width is set.
        """
        return self._tc.width

    @width.setter
    def width(self, value):
        self._tc.width = value

class _Column(Parented):
    """
    Table column
    """
    def __init__(self, gridCol, tbl, parent):
        super(_Column, self).__init__(parent)
        self._gridCol = gridCol
        self._tbl = tbl

    @property
    def cells(self):
        """
        Sequence of |_Cell| instances corresponding to cells in this column.
        """
        return tuple(self.table.column_cells(self._index))

    @property
    def table(self):
        """
        Reference to the |Table| object this column belongs to.
        """
        return self._parent.table

    @property
    def width(self):
        """
        The width of this column in EMU, or |None| if no explicit width is
        set.
        """
        return self._gridCol.w

    @width.setter
    def width(self, value):
        self._gridCol.w = value

    @property
    def _index(self):
        """
        Index of this column in its table, starting from zero.
        """
        return self._gridCol.gridCol_idx

class _Columns(Parented):
    """
    Sequence of |_Column| instances corresponding to the columns in a table.
    Supports ``len()``, iteration and indexed access.
    """
    def __init__(self, tbl, parent):
        super(_Columns, self).__init__(parent)
        self._tbl = tbl

    def __getitem__(self, idx):
        """
        Provide indexed access, e.g. 'columns[0]'
        """
        try:
            gridCol = self._gridCol_lst[idx]
        except IndexError:
            msg = "column index [%d] is out of range" % idx
            raise IndexError(msg)
        return _Column(gridCol, self._tbl, self)

    def __iter__(self):
        for gridCol in self._gridCol_lst:
            yield _Column(gridCol, self._tbl, self)

    def __len__(self):
        return len(self._gridCol_lst)

    @property
    def table(self):
        """
        Reference to the |Table| object this column collection belongs to.
        """
        return self._parent.table

    @property
    def _gridCol_lst(self):
        """
        Sequence containing ``<w:gridCol>`` elements for this table, each
        representing a table column.
        """
        tblGrid = self._tbl.tblGrid
        return tblGrid.gridCol_lst

class _Row(Parented):
    """
    Table row
    """
    def __init__(self, tr, parent):
        super(_Row, self).__init__(parent)
        self._tr = tr

    @property
    def cells(self):
        """
        Sequence of |_Cell| instances corresponding to cells in this row.
        """
        return tuple(self.table.row_cells(self._index))

    @property
    def table(self):
        """
        Reference to the |Table| object this row belongs to.
        """
        return self._parent.table

    @property
    def _index(self):
        """
        Index of this row in its table, starting from zero.
        """
        return self._tr.tr_idx

class _Rows(Parented):
    """
    Sequence of |_Row| instances corresponding to the rows in a table.
    Supports ``len()``, iteration and indexed access.
    """
    def __init__(self, tbl, parent):
        super(_Rows, self).__init__(parent)
        self._tbl = tbl

    def __getitem__(self, idx):
        """
        Provide indexed access, (e.g. 'rows[0]')
        """
        try:
            tr = self._tbl.tr_lst[idx]
        except IndexError:
            msg = "row index [%d] out of range" % idx
            raise IndexError(msg)
        return _Row(tr, self)

    def __iter__(self):
        return (_Row(tr, self) for tr in self._tbl.tr_lst)

    def __len__(self):
        return len(self._tbl.tr_lst)

    @property
    def table(self):
        """
        Reference to the |Table| object this row collection belongs to.
        """
        return self._parent.table
<commit_msg>tbl: refactor Table.cell()<commit_after># encoding: utf-8

"""
The |Table| object and related proxy classes.
"""

from __future__ import absolute_import, print_function, unicode_literals

from .blkcntnr import BlockItemContainer
from .oxml.simpletypes import ST_Merge
from .shared import lazyproperty, Parented

class Table(Parented):
    """
    Proxy class for a WordprocessingML ``<w:tbl>`` element.
    """
    def __init__(self, tbl, parent):
        super(Table, self).__init__(parent)
        self._tbl = tbl

    def add_column(self):
        """
        Return a |_Column| instance, newly added rightmost to the table.
        """
        tblGrid = self._tbl.tblGrid
        gridCol = tblGrid.add_gridCol()
        for tr in self._tbl.tr_lst:
            tr.add_tc()
        return _Column(gridCol, self._tbl, self)

    def add_row(self):
        """
        Return a |_Row| instance, newly added bottom-most to the table.
        """
        tbl = self._tbl
        tr = tbl.add_tr()
        for gridCol in tbl.tblGrid.gridCol_lst:
            tr.add_tc()
        return _Row(tr, self)

    @property
    def autofit(self):
        """
        |True| if column widths can be automatically adjusted to improve the
        fit of cell contents. |False| if table layout is fixed. Column widths
        are adjusted in either case if total column width exceeds page width.
        Read/write boolean.
        """
        return self._tblPr.autofit

    @autofit.setter
    def autofit(self, value):
        self._tblPr.autofit = value

    def cell(self, row_idx, col_idx):
        """
        Return |_Cell| instance correponding to table cell at *row_idx*,
        *col_idx* intersection, where (0, 0) is the top, left-most cell.
        """
        cell_idx = col_idx + (row_idx * self._column_count)
        return self._cells[cell_idx]

    def column_cells(self, column_idx):
        """
        Sequence of cells in the column at *column_idx* in this table.
        """
        cells = self._cells
        idxs = range(column_idx, len(cells), self._column_count)
        return [cells[idx] for idx in idxs]

    @lazyproperty
    def columns(self):
        """
        |_Columns| instance containing the sequence of rows in this table.
        """
        return _Columns(self._tbl, self)

    def row_cells(self, row_idx):
        """
        Sequence of cells in the row at *row_idx* in this table.
        """
        column_count = self._column_count
        start = row_idx * column_count
        end = start + column_count
        return self._cells[start:end]

    @lazyproperty
    def rows(self):
        """
        |_Rows| instance containing the sequence of rows in this table.
        """
        return _Rows(self._tbl, self)

    @property
    def style(self):
        """
        String name of style to be applied to this table, e.g.
        'LightShading-Accent1'. Name is derived by removing spaces from the
        table style name displayed in the Word UI.
        """
        return self._tblPr.style

    @style.setter
    def style(self, value):
        self._tblPr.style = value

    @property
    def table(self):
        """
        Provide child objects with reference to the |Table| object they
        belong to, without them having to know their direct parent is
        a |Table| object. This is the terminus of a series of `parent._table`
        calls from an arbitrary child through its ancestors.
        """
        return self

    @property
    def _cells(self):
        """
        A sequence of |_Cell| objects, one for each cell of the layout grid.
        If the table contains a span, one or more |_Cell| object references
        are repeated.
        """
        col_count = self._column_count
        cells = []
        for tc in self._tbl.iter_tcs():
            for grid_span_idx in range(tc.grid_span):
                if tc.vMerge == ST_Merge.CONTINUE:
                    cells.append(cells[-col_count])
                elif grid_span_idx > 0:
                    cells.append(cells[-1])
                else:
                    cells.append(_Cell(tc, self))
        return cells

    @property
    def _column_count(self):
        """
        The number of grid columns in this table.
        """
        return self._tbl.col_count

    @property
    def _tblPr(self):
        return self._tbl.tblPr

class _Cell(BlockItemContainer):
    """
    Table cell
    """
    def __init__(self, tc, parent):
        super(_Cell, self).__init__(tc, parent)
        self._tc = tc

    def add_paragraph(self, text='', style=None):
        """
        Return a paragraph newly added to the end of the content in this
        cell. If present, *text* is added to the paragraph in a single run.
        If specified, the paragraph style *style* is applied. If *style* is
        not specified or is |None|, the result is as though the 'Normal'
        style was applied. Note that the formatting of text in a cell can be
        influenced by the table style. *text* can contain tab (``\\t``)
        characters, which are converted to the appropriate XML form for
        a tab. *text* can also include newline (``\\n``) or carriage return
        (``\\r``) characters, each of which is converted to a line break.
        """
        return super(_Cell, self).add_paragraph(text, style)

    def add_table(self, rows, cols):
        """
        Return a table newly added to this cell after any existing cell
        content, having *rows* rows and *cols* columns. An empty paragraph is
        added after the table because Word requires a paragraph element as
        the last element in every cell.
        """
        new_table = super(_Cell, self).add_table(rows, cols)
        self.add_paragraph()
        return new_table

    @property
    def paragraphs(self):
        """
        List of paragraphs in the cell. A table cell is required to contain
        at least one block-level element and end with a paragraph. By
        default, a new cell contains a single paragraph. Read-only
        """
        return super(_Cell, self).paragraphs

    @property
    def tables(self):
        """
        List of tables in the cell, in the order they appear. Read-only.
        """
        return super(_Cell, self).tables

    @property
    def text(self):
        """
        The entire contents of this cell as a string of text. Assigning
        a string to this property replaces all existing content with a single
        paragraph containing the assigned text in a single run.
        """
        return '\n'.join(p.text for p in self.paragraphs)

    @text.setter
    def text(self, text):
        """
        Write-only. Set entire contents of cell to the string *text*. Any
        existing content or revisions are replaced.
        """
        tc = self._tc
        tc.clear_content()
        p = tc.add_p()
        r = p.add_r()
        r.text = text

    @property
    def width(self):
        """
        The width of this cell in EMU, or |None| if no explicit width is set.
        """
        return self._tc.width

    @width.setter
    def width(self, value):
        self._tc.width = value

class _Column(Parented):
    """
    Table column
    """
    def __init__(self, gridCol, tbl, parent):
        super(_Column, self).__init__(parent)
        self._gridCol = gridCol
        self._tbl = tbl

    @property
    def cells(self):
        """
        Sequence of |_Cell| instances corresponding to cells in this column.
        """
        return tuple(self.table.column_cells(self._index))

    @property
    def table(self):
        """
        Reference to the |Table| object this column belongs to.
        """
        return self._parent.table

    @property
    def width(self):
        """
        The width of this column in EMU, or |None| if no explicit width is
        set.
        """
        return self._gridCol.w

    @width.setter
    def width(self, value):
        self._gridCol.w = value

    @property
    def _index(self):
        """
        Index of this column in its table, starting from zero.
        """
        return self._gridCol.gridCol_idx

class _Columns(Parented):
    """
    Sequence of |_Column| instances corresponding to the columns in a table.
    Supports ``len()``, iteration and indexed access.
    """
    def __init__(self, tbl, parent):
        super(_Columns, self).__init__(parent)
        self._tbl = tbl

    def __getitem__(self, idx):
        """
        Provide indexed access, e.g. 'columns[0]'
        """
        try:
            gridCol = self._gridCol_lst[idx]
        except IndexError:
            msg = "column index [%d] is out of range" % idx
            raise IndexError(msg)
        return _Column(gridCol, self._tbl, self)

    def __iter__(self):
        for gridCol in self._gridCol_lst:
            yield _Column(gridCol, self._tbl, self)

    def __len__(self):
        return len(self._gridCol_lst)

    @property
    def table(self):
        """
        Reference to the |Table| object this column collection belongs to.
        """
        return self._parent.table

    @property
    def _gridCol_lst(self):
        """
        Sequence containing ``<w:gridCol>`` elements for this table, each
        representing a table column.
        """
        tblGrid = self._tbl.tblGrid
        return tblGrid.gridCol_lst

class _Row(Parented):
    """
    Table row
    """
    def __init__(self, tr, parent):
        super(_Row, self).__init__(parent)
        self._tr = tr

    @property
    def cells(self):
        """
        Sequence of |_Cell| instances corresponding to cells in this row.
        """
        return tuple(self.table.row_cells(self._index))

    @property
    def table(self):
        """
        Reference to the |Table| object this row belongs to.
        """
        return self._parent.table

    @property
    def _index(self):
        """
        Index of this row in its table, starting from zero.
        """
        return self._tr.tr_idx

class _Rows(Parented):
    """
    Sequence of |_Row| instances corresponding to the rows in a table.
    Supports ``len()``, iteration and indexed access.
    """
    def __init__(self, tbl, parent):
        super(_Rows, self).__init__(parent)
        self._tbl = tbl

    def __getitem__(self, idx):
        """
        Provide indexed access, (e.g. 'rows[0]')
        """
        try:
            tr = self._tbl.tr_lst[idx]
        except IndexError:
            msg = "row index [%d] out of range" % idx
            raise IndexError(msg)
        return _Row(tr, self)

    def __iter__(self):
        return (_Row(tr, self) for tr in self._tbl.tr_lst)

    def __len__(self):
        return len(self._tbl.tr_lst)

    @property
    def table(self):
        """
        Reference to the |Table| object this row collection belongs to.
        """
        return self._parent.table
<|endoftext|>


<commit_before>"""empty message

Revision ID: 0085_set_international
Revises: 0084_add_job_stats
Create Date: 2017-05-05 15:26:34.621670

"""
from datetime import datetime
from alembic import op

# revision identifiers, used by Alembic.
revision = '0085_set_international'
down_revision = '0084_add_job_stats'

def upgrade():
    conn = op.get_bind()
    start = datetime.utcnow()
    notification_history = "select id from notification_history where international is null limit 10000"

    results = conn.execute(notification_history)
    res = results.fetchall()

    while len(res) > 0:
        conn.execute("update notification_history set international = False where id in ({})".format(
            notification_history))
        results = conn.execute(notification_history)
        res = results.fetchall()

    notifications = "select id from notifications where international is null limit 10000"
    results2 = conn.execute(notifications)
    res2 = results2.fetchall()
    while len(res2) > 0:
        conn.execute("update notifications set international = False where id in ({})".format(notifications))

        results2 = conn.execute(notifications)
        res2 = results2.fetchall()

    end = datetime.utcnow()
    print("Started at: {}   ended at: {}, taking {}".format(start, end, end-start))

def downgrade():
    # There is no way to downgrade this update.
    pass
<commit_msg>Revert "Script to update the international flag from null to false"<commit_after><|endoftext|>


<commit_before># Copyright 2014 CloudFounders NV
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Native Injector module
"""
import os
from ConfigParser import RawConfigParser
from subprocess import check_output, CalledProcessError

class Injector(object):
    """
    Injector class, provides all logic to inject. However, the unittest injector
    only provides functionality required in the unittests
    """

    def __init__(self):
        """
        This class should be fully static
        """
        raise RuntimeError('This class should not be instantiated.')

    @staticmethod
    def inject_configuration(provider):
        """ Injects the Config module """
        def _get(key):
            filename, section, item = key.split('.', 2)
            config = RawConfigParser()
            config.read('/opt/OpenvStorage/config/{0}.cfg'.format(filename))
            return config.get(section, item)

        def _set(key, value):
            filename, section, item = key.split('.', 2)
            config = RawConfigParser()
            config.read('/opt/OpenvStorage/config/{0}.cfg'.format(filename))
            config.set(section, item, value)
            with open('/opt/OpenvStorage/config/{0}.cfg'.format(filename), 'w') as config_file:
                config.write(config_file)

        def _get_int(key):
            return int(_get(key))

        provider.get = staticmethod(_get)
        provider.getInt = staticmethod(_get_int)
        provider.set = staticmethod(_set)
        return provider

    @staticmethod
    def inject_remote(provider):
        """ Injects the remote module """
        class Cuisine:
            import cuisine
            import fabric

            api = cuisine
            fabric = fabric.api

        provider.fabric = Cuisine().fabric
        provider.cuisine = Cuisine()

        return provider

    @staticmethod
    def inject_service(provider):
        """ Injects the Service module """

        def _service_exists(name, path=None):
            if path is None:
                path = '/etc/init/'
            return os.path.exists('{0}{1}.conf'.format(path, name))

        def _get_name(name, path=None):
            if _service_exists(name, path):
                return name
            name = 'ovs-{0}'.format(name)
            if _service_exists(name, path):
                return name
            raise ValueError('Service {0} could not be found.'.format(name))

        def add_service(package, name, command, stop_command, params=None):
            _ = package, command, stop_command
            if params is None:
                params = {}

            name = _get_name(name, '/opt/OpenvStorage/config/templates/upstart/')
            template_dir = '/opt/OpenvStorage/config/templates/upstart/{0}'
            upstart_dir = '/etc/init/{0}'
            upstart_conf = '{0}.conf'.format(name)
            with open(template_dir.format(upstart_conf), 'r') as template_file:
                template_conf = template_file.read()

            for key, value in params.iteritems():
                print 'replacing {0} by {1}'.format(key, value)
                template_conf = template_conf.replace(key, value)

            print '\n\n\n service {0} configfile \n {1}'.format(name, template_conf)
            with open(upstart_dir.format(upstart_conf), 'wb') as upstart_file:
                upstart_file.write(template_conf)

        def get_service_status(name):
            try:
                name = _get_name(name)
                output = check_output('status {0}'.format(name), shell=True)
                if 'start' in output:
                    return True
                if 'stop' in output:
                    return False
            except CalledProcessError:
                pass
            return None

        def remove_service(domain, name):
            _ = domain
            # remove upstart.conf file
            name = _get_name(name)
            check_output('rm -rf /etc/init/{0}.conf'.format(name), shell=True)
            check_output('rm -rf /etc/init/{0}.override'.format(name), shell=True)

        def disable_service(name):
            name = _get_name(name)
            check_output('echo "manual" > /etc/init/{0}.override'.format(name), shell=True)

        def enable_service(name):
            name = _get_name(name)
            check_output('rm -f /etc/init/{0}.override'.format(name), shell=True)

        def start_service(name):
            try:
                name = _get_name(name)
                output = check_output('start {0}'.format(name), shell=True)
            except CalledProcessError as cpe:
                output = cpe.output
            return output

        def stop_service(name):
            try:
                name = _get_name(name)
                output = check_output('stop {0}'.format(name), shell=True)
            except CalledProcessError as cpe:
                output = cpe.output
            return output

        def restart_service(name):
            try:
                name = _get_name(name)
                output = check_output('restart {0}'.format(name), shell=True)
            except CalledProcessError as cpe:
                output = cpe.output
            return output

        def has_service(name):
            try:
                _get_name(name)
                return True
            except ValueError:
                return False

        provider.add_service = staticmethod(add_service)
        provider.remove_service = staticmethod(remove_service)
        provider.get_service_status = staticmethod(get_service_status)
        provider.disable_service = staticmethod(disable_service)
        provider.enable_service = staticmethod(enable_service)
        provider.start_service = staticmethod(start_service)
        provider.stop_service = staticmethod(stop_service)
        provider.restart_service = staticmethod(restart_service)
        provider.has_service = staticmethod(has_service)
        return provider

    @staticmethod
    def inject_process(provider):
        """ Injects the Process module """

        def check_process(name):
            output = check_output('ps aux | grep -v grep | grep {0} || true'.format(name), shell = True)
            # It returns 1 if the process is not running, else it returns 0. Don't ask questions...
            return 1 if name not in output else 0

        provider.checkProcess = staticmethod(check_process)
        return provider

    @staticmethod
    def inject_package(provider):
        """ Injects the Package module """

        def _get_version(package):
            return check_output("dpkg -s {0} | grep Version | cut -d ' ' -f 2".format(package), shell=True).strip()

        def get_versions():
            versions = {}
            for package in ['openvstorage', 'openvstorage-backend', 'volumedriver-server', 'volumedriver-base', 'alba', 'alba-asdmanager']:
                version_info = _get_version(package)
                if version_info:
                    versions[package] = version_info
            return versions

        provider.get_versions = staticmethod(get_versions)
        return provider
<commit_msg>OVS-2042 - Rename packages<commit_after># Copyright 2014 CloudFounders NV
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Native Injector module
"""
import os
from ConfigParser import RawConfigParser
from subprocess import check_output, CalledProcessError

class Injector(object):
    """
    Injector class, provides all logic to inject. However, the unittest injector
    only provides functionality required in the unittests
    """

    def __init__(self):
        """
        This class should be fully static
        """
        raise RuntimeError('This class should not be instantiated.')

    @staticmethod
    def inject_configuration(provider):
        """ Injects the Config module """
        def _get(key):
            filename, section, item = key.split('.', 2)
            config = RawConfigParser()
            config.read('/opt/OpenvStorage/config/{0}.cfg'.format(filename))
            return config.get(section, item)

        def _set(key, value):
            filename, section, item = key.split('.', 2)
            config = RawConfigParser()
            config.read('/opt/OpenvStorage/config/{0}.cfg'.format(filename))
            config.set(section, item, value)
            with open('/opt/OpenvStorage/config/{0}.cfg'.format(filename), 'w') as config_file:
                config.write(config_file)

        def _get_int(key):
            return int(_get(key))

        provider.get = staticmethod(_get)
        provider.getInt = staticmethod(_get_int)
        provider.set = staticmethod(_set)
        return provider

    @staticmethod
    def inject_remote(provider):
        """ Injects the remote module """
        class Cuisine:
            import cuisine
            import fabric

            api = cuisine
            fabric = fabric.api

        provider.fabric = Cuisine().fabric
        provider.cuisine = Cuisine()

        return provider

    @staticmethod
    def inject_service(provider):
        """ Injects the Service module """

        def _service_exists(name, path=None):
            if path is None:
                path = '/etc/init/'
            return os.path.exists('{0}{1}.conf'.format(path, name))

        def _get_name(name, path=None):
            if _service_exists(name, path):
                return name
            name = 'ovs-{0}'.format(name)
            if _service_exists(name, path):
                return name
            raise ValueError('Service {0} could not be found.'.format(name))

        def add_service(package, name, command, stop_command, params=None):
            _ = package, command, stop_command
            if params is None:
                params = {}

            name = _get_name(name, '/opt/OpenvStorage/config/templates/upstart/')
            template_dir = '/opt/OpenvStorage/config/templates/upstart/{0}'
            upstart_dir = '/etc/init/{0}'
            upstart_conf = '{0}.conf'.format(name)
            with open(template_dir.format(upstart_conf), 'r') as template_file:
                template_conf = template_file.read()

            for key, value in params.iteritems():
                print 'replacing {0} by {1}'.format(key, value)
                template_conf = template_conf.replace(key, value)

            print '\n\n\n service {0} configfile \n {1}'.format(name, template_conf)
            with open(upstart_dir.format(upstart_conf), 'wb') as upstart_file:
                upstart_file.write(template_conf)

        def get_service_status(name):
            try:
                name = _get_name(name)
                output = check_output('status {0}'.format(name), shell=True)
                if 'start' in output:
                    return True
                if 'stop' in output:
                    return False
            except CalledProcessError:
                pass
            return None

        def remove_service(domain, name):
            _ = domain
            # remove upstart.conf file
            name = _get_name(name)
            check_output('rm -rf /etc/init/{0}.conf'.format(name), shell=True)
            check_output('rm -rf /etc/init/{0}.override'.format(name), shell=True)

        def disable_service(name):
            name = _get_name(name)
            check_output('echo "manual" > /etc/init/{0}.override'.format(name), shell=True)

        def enable_service(name):
            name = _get_name(name)
            check_output('rm -f /etc/init/{0}.override'.format(name), shell=True)

        def start_service(name):
            try:
                name = _get_name(name)
                output = check_output('start {0}'.format(name), shell=True)
            except CalledProcessError as cpe:
                output = cpe.output
            return output

        def stop_service(name):
            try:
                name = _get_name(name)
                output = check_output('stop {0}'.format(name), shell=True)
            except CalledProcessError as cpe:
                output = cpe.output
            return output

        def restart_service(name):
            try:
                name = _get_name(name)
                output = check_output('restart {0}'.format(name), shell=True)
            except CalledProcessError as cpe:
                output = cpe.output
            return output

        def has_service(name):
            try:
                _get_name(name)
                return True
            except ValueError:
                return False

        provider.add_service = staticmethod(add_service)
        provider.remove_service = staticmethod(remove_service)
        provider.get_service_status = staticmethod(get_service_status)
        provider.disable_service = staticmethod(disable_service)
        provider.enable_service = staticmethod(enable_service)
        provider.start_service = staticmethod(start_service)
        provider.stop_service = staticmethod(stop_service)
        provider.restart_service = staticmethod(restart_service)
        provider.has_service = staticmethod(has_service)
        return provider

    @staticmethod
    def inject_process(provider):
        """ Injects the Process module """

        def check_process(name):
            output = check_output('ps aux | grep -v grep | grep {0} || true'.format(name), shell = True)
            # It returns 1 if the process is not running, else it returns 0. Don't ask questions...
            return 1 if name not in output else 0

        provider.checkProcess = staticmethod(check_process)
        return provider

    @staticmethod
    def inject_package(provider):
        """ Injects the Package module """

        def _get_version(package):
            return check_output("dpkg -s {0} | grep Version | cut -d ' ' -f 2".format(package), shell=True).strip()

        def get_versions():
            versions = {}
            for package in ['openvstorage', 'openvstorage-backend', 'volumedriver-server', 'volumedriver-base', 'alba', 'openvstorage-sdm']:
                version_info = _get_version(package)
                if version_info:
                    versions[package] = version_info
            return versions

        provider.get_versions = staticmethod(get_versions)
        return provider
<|endoftext|>


<commit_before>#
# Copyright (c) 2010 rPath, Inc.
#
# All Rights Reserved
#
import sys
import datetime
from dateutil import tz

from django.db import models
from django.core import exceptions

from mint.django_rest.rbuilder import modellib
from mint.django_rest.rbuilder import models as rbuildermodels

from xobj import xobj

class Inventory(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'inventory',
                elements = ['managementNodes', 'systems', 'log', "eventTypes"])

    def __init__(self):
        self.managementNodes = modellib.XObjHrefModel('managementNodes/')
        self.systems = modellib.XObjHrefModel('systems/')
        self.log = modellib.XObjHrefModel('log/')
        self.eventTypes = modellib.XObjHrefModel('eventTypes/')

class Systems(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'systems',
                elements=['system'])
    list_fields = ['system']
    system = []

    def save(self):
        return [s.save() for s in self.system]

class ManagementNodes(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'managementNodes',
                elements=['managementNode'])
    list_fields = ['managementNode']
    managementNode = []

    def save(self):
        return [s.save() for s in self.managementNode]

class EventTypes(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'eventTypes',
                elements=['eventType'])
    list_fields = ['eventType']
    eventType = []

    def save(self):
        return [s.save() for s in self.eventType]

class SystemsLog(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag='systemsLog')
    list_fields = ['systemLogEntry']
    systemLogEntry = []

class SystemEvents(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'systemEvents')
    list_fields = ['systemEvent']
    systemEvent = []

    def save(self):
        return [s.save() for s in self.systemEvent]

class Networks(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag='networks',
                elements=['network'])
    list_fields = ['network']

class System(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_system'
    _xobj = xobj.XObjMetadata(
                tag = 'system',
                attributes = {'id':str},
                elements = ['networks'])

    # need our own object manager for dup detection
    objects = modellib.SystemManager()

    UNMANAGED = "unmanaged"
    ACTIVATED = "activated"
    RESPONSIVE = "responsive"
    SHUTDOWN = "shutdown"
    NONRESPONSIVE = "non-responsive"
    DEAD = "dead"
    MOTHBALLED = "mothballed"

    system_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=8092)
    description = models.CharField(max_length=8092, null=True)
    created_date = modellib.DateTimeUtcField(auto_now_add=True)
    # Launch date is nullable, we may get it reported from the hypervisor or
    # physical target, we may not.
    launch_date = modellib.DateTimeUtcField(null=True)
    target = models.ForeignKey(rbuildermodels.Targets, null=True)
    target_system_id = models.CharField(max_length=255, null=True)
    reservation_id = models.CharField(max_length=255, null=True)
    os_type = models.CharField(max_length=64, null=True)
    os_major_version = models.CharField(max_length=32, null=True)
    os_minor_version = models.CharField(max_length=32, null=True)
    activation_date = modellib.DateTimeUtcField(null=True)
    generated_uuid = models.CharField(max_length=64, unique=True, null=True)
    local_uuid = models.CharField(max_length=64, null=True)
    ssl_client_certificate = models.CharField(max_length=8092, null=True)
    ssl_client_key = models.CharField(max_length=8092, null=True)
    ssl_server_certificate = models.CharField(max_length=8092, null=True)
    scheduled_event_start_date = modellib.DateTimeUtcField(null=True)
    launching_user = models.ForeignKey(rbuildermodels.Users, null=True)
    available = models.NullBooleanField()
    activated = models.NullBooleanField()
    STATE_CHOICES = (
        (UNMANAGED, UNMANAGED),
        (ACTIVATED, ACTIVATED),
        (RESPONSIVE, RESPONSIVE),
        (SHUTDOWN, SHUTDOWN),
        (NONRESPONSIVE, NONRESPONSIVE),
        (DEAD, DEAD),
        (MOTHBALLED, MOTHBALLED),
    )
    current_state = models.CharField(max_length=32, choices=STATE_CHOICES, null=True)
    installed_software = models.ManyToManyField('Trove', null=True)
    is_management_node = models.NullBooleanField()
    # the management node managing this system.
    managing_node = models.ForeignKey('ManagementNode', null=True,
                        related_name='systems')

    load_fields = [local_uuid]

    def addJobs(self):
        return
        # Put these imports here for now so they don't break anything
        # globally.
        from rmake3 import client
        RMAKE_ADDRESS = 'http://localhost:9998'
        rmake_client = client.RmakeClient(RMAKE_ADDRESS)

        job_uuids = [sj.job_uuid for sj in self.system_jobs.all()]
        rmake_jobs = rmake_client.getJobs(job_uuids)

        for rmake_job in rmake_jobs:
            # TODO, make a models.Job instance

            # add it to the system
            pass

class EventType(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_event_type'

    # on-demand events need to be > 100 to be dispatched immediately
    # DO NOT CHANGE POLL PRIORITIES HERE WITHOUT CHANGING IN schema.py also
    ON_DEMAND_BASE = 100

    SYSTEM_POLL = "system poll"
    SYSTEM_POLL_PRIORITY = 50
    SYSTEM_POLL_DESC = "standard system polling event"

    SYSTEM_POLL_IMMEDIATE = "immediate system poll"
    SYSTEM_POLL_IMMEDIATE_PRIORITY = ON_DEMAND_BASE +5
    SYSTEM_POLL_IMMEDIATE_DESC = "on-demand system polling event"

    SYSTEM_ACTIVATION = "system activation"
    SYSTEM_ACTIVATION_PRIORITY = ON_DEMAND_BASE +10
    SYSTEM_ACTIVATION_DESC = "on-demand system activation event"

    event_type_id = models.AutoField(primary_key=True)
    EVENT_TYPES = (
        (SYSTEM_ACTIVATION, SYSTEM_ACTIVATION_DESC),
        (SYSTEM_POLL_IMMEDIATE, SYSTEM_POLL_IMMEDIATE_DESC),
        (SYSTEM_POLL, SYSTEM_POLL_DESC),
    )
    name = models.CharField(max_length=8092, db_index=True, choices=EVENT_TYPES)
    description = models.CharField(max_length=8092)
    priority = models.SmallIntegerField(db_index=True)

class Job(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_job'
    job_id = models.AutoField(primary_key=True)
    job_uuid = models.CharField(max_length=64)
    event_type = models.ForeignKey(EventType)
    time_created = modellib.DateTimeUtcField(auto_now_add=True)

class SystemEvent(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_system_event'
    _xobj = xobj.XObjMetadata(
                tag = 'systemEvent',
                attributes = {'id':str})

    system_event_id = models.AutoField(primary_key=True)
    system = modellib.DeferredForeignKey(System, db_index=True,
        related_name='system_events')
    event_type = modellib.DeferredForeignKey(EventType,
        related_name='system_events')
    time_created = modellib.DateTimeUtcField(auto_now_add=True)
    time_enabled = modellib.DateTimeUtcField(
        default=datetime.datetime.now(tz.tzutc()), db_index=True)
    priority = models.SmallIntegerField(db_index=True)

    def dispatchImmediately(self):
        return self.event_type.priority >= EventType.ON_DEMAND_BASE

    def get_absolute_url(self, request, parent=None):
        if isinstance(parent, EventType):
            self.view_name = 'SystemEventsByType'
        elif isinstance(parent, System):
            self.view_name = 'SystemsSystemEvent'
        return modellib.XObjIdModel.get_absolute_url(self, request, parent)

class ManagementNode(System):
    class Meta:
        db_table = 'inventory_management_node'
    _xobj = xobj.XObjMetadata(
                tag = 'managementNode',
                attributes = {'id':str})
    local = models.NullBooleanField()

    load_ignore_fields = ["system_ptr"]

    # need our own object manager for dup detection
    objects = modellib.ManagementNodeManager()

    def save(self, *args, **kw):
        self.is_management_node = True
        modellib.XObjModel.save(self, *args, **kw)

class Network(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_network'
    _xobj = xobj.XObjMetadata(
                tag='network')
    network_id = models.AutoField(primary_key=True)
    system = models.ForeignKey(System, related_name='networks')
    ip_address = models.CharField(max_length=15, null=True)
    # TODO: how long should this be?
    ipv6_address = models.CharField(max_length=32, null=True)
    device_name = models.CharField(max_length=255)
    public_dns_name = models.CharField(max_length=255, db_index=True)
    netmask = models.CharField(max_length=20, null=True)
    port_type = models.CharField(max_length=32, null=True)
    primary = models.NullBooleanField()
    # TODO: add all the other fields we need about a network

    load_fields = [ip_address, public_dns_name]

    def natural_key(self):
        return self.ip_address, self.public_dns_name

class SystemLog(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_system_log'
    system_log_id = models.AutoField(primary_key=True)
    system = modellib.DeferredForeignKey(System, related_name='system_log')

    def get_absolute_url(self, request, parent=None):
        if not parent:
            parent = self.system
        if isinstance(parent, System):
            view_name = 'SystemLog'
        return modellib.XObjIdModel.get_absolute_url(self, request, parent)

class SystemLogEntry(modellib.XObjModel):
    _xobj = xobj.XObjMetadata(
                tag='systemLogEntry')
    class Meta:
        db_table = 'inventory_system_log_entry'
        ordering = ['entry_date']

    ADDED = "System added to inventory"
    ACTIVATED = "System activated via ractivate"
    MANUALLY_ACTIVATED = "System manually activated via rBuilder"
    POLLED = "System polled."
    FETCHED = "System data fetched."
    choices = (
        (ADDED, ADDED),
        (ACTIVATED, ACTIVATED),
        (MANUALLY_ACTIVATED, MANUALLY_ACTIVATED),
        (POLLED, POLLED),
        (FETCHED, FETCHED),
    )

    system_log_entry_id = models.AutoField(primary_key=True)
    system_log = models.ForeignKey(SystemLog,
        related_name='system_log_entries')
    entry = models.CharField(max_length=8092, choices=choices)
    entry_date = modellib.DateTimeUtcField(auto_now_add=True)

class Trove(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_trove'
        unique_together = (('name', 'version', 'flavor'),)
    trove_id = models.AutoField(primary_key=True)
    name = models.TextField()
    version = modellib.SerializedForeignKey('Version')
    flavor = models.TextField()
    is_top_level = models.BooleanField()
    last_available_update_refresh = modellib.DateTimeUtcField(
        auto_now_add=True)
    available_updates = models.ManyToManyField('Version',
        related_name='available_updates')

    def _is_top_level_group(self):
        return self.name.startswith('group-') and \
            self.name.endswith('-appliance')

    def save(self, *args, **kw):
        if self._is_top_level_group():
            self.is_top_level = True
        else:
            self.is_top_level = False
        modellib.XObjModel.save(self, *args, **kw)

class Version(modellib.XObjModel):
    serialize_accessors = False
    class Meta:
        db_table = 'inventory_version'
    version_id = models.AutoField(primary_key=True)
    full = models.TextField()
    label = models.TextField()
    revision = models.TextField()
    ordering = models.TextField()
    flavor = models.TextField()

class SystemJob(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_system_job'
    system_job_id = models.AutoField(primary_key=True)
    system = models.ForeignKey(System)
    job = models.ForeignKey(Job)

class ErrorResponse(modellib.XObjModel):
    _xobj = xobj.XObjMetadata(
                tag='fault')
    class Meta:
        abstract = True
    code = models.TextField()
    message = models.TextField()
    traceback = models.TextField()
    product_code = models.TextField()

for mod_obj in sys.modules[__name__].__dict__.values():
    if hasattr(mod_obj, '_xobj'):
        if mod_obj._xobj.tag:
            modellib.type_map[mod_obj._xobj.tag] = mod_obj
for mod_obj in rbuildermodels.__dict__.values():
    if hasattr(mod_obj, '_meta'):
        modellib.type_map[mod_obj._meta.verbose_name] = mod_obj
<commit_msg>set priority on systemEvent based on eventType<commit_after>#
# Copyright (c) 2010 rPath, Inc.
#
# All Rights Reserved
#
import sys
import datetime
from dateutil import tz

from django.db import models
from django.core import exceptions

from mint.django_rest.rbuilder import modellib
from mint.django_rest.rbuilder import models as rbuildermodels

from xobj import xobj

class Inventory(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'inventory',
                elements = ['managementNodes', 'systems', 'log', "eventTypes"])

    def __init__(self):
        self.managementNodes = modellib.XObjHrefModel('managementNodes/')
        self.systems = modellib.XObjHrefModel('systems/')
        self.log = modellib.XObjHrefModel('log/')
        self.eventTypes = modellib.XObjHrefModel('eventTypes/')

class Systems(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'systems',
                elements=['system'])
    list_fields = ['system']
    system = []

    def save(self):
        return [s.save() for s in self.system]

class ManagementNodes(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'managementNodes',
                elements=['managementNode'])
    list_fields = ['managementNode']
    managementNode = []

    def save(self):
        return [s.save() for s in self.managementNode]

class EventTypes(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'eventTypes',
                elements=['eventType'])
    list_fields = ['eventType']
    eventType = []

    def save(self):
        return [s.save() for s in self.eventType]

class SystemsLog(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag='systemsLog')
    list_fields = ['systemLogEntry']
    systemLogEntry = []

class SystemEvents(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = 'systemEvents')
    list_fields = ['systemEvent']
    systemEvent = []

    def save(self):
        return [s.save() for s in self.systemEvent]

class Networks(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag='networks',
                elements=['network'])
    list_fields = ['network']

class System(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_system'
    _xobj = xobj.XObjMetadata(
                tag = 'system',
                attributes = {'id':str},
                elements = ['networks'])

    # need our own object manager for dup detection
    objects = modellib.SystemManager()

    UNMANAGED = "unmanaged"
    ACTIVATED = "activated"
    RESPONSIVE = "responsive"
    SHUTDOWN = "shutdown"
    NONRESPONSIVE = "non-responsive"
    DEAD = "dead"
    MOTHBALLED = "mothballed"

    system_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=8092)
    description = models.CharField(max_length=8092, null=True)
    created_date = modellib.DateTimeUtcField(auto_now_add=True)
    # Launch date is nullable, we may get it reported from the hypervisor or
    # physical target, we may not.
    launch_date = modellib.DateTimeUtcField(null=True)
    target = models.ForeignKey(rbuildermodels.Targets, null=True)
    target_system_id = models.CharField(max_length=255, null=True)
    reservation_id = models.CharField(max_length=255, null=True)
    os_type = models.CharField(max_length=64, null=True)
    os_major_version = models.CharField(max_length=32, null=True)
    os_minor_version = models.CharField(max_length=32, null=True)
    activation_date = modellib.DateTimeUtcField(null=True)
    generated_uuid = models.CharField(max_length=64, unique=True, null=True)
    local_uuid = models.CharField(max_length=64, null=True)
    ssl_client_certificate = models.CharField(max_length=8092, null=True)
    ssl_client_key = models.CharField(max_length=8092, null=True)
    ssl_server_certificate = models.CharField(max_length=8092, null=True)
    scheduled_event_start_date = modellib.DateTimeUtcField(null=True)
    launching_user = models.ForeignKey(rbuildermodels.Users, null=True)
    available = models.NullBooleanField()
    activated = models.NullBooleanField()
    STATE_CHOICES = (
        (UNMANAGED, UNMANAGED),
        (ACTIVATED, ACTIVATED),
        (RESPONSIVE, RESPONSIVE),
        (SHUTDOWN, SHUTDOWN),
        (NONRESPONSIVE, NONRESPONSIVE),
        (DEAD, DEAD),
        (MOTHBALLED, MOTHBALLED),
    )
    current_state = models.CharField(max_length=32, choices=STATE_CHOICES, null=True)
    installed_software = models.ManyToManyField('Trove', null=True)
    is_management_node = models.NullBooleanField()
    # the management node managing this system.
    managing_node = models.ForeignKey('ManagementNode', null=True,
                        related_name='systems')

    load_fields = [local_uuid]

    def addJobs(self):
        return
        # Put these imports here for now so they don't break anything
        # globally.
        from rmake3 import client
        RMAKE_ADDRESS = 'http://localhost:9998'
        rmake_client = client.RmakeClient(RMAKE_ADDRESS)

        job_uuids = [sj.job_uuid for sj in self.system_jobs.all()]
        rmake_jobs = rmake_client.getJobs(job_uuids)

        for rmake_job in rmake_jobs:
            # TODO, make a models.Job instance

            # add it to the system
            pass

class EventType(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_event_type'

    # on-demand events need to be > 100 to be dispatched immediately
    # DO NOT CHANGE POLL PRIORITIES HERE WITHOUT CHANGING IN schema.py also
    ON_DEMAND_BASE = 100

    SYSTEM_POLL = "system poll"
    SYSTEM_POLL_PRIORITY = 50
    SYSTEM_POLL_DESC = "standard system polling event"

    SYSTEM_POLL_IMMEDIATE = "immediate system poll"
    SYSTEM_POLL_IMMEDIATE_PRIORITY = ON_DEMAND_BASE +5
    SYSTEM_POLL_IMMEDIATE_DESC = "on-demand system polling event"

    SYSTEM_ACTIVATION = "system activation"
    SYSTEM_ACTIVATION_PRIORITY = ON_DEMAND_BASE +10
    SYSTEM_ACTIVATION_DESC = "on-demand system activation event"

    event_type_id = models.AutoField(primary_key=True)
    EVENT_TYPES = (
        (SYSTEM_ACTIVATION, SYSTEM_ACTIVATION_DESC),
        (SYSTEM_POLL_IMMEDIATE, SYSTEM_POLL_IMMEDIATE_DESC),
        (SYSTEM_POLL, SYSTEM_POLL_DESC),
    )
    name = models.CharField(max_length=8092, db_index=True, choices=EVENT_TYPES)
    description = models.CharField(max_length=8092)
    priority = models.SmallIntegerField(db_index=True)

class Job(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_job'
    job_id = models.AutoField(primary_key=True)
    job_uuid = models.CharField(max_length=64)
    event_type = models.ForeignKey(EventType)
    time_created = modellib.DateTimeUtcField(auto_now_add=True)

class SystemEvent(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_system_event'
    _xobj = xobj.XObjMetadata(
                tag = 'systemEvent',
                attributes = {'id':str})

    system_event_id = models.AutoField(primary_key=True)
    system = modellib.DeferredForeignKey(System, db_index=True,
        related_name='system_events')
    event_type = modellib.DeferredForeignKey(EventType,
        related_name='system_events')
    time_created = modellib.DateTimeUtcField(auto_now_add=True)
    time_enabled = modellib.DateTimeUtcField(
        default=datetime.datetime.now(tz.tzutc()), db_index=True)
    priority = models.SmallIntegerField(db_index=True)

    def dispatchImmediately(self):
        return self.event_type.priority >= EventType.ON_DEMAND_BASE

    def get_absolute_url(self, request, parent=None):
        if isinstance(parent, EventType):
            self.view_name = 'SystemEventsByType'
        elif isinstance(parent, System):
            self.view_name = 'SystemsSystemEvent'
        return modellib.XObjIdModel.get_absolute_url(self, request, parent)

    def save(self, *args, **kw):
        self.priority = self.event_type.priority
        modellib.XObjIdModel.save(self, *args, **kw)

class ManagementNode(System):
    class Meta:
        db_table = 'inventory_management_node'
    _xobj = xobj.XObjMetadata(
                tag = 'managementNode',
                attributes = {'id':str})
    local = models.NullBooleanField()

    load_ignore_fields = ["system_ptr"]

    # need our own object manager for dup detection
    objects = modellib.ManagementNodeManager()

    def save(self, *args, **kw):
        self.is_management_node = True
        modellib.XObjModel.save(self, *args, **kw)

class Network(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_network'
    _xobj = xobj.XObjMetadata(
                tag='network')
    network_id = models.AutoField(primary_key=True)
    system = models.ForeignKey(System, related_name='networks')
    ip_address = models.CharField(max_length=15, null=True)
    # TODO: how long should this be?
    ipv6_address = models.CharField(max_length=32, null=True)
    device_name = models.CharField(max_length=255)
    public_dns_name = models.CharField(max_length=255, db_index=True)
    netmask = models.CharField(max_length=20, null=True)
    port_type = models.CharField(max_length=32, null=True)
    primary = models.NullBooleanField()
    # TODO: add all the other fields we need about a network

    load_fields = [ip_address, public_dns_name]

    def natural_key(self):
        return self.ip_address, self.public_dns_name

class SystemLog(modellib.XObjIdModel):
    class Meta:
        db_table = 'inventory_system_log'
    system_log_id = models.AutoField(primary_key=True)
    system = modellib.DeferredForeignKey(System, related_name='system_log')

    def get_absolute_url(self, request, parent=None):
        if not parent:
            parent = self.system
        if isinstance(parent, System):
            view_name = 'SystemLog'
        return modellib.XObjIdModel.get_absolute_url(self, request, parent)

class SystemLogEntry(modellib.XObjModel):
    _xobj = xobj.XObjMetadata(
                tag='systemLogEntry')
    class Meta:
        db_table = 'inventory_system_log_entry'
        ordering = ['entry_date']

    ADDED = "System added to inventory"
    ACTIVATED = "System activated via ractivate"
    MANUALLY_ACTIVATED = "System manually activated via rBuilder"
    POLLED = "System polled."
    FETCHED = "System data fetched."
    choices = (
        (ADDED, ADDED),
        (ACTIVATED, ACTIVATED),
        (MANUALLY_ACTIVATED, MANUALLY_ACTIVATED),
        (POLLED, POLLED),
        (FETCHED, FETCHED),
    )

    system_log_entry_id = models.AutoField(primary_key=True)
    system_log = models.ForeignKey(SystemLog,
        related_name='system_log_entries')
    entry = models.CharField(max_length=8092, choices=choices)
    entry_date = modellib.DateTimeUtcField(auto_now_add=True)

class Trove(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_trove'
        unique_together = (('name', 'version', 'flavor'),)
    trove_id = models.AutoField(primary_key=True)
    name = models.TextField()
    version = modellib.SerializedForeignKey('Version')
    flavor = models.TextField()
    is_top_level = models.BooleanField()
    last_available_update_refresh = modellib.DateTimeUtcField(
        auto_now_add=True)
    available_updates = models.ManyToManyField('Version',
        related_name='available_updates')

    def _is_top_level_group(self):
        return self.name.startswith('group-') and \
            self.name.endswith('-appliance')

    def save(self, *args, **kw):
        if self._is_top_level_group():
            self.is_top_level = True
        else:
            self.is_top_level = False
        modellib.XObjModel.save(self, *args, **kw)

class Version(modellib.XObjModel):
    serialize_accessors = False
    class Meta:
        db_table = 'inventory_version'
    version_id = models.AutoField(primary_key=True)
    full = models.TextField()
    label = models.TextField()
    revision = models.TextField()
    ordering = models.TextField()
    flavor = models.TextField()

class SystemJob(modellib.XObjModel):
    class Meta:
        db_table = 'inventory_system_job'
    system_job_id = models.AutoField(primary_key=True)
    system = models.ForeignKey(System)
    job = models.ForeignKey(Job)

class ErrorResponse(modellib.XObjModel):
    _xobj = xobj.XObjMetadata(
                tag='fault')
    class Meta:
        abstract = True
    code = models.TextField()
    message = models.TextField()
    traceback = models.TextField()
    product_code = models.TextField()

for mod_obj in sys.modules[__name__].__dict__.values():
    if hasattr(mod_obj, '_xobj'):
        if mod_obj._xobj.tag:
            modellib.type_map[mod_obj._xobj.tag] = mod_obj
for mod_obj in rbuildermodels.__dict__.values():
    if hasattr(mod_obj, '_meta'):
        modellib.type_map[mod_obj._meta.verbose_name] = mod_obj
<|endoftext|>


<commit_before>#!/usr/bin/python
#
# Copyright (c) 2011 rPath, Inc.
#
# All rights reserved.
#

import sys

from django.db import models

from mint.django_rest.rbuilder import modellib
from mint.django_rest.rbuilder.inventory import models as inventorymodels

from xobj import xobj

OPERATOR_CHOICES = [(k, v) for k, v in modellib.filterTermMap.items()]

class AllMembers(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "all_members")
    view_name = "QuerySetAllResult"

class ChosenMembers(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "chosen_members")
    view_name = "QuerySetChosenResult"

class FilteredMembers(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "filtered_members")
    view_name = "QuerySetFilteredResult"

class QuerySets(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "query_sets")
    list_fields = ["query_set"]
    query_set = []

class SystemQuerySetDescriptor(modellib.XObjModel):
    def to_xml(self, *args, **kwargs):
        return descriptor_xml.system_descriptor_xml

class QuerySet(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "query_set")

    query_set_id = models.AutoField(primary_key=True)
    name = models.TextField()
    created_date = modellib.DateTimeUtcField(auto_now_add=True)
    modified_date = modellib.DateTimeUtcField(auto_now_add=True)
    children = models.ManyToManyField("self", symmetrical=False)
    filter_entries = models.ManyToManyField("FilterEntry")
    resource_type = models.TextField()

    def serialize(self, request=None, values=None):
        xobjModel = modellib.XObjIdModel.serialize(self, request, values)

        am = AllMembers()
        am._parents = [self]
        xobjModel.all_members = am.serialize(request)
        cm = ChosenMembers()
        cm._parents = [self]
        xobjModel.chosen_members = cm.serialize(request)
        fm = FilteredMembers()
        fm._parents = [self]
        xobjModel.filtered_members = fm.serialize(request)

        return xobjModel

class FilterEntry(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "filter_entry")

    filter_entry_id = models.AutoField(primary_key=True)
    field = models.TextField()
    operator = models.TextField(choices=OPERATOR_CHOICES)
    value = models.TextField()

class QueryTag(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = 'query_tag')

    query_tag_id = models.AutoField(primary_key=True)
    query_set = modellib.ForeignKey("QuerySet", related_name="querytags", null=True)
    query_tag = models.TextField()

class InclusionMethod(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = 'includsion_method')

    METHOD_CHOICES = [
        ('chosen', 'Chosen'),
        ('filtered', 'Filtered'),
    ]

    inclusion_method_id = models.AutoField(primary_key=True)
    inclusion_method = models.TextField(choices=METHOD_CHOICES)

class SystemTag(modellib.XObjModel):
    _xobj = xobj.XObjMetadata(
                tag = 'system_tag')

    system_tag_id = models.AutoField(primary_key=True)
    system = modellib.ForeignKey(inventorymodels.System)
    query_tag = modellib.ForeignKey(QueryTag)
    inclusion_method = modellib.ForeignKey(InclusionMethod)

for mod_obj in sys.modules[__name__].__dict__.values():
    if hasattr(mod_obj, '_xobj'):
        if mod_obj._xobj.tag:
            modellib.type_map[mod_obj._xobj.tag] = mod_obj
<commit_msg>stub this out for now<commit_after>#!/usr/bin/python
#
# Copyright (c) 2011 rPath, Inc.
#
# All rights reserved.
#

import sys

from django.db import models

from mint.django_rest.rbuilder import modellib
from mint.django_rest.rbuilder.inventory import models as inventorymodels

from xobj import xobj

OPERATOR_CHOICES = [(k, v) for k, v in modellib.filterTermMap.items()]

class AllMembers(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "all_members")
    view_name = "QuerySetAllResult"

class ChosenMembers(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "chosen_members")
    view_name = "QuerySetChosenResult"

class FilteredMembers(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "filtered_members")
    view_name = "QuerySetFilteredResult"

class QuerySets(modellib.XObjModel):
    class Meta:
        abstract = True
    _xobj = xobj.XObjMetadata(
                tag = "query_sets")
    list_fields = ["query_set"]
    query_set = []

class SystemQuerySetDescriptor(modellib.XObjModel):
    def to_xml(self, *args, **kwargs):
        pass
        # return descriptor_xml.system_descriptor_xml

class QuerySet(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "query_set")

    query_set_id = models.AutoField(primary_key=True)
    name = models.TextField()
    created_date = modellib.DateTimeUtcField(auto_now_add=True)
    modified_date = modellib.DateTimeUtcField(auto_now_add=True)
    children = models.ManyToManyField("self", symmetrical=False)
    filter_entries = models.ManyToManyField("FilterEntry")
    resource_type = models.TextField()

    def serialize(self, request=None, values=None):
        xobjModel = modellib.XObjIdModel.serialize(self, request, values)

        am = AllMembers()
        am._parents = [self]
        xobjModel.all_members = am.serialize(request)
        cm = ChosenMembers()
        cm._parents = [self]
        xobjModel.chosen_members = cm.serialize(request)
        fm = FilteredMembers()
        fm._parents = [self]
        xobjModel.filtered_members = fm.serialize(request)

        return xobjModel

class FilterEntry(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = "filter_entry")

    filter_entry_id = models.AutoField(primary_key=True)
    field = models.TextField()
    operator = models.TextField(choices=OPERATOR_CHOICES)
    value = models.TextField()

class QueryTag(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = 'query_tag')

    query_tag_id = models.AutoField(primary_key=True)
    query_set = modellib.ForeignKey("QuerySet", related_name="querytags", null=True)
    query_tag = models.TextField()

class InclusionMethod(modellib.XObjIdModel):
    _xobj = xobj.XObjMetadata(
                tag = 'includsion_method')

    METHOD_CHOICES = [
        ('chosen', 'Chosen'),
        ('filtered', 'Filtered'),
    ]

    inclusion_method_id = models.AutoField(primary_key=True)
    inclusion_method = models.TextField(choices=METHOD_CHOICES)

class SystemTag(modellib.XObjModel):
    _xobj = xobj.XObjMetadata(
                tag = 'system_tag')

    system_tag_id = models.AutoField(primary_key=True)
    system = modellib.ForeignKey(inventorymodels.System)
    query_tag = modellib.ForeignKey(QueryTag)
    inclusion_method = modellib.ForeignKey(InclusionMethod)

for mod_obj in sys.modules[__name__].__dict__.values():
    if hasattr(mod_obj, '_xobj'):
        if mod_obj._xobj.tag:
            modellib.type_map[mod_obj._xobj.tag] = mod_obj
<|endoftext|>


<commit_before>import json
# -*- coding: utf-8 -*-

import json
try:
    from onyphe import Onyphe
except ImportError:
    print("pyonyphe module not installed.")

misperrors = {'error': 'Error'}

mispattributes = {'input': ['ip-src', 'ip-dst', 'hostname', 'domain'], 'output': ['hostname', 'domain', 'ip-src', 'ip-dst','url']}
# possible module-types: 'expansion', 'hover' or both
moduleinfo = {'version': '1', 'author': 'Sebastien Larinier @sebdraven',
              'description': 'Query on Onyphe',
              'module-type': ['expansion', 'hover']}

# config fields that your code expects from the site admin
moduleconfig = ['apikey']

def handler(q=False):
    if q:

        request = json.loads(q)

        if not request.get('config') and not (request['config'].get('apikey')):
            misperrors['error'] = 'Onyphe authentication is missing'
            return misperrors

        api = Onyphe(request['config'].get('apikey'))

        if not api:
            misperrors['error'] = 'Onyphe Error instance api'

        ip = ''
        if request.get('ip-src'):
            ip = request['ip-src']
            return handle_ip(api ,ip, misperrors)
        elif request.get('ip-dst'):
            ip = request['ip-dst']
            return handle_ip(api,ip,misperrors)
        elif request.get('domain'):
            domain = request['domain']
        elif request.get('hostname'):
            hostname = request['hostname']
        else:
            misperrors['error'] = "Unsupported attributes type"
            return misperrors

    else:
        return False

def handle_domain(api, domain, misperrors):
    pass

def handle_ip(api, ip, misperrors):
    result_filtered = {"results": []}

    r, status_ok = expand_syscan(api, ip, misperrors)

    if status_ok:
         result_filtered['results'].extend(r)
    else:
         misperrors['error'] = "Error syscan result"

    r, status_ok = expand_pastries(api,misperrors,ip=ip)

    if status_ok:
        result_filtered['results'].extend(r)
    else:
        misperrors['error'] = 'Error pastries result'
        return misperrors

    # r, status_ok = expand_datascan(api, misperrors, ip=ip)
    #
    # if status_ok:
    #     result_filtered['results'].append(r)
    # else:
    #     return r
    #
    # r, status_ok = expand_forward(api, ip, misperrors)
    #
    # if status_ok:
    #     result_filtered['results'].append(r)
    # else:
    #     return r
    #
    # r, status_ok = expand_reverse(api, ip, misperrors)
    #
    # if status_ok:
    #     result_filtered['results'].append(r)
    # else:
    #     return r
    print(result_filtered)
    return result_filtered

def expand_syscan(api, ip, misperror):
    status_ok = False
    r = []
    asn_list = []
    os_list = []
    geoloc = []
    orgs = []
    results = api.synscan(ip)

    if results['status'] == 'ok':
        status_ok = True
        for elem in results['results']:
            asn_list.append(elem['asn'])
            os_target = elem['os']
            geoloc.append(elem['location'])
            orgs.append(elem['organization'])
            if os_target != 'Unknown':
                os_list.append(elem['os'])

            r.append({'types': ['target-machine'],
                    'values': list(set(os_list)),
                    'categories': ['Targeting data']})

            r.append({'types': ['target-location'],
                      'values': list(set(geoloc)),
                      'categories': ['Targeting data']})

            r.append({'types': ['target-org'],
                      'values': list(set(orgs)),
                      'categories': ['Targeting data']})

            r.append({'types': ['AS'], 'values': list(set(asn_list)),
                      'categories': ['Network activity']})

    return r, status_ok

def expand_datascan(api, misperror,**kwargs):
    status_ok = False
    r = None

    return r,status_ok

def expand_reverse(api, ip, misperror):
    status_ok = False
    r = None

    return r, status_ok

def expand_forward(api, ip, misperror):
    status_ok = False
    r = None

    return r, status_ok

def expand_pastries(api, misperror, **kwargs):
    status_ok = False
    r = []
    ip = None
    domain = None
    result = None
    urls_pasties = []
    domains = []
    ips = []
    if 'ip' in kwargs:
        ip = kwargs.get('ip')
        result = api.pastries(ip)

    if 'domain' in kwargs:
        domain = kwargs.get('domain')
        result = api.pastries(domain)

    if result['status'] =='ok':
        status_ok = True
        for item in result['results']:
            if item['@category'] == 'pastries':
                if item['@type'] == 'pastebin':
                    urls_pasties.append('https://pastebin.com/raw/%s' % item['key'])

                    if 'domain' in item:
                        domains.extend(item['domain'])
                    if 'ip' in item:
                        ips.extend(item['ip'])
                    if 'hostname' in item:
                        domains.extend(item['hostname'])

        r.append({'types': ['url'], 'values': urls_pasties,
                                           'categories': ['External analysis']})
        r.append({'types': ['domain'], 'values': list(set(domains)),
                                           'categories': ['Network activity']})

        r.append({'types': ['ip-dst'], 'values': list(set(ips)),
                  'categories': ['Network activity']})

    return r, status_ok

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo<commit_msg>error loops<commit_after>import json
# -*- coding: utf-8 -*-

import json
try:
    from onyphe import Onyphe
except ImportError:
    print("pyonyphe module not installed.")

misperrors = {'error': 'Error'}

mispattributes = {'input': ['ip-src', 'ip-dst', 'hostname', 'domain'], 'output': ['hostname', 'domain', 'ip-src', 'ip-dst','url']}
# possible module-types: 'expansion', 'hover' or both
moduleinfo = {'version': '1', 'author': 'Sebastien Larinier @sebdraven',
              'description': 'Query on Onyphe',
              'module-type': ['expansion', 'hover']}

# config fields that your code expects from the site admin
moduleconfig = ['apikey']

def handler(q=False):
    if q:

        request = json.loads(q)

        if not request.get('config') and not (request['config'].get('apikey')):
            misperrors['error'] = 'Onyphe authentication is missing'
            return misperrors

        api = Onyphe(request['config'].get('apikey'))

        if not api:
            misperrors['error'] = 'Onyphe Error instance api'

        ip = ''
        if request.get('ip-src'):
            ip = request['ip-src']
            return handle_ip(api ,ip, misperrors)
        elif request.get('ip-dst'):
            ip = request['ip-dst']
            return handle_ip(api,ip,misperrors)
        elif request.get('domain'):
            domain = request['domain']
        elif request.get('hostname'):
            hostname = request['hostname']
        else:
            misperrors['error'] = "Unsupported attributes type"
            return misperrors

    else:
        return False

def handle_domain(api, domain, misperrors):
    pass

def handle_ip(api, ip, misperrors):
    result_filtered = {"results": []}

    r, status_ok = expand_syscan(api, ip, misperrors)

    if status_ok:
         result_filtered['results'].extend(r)
    else:
         misperrors['error'] = "Error syscan result"

    r, status_ok = expand_pastries(api,misperrors,ip=ip)

    if status_ok:
        result_filtered['results'].extend(r)
    else:
        misperrors['error'] = 'Error pastries result'
        return misperrors

    # r, status_ok = expand_datascan(api, misperrors, ip=ip)
    #
    # if status_ok:
    #     result_filtered['results'].append(r)
    # else:
    #     return r
    #
    # r, status_ok = expand_forward(api, ip, misperrors)
    #
    # if status_ok:
    #     result_filtered['results'].append(r)
    # else:
    #     return r
    #
    # r, status_ok = expand_reverse(api, ip, misperrors)
    #
    # if status_ok:
    #     result_filtered['results'].append(r)
    # else:
    #     return r
    print(result_filtered)
    return result_filtered

def expand_syscan(api, ip, misperror):
    status_ok = False
    r = []
    asn_list = []
    os_list = []
    geoloc = []
    orgs = []
    results = api.synscan(ip)

    if results['status'] == 'ok':
        status_ok = True
        for elem in results['results']:
            asn_list.append(elem['asn'])
            os_target = elem['os']
            geoloc.append(elem['location'])
            orgs.append(elem['organization'])
            if os_target != 'Unknown':
                os_list.append(elem['os'])

        r.append({'types': ['target-machine'],
                    'values': list(set(os_list)),
                    'categories': ['Targeting data']})

        r.append({'types': ['target-location'],
                      'values': list(set(geoloc)),
                      'categories': ['Targeting data']})

        r.append({'types': ['target-org'],
                      'values': list(set(orgs)),
                      'categories': ['Targeting data']})

        r.append({'types': ['AS'], 'values': list(set(asn_list)),
                      'categories': ['Network activity']})

    return r, status_ok

def expand_datascan(api, misperror,**kwargs):
    status_ok = False
    r = None

    return r,status_ok

def expand_reverse(api, ip, misperror):
    status_ok = False
    r = None

    return r, status_ok

def expand_forward(api, ip, misperror):
    status_ok = False
    r = None

    return r, status_ok

def expand_pastries(api, misperror, **kwargs):
    status_ok = False
    r = []
    ip = None
    domain = None
    result = None
    urls_pasties = []
    domains = []
    ips = []
    if 'ip' in kwargs:
        ip = kwargs.get('ip')
        result = api.pastries(ip)

    if 'domain' in kwargs:
        domain = kwargs.get('domain')
        result = api.pastries(domain)

    if result['status'] =='ok':
        status_ok = True
        for item in result['results']:
            if item['@category'] == 'pastries':
                if item['@type'] == 'pastebin':
                    urls_pasties.append('https://pastebin.com/raw/%s' % item['key'])

                    if 'domain' in item:
                        domains.extend(item['domain'])
                    if 'ip' in item:
                        ips.extend(item['ip'])
                    if 'hostname' in item:
                        domains.extend(item['hostname'])

        r.append({'types': ['url'], 'values': urls_pasties,
                                           'categories': ['External analysis']})
        r.append({'types': ['domain'], 'values': list(set(domains)),
                                           'categories': ['Network activity']})

        r.append({'types': ['ip-dst'], 'values': list(set(ips)),
                  'categories': ['Network activity']})

    return r, status_ok

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo<|endoftext|>