Merge master branch to get tar component.
[slapos.git] / slapos / recipe / agent / __init__.py
1 ##############################################################################
2 #
3 # Copyright (c) 2012 Vifib SARL and Contributors. All Rights Reserved.
4 #
5 # WARNING: This program as such is intended to be used by professional
6 # programmers who take the whole responsibility of assessing all potential
7 # consequences resulting from its eventual inadequacies and bugs
8 # End users who are looking for a ready-to-use solution with commercial
9 # guarantees and support are strongly adviced to contract a Free Software
10 # Service Company
11 #
12 # This program is Free Software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 3
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #
26 #############################################################################
27
28 import os
29 import sys
30 import zc.buildout
31 import slapos.slap
32 from slapos.recipe.librecipe import BaseSlapRecipe
33 from slapos.recipe.librecipe import GenericSlapRecipe
34 import json
35 import ConfigParser
36
37 # XXX: BaseSlapRecipe and GenericSlapRecipe are deprecated, use
38 # GenericBaseRecipe and move partition parameter fetching to software release.
39 class Recipe(BaseSlapRecipe, GenericSlapRecipe):
40 def install(self):
41 self.path_list = []
42 crond = self.installCrond()
43
44 slap = slapos.slap.slap()
45 slap.initializeConnection(self.server_url, self.key_file, self.cert_file)
46 parameter_dict = slap.registerComputerPartition(
47 self.computer_id,
48 self.computer_partition_id,
49 ).getInstanceParameterDict()
50
51 # XXX: should probably expect one more (SR-originating) parameter instead
52 # of using self.work_directory .
53 configuration_path = os.path.join(self.work_directory, "agent.cfg")
54 with open(configuration_path, "w") as configuration:
55 configuration.write(parameter_dict["configuration"])
56 agent_crond_path = os.path.join(crond, "agent")
57 with open(agent_crond_path, "w") as agent_crond:
58 agent_crond.write("*/5 * * * * %s -S %s --pidfile=%s --log=%s "
59 "%s 2>&1 > /dev/null\n" % (
60 self.options["python_binary"],
61 self.options["agent_binary"],
62 self.options["pidfile"],
63 self.options["log"],
64 configuration_path,
65 ))
66
67 return self.path_list + [configuration_path, agent_crond_path]
68
69 def installCrond(self):
70 _, ws = self.egg.working_set()
71 timestamps = self.createDataDirectory('cronstamps')
72 cron_output = os.path.join(self.log_directory, 'cron-output')
73 self._createDirectory(cron_output)
74 catcher = zc.buildout.easy_install.scripts([('catchcron',
75 __name__ + '.catdatefile', 'catdatefile')], ws, sys.executable,
76 self.bin_directory, arguments=[cron_output])[0]
77 self.path_list.append(catcher)
78 cron_d = os.path.join(self.etc_directory, 'cron.d')
79 crontabs = os.path.join(self.etc_directory, 'crontabs')
80 self._createDirectory(cron_d)
81 self._createDirectory(crontabs)
82 wrapper = zc.buildout.easy_install.scripts([('crond',
83 'slapos.recipe.librecipe.execute', 'execute')], ws, sys.executable,
84 self.wrapper_directory, arguments=[
85 self.options['dcrond_binary'].strip(), '-s', cron_d, '-c', crontabs,
86 '-t', timestamps, '-f', '-l', '5', '-M', catcher]
87 )[0]
88 self.path_list.append(wrapper)
89 return cron_d