Skip to content

Commit 939ac6b

Browse files
committed
Add download-rhcos-image subcommands
1 parent de1444d commit 939ac6b

File tree

5 files changed

+64
-36
lines changed

5 files changed

+64
-36
lines changed

osia/cli.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020

2121
import coloredlogs # type: ignore[import-untyped]
2222
import distro
23-
from semantic_version import (SimpleSpec, # type: ignore[import-untyped]
24-
Version)
23+
from semantic_version import SimpleSpec # type: ignore[import-untyped]
24+
from semantic_version import Version
2525

2626
from .config import read_config
2727
from .config.config import (ARCH_AARCH64, ARCH_AMD, ARCH_ARM, ARCH_PPC,
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, get_url
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,22 @@ 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+
188+
url, version = get_url(conf['installer'])
189+
print(download_rhcos_image(args.images_dir, url, version))
190+
191+
181192
def _get_helper(parser: argparse.ArgumentParser):
182193
def printer(unused_conf):
183194
print("Operation not set, please specify either install or clean!")
@@ -186,12 +197,22 @@ def printer(unused_conf):
186197
return printer
187198

188199

189-
def _create_commons() -> argparse.ArgumentParser:
190-
commons = argparse.ArgumentParser(add_help=False)
200+
def _add_cluster_commons(commons: argparse.ArgumentParser) -> argparse.ArgumentParser:
191201
common_arguments: list[tuple[list[str], dict]] = [
192202
(['--cluster-name'], dict(required=True, help='Name of the cluster')),
193203
(['--installer'], dict(required=False,
194204
help='Executable binary of openshift install cli', default=None)),
205+
(['--skip-git'], dict(help='When set, the persistance will be skipped',
206+
action='store_true')),
207+
]
208+
for args, kwargs in common_arguments:
209+
commons.add_argument(*args, **kwargs)
210+
return commons
211+
212+
213+
def _create_commons() -> argparse.ArgumentParser:
214+
commons = argparse.ArgumentParser(add_help=False)
215+
common_arguments: list[tuple[list[str], dict]] = [
195216
(['--installer-version'], dict(help='Version of downloader to be downloaded',
196217
default='latest', type=str)),
197218
(['--installer-arch'], dict(help='Architecture of downloader to be downloaded',
@@ -204,8 +225,8 @@ def _create_commons() -> argparse.ArgumentParser:
204225
default='prod')),
205226
(['--installers-dir'], dict(help='Folder where installers are stored',
206227
required=False, default='installers')),
207-
(['--skip-git'], dict(help='When set, the persistance will be skipped',
208-
action='store_true')),
228+
(['--images-dir'], dict(help='Directory where images should be stored',
229+
required=False, default='images')),
209230
(['-v', '--verbose'], dict(help='Increase verbosity level', action='store_true')),
210231
]
211232
for args, kwargs in common_arguments:
@@ -216,23 +237,29 @@ def _create_commons() -> argparse.ArgumentParser:
216237
def _setup_parser() -> argparse.ArgumentParser:
217238
commons = _create_commons()
218239

240+
cluster_commons = argparse.ArgumentParser(add_help=False, parents=[commons])
241+
cluster_commons = _add_cluster_commons(cluster_commons)
242+
219243
parser = argparse.ArgumentParser("osia")
220244
parser.set_defaults(func=_get_helper(parser))
221245
sub_parsers = parser.add_subparsers()
222246

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

225249
for arg, value in sorted({k: v for _, x in ARGUMENTS.items() for k, v in x.items()}.items()):
226250
install.add_argument(f"--{arg.replace('_', '-')}",
227251
**{k: v for k, v in value.items() if k != 'proc'})
228252
install.set_defaults(func=_exec_install_cluster)
229253

230-
clean = sub_parsers.add_parser('clean', help='Remove cluster', parents=[commons])
254+
clean = sub_parsers.add_parser('clean', help='Remove cluster', parents=[cluster_commons])
231255
clean.set_defaults(func=_exec_delete_cluster)
232256

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)
257+
installer = sub_parsers.add_parser('download-installer', help='Download installer', parents=[commons])
258+
installer.add_argument("--enable-fips", help='Enable fips mode to the cluster', action='store_true')
259+
installer.set_defaults(func=_exec_download_insaller)
260+
261+
rhcos_image = sub_parsers.add_parser('download-rhcos-image', help='Download rhcos image', parents=[commons])
262+
rhcos_image.set_defaults(func=_exec_download_rhcos_image)
236263

237264
return parser
238265

osia/installer/clouds/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
from typing import Protocol
2525

2626
from jinja2 import Environment, PackageLoader
27-
from semantic_version import (SimpleSpec, # type: ignore[import-untyped]
28-
Version)
27+
from semantic_version import SimpleSpec # type: ignore[import-untyped]
28+
from semantic_version import Version
2929

3030

3131
class AbstractInstaller(ABC):

osia/installer/clouds/openstack.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from openstack.network.v2.port import Port
2828

2929
from osia.installer.clouds.base import AbstractInstaller
30-
from osia.installer.downloader import download_image, get_url
30+
from osia.installer.downloader import download_rhcos_image, get_url
3131

3232

3333
class ImageException(Exception):
@@ -157,16 +157,9 @@ def upload_uniq_image(osp_connection: Connection,
157157
images_dir: str,
158158
installer: str):
159159
"""Function uploads unique image to the cluster, instead of making shared one"""
160-
inst_url, version = get_url(installer)
160+
url, version = get_url(installer)
161161
image_name = f"osia-{cluster_name}-{version}"
162-
image_path = Path(images_dir).joinpath(f"rhcos-{version}.qcow2")
163-
image_file = None
164-
if image_path.exists():
165-
logging.info("Found image at %s", image_path.name)
166-
image_file = image_path.as_posix()
167-
else:
168-
logging.info("Starting download of image %s", inst_url)
169-
image_file = download_image(inst_url, image_path.as_posix())
162+
image_file = download_rhcos_image(images_dir, url, version)
170163

171164
logging.info("Starting upload of image into openstack")
172165
osp_connection.create_image(image_name, filename=image_file,
@@ -190,18 +183,11 @@ def resolve_image(osp_connection: Connection,
190183
error: Exception | None):
191184
"""Function searches for image in openstack and creates it
192185
if it doesn't exist"""
193-
inst_url, version = get_url(installer)
186+
url, version = get_url(installer)
194187
image_name = f"osia-rhcos-{version}"
195188
image = osp_connection.image.find_image(image_name, ignore_missing=True)
196189
if image is None:
197-
image_path = Path(images_dir).joinpath(f"rhcos-{version}.qcow2")
198-
image_file = None
199-
if image_path.exists():
200-
logging.info("Found image at %s", image_path.name)
201-
image_file = image_path.as_posix()
202-
else:
203-
logging.info("Starting download of image %s", inst_url)
204-
image_file = download_image(inst_url, image_path.as_posix())
190+
image_file = download_rhcos_image(images_dir, url, version)
205191

206192
logging.info("Starting upload of image into openstack")
207193
osp_connection.create_image(image_name, filename=image_file,

osia/installer/downloader/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
"""Module implements download logic of resources
1616
required by installer"""
17-
from .image import download_image, get_url
17+
from .image import download_image, download_rhcos_image, get_url
1818
from .install import download_installer
1919

20-
__all__ = ['download_installer', 'download_image', 'get_url']
20+
__all__ = ['download_installer', 'download_image', 'download_rhcos_image', 'get_url']

osia/installer/downloader/image.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,18 @@ 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, url: str, version: str) -> str:
104+
""" Download rhcos image """
105+
image_path = Path(images_dir).joinpath(f"rhcos-{version}.qcow2")
106+
image_file = None
107+
if image_path.exists():
108+
logging.info("Found image at %s", image_path.name)
109+
image_file = image_path.as_posix()
110+
return image_file
111+
112+
logging.info("Starting download of image %s", url)
113+
image_file = download_image(url, image_path.as_posix())
114+
115+
return image_path.as_posix()

0 commit comments

Comments
 (0)