From b6aabcca6443d66c2b3de7cc646aab51e3331779 Mon Sep 17 00:00:00 2001 From: Marius Ciotlos Date: Fri, 15 Jun 2018 13:54:54 +0300 Subject: [PATCH 1/4] Add support for version numbers and hash --- .gitignore | 3 ++- lib/controller.js | 11 ++++++++++- tag.sh | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100755 tag.sh diff --git a/.gitignore b/.gitignore index 6aea616b..dc70a7ef 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,5 @@ typings/ # data data/database.db data/config.json -data/log*.txt \ No newline at end of file +data/log*.txt +data/hash.json \ No newline at end of file diff --git a/lib/controller.js b/lib/controller.js index 715f7e45..3309efa3 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -5,7 +5,15 @@ const settings = require('./util/settings'); const zigbeeShepherdConverters = require('zigbee-shepherd-converters'); const homeassistant = require('./homeassistant'); const objectAssignDeep = require(`object-assign-deep`); - +const data = require('./util/data'); +const pjson = require('../package.json'); +let version = `${pjson.version}`; +try { + const hjson = require(data.joinPath('hash.json')).id; + version = version + `:${hjson}`; +} catch (error) { + logger.debug('Error parsing git hash version', error); +} const mqttConfigRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/\\w+`, 'g'); const mqttDeviceRegex = new RegExp(`${settings.get().mqtt.base_topic}/[\\w\\s\\d]+/set`, 'g'); const mqttDevicePrefixRegex = new RegExp(`${settings.get().mqtt.base_topic}/[\\w\\s\\d]+/[\\w\\s\\d]+/set`, 'g'); @@ -39,6 +47,7 @@ class Controller { } start() { + logger.info(`Starting ... ${version}`); this.zigbee.start(this.handleZigbeeMessage, (error) => { if (error) { logger.error('Failed to start'); diff --git a/tag.sh b/tag.sh new file mode 100755 index 00000000..fec26218 --- /dev/null +++ b/tag.sh @@ -0,0 +1,3 @@ +#!/bin/sh +echo "{\"id\": \"$(git rev-parse --short HEAD)\"}" > data/hash.json +echo "Hash version '$(git rev-parse --short HEAD)' generated successfully" \ No newline at end of file From 96191a283604b82011d9f8a272e4fd1a5496f638 Mon Sep 17 00:00:00 2001 From: Koenkk Date: Fri, 15 Jun 2018 17:48:10 +0200 Subject: [PATCH 2/4] Update log version on startup --- .dockerignore | 1 - .gitignore | 1 - docker/Dockerfile.amd64 | 2 +- docker/Dockerfile.arm32v6 | 2 +- docker/Dockerfile.arm64v8 | 2 +- lib/controller.js | 99 +++++++++++++++++++++------------------ npm-shrinkwrap.json | 19 +++++--- package.json | 3 +- tag.sh | 3 -- 9 files changed, 71 insertions(+), 61 deletions(-) delete mode 100755 tag.sh diff --git a/.dockerignore b/.dockerignore index 122d23db..ff2be3fa 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,5 @@ node_modules .travis* -.git .gitignore *.png firmware diff --git a/.gitignore b/.gitignore index dc70a7ef..24eb4d12 100644 --- a/.gitignore +++ b/.gitignore @@ -61,4 +61,3 @@ typings/ data/database.db data/config.json data/log*.txt -data/hash.json \ No newline at end of file diff --git a/docker/Dockerfile.amd64 b/docker/Dockerfile.amd64 index 9c21dd63..8ee70a5e 100644 --- a/docker/Dockerfile.amd64 +++ b/docker/Dockerfile.amd64 @@ -10,7 +10,7 @@ WORKDIR /app # Install dependencies RUN apk add --update --no-cache make gcc g++ python linux-headers udev nodejs git && \ npm install --unsafe-perm && \ - apk del make gcc g++ python linux-headers udev git + apk del make gcc g++ python linux-headers udev # Entrypoint ENTRYPOINT ["./run.sh"] diff --git a/docker/Dockerfile.arm32v6 b/docker/Dockerfile.arm32v6 index d1dc6795..ee8a7fdc 100644 --- a/docker/Dockerfile.arm32v6 +++ b/docker/Dockerfile.arm32v6 @@ -12,7 +12,7 @@ RUN [ "qemu-arm-static", "/bin/sh", "-c", \ cp /app/docker/run.sh /app && chmod +x /app/run.sh && \ apk add --update --no-cache make gcc g++ python linux-headers udev nodejs git && \ npm install --unsafe-perm && \ - apk del make gcc g++ python linux-headers udev git"] + apk del make gcc g++ python linux-headers udev"] # Entrypoint ENTRYPOINT ["./run.sh"] \ No newline at end of file diff --git a/docker/Dockerfile.arm64v8 b/docker/Dockerfile.arm64v8 index cc9a8128..d9e207ad 100644 --- a/docker/Dockerfile.arm64v8 +++ b/docker/Dockerfile.arm64v8 @@ -12,7 +12,7 @@ RUN [ "qemu-aarch64-static", "/bin/sh", "-c", \ cp /app/docker/run.sh /app && chmod +x /app/run.sh && \ apk add --update --no-cache make gcc g++ python linux-headers udev nodejs git && \ npm install --unsafe-perm && \ - apk del make gcc g++ python linux-headers udev git"] + apk del make gcc g++ python linux-headers udev"] # Entrypoint ENTRYPOINT ["./run.sh"] \ No newline at end of file diff --git a/lib/controller.js b/lib/controller.js index 3309efa3..fbc93f1d 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -5,15 +5,7 @@ const settings = require('./util/settings'); const zigbeeShepherdConverters = require('zigbee-shepherd-converters'); const homeassistant = require('./homeassistant'); const objectAssignDeep = require(`object-assign-deep`); -const data = require('./util/data'); -const pjson = require('../package.json'); -let version = `${pjson.version}`; -try { - const hjson = require(data.joinPath('hash.json')).id; - version = version + `:${hjson}`; -} catch (error) { - logger.debug('Error parsing git hash version', error); -} + const mqttConfigRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/\\w+`, 'g'); const mqttDeviceRegex = new RegExp(`${settings.get().mqtt.base_topic}/[\\w\\s\\d]+/set`, 'g'); const mqttDevicePrefixRegex = new RegExp(`${settings.get().mqtt.base_topic}/[\\w\\s\\d]+/[\\w\\s\\d]+/set`, 'g'); @@ -47,44 +39,45 @@ class Controller { } start() { - logger.info(`Starting ... ${version}`); - this.zigbee.start(this.handleZigbeeMessage, (error) => { - if (error) { - logger.error('Failed to start'); - } else { - // Log zigbee clients on startup and configure. - const devices = this.zigbee.getAllClients(); - logger.info(`Currently ${devices.length} devices are joined:`); - devices.forEach((device) => { - logger.info(this.getDeviceStartupLogMessage(device)); - this.configureDevice(device); - }); + this.startupLogVersion(() => { + this.zigbee.start(this.handleZigbeeMessage, (error) => { + if (error) { + logger.error('Failed to start'); + } else { + // Log zigbee clients on startup and configure. + const devices = this.zigbee.getAllClients(); + logger.info(`Currently ${devices.length} devices are joined:`); + devices.forEach((device) => { + logger.info(this.getDeviceStartupLogMessage(device)); + this.configureDevice(device); + }); - // Enable zigbee join. - if (settings.get().permit_join) { - logger.warn('`permit_join` set to `true` in configuration.yaml.'); - logger.warn('Allowing new devices to join.'); - logger.warn('Set `permit_join` to `false` once you joined all devices.'); - this.zigbee.permitJoin(true); + // Enable zigbee join. + if (settings.get().permit_join) { + logger.warn('`permit_join` set to `true` in configuration.yaml.'); + logger.warn('Allowing new devices to join.'); + logger.warn('Set `permit_join` to `false` once you joined all devices.'); + this.zigbee.permitJoin(true); + } + + // Start timers. + this.pollTimer(true); + this.softResetTimeout(true); + + // Connect to MQTT broker + const subscriptions = [ + `${settings.get().mqtt.base_topic}/+/set`, + `${settings.get().mqtt.base_topic}/+/+/set`, + `${settings.get().mqtt.base_topic}/bridge/config/+`, + ]; + + if (settings.get().homeassistant) { + subscriptions.push('hass/status'); + } + + this.mqtt.connect(this.handleMQTTMessage, subscriptions, () => this.handleMQTTConnected()); } - - // Start timers. - this.pollTimer(true); - this.softResetTimeout(true); - - // Connect to MQTT broker - const subscriptions = [ - `${settings.get().mqtt.base_topic}/+/set`, - `${settings.get().mqtt.base_topic}/+/+/set`, - `${settings.get().mqtt.base_topic}/bridge/config/+`, - ]; - - if (settings.get().homeassistant) { - subscriptions.push('hass/status'); - } - - this.mqtt.connect(this.handleMQTTMessage, subscriptions, () => this.handleMQTTConnected()); - } + }); }); } @@ -529,6 +522,22 @@ class Controller { this.mqtt.publish(deviceSettings.friendly_name, JSON.stringify(payload), options); } + + startupLogVersion(callback) { + const git = require('git-last-commit'); + const packageJSON = require('../package.json'); + const version = packageJSON.version; + + git.getLastCommit((err, commit) => { + if (err) { + logger.info(`Starting zigbee2mqtt version ${version}`); + } else { + logger.info(`Starting zigbee2mqtt version ${version} (commit ${commit.shortHash})`); + } + + callback(); + }); + } } module.exports = Controller; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 9f16ef58..c00a71e6 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "acorn": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.6.2.tgz", - "integrity": "sha512-zUzo1E5dI2Ey8+82egfnttyMlMZ2y0D8xOCO3PNPPlYXpl8NZvF6Qk9L9BEtJs+43FqEmfBViDqc5d1ckRDguw==", + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.7.1.tgz", + "integrity": "sha512-d+nbxBUGKg7Arpsvbnlq61mc12ek3EY8EQldM3GPAhWJ1UVxC6TDGbIvUMNU6obBX3i1+ptCIzV4vq0gFPEGVQ==", "dev": true }, "acorn-jsx": { @@ -590,7 +590,7 @@ "integrity": "sha512-yAcIQxtmMiB/jL32dzEp2enBeidsB7xWPLNiw3IIkpVds1P+h7qF9YwJq1yUNzp2OKXgAprs4F61ih66UsoD1A==", "dev": true, "requires": { - "acorn": "5.6.2", + "acorn": "5.7.1", "acorn-jsx": "3.0.1" } }, @@ -720,6 +720,11 @@ "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "dev": true }, + "git-last-commit": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/git-last-commit/-/git-last-commit-0.3.0.tgz", + "integrity": "sha1-zHcBrcYpt0f9OzOuXddTp0as6d8=" + }, "glob": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", @@ -2782,9 +2787,9 @@ } }, "zigbee-shepherd-converters": { - "version": "2.0.7", - "resolved": "https://registry.npmjs.org/zigbee-shepherd-converters/-/zigbee-shepherd-converters-2.0.7.tgz", - "integrity": "sha512-yBLpHohmjw192Sp5rjX8pozLcjcnhy5AmVIob+CI0+AgGCHEpzGBjc9JvxLyH/N/XVOjMiyVK6wOb5B/Ua1xaw==", + "version": "2.0.8", + "resolved": "https://registry.npmjs.org/zigbee-shepherd-converters/-/zigbee-shepherd-converters-2.0.8.tgz", + "integrity": "sha512-UAtzGF5HZpcfvclmD1fxFkNke0EEOlXMO1VZB8z+FzkuexW6ILVG+7sbys4XIxcrmPuD6/yRGAPsWDqmLITrOg==", "requires": { "debounce": "1.1.0" }, diff --git a/package.json b/package.json index a334b8be..f7b74e83 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "zigbee-shepherd-converters": "*", "json2yaml": "*", "zcl-packet": "git+https://github.com/Koenkk/zcl-packet.git", - "js-yaml": "*" + "js-yaml": "*", + "git-last-commit": "*" }, "devDependencies": { "eslint": "*", diff --git a/tag.sh b/tag.sh deleted file mode 100755 index fec26218..00000000 --- a/tag.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -echo "{\"id\": \"$(git rev-parse --short HEAD)\"}" > data/hash.json -echo "Hash version '$(git rev-parse --short HEAD)' generated successfully" \ No newline at end of file From d69111f577862e5233f728cb3cef940b89696102 Mon Sep 17 00:00:00 2001 From: Koenkk Date: Sat, 16 Jun 2018 00:10:08 +0200 Subject: [PATCH 3/4] Add .hash.json for Docker. --- .dockerignore | 1 + .gitignore | 2 +- .travis/docker.sh | 2 +- docker/Dockerfile.amd64 | 6 +++++- docker/Dockerfile.arm32v6 | 6 +++++- docker/Dockerfile.arm64v8 | 6 +++++- lib/controller.js | 12 ++++++++++-- 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/.dockerignore b/.dockerignore index ff2be3fa..122d23db 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,6 @@ node_modules .travis* +.git .gitignore *.png firmware diff --git a/.gitignore b/.gitignore index 24eb4d12..6aea616b 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,4 @@ typings/ # data data/database.db data/config.json -data/log*.txt +data/log*.txt \ No newline at end of file diff --git a/.travis/docker.sh b/.travis/docker.sh index c2bc0c46..49bec586 100755 --- a/.travis/docker.sh +++ b/.travis/docker.sh @@ -5,7 +5,7 @@ login() { } build_and_push() { - docker build -t $DOCKER_USERNAME/zigbee2mqtt:$1 -f $2 . + docker build --build-arg COMMIT=$(git rev-parse --short HEAD) -t $DOCKER_USERNAME/zigbee2mqtt:$1 -f $2 . docker push $DOCKER_USERNAME/zigbee2mqtt:$1 } diff --git a/docker/Dockerfile.amd64 b/docker/Dockerfile.amd64 index 8ee70a5e..585b4517 100644 --- a/docker/Dockerfile.amd64 +++ b/docker/Dockerfile.amd64 @@ -7,10 +7,14 @@ RUN cp /app/docker/run.sh /app RUN chmod +x /app/run.sh WORKDIR /app +# Write .hash.json +ARG COMMIT +RUN echo "{\"hash\": \"$COMMIT\"}" > .hash.json + # Install dependencies RUN apk add --update --no-cache make gcc g++ python linux-headers udev nodejs git && \ npm install --unsafe-perm && \ - apk del make gcc g++ python linux-headers udev + apk del make gcc g++ python linux-headers udev git # Entrypoint ENTRYPOINT ["./run.sh"] diff --git a/docker/Dockerfile.arm32v6 b/docker/Dockerfile.arm32v6 index ee8a7fdc..c8260dfd 100644 --- a/docker/Dockerfile.arm32v6 +++ b/docker/Dockerfile.arm32v6 @@ -5,6 +5,10 @@ ENV QEMU_EXECVE 1 COPY docker/qemu-arm-static /usr/bin WORKDIR /app +# Write .hash.json +ARG COMMIT +RUN echo "{\"hash\": \"$COMMIT\"}" > .hash.json + # Copy files & install dependencies ADD . /app RUN [ "qemu-arm-static", "/bin/sh", "-c", \ @@ -12,7 +16,7 @@ RUN [ "qemu-arm-static", "/bin/sh", "-c", \ cp /app/docker/run.sh /app && chmod +x /app/run.sh && \ apk add --update --no-cache make gcc g++ python linux-headers udev nodejs git && \ npm install --unsafe-perm && \ - apk del make gcc g++ python linux-headers udev"] + apk del make gcc g++ python linux-headers udev git"] # Entrypoint ENTRYPOINT ["./run.sh"] \ No newline at end of file diff --git a/docker/Dockerfile.arm64v8 b/docker/Dockerfile.arm64v8 index d9e207ad..bd519d54 100644 --- a/docker/Dockerfile.arm64v8 +++ b/docker/Dockerfile.arm64v8 @@ -5,6 +5,10 @@ ENV QEMU_EXECVE 1 COPY docker/qemu-aarch64-static /usr/bin WORKDIR /app +# Write .hash.json +ARG COMMIT +RUN echo "{\"hash\": \"$COMMIT\"}" > .hash.json + # Copy files & install dependencies ADD . /app RUN [ "qemu-aarch64-static", "/bin/sh", "-c", \ @@ -12,7 +16,7 @@ RUN [ "qemu-aarch64-static", "/bin/sh", "-c", \ cp /app/docker/run.sh /app && chmod +x /app/run.sh && \ apk add --update --no-cache make gcc g++ python linux-headers udev nodejs git && \ npm install --unsafe-perm && \ - apk del make gcc g++ python linux-headers udev"] + apk del make gcc g++ python linux-headers udev git"] # Entrypoint ENTRYPOINT ["./run.sh"] \ No newline at end of file diff --git a/lib/controller.js b/lib/controller.js index fbc93f1d..4ca3666a 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -529,12 +529,20 @@ class Controller { const version = packageJSON.version; git.getLastCommit((err, commit) => { + let commitHash = null; + if (err) { - logger.info(`Starting zigbee2mqtt version ${version}`); + try { + commitHash = require('../.hash.json').hash; + } catch (error) { + commitHash = 'unknown'; + } } else { - logger.info(`Starting zigbee2mqtt version ${version} (commit ${commit.shortHash})`); + commitHash = commit.shortHash; } + logger.info(`Starting zigbee2mqtt version ${version} (commit #${commitHash})`); + callback(); }); } From deab0004f2aa150f10cb935e2226fb77125b54a1 Mon Sep 17 00:00:00 2001 From: Koenkk Date: Sat, 16 Jun 2018 00:12:38 +0200 Subject: [PATCH 4/4] Set version to 0.0.0 --- npm-shrinkwrap.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index c00a71e6..009ed9cd 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "zigbee2mqtt", - "version": "0.1.0", + "version": "0.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f7b74e83..27561a32 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zigbee2mqtt", - "version": "0.1.0", + "version": "0.0.0", "description": "Zigbee to MQTT bridge using zigbee-shepherd", "main": "index.js", "repository": {