consul-service

Example:

# Download/install the Consul executable and create, enable and start a systemd service unit for it.
- consul-service:
    enabled: true
    started: true

Description

Create a user named 'consul' (if necessary), download the Consul binary into '/usr/local/bin' and create a systemd service unit ('consul') and enable/start it if so specified.

It is recommended to create the approriate Consul server/client configuration files beforehand (in '/etc/consul.d'), but this frecklet also allows for providing basic configuration options if necessary.

Resources

Variables

Name Type Default Description

arch

string --

The architecture of the host system.

consul_config

dict --

The consul configuration.

dest

string --

The (absolute) path to the parent folder of the downloaded executable file.

enabled

boolean --

Whether to enable the service.

platform

string --

The platform of the host system.

started

boolean --

Whether to start the service.

version

string 1.5.3

The version of consul to install.

Examples

Example 1

Download/install the Consul executable and create, enable and start a systemd service unit for it.

Code
- consul-service:
    enabled: true
    started: true
Description

This creates a user 'consul' (if it doesn't exist), then installs the Consul binary into '/usr/local/bin' (for the Linux/amd64 platform/architecture). Then a systemd service unit 'consul' is created, started and enabled, with the recommended settings for Consul. Consul server/client configuration file(s) are created separately, beforehand, in this case.

Example 2

Install Consul & Consul service, incl. configuration.

Code
- consul-service:
    enabled: true
    started: true
    consul_config:
      encrypt: Tp9B4ByL9pGHYmprFZi/0Q==
      datacenter: dc1
      data_dir: /var/lib/consul
      client_addr: 0.0.0.0
      retry_join: '["10.14.14.10"]'
Description

Similar to the above example, except that a consul configuration file ('/etc/consul.d/consul.hcl') is created in the same step. This does not parse any config option, so you have to create folders ('/var/lib/consul' in this example) and such yourself beforehand. Also, not that all the key values need to be strings, as they will just be copied as they are into the configuration file (the 'retry_join' list in this example). Usually, it's a better approach to construct the config file beforehand, separately.

Code

doc:
  short_help: Install Hashicorp Consul and run as service.
  help: |
    Create a user named 'consul' (if necessary), download the Consul binary into '/usr/local/bin' and create a
    systemd service unit ('consul') and enable/start it if so specified.

    It is recommended to create the approriate Consul server/client configuration files beforehand (in '/etc/consul.d'), but this frecklet also allows for providing basic configuration options if necessary.
  references:
    consul deployment guide: https://learn.hashicorp.com/consul/datacenter-deploy/deployment-guide
  examples:
  - title: Download/install the Consul executable and create, enable and start a systemd
      service unit for it.
    desc: |
      This creates a user 'consul' (if it doesn't exist), then installs the Consul binary into '/usr/local/bin' (for the Linux/amd64 platform/architecture). Then a systemd service unit 'consul' is created, started and enabled, with the recommended settings for Consul. Consul server/client configuration file(s) are created separately, beforehand, in this case.
    vars:
      enabled: true
      started: true
  - title: Install Consul & Consul service, incl. configuration.
    desc: |
      Similar to the above example, except that a consul configuration file ('/etc/consul.d/consul.hcl') is created in the same step. This does not parse any config option, so you have to create folders ('/var/lib/consul' in this example) and such yourself beforehand. Also, not that all the key values need to be strings, as they will just be copied as they are into the configuration file (the 'retry_join' list in this example). Usually, it's a better approach to construct the config file beforehand, separately.
    vars:
      enabled: true
      started: true
      consul_config:
        encrypt: Tp9B4ByL9pGHYmprFZi/0Q==
        datacenter: dc1
        data_dir: /var/lib/consul
        client_addr: 0.0.0.0
        retry_join: '["10.14.14.10"]'

args:
  _import:
  - consul-installed
  - systemd-service-unit
  consul_config:
    doc:
      short_help: The consul configuration.
    type: dict
    empty: false
    required: false
    keyschema:
      type: string

frecklets:
- user-exists:
    name: consul
    group: consul
      #system_user: true
- consul-installed:
    version: '{{:: version ::}}'
    dest: '{{:: dest ::}}'
    platform: '{{:: platform ::}}'
    arch: '{{:: arch ::}}'
    owner: root
    group: root
- config-values-in-file:
    frecklet::skip: '{{ consul_config | true_if_empty }}'
    path: /etc/consul.d/consul.hcl
    owner: consul
    group: consul
    mode: '0640'
    config: '{{:: consul_config ::}}'
- systemd-service-unit:
    name: consul
    unit_description: HashiCorp Consul - A service mesh solution
    unit_documentation:
    - https://www.consul.io/
    unit_requires:
    - network-online.target
    unit_after:
    - network-online.target
    unit_condition:
    - condition_type: FileNotEmpty
      condition: /etc/consul.d/consul.hcl
    service_user: consul
    service_group: consul
    service_exec_start: /usr/local/bin/consul agent -config-dir=/etc/consul.d/
    service_exec_reload: /usr/local/bin/consul reload
    service_kill_mode: process
    service_restart: on-failure
    service_limit:
    - limit_type: NOFILE
      limit: 65536
    install_wanted_by:
    - multi-user.target
    enabled: '{{:: enabled ::}}'
    started: '{{:: started ::}}'
frecklecute consul-service --help

Usage: frecklecute consul-service [OPTIONS]

  Create a user named 'consul' (if necessary), download the Consul binary
  into '/usr/local/bin' and create a systemd service unit ('consul') and
  enable/start it if so specified.

  It is recommended to create the approriate Consul server/client
  configuration files beforehand (in '/etc/consul.d'), but this frecklet
  also allows for providing basic configuration options if necessary.

Options:
  --arch ARCH                    The architecture of the host system.
  --consul-config CONSUL_CONFIG  The consul configuration.
  --dest DEST                    The (absolute) path to the parent folder of
                                 the downloaded executable file.
  --enabled / --no-enabled       Whether to enable the service.
  --platform PLATFORM            The platform of the host system.
  --started / --no-started       Whether to start the service.
  --version VERSION              The version of consul to install.
  --help                         Show this message and exit.
# -*- coding: utf-8 -*-


#
# module path: pycklets.consul_service.ConsulService
#


from dataclasses import dataclass
from pyckles import AutoPycklet
from typing import *    # noqa

@dataclass
class ConsulService(AutoPycklet):
    """Create a user named 'consul' (if necessary), download the Consul binary into '/usr/local/bin' and create a
     systemd service unit ('consul') and enable/start it if so specified.

     It is recommended to create the approriate Consul server/client configuration files beforehand (in '/etc/consul.d'), but this frecklet also allows for providing basic configuration options if necessary.

       Args:
         arch: The architecture of the host system.
         consul_config: The consul configuration.
         dest: The (absolute) path to the parent folder of the downloaded executable file.
         enabled: Whether to enable the service.
         platform: The platform of the host system.
         started: Whether to start the service.
         version: The version of consul to install.

    """

    FRECKLET_ID = "consul-service"

    arch: str = None
    consul_config: Dict = None
    dest: str = None
    enabled: bool = None
    platform: str = None
    started: bool = None
    version: str = None


    def __post_init__(self):
        super(ConsulService, self).__init__(var_names=["arch", "consul_config", "dest", "enabled", "platform", "started", "version"])


frecklet_class = ConsulService
# -*- coding: utf-8 -*-


#
# module path: pycklets.consul_service.ConsulService
#


from pyckles import AutoPycklet

class ConsulService(AutoPycklet):
    """Create a user named 'consul' (if necessary), download the Consul binary into '/usr/local/bin' and create a
     systemd service unit ('consul') and enable/start it if so specified.

     It is recommended to create the approriate Consul server/client configuration files beforehand (in '/etc/consul.d'), but this frecklet also allows for providing basic configuration options if necessary.

       Args:
         arch: The architecture of the host system.
         consul_config: The consul configuration.
         dest: The (absolute) path to the parent folder of the downloaded executable file.
         enabled: Whether to enable the service.
         platform: The platform of the host system.
         started: Whether to start the service.
         version: The version of consul to install.

    """

    FRECKLET_ID = "consul-service"

    def __init__(self, arch=None, consul_config=None, dest=None, enabled=None, platform=None, started=None, version="1.5.3"):

        super(ConsulService, self).__init__(var_names=["arch", "consul_config", "dest", "enabled", "platform", "started", "version"])
        self._arch = arch
        self._consul_config = consul_config
        self._dest = dest
        self._enabled = enabled
        self._platform = platform
        self._started = started
        self._version = version

    @property
    def arch(self):
        return self._arch

    @arch.setter
    def arch(self, arch):
        self._arch = arch

    @property
    def consul_config(self):
        return self._consul_config

    @consul_config.setter
    def consul_config(self, consul_config):
        self._consul_config = consul_config

    @property
    def dest(self):
        return self._dest

    @dest.setter
    def dest(self, dest):
        self._dest = dest

    @property
    def enabled(self):
        return self._enabled

    @enabled.setter
    def enabled(self, enabled):
        self._enabled = enabled

    @property
    def platform(self):
        return self._platform

    @platform.setter
    def platform(self, platform):
        self._platform = platform

    @property
    def started(self):
        return self._started

    @started.setter
    def started(self, started):
        self._started = started

    @property
    def version(self):
        return self._version

    @version.setter
    def version(self, version):
        self._version = version



frecklet_class = ConsulService