Passed
Push — master ( 3b532d...9b942b )
by P.R.
02:53
created

HaltCommand.__read_config()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 9.6666
c 0
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
from enarksh.C import C
14
from enarksh.Config import Config
15
from enarksh.message.HaltMessage import HaltMessage
16
from enarksh.style.EnarkshStyle import EnarkshStyle
17
18
19
class HaltCommand(Command):
20
    """
21
    Halts a daemon of Enarksh
22
23
    halt
24
25
        {daemon : The Enarksh daemon to halt: controller, logger, or spanwerd}
26
    """
27
28
    # ------------------------------------------------------------------------------------------------------------------
29
    def __init__(self):
30
        """
31
        Object constructor.
32
        """
33
        Command.__init__(self)
34
35
        self._zmq_context = None
36
        """
37
        The ZMQ context.
38
39
        :type: Context
40
        """
41
42
        self.__end_points = {}
43
        """
44
        The end points of the Enarksh daemons.
45
46
        :type: dict[string,string]
47
        """
48
49
    # ------------------------------------------------------------------------------------------------------------------
50
    def __read_config(self):
51
        """
52
        Reads the pull end point of the controller, logger, and spawner.
53
        """
54
        config = Config.get()
55
56
        self.__end_points['controller'] = config.get_controller_pull_end_point()
57
        self.__end_points['logger'] = config.get_logger_pull_end_point()
58
        self.__end_points['spawner'] = config.get_spawner_pull_end_point()
59
60
    # ------------------------------------------------------------------------------------------------------------------
61
    def __zmq_init(self):
62
        """
63
        Initializes ZMQ.
64
        """
65
        self._zmq_context = zmq.Context()
66
67
    # ------------------------------------------------------------------------------------------------------------------
68
    def handle(self):
69
        """
70
        Executes the halt command.
71
        """
72
        self.output = EnarkshStyle(self.input, self.output)
73
74
        os.chdir(C.HOME)
75
76
        self.__read_config()
77
        self.__zmq_init()
78
79
        daemon = self.input.get_argument('daemon')
80
81
        if daemon not in self.__end_points:
82
            self.output.error("Unknown daemon '{}'".format(daemon))
83
            return -1
84
85
        # Connect tot the daemon.
86
        zmq_daemon = self._zmq_context.socket(zmq.PUSH)
87
        zmq_daemon.connect(self.__end_points[daemon])
88
89
        # Send the halt message tot the daemon.
90
        message = HaltMessage()
91
        zmq_daemon.send_pyobj(message)
92
93
# ----------------------------------------------------------------------------------------------------------------------
94