Skip to content

Commit

Permalink
Setting FQDN dor Debian and RedHat
Browse files Browse the repository at this point in the history
  • Loading branch information
Suszyński Krzysztof committed Aug 8, 2017
1 parent 18a6794 commit 6b55f24
Show file tree
Hide file tree
Showing 42 changed files with 359 additions and 112 deletions.
6 changes: 3 additions & 3 deletions puppeter/domain/facter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar, Type, Callable
from typing import TypeVar, Type, Callable, Optional

T = TypeVar('T')

Expand All @@ -12,7 +12,7 @@ class Facter:

@staticmethod
def get(enumvar):
# type: (Type[T]) -> T | None
# type: (Type[T]) -> Optional[T]
if enumvar in Facter._facts:
return Facter._facts[enumvar]
elif enumvar in Facter.__resolvers:
Expand All @@ -32,7 +32,7 @@ def set(enumvar, resolver):

@staticmethod
def __resolve(enumvar):
# type: (Type[T]) -> T | None
# type: (Type[T]) -> Optional[T]
for resolver in Facter.__resolvers[enumvar]:
resolved = resolver()
if resolved is not None:
Expand Down
10 changes: 6 additions & 4 deletions puppeter/domain/gateway/answers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from six import with_metaclass
from abc import ABCMeta, abstractmethod

from typing import IO

from puppeter.domain.model.answers import Answers


class AnswersGateway(with_metaclass(ABCMeta, object)):
@abstractmethod
def read_answers_from_file(self, file):
# type: (file) -> Answers
def read_answers_from_file(self, target):
# type: (IO) -> Answers
pass

@abstractmethod
def write_answers_to_file(self, answers, file):
# type: (Answers, file) -> None
def write_answers_to_file(self, answers, target):
# type: (Answers, IO) -> None
pass


Expand Down
14 changes: 14 additions & 0 deletions puppeter/domain/gateway/fqdn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from abc import ABCMeta, abstractmethod

from six import with_metaclass
from typing import Sequence

from puppeter.domain.model.configurer import Configurer
from puppeter.domain.model.fqdn import FqdnConfiguration


class FqdnSetterGateway(with_metaclass(ABCMeta)):
@abstractmethod
def process_fully_qualified_domain_name(self, fqdn):
# type: (FqdnConfiguration) -> Sequence[Configurer]
pass
7 changes: 4 additions & 3 deletions puppeter/domain/gateway/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class InstallerGateway(with_metaclass(ABCMeta, object)):

def configurers(self, installer):
configurers = []
configurers.extend(self._provide_install_configurers(installer))
configurers.extend(self._puppet_cert_issue(installer))
configurers.extend(self._puppet_services(installer))
if installer is not None:
configurers.extend(self._provide_install_configurers(installer))
configurers.extend(self._puppet_cert_issue(installer))
configurers.extend(self._puppet_services(installer))
return tuple(configurers)

@abstractmethod
Expand Down
4 changes: 2 additions & 2 deletions puppeter/domain/gateway/installer.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Sequence
from typing import Sequence, Optional

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


class InstallerGateway:
def configurers(self, installer: Installer) -> Sequence[Configurer]: ...
def configurers(self, installer: Optional[Installer]) -> Sequence[Configurer]: ...
def _provide_install_configurers(self, installer: Installer) -> Sequence[Configurer]: ...
def _puppet_cert_issue(self, installer: Installer) -> Sequence[Configurer]: ...
def _puppet_services(self, installer: Installer) -> Sequence[Configurer]: ...
32 changes: 18 additions & 14 deletions puppeter/domain/model/answers.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
from typing import Optional

from puppeter.domain.model.fqdn import FqdnConfiguration
from puppeter.domain.model.installer import Installer


class Answers:
def __init__(self,
installer=None,
fqdn_configurator=None,
timesync_configurator=None,
csrattrs_configurator=None):
self.__installer = installer # type: Installer
self.__fqdn_configurator = fqdn_configurator
self.__timesync_configurator = timesync_configurator
self.__csrattrs_configurator = csrattrs_configurator
fqdn_configuration=None,
timesync_configuration=None,
csrattrs_configuration=None):
self.__installer = installer # type: Optional[Installer]
self.__fqdn_configuration = fqdn_configuration # type: Optional[FqdnConfiguration]
self.__timesync_configuration = timesync_configuration
self.__csrattrs_configuration = csrattrs_configuration

def installer(self):
# type: () -> Installer
# type: () -> Optional[Installer]
return self.__installer

def fqdn_configurator(self):
return self.__fqdn_configurator
def fqdn_configuration(self):
# type: () -> Optional[FqdnConfiguration]
return self.__fqdn_configuration

def timesync_configurator(self):
return self.__timesync_configurator
def timesync_configuration(self):
return self.__timesync_configuration

def csrattrs_configurator(self):
return self.__csrattrs_configurator
def csrattrs_configuration(self):
return self.__csrattrs_configuration
32 changes: 32 additions & 0 deletions puppeter/domain/model/fqdn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Union

from puppeter.domain.model.withoptions import WithOptions


class FqdnConfiguration(WithOptions):
def __init__(self, fqdn='localhost.localdomain'):
self.__fqdn = fqdn.strip()

def raw_options(self):
return self.fqdn()

def read_raw_options(self, options):
self.__fqdn = str(options).strip()

def fqdn(self):
# type: () -> str
return self.__fqdn

def hostname(self):
# type: () -> str
return self.fqdn().split('.')[0]

def domain(self):
# type: () -> Union[str,None]
domain = '.'.join(self.fqdn().split('.')[1:])
domain = None if domain == '' else domain
return domain

def has_domain(self):
# type: () -> bool
return self.domain() is not None
32 changes: 4 additions & 28 deletions puppeter/domain/model/installer.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
import collections
import re
from six import with_metaclass, iteritems
from abc import abstractmethod, ABCMeta

from enum import Enum
from typing import Sequence, Dict, Any
from six import with_metaclass
from typing import Sequence

from puppeter.container import Named
from puppeter.domain.model.gemrequirement import GemRequirement
from puppeter.domain.model.withoptions import WithOptions


class Mode(Enum):
Agent = 1
Server = 2


class WithOptions(with_metaclass(ABCMeta, object)):

@abstractmethod
def raw_options(self):
# type: () -> Dict[str, Any]
pass

@abstractmethod
def read_raw_options(self, options):
# type: (Dict[str, Any]) -> None
pass

@staticmethod
def _update(orig_dict, new_dict):
for key, val in iteritems(new_dict):
if isinstance(val, collections.Mapping):
tmp = WithOptions._update(orig_dict=orig_dict.get(key, {}), new_dict=val)
orig_dict[key] = tmp
elif isinstance(val, list):
orig_dict[key] = (orig_dict.get(key, []) + val)
else:
orig_dict[key] = new_dict[key]
return orig_dict


class Installer(WithOptions):
def __init__(self):
self.__mode = Mode.Agent # type: Mode
Expand Down
30 changes: 30 additions & 0 deletions puppeter/domain/model/withoptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import collections
from abc import ABCMeta, abstractmethod

from six import with_metaclass, iteritems
from typing import Dict, Any


class WithOptions(with_metaclass(ABCMeta, object)):

@abstractmethod
def raw_options(self):
# type: () -> Dict[str, Any]
pass

@abstractmethod
def read_raw_options(self, options):
# type: (Dict[str, Any]) -> None
pass

@staticmethod
def _update(orig_dict, new_dict):
for key, val in iteritems(new_dict):
if isinstance(val, collections.Mapping):
tmp = WithOptions._update(orig_dict=orig_dict.get(key, {}), new_dict=val)
orig_dict[key] = tmp
elif isinstance(val, list):
orig_dict[key] = (orig_dict.get(key, []) + val)
else:
orig_dict[key] = new_dict[key]
return orig_dict
3 changes: 3 additions & 0 deletions puppeter/persistence/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from puppeter import container
from puppeter.domain.gateway.answers import AnswersGateway
from puppeter.domain.gateway.fqdn import FqdnSetterGateway
from puppeter.persistence.gateway.answers import YamlAnswersGateway
from puppeter.persistence.gateway.fqdn import FqdnSetterGatewayImpl


container.bind(AnswersGateway, YamlAnswersGateway)
container.bind(FqdnSetterGateway, FqdnSetterGatewayImpl)
35 changes: 28 additions & 7 deletions puppeter/persistence/gateway/answers.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
from typing import Any, Callable, Dict, TypeVar, Optional

import puppeter
from puppeter import container
from puppeter.domain.model.fqdn import FqdnConfiguration
from puppeter.domain.model.installer import Installer
from puppeter.domain.model.answers import Answers
from puppeter.domain.gateway.answers import AnswersGateway
from puppeter.container import Named
import ruamel.yaml

T = TypeVar('T')


@Named('yaml')
class YamlAnswersGateway(AnswersGateway):
def write_answers_to_file(self, answers, file):
def write_answers_to_file(self, answers, target_file):
raw_answers = {
'installer': answers.installer().raw_options()
'installer': answers.installer().raw_options(),
'fqdn': answers.fqdn_configuration().raw_options()
}
yaml = ruamel.yaml.dump(raw_answers, Dumper=ruamel.yaml.RoundTripDumper)
file.write(yaml)
target_file.write(yaml)

def read_answers_from_file(self, file):
code = ruamel.yaml.load(file.read(), ruamel.yaml.RoundTripLoader)
def read_answers_from_file(self, target_file):
code = ruamel.yaml.load(target_file.read(), ruamel.yaml.RoundTripLoader)
log = puppeter.get_logger(YamlAnswersGateway)
log.debug("Answers loaded from file: %s", code)
installer = self.__load_installer(code['installer'])
installer = self.__process_data(code,
'installer',
lambda val: self.__load_installer(val))
fqdn = self.__process_data(code,
'fqdn',
lambda val: FqdnConfiguration(val))
return Answers(
installer=installer
installer=installer,
fqdn_configuration=fqdn
)

@staticmethod
Expand All @@ -31,3 +43,12 @@ def __load_installer(options):
installer = container.get_named(Installer, bean_name)
installer.read_raw_options(options)
return installer

@staticmethod
def __process_data(data, key, func):
# type: (Dict[str,Any],str,Callable[[Any],T]) -> Optional[T]
try:
value = data[key]
return func(value)
except KeyError:
return None
7 changes: 7 additions & 0 deletions puppeter/persistence/gateway/debian-persist-fqdn.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
file { '/etc/hostname':
ensure => 'file',
owner => 'root',
group => 'root',
mode => '0644',
content => '@{hostname}'
}
Loading

0 comments on commit 6b55f24

Please sign in to comment.