-
Notifications
You must be signed in to change notification settings - Fork 1
/
gecoscclink_step02.py
297 lines (243 loc) · 9.67 KB
/
gecoscclink_step02.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import nsis
import traceback
import sys
import os
import logging
import json
import base64
import binascii
import subprocess
debug_mode = False
def remove_file(filename):
try:
if os.path.isfile(filename):
os.remove(filename)
except:
logging.error("Error removing %s file"%(filename))
logging.error(str(traceback.format_exc()))
return False
return True
def save_file(filename, filecontent):
# Check directory
if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
# Write the content
fd = open(filename, 'w')
fd.write(filecontent)
fd.close()
return True
def clean_connection_files_on_error():
if debug_mode:
logging.info("_clean_connection_files_on_error DISABLED FOR DEBUG")
return
logging.info("_clean_connection_files_on_error")
remove_file('C:\\chef\\client.pem')
remove_file('C:\\chef\\client.rb')
remove_file('C:\\chef\\knife.rb')
remove_file('C:\\etc\\chef.control')
remove_file('C:\\etc\\gcc.control')
def execute_command(cmd, my_env={}):
try:
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=my_env)
for line in p.stdout.readlines():
logging.debug(line)
retval = p.wait()
if retval != 0:
logging.error('Error running command: %s'%(cmd))
return False
except:
logging.error('Error running command: %s'%(cmd))
logging.error(str(traceback.format_exc()))
return False
return True
# This script links the computer to the Chef server
#
# IN parameters:
# $0 = GECOS CC URL
# $1 = User
# $2 = Password
# $3 = Selected OU
# $4 = Workstation name
# $5 = Node name
#
# OUT parameters:
# $6 = true if success
#
try:
logging.basicConfig(filename=os.environ["PYLOGFILE"],level=logging.DEBUG)
logging.info('========== GECOS CC link - Step 02 ==============')
from gecosws_config_assistant.util.GecosCC import GecosCC
from gecosws_config_assistant.util.SSLUtil import SSLUtil
from gecosws_config_assistant.util.GemUtil import GemUtil
from gecosws_config_assistant.util.Template import Template
from gecosws_config_assistant.dto.GecosAccessData import GecosAccessData
from gecosws_config_assistant.dto.WorkstationData import WorkstationData
from gecosws_config_assistant.dao.WorkstationDataDAO import WorkstationDataDAO
from gecosws_config_assistant.firstboot_lib.firstbootconfig import get_data_file
# List of GEMS required to run GECOS
necessary_gems = ['json', 'rest-client', 'activesupport:4.2.11.1', 'netaddr']
nsis.setvar('6', "false")
gecosAccessData = GecosAccessData()
gecosAccessData.set_url(nsis.getvar('$0'))
gecosAccessData.set_login(nsis.getvar('$1'))
gecosAccessData.set_password(nsis.getvar('$2'))
gecoscc = GecosCC()
workstationDataDao = WorkstationDataDAO()
workstationData = workstationDataDao.load()
conf = json.loads(os.environ["AUTOCFGJSON"])
# Get client.pem from server
logging.info("Get client.pem from server")
gecosCC = GecosCC()
rekey = False
if (
gecosCC.is_registered_chef_node(
gecosAccessData,
workstationData.get_node_name()
)
):
# re-register
rekey = True
client_pem = gecosCC.reregister_chef_node(
gecosAccessData,
workstationData.get_node_name())
else:
# register
client_pem = gecosCC.register_chef_node(
gecosAccessData,
workstationData.get_node_name())
# Check the client_pem data
if client_pem is False:
logging.error('There was an error while getting the client certificate')
if not debug_mode:
gecosCC.unregister_chef_node(
gecosAccessData,
workstationData.get_node_name())
clean_connection_files_on_error()
sys.exit()
# Save Chef client certificate in a PEM file
if not save_file('c:\\chef\\client.pem', client_pem):
logging.error("There was an error while saving the client certificate")
if not debug_mode:
gecosCC.unregister_chef_node(
gecosAccessData,
workstationData.get_node_name())
clean_connection_files_on_error()
sys.exit()
logging.info("- Create c:\\chef/client.rb")
chef_admin_name = gecosAccessData.get_login()
chef_url = gecosAccessData.get_url()
chef_url = chef_url.split('//')[1].split(':')[0]
chef_url = "https://" + chef_url + '/'
if (conf is not None
and conf.has_key("chef")
and conf["chef"].has_key("chef_server_uri")
and not 'localhost' in conf["chef"]["chef_server_uri"]):
chef_url = conf["chef"]["chef_server_uri"]
logging.debug("chef_url retrieved from GECOS auto conf")
if (conf is not None
and conf.has_key("chef")
and conf["chef"].has_key("chef_admin_name")):
chef_admin_name = conf["chef"]["chef_admin_name"]
logging.debug("chef_admin_name retrieved from GECOS auto conf")
# Check Chef HTTPS certificate
if chef_url.startswith('https://'):
# Check server certificate
sslUtil = SSLUtil()
if not sslUtil.isServerCertificateTrusted(chef_url):
if (
sslUtil.getUntrustedCertificateErrorCode(chef_url) == \
SSL_R_CERTIFICATE_VERIFY_FAILED
):
# Error code SSL_R_CERTIFICATE_VERIFY_FAILED
# means that the certificate is not trusted
sslUtil.getUntrustedCertificateErrorCode(chef_url)
certificate = sslUtil.getServerCertificate(chef_url)
info = sslUtil.getCertificateInfo(certificate)
# TODO: Disable certificate validation without asking
SSLUtil.disableSSLCertificatesVerification()
else:
# Any other error code must be shown
errormsg = sslUtil.getUntrustedCertificateCause(chef_url)
logging.debug(
"Error connecting to HTTPS server: %s", errormsg)
if not debug_mode:
gecosCC.unregister_chef_node(
gecosAccessData,
workstationData.get_node_name())
clean_connection_files_on_error()
sys.exit()
template = Template()
template.source = get_data_file('templates/client.rb')
template.destination = 'c:\\chef\\client.rb'
template.owner = 'root'
template.group = 'root'
template.mode = 00644
template.variables = { 'chef_url': chef_url,
'chef_admin_name': chef_admin_name,
'chef_node_name': workstationData.get_node_name(),
'INSTDIR': os.environ["INSTDIR"].replace('\\','/')}
if not template.save():
logging.error('Error saving c:\\chef\\client.rb')
clean_connection_files_on_error()
sys.exit()
gemUtil = GemUtil()
# Check installed GEMs
for gem_name in necessary_gems:
if not gemUtil.is_gem_intalled(gem_name):
if not gemUtil.install_gem(gem_name):
# Error installing a GEM
logging.error("There was an error while installing a " +
"required GEM: " + gem_name)
clean_connection_files_on_error()
sys.exit()
logging.info('- Linking to the chef server ')
home = ''
if 'HOME' in os.environ:
home = os.environ['HOME']
elif 'USERPROFILE' in os.environ:
home = os.environ['USERPROFILE']
env = {'LANG': 'es_ES.UTF-8', 'LC_ALL': 'es_ES.UTF-8', 'HOME': home}
if 'USERPROFILE' in os.environ:
env['USERPROFILE'] = os.environ['USERPROFILE']
if 'HOMEDRIVE' in os.environ:
env['HOMEDRIVE'] = os.environ['HOMEDRIVE']
if 'HOMEPATH' in os.environ:
env['HOMEPATH'] = os.environ['HOMEPATH']
if 'SystemDrive' in os.environ:
env['SystemDrive'] = os.environ['SystemDrive']
if 'SystemRoot' in os.environ:
env['SystemRoot'] = os.environ['SystemRoot']
if 'USERNAME' in os.environ:
env['USERNAME'] = os.environ['USERNAME']
if not execute_command('C:\\opscode\\chef\\bin\\chef-client.bat -j "%s\\base.json"'%(os.path.join(os.environ['INSTDIR'], 'data')), env):
logging.error("Can't link to chef server")
clean_connection_files_on_error()
sys.exit()
#loggingdebug('- Start chef client service ')
#execute_command('service chef-client start')
logging.debug('- Create a control file ')
template = Template()
template.source = get_data_file('templates/chef.control')
template.destination = 'C:\\etc\\chef.control'
template.owner = 'root'
template.group = 'root'
template.mode = 00755
template.variables = { 'chef_url': chef_url,
'chef_admin_name': chef_admin_name,
'chef_node_name': workstationData.get_node_name()}
if not template.save():
logging.error("Can't create/modify /etc/chef.control file")
clean_connection_files_on_error()
sys.exit()
nsis.setvar('6', "true")
except SystemExit:
nsis.setvar('6', "false")
except:
nsis.setvar('4', str(sys.exc_info()[0]))
nsis.setvar('5', str(traceback.format_exc()))
logging.error(str(sys.exc_info()[0]))
logging.error(str(traceback.format_exc()))
raise