-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathharness-deployment
More file actions
164 lines (136 loc) · 9 KB
/
harness-deployment
File metadata and controls
164 lines (136 loc) · 9 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
import logging
import sys
import os
from ch_cli_tools.dockercompose import create_docker_compose_configuration
from ch_cli_tools.helm import create_helm_chart, deploy
from ch_cli_tools.configurationgenerator import hosts_info
from ch_cli_tools.skaffold import create_skaffold_configuration, create_vscode_debug_configuration
from ch_cli_tools.codefresh import create_codefresh_deployment_scripts, write_env_file
from ch_cli_tools.preprocessing import preprocess_build_overrides, generate_hash_based_image_tags
from ch_cli_tools.utils import merge_app_directories, merge_to_yaml_file
from ch_cli_tools.migration import perform_migration
from cloudharness_utils.constants import DEPLOYMENT_PATH, COMPOSE_ENGINE, HELM_ENGINE, VALUES_MANUAL_PATH, HELM_PATH, COMPOSE
HERE = os.path.dirname(os.path.realpath(__file__)).replace(os.path.sep, '/')
ROOT = os.path.dirname(os.path.dirname(HERE)).replace(os.path.sep, '/')
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description='Walk filesystem inside ./applications and define build and deploy scripts.')
parser.add_argument('paths', metavar='paths', default=[ROOT], type=str, nargs='*',
help='Base paths to start looking for applications. The paths will be processed '
'in the given order so determining the build and overriding precedence.')
parser.add_argument('-i', '--include', dest='include', action="append", default=[],
help='Specify the applications to include and exclude the rest. '
'Omit to build images for all Docker files.')
parser.add_argument('-t', '--tag', dest='tag', action="store", default=None,
help='Add a tag with the current version (default no value, content hash is used)')
parser.add_argument('-n', '--namespace', dest='namespace', action="store", default=None,
help='Specify the namespace of the deployment (default taken from values.yaml)')
parser.add_argument('--name', dest='name', action="store", default=None,
help='Specify the helm chart name (default: namespace when provided, otherwise chart default)')
parser.add_argument('--chart-version', dest='chart_version', action="store", default=None,
help='Specify the helm chart version')
parser.add_argument('--app-version', dest='app_version', action="store", default=None,
help='Specify the helm chart appVersion')
parser.add_argument('-r', '--registry', dest='registry', action="store", default='',
help='Specify image registry prefix')
parser.add_argument('-rsn', '-rs', '--registry-secret-name', dest='registry_secret_name', action="store", default='',
help='Specify image registry secret (-rs is deprecated, use -rsn)')
parser.add_argument('-o', '--output', dest='output_path', action="store", default='./deployment',
help='Specify helm chart base path (default `./deployment)`')
parser.add_argument('-d', '--domain', dest='domain', action="store", default="cloudharness.metacell.us",
help='Specify a domain different from cloudharness.metacell.us')
parser.add_argument('-l', '--local', dest='local', action="store_true",
help='Specify for local deployments info and setup')
parser.add_argument('-u', '--disable-security', dest='unsecured', action="store_true",
help='Disable secured gatekeepers access')
parser.add_argument('-ex', '--exclude', dest='exclude', action="append", default=[],
help='Specify application to exclude from the deployment')
parser.add_argument('-e', '--env', dest='env', action="store", default="",
help='Specify the name of the environment(s) (will load values-[ENV].yaml files). Specify multiple envs with dashes (e.g env1-env2)')
parser.add_argument('-m', '--merge', dest='merge', action="store", default=None,
help='Deprecated -- Merge is now automatic')
parser.add_argument('-dtls', '--disable-tls', dest='no_tls', action="store_true",
help='Disable tls (serve on http)')
parser.add_argument('--deploy', dest='deploy', action="store_true",
help='Deploy helm chart')
parser.add_argument('-N', '--no-cd', dest='no_cd_gen', action="store_const", default=None, const=True,
help=f'Do not generate ci/cd files')
parser.add_argument('-we', '--write-env', dest='write_env', action="store_const", default=None, const=True,
help=f'Write build env to .env file in {DEPLOYMENT_PATH}')
parser.add_argument('--migrate', dest='migration', action="store_true",
help='Perform a migration of the deployment to the latest supported version.')
parser.add_argument('--migrate-accept-all', dest='migration_accept_all', action="store_true",
help='Perform a migration of the deployment to the latest supported version without asking for confirmation. Use with caution.')
parser.add_argument('--docker-compose', dest='docker_compose', action="store_true",
help='Generate docker-compose.yaml and dedicated Skaffold configuration')
parser.add_argument('-cu', '--cache-url', dest='image_cache_url', action="store", default='',
help='Specify a custom cache URL for the docker images. This is used to speed up builds by caching layers. Example: https://cache.cloudharness.io?repository=[REGISTRY]/[IMAGE_NAME]&tag=[TAG]')
args, unknown = parser.parse_known_args(sys.argv[1:])
root_paths = [os.path.join(os.getcwd(), path) for path in args.paths]
envs = (args.env.split("-")
if "-" in args.env else [args.env]) if args.env else ()
if unknown:
print('There are unknown args. Make sure to call the script with the accepted args. Try --help')
print(f'unknown: {unknown}')
else:
if args.merge:
logging.warning(
"Merge (-m, --merge) argument is deprecated. Directory merging is now set automatically")
merge_app_directories(root_paths, destination=args.merge)
root_paths = [args.merge]
if args.migration_accept_all:
perform_migration(root_paths, accept_all=True)
elif args.migration:
perform_migration(root_paths, accept_all=False)
chart_fn = create_helm_chart if not args.docker_compose else create_docker_compose_configuration
chart_args = dict(
root_paths=root_paths,
tag=args.tag,
registry=args.registry,
domain=args.domain,
local=args.local,
secured=not args.unsecured,
output_path=args.output_path,
exclude=args.exclude,
include=args.include,
registry_secret_name=args.registry_secret_name,
tls=not args.no_tls,
env=envs,
namespace=args.namespace,
)
if not args.docker_compose:
chart_args.update(dict(
name=args.name,
chart_version=args.chart_version,
app_version=args.app_version,
))
helm_values = chart_fn(**chart_args)
merged_root_paths = preprocess_build_overrides(
root_paths=root_paths, helm_values=helm_values)
# If tags are content hashes (tag omitted in non-local mode), recompute
# only image tags after overrides are merged so hashes reflect merged
# build contexts without rerunning chart generation.
if args.tag is None and not args.local:
helm_values = generate_hash_based_image_tags(root_paths=root_paths, helm_values=helm_values)
# Overwrite the values.yaml saved in the previous step with updated image tags
templates_path = COMPOSE if args.docker_compose else HELM_PATH
values_path = os.path.join(args.output_path, templates_path, VALUES_MANUAL_PATH)
merge_to_yaml_file(helm_values.to_dict() if hasattr(helm_values, 'to_dict') else helm_values, values_path)
if not args.no_cd_gen and envs and not args.docker_compose:
create_codefresh_deployment_scripts(
merged_root_paths,
include=args.include,
exclude=args.exclude,
envs=envs,
base_image_name=helm_values['name'],
helm_values=helm_values)
if args.write_env:
write_env_file(helm_values, os.path.join(root_paths[-1], DEPLOYMENT_PATH, ".env"), args.image_cache_url)
create_skaffold_configuration(merged_root_paths, helm_values, backend_deploy=COMPOSE_ENGINE if args.docker_compose else HELM_ENGINE, env=envs)
if not args.docker_compose:
create_vscode_debug_configuration(root_paths, helm_values)
hosts_info(helm_values)
if args.deploy:
deploy(args.namespace, args.output_path)