135 lines
3.0 KiB
Go
135 lines
3.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
cli "github.com/urfave/cli/v2"
|
|
|
|
"aws-mgmt/pkg/client"
|
|
)
|
|
|
|
const hibernateDesc = `Hibernate an EC2 instance.
|
|
|
|
The TIME string is the number of seconds to delay the hibernation, and defaults
|
|
to 10. "now" is an alias for 0.
|
|
|
|
If run on an EC2 instance with an appropriate IAM EC2 role, the instance will
|
|
be hibernated. Required IAM permissions include:
|
|
- ec2:StopInstances
|
|
|
|
Alternatively, an instance ID must be specified: that instance will be
|
|
hibernated. Required IAM permissions include:
|
|
- ec2:StopInstances
|
|
`
|
|
|
|
var HibernateApp = &cli.App{
|
|
Name: "hibernate",
|
|
Usage: "hibernate an instance",
|
|
UsageText: "hibernate [options] [TIME]",
|
|
HideHelpCommand: true,
|
|
Args: true,
|
|
ArgsUsage: "[TIME]",
|
|
Action: hibernateFunc,
|
|
Description: hibernateDesc,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "instance-id",
|
|
Value: "",
|
|
Usage: "set the instance id, defaults to the instance running this command",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "loglevel",
|
|
Value: "error",
|
|
Usage: "set the loglevel: debug, info, error",
|
|
},
|
|
},
|
|
}
|
|
|
|
var HibernateCommand = &cli.Command{
|
|
Name: "hibernate",
|
|
Usage: "hibernate an instance",
|
|
UsageText: "hibernate [options] [TIME]",
|
|
HideHelpCommand: true,
|
|
Args: true,
|
|
ArgsUsage: "[TIME]",
|
|
Action: hibernateFunc,
|
|
Description: hibernateDesc,
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "instance-id",
|
|
Value: "",
|
|
Usage: "set the instance id, defaults to the instance running this command",
|
|
},
|
|
&cli.StringFlag{
|
|
Name: "loglevel",
|
|
Value: "error",
|
|
Usage: "set the loglevel: debug, info, error",
|
|
},
|
|
},
|
|
}
|
|
|
|
func hibernateFunc(ctx *cli.Context) error {
|
|
|
|
// create logger
|
|
log, err := createLogger(ctx.String("loglevel"))
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create a logger, %w", err)
|
|
}
|
|
|
|
// set timer for shutdown
|
|
timer, err := setTimer(ctx.Args().Get(0))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Debug("timer is set", "timer", timer)
|
|
|
|
// create aws config
|
|
cfg, err := client.CreateAWSConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
awsClient := client.NewAWSClient(
|
|
client.SetConfig(cfg),
|
|
)
|
|
|
|
// determine instance id
|
|
instance_id := ctx.String("instance-id")
|
|
if instance_id == "" {
|
|
instance_id, err = awsClient.GetInstanceID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
log.Debug("instance id is set", "instance_id", instance_id)
|
|
|
|
log.Info("waiting to hibernate", "timer", timer)
|
|
time.Sleep(timer)
|
|
|
|
// stop instance
|
|
state, err := awsClient.StopInstance(instance_id, true)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to stop instance, %w", err)
|
|
}
|
|
log.Info("hibernating instance", "instance_id", instance_id, "state", state)
|
|
|
|
return nil
|
|
}
|
|
|
|
func setTimer(t string) (time.Duration, error) {
|
|
var err error
|
|
timer := time.Duration(10 * time.Second)
|
|
if t != "" {
|
|
if t == "now" {
|
|
timer = time.Duration(0)
|
|
} else {
|
|
timer, err = time.ParseDuration(t + "s")
|
|
if err != nil {
|
|
return timer, fmt.Errorf("unable to set timer, %w", err)
|
|
}
|
|
}
|
|
}
|
|
return timer, nil
|
|
}
|