-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
192 lines (169 loc) · 5.86 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/mcastellin/aws-fail-az/awsapis"
"github.com/mcastellin/aws-fail-az/cmd"
"github.com/spf13/cobra"
)
// BuildVersion for this application
var BuildVersion string
var (
awsRegion string
awsProfile string
stdin bool
namespace string
resourceType string
resourceKey string
resourceStateData string
)
var rootCmd = &cobra.Command{
Use: "aws-fail-az",
Short: "aws-fail-az is an AWS utility to simulate Availability Zone failure",
}
var failCmd = &cobra.Command{
Use: "fail [CONFIG_FILE]",
Short: "Start AZ failure injection based on the provided configuration from stdin",
RunE: func(_ *cobra.Command, args []string) error {
if !stdin && len(args) != 1 {
return fmt.Errorf("Only one fault configuration file should be provided. Found %d.", len(args))
} else if stdin && len(args) > 0 {
return fmt.Errorf("Configuration files are not supported when reading from stdin. Found %d.", len(args))
}
configFile := ""
if !stdin {
configFile = args[0]
}
provider, err := createProvider()
if err != nil {
return err
}
op := &cmd.FailCommand{
Provider: provider,
Namespace: namespace,
ReadFromStdin: stdin,
ConfigFile: configFile,
}
return op.Run()
},
}
var recoverCmd = &cobra.Command{
Use: "recover",
Short: "Recover from AZ failure and restore saved resources state",
RunE: func(_ *cobra.Command, args []string) error {
provider, err := createProvider()
if err != nil {
return err
}
op := &cmd.RecoverCommand{Provider: provider, Namespace: namespace}
return op.Run()
},
}
var stateSaveCmd = &cobra.Command{
Use: "state-save",
Short: "Store a state object in Dynamodb",
RunE: func(_ *cobra.Command, args []string) error {
if stdin && len(resourceStateData) > 0 {
return fmt.Errorf("State files are not supported when reading from stdin. Found %d.", len(args))
}
provider, err := createProvider()
if err != nil {
return err
}
op := &cmd.SaveStateCommand{
Provider: provider,
Namespace: namespace,
ResourceType: resourceType,
ResourceKey: resourceKey,
ReadFromStdin: stdin,
StateData: resourceStateData,
}
return op.Run()
},
}
var stateReadCmd = &cobra.Command{
Use: "state-read",
Short: "Read a state object from Dynamodb",
RunE: func(_ *cobra.Command, args []string) error {
provider, err := createProvider()
if err != nil {
return err
}
op := &cmd.ReadStatesCommand{
Provider: provider,
Namespace: namespace,
ResourceType: resourceType,
ResourceKey: resourceKey,
}
return op.Run()
},
}
var stateDeleteCmd = &cobra.Command{
Use: "state-delete",
Short: "Delete a state object from Dynamodb",
RunE: func(_ *cobra.Command, args []string) error {
provider, err := createProvider()
if err != nil {
return err
}
op := &cmd.DeleteStateCommand{
Provider: provider,
Namespace: namespace,
ResourceType: resourceType,
ResourceKey: resourceKey,
}
return op.Run()
},
}
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the command version",
Run: func(_ *cobra.Command, args []string) {
fmt.Printf("aws-fail-az v%s\n", BuildVersion)
},
}
func createProvider() (awsapis.AWSProvider, error) {
config.WithSharedConfigProfile("devlearnops")
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithSharedConfigProfile(awsProfile),
config.WithRegion(awsRegion))
if err != nil {
return nil, fmt.Errorf("Failed to load AWS configuration: %v", err)
}
return awsapis.NewProviderFromConfig(&cfg), nil
}
func main() {
failCmd.Flags().StringVar(&namespace, "ns", "", "The namespace assigned to this operation. Used to uniquely identify resources state for recovery.")
failCmd.Flags().BoolVar(&stdin, "stdin", false, "Read fail configuration from stdin.")
recoverCmd.Flags().StringVar(&namespace, "ns", "", "The namespace assigned to this operation. Used to uniquely identify resources state for recovery.")
stateSaveCmd.Flags().StringVar(&namespace, "ns", "", "The namespace assigned to this operation. Used to uniquely identify resources state for recovery.")
stateSaveCmd.Flags().StringVar(&resourceType, "type", "", "The type of resource state to store")
stateSaveCmd.Flags().StringVar(&resourceKey, "key", "", "A unique key to identify this resource")
stateSaveCmd.Flags().StringVar(&resourceStateData, "data", "", "The payload for the resource state as a string value")
stateSaveCmd.Flags().BoolVar(&stdin, "stdin", false, "Read resource state from stdin.")
stateSaveCmd.MarkFlagRequired("type")
stateSaveCmd.MarkFlagRequired("key")
stateReadCmd.Flags().StringVar(&namespace, "ns", "", "The namespace assigned to this operation. Used to uniquely identify resources state for recovery.")
stateReadCmd.Flags().StringVar(&resourceType, "type", "", "Filter states by resource type")
stateReadCmd.Flags().StringVar(&resourceKey, "key", "", "Filter states by resource key")
stateDeleteCmd.Flags().StringVar(&namespace, "ns", "", "The namespace assigned to this operation. Used to uniquely identify resources state for recovery.")
stateDeleteCmd.Flags().StringVar(&resourceType, "type", "", "Filter states by resource type")
stateDeleteCmd.Flags().StringVar(&resourceKey, "key", "", "Filter states by resource key")
stateDeleteCmd.MarkFlagRequired("type")
stateDeleteCmd.MarkFlagRequired("key")
rootCmd.PersistentFlags().StringVar(&awsRegion, "region", "", "The AWS region")
rootCmd.PersistentFlags().StringVar(&awsProfile, "profile", "", "The AWS profile")
rootCmd.AddCommand(failCmd)
rootCmd.AddCommand(recoverCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(stateSaveCmd)
rootCmd.AddCommand(stateReadCmd)
rootCmd.AddCommand(stateDeleteCmd)
rootCmd.SilenceUsage = true
rootCmd.SilenceErrors = true
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}