/**
 * Copyright 2021 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 { log } from "@opstrace/utils" ; import { upgradeCluster , setUpgradeConfig } from "@opstrace/upgrader" ; import { LatestAWSInfraConfigType , LatestClusterConfigType , LatestGCPInfraConfigType , setClusterConfig } from "@opstrace/config" ; import * as cli from "./index" ; import * as util from "./util" ; import * as schemas from "./schemas" ; import * as ucc from "./ucc" ; import { setGcpProjectID } from "@opstrace/installer" ; import { logDockerHubCredentialsMessage } from "@opstrace/controller-config" ; export async function upgrade ( ) { log . info ( `About to upgrade Opstrace instance '${ cli . CLIARGS . instanceName }' (${ cli . CLIARGS . cloudProvider }).` ) ; const [ userClusterConfig , infraConfigAWS , infraConfigGCP ] = await ucc . uccGetAndValidate ( cli . CLIARGS . clusterConfigFilePath , cli . CLIARGS . cloudProvider ) ; const renderedClusterConfig = { ... userClusterConfig , ... { aws : infraConfigAWS , gcp : infraConfigGCP , cloud_provider : cli . CLIARGS . cloudProvider , cluster_name : cli . CLIARGS . instanceName , tenant_api_authenticator_pubkey_set_json : "" } } ; setClusterConfig ( renderedClusterConfig ) ; let gcpProjectID ; let gcpRegion ; if ( cli . CLIARGS . cloudProvider == "gcp" ) { const gcpopts = util . gcpValidateCredFileAndGetDetailOrError ( ) ; gcpProjectID = gcpopts . projectId ; gcpRegion = util . gcpGetClusterRegion ( ) ; log . info ( "GCP project ID: %s" , gcpopts . projectId ) ; log . info ( "GCP service account email notation: %s" , gcpopts . credentials . client_email ) ; setGcpProjectID ( gcpopts . projectId ) ; } let awsRegion ; if ( cli . CLIARGS . cloudProvider == "aws" ) { awsRegion = await util . awsGetClusterRegionWithCmdlineFallback ( ) ; } setUpgradeConfig ( { cloudProvider : cli . CLIARGS . cloudProvider , clusterName : cli . CLIARGS . instanceName , gcpProjectID : gcpProjectID , gcpRegion : gcpRegion , awsRegion : awsRegion } ) ; logDockerHubCredentialsMessage ( "upgrade" ) ; await util . promptForProceed ( ) ; await upgradeCluster ( util . smErrorLastResort ) ; }