Skip to content

Commit

Permalink
Installers and its templates, more facts
Browse files Browse the repository at this point in the history
  • Loading branch information
Suszyński Krzysztof committed Jul 11, 2017
1 parent 32b61d4 commit 4a333f6
Show file tree
Hide file tree
Showing 30 changed files with 389 additions and 48 deletions.
9 changes: 9 additions & 0 deletions data/templates/installer/pc/debian/puppetagent.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x
set +e

if ! dpkg -l 'puppet-agent' | grep -q ii; then
set -e
sudo apt-get install -y puppet-agent
fi
9 changes: 9 additions & 0 deletions data/templates/installer/pc/debian/puppetserver.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x
set +e

if ! dpkg -l 'puppetserver' | grep -q ii; then
set -e
sudo apt-get install -y puppetserver
fi
9 changes: 9 additions & 0 deletions data/templates/installer/pc/redhat/puppetagent.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x
set +e

if ! rpm -q 'puppet-agent'; then
set -e
sudo yum install -y puppet-agent
fi
9 changes: 9 additions & 0 deletions data/templates/installer/pc/redhat/puppetserver.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x
set +e

if ! rpm -q 'puppetserver'; then
set -e
sudo yum install -y puppetserver
fi
13 changes: 13 additions & 0 deletions data/templates/installer/pc4x/debian/repo.sh
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -x
set +e

if ! dpkg -l 'puppetlabs-release-pc1' | grep -q ii; then
set -e
cd /tmp
wget "https://apt.puppetlabs.com/puppetlabs-release-pc1-@{codename}.deb"
sudo dpkg -i "puppetlabs-release-pc1-@{codename}.deb"
rm "puppetlabs-release-pc1-@{codename}.deb"
sudo apt-get update
fi
9 changes: 9 additions & 0 deletions data/templates/installer/pc4x/redhat/repo.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x
set +e

if ! rpm -q puppetlabs-release-pc1; then
set -e
sudo rpm -Uvh "https://yum.puppetlabs.com/puppetlabs-release-pc1-el-@{major}.noarch.rpm"
fi
47 changes: 35 additions & 12 deletions puppeter/container.py
@@ -1,12 +1,19 @@
def Named(bean_name):
def named_decorator(cls):
cls.__bean_name = bean_name

class NamedBean(cls):
def __init__(self, *args):
self.wrapped = cls(*args)
def __init__(self, *args, **kwargs):
self.wrapped = cls(*args, **kwargs)

def bean_name(self):
@staticmethod
def bean_name():
return bean_name

@staticmethod
def original_cls():
return cls

def __repr__(self):
return '@Named(\'%s\') %s' % (bean_name, repr(self.wrapped))

Expand All @@ -26,29 +33,29 @@ def bind_to_instance(cls, impl):
beans.append(__Bean(cls, impl=impl))


def get_all(cls):
def get_all(cls, *args, **kwargs):
beans = __get_all_beans(cls)
try:
return tuple(map(lambda bean: bean.impl(), beans))
return tuple(map(lambda bean: bean.impl(*args, **kwargs), beans))
except Exception:
return tuple()


def get(cls):
def get(cls, *args, **kwargs):
beans = __get_all_beans(cls)
if len(beans) == 1:
return beans[0].impl()
return beans[0].impl(*args, **kwargs)
else:
impls = list(map(lambda bean: bean.impl_cls_name(), beans))
raise ValueError('Zero or more then one implementation found for class %s. '
'Found those implementations: %s. '
'Use @Named beans and get_named() function!' % (cls, impls))


def get_named(cls, bean_name):
def get_named(cls, bean_name, *args, **kwargs):
for bean in __get_all_beans(cls):
if bean.name() == bean_name:
return bean.impl()
return bean.impl(*args, **kwargs)
raise ValueError('Bean named %s has not been found for class %s' % (bean_name, cls))


Expand All @@ -72,13 +79,29 @@ def __init__(self, cls, impl=None, impl_cls=None):
self.__impl = impl
self.__impl_cls = impl_cls

def __repr__(self):
name = self.name()
if name is not None:
return 'Bean named \'%s\' of %s' % (name, repr(self.impl_cls()))
else:
return 'Bean of %s' % repr(self.impl_cls())

def impl_cls_name(self):
return self.__impl_cls

def impl_cls(self):
if self.__impl is None:
return self.__impl_cls
else:
return self.__impl.__class__

def name(self):
return self.impl().bean_name()
try:
return self.impl_cls().bean_name()
except AttributeError:
return None

def impl(self):
def impl(self, *args, **kwargs):
if self.__impl is None:
self.__impl = self.__impl_cls()
self.__impl = self.__impl_cls(*args, **kwargs)
return self.__impl
File renamed without changes.
18 changes: 18 additions & 0 deletions puppeter/domain/gateway/installer.py
@@ -0,0 +1,18 @@
from __future__ import absolute_import
from six import with_metaclass
from abc import ABCMeta, abstractmethod

from puppeter.domain.model.configurer import Configurer
from puppeter.domain.model import Installer


class InstallerGateway(with_metaclass(ABCMeta, object)):
def produce_commands(self, installer):
# type: (Installer) -> [str]
return self._provide_configurer(installer)\
.produce_commands()

@abstractmethod
def _provide_configurer(self, installer):
# type: (Installer) -> Configurer
pass
9 changes: 9 additions & 0 deletions puppeter/domain/model/configurer.py
@@ -0,0 +1,9 @@
from abc import ABCMeta, abstractmethod
from six import with_metaclass


class Configurer(with_metaclass(ABCMeta, object)):
@abstractmethod
def produce_commands(self):
# type: () -> [str]
raise NotImplementedError()
19 changes: 0 additions & 19 deletions puppeter/domain/model/os.py

This file was deleted.

51 changes: 51 additions & 0 deletions puppeter/domain/model/osfacts.py
@@ -0,0 +1,51 @@
from enum import Enum

import re


class OsFamily(Enum):
Unknown = 1
RedHat = 2
Debian = 3
Suse = 4


class OperatingSystem(Enum):
Unknown = 1
RedHat = 2
CentOS = 3
Scientific = 4
OracleLinux = 5
Debian = 6
Ubuntu = 7
OpenSuse = 8


class OperatingSystemRelease(str):
VERSION_RE = re.compile('^(\d+)(?:\.(\d+))*$')

def __init__(self, version):
super(OperatingSystemRelease, self).__init__()
self.__version = str(version)

def __str__(self):
return self.__version

def major(self):
return self.VERSION_RE\
.match(self)\
.group(1)

def minor(self):
return self.VERSION_RE\
.match(self)\
.group(2)


class OperatingSystemCodename(str):
def __init__(self, codename):
super(OperatingSystemCodename, self).__init__()
self.__codename = str(codename)

def __str__(self):
return self.__codename
5 changes: 5 additions & 0 deletions puppeter/persistence/gateway/installer/__init__.py
@@ -0,0 +1,5 @@
from puppeter import container
from puppeter.domain.model.configurer import Configurer
from puppeter.persistence.gateway.installer.linux import RubyGemConfigurer

container.bind(Configurer, RubyGemConfigurer)
24 changes: 24 additions & 0 deletions puppeter/persistence/gateway/installer/debian/__init__.py
@@ -0,0 +1,24 @@
from puppeter import container
from puppeter.container import Named
from puppeter.domain.model.configurer import Configurer
from puppeter.domain.gateway.installer import InstallerGateway
from puppeter.persistence.gateway.installer.debian.pc3x import PC3xConfigurer
from puppeter.persistence.gateway.installer.debian.pc4x import PC4xConfigurer
from puppeter.persistence.gateway.installer.debian.pc5x import PC5xConfigurer


@Named('debian')
class DebianInstallerGateway(InstallerGateway):

def _provide_configurer(self, installer):
name = installer.bean_name()
if name == 'gem':
return container.get_named(Configurer, 'gem', installer=installer)
name += '-debian'
return container.get_named(Configurer, name, installer=installer)


container.bind(InstallerGateway, DebianInstallerGateway)
container.bind(Configurer, PC3xConfigurer)
container.bind(Configurer, PC4xConfigurer)
container.bind(Configurer, PC5xConfigurer)
11 changes: 11 additions & 0 deletions puppeter/persistence/gateway/installer/debian/pc3x.py
@@ -0,0 +1,11 @@
from puppeter.container import Named
from puppeter.domain.model.configurer import Configurer


@Named('pc3x-debian')
class PC3xConfigurer(Configurer):
def __init__(self, installer):
self.installer = installer

def produce_commands(self):
raise NotImplementedError('Not yet implemented!')
16 changes: 16 additions & 0 deletions puppeter/persistence/gateway/installer/debian/pc4x.py
@@ -0,0 +1,16 @@
from puppeter.container import Named
from puppeter.domain.facter import Facter
from puppeter.domain.model.osfacts import OperatingSystemCodename
from puppeter.domain.model.configurer import Configurer


@Named('pc4x-debian')
class PC4xConfigurer(Configurer):
def __init__(self, installer):
self.__installer = installer

def produce_commands(self):
codename = Facter.get(OperatingSystemCodename)
cmds = []
cmds.append("")
raise NotImplementedError('Not yet implemented! %s' % codename)
11 changes: 11 additions & 0 deletions puppeter/persistence/gateway/installer/debian/pc5x.py
@@ -0,0 +1,11 @@
from puppeter.container import Named
from puppeter.domain.model.configurer import Configurer


@Named('pc5x-debian')
class PC5xConfigurer(Configurer):
def __init__(self, installer):
self.installer = installer

def produce_commands(self):
raise NotImplementedError('Not yet implemented!')
11 changes: 11 additions & 0 deletions puppeter/persistence/gateway/installer/linux.py
@@ -0,0 +1,11 @@
from puppeter.container import Named
from puppeter.domain.model.configurer import Configurer


@Named('gem')
class RubyGemConfigurer(Configurer):
def __init__(self, installer):
self.installer = installer

def produce_commands(self):
raise NotImplementedError('Not yet implemented!')
24 changes: 24 additions & 0 deletions puppeter/persistence/gateway/installer/redhat/__init__.py
@@ -0,0 +1,24 @@
from puppeter import container
from puppeter.container import Named
from puppeter.domain.model.configurer import Configurer
from puppeter.domain.gateway.installer import InstallerGateway
from puppeter.persistence.gateway.installer.redhat.pc3x import PC3xConfigurer
from puppeter.persistence.gateway.installer.redhat.pc4x import PC4xConfigurer
from puppeter.persistence.gateway.installer.redhat.pc5x import PC5xConfigurer


@Named('redhat')
class RedHatInstallerGateway(InstallerGateway):

def _provide_configurer(self, installer):
name = installer.bean_name()
if name == 'gem':
return container.get_named(Configurer, 'gem', installer=installer)
name += '-redhat'
return container.get_named(Configurer, name, installer=installer)


container.bind(InstallerGateway, RedHatInstallerGateway)
container.bind(Configurer, PC3xConfigurer)
container.bind(Configurer, PC4xConfigurer)
container.bind(Configurer, PC5xConfigurer)
11 changes: 11 additions & 0 deletions puppeter/persistence/gateway/installer/redhat/pc3x.py
@@ -0,0 +1,11 @@
from puppeter.container import Named
from puppeter.domain.model.configurer import Configurer


@Named('pc3x-redhat')
class PC3xConfigurer(Configurer):
def __init__(self, installer):
self.installer = installer

def produce_commands(self):
raise NotImplementedError('Not yet implemented!')
11 changes: 11 additions & 0 deletions puppeter/persistence/gateway/installer/redhat/pc4x.py
@@ -0,0 +1,11 @@
from puppeter.container import Named
from puppeter.domain.model.configurer import Configurer


@Named('pc4x-redhat')
class PC4xConfigurer(Configurer):
def __init__(self, installer):
self.__installer = installer

def produce_commands(self):
raise NotImplementedError('Not yet implemented!')

0 comments on commit 4a333f6

Please sign in to comment.