Skip to content

Commit 2eb9541

Browse files
committed
Add download-rhcos-image subcommands
1 parent de1444d commit 2eb9541

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

osia/cli.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
ARCH_S390X, ARCH_X86_64)
2929
from .installer import (delete_cluster, download_installer, install_cluster,
3030
storage)
31+
from .installer.downloader.image import download_rhcos_image
3132

3233

3334
def _identity(in_attr: str) -> str:
@@ -66,8 +67,6 @@ def _read_list(in_str: str) -> list[str]:
6667
'worker_flavor': {'help': 'flavor of worker node'},
6768
'worker_replicas': {'help': 'Number of replicas of worker nodes', 'type': int},
6869
'certificate_bundle_file': {'help': 'CA bundle file'},
69-
'images_dir': {'help': 'Directory where images should be stored', 'type': str,
70-
'default': 'images'},
7170
'skip_clean': {'help': 'Skip clean when installation fails', 'action': 'store_true'},
7271
'enable_fips': {'help': 'Enable fips mode to the cluster', 'action': 'store_true'},
7372
},
@@ -174,10 +173,20 @@ def _exec_delete_cluster(args):
174173

175174

176175
def _exec_download_insaller(args):
176+
args.cluster_name = ""
177+
args.installer = None
177178
conf = _merge_dictionaries(args)
178179
print(conf['installer'])
179180

180181

182+
def _exec_download_rhcos_image(args):
183+
args.cluster_name = ""
184+
args.installer = None
185+
args.enable_fips = None
186+
conf = _merge_dictionaries(args)
187+
print(download_rhcos_image(args.images_dir, conf['installer']))
188+
189+
181190
def _get_helper(parser: argparse.ArgumentParser):
182191
def printer(unused_conf):
183192
print("Operation not set, please specify either install or clean!")
@@ -186,12 +195,22 @@ def printer(unused_conf):
186195
return printer
187196

188197

189-
def _create_commons() -> argparse.ArgumentParser:
190-
commons = argparse.ArgumentParser(add_help=False)
198+
def _add_cluster_commons(commons: argparse.ArgumentParser) -> argparse.ArgumentParser:
191199
common_arguments: list[tuple[list[str], dict]] = [
192200
(['--cluster-name'], dict(required=True, help='Name of the cluster')),
193201
(['--installer'], dict(required=False,
194202
help='Executable binary of openshift install cli', default=None)),
203+
(['--skip-git'], dict(help='When set, the persistance will be skipped',
204+
action='store_true')),
205+
]
206+
for args, kwargs in common_arguments:
207+
commons.add_argument(*args, **kwargs)
208+
return commons
209+
210+
211+
def _create_commons() -> argparse.ArgumentParser:
212+
commons = argparse.ArgumentParser(add_help=False)
213+
common_arguments: list[tuple[list[str], dict]] = [
195214
(['--installer-version'], dict(help='Version of downloader to be downloaded',
196215
default='latest', type=str)),
197216
(['--installer-arch'], dict(help='Architecture of downloader to be downloaded',
@@ -204,8 +223,8 @@ def _create_commons() -> argparse.ArgumentParser:
204223
default='prod')),
205224
(['--installers-dir'], dict(help='Folder where installers are stored',
206225
required=False, default='installers')),
207-
(['--skip-git'], dict(help='When set, the persistance will be skipped',
208-
action='store_true')),
226+
(['--images-dir'], dict(help='Directory where images should be stored',
227+
required=False, default='images')),
209228
(['-v', '--verbose'], dict(help='Increase verbosity level', action='store_true')),
210229
]
211230
for args, kwargs in common_arguments:
@@ -216,23 +235,29 @@ def _create_commons() -> argparse.ArgumentParser:
216235
def _setup_parser() -> argparse.ArgumentParser:
217236
commons = _create_commons()
218237

238+
cluster_commons = argparse.ArgumentParser(add_help=False, parents=[commons])
239+
cluster_commons = _add_cluster_commons(cluster_commons)
240+
219241
parser = argparse.ArgumentParser("osia")
220242
parser.set_defaults(func=_get_helper(parser))
221243
sub_parsers = parser.add_subparsers()
222244

223-
install = sub_parsers.add_parser('install', help='Install new cluster', parents=[commons])
245+
install = sub_parsers.add_parser('install', help='Install new cluster', parents=[cluster_commons])
224246

225247
for arg, value in sorted({k: v for _, x in ARGUMENTS.items() for k, v in x.items()}.items()):
226248
install.add_argument(f"--{arg.replace('_', '-')}",
227249
**{k: v for k, v in value.items() if k != 'proc'})
228250
install.set_defaults(func=_exec_install_cluster)
229251

230-
clean = sub_parsers.add_parser('clean', help='Remove cluster', parents=[commons])
252+
clean = sub_parsers.add_parser('clean', help='Remove cluster', parents=[cluster_commons])
231253
clean.set_defaults(func=_exec_delete_cluster)
232254

233-
download = sub_parsers.add_parser('download-installer', help='Download installer', parents=[commons])
234-
download.add_argument("--enable-fips", action='store_true')
235-
download.set_defaults(func=_exec_download_insaller)
255+
installer = sub_parsers.add_parser('download-installer', help='Download installer', parents=[commons])
256+
installer.add_argument("--enable-fips", help='Enable fips mode to the cluster', action='store_true')
257+
installer.set_defaults(func=_exec_download_insaller)
258+
259+
rhcos_image = sub_parsers.add_parser('download-rhcos-image', help='Download rhcos image', parents=[commons])
260+
rhcos_image.set_defaults(func=_exec_download_rhcos_image)
236261

237262
return parser
238263

osia/installer/downloader/image.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,19 @@ def download_image(image_url: str, image_file: str):
9898
logging.debug("Directory %s for images already exists", directory)
9999
res_file = get_data(image_url, image_file, _extract_gzip)
100100
return res_file
101+
102+
103+
def download_rhcos_image(images_dir: str, installer: str) -> str:
104+
""" Download rhcos image """
105+
inst_url, version = get_url(installer)
106+
image_path = Path(images_dir).joinpath(f"rhcos-{version}.qcow2")
107+
image_file = None
108+
if image_path.exists():
109+
logging.info("Found image at %s", image_path.name)
110+
image_file = image_path.as_posix()
111+
return image_file
112+
113+
logging.info("Starting download of image %s", inst_url)
114+
image_file = download_image(inst_url, image_path.as_posix())
115+
116+
return image_path.as_posix()

0 commit comments

Comments
 (0)