-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathinit.py
More file actions
203 lines (164 loc) · 6.34 KB
/
init.py
File metadata and controls
203 lines (164 loc) · 6.34 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed 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.
############
import os
from cloudify_cli import blueprint, env, exceptions, local
from cloudify_cli.cli import cfy
from cloudify_cli.config import config
from cloudify_cli.exceptions import CloudifyCliError
from cloudify_cli.logger import DEFAULT_LOG_FILE, configure_loggers
@cfy.command(name='init',)
@cfy.argument('blueprint-path', required=False)
@cfy.options.blueprint_filename()
@cfy.options.blueprint_id(required=False, validate=True)
@cfy.options.reset_context
@cfy.options.inputs
@cfy.options.install_plugins
@cfy.options.init_hard_reset
@cfy.options.enable_colors
@cfy.options.common_options
@cfy.pass_logger
def init(blueprint_path,
blueprint_filename,
blueprint_id,
reset_context,
inputs,
install_plugins,
hard,
enable_colors,
logger):
"""Initialize a Cloudify environment.
This is required to perform many actions and should be the first
action performed after installing Cloudify.
Note: Running `cfy install` or `cfy profiles use` will
initialize an environment automatically.
Providing a `BLUEPRINT_PATH` will also initialize a blueprint to
work on.
After initialization, the CLI's configuration can be found under
~/.cloudify/config.yaml. For more information refer to the docs
at http://docs.getcloudify.org
"""
profile_name = 'local'
if blueprint_path:
if reset_context or hard:
logger.warning(
'The `--reset-context` and `--hard` flags are ignored '
'when initializing a blueprint')
init_local_profile(
reset_context=True,
hard=False,
enable_colors=enable_colors
)
env.set_active_profile(profile_name)
processed_blueprint_path = blueprint.get(
blueprint_path,
blueprint_filename
)
blueprint_id = blueprint_id or blueprint.generate_id(
processed_blueprint_path,
blueprint_filename
)
if local.blueprint_exists(blueprint_id):
local.remove(blueprint_id)
try:
storage = local.get_storage()
local.initialize_blueprint(
blueprint_path=processed_blueprint_path,
name=blueprint_id or 'local',
inputs=inputs,
storage=storage,
install_plugins=install_plugins,
resolver=config.get_import_resolver()
)
except ImportError as e:
e.possible_solutions = [
"Run `cfy init {0} --install-plugins`".format(blueprint_path),
"Run `cfy install-plugins {0}`".format(blueprint_path)
]
raise
logger.info("Initialized {0}\nIf you make changes to the "
"blueprint, run `cfy init {0}` "
"again to apply them".format(blueprint_path))
else:
if env.is_initialized() and not (reset_context or hard):
raise CloudifyCliError(
'Environment is already initialized. '
'You can reset the environment by running `cfy init -r`')
init_local_profile(reset_context, hard, enable_colors)
env.set_active_profile(profile_name)
@cfy.pass_logger
def init_local_profile(reset_context=False,
hard=False,
enable_colors=False,
logger=None):
logger.info('Initializing local profile ...')
if reset_context:
if hard:
os.remove(config.CLOUDIFY_CONFIG_PATH)
_create_profiles_dir_and_config(hard, enable_colors)
logger.info('Initialization completed successfully')
@cfy.pass_logger
def init_manager_profile(profile_name,
reset_context=False,
hard=False,
enable_colors=False,
logger=None,
profile=None):
logger.info('Initializing profile {0}...'.format(profile_name))
context_file_path = env.get_context_path(profile_name)
if context_file_path and os.path.isfile(context_file_path):
if reset_context:
if hard:
os.remove(config.CLOUDIFY_CONFIG_PATH)
else:
os.remove(context_file_path)
else:
_raise_initialized_error(profile_name)
_create_profiles_dir_and_config(hard, enable_colors)
if not profile:
profile = env.ProfileContext()
profile.manager_ip = profile_name
profile.save()
logger.info('Initialization completed successfully')
def _create_profiles_dir_and_config(hard, enable_colors):
if not os.path.isdir(env.PROFILES_DIR):
os.makedirs(env.PROFILES_DIR, mode=0o700)
if not env.config_initialized_with_logging() or hard:
set_config(enable_colors=enable_colors)
configure_loggers()
def _raise_initialized_error(profile_name):
error = exceptions.CloudifyCliError(
'{0} profile already initialized'.format(profile_name))
error.possible_solutions = [
"Run 'cfy init -r' to force re-initialization "
]
raise error
def set_config(enable_colors=False):
config_template = """---
colors: {enable_colors}
logging:
# path to a file where cli logs will be saved.
filename: {log_path}
# configuring level per logger
loggers:
# main logger of the cli. provides basic descriptions for executed operations.
cloudify.cli.main: info
# rest client http logs, including headers.
cloudify.rest_client.http: info
""" # noqa
with open(config.CLOUDIFY_CONFIG_PATH, 'w') as f:
f.write(config_template.format(enable_colors=enable_colors,
log_path=DEFAULT_LOG_FILE))
f.write(os.linesep)