forked from GoogleCloudPlatform/cloud-foundation-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.py
executable file
·298 lines (271 loc) · 10.4 KB
/
prepare.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
298
#!/usr/bin/env python3
# Copyright 2023 Google LLC
#
# 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.
'''Prepare OCP installation files for UPI installation on GCP.
This module helps generating installation files for OpenShift on GCP with User
Provided Infrastructure, leveraging variables set in the accompanying Terraform
files, that create the infrastructure for a cluster.
It helps supporting features like Shared VPC, CMEK encryption for disks, etc.
'''
import glob
import logging
import os
import pathlib
import re
import subprocess
import sys
import click
import hcl
from ruamel import yaml
__author__ = '[email protected]'
__version__ = '1.0'
class Error(Exception):
pass
def _parse_tfvars(tfvars=None, tfdir=None):
'Parse vars and tfvars files and return variables.'
logging.info('parsing tf variables')
result = {}
try:
with open(os.path.join(tfdir, 'variables.tf')) as f:
result = {k: v.get('default') for k, v in hcl.load(f)['variable'].items()}
if tfvars:
with open(os.path.join(tfdir, tfvars)) as f:
result.update(hcl.load(f))
else:
logging.info('no tfvars file used')
except (KeyError, ValueError) as e:
raise Error(f'Wrong variable files syntax: {e}')
except (IOError, OSError) as e:
raise Error(f'Cannot open variable files: {e}')
for k, v in result.items():
if k == 'post_bootstrap_config':
continue
if v is None:
raise Error(f'Terraform variable {k} not set.')
return result
def _check_convert_paths(**paths):
'Return dictionary of path objects, check they point to existing resources.'
logging.info('checking paths')
result = {}
for k, v in paths.items():
p = pathlib.Path(v).expanduser()
if not p.exists():
raise Error(f'Missing file/dir \'{p}\'.')
result[k] = p
return result
def _run_installer(cmdline, env=None):
'Run command and catch errors.'
logging.info(f'running command {" ".join(cmdline)}')
try:
p = subprocess.Popen(cmdline, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env or {})
except subprocess.CalledProcessError as e:
raise Error(f'Error running command: {e.output}')
out, err = p.communicate()
out = out.decode('utf-8', errors='ignore')
err = err.decode('utf-8', errors='ignore')
retcode = p.returncode
if retcode > 0:
raise Error(f'Error in openshift-installer ({retcode}): {err}')
@click.group(invoke_without_command=True,
help=f'{__doc__}\nWith no command, run through all stages.')
@click.option('--tfdir', type=click.Path(exists=True), default='./tf',
help='Terraform folder.')
@click.option('--tfvars',
help='Terraform vars file, relative to Terraform folder.')
@click.option(
'-v', '--verbosity', default='INFO',
type=click.Choice(['DEBUG', 'INFO', 'WARNING', 'ERROR',
'CRITICAL']), help='Verbosity level (logging constant).')
@click.pass_context
def cli(ctx=None, credentials=None, tfdir=None, tfvars=None, verbosity='INFO'):
'Program entry point.'
logging.basicConfig(level=getattr(logging, verbosity))
logging.info('program starting')
ctx.ensure_object(dict)
try:
tfvars_ = _parse_tfvars(tfvars, tfdir)
ctx.obj['tfvars'] = tfvars_
ctx.obj['paths'] = _check_convert_paths(**tfvars_['fs_paths'])
except Error as e:
print(f'Error: {e.args[0]}')
sys.exit(1)
if ctx.invoked_subcommand is None:
commands = [
'install-config', 'manifests', 'manifests-edit', 'ignition-configs'
]
else:
commands = [ctx.invoked_subcommand]
try:
for c in commands:
ctx.invoke(ctx.command.commands[c])
except Error as e:
print(e)
sys.exit(1)
sys.exit(0)
@cli.command(help='Generate ignition files from manifests.')
@click.pass_context
def ignition_configs(ctx=None):
'Create ignition config files from manifests.'
logging.info('generating ignition config files')
cmdline = [
str(ctx.obj['paths']['openshift_install']), 'create', 'ignition-configs',
'--dir',
str(ctx.obj['paths']['config_dir'])
]
env = {'GOOGLE_APPLICATION_CREDENTIALS': ctx.obj['paths']['credentials']}
_run_installer(cmdline, env)
@cli.command(help='Generate install config from tfvars file.')
@click.pass_context
def install_config(ctx=None):
'Create install config from terraform variables.'
logging.info('generating install config')
y = yaml.YAML()
try:
with open('install-config.tpl.yml') as f:
data = y.load(f)
except (IOError, OSError) as e:
raise Error(f'Cannot open install-config template: {e}')
except yaml.YAMLError as e:
raise Error(f'Parsing error in install-config template: {e}')
vars = ctx.obj['tfvars']
paths = ctx.obj['paths']
vars_key = vars['disk_encryption_key']
vars_net = vars['install_config_params']['network']
vars_proxy = vars['install_config_params']['proxy']
data_disk = data['compute'][0]['platform']['gcp']['osDisk']
data['baseDomain'] = vars['domain']
data['metadata']['name'] = vars['cluster_name']
data['platform']['gcp']['projectID'] = vars['service_project']['project_id']
data['platform']['gcp']['region'] = vars['region']
data_disk['diskSizeGB'] = int(vars['install_config_params']['disk_size'])
if vars_key and vars_key != 'null':
data_disk.insert(
len(data_disk), 'encryptionKey', {
'kmsKey': {
'projectID': vars_key['project_id'],
'keyRing': vars_key['keyring'],
'location': vars_key['location'],
'name': vars_key['name']
}
})
data['networking']['clusterNetwork'][0]['cidr'] = vars_net['cluster']
data['networking']['clusterNetwork'][0]['hostPrefix'] = vars_net[
'host_prefix']
data['networking']['machineNetwork'][0]['cidr'] = vars_net['machine']
data['networking']['serviceNetwork'][0] = vars_net['service']
if vars_proxy and vars_proxy != 'null':
noproxy = [t.strip() for t in vars_proxy['noproxy'].split(',') if t.strip()]
noproxy += [f'.{vars["domain"]}', vars_net['machine']]
noproxy += vars['allowed_ranges']
data.insert(
len(data), 'proxy', {
'httpProxy': vars_proxy['http'],
'httpsProxy': vars_proxy['https'],
'noProxy': ','.join(noproxy)
})
for k, v in dict(pull_secret='pullSecret', ssh_key='sshKey').items():
if k not in paths:
raise Error(f'Key \'{k}\' missing from fs_paths in Terraform variables.')
try:
with paths[k].open() as f:
data[v] = f.read().strip()
except (IOError, OSError) as e:
raise Error(f'Cannot read file: {e}')
try:
with (paths['config_dir'] / 'install-config.yaml').open('w') as f:
y.dump(data, f)
except (IOError, OSError) as e:
raise Error(f'Cannot write install config: {e}')
except yaml.YAMLError as e:
raise Error(f'Error dumping install-config template: {e}')
@cli.command(help='Generate manifests from install config.')
@click.pass_context
def manifests(ctx=None):
'Create manifests from install config.'
logging.info('generating manifests')
cmdline = [
str(ctx.obj['paths']['openshift_install']), 'create', 'manifests',
'--dir',
str(ctx.obj['paths']['config_dir'])
]
env = {'GOOGLE_APPLICATION_CREDENTIALS': ctx.obj['paths']['credentials']}
_run_installer(cmdline, env)
@cli.command(help='Edit manifests.')
@click.pass_context
def manifests_edit(ctx=None):
'Edit generated manifests.'
logging.info('edit manifests')
dir_ = ctx.obj['paths']['config_dir'] / 'openshift'
for fileobj in dir_.glob('99_openshift-cluster-api_master-machines-*.yaml'):
logging.info(f'removing {fileobj.name}')
fileobj.unlink()
tfvars = ctx.obj['tfvars']
for fileobj in dir_.glob('99_openshift-cluster-api_worker-machineset-*.yaml'):
logging.info(f'editing {fileobj.name}')
y = yaml.YAML()
try:
with fileobj.open() as f:
data = y.load(f)
data_v = data['spec']['template']['spec']['providerSpec']['value']
data_v['region'] = tfvars['region']
data_v['projectID'] = tfvars['service_project']['project_id']
if not 'ocp-worker' in data_v['tags']:
data_v['tags'].append('ocp-worker')
if tfvars['install_config_params']['labels']:
data_v['labels'] = tfvars['install_config_params']['labels'].copy()
for i, d in enumerate(data_v['disks']):
d['labels'] = tfvars['install_config_params']['labels'].copy()
data_n = data_v['networkInterfaces'][0]
data_n['network'] = tfvars['host_project']['vpc_name']
data_n['subnetwork'] = tfvars['host_project']['workers_subnet_name']
data_n.insert(len(data_n), 'projectID',
tfvars['host_project']['project_id'])
with fileobj.open('w') as f:
y.dump(data, f)
except (IOError, OSError, yaml.YAMLError) as e:
raise Error(f'error editing file {fileobj}: {e}')
dir_ = ctx.obj['paths']['config_dir'] / 'manifests'
fileobj = dir_ / 'cloud-provider-config.yaml'
vars_h = tfvars["host_project"]
logging.info(f'editing {fileobj.name}')
try:
with fileobj.open() as f:
data = y.load(f)
config = [
l for l in data['data']['config'].strip().split('\n')
if 'network-' not in l.rpartition('=')[0]
]
config += [
f'network-project-id = {vars_h["project_id"]}',
f'network-name = {vars_h["vpc_name"]}',
f'subnetwork-name = {vars_h["default_subnet_name"]}',
]
data['data']['config'] = '\n'.join(config)
with fileobj.open('w') as f:
y.dump(data, f)
except (IOError, OSError, yaml.YAMLError) as e:
raise Error(f'error editing file {fileobj}: {e}')
fileobj = dir_ / 'cluster-scheduler-02-config.yml'
logging.info(f'editing {fileobj.name}')
try:
with fileobj.open() as f:
data = y.load(f)
data['spec']['mastersSchedulable'] = False
with fileobj.open('w') as f:
y.dump(data, f)
except (IOError, OSError, yaml.YAMLError) as e:
raise Error(f'error editing file {fileobj}: {e}')
if __name__ == '__main__':
cli()