Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ANXS/nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Freedman committed Feb 7, 2015
2 parents 8ebcc0b + 3cf074e commit 7e2a7b9
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 35 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ Ansible role for installing nodejs, from package or by building it from source.
#### Variables

```yaml
nodejs_install_method: "source" # "package" or "source"
nodejs_version: "0.10.26" # nodejs version to install.
nodejs_install_method: "source" # "package" or "source" or "binary"
```
For "unstable" node versions (0.11), you may wish to use "binary" if you don't want to compile from source:
```yaml
nodejs_version: "0.11.13" # nodejs version to install.
nodejs_install_method: "binary" # "source" or "binary" (package is not available for 0.11)
```
#### Thanks to
- [Nicolas Bazire](https://github.com/nicbaz)
- [Luís Couto](https://github.com/Couto)
- [Stephan Hochhaus](https://github.com/yauh)
- [Nathan Palmer](https://github.com/nathanpalmer)
- [Manuel Tiago Pereira](https://github.com/mtpereira)
- [Umair Siddique](https://github.com/umairsiddique)
#### License
Expand Down
14 changes: 10 additions & 4 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# file: nodejs/defaults/main.yml

nodejs_install_method: "source"
nodejs_version: "0.10.26"
nodejs_url: "http://nodejs.org/dist/v{{nodejs_version}}/node-v{{nodejs_version}}.tar.gz"
nodejs_prefix: "/usr/local/nodejs/node-v{{nodejs_version}}"
nodejs_install_method : "source"
nodejs_version : "0.10.26"

nodejs_directory : "/usr/local/nodejs"
nodejs_source_url : "http://nodejs.org/dist/v{{nodejs_version}}/node-v{{nodejs_version}}.tar.gz"
nodejs_source_prefix : "{{nodejs_directory}}/node-v{{nodejs_version}}"

# possible achitectures: darwin-x64, darwin-x86, linux-x64, linux-x86, sunos-x64, sunos-x86
nodejs_binary_url : "http://nodejs.org/dist/v{{nodejs_version}}/node-v{{nodejs_version}}-{{ansible_system | lower}}-x{{ansible_userspace_bits |replace('32', '86')}}.tar.gz"
nodejs_binary_prefix : "{{nodejs_directory}}/node-v{{nodejs_version}}-{{ansible_system | lower}}-x{{ansible_userspace_bits |replace('32', '86')}}"
2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
galaxy_info:
author: pjan vandaele
company: ANXS
description: "Install nodejs, from package or build from source"
description: "Install nodejs, from package, build from source or download binary tarball"
min_ansible_version: 1.4
license: MIT
platforms:
Expand Down
29 changes: 29 additions & 0 deletions tasks/binary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# file: nodejs/tasks/binary.yml

- name: node.js | binary | Make sure that the directory to hold the node.js binaries exists
file:
path: "{{nodejs_binary_prefix}}"
state: directory
recurse: yes
mode: 0755

- name: node.js | binary | Download the node.js binary for your distribution
get_url:
url: "{{nodejs_binary_url}}"
dest: "/tmp/node-v{{nodejs_version}}.tar.gz"

- name: node.js | binary | Unpack the node.js package
unarchive:
src : "/tmp/node-v{{nodejs_version}}.tar.gz"
dest : "/usr/local/nodejs"
copy : no

- name: node.js | binary | Update the symbolic link to the node.js install
file:
path: "{{nodejs_directory}}/default"
src: "{{nodejs_binary_prefix}}"
state: link
force: yes

- include: update_path.yml

5 changes: 4 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
ignore_errors: yes
register: node_version
changed_when: node_version.rc != 0 or node_version.stdout != "v{{ nodejs_version }}"
when: nodejs_install_method == "source"
when: nodejs_install_method == "source" or nodejs_install_method == "binary"

- include: source.yml
when: nodejs_install_method == "source" and node_version.changed

- include: binary.yml
when: nodejs_install_method == "binary" and node_version.changed
40 changes: 12 additions & 28 deletions tasks/source.yml
Original file line number Diff line number Diff line change
@@ -1,52 +1,36 @@
# file: nodejs/tasks/source.yml

- name: node.js | Make sure that the directory to hold the node.js binaries exists
- name: node.js | source | Make sure that the directory to hold the node.js binaries exists
file:
path: /usr/local/nodejs
path: "{{nodejs_directory}}"
state: directory
recurse: yes
mode: 0755

- name: node.js | Download the node.js source
- name: node.js | source | Download the node.js source
get_url:
url: "{{nodejs_url}}"
url: "{{nodejs_source_url}}"
dest: "/tmp/node-v{{nodejs_version}}.tar.gz"

- name: node.js | Unpack the node.js source
- name: node.js | source | Unpack the node.js source
shell: tar -xvzf /tmp/node-v{{nodejs_version}}.tar.gz chdir=/tmp creates=/tmp/node-v{{nodejs_version}}

- name: node.js | Get the number of processors
- name: node.js | source |Get the number of processors
command: nproc
register: cpu_count

- name: node.js | Build node.js from source
- name: node.js | source | Build node.js from source
shell: >
cd /tmp/node-v{{nodejs_version}} &&
./configure --prefix={{nodejs_prefix}} &&
./configure --prefix={{nodejs_source_prefix}} &&
make -j {{cpu_count.stdout}} &&
sudo make install
- name: node.js | Update the symbolic link to the node.js install
- name: node.js | source | Update the symbolic link to the node.js install
file:
path: /usr/local/nodejs/default
src: "{{nodejs_prefix}}"
path: "{{nodejs_directory}}/default"
src: "{{nodejs_source_prefix}}"
state: link
force: yes

- name: node.js | Add the node.js binary to the system path
lineinfile: "dest={{item.dest}} regexp={{item.regexp}} line={{item.line}} state={{item.state}}"
with_items:
- { dest: "/etc/profile", regexp: "^NODE_HOME=/usr/local/nodejs/default", line: "NODE_HOME=/usr/local/nodejs/default", state: "present" }
- { dest: "/etc/profile", regexp: "^PATH=.*NODE_HOME.*", line: "PATH=$PATH:$NODE_HOME/bin", state: "present" }

- name: node.js | Inform the system where node is located and set this one as the default
shell: "{{item}}"
with_items:
- 'update-alternatives --install "/usr/bin/node" "node" "/usr/local/nodejs/default/bin/node" 1'
- 'update-alternatives --set node /usr/local/nodejs/default/bin/node'

- name: node.js | Inform the system where npm is located and set this one as the default
shell: "{{item}}"
with_items:
- 'update-alternatives --install "/usr/bin/npm" "npm" "/usr/local/nodejs/default/bin/npm" 1'
- 'update-alternatives --set npm /usr/local/nodejs/default/bin/npm'
- include: update_path.yml
20 changes: 20 additions & 0 deletions tasks/update_path.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# file: nodejs/tasks/update_path.yml

- name: node.js | update_path | Add the node.js binary to the system path
lineinfile: "dest={{item.dest}} regexp={{item.regexp}} line={{item.line}} state={{item.state}}"
with_items:
- { dest: "/etc/profile", regexp: "^NODE_HOME=/usr/local/nodejs/default", line: "NODE_HOME=/usr/local/nodejs/default", state: "present" }
- { dest: "/etc/profile", regexp: "^PATH=.*NODE_HOME.*", line: "PATH=$PATH:$NODE_HOME/bin", state: "present" }

- name: node.js | update_path | Inform the system where node is located and set this one as the default
shell: "{{item}}"
with_items:
- 'update-alternatives --install "/usr/bin/node" "node" "/usr/local/nodejs/default/bin/node" 1'
- 'update-alternatives --set node /usr/local/nodejs/default/bin/node'

- name: node.js | update_path | Inform the system where npm is located and set this one as the default
shell: "{{item}}"
with_items:
- 'update-alternatives --install "/usr/bin/npm" "npm" "/usr/local/nodejs/default/bin/npm" 1'
- 'update-alternatives --set npm /usr/local/nodejs/default/bin/npm'

2 changes: 2 additions & 0 deletions test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
- hosts: all
sudo: True
vars_files:
- 'defaults/main.yml'
tasks:
Expand Down

0 comments on commit 7e2a7b9

Please sign in to comment.