vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / cli / env.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 Environment (private)
18 """
19
20 import os
21 import shutil
22
23 from .config import config
24 from .logger import Logging
25 from .. import (application_model_storage, application_resource_storage)
26 from ..orchestrator.plugin import PluginManager
27 from ..storage.sql_mapi import SQLAlchemyModelAPI
28 from ..storage.filesystem_rapi import FileSystemResourceAPI
29
30
31 ARIA_DEFAULT_WORKDIR_NAME = '.aria'
32
33
34 class _Environment(object):
35
36     def __init__(self, workdir):
37
38         self._workdir = workdir
39         self._init_workdir()
40
41         self._config = config.CliConfig.create_config(workdir)
42         self._logging = Logging(self._config)
43
44         self._model_storage_dir = os.path.join(workdir, 'models')
45         self._resource_storage_dir = os.path.join(workdir, 'resources')
46         self._plugins_dir = os.path.join(workdir, 'plugins')
47
48         # initialized lazily
49         self._model_storage = None
50         self._resource_storage = None
51         self._plugin_manager = None
52
53     @property
54     def workdir(self):
55         return self._workdir
56
57     @property
58     def config(self):
59         return self._config
60
61     @property
62     def logging(self):
63         return self._logging
64
65     @property
66     def model_storage(self):
67         if not self._model_storage:
68             self._model_storage = self._init_sqlite_model_storage()
69         return self._model_storage
70
71     @property
72     def resource_storage(self):
73         if not self._resource_storage:
74             self._resource_storage = self._init_fs_resource_storage()
75         return self._resource_storage
76
77     @property
78     def plugin_manager(self):
79         if not self._plugin_manager:
80             self._plugin_manager = self._init_plugin_manager()
81         return self._plugin_manager
82
83     def reset(self, reset_config):
84         if reset_config:
85             shutil.rmtree(self._workdir)
86         else:
87             _, dirs, files = next(os.walk(self._workdir))
88             files.remove(config.CONFIG_FILE_NAME)
89
90             for dir_ in dirs:
91                 shutil.rmtree(os.path.join(self._workdir, dir_))
92             for file_ in files:
93                 os.remove(os.path.join(self._workdir, file_))
94
95     def _init_workdir(self):
96         if not os.path.exists(self._workdir):
97             os.makedirs(self._workdir)
98
99     def _init_sqlite_model_storage(self):
100         if not os.path.exists(self._model_storage_dir):
101             os.makedirs(self._model_storage_dir)
102
103         initiator_kwargs = dict(base_dir=self._model_storage_dir)
104         return application_model_storage(
105             SQLAlchemyModelAPI,
106             initiator_kwargs=initiator_kwargs)
107
108     def _init_fs_resource_storage(self):
109         if not os.path.exists(self._resource_storage_dir):
110             os.makedirs(self._resource_storage_dir)
111
112         fs_kwargs = dict(directory=self._resource_storage_dir)
113         return application_resource_storage(
114             FileSystemResourceAPI,
115             api_kwargs=fs_kwargs)
116
117     def _init_plugin_manager(self):
118         if not os.path.exists(self._plugins_dir):
119             os.makedirs(self._plugins_dir)
120
121         return PluginManager(self.model_storage, self._plugins_dir)
122
123
124 env = _Environment(os.path.join(
125     os.environ.get('ARIA_WORKDIR', os.path.expanduser('~')), ARIA_DEFAULT_WORKDIR_NAME))
126
127 logger = env.logging.logger