Various tooling improvements from other plugins (#96)
This commit is contained in:
parent
c557d38b4d
commit
a49d6f6dd4
14 changed files with 693 additions and 201 deletions
|
@ -1,122 +0,0 @@
|
|||
// main handles deployment of the plugin to a development server using either the Client4 API
|
||||
// or by copying the plugin bundle into a sibling mattermost-server/plugin directory.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mholt/archiver/v3"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := deploy()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to deploy: %s\n", err.Error())
|
||||
fmt.Println()
|
||||
fmt.Println("Usage:")
|
||||
fmt.Println(" deploy <plugin id> <bundle path>")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// deploy handles deployment of the plugin to a development server.
|
||||
func deploy() error {
|
||||
if len(os.Args) < 3 {
|
||||
return errors.New("invalid number of arguments")
|
||||
}
|
||||
|
||||
pluginID := os.Args[1]
|
||||
bundlePath := os.Args[2]
|
||||
|
||||
siteURL := os.Getenv("MM_SERVICESETTINGS_SITEURL")
|
||||
adminToken := os.Getenv("MM_ADMIN_TOKEN")
|
||||
adminUsername := os.Getenv("MM_ADMIN_USERNAME")
|
||||
adminPassword := os.Getenv("MM_ADMIN_PASSWORD")
|
||||
copyTargetDirectory, _ := filepath.Abs("../mattermost-server")
|
||||
|
||||
if siteURL != "" {
|
||||
client := model.NewAPIv4Client(siteURL)
|
||||
|
||||
if adminToken != "" {
|
||||
log.Printf("Authenticating using token against %s.", siteURL)
|
||||
client.SetToken(adminToken)
|
||||
|
||||
return uploadPlugin(client, pluginID, bundlePath)
|
||||
}
|
||||
|
||||
if adminUsername != "" && adminPassword != "" {
|
||||
client := model.NewAPIv4Client(siteURL)
|
||||
log.Printf("Authenticating as %s against %s.", adminUsername, siteURL)
|
||||
_, resp := client.Login(adminUsername, adminPassword)
|
||||
if resp.Error != nil {
|
||||
return errors.Wrapf(resp.Error, "failed to login as %s", adminUsername)
|
||||
}
|
||||
|
||||
return uploadPlugin(client, pluginID, bundlePath)
|
||||
}
|
||||
}
|
||||
|
||||
_, err := os.Stat(copyTargetDirectory)
|
||||
if os.IsNotExist(err) {
|
||||
return errors.New("no supported deployment method available, please install plugin manually")
|
||||
} else if err != nil {
|
||||
return errors.Wrapf(err, "failed to stat %s", copyTargetDirectory)
|
||||
}
|
||||
|
||||
log.Printf("Installing plugin to mattermost-server found in %s.", copyTargetDirectory)
|
||||
log.Print("Server restart required to load updated plugin.")
|
||||
return copyPlugin(pluginID, copyTargetDirectory, bundlePath)
|
||||
}
|
||||
|
||||
// uploadPlugin attempts to upload and enable a plugin via the Client4 API.
|
||||
// It will fail if plugin uploads are disabled.
|
||||
func uploadPlugin(client *model.Client4, pluginID, bundlePath string) error {
|
||||
pluginBundle, err := os.Open(bundlePath)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to open %s", bundlePath)
|
||||
}
|
||||
defer pluginBundle.Close()
|
||||
|
||||
log.Print("Uploading plugin via API.")
|
||||
_, resp := client.UploadPluginForced(pluginBundle)
|
||||
if resp.Error != nil {
|
||||
return errors.Wrap(resp.Error, "failed to upload plugin bundle")
|
||||
}
|
||||
|
||||
log.Print("Enabling plugin.")
|
||||
_, resp = client.EnablePlugin(pluginID)
|
||||
if resp.Error != nil {
|
||||
return errors.Wrap(resp.Error, "Failed to enable plugin")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// copyPlugin attempts to install a plugin by copying it to a sibling ../mattermost-server/plugin
|
||||
// directory. A server restart is required before the plugin will start.
|
||||
func copyPlugin(pluginID, targetPath, bundlePath string) error {
|
||||
targetPath = filepath.Join(targetPath, "plugins")
|
||||
|
||||
err := os.MkdirAll(targetPath, 0777)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to create %s", targetPath)
|
||||
}
|
||||
|
||||
existingPluginPath := filepath.Join(targetPath, pluginID)
|
||||
err = os.RemoveAll(existingPluginPath)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to remove existing existing plugin directory %s", existingPluginPath)
|
||||
}
|
||||
|
||||
err = archiver.Unarchive(bundlePath, targetPath)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to unarchive %s into %s", bundlePath, targetPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -127,7 +127,7 @@ func applyManifest(manifest *model.Manifest) error {
|
|||
if err := ioutil.WriteFile(
|
||||
"server/manifest.go",
|
||||
[]byte(fmt.Sprintf(pluginIDGoFileTemplate, manifestStr)),
|
||||
0644,
|
||||
0600,
|
||||
); err != nil {
|
||||
return errors.Wrap(err, "failed to write server/manifest.go")
|
||||
}
|
||||
|
@ -147,7 +147,7 @@ func applyManifest(manifest *model.Manifest) error {
|
|||
if err := ioutil.WriteFile(
|
||||
"webapp/src/manifest.js",
|
||||
[]byte(fmt.Sprintf(pluginIDJSFileTemplate, manifestStr)),
|
||||
0644,
|
||||
0600,
|
||||
); err != nil {
|
||||
return errors.Wrap(err, "failed to open webapp/src/manifest.js")
|
||||
}
|
||||
|
|
147
build/pluginctl/main.go
Normal file
147
build/pluginctl/main.go
Normal file
|
@ -0,0 +1,147 @@
|
|||
// main handles deployment of the plugin to a development server using the Client4 API.
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
)
|
||||
|
||||
const helpText = `
|
||||
Usage:
|
||||
pluginctl deploy <plugin id> <bundle path>
|
||||
pluginctl disable <plugin id>
|
||||
pluginctl enable <plugin id>
|
||||
pluginctl reset <plugin id>
|
||||
`
|
||||
|
||||
func main() {
|
||||
err := pluginctl()
|
||||
if err != nil {
|
||||
fmt.Printf("Failed: %s\n", err.Error())
|
||||
fmt.Print(helpText)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func pluginctl() error {
|
||||
if len(os.Args) < 3 {
|
||||
return errors.New("invalid number of arguments")
|
||||
}
|
||||
|
||||
client, err := getClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch os.Args[1] {
|
||||
case "deploy":
|
||||
if len(os.Args) < 4 {
|
||||
return errors.New("invalid number of arguments")
|
||||
}
|
||||
return deploy(client, os.Args[2], os.Args[3])
|
||||
case "disable":
|
||||
return disablePlugin(client, os.Args[2])
|
||||
case "enable":
|
||||
return enablePlugin(client, os.Args[2])
|
||||
case "reset":
|
||||
return resetPlugin(client, os.Args[2])
|
||||
default:
|
||||
return errors.New("invalid second argument")
|
||||
}
|
||||
}
|
||||
|
||||
func getClient() (*model.Client4, error) {
|
||||
siteURL := os.Getenv("MM_SERVICESETTINGS_SITEURL")
|
||||
adminToken := os.Getenv("MM_ADMIN_TOKEN")
|
||||
adminUsername := os.Getenv("MM_ADMIN_USERNAME")
|
||||
adminPassword := os.Getenv("MM_ADMIN_PASSWORD")
|
||||
|
||||
if siteURL == "" {
|
||||
return nil, errors.New("MM_SERVICESETTINGS_SITEURL is not set")
|
||||
}
|
||||
|
||||
client := model.NewAPIv4Client(siteURL)
|
||||
|
||||
if adminToken != "" {
|
||||
log.Printf("Authenticating using token against %s.", siteURL)
|
||||
client.SetToken(adminToken)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
if adminUsername != "" && adminPassword != "" {
|
||||
client := model.NewAPIv4Client(siteURL)
|
||||
log.Printf("Authenticating as %s against %s.", adminUsername, siteURL)
|
||||
_, resp := client.Login(adminUsername, adminPassword)
|
||||
if resp.Error != nil {
|
||||
return nil, fmt.Errorf("failed to login as %s: %w", adminUsername, resp.Error)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("one of MM_ADMIN_TOKEN or MM_ADMIN_USERNAME/MM_ADMIN_PASSWORD must be defined")
|
||||
}
|
||||
|
||||
// deploy attempts to upload and enable a plugin via the Client4 API.
|
||||
// It will fail if plugin uploads are disabled.
|
||||
func deploy(client *model.Client4, pluginID, bundlePath string) error {
|
||||
pluginBundle, err := os.Open(bundlePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open %s: %w", bundlePath, err)
|
||||
}
|
||||
defer pluginBundle.Close()
|
||||
|
||||
log.Print("Uploading plugin via API.")
|
||||
_, resp := client.UploadPluginForced(pluginBundle)
|
||||
if resp.Error != nil {
|
||||
return fmt.Errorf("failed to upload plugin bundle: %s", resp.Error.Error())
|
||||
}
|
||||
|
||||
log.Print("Enabling plugin.")
|
||||
_, resp = client.EnablePlugin(pluginID)
|
||||
if resp.Error != nil {
|
||||
return fmt.Errorf("failed to enable plugin: %s", resp.Error.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// disablePlugin attempts to disable the plugin via the Client4 API.
|
||||
func disablePlugin(client *model.Client4, pluginID string) error {
|
||||
log.Print("Disabling plugin.")
|
||||
_, resp := client.DisablePlugin(pluginID)
|
||||
if resp.Error != nil {
|
||||
return fmt.Errorf("failed to disable plugin: %w", resp.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// enablePlugin attempts to enable the plugin via the Client4 API.
|
||||
func enablePlugin(client *model.Client4, pluginID string) error {
|
||||
log.Print("Enabling plugin.")
|
||||
_, resp := client.EnablePlugin(pluginID)
|
||||
if resp.Error != nil {
|
||||
return fmt.Errorf("failed to enable plugin: %w", resp.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// resetPlugin attempts to reset the plugin via the Client4 API.
|
||||
func resetPlugin(client *model.Client4, pluginID string) error {
|
||||
err := disablePlugin(client, pluginID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = enablePlugin(client, pluginID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -8,7 +8,7 @@ endif
|
|||
$(shell cd build/manifest && $(GO) build -o ../bin/manifest)
|
||||
|
||||
# Ensure that the deployment tools are compiled. Go's caching makes this quick.
|
||||
$(shell cd build/deploy && $(GO) build -o ../bin/deploy)
|
||||
$(shell cd build/pluginctl && $(GO) build -o ../bin/pluginctl)
|
||||
|
||||
# Extract the plugin id from the manifest.
|
||||
PLUGIN_ID ?= $(shell build/bin/manifest id)
|
||||
|
|
Reference in a new issue