Skip to content

Commit

Permalink
Support multiple users deploying to different ports
Browse files Browse the repository at this point in the history
Add parameter `speed_user` so deployment can be done to different user
accounts. Add parameter `speed_port` so test server process can be
listening at different ports.
  • Loading branch information
awangc committed Nov 1, 2016
1 parent df7f794 commit 87a140f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
17 changes: 11 additions & 6 deletions ansible/README.md
Expand Up @@ -107,14 +107,15 @@ The above removes ssh host checking, so follow them at your own risk!
We can now issue the command to install

```
ansible-playbook deploy-servers.yml [-e version=<version_name>,aws_access=<aws access key>,aws_secret=<aws secret key>,aws_endpoint=<aws endpoint>]
ansible-playbook deploy-servers.yml [-e version=<version_name> speed_user=<test_user> speed_port=<server_port> aws_access=<aws access key> aws_secret=<aws secret key> aws_endpoint=<aws endpoint>]
```

Ansible will create a user named `test-user` if not present in the server. If `version_name` is not passed when
invoking the `ansible-playbook` command, the default `version_name` created will be `YYYYMMDDHHmmSS.<git hash>`. Do not change files
Ansible will create a user named `test-user` if not present in the server. It will start the test server process on port `server_port`.
If `version_name` is not passed when invoking the `ansible-playbook` command, the default `version_name` created will be `YYYYMMDDHHmmSS.<git hash>`.
Do not change files
or checkout a different git branch while the deployment playbook is executing as this may affect the code that is deployed.
Ansible will zip `package.json`, `index.js`, `config`, `modules` and `public/` and push to the test server, unzipping it under
`/opt/Speed-testJS_<version_name>`. It will then symlink `/opt/Speed-testJS` to that directory. It will install
`/opt/test-user/Speed-testJS_<version_name>`. It will then symlink `/opt/Speed-testJS` to that directory. It will install
`node`, `npm` and perform `npm install` to pull all the modules. It will also install `aws` credentials that
are used to store test results to DynamoDB.

Expand All @@ -127,16 +128,20 @@ of the process, you can ssh into the server
```
~/Speed-testJS/ansible$ ssh -i private/hackathon.pem clduser@<my-test-server>
[clduser@my-test-server ~]$ sudo su test-server
[clduser@my-test-server ~]$ sudo su test-user
[test-user@my-test-server ~]$ pm2 status
[test-user@my-test-server ~]$ pm2 logs # for checking logs
```

The log file is under `/var/logs/speed-test/speed-test.log`. Logrotate is configured to keep
The log file is under `/var/logs/test-user/speed-test/speed-test.log`. Logrotate is configured to keep
it under 100 MB, and keep 3 most recent log files.

Ansible will keep 5 versions of the software before starting to remove older deployments.

To have multiple users deploying to the same server just pass different names and ports for `speed_user` and `speed_port`. The test
server process will take the `speed_port` passed and the one immediately following it, thus the ports should be spaced by at least 2, i.e.,
if passing port 8080 to one user, the next available port will be 8082.

## Configuring via jumphost

If the test servers are not directly accessible, and we need to go through a jumphost to configure them, we
Expand Down
6 changes: 4 additions & 2 deletions ansible/deploy-servers.yml
Expand Up @@ -3,7 +3,8 @@
#
# optional parameters:
# from_dir: a path to the directory containing index.js, package.json and public/ (defaults to '..')
#
# speed_user: the user under which the software will be deployed
# speed_port: the port speed test server will be listening
# version: the version set for the code base
#

Expand All @@ -25,7 +26,8 @@
environment:
PATH: "/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
vars:
- test_user: 'test-user'
- test_user: "{{ speed_user | default('test-user') }}"
- web_port: "{{ speed_port | default(8080) }}"
- from_dir: '..'

pre_tasks:
Expand Down
6 changes: 3 additions & 3 deletions ansible/roles/test-server/defaults/main.yml
Expand Up @@ -5,9 +5,9 @@
prefix: "{{ bin_prefix | default ('Speed-testJS') }}"
zipfile: "{{ prefix }}.{{ code_version }}.zip"
zip_tmp: "/tmp/{{ zipfile }}"
project_dir : "/opt/{{ prefix }}"
dest_dir: "/opt/{{ prefix }}_{{ code_version }}"
log_dir: "/var/log/speed-test"
project_dir : "/opt/{{ test_user }}/{{ prefix }}"
dest_dir: "/opt/{{ test_user }}/{{ prefix }}_{{ code_version }}"
log_dir: "/var/log/{{ test_user }}/speed-test"
log_file: "speed-test.log"
log_path: "{{ log_dir }}/{{ log_file }}"
log_size_limit: "100M"
Expand Down
2 changes: 1 addition & 1 deletion ansible/roles/test-server/tasks/main.yml
Expand Up @@ -79,7 +79,7 @@
become_user: "{{ test_user }}"

- name: start service via pm2
shell: 'env PATH=/usr/local/bin:$PATH:/usr/bin pm2 start --node-args="--max-old-space-size={{ nodejs_mem }}" "{{ project_dir }}/index.js" --name speed-test --user {{ test_user }} --watch --merge-logs --output {{ log_path }} --error {{ log_path }} --log-date-format "YYYY-MM-DD HH:mm:ss,SSS "'
shell: 'env WEB_PORT={{ web_port }} PATH=/usr/local/bin:$PATH:/usr/bin pm2 start --node-args="--max-old-space-size={{ nodejs_mem }}" "{{ project_dir }}/index.js" --name speed-test --user {{ test_user }} --watch --merge-logs --output {{ log_path }} --error {{ log_path }} --log-date-format "YYYY-MM-DD HH:mm:ss,SSS "'
become: yes
become_user: "{{ test_user }}"

Expand Down
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -31,8 +31,8 @@ var downloadData = require('./modules/downloadData');
var dynamo = require('./modules/dynamo');

//variables
var webPort = 3000;
var webSocketPort = 3001;
var webPort = +process.env.WEB_PORT || 8080;
var webSocketPort = webPort + 1;

//used to read post data
app.use(bodyParser.urlencoded({ extended: false }));
Expand Down

0 comments on commit 87a140f

Please sign in to comment.