File size: 6,476 Bytes
99caad5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""One command gates tests, creates a clean public demo and publishes to the authorized Space."""
import argparse,hashlib,json,os,shutil,subprocess,sys,tempfile,time,uuid
from pathlib import Path
BASE=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(BASE))
def run(args,env=None):subprocess.run(args,cwd=BASE,env=env,check=True)
def verify():
    for task in [['unit','KnowledgeService/tests'],['AgentRuntime/tests/test_runtime.py'],['LLMGateway/tests/test_gateway.py'],
                 ['unit','LLMGateway/tests'],['unit','ContextService/tests'],['unit','WorkspaceService/tests'],['APIGateway/tests/registry_contract.py']]:
        run([sys.executable,'-X','utf8','tests/run_checks.py',*task])
    for folder in ['AdminWeb','EmployeeWeb']:
        for p in (BASE/folder).glob('*.js'):run(['node','--check',str(p)])
    scratch=BASE/'.test-data';scratch.mkdir(exist_ok=True)
    with tempfile.TemporaryDirectory(prefix='workos-schema-',dir=scratch) as d:
        env={**os.environ,'WORKOS_WORKSPACE_DB_URL':'sqlite+pysqlite:///'+(Path(d)/'schema.db').as_posix()}
        run([sys.executable,'-m','alembic','-c','WorkspaceService/alembic.ini','upgrade','head'],env)
        run([sys.executable,'-m','alembic','-c','WorkspaceService/alembic.ini','check'],env)
def load_config(path):
    if path:
        for line in Path(path).read_text(encoding='utf-8').splitlines():
            if line.strip() and not line.startswith('#') and '=' in line:
                k,v=line.split('=',1)
                if k in {'HF_TOKEN','HF_SPACE_ID'}:os.environ[k]=v.strip()
def build(target):
    from deploy.build_space import build_space
    return build_space(BASE,target)
def publish(folder,repo):
    from huggingface_hub import HfApi
    from huggingface_hub.errors import RepositoryNotFoundError
    api=HfApi(endpoint='https://huggingface.co',token=os.environ.get('HF_TOKEN') or None)
    print('Publish step: authenticate Hugging Face token',flush=True)
    account=api.whoami()['name']
    if account!='dellweb3' or repo!='dellweb3/workos-office':raise ValueError('Publish target/account differs from the authorized CI/CD configuration')
    print('Publish step: read target Space',flush=True)
    try:old=api.repo_info(repo,repo_type='space')
    except RepositoryNotFoundError:
        api.create_repo(repo,repo_type='space',space_sdk='docker',private=False,exist_ok=False);old=api.repo_info(repo,repo_type='space')
    if old.sdk not in {'docker',None}:raise ValueError('Refusing to overwrite a non-Docker Space')
    print('Publish step: upload tested files',flush=True)
    result=api.upload_folder(repo_id=repo,repo_type='space',folder_path=folder,commit_message='Work OS validated demo '+os.getenv('GITHUB_SHA','local-v0.8')[:12],parent_commit=old.sha)
    record={'space_id':repo,'account':account,'previous_commit':old.sha,'commit':result.oid,'url':'https://huggingface.co/spaces/'+repo,'status':'uploaded_waiting_for_build'}
    (BASE/'deploy/last-publication.json').write_text(json.dumps(record,ensure_ascii=False,indent=2),encoding='utf-8')
    print(json.dumps(record,ensure_ascii=False),flush=True)
    # Keep logs limited to state changes; never print token-bearing requests or raw exceptions.
    stage=None
    for _ in range(90):
        runtime=api.get_space_runtime(repo)
        current=getattr(runtime.stage,'value',str(runtime.stage))
        if current!=stage:stage=current;print('Space state: '+stage,flush=True)
        if stage=='RUNNING':
            import httpx
            url=api.repo_info(repo,repo_type='space').host
            url=url if url.startswith('http') else 'https://'+url
            response=httpx.get(url+'/health/live',timeout=30,follow_redirects=False)
            expected=json.loads((Path(folder)/'deployment-manifest.json').read_text())['build_id']
            if response.status_code==200 and response.json().get('mode') in {'public-isolated-fixture','public-isolated-real'} and response.json().get('build_id')==expected:
                run([sys.executable,'-X','utf8',str(BASE/'deploy/smoke_space.py'),url,'--out',str(BASE/'deploy/last-smoke.json'),'--mode',response.json()['mode'].removeprefix('public-isolated-')])
                record.update(status='healthy',app_url=url,smoke='passed');return record
        if stage in {'BUILD_ERROR','RUNTIME_ERROR','CONFIG_ERROR'}:raise RuntimeError('Space build/runtime failed; inspect Space logs. Prior revision recorded for rollback.')
        time.sleep(10)
    raise RuntimeError('Space build wait exceeded 15 minutes; uploaded commit is recorded above, inspect its build status before retrying.')
def main():
    parser=argparse.ArgumentParser();parser.add_argument('command',choices=['verify','build','publish']);parser.add_argument('--config');parser.add_argument('--out');parser.add_argument('--skip-tests',action='store_true',help='Only for an already verified CI build');args=parser.parse_args()
    load_config(args.config)
    if args.command=='verify':verify();return
    if args.command=='publish' and not args.skip_tests:verify()
    target=Path(args.out).resolve() if args.out else BASE.parent/'.workos-builds'/uuid.uuid4().hex
    manifest=build(target)
    if args.command=='build':print(json.dumps(manifest,ensure_ascii=False));return
    record=publish(str(target),os.environ.get('HF_SPACE_ID','dellweb3/workos-office'))
    (BASE/'deploy/last-publication.json').write_text(json.dumps(record,ensure_ascii=False,indent=2),encoding='utf-8')
    print(json.dumps(record,ensure_ascii=False))
if __name__=='__main__':
    try:main()
    except Exception as exc:
        # SDK tracebacks may contain headers or response bodies. Report only the class and safe local errors.
        print('DevOps failed: '+type(exc).__name__+(' '+str(exc) if isinstance(exc,(ValueError,RuntimeError,subprocess.CalledProcessError)) else ''),file=sys.stderr)
        response=getattr(exc,'response',None)
        status=getattr(response,'status_code',None)
        if isinstance(status,int):
            print('Hugging Face HTTP status: '+str(status),file=sys.stderr)
            hints={401:'HF_TOKEN was rejected. Check that the saved secret is a valid Hugging Face access token.',403:'Access denied. Check token write permission for dellweb3/workos-office.',429:'Hugging Face rate limit reached; retry later.'}
            if status in hints:print(hints[status],file=sys.stderr)
        sys.exit(1)