blob: 823c6f154ade3c76a334e027edc3ad11612a58de [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001from django.core.management.base import BaseCommand, CommandError
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002from django.db import transaction
Patrick Williamsc0f7c042017-02-23 20:41:17 -06003
4from django.core.management import call_command
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005from bldcontrol.bbcontroller import getBuildEnvironmentController, ShellCmdException
6from bldcontrol.models import BuildRequest, BuildEnvironment, BRError
Patrick Williamsc0f7c042017-02-23 20:41:17 -06007from orm.models import ToasterSetting, Build, Layer
8
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009import os
Patrick Williamsf1e5d692016-03-30 15:21:19 -050010import traceback
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011import warnings
12
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013
14def DN(path):
15 if path is None:
16 return ""
17 else:
18 return os.path.dirname(path)
19
20
Brad Bishopd7bf8c12018-02-25 22:55:05 -050021class Command(BaseCommand):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022 args = ""
23 help = "Verifies that the configured settings are valid and usable, or prompts the user to fix the settings."
24
25 def __init__(self, *args, **kwargs):
26 super(Command, self).__init__(*args, **kwargs)
27 self.guesspath = DN(DN(DN(DN(DN(DN(DN(__file__)))))))
28
Patrick Williamsc124f4f2015-09-15 14:41:29 -050029 def _verify_build_environment(self):
Patrick Williamsf1e5d692016-03-30 15:21:19 -050030 # provide a local build env. This will be extended later to include non local
31 if BuildEnvironment.objects.count() == 0:
32 BuildEnvironment.objects.create(betype=BuildEnvironment.TYPE_LOCAL)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
34 # we make sure we have builddir and sourcedir for all defined build envionments
35 for be in BuildEnvironment.objects.all():
36 be.needs_import = False
37 def _verify_be():
38 is_changed = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039
40 def _update_sourcedir():
Patrick Williamsf1e5d692016-03-30 15:21:19 -050041 be.sourcedir = os.environ.get('TOASTER_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042 return True
43
44 if len(be.sourcedir) == 0:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 is_changed = _update_sourcedir()
46
47 if not be.sourcedir.startswith("/"):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060048 print("\n -- Validation: The layers checkout directory must be set to an absolute path.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 is_changed = _update_sourcedir()
50
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 if is_changed:
52 if be.betype == BuildEnvironment.TYPE_LOCAL:
53 be.needs_import = True
54 return True
55
56 def _update_builddir():
Patrick Williamsf1e5d692016-03-30 15:21:19 -050057 be.builddir = os.environ.get('TOASTER_DIR')+"/build"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 return True
59
60 if len(be.builddir) == 0:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 is_changed = _update_builddir()
62
63 if not be.builddir.startswith("/"):
Patrick Williamsc0f7c042017-02-23 20:41:17 -060064 print("\n -- Validation: The build directory must to be set to an absolute path.")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 is_changed = _update_builddir()
66
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067 if is_changed:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060068 print("\nBuild configuration saved")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 be.save()
70 return True
71
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 if be.needs_import:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050073 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060074 print("Loading default settings")
75 call_command("loaddata", "settings")
76 template_conf = os.environ.get("TEMPLATECONF", "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077
Brad Bishopd7bf8c12018-02-25 22:55:05 -050078 if ToasterSetting.objects.filter(name='CUSTOM_XML_ONLY').count() > 0:
79 # only use the custom settings
80 pass
81 elif "poky" in template_conf:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060082 print("Loading poky configuration")
83 call_command("loaddata", "poky")
84 else:
85 print("Loading OE-Core configuration")
86 call_command("loaddata", "oe-core")
87 if template_conf:
88 oe_core_path = os.path.realpath(
89 template_conf +
90 "/../")
91 else:
92 print("TEMPLATECONF not found. You may have to"
93 " manually configure layer paths")
94 oe_core_path = input("Please enter the path of"
95 " your openembedded-core "
96 "layer: ")
97 # Update the layer instances of openemebedded-core
98 for layer in Layer.objects.filter(
99 name="openembedded-core",
100 local_source_dir="OE-CORE-LAYER-DIR"):
101 layer.local_path = oe_core_path
102 layer.save()
103
104 # Import the custom fixture if it's present
105 with warnings.catch_warnings():
106 warnings.filterwarnings(
107 action="ignore",
108 message="^.*No fixture named.*$")
109 print("Importing custom settings if present")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400110 try:
111 call_command("loaddata", "custom")
112 except:
113 print("NOTE: optional fixture 'custom' not found")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600114
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500115 # we run lsupdates after config update
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600116 print("\nFetching information from the layer index, "
117 "please wait.\nYou can re-update any time later "
118 "by running bitbake/lib/toaster/manage.py "
119 "lsupdates\n")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500120 call_command("lsupdates")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500122 # we don't look for any other config files
123 return is_changed
124 except Exception as e:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600125 print("Failure while trying to setup toaster: %s"
126 % e)
127 traceback.print_exc()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128
129 return is_changed
130
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500131 while _verify_be():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 pass
133 return 0
134
135 def _verify_default_settings(self):
136 # verify that default settings are there
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500137 if ToasterSetting.objects.filter(name='DEFAULT_RELEASE').count() != 1:
138 ToasterSetting.objects.filter(name='DEFAULT_RELEASE').delete()
139 ToasterSetting.objects.get_or_create(name='DEFAULT_RELEASE', value='')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 return 0
141
142 def _verify_builds_in_progress(self):
143 # we are just starting up. we must not have any builds in progress, or build environments taken
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500144 for b in BuildRequest.objects.filter(state=BuildRequest.REQ_INPROGRESS):
145 BRError.objects.create(req=b, errtype="toaster",
146 errmsg=
147 "Toaster found this build IN PROGRESS while Toaster started up. This is an inconsistent state, and the build was marked as failed")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500149 BuildRequest.objects.filter(state=BuildRequest.REQ_INPROGRESS).update(state=BuildRequest.REQ_FAILED)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500151 BuildEnvironment.objects.update(lock=BuildEnvironment.LOCK_FREE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152
153 # also mark "In Progress builds as failures"
154 from django.utils import timezone
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500155 Build.objects.filter(outcome=Build.IN_PROGRESS).update(outcome=Build.FAILED, completed_on=timezone.now())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156
157 return 0
158
159
160
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500161 def handle(self, **options):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500162 retval = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 retval += self._verify_build_environment()
164 retval += self._verify_default_settings()
165 retval += self._verify_builds_in_progress()
166
167 return retval