summaryrefslogtreecommitdiffstats
path: root/src/cephadm/cephadmlib/daemons/mgmt_gateway.py
blob: 2be18809aa383558406d05a86b363324c36ab102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import logging
import os
from typing import Dict, List, Tuple, Optional
import re

from ..call_wrappers import call, CallVerbosity
from ..container_daemon_form import ContainerDaemonForm, daemon_to_container
from ..container_types import CephContainer, extract_uid_gid
from ..context import CephadmContext
from ..context_getters import fetch_configs
from ..daemon_form import register as register_daemon_form
from ..daemon_identity import DaemonIdentity
from ..deployment_utils import to_deployment_container
from ceph.cephadm.images import DefaultImages
from ..data_utils import dict_get, is_fsid
from ..file_utils import populate_files, makedirs, recursive_chown
from ..exceptions import Error

logger = logging.getLogger()


@register_daemon_form
class MgmtGateway(ContainerDaemonForm):
    """Defines an MgmtGateway container"""

    daemon_type = 'mgmt-gateway'
    required_files = [
        'nginx.conf',
        'nginx_external_server.conf',
        'nginx_internal_server.conf',
        'nginx_internal.crt',
        'nginx_internal.key',
    ]

    default_image = DefaultImages.NGINX.image_ref

    @classmethod
    def for_daemon_type(cls, daemon_type: str) -> bool:
        return cls.daemon_type == daemon_type

    def __init__(
        self,
        ctx: CephadmContext,
        fsid: str,
        daemon_id: str,
        config_json: Dict,
        image: str = DefaultImages.NGINX.image_ref,
    ):
        self.ctx = ctx
        self.fsid = fsid
        self.daemon_id = daemon_id
        self.image = image
        self.files = dict_get(config_json, 'files', {})
        self.validate()

    @classmethod
    def init(
        cls, ctx: CephadmContext, fsid: str, daemon_id: str
    ) -> 'MgmtGateway':
        return cls(ctx, fsid, daemon_id, fetch_configs(ctx), ctx.image)

    @classmethod
    def create(
        cls, ctx: CephadmContext, ident: DaemonIdentity
    ) -> 'MgmtGateway':
        return cls.init(ctx, ident.fsid, ident.daemon_id)

    @property
    def identity(self) -> DaemonIdentity:
        return DaemonIdentity(self.fsid, self.daemon_type, self.daemon_id)

    def validate(self) -> None:
        if not is_fsid(self.fsid):
            raise Error(f'not an fsid: {self.fsid}')
        if not self.daemon_id:
            raise Error(f'invalid daemon_id: {self.daemon_id}')
        if not self.image:
            raise Error(f'invalid image: {self.image}')

        # check for the required files
        if self.required_files:
            for fname in self.required_files:
                if fname not in self.files:
                    raise Error(
                        'required file missing from config-json: %s' % fname
                    )

    def container(self, ctx: CephadmContext) -> CephContainer:
        ctr = daemon_to_container(ctx, self)
        return to_deployment_container(ctx, ctr)

    def uid_gid(self, ctx: CephadmContext) -> Tuple[int, int]:
        return extract_uid_gid(ctx, file_path='/etc/nginx/')

    def get_daemon_args(self) -> List[str]:
        return []

    def default_entrypoint(self) -> str:
        return ''

    def create_daemon_dirs(self, data_dir: str, uid: int, gid: int) -> None:
        """Create files under the container data dir"""
        if not os.path.isdir(data_dir):
            raise OSError('data_dir is not a directory: %s' % (data_dir))
        logger.info('Writing mgmt-gateway config...')
        config_dir = os.path.join(data_dir, 'etc/')
        ssl_dir = os.path.join(data_dir, 'etc/ssl')
        for ddir in [config_dir, ssl_dir]:
            makedirs(ddir, uid, gid, 0o755)
            recursive_chown(ddir, uid, gid)
        conf_files = {
            fname: content
            for fname, content in self.files.items()
            if fname.endswith('.conf')
        }
        cert_files = {
            fname: content
            for fname, content in self.files.items()
            if fname.endswith('.crt') or fname.endswith('.key')
        }
        populate_files(config_dir, conf_files, uid, gid)
        populate_files(ssl_dir, cert_files, uid, gid)

    def _get_container_mounts(self, data_dir: str) -> Dict[str, str]:
        mounts: Dict[str, str] = {}
        mounts[
            os.path.join(data_dir, 'nginx.conf')
        ] = '/etc/nginx/nginx.conf:Z'
        return mounts

    @staticmethod
    def get_version(ctx: CephadmContext, container_id: str) -> Optional[str]:
        """Return the version of the Nginx container"""
        version = None
        out, err, code = call(
            ctx,
            [
                ctx.container_engine.path,
                'exec',
                container_id,
                'nginx',
                '-v',
            ],
            verbosity=CallVerbosity.QUIET,
        )
        if code == 0:
            # nginx is using stderr to print the version!!
            match = re.search(r'nginx version:\s*nginx\/(.+)', err)
            if match:
                version = match.group(1)
        return version

    def customize_container_args(
        self, ctx: CephadmContext, args: List[str]
    ) -> None:
        uid, _ = self.uid_gid(ctx)
        extra_args = [
            '--user',
            str(uid),
        ]
        args.extend(extra_args)

    def customize_process_args(
        self, ctx: CephadmContext, args: List[str]
    ) -> None:
        # The following noqa comment is intentional to suppress warnings about using double quotes
        # instead of single quotes. We use double quotes here to ensure that single quotes are
        # used in the final parsed output: nginx -g 'daemon off;'
        args.extend(['nginx', '-g', "daemon off;"])  # noqa

    def customize_container_mounts(
        self, ctx: CephadmContext, mounts: Dict[str, str]
    ) -> None:
        data_dir = self.identity.data_dir(ctx.data_dir)
        mounts.update(
            {
                os.path.join(
                    data_dir, 'etc/nginx.conf'
                ): '/etc/nginx/nginx.conf:Z',
                os.path.join(
                    data_dir, 'etc/nginx_internal_server.conf'
                ): '/etc/nginx_internal_server.conf:Z',
                os.path.join(
                    data_dir, 'etc/nginx_external_server.conf'
                ): '/etc/nginx_external_server.conf:Z',
                os.path.join(data_dir, 'etc/ssl'): '/etc/nginx/ssl/',
            }
        )