forked from Yelp/paasta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadhoc_tools.py
142 lines (125 loc) · 4.64 KB
/
adhoc_tools.py
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
# Copyright 2015-2016 Yelp Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import service_configuration_lib
from paasta_tools.long_running_service_tools import LongRunningServiceConfig
from paasta_tools.long_running_service_tools import LongRunningServiceConfigDict
from paasta_tools.utils import BranchDictV2
from paasta_tools.utils import deep_merge_dictionaries
from paasta_tools.utils import DEFAULT_SOA_DIR
from paasta_tools.utils import load_service_instance_config
from paasta_tools.utils import load_v2_deployments_json
from paasta_tools.utils import NoConfigurationForServiceError
from paasta_tools.utils import NoDeploymentsAvailable
from paasta_tools.utils import prompt_pick_one
log = logging.getLogger(__name__)
def load_adhoc_job_config(
service, instance, cluster, load_deployments=True, soa_dir=DEFAULT_SOA_DIR
):
general_config = service_configuration_lib.read_service_configuration(
service, soa_dir=soa_dir
)
instance_config = load_service_instance_config(
service=service,
instance=instance,
instance_type="adhoc",
cluster=cluster,
soa_dir=soa_dir,
)
general_config = deep_merge_dictionaries(
overrides=instance_config, defaults=general_config
)
branch_dict = None
if load_deployments:
deployments_json = load_v2_deployments_json(service, soa_dir=soa_dir)
temp_instance_config = AdhocJobConfig(
service=service,
cluster=cluster,
instance=instance,
config_dict=general_config,
branch_dict=None,
soa_dir=soa_dir,
)
branch = temp_instance_config.get_branch()
deploy_group = temp_instance_config.get_deploy_group()
branch_dict = deployments_json.get_branch_dict(service, branch, deploy_group)
return AdhocJobConfig(
service=service,
cluster=cluster,
instance=instance,
config_dict=general_config,
branch_dict=branch_dict,
soa_dir=soa_dir,
)
class AdhocJobConfig(LongRunningServiceConfig):
config_filename_prefix = "adhoc"
def __init__(
self,
service: str,
instance: str,
cluster: str,
config_dict: LongRunningServiceConfigDict,
branch_dict: BranchDictV2,
soa_dir: str = DEFAULT_SOA_DIR,
) -> None:
super().__init__(
cluster=cluster,
instance=instance,
service=service,
config_dict=config_dict,
branch_dict=branch_dict,
soa_dir=soa_dir,
)
def get_default_interactive_config(
service: str, cluster: str, soa_dir: str, load_deployments: bool = False
) -> AdhocJobConfig:
default_job_config = {"cpus": 4, "mem": 10240, "disk": 1024}
try:
job_config = load_adhoc_job_config(
service=service, instance="interactive", cluster=cluster, soa_dir=soa_dir
)
except NoConfigurationForServiceError:
job_config = AdhocJobConfig(
service=service,
instance="interactive",
cluster=cluster,
config_dict={},
branch_dict=None,
soa_dir=soa_dir,
)
except NoDeploymentsAvailable:
job_config = load_adhoc_job_config(
service=service,
instance="interactive",
cluster=cluster,
soa_dir=soa_dir,
load_deployments=False,
)
if not job_config.branch_dict and load_deployments:
deployments_json = load_v2_deployments_json(service, soa_dir=soa_dir)
deploy_group = prompt_pick_one(
deployments_json.get_deploy_groups(), choosing="deploy group"
)
job_config.config_dict["deploy_group"] = deploy_group
job_config.branch_dict = {
"docker_image": deployments_json.get_docker_image_for_deploy_group(
deploy_group
),
"git_sha": deployments_json.get_git_sha_for_deploy_group(deploy_group),
"force_bounce": None,
"desired_state": "start",
}
for key, value in default_job_config.items():
job_config.config_dict.setdefault(key, value)
return job_config