|
| 1 | +/* |
| 2 | +Copyright © 2020 Mmadu Manasseh <[email protected]> |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "log" |
| 22 | + "os" |
| 23 | + "unsafe" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + |
| 27 | + homedir "github.com/mitchellh/go-homedir" |
| 28 | + "github.com/spf13/viper" |
| 29 | + |
| 30 | + "github.com/mensaah/reka/config" |
| 31 | + "github.com/mensaah/reka/provider" |
| 32 | + "github.com/mensaah/reka/provider/aws" |
| 33 | + "github.com/mensaah/reka/resource" |
| 34 | + "github.com/mensaah/reka/rules" |
| 35 | +) |
| 36 | + |
| 37 | +var ( |
| 38 | + cfgFile string |
| 39 | + providers []*provider.Provider |
| 40 | +) |
| 41 | + |
| 42 | +// rootCmd represents the base command when called without any subcommands |
| 43 | +var rootCmd = &cobra.Command{ |
| 44 | + Use: "reka", |
| 45 | + Short: "Run Reka using config file (default $HOME/.reka.yml)", |
| 46 | + Long: `A Cloud Infrastructure Management Tool to stop, resume, clean and destroy resources based on tags.`, |
| 47 | + // Uncomment the following line if your bare application |
| 48 | + // has an action associated with it: |
| 49 | + Run: func(cmd *cobra.Command, args []string) { |
| 50 | + refreshResources(providers) |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 55 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 56 | +func Execute() { |
| 57 | + if err := rootCmd.Execute(); err != nil { |
| 58 | + fmt.Println(err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func init() { |
| 64 | + cobra.OnInitialize(initConfig) |
| 65 | + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.reka.yaml)") |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +// initConfig reads in config file and ENV variables if set. |
| 70 | +func initConfig() { |
| 71 | + if cfgFile != "" { |
| 72 | + // Use config file from the flag. |
| 73 | + viper.SetConfigFile(cfgFile) |
| 74 | + } else { |
| 75 | + // Find home directory. |
| 76 | + home, err := homedir.Dir() |
| 77 | + if err != nil { |
| 78 | + fmt.Println(err) |
| 79 | + os.Exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + // Search config in home directory with name ".reka" (without extension). |
| 83 | + viper.AddConfigPath(home) |
| 84 | + viper.SetConfigName(".reka") |
| 85 | + } |
| 86 | + |
| 87 | + viper.AutomaticEnv() // read in environment variables that match |
| 88 | + |
| 89 | + // If a config file is found, read it in. |
| 90 | + if err := viper.ReadInConfig(); err == nil { |
| 91 | + fmt.Println("Using config file:", viper.ConfigFileUsed()) |
| 92 | + } |
| 93 | + |
| 94 | + // Load Config and Defaults |
| 95 | + config.LoadConfig() |
| 96 | + cfg := config.GetConfig() |
| 97 | + |
| 98 | + for _, rule := range cfg.Rules { |
| 99 | + // Convert Rule in config to rules.Rule type |
| 100 | + r := *((*rules.Rule)(unsafe.Pointer(&rule))) |
| 101 | + r.Tags = resource.Tags(rule.Tags) |
| 102 | + rules.ParseRule(r) |
| 103 | + } |
| 104 | + |
| 105 | + // Initialize Provider objects |
| 106 | + providers = initProviders() |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +// TODO Add logger to Providers during configuration |
| 111 | +func initProviders() []*provider.Provider { |
| 112 | + var providers []*provider.Provider |
| 113 | + for _, p := range config.GetProviders() { |
| 114 | + var ( |
| 115 | + provider *provider.Provider |
| 116 | + err error |
| 117 | + ) |
| 118 | + switch p { |
| 119 | + case aws.GetName(): |
| 120 | + provider, err = aws.NewProvider() |
| 121 | + if err != nil { |
| 122 | + log.Fatal("Could not initialize AWS Provider: ", err) |
| 123 | + } |
| 124 | + } |
| 125 | + // TODO Config providers |
| 126 | + providers = append(providers, provider) |
| 127 | + } |
| 128 | + return providers |
| 129 | +} |
| 130 | + |
| 131 | +// Refresh current status of resources from Providers |
| 132 | +func refreshResources(providers []*provider.Provider) { |
| 133 | + for _, provider := range providers { |
| 134 | + allResources := provider.GetAllResources() |
| 135 | + s, err := json.MarshalIndent(allResources, "", "\t") |
| 136 | + if err != nil { |
| 137 | + fmt.Println(err) |
| 138 | + } |
| 139 | + fmt.Println(string(s)) |
| 140 | + |
| 141 | + // stoppableResources := provider.GetStoppableResources(allResources) |
| 142 | + // fmt.Println("Stoppable Resources: ", stoppableResources) |
| 143 | + // errs := provider.StopResources(stoppableResources) |
| 144 | + // fmt.Println("Errors Stopping Resources: ", errs) |
| 145 | + |
| 146 | + // resumableResources := provider.GetResumableResources(allResources) |
| 147 | + // fmt.Println("Resumable Resources: ", resumableResources) |
| 148 | + // errs = provider.ResumeResources(resumableResources) |
| 149 | + // fmt.Println("Errors Resuming Resources: ", errs) |
| 150 | + |
| 151 | + // destroyableResources := provider.GetDestroyableResources(allResources) |
| 152 | + // fmt.Println("Destroyable Resources: ", destroyableResources) |
| 153 | + // errs = provider.DestroyResources(destroyableResources) |
| 154 | + // fmt.Println("Errors Destroying Resources: ", errs) |
| 155 | + } |
| 156 | +} |
0 commit comments