aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api/system.ts
blob: 794000c0389ae23bd87ac77b3d03ddcfd6290733 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * SonarQube
 * Copyright (C) 2009-2018 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { getJSON, post, postJSON } from '../helpers/request';
import throwGlobalError from '../app/utils/throwGlobalError';

export type SysValue = boolean | string | number | HealthType | SysValueObject | SysValueArray;
export interface SysValueObject {
  [key: string]: SysValue;
}
export interface SysValueArray extends Array<SysValue> {}

export interface SysInfoSection {
  [sectionName: string]: SysValueObject;
}

export enum HealthType {
  RED = 'RED',
  YELLOW = 'YELLOW',
  GREEN = 'GREEN'
}

export interface NodeInfo extends SysValueObject {
  'Compute Engine Logging': { 'Logs Level': string };
  Health: HealthType;
  'Health Causes': string[];
  Name: string;
  'Web Logging': { 'Logs Level': string };
}

export interface SysInfo extends SysValueObject {
  Health: HealthType;
  'Health Causes': string[];
  System: {
    'High Availability': boolean;
    'Logs Level': string;
    'Server ID': string;
  };
}

export interface ClusterSysInfo extends SysInfo {
  'Application Nodes': NodeInfo[];
  'Search Nodes': NodeInfo[];
}

export interface SystemUpgrade {
  version: string;
  description: string;
  releaseDate: string;
  changeLogUrl: string;
  downloadUrl: string;
  plugins: any;
}

export function setLogLevel(level: string): Promise<void | Response> {
  return post('/api/system/change_log_level', { level }).catch(throwGlobalError);
}

export function getSystemInfo(): Promise<SysInfo> {
  return getJSON('/api/system/info').catch(throwGlobalError);
}

export function getSystemStatus(): Promise<any> {
  return getJSON('/api/system/status');
}

export function getSystemUpgrades(): Promise<{
  upgrades: SystemUpgrade[];
  updateCenterRefresh: string;
}> {
  return getJSON('/api/system/upgrades');
}

export function getMigrationStatus(): Promise<any> {
  return getJSON('/api/system/db_migration_status');
}

export function migrateDatabase() {
  return postJSON('/api/system/migrate_db');
}

export function restart(): Promise<void | Response> {
  return post('/api/system/restart').catch(throwGlobalError);
}

const POLLING_INTERVAL = 2000;

function pollStatus(cb: Function): void {
  setTimeout(() => {
    getSystemStatus()
      .then(r => {
        if (r.status === 'UP') {
          cb();
        } else {
          pollStatus(cb);
        }
      })
      .catch(() => pollStatus(cb));
  }, POLLING_INTERVAL);
}

function promiseStatus(): Promise<any> {
  return new Promise(resolve => pollStatus(resolve));
}

export function restartAndWait(): Promise<any> {
  return restart().then(promiseStatus);
}