From b5fd7b1fb4f7d5cc573c7af2172c8822169fed42 Mon Sep 17 00:00:00 2001 From: Pierre Guilleminot Date: Wed, 27 Sep 2017 09:40:53 +0200 Subject: [PATCH 1/2] Add parameters to the execution on konnectors --- docs/konnectors.md | 15 +-------------- docs/konnectors_design.md | 7 ++++--- docs/konnectors_workflow_example.md | 1 + docs/registry-publish.md | 1 + pkg/apps/konnector.go | 1 + pkg/workers/exec/konnector.go | 6 ++++++ scripts/konnector-nsjail-run.sh | 2 ++ scripts/konnector-rkt-run.sh | 1 + 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/konnectors.md b/docs/konnectors.md index 443f2dc375f..725c1078944 100644 --- a/docs/konnectors.md +++ b/docs/konnectors.md @@ -15,28 +15,15 @@ type | the type of the konnector source ("node" is the only supported slug | the default slug (it can be changed at install time) icon | an icon for the home description | a short description of the konnector -fields | the list of required fields with their type source | where the files of the app can be downloaded developer | `name` and `url` for the developer default_locale | the locale used for the name and description fields locales | translations of the name and description fields in other locales version | the current version number +parameters | any json object that will be passed to the konnector on execution in the `COZY_PARAMETERS` environment variable license | [the SPDX license identifier](https://spdx.org/licenses/) permissions | a map of permissions needed by the app (see [here](permissions.md) for more details) -For the "fields" field here is an example : -``` -{ - "fields": { - "login": "string", - "password": "password", - "folderPath": "path" - } -} -``` - -This will allow the "My accounts" application to get the list of fields to display and their type. The list of possible types still needs to be defined. - ### POST /konnectors/:slug Install a konnector, ie download the files and put them in `/konnectors/:slug` in the virtual file system of the user, create an `io.cozy.konnectors` document, register the permissions, etc. diff --git a/docs/konnectors_design.md b/docs/konnectors_design.md index d68ebe98767..de36d056945 100644 --- a/docs/konnectors_design.md +++ b/docs/konnectors_design.md @@ -345,9 +345,10 @@ directory](https://github.com/cozy-labs/konnectors/tree/master/server/lib). The konnector will be run with the following environment variables : -- COZY_CREDENTIALS : containing the response to Oauth request as json string -- COZY_URL : to know what instance is running the konnector -- COZY_FIELDS : as a json string with all the values from the account associated to the konnector. This should correspond to fields defined in the manifest.konnectors with the "fields" attribute. +- `COZY_CREDENTIALS` : containing the response to Oauth request as json string +- `COZY_URL` : to know what instance is running the konnector +- `COZY_FIELDS` : as a json string with all the values from the account associated to the konnector. +- `COZY_PARAMETERS` : optional json string associated with the application, used to parameterize a konnector based on a common set of code. In the end of the konnector execution (or timeout), the logs are read in the log.txt file and added to the konnector own log file (in VFS) and the run directory is then destroyed. diff --git a/docs/konnectors_workflow_example.md b/docs/konnectors_workflow_example.md index ff60b5a7c1b..8877f6e0e24 100644 --- a/docs/konnectors_workflow_example.md +++ b/docs/konnectors_workflow_example.md @@ -363,6 +363,7 @@ Start the konnector through Rkt, passing as ENV variables : - `COZY_CREDENTIALS`: security token to communicate with Cozy - `COZY_FIELDS`: JSON-encoded worker_arguments + - `COZY_PARAMETERS`: JSON-encoded parameters associated with the konnector - `COZY_TYPE`: the type field of the konnector (eg. "node" etc.) - `COZY_LOCALE`: the locale of the user (eg. "en" etc.) - `COZY_URL`: the starting instance URL diff --git a/docs/registry-publish.md b/docs/registry-publish.md index d4e2718a2de..70771193e1b 100644 --- a/docs/registry-publish.md +++ b/docs/registry-publish.md @@ -31,6 +31,7 @@ Then you can adapt this script as your [`after_deploy` or `after_success`](https It contains environment variables that you can adapt as your need: - `COZY_APP_VERSION`: the version string of the deployed version + - `COZY_APP_PARAMS`: an optional JSON object (string, object or array) that will parameterize the application on its execution. - `COZY_BUILD_URL`: the URL of the deployed tarball for your application - `COZY_BUILD_BRANCH`: the name of the build branch from which the script creates dev releases diff --git a/pkg/apps/konnector.go b/pkg/apps/konnector.go index 57ebe632294..ff1ce2ffbba 100644 --- a/pkg/apps/konnector.go +++ b/pkg/apps/konnector.go @@ -29,6 +29,7 @@ type KonnManifest struct { DefaultLocale string `json:"default_locale"` Locales Locales `json:"locales"` + Parameters json.RawMessage `json:"parameters"` DocVersion string `json:"version"` License string `json:"license"` DocPermissions permissions.Set `json:"permissions"` diff --git a/pkg/workers/exec/konnector.go b/pkg/workers/exec/konnector.go index 32b3357c86a..1304bbbcdaf 100644 --- a/pkg/workers/exec/konnector.go +++ b/pkg/workers/exec/konnector.go @@ -166,6 +166,11 @@ func (w *konnectorWorker) PrepareCmdEnv(i *instance.Instance, m *jobs.Message) ( return } + paramsJSON, err := json.Marshal(w.man.Parameters) + if err != nil { + return + } + token := i.BuildKonnectorToken(w.man) cmd = config.GetConfig().Konnectors.Cmd @@ -173,6 +178,7 @@ func (w *konnectorWorker) PrepareCmdEnv(i *instance.Instance, m *jobs.Message) ( "COZY_URL=" + i.PageURL("/", nil), "COZY_CREDENTIALS=" + token, "COZY_FIELDS=" + string(fieldsJSON), + "COZY_PARAMETERS=" + string(paramsJSON), "COZY_TYPE=" + w.man.Type, "COZY_LOCALE=" + i.Locale, "COZY_JOB_ID=" + jobID, diff --git a/scripts/konnector-nsjail-run.sh b/scripts/konnector-nsjail-run.sh index 519da704e78..6a6f25b53ac 100755 --- a/scripts/konnector-nsjail-run.sh +++ b/scripts/konnector-nsjail-run.sh @@ -118,6 +118,7 @@ nsjail \ --log "nsjail-${log_name}.log" \ -E "COZY_URL=${COZY_URL}" \ -E "COZY_FIELDS=${COZY_FIELDS}" \ + -E "COZY_PARAMETERS=${COZY_PARAMETERS}" \ -E "COZY_CREDENTIALS=${COZY_CREDENTIALS}" \ -E "COZY_LOCALE=${COZY_LOCALE}" \ -R "${rundir}:/usr/src/konnector/" \ @@ -146,6 +147,7 @@ nsjail \ # --seccomp_string "${seccomp_string}" \ # -E "COZY_URL=${COZY_URL}" \ # -E "COZY_FIELDS=${COZY_FIELDS}" \ +# -E "COZY_PARAMETERS=${COZY_PARAMETERS}" \ # -E "COZY_CREDENTIALS=${COZY_CREDENTIALS}" \ # -E "COZY_LOCALE=${COZY_LOCALE}" \ # -R "${rundir}:/usr/src/konnector/" \ diff --git a/scripts/konnector-rkt-run.sh b/scripts/konnector-rkt-run.sh index 369fbb838a5..4da4329b1f3 100755 --- a/scripts/konnector-rkt-run.sh +++ b/scripts/konnector-rkt-run.sh @@ -11,6 +11,7 @@ node_image="$(dirname ${0})/nodeslim.aci" echo "COZY_URL=${COZY_URL}" > "${env_file}" echo "COZY_FIELDS=${COZY_FIELDS}" >> "${env_file}" +echo "COZY_PARAMETERS=${COZY_PARAMETERS}" >> "${env_file}" echo "COZY_CREDENTIALS=${COZY_CREDENTIALS}" >> "${env_file}" echo "COZY_LOCALE=${COZY_LOCALE}" >> "${env_file}" From 2381a92dfe10b9f753f52f3bfe35198bb017fa91 Mon Sep 17 00:00:00 2001 From: Pierre Guilleminot Date: Wed, 27 Sep 2017 10:03:48 +0200 Subject: [PATCH 2/2] Update deploy script to add parameters --- docs/registry-publish.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/registry-publish.md b/docs/registry-publish.md index 70771193e1b..ad9f6128ec0 100644 --- a/docs/registry-publish.md +++ b/docs/registry-publish.md @@ -31,7 +31,7 @@ Then you can adapt this script as your [`after_deploy` or `after_success`](https It contains environment variables that you can adapt as your need: - `COZY_APP_VERSION`: the version string of the deployed version - - `COZY_APP_PARAMS`: an optional JSON object (string, object or array) that will parameterize the application on its execution. + - `COZY_APP_PARAMETERS`: an optional JSON object (string, object or array) that will parameterize the application on its execution. - `COZY_BUILD_URL`: the URL of the deployed tarball for your application - `COZY_BUILD_BRANCH`: the name of the build branch from which the script creates dev releases @@ -45,9 +45,8 @@ set -e # COZY_BUILD_BRANCH: the name of the build branch from which the script # creates dev releases -if [ -z "${COZY_BUILD_BRANCH}" ]; then - COZY_BUILD_BRANCH="master" -fi +[ -z "${COZY_BUILD_BRANCH}" ] && COZY_BUILD_BRANCH="master" +[ -z "${COZY_APP_PARAMETERS}" ] && COZY_APP_PARAMETERS="null" if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then echo "No deployment: in pull-request" @@ -59,6 +58,11 @@ if [ "${TRAVIS_BRANCH}" != "${COZY_BUILD_BRANCH}" ] && [ -z "${TRAVIS_TAG}" ]; t exit 0 fi +if ! jq <<< "${COZY_APP_PARAMETERS}" > /dev/null; then + printf "Could not parse COZY_APP_PARAMETERS=%s as JSON\n" "${COZY_APP_PARAMETERS}" + exit 1 +fi + if [ -z "${COZY_APP_VERSION}" ]; then if [ -n "${TRAVIS_TAG}" ]; then COZY_APP_VERSION="${TRAVIS_TAG}" @@ -79,12 +83,12 @@ fi shasum=$(curl -sSL --fail "${COZY_BUILD_URL}" | shasum -a 256 | cut -d" " -f1) -printf 'Publishing version "%s" from "%s" (%s)\n' "${COZY_APP_VERSION}" "${COZY_BUILD_URL}" "${shasum}" +printf 'Publishing version "%s" from "%s" (%s)\n' "${COZY_APP_VERSION}" "${COZY_BUILD_URL}\n" "${shasum}" curl -sS --fail -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Token ${REGISTRY_TOKEN}" \ - -d "{\"version\": \"${COZY_APP_VERSION}\", \"url\": \"${COZY_BUILD_URL}\", \"sha256\": \"${shasum}\"}" \ + -d "{\"version\": \"${COZY_APP_VERSION}\", \"url\": \"${COZY_BUILD_URL}\", \"sha256\": \"${shasum}\", \"parameters\": ${COZY_APP_PARAMETERS}}" \ "https://registry.cozy.io/registry/versions" ```