Skip to content

Commit

Permalink
feat: new subcmd deploy process models
Browse files Browse the repository at this point in the history
Add new sub command to deploy process models, either specified by the user
via processModelPath or if not given it default process models BPMN and DMN
are be deployed.

The user is able to specify the count of versions which should be deployed for each.
This allows to experiment with deployment distribution.
  • Loading branch information
ChrisKujawa committed Nov 18, 2022
1 parent d9d8bdd commit 2bc08dd
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
81 changes: 81 additions & 0 deletions go-chaos/cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2022 Camunda Services GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
)

func init() {
rootCmd.AddCommand(deployCmd)

deployCmd.AddCommand(deployProcessModelCmd)

deployProcessModelCmd.Flags().StringVar(&processModelPath, "processModelPath", "",
"Specify the path to a BPMN process model, which should be deployed.")
deployProcessModelCmd.Flags().IntVar(&multipleVersions, "multipleVersions", 10,
"Specify how many different versions of a default BPMN and DMN model should be deployed. Useful for testing deployment distribution.")

deployProcessModelCmd.MarkFlagsMutuallyExclusive("processModelPath", "multipleVersions")
}

var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy certain resource",
Long: `Deploy certain resource, like process model(s) or kubernetes manifest.`,
}

var deployProcessModelCmd = &cobra.Command{
Use: "process",
Short: "Deploy a process model to Zeebe",
Long: `Deploy a process model to Zeebe.
Can be used to deploy a specific process model or multiple version of a default BPMN and DMN model.
Defaults to the later, which is useful for experimenting with deployment distribution.`,
Run: func(cmd *cobra.Command, args []string) {
k8Client, err := internal.CreateK8Client()
if err != nil {
panic(err)
}

port := 26500
closeFn := k8Client.MustGatewayPortForward(port, port)
defer closeFn()

zbClient, err := internal.CreateZeebeClient(port)
if err != nil {
panic(err.Error())
}
defer zbClient.Close()

if len(processModelPath) == 0 {
// deploy multi version
err := internal.DeployDifferentVersions(zbClient, int32(multipleVersions))
if err != nil {
panic(err.Error())
}
fmt.Printf("Deployed different process models of different types and versions to zeebe!\n")
} else {
processDefinitionKey, err := internal.DeployModel(zbClient, processModelPath)
if err != nil {
panic(err.Error())
}

fmt.Printf("Deployed given process model %s, under key %d!\n", processModelPath, processDefinitionKey)
}
},
}
4 changes: 3 additions & 1 deletion go-chaos/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package cmd

import (
"fmt"
"github.com/camunda/zeebe/clients/go/v8/pkg/zbc"
"os"

"github.com/camunda/zeebe/clients/go/v8/pkg/zbc"

"github.com/spf13/cobra"
"github.com/zeebe-io/zeebe-chaos/go-chaos/internal"
)
Expand All @@ -28,6 +29,7 @@ var (
role string
nodeId int
processModelPath string
multipleVersions int
variables string
msgName string
awaitResult bool
Expand Down
66 changes: 66 additions & 0 deletions go-chaos/internal/zeebe.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,72 @@ func DeployModel(client zbc.Client, fileName string) (int64, error) {
return processDefinitionKey, nil
}

type models struct {
bpmnBytes []byte
bpmnFileName string
dmnBytes []byte
dmnFileName string
}

func readModels(bpmnFileName string, dmnFileName string) (*models, error) {
bpmnBytes, err := bpmnContent.ReadFile(bpmnFileName)
if err != nil {
return nil, err
}

dmnBytes, err := bpmnContent.ReadFile(dmnFileName)
if err != nil {
return nil, err
}

return &models{bpmnBytes: bpmnBytes, dmnBytes: dmnBytes, bpmnFileName: bpmnFileName, dmnFileName: dmnFileName}, nil
}

func deployModels(client zbc.Client, models *models) error {
_, err :=
client.NewDeployResourceCommand().AddResource(models.bpmnBytes, models.bpmnFileName).AddResource(models.dmnBytes, models.dmnFileName).Send(context.TODO())
if err != nil {
return err
}
return nil
}

func DeployDifferentVersions(client zbc.Client, versions int32) error {
firstModel, err := readModels("bpmn/multi-version/multiVersionModel.bpmn", "bpmn/multi-version/fancyDecision.dmn")
if err != nil {
return err
}

secondModel, err := readModels("bpmn/multi-version/multiVersionModel_v2.bpmn", "bpmn/multi-version/fancyDecision_v2.dmn")
if err != nil {
return err
}

if Verbosity {
fmt.Printf("Deploy %d versions of different type of models.\n", versions)
}

count := int32(0)
for count < versions {
err := deployModels(client, firstModel)
if err != nil {
return err
}

err = deployModels(client, secondModel)
if err != nil {
return err
}

count += 2
if Verbosity {
fmt.Printf("Deployed [%d/%d] versions.\n", count, versions)
}
}

return nil
}

// if file not exist we read our default BPMN process model and return the content
func readBPMNFileOrDefault(fileName string) ([]byte, string, error) {
var bpmnBytes []byte
Expand Down

0 comments on commit 2bc08dd

Please sign in to comment.