- name: FAIL (STOP INSTALLING) IF nodejs_version is not set to 10.x fail: msg: "Sugarizer install cannot proceeed, as it currently requires Node.js 10.x, and your nodejs_version is set to {{ nodejs_version }}. Please check the value of nodejs_version in /opt/iiab/iiab/vars/default_vars.yml and possibly also /etc/iiab/local_vars.yml" when: sugarizer_install and (nodejs_version != "10.x") # 0. CLEAN UP PRIOR VERSIONS OF SUGARIZER (NEEDS WORK!) # - name: Wipe /library/www/html/sugarizer* if installing sugarizer-1.0 # shell: "rm -rf {{ doc_root }}/sugarizer*" # args: # warn: no # when: sugarizer_dir_version == "sugarizer-1.0" # 1. DOWNLOAD+LINK /opt/iiab/sugarizer - name: Clone llaske/sugarizer ({{ sugarizer_git_version }} branch/version) from GitHub to /opt/iiab/{{ sugarizer_dir_version }} (MAY DOWNLOAD 600+ MB) git: repo: https://github.com/llaske/sugarizer dest: "{{ sugarizer_location }}/{{ sugarizer_dir_version }}" version: "{{ sugarizer_git_version }}" force: yes depth: 1 when: internet_available - name: Create symlink /opt/iiab/sugarizer -> /opt/iiab/{{ sugarizer_dir_version }} file: src: "{{ sugarizer_location }}/{{ sugarizer_dir_version }}" dest: "{{ sugarizer_location }}/sugarizer" state: link # 2. DOWNLOAD+LINK /opt/iiab/sugarizer-server # 2018-07-11: http://download.iiab.io/packages/sugarizer-server-1.0.tar.gz # was flawed, as documented at: # https://github.com/iiab/iiab/pull/814#issuecomment-404211098 # Versions of MongoDB, npm (& Node.js ?) matter! Sugarizer 1.0 Context: # https://github.com/iiab/iiab/issues/798 # Going forward let's "git clone" IIAB's preferred versions, of sugarizer # AND sugarizer-server, as specified in roles/sugarizer/defaults/main.yml # 2018-07-14 BLOAT: git works well BUT even with "depth: 1" # - 229MB is unfort downloaded to /opt/iiab/sugarizer/.git # - 1.4MB is unfort downloaded to /opt/iiab/sugarizer-server/.git # CLARIF: during repeat runs of "./runrole sugarizer", this git sync shows # "changed" (whereas above git sync shows "ok"). Reason: "npm install" # (below) modifies /opt/iiab/sugarizer-server/node_modules - name: Clone llaske/sugarizer-server ({{ sugarizer_server_git_version }} branch/version) from GitHub to /opt/iiab/{{ sugarizer_server_dir_version }} git: repo: https://github.com/llaske/sugarizer-server dest: "{{ sugarizer_location }}/{{ sugarizer_server_dir_version }}" version: "{{ sugarizer_server_git_version }}" force: yes depth: 1 when: internet_available - name: Create symlink /opt/iiab/sugarizer-server -> /opt/iiab/{{ sugarizer_server_dir_version }} file: src: "{{ sugarizer_location }}/{{ sugarizer_server_dir_version }}" dest: "{{ sugarizer_location }}/sugarizer-server" state: link # 3. INSTALL A GOOD VERSION OF Node.js AND npm # 2019-01-16/29: @jvonau's PR #1403 moved install of Node.js (10.x for now) & # npm to roles/nodejs/tasks/main.yml, triggered by roles/sugarizer/meta/main.yml # 4. RUN "npm install" TO POPULATE ~35MB /opt/iiab/sugarizer-server/node_modules # Re-running "npm install" USED TO fail on Raspbian 9 if not other OS's ? # Strategies considered to avoid re-running it: # OLD WAY 1: test & set flag node_modules_exists: True # OLD WAY 2: "creates: ..." checks for non-existence of /opt/iiab/sugarizer-server-1.0/node_modules # OLD WAY 3: set "register: git_sug_server_output" above, then as nec delete /opt/iiab/sugarizer-server-1.0/node_modules "when: git_sug_server_output.changed" and as nec run "npm install" #- name: Check for /opt/iiab/sugarizer-server/node_modules # stat: # path: "{{ sugarizer_location }}/sugarizer-server/node_modules" # register: nmtest # ignore_errors: true # #- name: Set a flag to prevent re-running of "npm install" # set_fact: # node_modules_exists: True # when: nmtest.stat is defined and nmtest.stat.exists # NEW WAY BELOW: run "npm install --allow-root" every time, as modern versions # of npm are incremental, with sanity checks (all 3 may work: but npm 6.2.0 # is better than 5.6.0. which is better than Ubuntu 18.04's 3.5.2). # 2018-07-15: TK Kang & Holt confirmed sudo-driven "npm install" maxes out CPU # for hours, on diff OS's using npm 5.6.0 and 6.2.0. Hours later you may get # error: code EACCES, errno -13 (permission denied), # "Missing write access to /opt/iiab/sugarizer-server-1.0/node_modules" # # SOLUTION: Implement '--allow-root --unsafe-perm=true' below, as is critical # for 1st run of sudo-driven 'npm install' especially: # # ON DEBIAN: npm 5.6.0's --allow-root would be sufficient: causing creation # of /root/.npm cache & lock files to owned by root:root instead of # iiab-admin:iiab-admin...thus permitting it & IIAB installs to complete! # # ON RASPBIAN: npm 5.6.0's --unsafe-perm=true is *required* so that npm # install actually finished (in about 5 minutes). It's possible we should # remove --allow-root in favore of --unsafe-perm=true alone. But this needs # testing on different Linuxes before proceeding. # # CLARIF 1: Something like 'chown -R root:root /root/.npm' would do the job, # but cannot happen synchronously throughout the 1st run of 'npm install' # (when it's needed!) Similar to what --allow-root does on Debian. # # CLARIF 2: Ubuntu 18.04 is currently unaffected due to its ancient # npm 3.5.2, which instead uses /home/iiab-admin/.npm (which remains owned # by iiab-admin:iiab-admin, even with '--allow-root', but thankfully still # gets the job done, for now!) #- name: Create the express framework for Node.js (OS's other than Fedora 18) - name: Run 'npm install --allow-root --unsafe-perm=true' to create /opt/iiab/sugarizer-server/node_modules (CAN TAKE ~5 MINUTES) command: npm install --allow-root --unsafe-perm=true # "command:" a bit safer than "shell:" args: chdir: "{{ sugarizer_location }}/sugarizer-server" #creates: "{{ sugarizer_location }}/sugarizer-server/node_modules" # OLD WAY 2 when: internet_available # "npm install" generally requires Internet access # when: internet_available and git_sug_server_output.changed # OLD WAY 3 # when: internet_available and not is_F18 and not node_modules_exists # OLD WAY 1 #- name: Create the express framework for Node.js (Fedora 18) # shell: npm install # args: # chdir: "{{ sugarizer_location }}/sugarizer/server" # when: internet_available and is_F18 and not node_modules_exists # Add a nodejs express function that appends a prefix to urls - name: Run 'npm install --allow-root --unsafe-perm=true path-prefix-proxy' to create /opt/iiab/sugarizer-server/node_modules/path-prefix-proxy command: npm install --allow-root --unsafe-perm=true path-prefix-proxy args: chdir: "{{ sugarizer_location }}/sugarizer-server" when: internet_available # 5. CONFIG FILES - name: "Install from templates: sugarizer.service (systemd), sugarizer.conf (Apache)" template: src: "{{ item.src }}" dest: "{{ item.dest }}" mode: 0644 owner: root group: root with_items: - { src: 'sugarizer.service', dest: '/etc/systemd/system/sugarizer.service' } - { src: 'sugarizer.conf.j2', dest: '/etc/apache2/sites-available/sugarizer.conf' } #- { src: 'sugarizer.ini.j2', dest: '{{ sugarizer_location }}/sugarizer-server/env/sugarizer.ini' } #- { src: 'sugarizer.js', dest: '{{ sugarizer_location }}/sugarizer-server' } # sugarizer_port is set to 8089 in /opt/iiab/iiab/vars/default_vars.yml # If you need to change this, edit /etc/iiab/local_vars.yml prior to installing # SEE https://github.com/iiab/iiab/pull/1430#issuecomment-459129378 - name: Set Sugarizer port to {{ sugarizer_port }} in /opt/iiab/sugarizer-server/env/sugarizer.ini lineinfile: path: /opt/iiab/sugarizer-server/env/sugarizer.ini regexp: "^port = 8080$" line: "port = {{ sugarizer_port }}" # mongodb_port is set to 27018 in /opt/iiab/iiab/vars/default_vars.yml # If you need to change this, edit /etc/iiab/local_vars.yml prior to installing # SEE https://github.com/iiab/iiab/pull/1430#issuecomment-459129378 - name: Set MongoDB port to {{ mongodb_port }} in /opt/iiab/sugarizer-server/env/sugarizer.ini lineinfile: path: /opt/iiab/sugarizer-server/env/sugarizer.ini regexp: "^port = 27017$" line: "port = {{ mongodb_port }}" # 2-LINE FIX FOR sugarizer.js BY @georgejhunt FOR http://box/sugarizer # SEE https://github.com/iiab/iiab/pull/1430#issuecomment-459129378 # # 2019-02-02: USED TO WORK ON Node.js 8.x BUT SUGARIZER NO LONGER STARTS ON Node.js 10.x # As tested w/ MongoDB 3.0.14 (binaries) on RPi and MongoDB 3.6.3 (apt) on Ubuntu 18.04 # Errors are: "status=255" within "systemctl status sugarizer" # "Cannot find module 'path-prefix-proxy'" within "journalctl -eu sugarizer" # #- name: Customize pathPrefix /sugarizer in /opt/iiab/sugarizer-server/sugarizer.js # lineinfile: # path: /opt/iiab/sugarizer-server/sugarizer.js # regexp: "AUTO-INSERTED BY IIAB" # avoids inserting it twice! # insertbefore: "// Start listening$" # line: | # SEE https://yaml-multiline.info (use |+ to 'keep' newlines at end...though |8 and |+4 "indentation indicators" don't work with Ansible) # // AUTO-INSERTED BY IIAB FOR http://box/sugarizer # var pathPrefix = '/sugarizer'; # app.use(pathPrefix, require('path-prefix-proxy')(pathPrefix)); # # Use this instead, if tabs are truly nec: # # line: "\t// AUTO-INSERTED BY IIAB FOR http://box/sugarizer\n\tvar pathPrefix = '/sugarizer';\n\tapp.use(pathPrefix, require('path-prefix-proxy')(pathPrefix));\n" # Ansible's blockinfile module: # - inserts a mandatory marker line at beginning AND end of the block...ok fine # - doesn't support adding a newline after the block...ugly :( # - doesn't include above lineinfile's "regexp" parameter...and so risks inserting the block repeatedly, on each run :( # # blockinfile: # path: /opt/iiab/sugarizer-server/sugarizer.js # insertbefore: "// Start listening$" # marker: "// {mark} AUTO-INSERTED BY IIAB FOR http://box/sugarizer" # block: | # var pathPrefix = '/sugarizer'; # app.use(pathPrefix, require('path-prefix-proxy')(pathPrefix)); # # Use this instead, if tabs are truly nec: # # block: "\tvar pathPrefix = '/sugarizer';\n\tapp.use(pathPrefix, require('path-prefix-proxy')(pathPrefix));" - name: Create symlink sugarizer.conf from sites-enabled to sites-available, for short URL http://box/sugarizer (debuntu) file: src: /etc/apache2/sites-available/sugarizer.conf path: /etc/apache2/sites-enabled/sugarizer.conf state: link when: sugarizer_enabled and is_debuntu - name: "Remove symlink /etc/apache2/sites-enabled/sugarizer.conf, if sugarizer_enabled: False (debuntu)" file: path: /etc/apache2/sites-enabled/sugarizer.conf state: absent when: not sugarizer_enabled and is_debuntu # 6. RESTART/STOP SYSTEMD SERVICE - name: Enable & Restart systemd service if sugarizer_enabled, with "systemctl daemon-reload" (in case mongodb.service changed?) systemd: name: sugarizer daemon_reload: yes enabled: yes state: restarted when: sugarizer_enabled - name: "Disable systemd service, if sugarizer_enabled: False" systemd: name: sugarizer enabled: no state: stopped when: not sugarizer_enabled - name: Restart Apache ({{ apache_service }}) to enable/disable http://box/sugarizer (not just http://box:{{ sugarizer_port }}) systemd: name: "{{ apache_service }}" # httpd or apache2 state: restarted #when: sugarizer_enabled #- name: Enable services (all OS's) # service: # name: "{{ item.name }}" # enabled: yes # state: restarted # with_items: ## - { name: mongodb } # 2018-07-14: NICE TRY, but still doesn't bring http://box:8089 to life reliably, as a reboot usually does! (Is a "systemctl daemon-reload" or some such nec?) # - { name: sugarizer } # when: sugarizer_enabled #- name: Disable service (all OS's) # service: # name: sugarizer # enabled: no # state: stopped # when: not sugarizer_enabled - name: Add 'sugarizer' variable values to {{ iiab_ini_file }} ini_file: path: "{{ iiab_ini_file }}" section: sugarizer option: "{{ item.option }}" value: "{{ item.value }}" with_items: - option: name value: Sugarizer - option: description value: '"The Sugar Learning Platform began with the famous One Laptop Per Child project, written in Python. Sugarizer is the new HTML/JavaScript implementation of Sugar, usable in most all browsers."' - option: sugarizer_enabled value: "{{ sugarizer_enabled }}"