/
stop.go
54 lines (50 loc) · 1.64 KB
/
stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package plex
import (
"encoding/json"
"fmt"
"strconv"
sess "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
ebsService "github.com/eschizoid/flixctl/aws/ebs"
ec2Service "github.com/eschizoid/flixctl/aws/ec2"
snapService "github.com/eschizoid/flixctl/aws/snapshot"
slackService "github.com/eschizoid/flixctl/slack/plex"
"github.com/spf13/cobra"
)
var StopPlexCmd = &cobra.Command{
Use: "stop",
Short: "To Stop Plex",
Long: "to stop the Plex Media Server.",
Run: func(cmd *cobra.Command, args []string) {
shutdownCh := make(chan struct{})
go Indicator(shutdownCh)
Stop(slackNotification)
close(shutdownCh)
m := make(map[string]string)
m["plex_status"] = "stopped"
jsonString, _ := json.Marshal(m)
fmt.Println("\n" + string(jsonString))
},
}
func Stop(slackNotification string) {
var awsSession = sess.Must(sess.NewSessionWithOptions(sess.Options{
SharedConfigState: sess.SharedConfigEnable,
}))
svc := ec2.New(awsSession, awsSession.Config)
var instanceID = ec2Service.FetchInstanceID(svc, awsResourceTagNameValue)
var status = ec2Service.Status(svc, instanceID)
if status == Ec2StoppedStatus {
if notify, _ := strconv.ParseBool(slackNotification); notify {
slackService.SendStatus("stopped", slackIncomingHookURL)
}
return
}
var oldVolumeID = ebsService.FetchVolumeID(svc, awsResourceTagNameValue)
snapService.Create(svc, oldVolumeID, awsResourceTagNameValue)
ec2Service.Stop(svc, instanceID)
ebsService.Detach(svc, oldVolumeID)
ebsService.Delete(svc, oldVolumeID)
if notify, _ := strconv.ParseBool(slackNotification); notify {
slackService.SendStatus("stopped", slackIncomingHookURL)
}
}