diff --git a/website/ops/publish-cookbook-route.py b/website/ops/publish-cookbook-route.py index af672735..991ee263 100644 --- a/website/ops/publish-cookbook-route.py +++ b/website/ops/publish-cookbook-route.py @@ -10,14 +10,19 @@ IDS={'dunechest_'+name for name in ('ender_chest','speed_hopper_1','speed_hopper def route_content(old): for suffix in (*FILES,'recipe-icons/'): prefix='^~' if suffix.endswith('/') else '=' - route=f'location {prefix} /{suffix} {{\n proxy_pass http://127.0.0.1:3100;\n proxy_set_header Host www.dunerealms.com;\n add_header Cache-Control "no-cache" always;\n}}' matches=list(re.finditer(r'location\s+'+re.escape(prefix)+r'\s*/'+re.escape(suffix)+r'\s*\{[^{}]*\}',old)) - if len(matches)!=1:raise ValueError('Unknown or duplicate cookbook route: '+suffix) + if not matches:continue + if len(matches)!=1:raise ValueError('Duplicate cookbook route: '+suffix) match=matches[0] - if match[0]==route:continue if not re.search(r'alias\s+'+re.escape(LEGACY+suffix)+r'\s*;',match[0]):raise ValueError('Cookbook route is not the verified legacy alias') - old=old[:match.start()]+route+old[match.end():] + old=old[:match.start()]+old[match.end():] return old + +def serving_port(config): + ports={int(p) for p in re.findall(r'proxy_pass\s+http://127\.0\.0\.1:(\d+)',config)} & {3000,3100,3200} + if len(ports)!=1:raise ValueError('Ambiguous current application upstream') + return ports.pop() + def validate_catalogue(payload): recipes=json.loads(payload);found={r['id']:r for r in recipes if r.get('id') in IDS} if set(found)!=IDS or any(len(r.get('grid',[]))!=9 for r in found.values()):raise ValueError('Current built cookbook is incomplete') @@ -25,7 +30,8 @@ def validate_catalogue(payload): def main(): parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--expected-release',required=True);parser.add_argument('--apply',action='store_true');parser.add_argument('--plan-hash');args=parser.parse_args() if not re.fullmatch('[a-f0-9]{40}',args.expected_release) or CURRENT.resolve().name!='ci-'+args.expected_release:raise ValueError('Active built release changed') - request=urllib.request.Request('http://127.0.0.1:3100/recipe-catalogue.json',headers={'Host':'www.dunerealms.com'}) + port=serving_port(Path('/etc/nginx/sites-enabled/dunerealms-migration').read_text()) + request=urllib.request.Request(f'http://127.0.0.1:{port}/recipe-catalogue.json',headers={'Host':'www.dunerealms.com'}) with urllib.request.urlopen(request,timeout=15) as response:payload=response.read(1000000) count=validate_catalogue(payload);old=TARGET.read_text();new=route_content(old);sha=lambda b:hashlib.sha256(b).hexdigest() plan={'release':args.expected_release,'target':str(TARGET),'before':sha(old.encode()),'after':sha(new.encode()),'catalogueSha256':sha(payload),'recipes':count} diff --git a/website/ops/test_cookbook_route.py b/website/ops/test_cookbook_route.py index 383cff2d..9a4c4400 100644 --- a/website/ops/test_cookbook_route.py +++ b/website/ops/test_cookbook_route.py @@ -4,10 +4,13 @@ spec=importlib.util.spec_from_file_location('book',Path(__file__).with_name('pub class CookbookRoute(unittest.TestCase): def original(self):return '\n'.join('location '+('^~' if p.endswith('/') else '=')+' /'+p+' { alias '+book.LEGACY+p+'; }' for p in (*book.FILES,'recipe-icons/'))+'\n'+book.shared.ROUTE def test_routes_entire_cookbook_and_preserves_shared_design(self): - after=book.route_content(self.original());self.assertEqual(after.count('proxy_pass http://127.0.0.1:3100;'),6);self.assertIn(book.shared.ROUTE,after);self.assertEqual(book.route_content(after),after) + after=book.route_content(self.original());self.assertNotIn('proxy_pass',after);self.assertNotIn('recipe-catalogue.json',after);self.assertIn(book.shared.ROUTE,after);self.assertEqual(book.route_content(after),after) def test_unknown_or_duplicate_route_is_refused(self): - for old in ('',self.original().replace(book.LEGACY+'recipe-catalogue.json','/unexpected.json'),self.original()+'\nlocation = /recipe-catalogue.json {}'): + for old in (self.original().replace(book.LEGACY+'recipe-catalogue.json','/unexpected.json'),self.original()+'\nlocation = /recipe-catalogue.json {}'): with self.assertRaises(ValueError):book.route_content(old) + def test_serving_port_tracks_both_promotion_slots(self): + for port in (3100,3200):self.assertEqual(book.serving_port(f'proxy_pass http://127.0.0.1:{port};'),port) + with self.assertRaises(ValueError):book.serving_port('proxy_pass http://127.0.0.1:3100; proxy_pass http://127.0.0.1:3200;') def test_old_catalogue_or_missing_recipe_grid_is_refused(self): recipes=[{'id':key,'grid':[None]*9} for key in book.IDS];self.assertEqual(book.validate_catalogue(json.dumps(recipes)),6) with self.assertRaises(ValueError):book.validate_catalogue('[]')