|
9 | 9 | import yaml |
10 | 10 | import zipfile |
11 | 11 |
|
12 | | -from . import jasyncio |
| 12 | +from . import jasyncio, origin, errors |
13 | 13 |
|
14 | 14 |
|
15 | 15 | async def execute_process(*cmd, log=None): |
@@ -234,19 +234,172 @@ def generate_user_controller_access_token(username, controller_endpoints, secret |
234 | 234 | return base64.urlsafe_b64encode(registration_string) |
235 | 235 |
|
236 | 236 |
|
237 | | -def get_local_charm_metadata(path): |
| 237 | +def get_local_charm_data(path, yaml_file): |
238 | 238 | """Retrieve Metadata of a Charm from its path |
239 | 239 |
|
240 | 240 | :patam str path: Path of charm directory or .charm file |
| 241 | + :patam str yaml_file: name of the yaml file, can be either |
| 242 | + "metadata.yaml", or "manifest.yaml", or "charmcraft.yaml" |
241 | 243 |
|
242 | 244 | :return: Object of charm metadata |
243 | 245 | """ |
244 | 246 | if str(path).endswith('.charm'): |
245 | 247 | with zipfile.ZipFile(str(path), 'r') as charm_file: |
246 | | - metadata = yaml.load(charm_file.read('metadata.yaml'), Loader=yaml.FullLoader) |
| 248 | + metadata = yaml.load(charm_file.read(yaml_file), Loader=yaml.FullLoader) |
247 | 249 | else: |
248 | 250 | entity_path = Path(path) |
249 | | - metadata_path = entity_path / 'metadata.yaml' |
| 251 | + metadata_path = entity_path / yaml_file |
| 252 | + if not metadata_path.exists(): |
| 253 | + return {} |
250 | 254 | metadata = yaml.load(metadata_path.read_text(), Loader=yaml.FullLoader) |
251 | 255 |
|
252 | 256 | return metadata |
| 257 | + |
| 258 | + |
| 259 | +def get_local_charm_metadata(path): |
| 260 | + return get_local_charm_data(path, 'metadata.yaml') |
| 261 | + |
| 262 | + |
| 263 | +def get_local_charm_manifest(path): |
| 264 | + return get_local_charm_data(path, 'manifest.yaml') |
| 265 | + |
| 266 | + |
| 267 | +def get_local_charm_charmcraft_yaml(path): |
| 268 | + return get_local_charm_data(path, 'charmcraft.yaml') |
| 269 | + |
| 270 | + |
| 271 | +PRECISE = "precise" |
| 272 | +QUANTAL = "quantal" |
| 273 | +RARING = "raring" |
| 274 | +SAUCY = "saucy" |
| 275 | +TRUSTY = "trusty" |
| 276 | +UTOPIC = "utopic" |
| 277 | +VIVID = "vivid" |
| 278 | +WILY = "wily" |
| 279 | +XENIAL = "xenial" |
| 280 | +YAKKETY = "yakkety" |
| 281 | +ZESTY = "zesty" |
| 282 | +ARTFUL = "artful" |
| 283 | +BIONIC = "bionic" |
| 284 | +COSMIC = "cosmic" |
| 285 | +DISCO = "disco" |
| 286 | +EOAN = "eoan" |
| 287 | +FOCAL = "focal" |
| 288 | +GROOVY = "groovy" |
| 289 | +HIRSUTE = "hirsute" |
| 290 | +IMPISH = "impish" |
| 291 | +JAMMY = "jammy" |
| 292 | +KINETIC = "kinetic" |
| 293 | + |
| 294 | +UBUNTU_SERIES = { |
| 295 | + PRECISE: "12.04", |
| 296 | + QUANTAL: "12.10", |
| 297 | + RARING: "13.04", |
| 298 | + SAUCY: "13.10", |
| 299 | + TRUSTY: "14.04", |
| 300 | + UTOPIC: "14.10", |
| 301 | + VIVID: "15.04", |
| 302 | + WILY: "15.10", |
| 303 | + XENIAL: "16.04", |
| 304 | + YAKKETY: "16.10", |
| 305 | + ZESTY: "17.04", |
| 306 | + ARTFUL: "17.10", |
| 307 | + BIONIC: "18.04", |
| 308 | + COSMIC: "18.10", |
| 309 | + DISCO: "19.04", |
| 310 | + EOAN: "19.10", |
| 311 | + FOCAL: "20.04", |
| 312 | + GROOVY: "20.10", |
| 313 | + HIRSUTE: "21.04", |
| 314 | + IMPISH: "21.10", |
| 315 | + JAMMY: "22.04", |
| 316 | + KINETIC: "22.10", |
| 317 | +} |
| 318 | + |
| 319 | + |
| 320 | +def get_series_version(series_name): |
| 321 | + if series_name not in UBUNTU_SERIES: |
| 322 | + raise errors.JujuError("Unknown series : %s", series_name) |
| 323 | + return UBUNTU_SERIES[series_name] |
| 324 | + |
| 325 | + |
| 326 | +def get_version_series(version): |
| 327 | + if version not in UBUNTU_SERIES.values(): |
| 328 | + raise errors.JujuError("Unknown version : %s", version) |
| 329 | + return list(UBUNTU_SERIES.keys())[list(UBUNTU_SERIES.values()).index(version)] |
| 330 | + |
| 331 | + |
| 332 | +def get_local_charm_base(series, channel_from_arg, charm_metadata, |
| 333 | + charm_path, baseCls): |
| 334 | + """Deduce the base [channel/osname] of a local charm based on what we |
| 335 | + know already |
| 336 | +
|
| 337 | + :param str series: This may come from the argument or the metadata.yaml |
| 338 | + :param str channel_from_arg: This is channel passed as argument, if any. |
| 339 | + :param dict charm_metadata: metadata.yaml |
| 340 | + :param str charm_path: Path of charm directory/.charm file |
| 341 | + :param class baseCls: |
| 342 | + :return: Instance of the baseCls with channel/osname informaiton |
| 343 | + """ |
| 344 | + |
| 345 | + channel_for_base = '' |
| 346 | + os_name_for_base = '' |
| 347 | + # If user passed a channel_arg, then check the supported series against |
| 348 | + # the channel's track |
| 349 | + chnl_check = origin.Channel.parse(channel_from_arg) if channel_from_arg \ |
| 350 | + else None |
| 351 | + if chnl_check: |
| 352 | + not_supported_error = errors.JujuError( |
| 353 | + "Given channel [track/risk] is not supported --" |
| 354 | + "\n - Given channel : %s" |
| 355 | + "\n - Series in Charm Metadata : %s" % |
| 356 | + (channel_from_arg, charm_metadata['series'])) |
| 357 | + channel_for_base = chnl_check.track |
| 358 | + intented_series = get_version_series(channel_for_base) |
| 359 | + if intented_series not in charm_metadata['series']: |
| 360 | + raise not_supported_error |
| 361 | + # Also check the manifest if there's one |
| 362 | + charm_manifest = get_local_charm_manifest(charm_path) |
| 363 | + if 'bases' in charm_manifest: |
| 364 | + for base in charm_manifest['bases']: |
| 365 | + if channel_for_base == base['channel']: |
| 366 | + break |
| 367 | + else: |
| 368 | + raise not_supported_error |
| 369 | + |
| 370 | + # If we know the series, use it to get a channel |
| 371 | + if channel_for_base == '': |
| 372 | + channel_for_base = get_series_version(series) if series else '' |
| 373 | + if channel_for_base: |
| 374 | + # we currently only support ubuntu series (statically) |
| 375 | + # TODO (cderici) : go juju/core/series/supported.go and get the |
| 376 | + # others here too |
| 377 | + os_name_for_base = 'ubuntu' |
| 378 | + |
| 379 | + # Check the charm manifest |
| 380 | + if channel_for_base == '': |
| 381 | + charm_manifest = get_local_charm_manifest(charm_path) |
| 382 | + if 'bases' in charm_manifest: |
| 383 | + channel_for_base = charm_manifest['bases'][0]['channel'] |
| 384 | + os_name_for_base = charm_manifest['bases'][0]['name'] |
| 385 | + else: |
| 386 | + # Also check the charmcraft.yaml |
| 387 | + charmcraft_yaml = get_local_charm_charmcraft_yaml(charm_path) |
| 388 | + if 'bases' in charmcraft_yaml: |
| 389 | + channel_for_base = charmcraft_yaml['bases'][0]['run-on'][0]['channel'] |
| 390 | + os_name_for_base = charmcraft_yaml['bases'][0]['run-on'][0]['name'] |
| 391 | + |
| 392 | + if channel_for_base == '': |
| 393 | + raise errors.JujuError("Unable to determine base for charm : %s" % |
| 394 | + charm_path) |
| 395 | + |
| 396 | + return baseCls(channel_for_base, os_name_for_base) |
| 397 | + |
| 398 | + |
| 399 | +def base_channel_to_series(channel): |
| 400 | + """Returns the series string using the track inside the base channel |
| 401 | +
|
| 402 | + :param str channel: is track/risk (e.g. 20.04/stable) |
| 403 | + :return: str series (e.g. focal) |
| 404 | + """ |
| 405 | + return get_version_series(origin.Channel.parse(channel).track) |
0 commit comments