diff --git a/antidote-cis b/antidote-cis index bdc39a0c9c2ee0846087aaf5e7b9e26241d8c4f0..c45277ca278184f1d4ffea1bfc4e6cd9f6c90829 100755 --- a/antidote-cis +++ b/antidote-cis @@ -26,7 +26,7 @@ function echo2 () { function curl_wrapped () { # Add some options for every curl request, basing on config file. - curl_options=( "--http1.1" --max-time 30 ) # Old version of curl failed to fallback to http1.1 on http2 error. + curl_options=( -s --http1.1 --max-time 30 ) # Old version of curl failed to fallback to http1.1 on http2 error. [[ "$cis_bearer" != "" ]] && curl_options+=( -H "Authorization: $cis_bearer" ) [[ "$cis_cookie" != "" ]] && curl_options+=( --cookie "$cis_cookie" ) [[ "$cis_bearer$cis_cookie" = "" ]] && echo2 "Warning: You must set either cis_cookie or cis_bearer to authenticate your CIS API call, but you have set neither. curl requests is very likely to fail!" @@ -63,11 +63,11 @@ function cis_upload_pkg () { if [[ "${pkg_path:0:6}" = "cis://" ]]; then echo2 "Submitting request to upload package $cis_namespace.$workflow_name:$release_ver, using CIS internal package (trigger argless upload)..." - curl_wrapped -s --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"00000000-0000-0000-0000-000000000000","ActionType":501,"Notes":"antidote-auto-upload","ActionArgs":{"PackageSourcePath":null,"ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' + curl_wrapped --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"00000000-0000-0000-0000-000000000000","ActionType":501,"Notes":"antidote-auto-upload","ActionArgs":{"PackageSourcePath":null,"ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' else smb_url=`upload_to_smb_share "$release_ver" "$pkg_path"` || return $? echo2 "Submitting request to upload package $cis_namespace.$workflow_name:$release_ver, using smb:$smb_url..." - curl_wrapped -s --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"00000000-0000-0000-0000-000000000000","ActionType":501,"Notes":"antidote-auto-upload","ActionArgs":{"PackageSourcePath":"'"$smb_url"'","ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' + curl_wrapped --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"00000000-0000-0000-0000-000000000000","ActionType":501,"Notes":"antidote-auto-upload","ActionArgs":{"PackageSourcePath":"'"$smb_url"'","ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' fi return $? } @@ -76,7 +76,7 @@ function try_grep_pkgid () { # returns pkgid from stdout workflow_name="$1" release_ver="$2" - resp=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages?null"` || return $? + resp=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages?null"` || return $? [[ "$resp" = "" ]] && echo2 "Invalid api response. Is cookie expired or not correctly set?" && return 2 ! echo "$resp" | json2table Id,Version -p | grep -F "|$release_ver|" > /dev/null && echo "Error: release_ver $release_ver doesn't exist at all" 1>&2 && return 2 @@ -93,7 +93,7 @@ function cis_release_pkg () { release_ver="$2" echo2 "Release package $cis_namespace.$workflow_name:$release_ver..." pkg_id=`try_grep_pkgid "$workflow_name" "$release_ver"` || ! echo2 "Failed to get pkgid. Does release version exist?" || return $? - curl_wrapped -s --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"'"$pkg_id"'","ActionType":505,"Notes":"antidote-auto-release","ActionArgs":{"PackageSourcePath":null,"ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' + curl_wrapped --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"'"$pkg_id"'","ActionType":505,"Notes":"antidote-auto-release","ActionArgs":{"PackageSourcePath":null,"ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' return $? } function cis_set_default_pkgver () { @@ -101,7 +101,7 @@ function cis_set_default_pkgver () { release_ver="$2" echo2 "Setting default version for $cis_namespace.$workflow_name:$release_ver..." pkg_id=`try_grep_pkgid "$workflow_name" "$release_ver"` || ! echo2 "Failed to get pkgid. Does release version exist?" || return $? - curl_wrapped -s --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"'"$pkg_id"'","ActionType":503,"Notes":"antidote-auto-release","ActionArgs":{"PackageSourcePath":null,"ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' + curl_wrapped --data '[{"Cloud":"Public","PackageType":"'"$cis_namespace.$workflow_name"'","Version":"'"$release_ver"'","PackageId":"'"$pkg_id"'","ActionType":503,"Notes":"antidote-auto-release","ActionArgs":{"PackageSourcePath":null,"ScriptParameters":""}}]' "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages/Request" -H 'content-type: application/json' return $? } @@ -137,7 +137,7 @@ function assemble_workflow_parameters () { # Use variable $cis_default_workflow_parameter, query workflow parameter list, and fill them. # The tail of array variable $cis_default_workflow_parameter has higher preference (because params from command line are appended after tail) workflow_name="$1" - params_query_response=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/JobTypeDefinition/$cis_namespace" | json2table WorkflowDefinitions/InputParameters,Name -p | grep "|$workflow_name|" | cut -d '|' -f 1` || ! echo2 "Failed to get Workflow definition for workflow $workflow_name" || return $? + params_query_response=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/JobTypeDefinition/$cis_namespace" | json2table WorkflowDefinitions/InputParameters,Name -p | grep "|$workflow_name|" | cut -d '|' -f 1` || ! echo2 "Failed to get Workflow definition for workflow $workflow_name" || return $? [[ "$params_query_response" = "[]" ]] && params="" || params=`echo "$params_query_response" | json2table Name -p | sed 's/VAL: //g' | grep -oE '[A-Za-z0-9_-]+'` || ! echo2 "Failed to get Workflow definition for workflow $workflow_name" || return $? echo "$params" | _select_param_and_create_json_impl @@ -148,7 +148,7 @@ function assemble_runtime_settings () { # The tail of array variable $cis_default_workflow_parameter has higher preference (because params from command line are appended after tail) # runtime_settings are related to namespace, so we don't need workflow_name. (We're adding an '@' to all param name here) - params=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/JobTypeDefinition/$cis_namespace" | json2table GlobalSettings/Name -p | sed 's/VAL: /@/g' | tr -d '|'` || ! echo2 "Failed to get runtime settings for namespace $cis_namespace" || return $? + params=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/JobTypeDefinition/$cis_namespace" | json2table GlobalSettings/Name -p | sed 's/VAL: /@/g' | tr -d '|'` || ! echo2 "Failed to get runtime settings for namespace $cis_namespace" || return $? echo "$params" | _select_param_and_create_json_impl | tr -d @ } @@ -161,14 +161,14 @@ function cis_run_job () { workflow_params=`assemble_workflow_parameters "$workflow_name"` || ! echo2 "Failed to assemble wf_parameter" || return $? runtime_settings=`assemble_runtime_settings` || ! echo2 "Failed to assemble runtime_settings" || return $? echo2 "Using workflow parameters: $workflow_params, runtime settings: $runtime_settings" - job_id=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace/GenericJob?workflowDefinitionName=$workflow_name" -H 'content-type: application/json' --data '{"JobType":"'"$cis_namespace"'","Workflow":"'"$workflow_name"'","ServiceHostCapability":"AzureJB","ServiceHostLocation":"Any","RuntimePackageId":"4dd32e8b-6bc7-478e-a1ab-48fcdc1bf3d3","PackageId":"'"$pkg_id"'","DisplayName":"Antidote-autosubmitted-WF","WorkflowParameters":{'"$workflow_params"'},"RuntimeSettings":{'"$runtime_settings"'},"WorkflowSettings":{"RuntimeSettings":{'"$runtime_settings"'}}}'` || ! echo2 "failed to get job_id" || return $? + job_id=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace/GenericJob?workflowDefinitionName=$workflow_name" -H 'content-type: application/json' --data '{"JobType":"'"$cis_namespace"'","Workflow":"'"$workflow_name"'","ServiceHostCapability":"AzureJB","ServiceHostLocation":"Any","RuntimePackageId":"4dd32e8b-6bc7-478e-a1ab-48fcdc1bf3d3","PackageId":"'"$pkg_id"'","DisplayName":"Antidote-autosubmitted-WF","WorkflowParameters":{'"$workflow_params"'},"RuntimeSettings":{'"$runtime_settings"'},"WorkflowSettings":{"RuntimeSettings":{'"$runtime_settings"'}}}'` || ! echo2 "failed to get job_id" || return $? [[ "$job_id" =~ ^[0-9][0-9]*_[0-9a-f-]*$ ]] || ! echo2 "Failed to create job. Invalid cookie or invalid workflow_name?" || return 3 echo2 "Waiting for CIS to create job... (job_id=$job_id)" niddle_should_break='"DisplayStatus":"NotStarted"' niddle_should_crash='"CustomState":"Internal Error"' while true; do - resp=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace/GenericJob/$job_id/GetJobHierarchy?null"` + resp=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace/GenericJob/$job_id/GetJobHierarchy?null"` echo "$resp" | grep "$niddle_should_break" > /dev/null && break echo "$resp" | grep "$niddle_should_crash" > /dev/null && echo2 "CIS reported 'Internal Error' while creating job. https://beta-cps.trafficmanager.net/Public/$cis_namespace/JobDetails/$job_id" && return 3 echo2 -n . @@ -191,16 +191,18 @@ function cis_run_job () { function cis_get_job_status () { # Argument should be job_id of *root* workflow. (not child workflow!) jobid="$1" - getjobhierarchy_apires=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace/GenericJob/$jobid/GetJobHierarchy"` && - subworkflows_text=`echo "$getjobhierarchy_apires" | json2table DisplayName,Id,DisplayStatus -p` || ! echo2 "API GetJobHierarchy failed" || return $? + getjobhierarchy_apires=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace/GenericJob/$jobid/GetJobHierarchy"` && + subworkflows_text=`echo "$getjobhierarchy_apires" | json2table DisplayName,Id,DisplayStatus,JobType -p` || ! echo2 "API GetJobHierarchy failed" || return $? if inprogress_callstack=`echo "$subworkflows_text" | grep -F '|InProgress|'`; then # There's some workflow in-progress. It maybe inprogress or blocked. # Note that, there may be multiple workflows in "InProgress" state. In this scenario, # the last "InProgress" workflow is the bottom sub-workflow which we're interested in. # We just check the last sub-workflow, to see if there's any "Blocked" activity. + # Remote workflow may have different namespace, so we need to get bottom_subworkflow_namespace. bottom_subworkflow_id=`echo "$inprogress_callstack" | tail -n 1 | cut -d '|' -f 3` && - bottom_wf_pagedtasks_apires=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace/GenericJob/$bottom_subworkflow_id/WithPagedTasksAndIncidents"` && + bottom_subworkflow_namespace=`echo "$inprogress_callstack" | tail -n 1 | cut -d '|' -f 4` && + bottom_wf_pagedtasks_apires=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$bottom_subworkflow_namespace/GenericJob/$bottom_subworkflow_id/WithPagedTasksAndIncidents"` && bottom_wf_activities=`echo "$bottom_wf_pagedtasks_apires" | json2table Data/Tasks/DisplayName,StateName` || ! echo2 "API WithPagedTasksAndIncidents failed" || return $? if echo "$bottom_wf_activities" | tr -d ' ' | grep -F '|Blocked|' > /dev/null; then # I found some activity blocked! Show incident and exit. @@ -281,7 +283,7 @@ function subcmd_listver () { workflow_name="$1" [[ "$workflow_name" = "" ]] && workflow_name="$cis_default_workflow_name" - resp=`curl_wrapped -s "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages"` || return $? + resp=`curl_wrapped "https://beta-cps.trafficmanager.net/cis.client.svc/Public/$cis_namespace.$workflow_name/Packages"` || return $? [[ "$resp" = "" ]] && echo2 "Invalid api response. Is cookie expired or not correctly set?" && return 2 echo "$resp" | json2table Version,State -p return $?