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

Server start/stop api #98

Open
logwet opened this issue Jul 26, 2019 · 8 comments

Comments

@logwet
Copy link
Contributor

commented Jul 26, 2019

I want to write a Discord bot that'll allow the members of my server to start the server (ie. When Gamocosm creates a DO droplet from the snapshot of the previous session) outside of the scheduled autostart and shutdown hours. To do that I'd need to access an API that would interact directly with the server manager on https://gamocosm.com/
I already know there's an API to interact with servers that are already online, the MCSW, but as far as I'm aware there's no such API for servers that are offline.
Is it possible to implement this?

Thanks

@Raekye

This comment has been minimized.

Copy link
Member

commented Aug 4, 2019

I haven't been actively updating Gamocosm, but I probably should, I've been more productive with my own work lately and it's about time I dust this off again. This is certainly possible. I will try to fix outstanding issues today and update the code and refamiliarize myself with it. Then I'll work on this... hopefully within a week if I don't disappear again? 😅

@Raekye Raekye added the enhancement label Aug 4, 2019

Raekye added a commit that referenced this issue Aug 11, 2019

@Raekye

This comment has been minimized.

Copy link
Member

commented Aug 11, 2019

Surprisingly, I finished it in a week as promised 😅 It's subject to change, so I'll just document it here for now:

  • There's an API key per server, which you can see under the Advanced Settings tab
  • All API endpoints return a JSON dictionary
  • The following 4 endpoints return a single key error which should be null if successful, otherwise an error message
    • https://gamocosm.com/servers/[server id]/api/[api key]/start: starts the server + minecraft
    • https://gamocosm.com/servers/[server id]/api/[api key]/stop: stop the server + minecraft
    • https://gamocosm.com/servers/[server id]/api/[api key]/pause: stops minecraft
    • https://gamocosm.com/servers/[server id]/api/[api key]/resume: starts minecraft
  • The status endpoing: https://gamocosm.com/servers/[server id]/api/[api key]/resume returns 3 keys with boolean values:
    • server: true if digital ocean server is created and active
    • status: pending operation of the server (null if not doing anything, otherwise something like starting or preparing or saving)
    • minecraft: true if minecraft is running
  • Therefore a successfully/fully running server would have values true, null, and true respectively

Change notice

(Although I doubt other people will be using the API at this time) Anyone who uses this API for now should keep an eye on this thread/message me to contact them for changes to the API. It's quite simple so it might not have to be changed, but based on @logwet's feedback and anything else I think of it may change

@logwet

This comment has been minimized.

Copy link
Contributor Author

commented Aug 13, 2019

The API interface requires sign in/cookie authentication. I couldn't access it through my python script until I copied the cookie for the Gamocosm session into the headers I'm passing into requests. Once I do that it all works fine but I'm hesitant to leave my authentication data in plain text.

It'd also be nice to have end points for:

  • Restarting the DO server
  • Remote backup
  • World download (pass the url)
  • The server hostname (less important but useful for convenience)
@Raekye

This comment has been minimized.

Copy link
Member

commented Aug 13, 2019

Ah yes the cookie/session thing was an oversight. I'll look into it. As for authentication data in plain text, what do you mean? Gamocosm uses https, which I have to double check, but IIRC endpoints (the rest of the URL after the domain, which has your server ID and API key) are encrypted

@Raekye

This comment has been minimized.

Copy link
Member

commented Aug 13, 2019

The cookie/session issue should be fixed.

I forgot to mention there was already a /reboot endpoint which restarts the DO server. Additionally, I've added /backup which behaves similarly to the other endpoints (error: null on success, otherwise a message).

The /status endpoint now additionally returns:

  • ip: ip address or null if server is not running
  • domain: domain of server (should never be null)
  • download: download URL; null if server has not fully started

Finally, all endpoints except for /status now respond to POST requests (instead of GET). You can test the endpoints like:

# get request for status
curl https://gamocosm.com/servers/[server id]/api/[api key]/status

# post request for all "actions"
curl --data '' https://gamocosm.com/servers/[server id]/api/[api key]/start

In recap, there are 7 endpoints, accessible as https://gamocosm.com/servers/[server id/api/[api key]/[endpoint]:

  • status
  • start
  • stop
  • reboot
  • pause
  • resume
  • backup
@logwet

This comment has been minimized.

Copy link
Contributor Author

commented Aug 13, 2019

As for authentication data in plain text, what do you mean? Gamocosm uses https, which I have to double check, but IIRC endpoints (the rest of the URL after the domain, which has your server ID and API key) are encrypted

My wording was weird. I meant that I didn't want to hard code the cookie, but you've already solved that issue.

I'd like to be able to send commands to the server through the MCSW. Can you either add an endpoint to grab the MCSW key, or a POST endpoint to send a command to the server.

curl --data '{ "command": "say hi" }' "http://gamocosm-mothership:$MCSW_PASSWORD@SERVER_HOSTNAME:5000/exec"

@logwet

This comment has been minimized.

Copy link
Contributor Author

commented Aug 14, 2019

I've whipped up a quick bot to do some testing with Discord. It's very ugly, but it works.

@Raekye

This comment has been minimized.

Copy link
Member

commented Aug 15, 2019

Nice bot! I added an exec endpoint:

curl -H 'Content-Type: application/json' --data '{ "command": "say hi" }' http://gamocosm.com/servers/[server id]/api/[api key]/exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.