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 .
* /
2017-12-03 12:41:23 -08:00
package cmd
import (
2018-11-20 20:11:21 -08:00
"time"
2018-11-09 15:58:26 -08:00
2019-03-19 15:27:20 -07:00
"github.com/paypal/gorealis/v2/gen-go/apache/aurora"
"github.com/spf13/cobra"
2017-12-03 12:41:23 -08:00
)
2019-03-25 11:38:17 -07:00
var stopMaintenanceConfig = MonitorCmdConfig { }
2017-12-03 12:41:23 -08:00
func init ( ) {
rootCmd . AddCommand ( stopCmd )
// Stop subcommands
2019-03-25 11:38:17 -07:00
stopCmd . AddCommand ( stopMaintCmd . cmd )
stopMaintCmd . cmd . Run = endMaintenance
stopMaintCmd . cmd . Flags ( ) . DurationVar ( & stopMaintenanceConfig . monitorInterval , "interval" , time . Second * 5 , "Interval at which to poll scheduler." )
stopMaintCmd . cmd . Flags ( ) . DurationVar ( & stopMaintenanceConfig . monitorTimeout , "timeout" , time . Minute * 1 , "Time after which the monitor will stop polling and throw an error." )
2018-09-16 21:23:36 -07:00
// Stop update
stopCmd . AddCommand ( stopUpdateCmd )
stopUpdateCmd . Flags ( ) . StringVarP ( env , "environment" , "e" , "" , "Aurora Environment" )
stopUpdateCmd . Flags ( ) . StringVarP ( role , "role" , "r" , "" , "Aurora Role" )
stopUpdateCmd . Flags ( ) . StringVarP ( name , "name" , "n" , "" , "Aurora Name" )
2017-12-03 12:41:23 -08:00
}
var stopCmd = & cobra . Command {
Use : "stop" ,
Short : "Stop a service or maintenance on a host (DRAIN)." ,
}
2019-03-25 11:38:17 -07:00
var stopMaintCmd = MonitorCmdConfig {
cmd : & cobra . Command {
Use : "drain [space separated host list]" ,
Short : "Stop maintenance on a host (move to NONE)." ,
Long : ` Transition a list of hosts currently in a maintenance status out of it. ` ,
} ,
2017-12-03 12:41:23 -08:00
}
2018-09-16 21:23:36 -07:00
var stopUpdateCmd = & cobra . Command {
Use : "update [update ID]" ,
Short : "Stop update" ,
Long : ` To be written. ` ,
Run : stopUpdate ,
}
2017-12-03 12:41:23 -08:00
func endMaintenance ( cmd * cobra . Command , args [ ] string ) {
2018-11-09 15:58:26 -08:00
log . Println ( "Setting hosts to NONE maintenance status." )
log . Println ( args )
2018-12-27 11:31:51 -08:00
result , err := client . EndMaintenance ( args ... )
2017-12-03 12:41:23 -08:00
if err != nil {
2018-11-09 15:58:26 -08:00
log . Fatalf ( "error: %+v\n" , err )
2017-12-03 12:41:23 -08:00
}
2018-11-09 15:58:26 -08:00
log . Debugln ( result )
2017-12-03 12:41:23 -08:00
// Monitor change to NONE mode
2018-12-27 11:31:51 -08:00
hostResult , err := client . HostMaintenanceMonitor (
2017-12-03 12:41:23 -08:00
args ,
[ ] aurora . MaintenanceMode { aurora . MaintenanceMode_NONE } ,
2019-03-25 11:38:17 -07:00
stopMaintenanceConfig . monitorInterval ,
stopMaintenanceConfig . monitorTimeout )
2017-12-03 12:41:23 -08:00
2019-03-19 15:28:24 -07:00
maintenanceMonitorPrint ( hostResult , [ ] aurora . MaintenanceMode { aurora . MaintenanceMode_NONE } )
2018-11-16 21:54:18 -08:00
if err != nil {
log . Fatalln ( "error: %+v" , err )
2018-11-09 15:58:26 -08:00
}
2017-12-03 12:41:23 -08:00
}
2018-09-16 21:23:36 -07:00
func stopUpdate ( cmd * cobra . Command , args [ ] string ) {
if len ( args ) != 1 {
2018-11-09 15:58:26 -08:00
log . Fatalln ( "Only a single update ID must be provided." )
2018-09-16 21:23:36 -07:00
}
2018-11-09 15:58:26 -08:00
log . Infof ( "Stopping (aborting) update [%s/%s/%s] %s\n" , * env , * role , * name , args [ 0 ] )
2018-09-16 21:23:36 -07:00
2018-12-27 11:31:51 -08:00
err := client . AbortJobUpdate ( aurora . JobUpdateKey {
2018-09-16 21:23:36 -07:00
Job : & aurora . JobKey { Environment : * env , Role : * role , Name : * name } ,
ID : args [ 0 ] ,
} ,
"" )
if err != nil {
2018-11-09 15:58:26 -08:00
log . Fatalln ( err )
2018-09-16 21:23:36 -07:00
}
}