forked from apache/cloudstack-cloudmonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
191 lines (168 loc) · 6.83 KB
/
Copy pathconfig.py
File metadata and controls
191 lines (168 loc) · 6.83 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
# -*- coding: utf-8 -*-
# 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.
__version__ = "5.2.0"
__description__ = "Command Line Interface for Apache CloudStack"
__maintainer__ = "The Apache CloudStack Team"
__maintaineremail__ = "dev@cloudstack.apache.org"
__project__ = "The Apache CloudStack Team"
__projectemail__ = "dev@cloudstack.apache.org"
__projecturl__ = "http://cloudstack.apache.org"
try:
import os
import sys
from ConfigParser import ConfigParser
from os.path import expanduser
except ImportError, e:
print "ImportError", e
param_type = ['boolean', 'date', 'float', 'integer', 'short', 'list',
'long', 'object', 'map', 'string', 'tzdate', 'uuid']
iterable_type = ['set', 'list', 'object']
config_dir = expanduser('~/.cloudmonkey')
config_file = expanduser(config_dir + '/config')
# cloudmonkey config fields
mandatory_sections = ['core', 'ui']
default_profile_name = 'local'
config_fields = {'core': {}, 'ui': {}}
# core
config_fields['core']['asyncblock'] = 'true'
config_fields['core']['paramcompletion'] = 'false'
config_fields['core']['cache_file'] = expanduser(config_dir + '/cache')
config_fields['core']['history_file'] = expanduser(config_dir + '/history')
config_fields['core']['log_file'] = expanduser(config_dir + '/log')
config_fields['core']['profile'] = default_profile_name
# ui
config_fields['ui']['color'] = 'true'
config_fields['ui']['prompt'] = '🐵 > '
config_fields['ui']['display'] = 'default'
# default profile
default_profile = {}
default_profile['url'] = 'http://localhost:8080/client/api'
default_profile['timeout'] = '3600'
default_profile['expires'] = '600'
default_profile['username'] = 'admin'
default_profile['password'] = 'password'
default_profile['apikey'] = ''
default_profile['secretkey'] = ''
def write_config(get_attr, config_file):
global config_fields, mandatory_sections, default_profile, default_profile_name
config = ConfigParser()
if os.path.exists(config_file):
try:
with open(config_file, 'r') as cfg:
config.readfp(cfg)
except IOError, e:
print "Error: config_file not found", e
profile = None
try:
profile = get_attr('profile')
except AttributeError, e:
pass
if profile is None or profile == '':
profile = default_profile_name
if profile in mandatory_sections:
print "Server profile name cannot be", profile
sys.exit(1)
has_profile_changed = False
profile_in_use = default_profile_name
try:
profile_in_use = config.get('core', 'profile')
except Exception:
pass
if profile_in_use != profile:
has_profile_changed = True
for section in (mandatory_sections + [profile]):
if not config.has_section(section):
try:
config.add_section(section)
if section not in mandatory_sections:
for key in default_profile.keys():
config.set(section, key, default_profile[key])
else:
for key in config_fields[section].keys():
config.set(section, key, config_fields[section][key])
except ValueError, e:
print "Server profile name cannot be", profile
sys.exit(1)
if section in mandatory_sections:
section_keys = config_fields[section].keys()
else:
section_keys = default_profile.keys()
for key in section_keys:
try:
if not (has_profile_changed and section == profile):
config.set(section, key, get_attr(key))
except Exception:
pass
with open(config_file, 'w') as cfg:
config.write(cfg)
return config
def read_config(get_attr, set_attr, config_file):
global config_fields, config_dir, mandatory_sections, default_profile, default_profile_name
if not os.path.exists(config_dir):
os.makedirs(config_dir)
config_options = reduce(lambda x, y: x + y, map(lambda x:
config_fields[x].keys(), config_fields.keys()))
config_options += default_profile.keys()
config = ConfigParser()
if os.path.exists(config_file):
try:
with open(config_file, 'r') as cfg:
config.readfp(cfg)
except IOError, e:
print "Error: config_file not found", e
else:
config = write_config(get_attr, config_file)
print "Welcome! Use the `set` command to configure options"
print "Config file:", config_file
print "After setting up, run the `sync` command to sync apis\n"
missing_keys = []
profile = config.get('core', 'profile')
if profile is None or profile == '' or profile in mandatory_sections:
print "Server profile cannot be", profile
sys.exit(1)
if not config.has_section(profile):
print "Selected profile (%s) does not exit, will use the defaults" % profile
try:
config.add_section(profile)
except ValueError, e:
print "Server profile name cannot be", profile
sys.exit(1)
for key in default_profile.keys():
config.set(profile, key, default_profile[key])
for section in (mandatory_sections + [profile]):
if section in mandatory_sections:
section_keys = config_fields[section].keys()
else:
section_keys = default_profile.keys()
for key in section_keys:
try:
set_attr(key, config.get(section, key))
except Exception, e:
if section in mandatory_sections:
set_attr(key, config_fields[section][key])
else:
set_attr(key, default_profile[key])
missing_keys.append(key)
# Cosmetic fix for prompt
if key == 'prompt':
set_attr(key, get_attr('prompt').strip() + " ")
if len(missing_keys) > 0:
print "Missing configuration was set using default values for keys:"
print "`%s` in %s" % (', '.join(missing_keys), config_file)
write_config(get_attr, config_file)
return config_options