balena-os / wifi-connect

Easy WiFi setup for Linux devices from your mobile phone or laptop
Apache License 2.0
1.28k stars 360 forks source link

Running in Node.js application #210

Open dakshshah96 opened 6 years ago

dakshshah96 commented 6 years ago

I love working with Resin.io but can't figure out how to integrate resin-wifi-connect in my existing Node.js application.

Currently, my Dockerfile looks like this:

FROM resin/raspberrypi-alpine-node:slim

WORKDIR /usr/src/app

COPY package.json package.json

RUN npm install

COPY . .

CMD ["node", "index.js"]

It'd be great if someone can help me figure out how to run resin-wifi-connect before my Node.js application starts. cc @majorz

Would something like this work?

Dockerfile

FROM resin/raspberrypi-alpine-node:slim

ENV INITSYSTEM on

RUN apt-get update \
  && apt-get install -y dnsmasq wireless-tools \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

RUN curl https://api.github.com/repos/resin-io/resin-wifi-connect/releases/latest -s \
  | grep -hoP 'browser_download_url": "\K.*%%RESIN_ARCH%%\.tar\.gz' \
  | xargs -n1 curl -Ls \
  | tar -xvz -C /usr/src/app/

COPY scripts/start.sh .

CMD ["bash", "start.sh"]

start.sh

#!/usr/bin/env bash

export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket

# Choose a condition for running WiFi Connect according to your use case:

# 1. Is there a default gateway?
# ip route | grep default

# 2. Is there Internet connectivity?
# nmcli -t g | grep full

# 3. Is there Internet connectivity via a google ping?
# wget --spider http://google.com 2>&1

# 4. Is there an active WiFi connection?
iwgetid -r

if [ $? -eq 0 ]; then
    printf 'Skipping WiFi Connect\n'
else
    printf 'Starting WiFi Connect\n'
    ./wifi-connect
fi

# Start your application here.
npm install
node main.js

I don't know how to start my Node.js from the script file.

majorz commented 6 years ago

Alpine relies on musl libc and will not work with WiFi Connect. Also it does not have apt-get. You can use a Docker image based on Debian instead. We provide base images which target Node.js development. What type of device specifically you are using - is it a raspberry pi 3?

dakshshah96 commented 6 years ago

@majorz Yes, I'm using a Raspberry Pi 3 Model B. Is the resin/raspberrypi3-node base image fine?

EDIT: Yes, that image seems to not have a problem with apt-get. However, I get a different error now in the RUN curl ... part while pushing to resin:

[main] Step 5/9 : RUN curl https://api.github.com/repos/resin-io/resin-wifi-connect/releases/latest -s   | grep -hoP 'browser_download_url": "\K.*%%RESIN_ARCH%%\.tar\.gz'   | xargs -n1 curl -Ls   | tar -xvz -C /usr/src/app/
[main]   ---> Running in 30f7bd5cf79d
[main]  curl: no URL specified!
[main]  curl: try 'curl --help' or 'curl --manual' for more information
[main]
[main]
[main]  gzip: stdin: unexpected end of file
[main]
[main]  tar: Child returned status 1
[main]  tar: Error is not recoverable: exiting now
[main]
[main]  Removing intermediate container 30f7bd5cf79d
[main]  The command '/bin/sh -c curl https://api.github.com/repos/resin-io/resin-wifi-connect/releases/latest -s   | grep -hoP 'browser_download_url": "\K.*%%RESIN_ARCH%%\.tar\.gz'   | xargs -n1 curl -Ls   | tar -xvz -C /usr/src/app/' returned a non-zero code: 2

My Dockerfile looks like this:

# base image: for RaspberryPi
FROM resin/raspberrypi3-node

# enable systemd init system
ENV INITSYSTEM on

# set working directory
WORKDIR /usr/src/app

# -- start of resin-wifi-connect section -- #

# use apt-get to install dependencies
RUN apt-get update \
  && apt-get install -y dnsmasq wireless-tools \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

# install resin-wifi-connect
RUN curl https://api.github.com/repos/resin-io/resin-wifi-connect/releases/latest -s \
  | grep -hoP 'browser_download_url": "\K.*%%RESIN_ARCH%%\.tar\.gz' \
  | xargs -n1 curl -Ls \
  | tar -xvz -C /usr/src/app/

# -- end of resin-wifi-connect section -- #

# -- start of node.js application section -- #

COPY package*.json ./

RUN npm install

COPY . .

# -- end of node.js application section -- #

# run start script
CMD ["bash", "start.sh"]
majorz commented 6 years ago

I am away now and could not try running that, but you may go to the releases page, get the link to the armv7hf archive and do ´curl -OL https://...tar.gz´ to download and then ´tar zxf ...tar.gz´ to extract it. I can check it out tomorrow if you cannot resolve it.

dakshshah96 commented 6 years ago

@majorz I did that on my Terminal. curl downloads the tar.gz file properly and I was able to extract the contents as well with tar zxf ... without any errors.

jgentes commented 6 years ago

@dakshshah96 we run our node app in a separate container and it works great. There's no need to map any network connectivity between the containers.

majorz commented 6 years ago

@dakshshah96 I will close this now, but please reopen it or open a new issue in case you encounter difficulties or problems.

dakshshah96 commented 6 years ago

@majorz Still facing the same problem 😞

majorz commented 6 years ago

I will try to find time to look at it tomorrow.

wenzou commented 5 years ago

same problem here