-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
266 lines (219 loc) · 10 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import os
import sys
import json
import yaml
import logging
import kfp
import kfp.compiler as compiler
import importlib.util
from datetime import datetime
from typing import Optional
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
# TODO: Remove after version up to kfp=v1.1
_FILTER_OPERATIONS = {"UNKNOWN": 0,
"EQUALS": 1,
"NOT_EQUALS": 2,
"GREATER_THAN": 3,
"GREATER_THAN_EQUALS": 5,
"LESS_THAN": 6,
"LESS_THAN_EQUALS": 7}
# TODO: Remove after version up to kfp=v1.1
def get_pipeline_id(self, name):
"""Find the id of a pipeline by name.
Args:
name: Pipeline name.
Returns:
Returns the pipeline id if a pipeline with the name exists.
"""
pipeline_filter = json.dumps({
"predicates": [
{
"op": _FILTER_OPERATIONS["EQUALS"],
"key": "name",
"stringValue": name,
}
]
})
result = self._pipelines_api.list_pipelines(filter=pipeline_filter)
if result.pipelines is None:
return None
if len(result.pipelines) == 1:
return result.pipelines[0].id
elif len(result.pipelines) > 1:
raise ValueError(
"Multiple pipelines with the name: {} found, the name needs to be unique".format(name))
return None
# TODO: Remove after version up to kfp=v1.1
def upload_pipeline_version(
self,
pipeline_package_path,
pipeline_version_name: str,
pipeline_id: Optional[str] = None,
pipeline_name: Optional[str] = None):
"""Uploads a new version of the pipeline to the Kubeflow Pipelines cluster.
Args:
pipeline_package_path: Local path to the pipeline package.
pipeline_version_name: Name of the pipeline version to be shown in the UI.
pipeline_id: Optional. Id of the pipeline.
pipeline_name: Optional. Name of the pipeline.
Returns:
Server response object containing pipleine id and other information.
Throws:
ValueError when none or both of pipeline_id or pipeline_name are specified
Exception if pipeline id is not found.
"""
if all([pipeline_id, pipeline_name]) or not any([pipeline_id, pipeline_name]):
raise ValueError('Either pipeline_id or pipeline_name is required')
if pipeline_name:
pipeline_id = self.get_pipeline_id(pipeline_name)
response = self._upload_api.upload_pipeline_version(
pipeline_package_path,
name=pipeline_version_name,
pipelineid=pipeline_id
)
if self._is_ipython():
import IPython
html = 'Pipeline link <a href=%s/#/pipelines/details/%s>here</a>' % (
self._get_url_prefix(), response.id)
IPython.display.display(IPython.display.HTML(html))
return response
def load_function(pipeline_function_name: str, full_path_to_pipeline: str) -> object:
"""Function to load python function from filepath and filename
Arguments:
pipeline_function_name {str} -- The name of the pipeline function
full_path_to_pipeline {str} -- The full path name including the filename of the python file that
describes the pipeline you want to run on Kubeflow
Returns:
object -- [description]
"""
logging.info(
f"Loading the pipeline function from: {full_path_to_pipeline}")
logging.info(
f"The name of the pipeline function is: {pipeline_function_name}")
spec = importlib.util.spec_from_file_location(pipeline_function_name,
full_path_to_pipeline)
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
pipeline_func = getattr(foo, pipeline_function_name)
logging.info("Succesfully loaded the pipeline function.")
return pipeline_func
def pipeline_compile(pipeline_function: object) -> str:
"""Function to compile pipeline. The pipeline is compiled to a zip file.
Arguments:
pipeline_func {object} -- The kubeflow pipeline function
Returns:
str -- The name of the compiled kubeflow pipeline
"""
pipeline_name_zip = pipeline_function.__name__ + ".zip"
compiler.Compiler().compile(pipeline_function, pipeline_name_zip)
logging.info("The pipeline function is compiled.")
return pipeline_name_zip
def upload_pipeline(pipeline_name_zip: str, pipeline_name: str, github_sha: str, client):
""" Function to upload pipeline to kubeflow.
Arguments:
pipeline_name_zip {str} -- The name of the compiled pipeline.ArithmeticError
pipeline_name {str} -- The name of the pipeline function. This will be the name in the kubeflow UI.
"""
pipeline_id = client.get_pipeline_id(pipeline_name)
if pipeline_id is None:
pipeline_id = client.upload_pipeline(
pipeline_package_path=pipeline_name_zip,
pipeline_name=pipeline_name).to_dict()["id"]
client.upload_pipeline_version(
pipeline_package_path=pipeline_name_zip,
pipeline_version_name=github_sha,
pipeline_id=pipeline_id)
return pipeline_id
def read_pipeline_params(pipeline_paramters_path: str) -> dict:
# [TODO] add docstring here
pipeline_params = {}
with open(pipeline_paramters_path) as f:
try:
pipeline_params = yaml.safe_load(f)
logging.info(f"The pipeline paramters is: {pipeline_params}")
except yaml.YAMLError as exc:
logging.info("The yaml parameters could not be loaded correctly.")
raise ValueError(
f"The yaml parameters could not be loaded correctly with {exc}.")
logging.info(f"The paramters are: {pipeline_params}")
return pipeline_params
def run_pipeline_func(client: kfp.Client,
pipeline_name: str,
pipeline_id: str,
pipeline_paramters_path: dict,
recurring_flag: bool = False,
cron_exp: str = ''):
pipeline_params = read_pipeline_params(
pipeline_paramters_path=pipeline_paramters_path)
pipeline_params = pipeline_params if pipeline_params is not None else {}
experiment_id = None
experiment_name = "{}-{}".format(pipeline_name,
os.environ["INPUT_EXPERIMENT_NAME"])
try:
experiment_id = client.get_experiment(
experiment_name=experiment_name
).to_dict()["id"]
except ValueError:
experiment_id = client.create_experiment(
name=experiment_name
).to_dict()["id"]
namespace = os.getenv("INPUT_PIPELINE_NAMESPACE") if not str.isspace(
os.getenv("INPUT_PIPELINE_NAMESPACE")) else None
job_name = 'Run {} on {}'.format(pipeline_name,
datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
logging.info(f"experiment_id: {experiment_id}, \
job_name: {job_name}, \
pipeline_params: {pipeline_params}, \
pipeline_id: {pipeline_id}, \
namespace: {namespace}, \
cron_exp: {cron_exp}")
if recurring_flag == "true":
client.create_recurring_run(experiment_id=experiment_id,
job_name=job_name,
params=pipeline_params,
pipeline_id=pipeline_id,
cron_expression=cron_exp)
logging.info(
"Successfully started the recurring pipeline, head over to kubeflow to check it out")
client.run_pipeline(experiment_id=experiment_id,
job_name=job_name,
params=pipeline_params,
pipeline_id=pipeline_id)
logging.info(
"Successfully started the pipeline, head over to kubeflow to check it out")
def main():
logging.info(
"Started the process to compile and upload the pipeline to kubeflow.")
logging.info("Authenticating")
ga_credentials = os.environ["INPUT_GOOGLE_APPLICATION_CREDENTIALS"]
with open(ga_credentials) as f:
sa_details = json.load(f)
os.system("gcloud auth activate-service-account {} --key-file={} --project={}".format(sa_details['client_email'],
ga_credentials,
sa_details['project_id']))
pipeline_name = os.environ['INPUT_PIPELINE_FUNCTION_NAME']
pipeline_function = load_function(pipeline_function_name=pipeline_name,
full_path_to_pipeline=os.environ['INPUT_PIPELINE_CODE_PATH'])
github_sha = os.getenv("GITHUB_SHA")
if os.environ["INPUT_VERSION_GITHUB_SHA"] == "true":
logging.info(f"Versioned pipeline components with : {github_sha}")
pipeline_function = pipeline_function(github_sha=github_sha)
# TODO: Remove after version up to kfp=v1.1
kfp.Client.get_pipeline_id = get_pipeline_id
kfp.Client.upload_pipeline_version = upload_pipeline_version
client = kfp.Client(host=os.environ['INPUT_KUBEFLOW_URL'])
pipeline_name_zip = pipeline_compile(pipeline_function=pipeline_function)
pipeline_id = upload_pipeline(pipeline_name_zip=pipeline_name_zip,
pipeline_name=pipeline_name,
github_sha=github_sha,
client=client)
if os.getenv("INPUT_RUN_PIPELINE") == "true":
logging.info("Started the process to run the pipeline on kubeflow.")
run_pipeline_func(pipeline_name=pipeline_name,
pipeline_id=pipeline_id,
client=client,
pipeline_paramters_path=os.environ["INPUT_PIPELINE_PARAMETERS_PATH"],
recurring_flag=os.environ['INPUT_RUN_RECURRING_PIPELINE'],
cron_exp=os.environ['INPUT_CRON_EXPRESSION'])
if __name__ == "__main__":
main()