github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/autoagents/plugins/plugin.go (about)

     1  // Copyright (c) 2024, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package plugins
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/choria-io/go-choria/aagent/machine"
    11  	"github.com/choria-io/go-choria/aagent/plugin"
    12  	"github.com/choria-io/go-choria/aagent/watchers"
    13  	"github.com/choria-io/go-choria/build"
    14  	"github.com/choria-io/go-choria/config"
    15  )
    16  
    17  func init() {
    18  	bi := &build.Info{}
    19  	bi.RegisterMachine(fmt.Sprintf("Autonomous Agent Plugins Manager version %s", build.Version))
    20  }
    21  
    22  func ChoriaPlugin(cfg *config.Config) (*plugin.MachinePlugin, error) {
    23  	if !cfg.Choria.AutonomousAgentsDownload {
    24  		return nil, fmt.Errorf("autonomous agent plugin management is not enabled")
    25  	}
    26  
    27  	bucket := cfg.Choria.AutonomousAgentsBucket
    28  	key := cfg.Choria.AutonomousAgentsKey
    29  	purgeUnknown := cfg.Choria.AutonomousAgentsPurge
    30  	bucketInterval := cfg.Choria.AutonomousAgentsBucketPollInterval
    31  	checkInterval := cfg.Choria.AutonomousAgentCheckInterval
    32  	pluginsDirectory := cfg.Choria.MachineSourceDir
    33  	publicKey := cfg.Choria.AutonomousAgentPublicKey
    34  
    35  	m := machine.Machine{
    36  		MachineName:    "plugins_manager",
    37  		InitialState:   "INITIAL",
    38  		MachineVersion: build.Version,
    39  		Transitions: []*machine.Transition{
    40  			{
    41  				Name:        "maintenance",
    42  				From:        []string{"MANAGE"},
    43  				Destination: "MAINTENANCE",
    44  				Description: "Stops actively managing plugins",
    45  			},
    46  			{
    47  				Name:        "resume",
    48  				From:        []string{"MAINTENANCE"},
    49  				Destination: "MANAGE",
    50  				Description: "Resume normal operations after being in maintenance mode",
    51  			},
    52  			{
    53  				Name:        "manage_plugins",
    54  				From:        []string{"INITIAL"},
    55  				Destination: "MANAGE",
    56  				Description: "Actively manage plugins",
    57  			},
    58  		},
    59  		WatcherDefs: []*watchers.WatcherDef{
    60  			{
    61  				Name:              "initial_specification",
    62  				Type:              "kv",
    63  				Interval:          bucketInterval,
    64  				StateMatch:        []string{"INITIAL"},
    65  				SuccessTransition: "manage_plugins",
    66  				Properties: map[string]any{
    67  					"bucket":            bucket,
    68  					"key":               key,
    69  					"mode":              "poll",
    70  					"bucket_prefix":     false,
    71  					"on_successful_get": true,
    72  				},
    73  			},
    74  			{
    75  				Name:       "specification",
    76  				Type:       "kv",
    77  				Interval:   bucketInterval,
    78  				StateMatch: []string{"MANAGE"},
    79  				Properties: map[string]any{
    80  					"bucket":        bucket,
    81  					"key":           key,
    82  					"mode":          "poll",
    83  					"bucket_prefix": false,
    84  				},
    85  			},
    86  			{
    87  				Name:       "manage_machines",
    88  				StateMatch: []string{"MANAGE"},
    89  				Type:       "plugins",
    90  				Interval:   checkInterval,
    91  				Properties: map[string]any{
    92  					"data_item":               key,
    93  					"purge_unknown":           purgeUnknown,
    94  					"manager_machine_prefix":  "mm",
    95  					"plugin_manage_interval":  checkInterval,
    96  					"machine_manage_interval": checkInterval,
    97  					"public_key":              publicKey,
    98  					"plugins_directory":       pluginsDirectory,
    99  				},
   100  			},
   101  		},
   102  	}
   103  
   104  	return plugin.NewMachinePlugin(m.MachineName, &m), nil
   105  }