github.com/ystia/yorc/v4@v4.3.0/plugin/client.go (about)

     1  // Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package plugin
    16  
    17  import (
    18  	"os"
    19  	"os/exec"
    20  
    21  	hclog "github.com/hashicorp/go-hclog"
    22  	gplugin "github.com/hashicorp/go-plugin"
    23  	"github.com/ystia/yorc/v4/log"
    24  )
    25  
    26  // NewClient returns a properly configured plugin client for a given plugin path
    27  func NewClient(pluginPath string) *gplugin.Client {
    28  
    29  	// Filtering logs coming from plugin according to Yorc parent process log level
    30  	var logLevel hclog.Level
    31  	if log.IsDebug() {
    32  		logLevel = hclog.Debug
    33  	} else {
    34  		logLevel = hclog.Info
    35  	}
    36  
    37  	// Create an hclog.Logger to be able to infer plugin logs level
    38  	logger := hclog.New(&hclog.LoggerOptions{
    39  		Output: os.Stdout,
    40  		Level:  logLevel,
    41  		// Using the same time format as standard logs, instead of hclog default
    42  		// format which is a version of RFC3339
    43  		TimeFormat: "2006/01/02 15:04:05",
    44  	})
    45  
    46  	// Setup RPC communication
    47  	SetupPluginCommunication()
    48  
    49  	return gplugin.NewClient(&gplugin.ClientConfig{
    50  		HandshakeConfig: HandshakeConfig,
    51  		Plugins:         getPlugins(nil),
    52  		Cmd:             exec.Command(pluginPath),
    53  		Logger:          logger,
    54  	})
    55  }