Passed
Push — master ( 0600e6...77030f )
by P.R.
02:59
created

HaltCommand.__init__()   A

Complexity

Conditions 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 21
ccs 0
cts 5
cp 0
crap 2
rs 9.3142
c 1
b 0
f 0
1
"""
2
Enarksh
3
4
Copyright 2013-2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
import os
9
10
import zmq
11
from cleo import Command
12
13
import enarksh
14
from enarksh.message.HaltMessage import HaltMessage
15
from enarksh.style.EnarkshStyle import EnarkshStyle
16
17
18
class HaltCommand(Command):
19
    """
20
    Halts a daemon of Enarksh
21
22
    halt
23
24
        {daemon : The Enarksh daemon to halt: controllerd, loggerd, or spanwerd}
25
    """
26
27
    # ------------------------------------------------------------------------------------------------------------------
28
    def __init__(self):
29
        """
30
        Object constructor.
31
        """
32
        Command.__init__(self)
33
34
        self._zmq_context = None
35
        """
36
        The ZMQ context.
37
38
        :type: Context
39
        """
40
41
        self.__end_points = {'controllerd': enarksh.CONTROLLER_PULL_END_POINT,
42
                             'loggerd':     enarksh.LOGGER_PULL_END_POINT,
43
                             'spawnerd':    enarksh.SPAWNER_PULL_END_POINT}
44
        """
45
        The end points of the Enarksh daemons.
46
47
        :type: dict[string,string]
48
        """
49
50
    # ------------------------------------------------------------------------------------------------------------------
51
52
    def __zmq_init(self):
53
        """
54
        Initializes ZMQ.
55
        """
56
        self._zmq_context = zmq.Context()
57
58
    # ------------------------------------------------------------------------------------------------------------------
59
    def handle(self):
60
        """
61
        Executes the halt command.
62
        """
63
        self.output = EnarkshStyle(self.input, self.output)
64
65
        os.chdir(enarksh.HOME)
66
67
        self.__zmq_init()
68
69
        daemon = self.input.get_argument('daemon')
70
71
        if daemon not in self.__end_points:
72
            self.output.error("Unknown daemon '{}'".format(daemon))
73
            return -1
74
75
        # Connect tot the daemon.
76
        zmq_daemon = self._zmq_context.socket(zmq.PUSH)
77
        zmq_daemon.connect(self.__end_points[daemon])
78
79
        # Send the halt message tot the daemon.
80
        message = HaltMessage()
81
        zmq_daemon.send_pyobj(message)
82
83
# ----------------------------------------------------------------------------------------------------------------------
84