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 · 17 comments
Open

Server start/stop api #98

logwet opened this issue Jul 26, 2019 · 17 comments

Comments

@logwet
Copy link
Contributor

@logwet logwet 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
Copy link
Member

@Raekye Raekye 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 added a commit that referenced this issue Aug 11, 2019
@Raekye
Copy link
Member

@Raekye Raekye 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
Copy link
Contributor Author

@logwet logwet 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
Copy link
Member

@Raekye Raekye 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
Copy link
Member

@Raekye Raekye 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
Copy link
Contributor Author

@logwet logwet 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
Copy link
Contributor Author

@logwet logwet 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
Copy link
Member

@Raekye Raekye 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

@ehllie
Copy link

@ehllie ehllie commented Sep 30, 2019

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

@logwet
Copy link
Contributor Author

@logwet logwet commented Oct 1, 2019

@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

@CoWinkKeyDinkInc
Copy link

@CoWinkKeyDinkInc CoWinkKeyDinkInc commented Mar 21, 2020

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?

@MaeIsBad
Copy link
Contributor

@MaeIsBad MaeIsBad commented Mar 21, 2020

@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)

@unixfox
Copy link

@unixfox unixfox commented Mar 21, 2020

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.

@Raekye
Copy link
Member

@Raekye Raekye commented Apr 12, 2020

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

@Pablete1234
Copy link

@Pablete1234 Pablete1234 commented Aug 17, 2020

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

@qixils
Copy link

@qixils qixils commented Jan 14, 2021

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

@gabrielbrazs
Copy link

@gabrielbrazs gabrielbrazs commented Feb 26, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants