Gamocosm icon indicating copy to clipboard operation
Gamocosm copied to clipboard

Server start/stop api

Open logwet opened this issue 4 years ago • 20 comments

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

logwet avatar Jul 26 '19 14:07 logwet

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 avatar Aug 04 '19 14:08 Raekye

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

Raekye avatar Aug 11 '19 07:08 Raekye

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)

logwet avatar Aug 13 '19 08:08 logwet

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 avatar Aug 13 '19 15:08 Raekye

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

Raekye avatar Aug 13 '19 16:08 Raekye

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 avatar Aug 13 '19 23:08 logwet

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

logwet avatar Aug 14 '19 14:08 logwet

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

Raekye avatar Aug 15 '19 20:08 Raekye

Is the [server id] supposed to be the DO id, or something else?

Because when I use the id from DO I always get {"error":"Not found"} as a response

ehllie avatar Sep 30 '19 18:09 ehllie

@oscaretti The server id is part of the url you use to access the dashboard of your server. https://gamocosm.com/servers/[server-id]

Eg. If I accessed my server from https://gamocosm.com/servers/1zx0sepq-pqse-65hf-4o38-qan84i36826xj then the server-id would be 1zx0sepq-pqse-65hf-4o38-qan84i36826xj

logwet avatar Oct 01 '19 07:10 logwet

When I used curl https://gamocosm.com/servers/8*****/api/f****/stop. I instead got a 404 link. It didn't turn off the server or return anything other than the actual webpage. Using the same thing with /status works though. Is this possible to do as just a clickable link on a static webpage?

CoWinkKeyDinkInc avatar Mar 21 '20 05:03 CoWinkKeyDinkInc

@CoWinkKeyDinkInc you are supposed to make a post request to endpoints other than /status. I'm on my phone but I think the command was curl --data " " [URL]

Also keep in mind there seems to be a bug where /stop sometimes fails to destroy the droplet and only creates a snapshot and stops the minecraft server(#113). You might need to call the stop endpoint a few times for it to actually work and then delete old snapshots(you can use the digitalocean api for that)

MaeIsBad avatar Mar 21 '20 10:03 MaeIsBad

I wouldn't encourage to build a project on top of gamocosm until #113 is fixed because it's very annoying if the server doesn't get deleted and you have to manually delete the snapshot every time the server gets stopped. Personally I had to create a separate method in my project to handle the bug just for that: Alteiria/gamocosmautostart/index.js#L12 and Alteiria/gamocosmautostart/gamocosmGCSnapshot.js. I hope @Raekye will one-day fixes the issue.

unixfox avatar Mar 21 '20 11:03 unixfox

It's been a week since the fixes/last status update for #113, and it seems like there hasn't been any issues. Also, the latest commit adds a layer of redundancy for the snapshotting issue described. If a server didn't start correctly, the (old) snapshot it was created from may not be destroyed. Now, after stopping a server, once it successfully creates a new snapshot, it will also delete the old snapshot if present. So hopefully we don't see stray snapshots anymore

Raekye avatar Apr 12 '20 12:04 Raekye

I've made a simple discord bot in java that makes use of this API, and lets you have any amount of servers configured, one per channel. I'm leaving this here in case someone in the future wants extra reference or a java wrapper for the API: https://github.com/Pablete1234/Gamocord

Pablete1234 avatar Aug 17 '20 00:08 Pablete1234

I'm getting a generic 500 Internal Server Error when trying to use the /start endpoint even though it seems to work appropriately?

qixils avatar Jan 14 '21 20:01 qixils

I am also getting a code 500 response from the /start endpoint, but it seems to start the server properly anyways.

TehVenomm avatar Feb 26 '21 23:02 TehVenomm

Started getting HTTP 500 ... Something related to Cloudflare

danielospina-b avatar Sep 03 '22 16:09 danielospina-b

I don't know if I'm just that bad at checking my email but I swear I don't remember getting notifications for the previous two comments in 2021... GitHub does say I'm subscribed to this issue. Anyways, the main site seems up to me, let me investigate the API

Raekye avatar Sep 03 '22 16:09 Raekye

Ok sorry for the delay... did find problems with the /status and /start endpoint. The /start endpoint, as noted by gabrielbrazs, would start the server, but there was a typo regarding clearing the user's cache that would cause it to fail. My very limited testing seems to show those two things work now (as well as /stop)

Thanks for your patience, and please let me know if there are further issues!

Raekye avatar Sep 06 '22 10:09 Raekye

The API is now documented on the wiki. Since there hasn't been additional discussion on this issue, I will close it now. Please don't hesitate to open a new issue to report problems or request additional features. Thanks for everyone's participation!

Raekye avatar Jul 28 '23 13:07 Raekye