uselagoon / lagoon-facts-app

Apache License 2.0
3 stars 1 forks source link

How to integrate DrushConfigGet gather #26

Closed if-nick closed 5 months ago

if-nick commented 1 year ago

Our team wishes to see configuration settings across Drupal sites. The data is used for checking consistency of settings, to identify if sites need a patch or to identify sites having certain features. For example checking that aggregations is enabled and seeing sites' cache max age setting.

I've written the simple gatherers/DrushConfigGatherer.go below. It collects the following in key/ value format.

How can we add this internally for testing? Will f2-branch-* be an issue? If acceptable can it be added to lagoon-facts-app for use on our sites?

Script:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
    // "os"
    "os/exec"
)

const ShellToUse = "bash"

type ConfigItem struct {
    Key   string
    Value string
}

type Configs struct {
    Items []ConfigItem
}

func (configs *Configs) AddItem(item ConfigItem) {
    configs.Items = append(configs.Items, item)
}

func main() {

    configs := Configs{}
    prefix := "" // "cd /home/meyerj/lagoon/joce01; "

    err, result, _ := Shellout(prefix +
        "lando drush config:get system.performance css.preprocess --format=json")
    if err == nil {
        configs.AddItem(ConfigItem{"system.performance:css.preprocess", fmt.Sprint(result["system.performance:css.preprocess"])})
    }

    err, result, _ := Shellout(prefix +
        "lando drush config:get system.performance js.preprocess --format=json")
    if err == nil {
        configs.AddItem(ConfigItem{"system.performance:js.preprocess", fmt.Sprint(result["system.performance:js.preprocess"])})
    }

    err, result, _ = Shellout(prefix +
        "lando drush config:get system.performance cache.page.max_age --format=json")
    if err == nil {
        configs.AddItem(ConfigItem{"system.performance:cache.page.max_age", fmt.Sprint(result["system.performance:cache.page.max_age"])})
    }

    err, result, _ = Shellout(prefix +
        "lando drush config:get smtp.settings smtp_host --format=json")
    if err == nil {
        configs.AddItem(ConfigItem{"smtp.settings:smtp_host", fmt.Sprint(result["smtp.settings:smtp_host"])})
    }

    err, result, _ = Shellout(prefix +
        "lando drush config:get config_ignore.settings ignored_config_entities --format=json")
    if err == nil {
        configs.AddItem(ConfigItem{"config_ignore.settings:ignored_config_entities", fmt.Sprint(result["config_ignore.settings:ignored_config_entities"])})
    }

    err, result, _ = Shellout(prefix +
        "lando drush core:requirements --severity=2 --format=json")
    if err == nil {
        j, _ := json.MarshalIndent(result, "", "    ")
        configs.AddItem(ConfigItem{"core:requirements.severity.2", string(j)})
    }

    data, err := json.MarshalIndent(configs, "", "    ")
    if err == nil {
        log.Printf(string(data))
    }

}

func Shellout(command string) (error, map[string]interface{}, string) {
    var stdout bytes.Buffer
    var stderr bytes.Buffer

    cmd := exec.Command(ShellToUse, "-c", command)
    cmd.Stdout = &stdout
    cmd.Stderr = &stderr

    err := cmd.Run()
    if err != nil {
        log.Printf("Shellout could not process: %v\n\n", command)
    }

    var result map[string]interface{}
    json.Unmarshal([]byte(stdout.String()), &result)

    return err, result, stderr.String()
}

Results:


$ go run hello.go                                                                                                                                     ~/www/lagoon-facts-app
2022/11/08 22:32:50 {
    "Items": [
        {
            "Key": "system.performance:css.preprocess",
            "Value": "true"
        },
        {
            "Key": "system.performance:cache.page.max_age",
            "Value": "86400"
        },
        {
            "Key": "smtp.settings:smtp_host",
            "Value": "email-smtp.us-west-2.amazonaws.com"
        },
        {
            "Key": "smtp.settings:smtp_host",
            "Value": "email-smtp.us-west-2.amazonaws.com"
        },
        {
            "Key": "config_ignore.settings:ignored_config_entities",
            "Value": "[key.* fastly_streamline_access.settings miniorange_saml.settings smtp.settings webform.webform.* google_analytics.settings cloudflare.settings recaptcha.settings]"
        },
        {
            "Key": "core:requirements.severity.2",
            "Value": "{\n    \"cloudflare\": {\n        \"severity\": \"Error\",\n        \"title\": \"CloudFlare - Credentials\",\n        \"value\": \"\"\n    },\n    \"entity_update\": {\n        \"severity\": \"Error\",\n        \"title\": \"Entity/field definitions\",\n        \"value\": \"Mismatched entity and/or field definitions\\n\"\n    },\n    \"purgersavailable\": {\n        \"severity\": \"Error\",\n        \"title\": \"Purge: Purgers\",\n        \"value\": \"\"\n    },\n    \"webform_libraries\": {\n        \"severity\": \"Error\",\n        \"title\": \"Webform: External libraries\",\n        \"value\": \"19 libraries (0 installed; 2 excluded; 17 CDN)\\n\"\n    }\n}"
        }
    ]
}
bomoko commented 1 year ago

Hi @if-nick

We could certainly figure out some way of incorporating something like this into the facts app generally.

We should pick this up in an actual meeting.

bomoko commented 5 months ago

I think this was addressed?