summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/cli/env.py
blob: 84bdebed6d6975b2ed86265d51a82d52ee79a5ed (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
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Environment (private)
"""

import os
import shutil

from .config import config
from .logger import Logging
from .. import (application_model_storage, application_resource_storage)
from ..orchestrator.plugin import PluginManager
from ..storage.sql_mapi import SQLAlchemyModelAPI
from ..storage.filesystem_rapi import FileSystemResourceAPI


ARIA_DEFAULT_WORKDIR_NAME = '.aria'


class _Environment(object):

    def __init__(self, workdir):

        self._workdir = workdir
        self._init_workdir()

        self._config = config.CliConfig.create_config(workdir)
        self._logging = Logging(self._config)

        self._model_storage_dir = os.path.join(workdir, 'models')
        self._resource_storage_dir = os.path.join(workdir, 'resources')
        self._plugins_dir = os.path.join(workdir, 'plugins')

        # initialized lazily
        self._model_storage = None
        self._resource_storage = None
        self._plugin_manager = None

    @property
    def workdir(self):
        return self._workdir

    @property
    def config(self):
        return self._config

    @property
    def logging(self):
        return self._logging

    @property
    def model_storage(self):
        if not self._model_storage:
            self._model_storage = self._init_sqlite_model_storage()
        return self._model_storage

    @property
    def resource_storage(self):
        if not self._resource_storage:
            self._resource_storage = self._init_fs_resource_storage()
        return self._resource_storage

    @property
    def plugin_manager(self):
        if not self._plugin_manager:
            self._plugin_manager = self._init_plugin_manager()
        return self._plugin_manager

    def reset(self, reset_config):
        if reset_config:
            shutil.rmtree(self._workdir)
        else:
            _, dirs, files = next(os.walk(self._workdir))
            files.remove(config.CONFIG_FILE_NAME)

            for dir_ in dirs:
                shutil.rmtree(os.path.join(self._workdir, dir_))
            for file_ in files:
                os.remove(os.path.join(self._workdir, file_))

    def _init_workdir(self):
        if not os.path.exists(self._workdir):
            os.makedirs(self._workdir)

    def _init_sqlite_model_storage(self):
        if not os.path.exists(self._model_storage_dir):
            os.makedirs(self._model_storage_dir)

        initiator_kwargs = dict(base_dir=self._model_storage_dir)
        return application_model_storage(
            SQLAlchemyModelAPI,
            initiator_kwargs=initiator_kwargs)

    def _init_fs_resource_storage(self):
        if not os.path.exists(self._resource_storage_dir):
            os.makedirs(self._resource_storage_dir)

        fs_kwargs = dict(directory=self._resource_storage_dir)
        return application_resource_storage(
            FileSystemResourceAPI,
            api_kwargs=fs_kwargs)

    def _init_plugin_manager(self):
        if not os.path.exists(self._plugins_dir):
            os.makedirs(self._plugins_dir)

        return PluginManager(self.model_storage, self._plugins_dir)


env = _Environment(os.path.join(
    os.environ.get('ARIA_WORKDIR', os.path.expanduser('~')), ARIA_DEFAULT_WORKDIR_NAME))

logger = env.logging.logger