Skip to content

Commit

Permalink
Merge pull request #263 from AnalogJ/beta
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ committed May 28, 2022
2 parents 989fbc2 + 4aa1485 commit b208493
Show file tree
Hide file tree
Showing 108 changed files with 15,471 additions and 178 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/docker-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
Expand Down Expand Up @@ -95,7 +97,8 @@ jobs:
type=ref,enable=true,event=branch,suffix=-web
type=ref,enable=true,event=tag,suffix=-web
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: "Generate frontend version information"
run: "cd webapp/frontend && ./git.version.sh"
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
Expand Down Expand Up @@ -141,7 +144,8 @@ jobs:
type=ref,enable=true,event=branch,suffix=-omnibus
type=ref,enable=true,event=tag,suffix=-omnibus
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: "Generate frontend version information"
run: "cd webapp/frontend && ./git.version.sh"
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release-frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ jobs:
uses: actions/checkout@v2
with:
ref: ${{github.event.release.tag_name}}
- name: "Generate frontend version information"
run: "cd webapp/frontend && ./git.version.sh"
- name: Build Frontend
run: |
cd webapp/frontend
npm install -g @angular/cli@9.1.4
npm install
mkdir -p dist
ng build --output-path=dist --deploy-url="/web/" --base-href="/web/" --prod
npm run build:prod -- --output-path=dist
tar -czf scrutiny-web-frontend.tar.gz dist
- name: Upload Frontend Asset
id: upload-release-asset3
Expand Down
200 changes: 119 additions & 81 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,109 @@
# Contributing

There are multiple ways to develop on the scrutiny codebase locally. The two most popular are:
- Docker Development Container - only requires docker
- Run Components Locally - requires smartmontools, golang & nodejs installed locally

## Docker Development
```
docker build -f docker/Dockerfile . -t chcr.io/analogj/scrutiny:master-omnibus
docker run -it --rm -p 8080:8080 \
-v /run/udev:/run/udev:ro \
--cap-add SYS_RAWIO \
--device=/dev/sda \
--device=/dev/sdb \
ghcr.io/analogj/scrutiny:master-omnibus
/opt/scrutiny/bin/scrutiny-collector-metrics run
```


## Local Development

### Frontend
The frontend is written in Angular.
If you're working on the frontend and can use mocked data rather than a real backend, you can use
```
cd webapp/frontend
npm install
ng serve --deploy-url="/web/" --base-href="/web/"
```

However, if you need to also run the backend, and use real data, you'll need to run the following command:
```
cd webapp/frontend && ng build --watch --output-path=../../dist --prod
```

> Note: if you do not add `--prod` flag, app will display mocked data for api calls.
### Backend

If you're using the `ng build` command above to generate your frontend, you'll need to create a custom config file and
override the `web.src.frontend.path` value.

```
# config file for local development. store as scrutiny.yaml
version: 1
web:
listen:
port: 8080
host: 0.0.0.0
database:
# can also set absolute path here
location: ./scrutiny.db
src:
frontend:
path: ./dist
influxdb:
retention_policy: false
log:
file: 'web.log' #absolute or relative paths allowed, eg. web.log
level: DEBUG
```

Once you've created a config file, you can pass it to the scrutiny binary during startup.

```
go run webapp/backend/cmd/scrutiny/scrutiny.go start --config ./scrutiny.yaml
```

Now visit http://localhost:8080
The Scrutiny repository is a [monorepo](https://en.wikipedia.org/wiki/Monorepo) containing source code for:
- Scrutiny Backend Server (API)
- Scrutiny Frontend Angular SPA
- S.M.A.R.T Collector

Depending on the functionality you are adding, you may need to setup a development environment for 1 or more projects.

# Modifying the Scrutiny Backend Server (API)

1. install the [Go runtime](https://go.dev/doc/install) (v1.17+)
2. download the `scrutiny-web-frontend.tar.gz` for the [latest release](https://github.com/AnalogJ/scrutiny/releases/latest). Extract to a folder named `dist`
3. create a `scrutiny.yaml` config file
```yaml
# config file for local development. store as scrutiny.yaml
version: 1

web:
listen:
port: 8080
host: 0.0.0.0
database:
# can also set absolute path here
location: ./scrutiny.db
src:
frontend:
path: ./dist
influxdb:
retention_policy: false

log:
file: 'web.log' #absolute or relative paths allowed, eg. web.log
level: DEBUG

```
4. start a InfluxDB docker container.
```bash
docker run -p 8086:8086 --rm influxdb:2.2
```
5. start the scrutiny web server
```bash
go mod vendor
go run webapp/backend/cmd/scrutiny/scrutiny.go start --config ./scrutiny.yaml
```
6. open your browser to [http://localhost:8080/web](http://localhost:8080/web)

# Modifying the Scrutiny Frontend Angular SPA

The frontend is written in Angular. If you're working on the frontend and can use mocked data rather than a real backend, you can follow the instructions below:

1. install [NodeJS](https://nodejs.org/en/download/)
2. start the Angular Frontend Application
```bash
cd webapp/frontend
npm install
npm run start -- --deploy-url="/web/" --base-href="/web/" --port 4200
```
3. open your browser and visit [http://localhost:4200/web](http://localhost:4200/web)

# Modifying both Scrutiny Backend and Frontend Applications
If you're developing a feature that requires changes to the backend and the frontend, or a frontend feature that requires real data,
you'll need to follow the steps below:

1. install the [Go runtime](https://go.dev/doc/install) (v1.17+)
2. install [NodeJS](https://nodejs.org/en/download/)
3. create a `scrutiny.yaml` config file
```yaml
# config file for local development. store as scrutiny.yaml
version: 1

web:
listen:
port: 8080
host: 0.0.0.0
database:
# can also set absolute path here
location: ./scrutiny.db
src:
frontend:
path: ./dist
influxdb:
retention_policy: false

log:
file: 'web.log' #absolute or relative paths allowed, eg. web.log
level: DEBUG

```
4. start a InfluxDB docker container.
```bash
docker run -p 8086:8086 --rm influxdb:2.2
```
5. build the Angular Frontend Application
```bash
cd webapp/frontend
npm install
npm run build:prod -- --watch --output-path=../../dist
# Note: if you do not add `--prod` flag, app will display mocked data for api calls.
```
6. start the scrutiny web server
```bash
go mod vendor
go run webapp/backend/cmd/scrutiny/scrutiny.go start --config ./scrutiny.yaml
```
7. open your browser to [http://localhost:8080/web](http://localhost:8080/web)


If you'd like to populate the database with some test data, you can run the following commands:
Expand All @@ -82,15 +116,6 @@ If you'd like to populate the database with some test data, you can run the fol
docker run -p 8086:8086 --rm influxdb:2.2
docker run --rm -p 8086:8086 \
-e DOCKER_INFLUXDB_INIT_MODE=setup \
-e DOCKER_INFLUXDB_INIT_USERNAME=admin \
-e DOCKER_INFLUXDB_INIT_PASSWORD=password12345 \
-e DOCKER_INFLUXDB_INIT_ORG=scrutiny \
-e DOCKER_INFLUXDB_INIT_BUCKET=metrics \
influxdb:2.2
# curl -X POST -H "Content-Type: application/json" -d @webapp/backend/pkg/web/testdata/register-devices-req.json localhost:8080/api/devices/register
# curl -X POST -H "Content-Type: application/json" -d @webapp/backend/pkg/models/testdata/smart-ata.json localhost:8080/api/device/0x5000cca264eb01d7/smart
# curl -X POST -H "Content-Type: application/json" -d @webapp/backend/pkg/models/testdata/smart-ata-date.json localhost:8080/api/device/0x5000cca264eb01d7/smart
Expand All @@ -105,14 +130,14 @@ curl localhost:8080/api/summary
```

### Collector
# Modifying the Collector
```
brew install smartmontools
go run collector/cmd/collector-metrics/collector-metrics.go run --debug
```


## Debugging
# Debugging

If you need more verbose logs for debugging, you can use the following environmental variables:

Expand All @@ -131,3 +156,16 @@ Finally, you can copy the files from the scrutiny container to your host using t
docker cp scrutiny:/tmp/collector.log collector.log
docker cp scrutiny:/tmp/web.log web.log
```

# Docker Development

```
docker build -f docker/Dockerfile . -t chcr.io/analogj/scrutiny:master-omnibus
docker run -it --rm -p 8080:8080 \
-v /run/udev:/run/udev:ro \
--cap-add SYS_RAWIO \
--device=/dev/sda \
--device=/dev/sdb \
ghcr.io/analogj/scrutiny:master-omnibus
/opt/scrutiny/bin/scrutiny-collector-metrics run
```
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ COPY webapp/frontend /opt/scrutiny/src
RUN npm install -g @angular/cli@9.1.4 && \
mkdir -p /scrutiny/dist && \
npm install && \
ng build --output-path=/opt/scrutiny/dist --prod
npm run build:prod -- --output-path=/opt/scrutiny/dist


########
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.web
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ COPY webapp/frontend /opt/scrutiny/src
RUN npm install -g @angular/cli@9.1.4 && \
mkdir -p /opt/scrutiny/dist && \
npm install && \
ng build --output-path=/opt/scrutiny/dist --prod
npm run build:prod -- --output-path=/opt/scrutiny/dist


########
Expand Down
50 changes: 49 additions & 1 deletion docs/TROUBLESHOOTING_REVERSE_PROXY.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,52 @@ api:
You may also configure these values using the following environmental variables (both are required).

- `COLLECTOR_API_ENDPOINT=http://localhost:8080/custombasepath`
- `SCRUTINY_WEB_LISTEN_BASEPATH=/custombasepath`
- `SCRUTINY_WEB_LISTEN_BASEPATH=/custombasepath`

# Real Examples

## Caddy

1. Create a Caddyfile
```yaml
# Caddyfile
:9090

# The `scrutiny` text in this file must match the service name in the docker-compose file below.
# The `/custom/` text is the custom base path scrutiny will be availble on.
reverse_proxy /custom/* scrutiny:8080

```
2. Create a `docker-compose.yml` file

```yaml
# docker-compose.yml
version: '3.5'

services:
scrutiny:
container_name: scrutiny
image: ghcr.io/analogj/scrutiny:master-omnibus
cap_add:
- SYS_RAWIO
ports:
- "8086:8086" # influxDB admin
volumes:
- /run/udev:/run/udev:ro
- ./config:/opt/scrutiny/config
- ./influxdb:/opt/scrutiny/influxdb
devices:
- "/dev/sda"
- "/dev/sdb"
environment:
- SCRUTINY_WEB_LISTEN_BASEPATH=/custom
- COLLECTOR_API_ENDPOINT=http://localhost:8080/custom
caddy:
image: caddy
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
ports:
- "9090:9090"
```
3. run `docker-compose up`
4. visit [http://localhost:9090/custom/web](http://localhost:9090/custom/web) - access the scrutiny container via caddy reverse proxy
1 change: 1 addition & 0 deletions webapp/backend/pkg/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type DeviceRepo interface {
UpdateDevice(ctx context.Context, wwn string, collectorSmartData collector.SmartInfo) (models.Device, error)
UpdateDeviceStatus(ctx context.Context, wwn string, status pkg.DeviceStatus) (models.Device, error)
GetDeviceDetails(ctx context.Context, wwn string) (models.Device, error)
DeleteDevice(ctx context.Context, wwn string) error

SaveSmartAttributes(ctx context.Context, wwn string, collectorSmartData collector.SmartInfo) (measurements.Smart, error)
GetSmartAttributeHistory(ctx context.Context, wwn string, durationKey string, attributes []string) ([]measurements.Smart, error)
Expand Down

0 comments on commit b208493

Please sign in to comment.