Last active
December 7, 2017 13:47
-
-
Save alexcombessie/e517df156013da3b7c139f422d9ff928 to your computer and use it in GitHub Desktop.
Deploy Dataiku predictive API package from Design to API node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Author = COMBESSIE, Alexandre (Dataiku) | |
## How to deploy a DSS API package from Design or Automation to API node | |
## This script is designed to be run in a DSS Design or Automation node | |
## It can run on an external system with a Dataiku API client token | |
## For details on how to query the deployed API endpoint, | |
## check https://doc.dataiku.com/dss/api/4.1/apinode-user/ | |
## Note that for Python/R function endpoints the URL should end by /run instead of /predict | |
# LIBRARIES | |
import dataiku | |
import dataikuapi | |
# PARAMETERS | |
## DESIGN OR AUTOMATION NODE | |
### These are example parameters that you should edit | |
design_node_client = dataiku.api_client() | |
project_key = dataiku.default_project_key() | |
service_id = design_node_client.get_project(project_key).list_api_services()[0]["id"] # take the first service by id | |
generation_id = "version_X.X" | |
path_to_store_package = "/".join(dataiku.get_custom_variables()["dip.home"].split("/")[:-1]) #one level above DATADIR | |
## API NODE | |
### These are example parameters that you should edit | |
### Run <DSS_API_DATADIR>/bin/apinode-admin admin-key-create in SHELL to get this | |
api_node_admin_key = "xABEaTKRg1Hkh2gaZbj6PIGsAMvUXua3" | |
api_node_uri = "http://localhost:11400" | |
# HELPER FUNCTIONS | |
def download_api_package(design_node_client, project_key, service_id, generation_id, path_to_store_package): | |
current_project = design_node_client.get_project(project_key) | |
api_services = current_project.list_api_services() | |
print("api_services", api_services) | |
api_service = current_project.get_api_service(service_id) | |
if generation_id not in [x["id"] for x in api_service.list_packages()]: | |
api_service.create_package(generation_id) | |
print("Package generation " + generation_id + " created") | |
else: | |
print("Package generation " + generation_id + " already created") | |
package_path = path_to_store_package + "/" + generation_id + ".zip" | |
api_service.download_package_to_file(generation_id, package_path) | |
return(0, "API package downloaded from Design node") | |
def deploy_api_package(service_id, generation_id, path_to_store_package, api_node_uri, api_node_admin_key): | |
api_node_admin_client = dataikuapi.APINodeAdminClient(api_node_uri, api_node_admin_key) | |
if service_id not in [x["serviceId"] for x in api_node_admin_client.list_services()]: | |
api_node_admin_client.create_service(service_id) | |
print(service_id + " service created") | |
else: | |
print(service_id + " service already created") | |
api_node_service = api_node_admin_client.service(service_id) | |
if generation_id not in [x["generationId"] for x in api_node_service.list_generations()]: | |
api_node_service.import_generation_from_archive( | |
path_to_store_package + "/" + generation_id + ".zip") | |
print(generation_id + " generation imported") | |
else: | |
print(generation_id + " generation already imported") | |
api_node_service.switch_to_newest() | |
api_node_service.disable() | |
api_node_service.enable() | |
return(0, "New predictive API deployed!") | |
# RUNNING IT | |
download_api_package(design_node_client, project_key, service_id, generation_id, path_to_store_package) | |
deploy_api_package(service_id, generation_id, path_to_store_package, api_node_uri, api_node_admin_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment