MaaS - wakeonlan/etherwake manual shutdown.

Abstract


MaaS is a great tool which allows to control machines: powering on/off, installing new OS and all it is done with just few clicks. But there are some oddities. It is not powering down 
the nodes when power type is ether_wake (same issue for wakeonlan). So, this post is about hacking into maas source code and making possible to shutdown the nodes.
Solution
First of all, let's look into the DESCRIPTION paragraph for the etherwake man pages: etherwake  is  a program that generates and transmits a Wake-On-LAN (WOL) "Magic Packet", 
used for restarting machines that have been soft-powered-down (ACPI D3-warm state).
So, this tool cannot power off the machine and it seems like for now maas repository package doesn't have any feature for that. So, I come across my own solution. And here it is:
  1. First of all make sure that your ssh-key to controll machines via maas belongs to maas user. In the maas documentation there is part where you should generate ssh key and copy it into the MaaS WebUI. This is very important because we will use ssh connection to shutdown the machines via maas python script. The script is run by maas user;
  2. Check ssh connection from maas controller machine to the nodes which were added to the maas cluster;
  3. After that follow the steps to adjust maas code to the new changes.
On the maas controller node create directory: maas_extra under the / directory. After that go inside the maas_extra and create file shutdown_manually.py Open the file with your favourite text editor and paste the following code:
# Shuts down the node via ssh by a given hostname.

import os
import sys
import syslog

if __name__ == "__main__":
    cmd = "ssh ubuntu@" + str(sys.argv[1]) + " sudo poweroff"
    syslog.syslog(cmd)
    os.system(cmd)
After that do chmod 777 for a maas_extra folder. This script will be called from maas python script by maas user. That's why this is very important that we have ssh key for maas user to connect to machines.
So, after that please open following file /usr/lib/python2.7/dist-packages/maasserver/node_action.py and edit it with the following code:
  1. Add import os to the top of the file;
  2. Next find the class StopNode, and in this class find a method execute;
  3. After the try: add paste following code: 
cmd = 'python /maas_extra/shutdown_manually.py ' + self.node.hostname  os.system(cmd) So, now execute method will look like that:
def execute(self, allow_redirect=True):
        """See `NodeAction.execute`."""
        try:
            cmd = 'python /maas_extra/shutdown_manually.py ' + self.node.hostname
            os.system(cmd)
            self.node.stop(self.user)
        except RPC_EXCEPTIONS + (ExternalProcessError,) as exception:
            raise NodeActionError(exception)
        else:
            return "This node has been asked to shut down."
To accept the changes you have to restart apache server:
/etc/init.d/apache2 restart
After that go to the MaaS WebUI and try to shutdown the node, of course make sure that it is running. Also we need to do the same procedure for a maas command line interface.
 So, open the file /usr/lib/python2.7/dist-packages/maasserver/api/nodes.py and import os  at the top of the file, also find the function stop. After the node assigning 
add the same code like in the paragraph 3. at the top of this post. So now part of the function stop should look like this:
stop_mode = request.POST.get('stop_mode', 'hard')
node = Node.objects.get_node_or_404(
    system_id=system_id, user=request.user,
    perm=NODE_PERMISSION.EDIT)
cmd = 'python /maas_extra/shutdown_manually.py ' + node.hostname
os.system(cmd)
After that try to shutdown the node via command line. Now maas can start the nodes via etherwake + shut it down via ssh.

Comments

  1. This is great.. but what version of maas is this for? version 2.0 i hope?

    All so is there a way of changing the maas 2.0 web gui to show a power type of WOL and take its MAC address?

    Thanks in advance for any help

    ReplyDelete
    Replies
    1. This is for MaaS 1.6

      Yes, it is possible because you can change the code (html of web interface and other code for retrieving WOL) and for MAC address there is a header, to the right of FQDN, it should be there.

      Delete

Post a Comment

Popular Posts