|
45 | 45 | deployment_status, |
46 | 46 | ) |
47 | 47 | from .....deployment.kubernetes._util import core_v1_client |
48 | | -from .....deployment.kubernetes.neonvm import PowerState as NeonVMPowerState |
49 | 48 | from .....deployment.kubernetes.neonvm import set_virtualmachine_power_state |
50 | 49 | from .....deployment.kubernetes.volume_clone import ( |
51 | 50 | clone_branch_database_volume, |
|
94 | 93 | from ....settings import get_settings as get_api_settings |
95 | 94 | from .api_keys import api as api_key_api |
96 | 95 | from .auth import api as auth_api |
| 96 | +from .control_tasks import ( |
| 97 | + _CONTROL_TO_POWER_STATE, |
| 98 | + _CONTROL_TRANSITION_INITIAL, |
| 99 | + dispatch_control, |
| 100 | +) |
97 | 101 | from .resize_tasks import dispatch_resize |
98 | 102 | from .tasks import task_api |
99 | 103 |
|
@@ -1889,109 +1893,61 @@ async def resize( |
1889 | 1893 | 404: NotFound, |
1890 | 1894 | } |
1891 | 1895 |
|
1892 | | -_CONTROL_TO_AUTOSCALER_POWERSTATE: dict[str, NeonVMPowerState] = { |
1893 | | - "pause": "Stopped", |
1894 | | - "resume": "Running", |
1895 | | - "start": "Running", |
1896 | | - "stop": "Stopped", |
1897 | | -} |
1898 | | - |
1899 | | -_CONTROL_TRANSITION_INITIAL: dict[str, BranchServiceStatus] = { |
1900 | | - "pause": BranchServiceStatus.PAUSING, |
1901 | | - "resume": BranchServiceStatus.RESUMING, |
1902 | | - "start": BranchServiceStatus.STARTING, |
1903 | | - "stop": BranchServiceStatus.STOPPING, |
1904 | | -} |
1905 | | - |
1906 | | -_CONTROL_TRANSITION_FINAL: dict[str, BranchServiceStatus | None] = { |
1907 | | - "pause": BranchServiceStatus.PAUSED, |
1908 | | - "resume": BranchServiceStatus.STARTING, |
1909 | | - "start": None, |
1910 | | - "stop": BranchServiceStatus.STOPPED, |
1911 | | -} |
1912 | | - |
1913 | 1896 |
|
1914 | 1897 | async def _set_branch_status(session: SessionDep, branch: Branch, status: BranchServiceStatus): |
1915 | 1898 | branch.set_status(status) |
1916 | 1899 | await session.commit() |
1917 | 1900 |
|
1918 | 1901 |
|
1919 | | -async def _set_final_branch_status(session: SessionDep, branch: Branch, action: str) -> None: |
1920 | | - final_status = _CONTROL_TRANSITION_FINAL[action] |
1921 | | - if final_status is None: |
1922 | | - return |
1923 | | - await _set_branch_status(session, branch, final_status) |
1924 | | - |
1925 | | - |
1926 | | -async def _set_autoscaler_power_state(action: str, namespace: str, name: str) -> None: |
1927 | | - power_state = _CONTROL_TO_AUTOSCALER_POWERSTATE.get(action) |
1928 | | - if power_state is None: |
1929 | | - return |
1930 | | - await set_virtualmachine_power_state(namespace, name, power_state) |
1931 | | - |
1932 | | - |
1933 | | -async def _apply_branch_action( |
1934 | | - *, |
1935 | | - action: str, |
1936 | | - autoscaler_namespace: str, |
1937 | | - autoscaler_vm_name: str, |
1938 | | -) -> None: |
1939 | | - await _set_autoscaler_power_state(action, autoscaler_namespace, autoscaler_vm_name) |
1940 | | - |
1941 | | - |
1942 | 1902 | @instance_api.post( |
1943 | 1903 | "/pause", |
1944 | 1904 | name="organizations:projects:branch:pause", |
1945 | | - status_code=204, |
| 1905 | + status_code=202, |
1946 | 1906 | responses=_control_responses, |
1947 | 1907 | ) |
1948 | 1908 | @instance_api.post( |
1949 | 1909 | "/resume", |
1950 | 1910 | name="organizations:projects:branch:resume", |
1951 | | - status_code=204, |
| 1911 | + status_code=202, |
1952 | 1912 | responses=_control_responses, |
1953 | 1913 | ) |
1954 | 1914 | @instance_api.post( |
1955 | 1915 | "/start", |
1956 | 1916 | name="organizations:projects:branch:start", |
1957 | | - status_code=204, |
| 1917 | + status_code=202, |
1958 | 1918 | responses=_control_responses, |
1959 | 1919 | ) |
1960 | 1920 | @instance_api.post( |
1961 | 1921 | "/stop", |
1962 | 1922 | name="organizations:projects:branch:stop", |
1963 | | - status_code=204, |
| 1923 | + status_code=202, |
1964 | 1924 | responses=_control_responses, |
1965 | 1925 | ) |
1966 | 1926 | async def control_branch( |
1967 | 1927 | session: SessionDep, |
1968 | 1928 | request: Request, |
1969 | | - _organization: OrganizationDep, |
1970 | | - _project: ProjectDep, |
| 1929 | + organization: OrganizationDep, |
| 1930 | + project: ProjectDep, |
1971 | 1931 | branch: BranchDep, |
1972 | 1932 | ): |
1973 | 1933 | action = request.scope["route"].name.split(":")[-1] |
1974 | | - assert action in _CONTROL_TO_AUTOSCALER_POWERSTATE |
| 1934 | + assert action in _CONTROL_TO_POWER_STATE |
1975 | 1935 | branch_in_session = await session.merge(branch) |
1976 | | - branch_id = branch_in_session.id |
1977 | | - autoscaler_namespace, autoscaler_vm_name = get_autoscaler_vm_identity(branch_id) |
1978 | 1936 | await _set_branch_status(session, branch_in_session, _CONTROL_TRANSITION_INITIAL[action]) |
1979 | | - try: |
1980 | | - await _apply_branch_action( |
1981 | | - action=action, |
1982 | | - autoscaler_namespace=autoscaler_namespace, |
1983 | | - autoscaler_vm_name=autoscaler_vm_name, |
1984 | | - ) |
1985 | | - except ApiException as e: |
1986 | | - await _set_branch_status(session, branch_in_session, BranchServiceStatus.ERROR) |
1987 | | - status = 404 if e.status == 404 else 400 |
1988 | | - raise HTTPException(status_code=status, detail=e.body or str(e)) from e |
1989 | | - except VelaKubernetesError as e: |
1990 | | - await _set_branch_status(session, branch_in_session, BranchServiceStatus.ERROR) |
1991 | | - raise HTTPException(status_code=500, detail=str(e)) from e |
1992 | | - else: |
1993 | | - await _set_final_branch_status(session, branch_in_session, action) |
1994 | | - return Response(status_code=204) |
| 1937 | + |
| 1938 | + task_id = dispatch_control(str(branch_in_session.id), action) |
| 1939 | + branch_in_session.control_task_id = task_id |
| 1940 | + await session.commit() |
| 1941 | + |
| 1942 | + task_url = url_path_for( |
| 1943 | + request, |
| 1944 | + "organizations:projects:branch:tasks:detail", |
| 1945 | + organization_id=await organization.awaitable_attrs.id, |
| 1946 | + project_id=await project.awaitable_attrs.id, |
| 1947 | + branch_id=await branch_in_session.awaitable_attrs.id, |
| 1948 | + task_id=task_id, |
| 1949 | + ) |
| 1950 | + return Response(status_code=202, headers={"Location": task_url}) |
1995 | 1951 |
|
1996 | 1952 |
|
1997 | 1953 | instance_api.include_router(auth_api, prefix="/auth") |
|
0 commit comments