Skip to content

Commit dddf2bd

Browse files
committed
Add download-rhcos-image subcommands
1 parent b485118 commit dddf2bd

File tree

5 files changed

+63
-35
lines changed

5 files changed

+63
-35
lines changed

osia/cli.py

Lines changed: 39 additions & 12 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,11 +197,20 @@ 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'], {"required": True, "help": "Name of the cluster"}),
193203
(['--installer'], {"required": False, "help": 'Executable binary of openshift install cli', "default": None}),
204+
(['--skip-git'], {"help": 'When set, the persistance will be skipped', "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]] = [
194214
(['--installer-version'], {"help": 'Version of downloader to be downloaded', "default": 'latest', "type": str}),
195215
(['--installer-arch'], {"help": 'Architecture of downloader to be downloaded',
196216
"choices": [ARCH_AMD, ARCH_X86_64, ARCH_ARM, ARCH_AARCH64, ARCH_PPC, ARCH_S390X],
@@ -199,7 +219,8 @@ def _create_commons() -> argparse.ArgumentParser:
199219
"choices": ["prod", "devel", "prev"], "default": 'prod'}),
200220
(['--installers-dir'], {"help": 'Folder where installers are stored', "required": False,
201221
"default": 'installers'}),
202-
(['--skip-git'], {"help": 'When set, the persistance will be skipped', "action": 'store_true'}),
222+
(['--images-dir'], {"help": 'Directory where images should be stored', "required": False,
223+
"default": 'images'}),
203224
(['-v', '--verbose'], {"help": 'Increase verbosity level', "action": 'store_true'}),
204225
]
205226
for args, kwargs in common_arguments:
@@ -210,23 +231,29 @@ def _create_commons() -> argparse.ArgumentParser:
210231
def _setup_parser() -> argparse.ArgumentParser:
211232
commons = _create_commons()
212233

234+
cluster_commons = argparse.ArgumentParser(add_help=False, parents=[commons])
235+
cluster_commons = _add_cluster_commons(cluster_commons)
236+
213237
parser = argparse.ArgumentParser("osia")
214238
parser.set_defaults(func=_get_helper(parser))
215239
sub_parsers = parser.add_subparsers()
216240

217-
install = sub_parsers.add_parser('install', help='Install new cluster', parents=[commons])
241+
install = sub_parsers.add_parser('install', help='Install new cluster', parents=[cluster_commons])
218242

219243
for arg, value in sorted({k: v for _, x in ARGUMENTS.items() for k, v in x.items()}.items()):
220244
install.add_argument(f"--{arg.replace('_', '-')}",
221245
**{k: v for k, v in value.items() if k != 'proc'})
222246
install.set_defaults(func=_exec_install_cluster)
223247

224-
clean = sub_parsers.add_parser('clean', help='Remove cluster', parents=[commons])
248+
clean = sub_parsers.add_parser('clean', help='Remove cluster', parents=[cluster_commons])
225249
clean.set_defaults(func=_exec_delete_cluster)
226250

227-
download = sub_parsers.add_parser('download-installer', help='Download installer', parents=[commons])
228-
download.add_argument("--enable-fips", action='store_true')
229-
download.set_defaults(func=_exec_download_insaller)
251+
installer = sub_parsers.add_parser('download-installer', help='Download installer', parents=[commons])
252+
installer.add_argument("--enable-fips", help='Enable fips mode to the cluster', action='store_true')
253+
installer.set_defaults(func=_exec_download_insaller)
254+
255+
rhcos_image = sub_parsers.add_parser('download-rhcos-image', help='Download rhcos image', parents=[commons])
256+
rhcos_image.set_defaults(func=_exec_download_rhcos_image)
230257

231258
return parser
232259

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)