Allow to ignore errors when installing multiple configuration items (#1359)

* Allow to ignore errors when installing multiple configuration items
This commit is contained in:
AlteredCoder 2022-03-16 17:27:30 +01:00 committed by GitHub
parent 24797c1534
commit b57eb92bbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 10 deletions

View file

@ -45,6 +45,7 @@ func NewCollectionsCmd() *cobra.Command {
},
}
var ignoreError bool
var cmdCollectionsInstall = &cobra.Command{
Use: "install collection",
Short: "Install given collection(s)",
@ -54,12 +55,19 @@ func NewCollectionsCmd() *cobra.Command {
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
for _, name := range args {
InstallItem(name, cwhub.COLLECTIONS, forceAction)
if err := InstallItem(name, cwhub.COLLECTIONS, forceAction); err != nil {
if ignoreError {
log.Errorf("Error while installing '%s': %s", name, err)
} else {
log.Fatalf("Error while installing '%s': %s", name, err)
}
}
}
},
}
cmdCollectionsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdCollectionsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdCollectionsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple collections")
cmdCollections.AddCommand(cmdCollectionsInstall)
var cmdCollectionsRemove = &cobra.Command{

View file

@ -48,6 +48,7 @@ cscli parsers remove crowdsecurity/sshd-logs
},
}
var ignoreError bool
var cmdParsersInstall = &cobra.Command{
Use: "install [config]",
Short: "Install given parser(s)",
@ -57,12 +58,19 @@ cscli parsers remove crowdsecurity/sshd-logs
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
for _, name := range args {
InstallItem(name, cwhub.PARSERS, forceAction)
if err := InstallItem(name, cwhub.PARSERS, forceAction); err != nil {
if ignoreError {
log.Errorf("Error while installing '%s': %s", name, err)
} else {
log.Fatalf("Error while installing '%s': %s", name, err)
}
}
}
},
}
cmdParsersInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdParsersInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdParsersInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple parsers")
cmdParsers.AddCommand(cmdParsersInstall)
var cmdParsersRemove = &cobra.Command{

View file

@ -47,6 +47,7 @@ func NewPostOverflowsCmd() *cobra.Command {
},
}
var ignoreError bool
var cmdPostOverflowsInstall = &cobra.Command{
Use: "install [config]",
Short: "Install given postoverflow(s)",
@ -56,12 +57,19 @@ func NewPostOverflowsCmd() *cobra.Command {
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
for _, name := range args {
InstallItem(name, cwhub.PARSERS_OVFLW, forceAction)
if err := InstallItem(name, cwhub.PARSERS_OVFLW, forceAction); err != nil {
if ignoreError {
log.Errorf("Error while installing '%s': %s", name, err)
} else {
log.Fatalf("Error while installing '%s': %s", name, err)
}
}
}
},
}
cmdPostOverflowsInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdPostOverflowsInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple postoverflows")
cmdPostOverflows.AddCommand(cmdPostOverflowsInstall)
var cmdPostOverflowsRemove = &cobra.Command{

View file

@ -48,6 +48,7 @@ cscli scenarios remove crowdsecurity/ssh-bf
},
}
var ignoreError bool
var cmdScenariosInstall = &cobra.Command{
Use: "install [config]",
Short: "Install given scenario(s)",
@ -57,12 +58,19 @@ cscli scenarios remove crowdsecurity/ssh-bf
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
for _, name := range args {
InstallItem(name, cwhub.SCENARIOS, forceAction)
if err := InstallItem(name, cwhub.SCENARIOS, forceAction); err != nil {
if ignoreError {
log.Errorf("Error while installing '%s': %s", name, err)
} else {
log.Fatalf("Error while installing '%s': %s", name, err)
}
}
}
},
}
cmdScenariosInstall.PersistentFlags().BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
cmdScenariosInstall.PersistentFlags().BoolVar(&forceAction, "force", false, "Force install : Overwrite tainted and outdated files")
cmdScenariosInstall.PersistentFlags().BoolVar(&ignoreError, "ignore", false, "Ignore errors when installing multiple scenarios")
cmdScenarios.AddCommand(cmdScenariosInstall)
var cmdScenariosRemove = &cobra.Command{

View file

@ -178,33 +178,35 @@ func ListItems(itemTypes []string, args []string, showType bool, showHeader bool
}
}
func InstallItem(name string, obtype string, force bool) {
func InstallItem(name string, obtype string, force bool) error {
it := cwhub.GetItem(obtype, name)
if it == nil {
log.Fatalf("unable to retrieve item : %s", name)
return fmt.Errorf("unable to retrieve item : %s", name)
}
item := *it
if downloadOnly && item.Downloaded && item.UpToDate {
log.Warningf("%s is already downloaded and up-to-date", item.Name)
if !force {
return
return nil
}
}
item, err := cwhub.DownloadLatest(csConfig.Hub, item, force, false)
if err != nil {
log.Fatalf("error while downloading %s : %v", item.Name, err)
return fmt.Errorf("error while downloading %s : %v", item.Name, err)
}
cwhub.AddItem(obtype, item)
if downloadOnly {
log.Infof("Downloaded %s to %s", item.Name, csConfig.Hub.HubDir+"/"+item.RemotePath)
return
return nil
}
item, err = cwhub.EnableItem(csConfig.Hub, item)
if err != nil {
log.Fatalf("error while enabling %s : %v.", item.Name, err)
return fmt.Errorf("error while enabling %s : %v.", item.Name, err)
}
cwhub.AddItem(obtype, item)
log.Infof("Enabled %s", item.Name)
return nil
}
func RemoveMany(itemType string, name string) {