Prevent Command Execution Failures using Dynamic Instance Naming

Photo of Andrew Pielage by Andrew Pielage

When creating instances, it can sometimes be a chore remembering what names are already in use or even coming up with a name to use in the first place!  The Dynamic Instance Naming feature, sometimes called auto-naming, introduces a new option to the create-instance & create-local-instance commands that, when enabled, will resolve any name conflicts or generate a name for you.

The Dynamic Instance Naming feature is activated by the new -a (or --autoname if you fancy being verbose) option on the command line, like so:

asadmin -a create-instance --node localhost-domain1 instance1

In the above example, if an instance named instance1 didn’t exist, you’d get your instance as normal. If an instance did exist with the same name (or another resource has that name – remember that things like deployment groups share the same name space!), what would happen is you’d get an instance named something along the lines of instance1-BemusedCatfish. Previously, you’d have just got an error!

Now, what would happen if you omitted the instance name altogether like so?

asadmin -a create-instance --node localhost-domain1

Payara Server will now auto-generate a name for you, that’s what would happen! The server will auto-generate a name similar to ConfusedCarp and create an instance using it. Those of you familiar with the Payara Platform will recognise that this is very similar to the names that Payara Micro instances generate for themselves!

Obviously, if you’d rather the commands fail when a conflict is found, or prompt you to enter a name, you can simply omit this new option: it’s all optional and off by default.

Setting Up Instances Via Scripts

Now while this may just sound like a silly gimmick feature, it does have some actual uses. The most obvious one is when setting up instances via scripts. Rather than having to specify the name of each instance before you start running commands, and then having those commands fail if a conflict is found, you can now use the auto-name option and combine it with the new, extra terse option to set the names of variables on the fly:

INSTANCE_NAME=”$(asadmin -T -a create-local-instance --node localhost-domain1)”
asadmin start-instance ${INSTANCE_NAME}

"Wait? Extra terse? What’s that?" I hear you cry: it’s another option we added, specifically for this situation. It’s enabled via the -T flag (in comparison to “normal” terseness being enabled via “-t”), and will make the output of the command simply the instance name, like so:

> asadmin -T -a create-local-instance --node localhost-domain1
> ObnoxiousShark

Auto-Naming in Docker Nodes

The auto-naming feature has also been brought into our tech-preview of Docker Nodes. The default image (payara/server-node) has been updated to now also auto-generate instance names if one isn’t provided. This doesn't normally happen fi you're creating instances using asadmin or the Admin Console - this only happens if you’re creating the containers directly using the Docker CLI or REST API. With this functionality now present, you could now run the following command repeatedly without changes from the Docker CLI to create your containers and instances , auto-generating names for the instances in each (assuming you’ve set up a Docker Node on the machine – that’s still required unfortunately!):

docker container create --network host --mount type=bind,source="/home/anon/passwordfile.txt",target="/opt/payara/passwords/passwordfile.txt",readonly -e PAYARA_DAS_HOST=payaraDas -e PAYARA_DAS_PORT=4848 -e PAYARA_NODE_NAME=docky1 payara/server-node:5.193

The name conflict resolution feature is also present in the Docker Nodes image, so if you ran the above command with an instance name provided it will resolve the conflicts as it would when creating “normal” instances.

docker container create --network host --mount type=bind,source="/home/anon/passwordfile.txt",target="/opt/payara/passwords/passwordfile.txt",readonly -e PAYARA_DAS_HOST=payaraDas -e PAYARA_DAS_PORT=4848 -e PAYARA_NODE_NAME=docky1 -e PAYARA_INSTANCE_NAME=insty1 payara/server-node:5.193

Get the Full Documentation 

Full documentation of the auto-name and extra terse features can be found here, and documentation of creating Docker Node instances directly from Docker can be found here.

Don't have the Payara Platform yet? 

 Payara Platform  Download Here 

As always, you can contact us via the usual methods: leave a comment, find us on Facebook & Twitter, or visit our GitHub or Forum.

Comments