-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-deployment-tools.py
More file actions
96 lines (80 loc) · 2.97 KB
/
sql-deployment-tools.py
File metadata and controls
96 lines (80 loc) · 2.97 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
import argparse
import json
import os
import sys
from src.config import ConfigurationError, load_configuration
from src.deploy import deploy_agent_job, deploy_ssis
if __name__ == "__main__":
parent_parser = argparse.ArgumentParser(description="SSIS Deployment Helper")
subparsers = parent_parser.add_subparsers(help="sub-command help", dest="command")
validate = subparsers.add_parser("validate", help="Validate the config file")
validate.add_argument(
"--config",
help="TOML configuration file (default: config.toml)",
type=argparse.FileType("r"),
default="config.toml",
required=False,
)
deploy = subparsers.add_parser(
"deploy", help="Deploy SSIS based on the configuration"
)
deploy.add_argument(
"--config",
help="TOML configuration file (default: config.toml)",
type=argparse.FileType("r"),
default="config.toml",
required=False,
)
deploy.add_argument(
"--ispac",
type=str,
help="Path to `*.ispac` file to be deployed.",
required=False,
)
deploy.add_argument(
"--connection-string",
type=str,
help=(
"Connection string. If not supplied we'll attempt to use environment"
" variable `CONNECTION_STRING`"
),
required=False,
)
deploy.add_argument(
"--replacement-tokens",
type=json.loads,
help="Variables to be replaced in the TOML file",
required=False,
)
args = parent_parser.parse_args()
if not args.config.name.endswith(".toml"):
raise ValueError("Config must be a TOML file.")
configuration = args.config.read()
args.config.close()
if args.command == "validate":
try:
load_configuration(configuration)
except ConfigurationError:
sys.exit("Invalid configuration.")
elif args.command == "deploy":
if args.ispac:
if not args.ispac.endswith(".ispac"):
raise ValueError("Not an ISPAC file.")
if not os.path.exists(args.ispac):
raise FileNotFoundError("Cannot find specific ISPAC file.")
connection_string = args.connection_string or os.getenv("CONNECTION_STRING")
if not connection_string:
raise ValueError(
"Missing connection string. Can either be supplied using the"
" `--connection-string` argument or via an environment variable"
" `CONNECTION_STRING`"
)
if args.replacement_tokens:
configuration = configuration.format(**args.replacement_tokens)
ssis_deployment = load_configuration(configuration)
if args.ispac:
deploy_ssis(connection_string, args.ispac, ssis_deployment)
if ssis_deployment.job is not None:
deploy_agent_job(connection_string, ssis_deployment)
else:
raise NotImplementedError(f"Command not supported: {args.command}")