2019-03-25 17:33:21 -07:00
/ * *
* 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 .
* /
2019-03-22 20:50:41 -07:00
package cmd
import (
"strings"
"time"
2020-02-19 12:18:50 -08:00
"github.com/aurora-scheduler/gorealis/v2/gen-go/apache/aurora"
2019-03-22 20:50:41 -07:00
"github.com/spf13/cobra"
)
func init ( ) {
rootCmd . AddCommand ( monitorCmd )
2019-03-25 11:38:17 -07:00
monitorCmd . AddCommand ( monitorHostCmd . cmd )
monitorHostCmd . cmd . Run = monitorHost
monitorHostCmd . cmd . Flags ( ) . DurationVar ( & monitorHostCmd . monitorInterval , "interval" , time . Second * 5 , "Interval at which to poll scheduler." )
monitorHostCmd . cmd . Flags ( ) . DurationVar ( & monitorHostCmd . monitorTimeout , "timeout" , time . Minute * 10 , "Time after which the monitor will stop polling and throw an error." )
monitorHostCmd . cmd . Flags ( ) . StringSliceVar ( & monitorHostCmd . statusList , "statuses" , [ ] string { aurora . MaintenanceMode_DRAINED . String ( ) } , "List of acceptable statuses for a host to be in. (case-insensitive) [NONE, SCHEDULED, DRAINED, DRAINING]" )
2019-03-22 20:50:41 -07:00
}
var monitorCmd = & cobra . Command {
Use : "monitor" ,
Short : "Watch for a specific state change" ,
}
2019-06-28 10:21:51 -07:00
var monitorHostCmd = monitorCmdConfig {
2019-03-25 11:38:17 -07:00
cmd : & cobra . Command {
Use : "hosts" ,
Short : "Watch a host maintenance status until it enters one of the desired statuses." ,
Long : ` Provide a list of hosts to monitor for desired statuses . Statuses may be passed using the -- statuses
2019-03-22 20:50:41 -07:00
flag with a list of comma separated statuses . Statuses include [ NONE , SCHEDULED , DRAINED , DRAINING ] ` ,
} ,
2019-03-25 11:38:17 -07:00
statusList : make ( [ ] string , 0 ) ,
2019-03-22 20:50:41 -07:00
}
func monitorHost ( cmd * cobra . Command , args [ ] string ) {
maintenanceModes := make ( [ ] aurora . MaintenanceMode , 0 )
2019-03-25 11:38:17 -07:00
for _ , status := range monitorHostCmd . statusList {
2019-03-22 20:50:41 -07:00
mode , err := aurora . MaintenanceModeFromString ( strings . ToUpper ( status ) )
if err != nil {
log . Fatal ( err )
}
maintenanceModes = append ( maintenanceModes , mode )
}
2019-03-25 11:38:17 -07:00
log . Infof ( "Monitoring for %v at %v intervals" , monitorHostCmd . monitorTimeout , monitorHostCmd . monitorInterval )
2020-02-07 18:41:59 -08:00
hostResult , err := client . MonitorHostMaintenance ( args , maintenanceModes , monitorHostCmd . monitorInterval , monitorHostCmd . monitorTimeout )
2019-03-22 20:50:41 -07:00
maintenanceMonitorPrint ( hostResult , maintenanceModes )
2019-03-25 11:38:17 -07:00
2019-03-22 20:50:41 -07:00
if err != nil {
2019-03-25 11:38:17 -07:00
log . Fatal ( err )
2019-03-22 20:50:41 -07:00
}
}