Skip to content

Commit

Permalink
Add react prefix to all variables
Browse files Browse the repository at this point in the history
  • Loading branch information
moshthepitt committed Mar 28, 2019
1 parent ce562ed commit 6c9d67e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 72 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ We intentionally consider the installation and configuration of web servers, and
Some of the more important variables are briefly described below. You can see all variables by looking at the `defaults/main.yml` file.

```yml
system_user: "react" # name of the user that will own the django installation
node_version: 10.x # the version of node to install
react_system_user: "react" # name of the user that will own the django installation
react_node_version: 10.x # the version of node to install

git_url: "https://github.com/onaio/kaznet-frontend.git" # the git repo of your django app which we are installing
git_key:
react_git_url: "https://github.com/onaio/kaznet-frontend.git" # the git repo of your django app which we are installing
react_git_key:
```
### Custom environment variables
[Create react app](https://github.com/facebook/create-react-app) supports [custom environment variables](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-custom-environment-variables) and this role does too!
You can set custom environment variables by using the `app_settings` variable, like so:
You can set custom environment variables by using the `react_app_settings` variable, like so:

```yml
app_settings:
react_app_settings:
REACT_APP_WEBSITE_NAME: 'Example App'
SOMETHING_ELSE: "you can put anything here"
```
Expand Down
34 changes: 17 additions & 17 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# user
system_user: "react"
system_group: "www-data"
system_user_home: "/home/{{ system_user }}"
react_system_user: "react"
react_system_group: "www-data"
react_system_user_home: "/home/{{ react_system_user }}"

# git
git_url:
git_version: "master"
git_key:
git_key_filename: "id_ed25519"
remove_git_key: True
react_git_url:
react_git_version: "master"
react_git_key:
react_git_key_filename: "id_ed25519"
react_remove_git_key: True

# system-wide dependencies
system_wide_dependencies:
react_system_wide_dependencies:
- git
- nodejs
- yarn
node_version: 10.x
react_node_version: 10.x

# app
env_name: "prod"
app_name: "{{ system_user }}"
codebase_path: "{{ system_user_home }}/app"
versioned_path: "{{ codebase_path }}-versioned"
checkout_path: "{{ versioned_path }}/{{ ansible_date_time['epoch'] }}"
log_path: "/var/log/{{ app_name }}"
react_env_name: "prod"
react_app_name: "{{ react_system_user }}"
react_codebase_path: "{{ react_system_user_home }}/app"
react_versioned_path: "{{ react_codebase_path }}-versioned"
react_checkout_path: "{{ react_versioned_path }}/{{ ansible_date_time['epoch'] }}"
react_log_path: "/var/log/{{ react_app_name }}"

# app settings
app_settings:
react_app_settings:
REACT_APP_WEBSITE_NAME: 'React Example App'
4 changes: 2 additions & 2 deletions tasks/cleanup.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
- name: Get previous app deploy listing
shell: ls -tr1 chdir={{ versioned_path }}
shell: ls -tr1 chdir={{ react_versioned_path }}
register: versioned_list

- name: Remove extra files from app directroy
file:
path: "{{ versioned_path }}/{{ versioned_list.stdout_lines[item|int] }}"
path: "{{ react_versioned_path }}/{{ versioned_list.stdout_lines[item|int] }}"
state: absent
with_sequence: start=0 end={{ versioned_list.stdout_lines|length - 10 }}
ignore_errors: yes
22 changes: 11 additions & 11 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
- name: Copy environment variables file
template:
src: env.j2
dest: "{{ checkout_path }}/.env"
dest: "{{ react_checkout_path }}/.env"
mode: 0644
owner: "{{ system_user }}"
group: "{{ system_group }}"
owner: "{{ react_system_user }}"
group: "{{ react_system_group }}"

- name: Install Javascript requirements
shell: yarn install
args:
chdir: "{{ checkout_path }}"
chdir: "{{ react_checkout_path }}"
become: True
become_user: "{{ system_user }}"
become_user: "{{ react_system_user }}"

- name: Compile Javascript
shell: yarn build
args:
chdir: "{{ checkout_path }}"
chdir: "{{ react_checkout_path }}"
become: True
become_user: "{{ system_user }}"
become_user: "{{ react_system_user }}"

- name: Make the new codebase current
file:
src: "{{ checkout_path }}"
dest: "{{ codebase_path }}"
src: "{{ react_checkout_path }}"
dest: "{{ react_codebase_path }}"
state: link
force: yes
owner: "{{ system_user }}"
group: "{{ system_group }}"
owner: "{{ react_system_user }}"
group: "{{ react_system_group }}"
60 changes: 30 additions & 30 deletions tasks/install.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
- name: Add system user
user:
name: "{{ system_user }}"
name: "{{ react_system_user }}"
shell: /bin/bash
group: "{{ system_group }}"
group: "{{ react_system_group }}"
append: yes
createhome: yes

Expand All @@ -20,7 +20,7 @@
become_user: root

- name: Download node
shell: curl -sL https://deb.nodesource.com/setup_{{ node_version }} | sudo -E bash -
shell: curl -sL https://deb.nodesource.com/setup_{{ react_node_version }} | sudo -E bash -
become: True
become_user: root
args:
Expand All @@ -32,69 +32,69 @@

- name: Install system-wide dependencies
apt:
name: "{{ system_wide_dependencies }}"
name: "{{ react_system_wide_dependencies }}"
state: latest
update_cache: yes
cache_valid_time: 600

- name: Ensure required directories are present
file:
state: directory
owner: "{{ system_user }}"
owner: "{{ react_system_user }}"
group: www-data
path: "{{ item }}"
when:
- item is defined
- item is not none
with_items:
- "{{ versioned_path }}"
- "{{ checkout_path }}"
- "{{ system_user_home }}/.ssh"
- "{{ react_versioned_path }}"
- "{{ react_checkout_path }}"
- "{{ react_system_user_home }}/.ssh"

- name: Git clone without key
git:
accept_hostkey: "yes"
repo: "{{ git_url }}"
dest: "{{ checkout_path }}"
version: "{{ git_version }}"
repo: "{{ react_git_url }}"
dest: "{{ react_checkout_path }}"
version: "{{ react_git_version }}"
depth: 1
become: True
become_user: "{{ system_user }}"
become_user: "{{ react_system_user }}"
when:
- git_key is not defined or git_key is none
- react_git_key is not defined or react_git_key is none

- name: Copy git key
copy:
content: "{{ git_key }}"
dest: "{{ system_user_home }}/.ssh/{{ git_key_filename }}"
owner: "{{ system_user }}"
content: "{{ react_git_key }}"
dest: "{{ react_system_user_home }}/.ssh/{{ react_git_key_filename }}"
owner: "{{ react_system_user }}"
mode: 0600
no_log: False
when:
- git_key is defined
- git_key is not none
- react_git_key is defined
- react_git_key is not none

- name: Git clone with key
git:
accept_hostkey: "yes"
repo: "{{ git_url }}"
dest: "{{ checkout_path }}"
version: "{{ git_version }}"
repo: "{{ react_git_url }}"
dest: "{{ react_checkout_path }}"
version: "{{ react_git_version }}"
depth: 1
key_file: "{{ system_user_home }}/.ssh/{{ git_key_filename }}"
key_file: "{{ react_system_user_home }}/.ssh/{{ react_git_key_filename }}"
become: True
become_user: "{{ system_user }}"
become_user: "{{ react_system_user }}"
when:
- git_key is defined
- git_key is not none
- react_git_key is defined
- react_git_key is not none

- name: Remove Git Key
file:
state: absent
path: "{{ system_user_home }}/.ssh/{{ git_key_filename }}"
path: "{{ react_system_user_home }}/.ssh/{{ react_git_key_filename }}"
become: True
become_user: "{{ system_user }}"
become_user: "{{ react_system_user }}"
when:
- git_key is defined
- git_key is not none
- remove_git_key == True
- react_git_key is defined
- react_git_key is not none
- react_remove_git_key == True
2 changes: 1 addition & 1 deletion templates/env.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% for k,v in app_settings.items() %}
{% for k,v in react_app_settings.items() %}
{{k}}={{v}}
{% endfor %}
10 changes: 5 additions & 5 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
- name: Test React role
hosts: all
vars:
system_user: "react_example_app"
node_version: 10.x
git_url: "https://github.com/onaio/kaznet-frontend"
git_version: "v1.0.4"
app_settings:
react_system_user: "react_example_app"
react_node_version: 10.x
react_git_url: "https://github.com/onaio/kaznet-frontend"
react_git_version: "v1.0.4"
react_app_settings:
REACT_APP_WEBSITE_NAME: 'Example App'
SOMETHING_ELSE: "you can put anything here"
gather_facts: True
Expand Down

0 comments on commit 6c9d67e

Please sign in to comment.