Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
Added abstraction for OS commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashcrow authored and mbarnes committed Jan 25, 2016
1 parent 525f18f commit b9f6d91
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/apidoc/commissaire.oscmd.atomic.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
commissaire.oscmd.atomic module
===============================

.. automodule:: commissaire.oscmd.atomic
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions doc/apidoc/commissaire.oscmd.fedora.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
commissaire.oscmd.fedora module
===============================

.. automodule:: commissaire.oscmd.fedora
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions doc/apidoc/commissaire.oscmd.rhel.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
commissaire.oscmd.rhel module
=============================

.. automodule:: commissaire.oscmd.rhel
:members:
:undoc-members:
:show-inheritance:
17 changes: 17 additions & 0 deletions doc/apidoc/commissaire.oscmd.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
commissaire.oscmd package
=========================

.. automodule:: commissaire.oscmd
:members:
:undoc-members:
:show-inheritance:

Submodules
----------

.. toctree::

commissaire.oscmd.atomic
commissaire.oscmd.fedora
commissaire.oscmd.rhel

1 change: 1 addition & 0 deletions doc/apidoc/commissaire.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Subpackages
commissaire.compat
commissaire.handlers
commissaire.jobs
commissaire.oscmd
commissaire.transport

Submodules
Expand Down
64 changes: 64 additions & 0 deletions src/commissaire/oscmd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://
"""
Abstraction of commands that change across operating systems.
"""


class OSCmdBase:
"""
Operating system command abstraction.
"""

#: The type of Operating System
os_type = None

def reboot(self):
"""
Reboot command. Must be overriden.
:return: The command to execute as a list
:rtype: list
"""
raise NotImplementedError('{0}.reboot() must be overriden.'.format(
self.__class__.__name__))

def upgrade(self):
"""
Upgrade command. Must be overriden.
:return: The command to execute as a list
:rtype: list
"""
raise NotImplementedError('{0}.upgrade() must be overriden.'.format(
self.__class__.__name__))


def get_oscmd(os_type):
"""
Returns the proper OSCmd class based on os_type.
:param os_type: The type of OS: EX: fedora
:type os_type: string
:return: The proper OSCmd class.
:rtype: commissaire.oscmd.OSCmdBase
"""
try:
return getattr(__import__(
'commissaire.oscmd.{0}'.format(os_type),
fromlist=[True]), 'OSCmd')
except ImportError:
# TODO: Make this a specific exception
raise Exception('No OSCmd class for {0}'.format(os_type))
46 changes: 46 additions & 0 deletions src/commissaire/oscmd/atomic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://
"""
Atomic commands.
"""

from commissaire.oscmd import OSCmdBase


class OSCmd(OSCmdBase):
"""
Commmands for Atomic.
"""

#: The type of Operating System
os_type = 'atomic'

def reboot(self):
"""
Atomic reboot command.
:return: The command to execute as a list
:rtype: list
"""
return ['systemctl', 'reboot']

def upgrade(self):
"""
Atomic upgrade command.
:return: The command to execute as a list
:rtype: list
"""
return ['rpm-ostree', 'upgrade']
46 changes: 46 additions & 0 deletions src/commissaire/oscmd/fedora.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://
"""
Fedora commands.
"""

from commissaire.oscmd import OSCmdBase


class OSCmd(OSCmdBase):
"""
Commmands for Fedora.
"""

#: The type of Operating System
os_type = 'fedora'

def reboot(self):
"""
Fedora reboot command.
:return: The command to execute as a list
:rtype: list
"""
return ['systemctl', 'reboot']

def upgrade(self):
"""
Fedora upgrade command.
:return: The command to execute as a list
:rtype: list
"""
return ['dnf', 'update', '-y']
46 changes: 46 additions & 0 deletions src/commissaire/oscmd/rhel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://
"""
RHEL commands.
"""

from commissaire.oscmd import OSCmdBase


class OSCmd(OSCmdBase):
"""
Commmands for RHEL.
"""

#: The type of Operating System
os_type = 'rhel'

def reboot(self):
"""
RHEL reboot command.
:return: The command to execute as a list
:rtype: list
"""
return ['systemctl', 'reboot']

def upgrade(self):
"""
RHEL upgrade command.
:return: The command to execute as a list
:rtype: list
"""
return ['yum', 'update', '-y']
67 changes: 67 additions & 0 deletions test/test_oscmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Test cases for the commissaire.oscmd module.
"""

from . import TestCase
from commissaire import oscmd


class Test_OSCmdBase(TestCase):
"""
Tests for the OSCmdBase class.
"""

def before(self):
"""
Sets up a fresh instance of the class before each run.
"""
self.instance = oscmd.OSCmdBase()

def test_oscmd_base_reboot_raises(self):
"""
Verify OSCmdBase's raises on reboot.
"""
self.assertRaises(
NotImplementedError,
self.instance.reboot)

def test_oscmd_base_upgrade_raises(self):
"""
Verify OSCmdBase's raises on upgrade.
"""
self.assertRaises(
NotImplementedError,
self.instance.upgrade)


class Test_get_oscmd(TestCase):

def test_get_oscmd_with_valid_os_types(self):
"""
Verify get_oscmd returns proper modules.
"""
for os_type in ('atomic', 'fedora', 'rhel'):
self.assertEquals(
'commissaire.oscmd.{0}'.format(os_type),
oscmd.get_oscmd(os_type).__module__)

def test_get_oscmd_with_invalid_os_types(self):
"""
Verify get_oscmd errors when the os_type does not exist.
"""

self.assertRaises(Exception, oscmd.get_oscmd, 'not_a_real_os_type')
50 changes: 50 additions & 0 deletions test/test_oscmd_atomic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (C) 2016 Red Hat, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Test cases for the commissaire.oscmd.atomic module.
"""

from . import TestCase
from commissaire.oscmd import atomic


class Test_Atomic_OSCmd(TestCase):
"""
Tests for the OSCmd class for Atomic.
"""

def before(self):
"""
Sets up a fresh instance of the class before each run.
"""
self.instance = atomic.OSCmd()

def test_atomic_oscmd_reboot(self):
"""
Verify Atomic's OSCmd returns proper data on reboot.
"""
cmd = self.instance.reboot()
self.assertEquals(
list,
type(cmd))

def test_atomic_oscmd_base_upgrade(self):
"""
Verify Atomic's OSCmd returns proper data on upgrade.
"""
cmd = self.instance.upgrade()
self.assertEquals(
list,
type(cmd))

0 comments on commit b9f6d91

Please sign in to comment.