-
Notifications
You must be signed in to change notification settings - Fork 11
/
create-upload-deploy.sh
executable file
·117 lines (102 loc) · 3.98 KB
/
create-upload-deploy.sh
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
#!/usr/bin/env bash
#
# Create content in RStudio Connect with a given title. Does not prevent the
# creation of duplicate titles. Subsequently, create a bundle, upload that
# bundle to RStudio Connect, deploy that bundle, then wait for deployment to
# complete.
#
# Run this script from the content root directory.
#
set -e
if [ -z "${CONNECT_SERVER}" ] ; then
echo "The CONNECT_SERVER environment variable is not defined. It defines"
echo "the base URL of your RStudio Connect instance."
echo
echo " export CONNECT_SERVER='http://connect.company.com/'"
exit 1
fi
if [[ "${CONNECT_SERVER}" != */ ]] ; then
echo "The CONNECT_SERVER environment variable must end in a trailing slash. It"
echo "defines the base URL of your RStudio Connect instance."
echo
echo " export CONNECT_SERVER='http://connect.company.com/'"
exit 1
fi
if [ -z "${CONNECT_API_KEY}" ] ; then
echo "The CONNECT_API_KEY environment variable is not defined. It must contain"
echo "an API key owned by a 'publisher' account in your RStudio Connect instance."
echo
echo " export CONNECT_API_KEY='jIsDWwtuWWsRAwu0XoYpbyok2rlXfRWa'"
exit 1
fi
if [ $# -eq 0 ] ; then
echo "usage: $0 <content-title>"
exit 1
fi
BUNDLE_PATH="bundle.tar.gz"
# Remove any bundle from previous attempts.
rm -f "${BUNDLE_PATH}"
# Create an archive with all of our Shiny application source and data.
#
# Make sure you bundle the manifest.json and primary content files at the
# top-level; do not include the containing directory in the archive.
echo "Creating bundle archive: ${BUNDLE_PATH}"
tar czf "${BUNDLE_PATH}" manifest.json app.R data
# Only "name" is required by the RStudio Connect API but we use "title" for
# better presentation. We build a random name to avoid colliding with existing
# content.
TITLE="$@"
# Assign a random name. Avoid collisions so we always create something.
# Inspired by http://tldp.org/LDP/abs/html/randomvar.html
letters=(a b c d e f g h i j k l m n o p q r s t u v w x y z)
num_letters=${#letters[*]}
NAME=$(for((i=1;i<=13;i++)); do printf '%s' "${letters[$((RANDOM%num_letters))]}"; done)
# Build the JSON to create content.
DATA=$(jq --arg title "${TITLE}" \
--arg name "${NAME}" \
'. | .["title"]=$title | .["name"]=$name' \
<<<'{}')
RESULT=$(curl --silent --show-error -L --max-redirs 0 --fail -X POST \
-H "Authorization: Key ${CONNECT_API_KEY}" \
--data "${DATA}" \
"${CONNECT_SERVER}__api__/v1/content")
CONTENT=$(echo "$RESULT" | jq -r .guid)
echo "Created content: ${CONTENT}"
# Upload the bundle
UPLOAD=$(curl --silent --show-error -L --max-redirs 0 --fail -X POST \
-H "Authorization: Key ${CONNECT_API_KEY}" \
--data-binary @"${BUNDLE_PATH}" \
"${CONNECT_SERVER}__api__/v1/content/${CONTENT}/bundles")
BUNDLE=$(echo "$UPLOAD" | jq -r .id)
echo "Created bundle: $BUNDLE"
# Deploy the bundle.
DATA=$(jq --arg bundle_id "${BUNDLE}" \
'. | .["bundle_id"]=$bundle_id' \
<<<'{}')
DEPLOY=$(curl --silent --show-error -L --max-redirs 0 --fail -X POST \
-H "Authorization: Key ${CONNECT_API_KEY}" \
--data "${DATA}" \
"${CONNECT_SERVER}__api__/v1/content/${CONTENT}/deploy")
TASK=$(echo "$DEPLOY" | jq -r .task_id)
# Poll until the task completes.
FINISHED=false
CODE=-1
FIRST=0
echo "Deployment task: ${TASK}"
while [ "${FINISHED}" != "true" ] ; do
DATA=$(curl --silent --show-error -L --max-redirs 0 --fail \
-H "Authorization: Key ${CONNECT_API_KEY}" \
"${CONNECT_SERVER}__api__/v1/tasks/${TASK}?wait=1&first=${FIRST}")
# Extract parts of the task status.
FINISHED=$(echo "${DATA}" | jq .finished)
CODE=$(echo "${DATA}" | jq .code)
FIRST=$(echo "${DATA}" | jq .last)
# Present the latest output lines.
echo "${DATA}" | jq -r '.output | .[]'
done
if [ "${CODE}" -ne 0 ]; then
ERROR=$(echo "${DATA}" | jq -r .error)
echo "Task: ${TASK} ${ERROR}"
exit 1
fi
echo "Task: ${TASK} Complete."