install.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, config.DataFolder)
  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 NewInstallCmd() *cobra.Command {
  38. /* ---- INSTALL COMMAND */
  39. var cmdInstall = &cobra.Command{
  40. Use: "install [type] [config]",
  41. Short: "Install configuration(s) from hub",
  42. Long: `
  43. Install configuration from the CrowdSec Hub.
  44. In order to download latest versions of configuration,
  45. you should [update cscli](./cscli_update.md).
  46. [type] must be parser, scenario, postoverflow, collection.
  47. [config_name] must be a valid config name from [Crowdsec Hub](https://hub.crowdsec.net).
  48. `,
  49. Example: `cscli install [type] [config_name]`,
  50. Args: cobra.MinimumNArgs(1),
  51. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  52. if !config.configured {
  53. return fmt.Errorf("you must configure cli before interacting with hub")
  54. }
  55. return nil
  56. },
  57. PersistentPostRun: func(cmd *cobra.Command, args []string) {
  58. log.Infof("Run 'systemctl reload crowdsec' for the new configuration to be effective.")
  59. },
  60. }
  61. cmdInstall.PersistentFlags().BoolVarP(&download_only, "download-only", "d", false, "Only download packages, don't enable")
  62. cmdInstall.PersistentFlags().BoolVar(&force_install, "force", false, "Force install : Overwrite tainted and outdated files")
  63. var cmdInstallParser = &cobra.Command{
  64. Use: "parser [config]",
  65. Short: "Install given parser",
  66. Long: `Fetch and install given parser from hub`,
  67. Example: `cscli install parser crowdsec/xxx`,
  68. Args: cobra.MinimumNArgs(1),
  69. Run: func(cmd *cobra.Command, args []string) {
  70. if err := cwhub.GetHubIdx(); err != nil {
  71. log.Fatalf("failed to get Hub index : %v", err)
  72. }
  73. for _, name := range args {
  74. InstallItem(name, cwhub.PARSERS)
  75. }
  76. },
  77. }
  78. cmdInstall.AddCommand(cmdInstallParser)
  79. var cmdInstallScenario = &cobra.Command{
  80. Use: "scenario [config]",
  81. Short: "Install given scenario",
  82. Long: `Fetch and install given scenario from hub`,
  83. Example: `cscli install scenario crowdsec/xxx`,
  84. Args: cobra.MinimumNArgs(1),
  85. Run: func(cmd *cobra.Command, args []string) {
  86. if err := cwhub.GetHubIdx(); err != nil {
  87. log.Fatalf("failed to get Hub index : %v", err)
  88. }
  89. for _, name := range args {
  90. InstallItem(name, cwhub.SCENARIOS)
  91. }
  92. },
  93. }
  94. cmdInstall.AddCommand(cmdInstallScenario)
  95. var cmdInstallCollection = &cobra.Command{
  96. Use: "collection [config]",
  97. Short: "Install given collection",
  98. Long: `Fetch and install given collection from hub`,
  99. Example: `cscli install collection crowdsec/xxx`,
  100. Args: cobra.MinimumNArgs(1),
  101. Run: func(cmd *cobra.Command, args []string) {
  102. if err := cwhub.GetHubIdx(); err != nil {
  103. log.Fatalf("failed to get Hub index : %v", err)
  104. }
  105. for _, name := range args {
  106. InstallItem(name, cwhub.COLLECTIONS)
  107. }
  108. },
  109. }
  110. cmdInstall.AddCommand(cmdInstallCollection)
  111. var cmdInstallPostoverflow = &cobra.Command{
  112. Use: "postoverflow [config]",
  113. Short: "Install given postoverflow parser",
  114. Long: `Fetch and install given postoverflow from hub.
  115. As a reminder, postoverflows are parsing configuration that will occur after the overflow (before a decision is applied).`,
  116. Example: `cscli install collection crowdsec/xxx`,
  117. Args: cobra.MinimumNArgs(1),
  118. Run: func(cmd *cobra.Command, args []string) {
  119. if err := cwhub.GetHubIdx(); err != nil {
  120. log.Fatalf("failed to get Hub index : %v", err)
  121. }
  122. for _, name := range args {
  123. InstallItem(name, cwhub.PARSERS_OVFLW)
  124. }
  125. },
  126. }
  127. cmdInstall.AddCommand(cmdInstallPostoverflow)
  128. return cmdInstall
  129. }