Allow make deploy using token (#79)

* Allow make deploy using token

Introduce support for `MM_ADMIN_TOKEN` as an alternative to defining `MM_ADMIN_USERNAME` and `MM_ADMIN_PASSWORD.

* reverse README.md authentication schemes

* wrap errors

* Update build/deploy/main.go

Co-Authored-By: Ben Schumacher <ben.schumacher@mattermost.com>

* fix linting

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
This commit is contained in:
Jesse Hallam 2020-02-08 11:14:26 -04:00 committed by GitHub
parent adf353748e
commit 282a8cf227
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View file

@ -34,7 +34,7 @@ This will produce a single plugin file (with support for multiple architectures)
dist/com.example.my-plugin.tar.gz
```
There is a build target to automate deploying and enabling the plugin to your server, but it requires configuration and [http](https://httpie.org/) to be installed:
There is a build target to automate deploying and enabling the plugin to your server, but it requires login credentials:
```
export MM_SERVICESETTINGS_SITEURL=http://localhost:8065
export MM_ADMIN_USERNAME=admin
@ -42,6 +42,13 @@ export MM_ADMIN_PASSWORD=password
make deploy
```
or configuration of a [personal access token](https://docs.mattermost.com/developer/personal-access-tokens.html):
```
export MM_SERVICESETTINGS_SITEURL=http://localhost:8065
export MM_ADMIN_TOKEN=j44acwd8obn78cdcx7koid4jkr
make deploy
```
Alternatively, if you are running your `mattermost-server` out of a sibling directory by the same name, use the `deploy` target alone to unpack the files into the right directory. You will need to restart your server and manually enable your plugin.
In production, deploy and upload your plugin via the [System Console](https://about.mattermost.com/default-plugin-uploads).

View file

@ -34,18 +34,38 @@ func deploy() error {
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 != "" && adminUsername != "" && adminPassword != "" {
return uploadPlugin(pluginID, bundlePath, siteURL, adminUsername, adminPassword)
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: %s", adminUsername, resp.Error.Error())
}
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.Errorf("Failed to stat %s", copyTargetDirectory)
return errors.Wrapf(err, "failed to stat %s", copyTargetDirectory)
}
log.Printf("Installing plugin to mattermost-server found in %s.", copyTargetDirectory)
@ -55,14 +75,7 @@ func deploy() error {
// uploadPlugin attempts to upload and enable a plugin via the Client4 API.
// It will fail if plugin uploads are disabled.
func uploadPlugin(pluginID, bundlePath, siteURL, adminUsername, adminPassword string) error {
client := model.NewAPIv4Client(siteURL)
log.Printf("Authenticating as %s against %s.", adminUsername, siteURL)
_, resp := client.Login(adminUsername, adminPassword)
if resp.Error != nil {
return fmt.Errorf("Failed to login as %s: %s", adminUsername, resp.Error.Error())
}
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)
@ -70,7 +83,7 @@ func uploadPlugin(pluginID, bundlePath, siteURL, adminUsername, adminPassword st
defer pluginBundle.Close()
log.Print("Uploading plugin via API.")
_, resp = client.UploadPluginForced(pluginBundle)
_, resp := client.UploadPluginForced(pluginBundle)
if resp.Error != nil {
return fmt.Errorf("Failed to upload plugin bundle: %s", resp.Error.Error())
}