-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathcmd-ore-wrapper
executable file
·129 lines (112 loc) · 4.47 KB
/
cmd-ore-wrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
# NOTE: PYTHONUNBUFFERED is set in the entrypoint for unbuffered output
#
# This is a generic wrapper around 'ore' commands and serves
# as a replacement for 'cmd-*-replicate' and 'cmd-buildextend-*'.
import logging as log
import os
import sys
from cosalib.cli import (
cloud_clis,
get_cloud_cli,
get_cloud_ore_cmds,
BuildCli
)
from cosalib.qemuvariants import get_qemu_variant
from cosalib.ibmcloud import get_ibmcloud_variant
from cosalib.kubevirt import get_kubevirt_variant
if __name__ == '__main__':
log.basicConfig(
format='[%(levelname)s]: %(message)s',
level=log.INFO)
parser = BuildCli(
description="""interface for running commands against ore
Each target has its own sub options. To access them us:
'--target <target> --help'
""",
add_help=False,
)
# Determine if this a symlink, default to that.
sys_target = None
default_replicate = False
default_build_artifact = False
self_basename = os.path.basename(sys.argv[0])
# Check if this is a legacy interface
if str(self_basename).endswith("-replicate"):
log.info("symlink is for a replication command")
default_replicate = True
if str(self_basename).startswith("cmd-buildextend-"):
log.info("symlink is for a build and publish command")
default_build_artifact = True
# previous cmd-buildextend-<target> used symlinks
for k in cloud_clis():
if k in self_basename:
sys_target = k
log.info(f"ore target {sys_target} found via symlink")
break
parser.add_argument("--target",
default=sys_target,
choices=cloud_clis(),
help="Target type for ore command")
pre_args = parser.parse_known_args()[0]
target = pre_args.target
log.debug(f"extending cli for {target}")
# Check to make sure that a target has been chosen
if target is None:
parser.print_help()
log.fatal("--target is required")
sys.exit(1)
# Extend the CLI for the target
parser = get_cloud_cli(target, parser)
parser.add_argument("--build-artifact", "--build-if-missing",
action='store_true', default=default_build_artifact,
help="Build the artifact if missing")
parser.add_argument("--config", "--config-file",
help="ore configuration")
parser.add_argument("--force", action='store_true',
help="Force the operation if it has already happened")
parser.add_argument("--compress", action='store_true',
help="Compress generated image")
parser.add_argument("--help", action='store_true',
help="Print this message")
parser.add_argument("--upload", action='store_true',
help="Upload the disk to the ore target")
parser.add_argument("--replicate", action='store_true',
default=default_replicate,
help="For specific clouds, replicate various regions")
parser.add_argument("--region", "--regions", dest="region",
help="Upload/replicate to specific regions",
nargs='+')
parser.add_argument("--source-region", help="Region to copy AMI from")
parser.add_argument("--arch", dest="arch", help="Architecture to target")
parser.description = (
f"'ore' interface for running ore commands for {target.upper()}"
)
args = parser.parse_args()
if args.help:
parser.print_help()
sys.exit()
# Now _extend the parser with the cloud targets_
if target in ['ibmcloud', 'powervs']:
build = get_ibmcloud_variant(target, args)
elif target == "kubevirt":
build = get_kubevirt_variant(target, args)
else:
build = get_qemu_variant(target, args)
log.info(f"operating on {build.image_name}")
if args.build_artifact:
if args.force:
build.build_artifacts()
else:
build.ensure_built()
if args.upload:
if not build.have_artifact:
raise Exception(f"Missing build artifact {build.image_path}")
log.info("executing upload commands for ore")
cmd, _ = get_cloud_ore_cmds(target)
cmd(build, args)
if args.replicate:
log.info("executing replicate commands for ore")
_, cmd = get_cloud_ore_cmds(target)
cmd(build, args)
log.info("finishing ore processing")