Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Private Servers and for Memory Segments #8

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.coffee text eol=lf
*.j2 text eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
docker-compose.env
69 changes: 68 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,25 @@ Create a password for your screeps account if you haven't already.

You're now ready to run this whale of a command. Replace the stuff in caps with values that makes sense

For `YOURSOURCE` use either `memory` (the default) or `segment=N` where `N` is the number
of the memory segment that holds your stats.

Examples:

- `-e screeps_stats_source=memory` is the default and need not be specified.
It will get statistics from `Memory.stats`
- `-e screeps_stats_source=segment=0` will get statistics from the `stats` key
in the JSON data stored in segment `0`

### Ansible Command-line


```
ansible-playbook \
-e screeps_username=YOURUSERNAME \
-e screeps_password=YOURPASSWORD \
-e screeps_email=YOUREMAIL \
--user ubuntu \
--private-key YOURPRIVATEKEY \
-i ,YOURSERVERIP \
playbook.yml
```
Expand Down Expand Up @@ -89,6 +101,61 @@ to install the plugins, then
`docker-compose restart grafana`
to apply. Refresh your browser and voila!

## Advanced configuration

### Alternative (Basic HTTP) Authentication

To read stats from a private server, you must use the option `screeps_basic_auth=1`
and pass the additional setting `screeps_hostname` which should use the `http` protocol,
use your private server's hostname or IP, and use the screep server port (by default
`21025` unless your server changed it).

The value of `SCREEPS_BASIC_AUTH` determines how Grafana will try to login to the Screeps server.

If it's `0` the default mechanism used by the official server will be used. If it is `1` an alternative
mechanism is used, using HTTP Basic Authentication. This method is required by some private server mods
such as [screepsmod-auth](https://github.com/ScreepsMods/screepsmod-auth).

```
ansible-playbook \
-e screeps_basic_auth=1 \
-e screeps_hostname=http://your.private.server.name:21025 \
-e screeps_username=YOURUSERNAME \
-e screeps_password=YOURPASSWORD \
-e screeps_email=YOUREMAIL \
--user ubuntu \
-i ,YOURSERVERIP \
playbook.yml
```


## Important Development Notes

If you're modifying `screeps-grafana` and wanting to test your changes, you'll want
to know the following:

After you make changes to your own fork of this repository, you will need to generate
a docker image, publish it to Docker Hub and modify `docker-compose.yml` to use your own
custom docker image rather than `screepers/screeps-statsd`.

Where `YOU` is your [Docker Hub](https://hub.docker.com) username:

```bash
sudo docker build -t YOU/screeps-statsd .
sudo docker login -u YOU -p YOUR_DOCKER_HUB_PASSWORD
sudo docker push YOU/screeps-statsd
```

After completing these steps, your new custom image `YOU/screeps-statsd` will exist
on Docker Hub and you can use it.

Add `-e screeps_node_image=YOU/screeps-statsd` to the `ansible-playbook` command line
to use your own Docker Hub image instead of the default.

Each time you want to test new changes, you need to `docker build`, `docker push`
and then `ansible-playbook` to update your container with your new code.


## License

This software is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for more information.
10 changes: 5 additions & 5 deletions roles/screeps-statsd/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
- /data/data/grafana:/opt/grafana/data
when: data_volume.stat.exists

- name: copy docker-compose.yml
copy:
src: docker-compose.yml
dest: /root/
- name: Write docker-compose.yml
template:
src: docker-compose.yml.j2
dest: /root/docker-compose.yml

- name: create docker-compose.override.yml
copy:
Expand Down Expand Up @@ -92,4 +92,4 @@
- name: docker containers
command: bash setup.sh
args:
chdir: /root
chdir: /root
5 changes: 4 additions & 1 deletion roles/screeps-statsd/templates/docker-compose.env.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SCREEPS_USERNAME={{ screeps_username }}
SCREEPS_EMAIL={{ screeps_email }}
SCREEPS_PASSWORD={{ screeps_password }}
SCREEPS_PASSWORD={{ screeps_password }}
SCREEPS_HOSTNAME={{ screeps_hostname|default ('https://screeps.com') }}
SCREEPS_BASIC_AUTH={{ screeps_basic_auth|default ('0') }}
SCREEPS_STATS_SOURCE={{ screeps_stats_source|default ('memory') }}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
version: "2"
services:
node:
image: {{ screeps_node_image|default ('screepers/screeps-statsd') }}
build:
context: .
dockerfile: Dockerfile
context: .
dockerfile: Dockerfile
restart: always
links:
- statsd
Expand Down
51 changes: 43 additions & 8 deletions src/ScreepsStatsd.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ For full copyright and license information, please see the LICENSE file

@author Bryan Conrad <[email protected]>
@copyright 2016 Bryan Conrad
@copyright 2017 Ross Perkins
@link https://github.com/hopsoft/docker-graphite-statsd
@license http://choosealicense.com/licenses/MIT MIT License
###
Expand Down Expand Up @@ -43,39 +44,73 @@ class ScreepsStatsd
if(token != "" && succes)
@getMemory()
return
#console.log "ENV = " + JSON.stringify(process.env)
if process.env.SCREEPS_BASIC_AUTH == 1
@signinBasicAuth()
return
@client = new StatsD host: process.env.GRAPHITE_PORT_8125_UDP_ADDR
console.log "New login request - " + new Date()
options =
uri: 'https://screeps.com/api/auth/signin'
uri: process.env.SCREEPS_HOSTNAME + '/api/auth/signin'
json: true
method: 'POST'
body:
email: process.env.SCREEPS_EMAIL
password: process.env.SCREEPS_PASSWORD
console.log "New login request - " + options.uri + " - " + new Date()
rp(options).then (x) =>
token = x.token
@getMemory()

###
Sign-in using HTTP Basic Authentication (username & password).
This non-standard way of signing in is used by some private server
auth-mods. This can be disabled/enable via env-variables (see README).
###
signinBasicAuth: () =>
@client = new StatsD host: process.env.GRAPHITE_PORT_8125_UDP_ADDR
options =
uri: process.env.SCREEPS_HOSTNAME + '/api/auth/signin'
json: true
method: 'POST'
console.log "New login request via HTTP Basic - " + options.uri + " - " + new Date()
rp(options).auth(process.env.SCREEPS_USERNAME, process.env.SCREEPS_PASSWORD, true).then (x) =>
token = x.token
@getMemory()

getMemory: () =>
succes = false
apiEndpoint = '/api/user/memory'
fromSegment = process.env.SCREEPS_STATS_SOURCE && process.env.SCREEPS_STATS_SOURCE != 'memory'
if fromSegment
apiEndpoint = '/api/user/memory-segment?' + process.env.SCREEPS_STATS_SOURCE
options =
uri: 'https://screeps.com/api/user/memory'
method: 'GET'
uri: process.env.SCREEPS_HOSTNAME + apiEndpoint
method: 'GET'
json: true
resolveWithFullResponse: true
headers:
"X-Token": token
"X-Username": token
qs:
# segment api doesn't support limiting scope to 'stats' element
if not fromSegment
options.qs =
path: 'stats'
#console.log "Using request options: " + JSON.stringify(options)
rp(options).then (x) =>
# yeah... dunno why
token = x.headers['x-token']
return unless x.body.data
data = x.body.data.split('gz:')[1]
finalData = JSON.parse zlib.gunzipSync(new Buffer(data, 'base64')).toString()
succes = true
@report(finalData)
if fromSegment
# segments come as plain text, not deflated
finalData = JSON.parse x.body.data
# Use only the 'stats' data from this segment, in case there is other stuff
@report(finalData.stats)
else
# memory comes deflated, first 3 chars "gz:" to indicate the deflation
data = x.body.data.substring(3)
finalData = JSON.parse zlib.inflateSync(new Buffer(data, 'base64')).toString()
@report(finalData)

report: (data, prefix="") =>
if prefix is ''
Expand Down