How can I reboot my server using Memset API?

If your server is not responding, we can reboot it for you. Regardless of whether it's a fully dedicated server or a Miniserver, we will try the cleanest possible way to reboot it (power-down being the last resort).

You can also schedule a server reboot from the "Reboot & emergency contact facility" on the management page of your server, but there's another way: using Memset's API server.reboot method.

First you need to create an API key using the "API Keys Manager" link in your account overview page.

The API keys are 32 hexadecimal numbers that can be used to identify you when calling API methods over HTTPS. The operations that can be performed with that key can be limited using API scopes. For example, we want a key to reboot our server named myserver1, so we set the scope to: name: myserver1 and method: server.reboot.

We could create an unrestricted API key, but it's safer to limit the scope as much as we can. Therefore, if the key gets compromised, we have minimised the consequences (someone else rebooting our server is bad, but it could definitely be worse!).

With that key we can use one of the two access methods, for example: the RESTful interface.

We can perform the API calls with  cURL, a common stock tool in any Linux server, to call the method providing the API key for authentication:

curl --user 4c2c8e0e3d784ad3af18aa25582e059a:x \
https://api.memset.com/v1/json/server.reboot/myserver1/

Note: be careful with the shell history in a multi-user system, you might be disclosing the API key.

The result should be something like the following:

{
 "status": "PENDING", 
 "service": "myserver1", 
 "finished": false, 
 "error": false, 
 "type": "RESTART-SERVER", 
 "id": "2a359e042b3a6731c17a45e5418e722e"
}

You can track the status of the job using the returned id and job.status method, although we can't do it with that key because of its restricted scope:

curl --user 4c2c8e0e3d784ad3af18aa25582e059a:x \
https://api.memset.com/v1/json/job.status/2a359e042b3a6731c17a45e5418e722e/

As expected, we get an error:

{
 "error_type": "ApiErrorForbidden", 
 "error_code": 4, 
 "error": "Can't use this api_key with name='2a359e042b3a6731c17a45e5418e722e'"
}

Easy, isn't it? Take a look at the list of supported methods for other functionalities available trough the API.

Posted May 2012 by Juan J. Martínez , in Dev

blog comments powered by Disqus