Commit 80fc39e8 authored by Visay Keo's avatar Visay Keo

[IMP] Allow setting of remote path through config

parent 6d9f8ad3
## Introduction
This is a helper tool for fetch content from Flow Framework / Neos CMS installation on a remote server to your local computer running with docker.
This is a helper tool for fetch content from Flow Framework / Neos CMS installation on a remote server to your local
computer running with docker.
## Dependency
This is requires that your Flow Framework / Neos CMS installation is configured to run with `dockerflow` package. See https://github.com/Sebobo/Shel.DockerFlow
This is requires that your Flow Framework / Neos CMS installation is configured to run with `dockerflow` package
(see https://github.com/Sebobo/Shel.DockerFlow) and all containers are started before executing the sync.
## Installation
......@@ -20,24 +22,40 @@ Add the `syncontent` package into your `composer.json` file and update `composer
In the root directory of your project, execute:
```bash
bin/syncontent demo-014-007
bin/syncontent --remote-user=demo-014-007
```
- Replace the user `demo-014-007` with the one you want to get content from.
- You need to make sure that you have auto login with public key to `demo-014-007@10.10.10.27`
- Replace the user `demo-014-007` with the one you want to get content from
- You need to make sure that you have auto login with public key to `demo-014-007@YOUR_SERVER_HOSTNAME_OR_IP`.
The default host server is `10.10.10.27`, which is the internal web server
- You can define different server host with `--remote-host=YOUR_SERVER_HOSTNAME_OR_IP` argument
- Only `latest` or `demo` is supported at the moment. We don't recommend you to have autologin access to the live site
- You can only run the command from the root directory of your project
- If your remote path is in another directory below `public_html`, set it in the second argument like below:
- The document root on the server will have to be at `/home/demo-014-007/public_html`
- If you have a different document root path, you can overwrite the default with `--remote-path` argument
So the full overwriting way would be:
```bash
bin/syncontent --remote-user=demo-014-007 --remote-host=10.10.10.37 --remote-path=/home/user/neosbox
```
Or short form of the arguments:
```bash
bin/syncontent demo-014-007 neosbox
bin/syncontent demo-014-007 neosbox/my_root_dir
bin/syncontent -u=demo-014-007 -h=10.10.10.37 -p=/home/user/neosbox
```
### Available arguments
- `-u` | `--remote-user` : Set the login user to the remote server (__Required__)
- `-h` | `--remote-host` : Set the hostname or IP address of the remote server (__Default__: `10.10.10.27`)
- `-p` | `--remote-path` : Set path to the document root on the remote server (__Default__: `/home/<remote-user>/public_html`)
### Customization
If you are lazy typing the user again and again, you can define it by creating a file in
`Packages/Libraries/visay/syncontent/config/master` with the same name as the user of the server you want to use.
If you are lazy typing the user again and again, you can define it permanently by creating a file in
`Packages/Libraries/visay/syncontent/config/master` with the same name as the `--remote-user` value.
```
Packages
......@@ -48,23 +66,33 @@ Packages
├── bin
├── composer.json
├── config
   └── master
   └── demo-014-007
└── master
└── demo-014-007
└── README.md
```
With this file, you can now execute the content sync with just:
You can also overwrite the default value of `--remote-host` and `--remote-path` by setting it in the created master file.
```Packages/Libraries/visay/syncontent/config/master/demo-014-007
--remote-host=10.10.10.37
--remote-path=/home/user/neosbox
```
With this file and its content, you can now execute the content sync with just:
```bash
bin/syncontent
```
And the script will automatically take `demo-014-007` as the content master.
And the script will automatically take `demo-014-007` as the content master, `10.10.10.37` as server host and
`/home/user/neosbox` as remote path.
- You can also combine that some settings are in config file and some are passed as argument
- If the same config are set in the config and pass as argument, the argument value will take effect
- There should be only one master at a time. That means you should not have more than more file in the `config/master`
directory. In case there are more than one file exists, the script will take first file only to process.
- With this mode, you cannot overwrite the remote path as argument. This will come in the next release of this package.
- In most case, the default host and path work fine for internal Web Essentials project development
and you just need to define the `--remote-user`
## Author
......
#!/bin/bash
USER_NAME=$1
# Set static variables
ROOT_DIR=`pwd`
PACKAGE_DIR="${ROOT_DIR}/Packages/Libraries/visay/syncontent"
if [ -z "${USER_NAME}" ]; then
MASTER=`ls ${PACKAGE_DIR}/config/master | sort -n | head -1`
else
MASTER="${USER_NAME}"
REMOTE_DIR=$2
# Read all arguments and parse into variable ${REMOTE_USER}, ${REMOTE_PATH} and ${REMOTE_HOST}
for ARGS in "$@"
do
case ${ARGS} in
-u=*|--remote-user=*)
REMOTE_USER="${ARGS#*=}"
shift
;;
-p=*|--remote-path=*)
REMOTE_PATH="${ARGS#*=}"
shift
;;
-h=*|--remote-host=*)
REMOTE_HOST="${ARGS#*=}"
shift
;;
esac
done
# If remote user is not supplied, read configuration from file
if [ -z "${REMOTE_USER}" ]; then
REMOTE_USER=`ls ${PACKAGE_DIR}/config/master | sort -n | head -1`
fi
if [ -z "${MASTER}" ]; then
echo "Please specify the server to sync from:"
# If remote user is not set anywhere, throw error
if [ -z "${REMOTE_USER}" ]; then
echo "Please specify the user of the remote server to sync from:"
echo "Example: bin/syncontent latest-014-007"
echo "Example: bin/syncontent demo-014-007"
echo "Or specify it in config/master directory, read the doc"
exit 0
fi
STAGE=`echo ${MASTER} | cut -d '-' -f1`
# Extract from the remote user to get the stage name
MASTER_FILE="${PACKAGE_DIR}/config/master/${REMOTE_USER}"
STAGE=`echo ${REMOTE_USER} | cut -d '-' -f1`
# Check if stage is not latest or demo, throw error
if [ "${STAGE}" != "latest" ] && [ "${STAGE}" != "demo" ]; then
echo "Only latest or demo is supported at the moment:"
echo "Example: bin/syncontent latest-014-007"
......@@ -28,30 +49,74 @@ if [ "${STAGE}" != "latest" ] && [ "${STAGE}" != "demo" ]; then
exit 0
fi
SSH_USER="${MASTER}"
SSH_HOST="10.10.10.27"
# If remote host is not set, read it from config file
if [ -z "${REMOTE_HOST}" ]; then
if [ -f "${MASTER_FILE}" ]; then
IFS=$'\n'
for LINE in `cat ${MASTER_FILE}`
do
LINE=${LINE//[[:blank:]]/}
case ${LINE} in
-h=*|--remote-host=*)
REMOTE_HOST="${LINE#*=}"
shift
;;
esac
done
fi
fi
PING=`ssh -q -o "BatchMode=yes" ${SSH_USER}@${SSH_HOST} echo "OK"`
# If remote host cannot be found anywhere, fallback to default web server
if [ -z "${REMOTE_HOST}" ]; then
REMOTE_HOST="10.10.10.27"
fi
# Test connection to the server whether auto login is enabled
PING=`ssh -q -o "BatchMode=yes" ${REMOTE_USER}@${REMOTE_HOST} echo "OK"`
if [ "${PING}" != "OK" ]; then
echo "Public key authentication failed!"
echo "Make sure you have auto login when running: ssh ${SSH_USER}@${SSH_HOST}"
echo "Make sure you have auto login when running: ssh ${REMOTE_USER}@${REMOTE_HOST}"
exit 0
fi
if [ -z "${REMOTE_DIR}" ]; then
REMOTE_PATH="/home/${SSH_USER}/public_html"
else
REMOTE_PATH="/home/${SSH_USER}/public_html/${REMOTE_DIR}"
# If remote path is not set in argument, check for it in the configuration file
if [ -z "${REMOTE_PATH}" ]; then
if [ -f "${MASTER_FILE}" ]; then
IFS=$'\n'
for LINE in `cat ${MASTER_FILE}`
do
LINE=${LINE//[[:blank:]]/}
case ${LINE} in
-p=*|--remote-path=*)
REMOTE_PATH="${LINE#*=}"
shift
;;
esac
done
fi
fi
# If remote path is defined anywhere, set a standard value
if [ -z "${REMOTE_PATH}" ]; then
REMOTE_PATH="/home/${REMOTE_USER}/public_html"
fi
# Check if persistent directory exists at the remote path on the server
if (ssh ${REMOTE_USER}@${REMOTE_HOST} "[ ! -d ${REMOTE_PATH}/Data/Persistent ]"); then
echo "Directory '${REMOTE_PATH}/Data/Persistent' does not exist on the server"
echo "Check if your '--remote-path' config is set properly"
exit 0
fi
# Use Development context if Flow Context is not set
FLOW_CONTEXT=${FLOW_CONTEXT:=Development}
echo "Start content sync from ${SSH_USER} (Production context) to local (${FLOW_CONTEXT} context)"
echo "Start content sync from ${REMOTE_USER} (Production context)"
echo " to local docker in (${FLOW_CONTEXT} context)"
ansible-playbook -i ${PACKAGE_DIR}/ansible/hosts.ini ${PACKAGE_DIR}/ansible/playbook.yml \
--extra-vars "stage=${STAGE}
ssh_user=${SSH_USER}
ssh_user=${REMOTE_USER}
local_path=${ROOT_DIR}
remote_path=${REMOTE_PATH}
local_flow_context=${FLOW_CONTEXT}"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment