install.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/crowdsecurity/crowdsec/pkg/cwhub"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. )
  8. var download_only, force_install bool
  9. func InstallItem(name string, obtype string) {
  10. for _, it := range cwhub.HubIdx[obtype] {
  11. if it.Name == name {
  12. if download_only && it.Downloaded && it.UpToDate {
  13. log.Warningf("%s is already downloaded and up-to-date", it.Name)
  14. return
  15. }
  16. it, err := cwhub.DownloadLatest(it, cwhub.Hubdir, force_install)
  17. if err != nil {
  18. log.Fatalf("error while downloading %s : %v", it.Name, err)
  19. }
  20. cwhub.HubIdx[obtype][it.Name] = it
  21. if download_only {
  22. log.Infof("Downloaded %s to %s", it.Name, cwhub.Hubdir+"/"+it.RemotePath)
  23. return
  24. }
  25. it, err = cwhub.EnableItem(it, cwhub.Installdir, cwhub.Hubdir)
  26. if err != nil {
  27. log.Fatalf("error while enabled %s : %v.", it.Name, err)
  28. }
  29. cwhub.HubIdx[obtype][it.Name] = it
  30. log.Infof("Enabled %s", it.Name)
  31. return
  32. }
  33. }
  34. log.Warningf("%s not found in hub index", name)
  35. /*iterate of pkg index data*/
  36. }
  37. func InstallScenario(name string) {
  38. InstallItem(name, cwhub.SCENARIOS)
  39. }
  40. func InstallCollection(name string) {
  41. InstallItem(name, cwhub.COLLECTIONS)
  42. }
  43. func InstallParser(name string) {
  44. InstallItem(name, cwhub.PARSERS)
  45. }
  46. func InstallPostoverflow(name string) {
  47. InstallItem(name, cwhub.PARSERS_OVFLW)
  48. }
  49. func NewInstallCmd() *cobra.Command {
  50. /* ---- INSTALL COMMAND */
  51. var cmdInstall = &cobra.Command{
  52. Use: "install [type] [config]",
  53. Short: "Install configuration(s) from hub",
  54. Long: `
  55. Install configuration from the CrowdSec Hub.
  56. In order to download latest versions of configuration,
  57. you should [update cscli](./cscli_update.md).
  58. [type] must be parser, scenario, postoverflow, collection.
  59. [config_name] must be a valid config name from [Crowdsec Hub](https://hub.crowdsec.net).
  60. `,
  61. Example: `cscli install [type] [config_name]`,
  62. Args: cobra.MinimumNArgs(1),
  63. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  64. if !config.configured {
  65. return fmt.Errorf("you must configure cli before interacting with hub")
  66. }
  67. return nil
  68. },
  69. }
  70. cmdInstall.PersistentFlags().BoolVarP(&download_only, "download-only", "d", false, "Only download packages, don't enable")
  71. cmdInstall.PersistentFlags().BoolVar(&force_install, "force", false, "Force install : Overwrite tainted and outdated files")
  72. var cmdInstallParser = &cobra.Command{
  73. Use: "parser [config]",
  74. Short: "Install given log parser",
  75. Long: `Fetch and install given parser from hub`,
  76. Example: `cscli install parser crowdsec/xxx`,
  77. Args: cobra.MinimumNArgs(1),
  78. Run: func(cmd *cobra.Command, args []string) {
  79. if err := cwhub.GetHubIdx(); err != nil {
  80. log.Fatalf("failed to get Hub index : %v", err)
  81. }
  82. InstallItem(args[0], cwhub.PARSERS)
  83. },
  84. }
  85. cmdInstall.AddCommand(cmdInstallParser)
  86. var cmdInstallScenario = &cobra.Command{
  87. Use: "scenario [config]",
  88. Short: "Install given scenario",
  89. Long: `Fetch and install given scenario from hub`,
  90. Example: `cscli install scenario crowdsec/xxx`,
  91. Args: cobra.MinimumNArgs(1),
  92. Run: func(cmd *cobra.Command, args []string) {
  93. if err := cwhub.GetHubIdx(); err != nil {
  94. log.Fatalf("failed to get Hub index : %v", err)
  95. }
  96. InstallItem(args[0], cwhub.SCENARIOS)
  97. },
  98. }
  99. cmdInstall.AddCommand(cmdInstallScenario)
  100. var cmdInstallCollection = &cobra.Command{
  101. Use: "collection [config]",
  102. Short: "Install given collection",
  103. Long: `Fetch and install given collection from hub`,
  104. Example: `cscli install collection crowdsec/xxx`,
  105. Args: cobra.MinimumNArgs(1),
  106. Run: func(cmd *cobra.Command, args []string) {
  107. if err := cwhub.GetHubIdx(); err != nil {
  108. log.Fatalf("failed to get Hub index : %v", err)
  109. }
  110. InstallItem(args[0], cwhub.COLLECTIONS)
  111. },
  112. }
  113. cmdInstall.AddCommand(cmdInstallCollection)
  114. var cmdInstallPostoverflow = &cobra.Command{
  115. Use: "postoverflow [config]",
  116. Short: "Install given postoverflow parser",
  117. Long: `Fetch and install given postoverflow from hub.
  118. As a reminder, postoverflows are parsing configuration that will occur after the overflow (before a decision is applied).`,
  119. Example: `cscli install collection crowdsec/xxx`,
  120. Args: cobra.MinimumNArgs(1),
  121. Run: func(cmd *cobra.Command, args []string) {
  122. if err := cwhub.GetHubIdx(); err != nil {
  123. log.Fatalf("failed to get Hub index : %v", err)
  124. }
  125. InstallItem(args[0], cwhub.PARSERS_OVFLW)
  126. },
  127. }
  128. cmdInstall.AddCommand(cmdInstallPostoverflow)
  129. return cmdInstall
  130. }