Two scripts to submit to test report module
[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 class Recipe(BaseSlapRecipe, GenericSlapRecipe):
38 def install(self):
39 self.path_list = []
40 self.crond = self.installCrond()
41
42 slap = slapos.slap.slap()
43 computer_id = self.computer_id
44 computer_partition_id = self.computer_partition_id
45 server_url = self.server_url
46 key_file = self.key_file
47 cert_file = self.cert_file
48 slap.initializeConnection(server_url, key_file, cert_file)
49 self.computer_partition = slap.registerComputerPartition(
50 computer_id, computer_partition_id)
51 self.parameter_dict = self.computer_partition.getInstanceParameterDict()
52
53 configuration_path = os.path.join(self.work_directory, "agent.cfg")
54 configuration = ConfigParser.SafeConfigParser()
55 configuration.add_section("agent")
56 configuration.set("agent", "portal_url", self.parameter_dict["portal_url"])
57 configuration.set("agent", "master_url", self.parameter_dict["master_url"])
58 configuration.set("agent", "report_url", self.parameter_dict["report_url"])
59 key_filepath = os.path.join(self.work_directory, "key")
60 key_file = open(key_filepath, "w")
61 key_file.write(self.parameter_dict["key"])
62 key_file.close()
63 configuration.set("agent", "key_file", key_filepath)
64 cert_filepath = os.path.join(self.work_directory, "cert")
65 cert_file = open(cert_filepath, "w")
66 cert_file.write(self.parameter_dict["cert"])
67 cert_file.close()
68 configuration.set("agent", "cert_file", cert_filepath)
69 configuration.set("agent", "maximum_software_installation_duration",
70 self.parameter_dict["maximum_software_installation_duration"])
71 configuration.set("agent", "software_live_duration",
72 self.parameter_dict["software_live_duration"])
73 configuration.set("agent", "computer_list",
74 self.parameter_dict["computer_list"])
75 configuration.set("agent", "software_list",
76 self.parameter_dict["software_list"])
77 configuration.set("agent", "log_directory", self.options["log_directory"])
78 configuration.set("agent", "state_file", self.options["state_file"])
79 state = open(self.options["state_file"], "w")
80 state.write('')
81 state.close()
82 configuration.set("agent", "path_file", self.options["path_file"])
83 configuration.add_section("software_uri")
84 software_list = json.loads(self.parameter_dict["software_list"])
85 for software in software_list:
86 configuration.set("software_uri", software, self.parameter_dict[software])
87 configuration.write(open(configuration_path, "w"))
88
89 agent_crond_path = os.path.join(self.crond, "agent")
90 agent_crond = open(agent_crond_path, "w")
91 agent_crond.write("*/5 * * * * %s -S %s\n" % \
92 (self.options["python_binary"],
93 "%s --pidfile=%s %s" % \
94 (self.options["agent_binary"], self.options["pidfile"],
95 configuration_path)))
96 agent_crond.write("1 0 * * * %s -S %s %s\n" % \
97 (self.options["python_binary"],
98 self.options["report_start"], configuration_path))
99 agent_crond.write("59 23 * * * %s -S %s %s\n" % \
100 (self.options["python_binary"],
101 self.options["report_stop"], configuration_path))
102 agent_crond.close()
103
104 return self.path_list + [configuration_path, key_filepath, cert_filepath, agent_crond_path]
105
106 def installCrond(self):
107 _, self.ws = self.egg.working_set()
108 timestamps = self.createDataDirectory('cronstamps')
109 cron_output = os.path.join(self.log_directory, 'cron-output')
110 self._createDirectory(cron_output)
111 catcher = zc.buildout.easy_install.scripts([('catchcron',
112 __name__ + '.catdatefile', 'catdatefile')], self.ws, sys.executable,
113 self.bin_directory, arguments=[cron_output])[0]
114 self.path_list.append(catcher)
115 cron_d = os.path.join(self.etc_directory, 'cron.d')
116 crontabs = os.path.join(self.etc_directory, 'crontabs')
117 self._createDirectory(cron_d)
118 self._createDirectory(crontabs)
119 wrapper = zc.buildout.easy_install.scripts([('crond',
120 'slapos.recipe.librecipe.execute', 'execute')], self.ws, sys.executable,
121 self.wrapper_directory, arguments=[
122 self.options['dcrond_binary'].strip(), '-s', cron_d, '-c', crontabs,
123 '-t', timestamps, '-f', '-l', '5', '-M', catcher]
124 )[0]
125 self.path_list.append(wrapper)
126 return cron_d