blob: 14298d9dafcb5eb60930229871a2d8cf214c0b0d [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", "")
Brad Bishop5dd7cbb2018-09-05 22:26:40 -070077 custom_xml_only = os.environ.get("CUSTOM_XML_ONLY")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078
Brad Bishop5dd7cbb2018-09-05 22:26:40 -070079 if ToasterSetting.objects.filter(name='CUSTOM_XML_ONLY').count() > 0 or (not custom_xml_only == None):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050080 # only use the custom settings
81 pass
82 elif "poky" in template_conf:
Patrick Williamsc0f7c042017-02-23 20:41:17 -060083 print("Loading poky configuration")
84 call_command("loaddata", "poky")
85 else:
86 print("Loading OE-Core configuration")
87 call_command("loaddata", "oe-core")
88 if template_conf:
89 oe_core_path = os.path.realpath(
90 template_conf +
91 "/../")
92 else:
93 print("TEMPLATECONF not found. You may have to"
94 " manually configure layer paths")
95 oe_core_path = input("Please enter the path of"
96 " your openembedded-core "
97 "layer: ")
98 # Update the layer instances of openemebedded-core
99 for layer in Layer.objects.filter(
100 name="openembedded-core",
101 local_source_dir="OE-CORE-LAYER-DIR"):
102 layer.local_path = oe_core_path
103 layer.save()
104
105 # Import the custom fixture if it's present
106 with warnings.catch_warnings():
107 warnings.filterwarnings(
108 action="ignore",
109 message="^.*No fixture named.*$")
110 print("Importing custom settings if present")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400111 try:
112 call_command("loaddata", "custom")
113 except:
114 print("NOTE: optional fixture 'custom' not found")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600115
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500116 # we run lsupdates after config update
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600117 print("\nFetching information from the layer index, "
118 "please wait.\nYou can re-update any time later "
119 "by running bitbake/lib/toaster/manage.py "
120 "lsupdates\n")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500121 call_command("lsupdates")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500123 # we don't look for any other config files
124 return is_changed
125 except Exception as e:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600126 print("Failure while trying to setup toaster: %s"
127 % e)
128 traceback.print_exc()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129
130 return is_changed
131
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500132 while _verify_be():
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133 pass
134 return 0
135
136 def _verify_default_settings(self):
137 # verify that default settings are there
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500138 if ToasterSetting.objects.filter(name='DEFAULT_RELEASE').count() != 1:
139 ToasterSetting.objects.filter(name='DEFAULT_RELEASE').delete()
140 ToasterSetting.objects.get_or_create(name='DEFAULT_RELEASE', value='')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 return 0
142
143 def _verify_builds_in_progress(self):
144 # we are just starting up. we must not have any builds in progress, or build environments taken
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500145 for b in BuildRequest.objects.filter(state=BuildRequest.REQ_INPROGRESS):
146 BRError.objects.create(req=b, errtype="toaster",
147 errmsg=
148 "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 -0500149
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500150 BuildRequest.objects.filter(state=BuildRequest.REQ_INPROGRESS).update(state=BuildRequest.REQ_FAILED)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500152 BuildEnvironment.objects.update(lock=BuildEnvironment.LOCK_FREE)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153
154 # also mark "In Progress builds as failures"
155 from django.utils import timezone
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500156 Build.objects.filter(outcome=Build.IN_PROGRESS).update(outcome=Build.FAILED, completed_on=timezone.now())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157
158 return 0
159
160
161
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500162 def handle(self, **options):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163 retval = 0
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164 retval += self._verify_build_environment()
165 retval += self._verify_default_settings()
166 retval += self._verify_builds_in_progress()
167
168 return retval