Commit 075d3a90 authored by Visay Keo's avatar Visay Keo
Browse files

Merge branch 'custom_ssh_port' into 'master'

[TASK] Enable ssh remote port option

* to override the default ssh port (22), just use --remote-port option

See merge request !3
parents ea820944 7c2f8f13
Loading
Loading
Loading
Loading
+6 −3
Original line number Original line Diff line number Diff line
@@ -32,6 +32,7 @@ bin-dir/syncontent --remote-user=demo-014-007
- You need to make sure that you have auto login with public key to `demo-014-007@YOUR_SERVER_HOSTNAME_OR_IP`.
- 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 my internal web server
The default host server is `10.10.10.27`, which is my internal web server
- You can define your own server host with `--remote-host=YOUR_SERVER_HOSTNAME_OR_IP` argument
- You can define your own server host with `--remote-host=YOUR_SERVER_HOSTNAME_OR_IP` argument
- You can define your own server ssh port with `--remote-port=YOUR_SERVER_SSH_PORT` argument
- You can only run the command from the root directory of your project
- You can only run the command from the root directory of your project
- The document root on the server will have to be at `/home/demo-014-007/public_html`
- 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
- If you have a different document root path, you can overwrite the default with `--remote-path` argument
@@ -39,19 +40,20 @@ The default host server is `10.10.10.27`, which is my internal web server
So the full overwriting way would be:
So the full overwriting way would be:


```bash
```bash
bin-dir/syncontent --remote-user=demo-014-007 --remote-host=10.10.10.37 --remote-path=/home/user/neosbox
bin-dir/syncontent --remote-user=demo-014-007 --remote-host=10.10.10.37 --remote-port=2222 --remote-path=/home/user/neosbox
```
```


Or short form of the arguments:
Or short form of the arguments:


```bash
```bash
bin-dir/syncontent -u=demo-014-007 -h=10.10.10.37 -p=/home/user/neosbox
bin-dir/syncontent -u=demo-014-007 -h=10.10.10.37 -P=2222 -p=/home/user/neosbox
```
```


### Available arguments
### Available arguments


- `-u` | `--remote-user` : Set the login user to the remote server (__Required__)
- `-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`)
- `-h` | `--remote-host` : Set the hostname or IP address of the remote server (__Default__: `10.10.10.27`)
- `-P` | `--remote-port` : Set the ssh port of the remote server (__Default__: `22`)
- `-p` | `--remote-path` : Set path to the document root on the remote server (__Default__: `/home/<remote-user>/public_html`)
- `-p` | `--remote-path` : Set path to the document root on the remote server (__Default__: `/home/<remote-user>/public_html`)
- `-t` | `--ansible-tags`: Set the specific task you want run. See available sync tasks section for details
- `-t` | `--ansible-tags`: Set the specific task you want run. See available sync tasks section for details
- `--remote-php`         : Set the path to php on the remote server
- `--remote-php`         : Set the path to php on the remote server
@@ -80,10 +82,11 @@ vendor-dir
		└── README.md
		└── README.md
```
```


You can also overwrite the default value of `--remote-host`, `--remote-path`, `--remote-php`, etc, by setting it in the created master file.
You can also overwrite the default value of `--remote-host`, `--remote-port`, `--remote-path`, `--remote-php`, etc, by setting it in the created master file.


```vendor-dir/visay/syncontent/config/master/demo-014-007
```vendor-dir/visay/syncontent/config/master/demo-014-007
--remote-host=10.10.10.37
--remote-host=10.10.10.37
--remote-port=2222
--remote-path=/home/user/neosbox
--remote-path=/home/user/neosbox
--remote-php=/opt/php-versions/php55/bin/php
--remote-php=/opt/php-versions/php55/bin/php
```
```
+1 −1
Original line number Original line Diff line number Diff line
localhost ansible_connection=local
localhost ansible_connection=local


[target]
[target]
remote ansible_ssh_host={{ HOST }} ansible_ssh_private_key_file=~/.ssh/id_rsa
remote ansible_ssh_host={{ HOST }} ansible_ssh_port={{ PORT }} ansible_ssh_private_key_file=~/.ssh/id_rsa
+32 −6
Original line number Original line Diff line number Diff line
@@ -49,7 +49,7 @@ if [ -z "${FRAMEWORK}" ]; then
	exit 1
	exit 1
fi
fi


# Read all arguments and parse into variable ${REMOTE_USER}, ${REMOTE_PATH} and ${REMOTE_HOST}
# Read all arguments and parse into variable ${REMOTE_USER}, ${REMOTE_PATH}, ${REMOTE_HOST} and ${REMOTE_PORT}
for ARGS in "$@"
for ARGS in "$@"
do
do
	case ${ARGS} in
	case ${ARGS} in
@@ -65,6 +65,10 @@ do
		REMOTE_HOST="${ARGS#*=}"
		REMOTE_HOST="${ARGS#*=}"
		shift
		shift
		;;
		;;
		-P=*|--remote-port=*)
		REMOTE_PORT="${ARGS#*=}"
		shift
		;;
		-t=*|--ansible-tags=*)
		-t=*|--ansible-tags=*)
		ANSIBLE_TAGS="--tags=${ARGS#*=}"
		ANSIBLE_TAGS="--tags=${ARGS#*=}"
		shift
		shift
@@ -129,17 +133,39 @@ if [ -z "${REMOTE_HOST}" ]; then
	fi
	fi
fi
fi


# If remote port is not set, read it from config file
if [ -z "${REMOTE_PORT}" ]; then
	if [ -f "${MASTER_FILE}" ]; then
		IFS=$'\n'
		for LINE in `cat ${MASTER_FILE}`
		do
			LINE=${LINE//[[:blank:]]/}
			case ${LINE} in
				-P=*|--remote-port=*)
				REMOTE_PORT="${LINE#*=}"
				shift
				;;
			esac
		done
	fi
fi

# If remote host cannot be found anywhere, fallback to default web server
# If remote host cannot be found anywhere, fallback to default web server
if [ -z "${REMOTE_HOST}" ]; then
if [ -z "${REMOTE_HOST}" ]; then
	REMOTE_HOST="10.10.10.27"
	REMOTE_HOST="10.10.10.27"
fi
fi


# If remote port cannot be found anywhere, fallback to default ssh port 22
if [ -z "${REMOTE_PORT}" ]; then
	REMOTE_PORT="22"
fi

# Test connection to the server whether auto login is enabled
# Test connection to the server whether auto login is enabled
PING=`ssh -q -o "BatchMode=yes" ${REMOTE_USER}@${REMOTE_HOST} echo "OK"`
PING=`ssh -q -o "BatchMode=yes" ${REMOTE_USER}@${REMOTE_HOST} -p ${REMOTE_PORT} echo "OK"`
if [ "${PING}" != "OK" ]; then
if [ "${PING}" != "OK" ]; then
	echo -e ${RED}"-> Public key authentication failed!"${NC}
	echo -e ${RED}"-> Public key authentication failed!"${NC}
	echo
	echo
	echo -e ${BROWN}"Make sure you have auto login when running: ${WHITE}ssh ${REMOTE_USER}@${REMOTE_HOST}"${NC}
	echo -e ${BROWN}"Make sure you have auto login when running: ${WHITE}ssh ${REMOTE_USER}@${REMOTE_HOST} -p ${REMOTE_PORT}"${NC}
	echo
	echo
	exit 1
	exit 1
fi
fi
@@ -214,7 +240,7 @@ if [ "${FRAMEWORK}" == "typo3" ]; then
		exit 1
		exit 1
	fi
	fi


	if (ssh ${REMOTE_USER}@${REMOTE_HOST} "[ ! -f ${REMOTE_DUMP} ]"); then
	if (ssh ${REMOTE_USER}@${REMOTE_HOST} -p ${REMOTE_PORT} "[ ! -f ${REMOTE_DUMP} ]"); then
		echo -e ${RED}"-> Please make sure the dump file at ${REMOTE_DUMP} exists on remote server"${NC}
		echo -e ${RED}"-> Please make sure the dump file at ${REMOTE_DUMP} exists on remote server"${NC}
		echo
		echo
		exit 1
		exit 1
@@ -222,7 +248,7 @@ if [ "${FRAMEWORK}" == "typo3" ]; then
fi
fi


# Check if data directory exists at the remote path on the server
# Check if data directory exists at the remote path on the server
if (ssh ${REMOTE_USER}@${REMOTE_HOST} "[ ! -d ${REMOTE_PATH}/${DATA_DIR} ]"); then
if (ssh ${REMOTE_USER}@${REMOTE_HOST} -p ${REMOTE_PORT} "[ ! -d ${REMOTE_PATH}/${DATA_DIR} ]"); then
	echo -e ${RED}"-> Directory '${REMOTE_PATH}/${DATA_DIR}' does not exist on the server running ${FRAMEWORK}"${NC}
	echo -e ${RED}"-> Directory '${REMOTE_PATH}/${DATA_DIR}' does not exist on the server running ${FRAMEWORK}"${NC}
	echo
	echo
	echo -e ${BROWN}"Check if your '--remote-path' config is set properly"${NC}
	echo -e ${BROWN}"Check if your '--remote-path' config is set properly"${NC}
@@ -232,7 +258,7 @@ fi


# Create dynamic inventory file
# Create dynamic inventory file
rm -f ${PACKAGE_DIR}/ansible/hosts.ini
rm -f ${PACKAGE_DIR}/ansible/hosts.ini
sed "s/{{ HOST }}/${REMOTE_HOST}/g" ${PACKAGE_DIR}/ansible/hosts.ini.template > ${PACKAGE_DIR}/ansible/hosts.ini
sed "s/{{ HOST }}/${REMOTE_HOST}/g;s/{{ PORT }}/${REMOTE_PORT}/g" ${PACKAGE_DIR}/ansible/hosts.ini.template > ${PACKAGE_DIR}/ansible/hosts.ini


# Prepare all parameters for ansible
# Prepare all parameters for ansible
ANSIBLE_PARAMS="-i ${PACKAGE_DIR}/ansible/hosts.ini ${PACKAGE_DIR}/ansible/playbook.yml"
ANSIBLE_PARAMS="-i ${PACKAGE_DIR}/ansible/hosts.ini ${PACKAGE_DIR}/ansible/playbook.yml"