| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 1 | # Copyright 2020 Canonical Ltd. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | import json |
| 15 | import argparse |
| 16 | |
| 17 | CHANNEL_LIST = [ |
| 18 | "stable", |
| 19 | "candidate", |
| 20 | "edge", |
| 21 | ] |
| 22 | BUNDLE_PREFIX = "cs:~charmed-osm" |
| 23 | DEFAULT_BUNDLE = "bundles/osm/bundle.yaml" |
| 24 | HA_BUNDLE = "bundles/osm-ha/bundle.yaml" |
| 25 | |
| 26 | parser = argparse.ArgumentParser(description="Process some arguments.") |
| 27 | |
| 28 | parser.add_argument("--channel", help="Channel from the Charm Store") |
| 29 | parser.add_argument("--destination", help="Destination for the generated bundle") |
| 30 | parser.add_argument("--ha", help="Select HA bundle", action="store_true") |
| 31 | parser.add_argument("--local", help="Path to the bundle directory", action="store_true") |
| 32 | parser.add_argument("--store", help="Path to the bundle directory", action="store_true") |
| 33 | |
| 34 | args = parser.parse_args() |
| 35 | print(args) |
| 36 | if not args.local and not args.store: |
| 37 | raise Exception("--local or --store must be specified") |
| 38 | if args.local and args.store: |
| 39 | raise Exception("Both --local and --store cannot be specified. Please choose one.") |
| 40 | if not args.destination: |
| 41 | raise Exception("--destination must be specified") |
| 42 | if args.channel and not args.channel in CHANNEL_LIST: |
| 43 | raise Exception( |
| 44 | "Channel {} does not exist. Please choose one of these: {}".format( |
| 45 | args.channel, CHANNEL_LIST |
| 46 | ) |
| 47 | ) |
| 48 | channel = args.channel if args.channel else "stable" |
| 49 | path = HA_BUNDLE if args.ha else DEFAULT_BUNDLE |
| 50 | destination = args.destination |
| 51 | prefix = "." if args.local else BUNDLE_PREFIX |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 52 | suffix = "/build" if args.local else "" |
| David Garcia | 82c5ffa | 2020-03-09 08:38:17 +0100 | [diff] [blame] | 53 | |
| 54 | data = { |
| 55 | "channel": channel, |
| 56 | "prefix": prefix, |
| 57 | "suffix": suffix, |
| 58 | } |
| 59 | |
| 60 | with open(path) as template: |
| 61 | bundle_template = template.read() |
| 62 | template.close() |
| 63 | with open("{}".format(destination), "w") as text_file: |
| 64 | text_file.write(bundle_template % data) |
| 65 | text_file.close() |