mattmalec / Pterodactyl4J

Java wrapper for the API of Pterodactyl
Apache License 2.0
117 stars 30 forks source link

How can I create a server on a specific Node? #71

Closed Clayun closed 1 year ago

Clayun commented 1 year ago

When I try to create a server and specify a Node, I can't find a place to do so. How can I proceed?

Goksi commented 1 year ago

with ServerCreationAction#setAllocation, as single allocation can only be linked to specific Node. You can also use ServerCreationAction#setLocation to select random node from desired location.

Clayun commented 1 year ago

is setAllocation a port?

Clayun commented 1 year ago

i mean setAllocation(25565),is that right?

Raft08 commented 1 year ago

Get the allocation

Node serverNode = pteroApplication.retrieveNodeById(1).execute(); // Get the node with the ID 1

//Get All Allocations
List<ApplicationAllocation> nodesAllocation = serverNode.retrieveAllocations().execute(); //Gets all available allocation
List<ApplicationAllocation> freeAllocs = nodesAllocation.stream().filter(alloc -> !alloc.isAssigned()).toList(); // Filters out all already used allocations

//Get one specific
List<ApplicationAllocation> portAllocs = serverNode.retrieveAllocationsByPort(25565).execute(); //Get all allocation with that specific port
ApplicationAllocation alloc = portAllocs.get(0); //Get the first allocation of the list

//null checks

That should do the job

Clayun commented 1 year ago

image if i don't specific location and allocations,only specific the port,is that work?

Raft08 commented 1 year ago

I suppose your panel will return an error, Your server needs at least one port to bind to, The ApplicationAllocation defines the port and the server

I never used the setLocation parameter, it's optional

Raft08 commented 1 year ago

Thet the code I use to make my servers

        ServerCreationAction creationAction = this.api.createServer()
                .setCPU(eggConfig.resources().cpu())
                .setMemory(eggConfig.resources().memory(), DataType.MB)
                .setSwap(eggConfig.resources().swap(), DataType.MB)
                .setDisk(eggConfig.resources().disk(), DataType.MB)
                .setName(name)
                .setDescription("Server Generated by AtlasNetwork")
                .setOwner(this.api.retrieveUserById(this.config.userId()).execute())
                .startOnCompletion(true)
                .setEgg(egg)
                .setDockerImage(eggConfig.image());
Clayun commented 1 year ago

I just tried it out, and the code above can run.

I only specific the port and it works. Without a location or allocations.

.setPort(creator.getPort())

So i guess if two node has same port in one server, it will return a error

Raft08 commented 1 year ago

That's useful, I will use it in my code too, thanks!

mattmalec commented 1 year ago

just to add some clarity here, since an allocation is tied to a node, if you set a specific allocation, you can guarantee the server will deploy on that node.

however if you use the setPort and setLocation methods, those are used to find an open port across all nodes, or a random port on a specific location.

so tldr; use setAllocation when you want to bind a server to a specific port & node, use setPort when you don’t care where the server lands