install.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 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. }
  58. cmdInstall.PersistentFlags().BoolVarP(&download_only, "download-only", "d", false, "Only download packages, don't enable")
  59. cmdInstall.PersistentFlags().BoolVar(&force_install, "force", false, "Force install : Overwrite tainted and outdated files")
  60. var cmdInstallParser = &cobra.Command{
  61. Use: "parser [config]",
  62. Short: "Install given log parser",
  63. Long: `Fetch and install given parser from hub`,
  64. Example: `cscli install parser crowdsec/xxx`,
  65. Args: cobra.MinimumNArgs(1),
  66. Run: func(cmd *cobra.Command, args []string) {
  67. if err := cwhub.GetHubIdx(); err != nil {
  68. log.Fatalf("failed to get Hub index : %v", err)
  69. }
  70. InstallItem(args[0], cwhub.PARSERS)
  71. },
  72. }
  73. cmdInstall.AddCommand(cmdInstallParser)
  74. var cmdInstallScenario = &cobra.Command{
  75. Use: "scenario [config]",
  76. Short: "Install given scenario",
  77. Long: `Fetch and install given scenario from hub`,
  78. Example: `cscli install scenario crowdsec/xxx`,
  79. Args: cobra.MinimumNArgs(1),
  80. Run: func(cmd *cobra.Command, args []string) {
  81. if err := cwhub.GetHubIdx(); err != nil {
  82. log.Fatalf("failed to get Hub index : %v", err)
  83. }
  84. InstallItem(args[0], cwhub.SCENARIOS)
  85. },
  86. }
  87. cmdInstall.AddCommand(cmdInstallScenario)
  88. var cmdInstallCollection = &cobra.Command{
  89. Use: "collection [config]",
  90. Short: "Install given collection",
  91. Long: `Fetch and install given collection from hub`,
  92. Example: `cscli install collection crowdsec/xxx`,
  93. Args: cobra.MinimumNArgs(1),
  94. Run: func(cmd *cobra.Command, args []string) {
  95. if err := cwhub.GetHubIdx(); err != nil {
  96. log.Fatalf("failed to get Hub index : %v", err)
  97. }
  98. InstallItem(args[0], cwhub.COLLECTIONS)
  99. },
  100. }
  101. cmdInstall.AddCommand(cmdInstallCollection)
  102. var cmdInstallPostoverflow = &cobra.Command{
  103. Use: "postoverflow [config]",
  104. Short: "Install given postoverflow parser",
  105. Long: `Fetch and install given postoverflow from hub.
  106. As a reminder, postoverflows are parsing configuration that will occur after the overflow (before a decision is applied).`,
  107. Example: `cscli install collection crowdsec/xxx`,
  108. Args: cobra.MinimumNArgs(1),
  109. Run: func(cmd *cobra.Command, args []string) {
  110. if err := cwhub.GetHubIdx(); err != nil {
  111. log.Fatalf("failed to get Hub index : %v", err)
  112. }
  113. InstallItem(args[0], cwhub.PARSERS_OVFLW)
  114. },
  115. }
  116. cmdInstall.AddCommand(cmdInstallPostoverflow)
  117. return cmdInstall
  118. }