Skip to content

Commit 8048616

Browse files
committed
Add rsync-schema
Closes gh-38 Signed-off-by: Josh Cummings <[email protected]>
1 parent 4b758cb commit 8048616

File tree

20 files changed

+879
-1
lines changed

20 files changed

+879
-1
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
with:
1515
submodules: true
16-
- name: Run BATS tests
16+
- name: Run rsync-antora-reference BATS tests
1717
run: ./rsync-antora-reference/test/bats/bin/bats ./rsync-antora-reference/test/
18+
- name: Run rsync-schema BATS tests
19+
run: ./rsync-schema/test/bats/bin/bats ./rsync-schema/test/

rsync-schema/action.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: rsync Antora Docs
2+
description: A GitHub action that syncs Antora reference documentation using rsync with support for syncing a single version. It deploys to the docs server using the github repository in the path.
3+
branding:
4+
icon: 'copy'
5+
color: 'green'
6+
inputs:
7+
docs-username:
8+
description: The username to connect to the docs server
9+
required: true
10+
docs-host:
11+
description: The host of the docs server
12+
required: true
13+
docs-ssh-key:
14+
description: The ssh key used to connect to the docs-host
15+
required: true
16+
docs-ssh-host-key:
17+
description: The docs ssh host key used to connect to docs-host
18+
required: true
19+
dry-run:
20+
description: Set to false if should perform the sync, else a dry run is performed
21+
default: false
22+
required: false
23+
site-path:
24+
description: The path to the schema that should be synced
25+
default: build/schema
26+
required: false
27+
version:
28+
description: The schema version being published
29+
required: true
30+
31+
runs:
32+
using: 'composite'
33+
steps:
34+
- id: publish-docs
35+
shell: bash
36+
run: |
37+
PATH=$PATH:${{ github.action_path }}/src
38+
flag_options=''
39+
if [ -z "${{ inputs.dry-run }}" ]; then
40+
flag_options="${flag_options} --dry-run"
41+
fi
42+
${{ github.action_path }}/src/action.sh --docs-username "${{ inputs.docs-username }}" --docs-host "${{ inputs.docs-host }}" --docs-ssh-key "${{ inputs.docs-ssh-key }}" --docs-ssh-host-key "${{ inputs.docs-ssh-host-key }}" --site-path "${{ inputs.site-path }}" --version "${{ inputs.version }}" --github-repository "${{ github.repository }}" $flag_options

rsync-schema/src/action.sh

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
3+
__action_usage() {
4+
echo "usage: action.sh [OPTION]...
5+
6+
--docs-username=USERNAME the username used to connect to ssh
7+
--docs-host=HOST the host to connect to ssh
8+
--docs-ssh-key=KEY the private key used to connect to ssh
9+
--docs-ssh-host-key=HOST_KEY the host key used to connect to ssh
10+
--dry-run signals that rsync should be in dry run mode
11+
--site-path=PATH the local directory path to sync to the server. Default build/schema
12+
--version=VERSION the release version for the schema
13+
--github-repository=GH_REPO the github repository (e.g. spring-projects/spring-security)
14+
"
15+
}
16+
17+
__action_usage_error() {
18+
echo "Error: $1" >&2
19+
__action_usage
20+
exit 1
21+
}
22+
23+
__action() {
24+
local docs_username docs_host docs_ssh_key docs_ssh_host_key site_path version github_repository valid_args
25+
local rsync_flag_options=''
26+
valid_args=$(getopt --options '' --long docs-username:,docs-host:,docs-ssh-key:,docs-ssh-host-key:,dry-run,site-path:,version:,github-repository: -- "$@")
27+
if [[ $? -ne 0 ]]; then
28+
__action_usage
29+
exit 1;
30+
fi
31+
32+
eval set -- "$valid_args"
33+
34+
while [ : ]; do
35+
case "$1" in
36+
--docs-username)
37+
docs_username="$2"
38+
shift 2
39+
;;
40+
--docs-host)
41+
docs_host="$2"
42+
shift 2
43+
;;
44+
--docs-ssh-key)
45+
docs_ssh_key="$2"
46+
shift 2
47+
;;
48+
--docs-ssh-host-key)
49+
docs_ssh_host_key="$2"
50+
shift 2
51+
;;
52+
--dry-run)
53+
rsync_flag_options="${rsync_flag_options} --dry-run"
54+
shift
55+
;;
56+
--site-path)
57+
site_path="$2"
58+
shift 2
59+
;;
60+
--version)
61+
version="$2"
62+
shift 2
63+
;;
64+
--github-repository)
65+
github_repository="$2"
66+
shift 2
67+
;;
68+
--) shift;
69+
break
70+
;;
71+
*)
72+
__action_usage_error "Invalid argument $1 $2"
73+
;;
74+
esac
75+
done
76+
77+
if [ -z "$docs_username" ]; then
78+
__action_usage_error "Missing option '--docs-username'"
79+
fi
80+
if [ -z "$docs_host" ]; then
81+
__action_usage_error "Missing option '--docs-host'"
82+
fi
83+
if [ -z "$docs_ssh_key" ]; then
84+
__action_usage_error "Missing option '--docs-ssh-key'"
85+
fi
86+
if [ -z "$docs_ssh_host_key" ]; then
87+
__action_usage_error "Missing option '--docs-ssh-host-key'"
88+
fi
89+
if [ -z "$site_path" ]; then
90+
site_path="build/schema"
91+
fi
92+
if [ -z "$version" ]; then
93+
__action_usage_error "Missing option '--version'"
94+
fi
95+
if [ -z "$github_repository" ]; then
96+
__action_usage_error "Missing option '--github-repository'"
97+
fi
98+
99+
100+
## Extract repository_name from the owner/repository_name
101+
local github_repository_name="$(echo ${github_repository} | cut -d '/' -f 2)"
102+
local ssh_private_key_path="$HOME/.ssh/${github_repository:-publish-docs}"
103+
local ssh_host="${docs_username}@${docs_host}"
104+
105+
(
106+
set -e
107+
set -f
108+
109+
local ssh_host_path="/var/www/domains/spring.io/docs/htdocs/autorepo/schema/$github_repository_name/$version/"
110+
local pwd="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
111+
setup_ssh.sh --ssh-private-key-path "$ssh_private_key_path" --ssh-private-key "$docs_ssh_key" --ssh-known-host "$docs_ssh_host_key"
112+
rsync_schema.sh --ssh-host "$ssh_host" --ssh-host-path "$ssh_host_path" --local-path "$site_path" --ssh-private-key-path "$ssh_private_key_path" $rsync_flag_options
113+
)
114+
exit_code=$?
115+
116+
cleanup_ssh.sh --ssh-private-key-path "$ssh_private_key_path"
117+
118+
exit $exit_code
119+
}
120+
121+
122+
__action "$@"
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
3+
__check_github_repository_owner_usage() {
4+
echo "usage: check_github_repository_owner.sh [OPTION]...
5+
6+
--github-repository=REPO the github repository (e.g. spring-projects/spring-security)
7+
--ssh-docs-path=PATH the full path that the docs will be deployed to (e.g. https://docs.spring.io/spring-security/reference/ is \${HTTP_DOCS}/spring-security/reference)
8+
"
9+
}
10+
11+
__check_github_repository_owner_usage_error() {
12+
echo "Error: $1" >&2
13+
__check_github_repository_owner_usage
14+
exit 1
15+
}
16+
17+
__check_github_repository_owner() {
18+
local github_repository ssh_docs_path valid_args
19+
valid_args=$(getopt --options '' --long ,github-repository:,ssh-docs-path: -- "$@")
20+
if [[ $? -ne 0 ]]; then
21+
__check_github_repository_owner_usage
22+
exit 1;
23+
fi
24+
25+
eval set -- "$valid_args"
26+
27+
while [ : ]; do
28+
case "$1" in
29+
--github-repository)
30+
github_repository="$2"
31+
shift 2
32+
;;
33+
--ssh-docs-path)
34+
ssh_docs_path="$2"
35+
shift 2
36+
;;
37+
--) shift;
38+
break
39+
;;
40+
*)
41+
__check_github_repository_owner_usage_error "Invalid argument $1 $2"
42+
;;
43+
esac
44+
done
45+
46+
if ! [[ "$github_repository" =~ .+/.+ ]]; then
47+
__check_github_repository_owner_usage_error " '--github-repository' must be in the form of <owner>/<name> but got '$github_repository'"
48+
fi
49+
if ! [[ "$ssh_docs_path" =~ ^/.+ ]]; then
50+
__check_github_repository_owner_usage_error " '--ssh-docs-path' must start with and not equal / but got '$ssh_docs_path'"
51+
fi
52+
53+
local marker_file="${ssh_docs_path}/.github-repository"
54+
55+
if [ -d "$ssh_docs_path" ]; then
56+
# The path exists so ensure the marker file contents contain github_repository
57+
local marker_file_content=""
58+
if [ -f "$marker_file" ]; then
59+
marker_file_content="$(cat $marker_file)"
60+
fi
61+
if [ "$marker_file_content" == "$github_repository" ]; then
62+
echo "Owner is verified"
63+
exit 0
64+
else
65+
echo "Failed to verify that $ssh_docs_path is owned by $github_repository because the file $marker_file contains $marker_file_content" >&2
66+
exit 2
67+
fi
68+
else
69+
# The path does not yet exist so create the folder and add the marker file
70+
echo "Directory $ssh_docs_path does not exist. Marking as owned by $github_repository"
71+
mkdir -p "$ssh_docs_path"
72+
echo -n "$github_repository" >$marker_file
73+
exit 0
74+
fi
75+
76+
}
77+
78+
__check_github_repository_owner "$@"

rsync-schema/src/cleanup_ssh.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
__cleanup_ssh_usage() {
4+
echo "usage: cleanup_ssh.sh [OPTION]...
5+
6+
--ssh-private-key-path=PATH the path to the private key to use
7+
"
8+
}
9+
10+
__cleanup_ssh_usage_error() {
11+
echo "Error: $1" >&2
12+
usage
13+
exit 1
14+
}
15+
16+
__cleanup_ssh() {
17+
local ssh_private_key_path valid_args
18+
valid_args=$(getopt --options '' --long ssh-private-key-path: -- "$@")
19+
if [[ $? -ne 0 ]]; then
20+
usage
21+
exit 1;
22+
fi
23+
24+
eval set -- "$valid_args"
25+
26+
while [ : ]; do
27+
case "$1" in
28+
--ssh-private-key-path)
29+
ssh_private_key_path="$2"
30+
shift 2
31+
;;
32+
--) shift;
33+
break
34+
;;
35+
*)
36+
__cleanup_ssh_usage_error "Invalid argument $1 $2"
37+
;;
38+
esac
39+
done
40+
41+
rm -f "$ssh_private_key_path"
42+
}
43+
44+
__cleanup_ssh "$@"

rsync-schema/src/rsync_schema.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash
2+
3+
__rsync_schema_usage() {
4+
echo "usage: rsync_schema.sh [OPTION]...
5+
6+
--ssh-host=SSH_HOST the ssh host in the format USER@HOST to connect to ssh
7+
--ssh-private-key-path=KEY_PATH the path to the private key used to connect to ssh
8+
--ssh-host-path=HOST_PATH the path of the ssh server to sync the documentation to
9+
--dry-run signals that rsync should be in dry run mode
10+
--local-path=PATH the local directory path to sync to the server
11+
"
12+
}
13+
14+
__rsync_schema_usage_error() {
15+
echo "Error: $1" >&2
16+
__rsync_schema_usage
17+
exit 1
18+
}
19+
20+
__rsync_schema() {
21+
local ssh_host ssh_host_path local_path ssh_private_key_path
22+
local dry_run="false"
23+
valid_args=$(getopt --options '' --long ssh-host:,ssh-host-path:,dry-run,local-path:,ssh-private-key-path: -- "$@")
24+
if [[ $? -ne 0 ]]; then
25+
__rsync_schema_usage
26+
exit 1;
27+
fi
28+
29+
eval set -- "$valid_args"
30+
31+
while [ : ]; do
32+
case "$1" in
33+
--ssh-host)
34+
ssh_host="$2"
35+
shift 2
36+
;;
37+
--ssh-host-path)
38+
ssh_host_path="$2"
39+
shift 2
40+
;;
41+
--local-path)
42+
local_path="$2"
43+
shift 2
44+
;;
45+
--ssh-private-key-path)
46+
ssh_private_key_path="$2"
47+
shift 2
48+
;;
49+
--dry-run)
50+
dry_run=true
51+
shift
52+
;;
53+
--) shift;
54+
break
55+
;;
56+
*)
57+
__rsync_schema_usage_error "Invalid argument $1 $2"
58+
;;
59+
esac
60+
done
61+
62+
if [ -z "$ssh_host" ]; then
63+
__rsync_schema_usage_error "Missing option '--ssh-host'"
64+
fi
65+
if [ -z "$ssh_host_path" ]; then
66+
__rsync_schema_usage_error "Missing option '--ssh-host-path'"
67+
fi
68+
if [ -z "$local_path" ]; then
69+
__rsync_schema_usage_error "Missing option '--local-path'"
70+
fi
71+
if [ -z "$ssh_private_key_path" ]; then
72+
__rsync_schema_usage_error "Missing option '--ssh-private-key-path'"
73+
fi
74+
75+
local rsync_opts='-avz --delete '
76+
if [ "$dry_run" != "false" ]; then
77+
rsync_opts="$rsync_opts --dry-run "
78+
fi
79+
if [ -f "$local_path/.htaccess" ]; then
80+
rsync_opts="$rsync_opts --include /.htaccess "
81+
fi
82+
if [ -d "$local_path/.cache" ]; then
83+
rsync_opts="$rsync_opts$(find $local_path/.cache -printf ' --include /.cache/%P')"
84+
fi
85+
rsync_opts="$rsync_opts --exclude /.github-repository --exclude /.cache --exclude /.cache/* "
86+
# Disable filename expansion (globbing)
87+
# https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
88+
(
89+
set -f
90+
rsync $rsync_opts -e "ssh -i $ssh_private_key_path" $local_path/ "$ssh_host:$ssh_host_path"
91+
)
92+
}
93+
94+
95+
__rsync_schema "$@"

0 commit comments

Comments
 (0)