/**
 * Copyright 2020 Opstrace, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import fs from "fs" ; import { call , race , delay } from "redux-saga/effects" ; import createSagaMiddleware from "redux-saga" ; import { createStore , applyMiddleware } from "redux" ; import { smErrorLastResort } from "./util" ; import * as ucc from "./ucc" ; import * as cli from "./index" ; import { Dict , SECOND } from "@opstrace/utils" ; import { setClusterConfig , LatestClusterConfigType , LatestAWSInfraConfigType , LatestGCPInfraConfigType } from "@opstrace/config" ; import { ClusterCreateConfigInterface , setCreateConfig , waitUntilHTTPEndpointsAreReachable } from "@opstrace/installer" ; import * as schemas from "./schemas" ; function readTenantApiTokenFiles ( tenantNames ) { const tenantApiTokens = { } ; const tnames = [ ... tenantNames ] ; tnames . push ( "system" ) ; for ( const tname of tnames ) { const fpath = `tenant-api-token-${ tname }` ; const token = fs . readFileSync ( fpath ) ; tenantApiTokens [ tname ] = token . toString ( ) ; } return tenantApiTokens ; } /**
 * Simple function that checks if Loki and Cortex are up and running
 * in all tenants.
 */ async function checkClusterStatus ( ) { const [ userClusterConfig , infraConfigAWS , infraConfigGCP ] = await ucc . uccGetAndValidate ( cli . CLIARGS . clusterConfigFilePath , cli . CLIARGS . cloudProvider ) ; const ccfg = { ... userClusterConfig , ... { aws : infraConfigAWS , gcp : infraConfigGCP , cloud_provider : cli . CLIARGS . cloudProvider , cluster_name : cli . CLIARGS . instanceName , tenant_api_authenticator_pubkey_set_json : "" } } ; setClusterConfig ( ccfg ) ; const tenantApiTokens = readTenantApiTokenFiles ( ccfg . tenants ) ; const createConfig = { holdController : cli . CLIARGS . holdController , tenantApiTokens : tenantApiTokens , kubeconfigFilePath : "" } ; setCreateConfig ( createConfig ) ; await waitUntilHTTPEndpointsAreReachable ( ccfg ) ; } function * rootTaskStatusCore ( ) { const { timeout } = yield race ( { status : call ( checkClusterStatus ) , timeout : delay ( 300 * SECOND ) } ) ; if ( timeout ) { throw new Error ( "timeout checking the status of the cluster" ) ; } } function * rootTaskStatus ( ) { yield call ( rootTaskStatusCore ) ; } const reducer = ( state = 0 ) => { return state ; } ; export async function status ( ) { const sm = createSagaMiddleware ( { onError : smErrorLastResort } ) ; createStore ( reducer , applyMiddleware ( sm ) ) ; await sm . run ( rootTaskStatus ) . toPromise ( ) ; }