blob: 3ccc7c67c9191a269d26e07af615096f21a8ba26 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001from django.core.management.base import NoArgsCommand, CommandError
2from django.db import transaction
3from bldcontrol.bbcontroller import getBuildEnvironmentController, ShellCmdException
4from bldcontrol.models import BuildRequest, BuildEnvironment, BRError
5from orm.models import ToasterSetting, Build
6import os
7import sys, traceback
8
9def DN(path):
10 if path is None:
11 return ""
12 else:
13 return os.path.dirname(path)
14
15
16class Command(NoArgsCommand):
17 args = ""
18 help = "Verifies that the configured settings are valid and usable, or prompts the user to fix the settings."
19
20 def __init__(self, *args, **kwargs):
21 super(Command, self).__init__(*args, **kwargs)
22 self.guesspath = DN(DN(DN(DN(DN(DN(DN(__file__)))))))
23
24 def _find_first_path_for_file(self, startdirectory, filename, level = 0):
25 if level < 0:
26 return None
27 dirs = []
28 for i in os.listdir(startdirectory):
29 j = os.path.join(startdirectory, i)
30 if os.path.isfile(j):
31 if i == filename:
32 return startdirectory
33 elif os.path.isdir(j):
34 dirs.append(j)
35 for j in dirs:
36 ret = self._find_first_path_for_file(j, filename, level - 1)
37 if ret is not None:
38 return ret
39 return None
40
41 def _recursive_list_directories(self, startdirectory, level = 0):
42 if level < 0:
43 return []
44 dirs = []
45 try:
46 for i in os.listdir(startdirectory):
47 j = os.path.join(startdirectory, i)
48 if os.path.isdir(j):
49 dirs.append(j)
50 except OSError:
51 pass
52 for j in dirs:
53 dirs = dirs + self._recursive_list_directories(j, level - 1)
54 return dirs
55
56
57 def _get_suggested_sourcedir(self, be):
58 if be.betype != BuildEnvironment.TYPE_LOCAL:
59 return ""
60 return DN(DN(DN(self._find_first_path_for_file(self.guesspath, "toasterconf.json", 4))))
61
62 def _get_suggested_builddir(self, be):
63 if be.betype != BuildEnvironment.TYPE_LOCAL:
64 return ""
65 return DN(self._find_first_path_for_file(DN(self.guesspath), "bblayers.conf", 4))
66
67
68 def _verify_artifact_storage_dir(self):
69 # verify that we have a settings for downloading artifacts
70 while ToasterSetting.objects.filter(name="ARTIFACTS_STORAGE_DIR").count() == 0:
71 guessedpath = os.getcwd() + "/toaster_build_artifacts/"
72 print("\nToaster needs to know in which directory it can download build log files and other artifacts.\nToaster suggests \"%s\"." % guessedpath)
73 artifacts_storage_dir = raw_input("Press Enter to select \"%s\" or type the full path to a different directory: " % guessedpath)
74 if len(artifacts_storage_dir) == 0:
75 artifacts_storage_dir = guessedpath
76 if len(artifacts_storage_dir) > 0 and artifacts_storage_dir.startswith("/"):
77 try:
78 os.makedirs(artifacts_storage_dir)
79 except OSError as ose:
80 if "File exists" in str(ose):
81 pass
82 else:
83 raise ose
84 ToasterSetting.objects.create(name="ARTIFACTS_STORAGE_DIR", value=artifacts_storage_dir)
85 return 0
86
87
88 def _verify_build_environment(self):
89 # refuse to start if we have no build environments
90 while BuildEnvironment.objects.count() == 0:
91 print(" !! No build environments found. Toaster needs at least one build environment in order to be able to run builds.\n" +
92 "You can manually define build environments in the database table bldcontrol_buildenvironment.\n" +
93 "Or Toaster can define a simple localhost-based build environment for you.")
94
95 i = raw_input(" -- Do you want to create a basic localhost build environment ? (Y/n) ");
96 if not len(i) or i.startswith("y") or i.startswith("Y"):
97 BuildEnvironment.objects.create(pk = 1, betype = 0)
98 else:
99 raise Exception("Toaster cannot start without build environments. Aborting.")
100
101
102 # we make sure we have builddir and sourcedir for all defined build envionments
103 for be in BuildEnvironment.objects.all():
104 be.needs_import = False
105 def _verify_be():
106 is_changed = False
107 print("\nVerifying the build environment. If the local build environment is not properly configured, you will be asked to configure it.")
108
109 def _update_sourcedir():
110 suggesteddir = self._get_suggested_sourcedir(be)
111 if len(suggesteddir) > 0:
112 be.sourcedir = raw_input("This is the directory Toaster uses to check out the source code of the layers you will build. Toaster will create new clones of the layers, so existing content in the chosen directory will not be changed.\nToaster suggests you use \"%s\" as your layers checkout directory. If you select this directory, a layer like \"meta-intel\" will end up in \"%s/meta-intel\".\nPress Enter to select \"%s\" or type the full path to a different directory. If you provide your own directory, it must be a parent of the cloned directory for the sources you are using to run Toaster: " % (suggesteddir, suggesteddir, suggesteddir))
113 else:
114 be.sourcedir = raw_input("Toaster needs to know in which directory it should check out the source code of the layers you will build. The directory should be a parent of the cloned directory for the sources you are using to run Toaster. Toaster will create new clones of the layers, so existing content in the chosen directory will not be changed.\nType the full path to the directory (for example: \"%s\": " % os.environ.get('HOME', '/tmp/'))
115 if len(be.sourcedir) == 0 and len(suggesteddir) > 0:
116 be.sourcedir = suggesteddir
117 return True
118
119 if len(be.sourcedir) == 0:
120 print "\n -- Validation: The layers checkout directory must be set."
121 is_changed = _update_sourcedir()
122
123 if not be.sourcedir.startswith("/"):
124 print "\n -- Validation: The layers checkout directory must be set to an absolute path."
125 is_changed = _update_sourcedir()
126
127 if not be.sourcedir in DN(__file__):
128 print "\n -- Validation: The layers checkout directory must be a parent of the current checkout."
129 is_changed = _update_sourcedir()
130
131 if is_changed:
132 if be.betype == BuildEnvironment.TYPE_LOCAL:
133 be.needs_import = True
134 return True
135
136 def _update_builddir():
137 suggesteddir = self._get_suggested_builddir(be)
138 if len(suggesteddir) > 0:
139 be.builddir = raw_input("Toaster needs to know where your build directory is located.\nThe build directory is where all the artifacts created by your builds will be stored. Toaster suggests \"%s\".\nPress Enter to select \"%s\" or type the full path to a different directory: " % (suggesteddir, suggesteddir))
140 else:
141 be.builddir = raw_input("Toaster needs to know where is your build directory.\nThe build directory is where all the artifacts created by your builds will be stored. Type the full path to the directory (for example: \" %s/build\")" % os.environ.get('HOME','/tmp/'))
142 if len(be.builddir) == 0 and len(suggesteddir) > 0:
143 be.builddir = suggesteddir
144 return True
145
146 if len(be.builddir) == 0:
147 print "\n -- Validation: The build directory must be set."
148 is_changed = _update_builddir()
149
150 if not be.builddir.startswith("/"):
151 print "\n -- Validation: The build directory must to be set to an absolute path."
152 is_changed = _update_builddir()
153
154
155 if is_changed:
156 print "\nBuild configuration saved"
157 be.save()
158 return True
159
160
161 if be.needs_import:
162 print "\nToaster can use a SINGLE predefined configuration file to set up default project settings and layer information sources.\n"
163
164 # find configuration files
165 config_files = []
166 for dirname in self._recursive_list_directories(be.sourcedir,2):
167 if os.path.exists(os.path.join(dirname, ".templateconf")):
168 import subprocess
169 proc = subprocess.Popen('bash -c ". '+os.path.join(dirname, ".templateconf")+'; echo \"\$TEMPLATECONF\""', shell=True, stdout=subprocess.PIPE)
170 conffilepath, stderroroutput = proc.communicate()
171 proc.wait()
172 if proc.returncode != 0:
173 raise Exception("Failed to source TEMPLATECONF: %s" % stderroroutput)
174
175 conffilepath = os.path.join(conffilepath.strip(), "toasterconf.json")
176 candidatefilepath = os.path.join(dirname, conffilepath)
177 if "toaster_cloned" in candidatefilepath:
178 continue
179 if os.path.exists(candidatefilepath):
180 config_files.append(candidatefilepath)
181
182 if len(config_files) > 0:
183 print "Toaster will list now the configuration files that it found. Select the number to use the desired configuration file."
184 for cf in config_files:
185 print " [%d] - %s" % (config_files.index(cf) + 1, cf)
186 print "\n [0] - Exit without importing any file"
187 try:
188 i = raw_input("\nEnter your option: ")
189 if len(i) and (int(i) - 1 >= 0 and int(i) - 1 < len(config_files)):
190 print "\nImporting file: %s" % config_files[int(i)-1]
191 from loadconf import Command as LoadConfigCommand
192
193 LoadConfigCommand()._import_layer_config(config_files[int(i)-1])
194 # we run lsupdates after config update
195 print "\nLayer configuration imported. Updating information from the layer sources, please wait.\nYou can re-update any time later by running bitbake/lib/toaster/manage.py lsupdates"
196 from django.core.management import call_command
197 call_command("lsupdates")
198
199 # we don't look for any other config files
200 return is_changed
201 except Exception as e:
202 print "Failure while trying to import the toaster config file: %s" % e
203 traceback.print_exc(e)
204 else:
205 print "\nToaster could not find a configuration file. You need to configure Toaster manually using the web interface, or create a configuration file and use\n bitbake/lib/toaster/managepy.py loadconf [filename]\n command to load it. You can use https://wiki.yoctoproject.org/wiki/File:Toasterconf.json.txt.patch as a starting point."
206
207
208
209
210 return is_changed
211
212 while (_verify_be()):
213 pass
214 return 0
215
216 def _verify_default_settings(self):
217 # verify that default settings are there
218 if ToasterSetting.objects.filter(name = 'DEFAULT_RELEASE').count() != 1:
219 ToasterSetting.objects.filter(name = 'DEFAULT_RELEASE').delete()
220 ToasterSetting.objects.get_or_create(name = 'DEFAULT_RELEASE', value = '')
221 return 0
222
223 def _verify_builds_in_progress(self):
224 # we are just starting up. we must not have any builds in progress, or build environments taken
225 for b in BuildRequest.objects.filter(state = BuildRequest.REQ_INPROGRESS):
226 BRError.objects.create(req = b, errtype = "toaster", errmsg = "Toaster found this build IN PROGRESS while Toaster started up. This is an inconsistent state, and the build was marked as failed")
227
228 BuildRequest.objects.filter(state = BuildRequest.REQ_INPROGRESS).update(state = BuildRequest.REQ_FAILED)
229
230 BuildEnvironment.objects.update(lock = BuildEnvironment.LOCK_FREE)
231
232 # also mark "In Progress builds as failures"
233 from django.utils import timezone
234 Build.objects.filter(outcome = Build.IN_PROGRESS).update(outcome = Build.FAILED, completed_on = timezone.now())
235
236 return 0
237
238
239
240 def handle_noargs(self, **options):
241 retval = 0
242 retval += self._verify_artifact_storage_dir()
243 retval += self._verify_build_environment()
244 retval += self._verify_default_settings()
245 retval += self._verify_builds_in_progress()
246
247 return retval