github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/jujud/agent/checkconnection.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent
     5  
     6  import (
     7  	"io"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/agent"
    14  	"github.com/juju/juju/api"
    15  	jujucmd "github.com/juju/juju/cmd"
    16  	"github.com/juju/juju/cmd/jujud/util"
    17  	"github.com/juju/juju/worker/apicaller"
    18  )
    19  
    20  // ConnectFunc connects to the API as the given agent.
    21  type ConnectFunc func(agent.Agent) (io.Closer, error)
    22  
    23  // ConnectAsAgent really connects to the API specified in the agent
    24  // config. It's extracted so tests can pass something else in.
    25  func ConnectAsAgent(a agent.Agent) (io.Closer, error) {
    26  	return apicaller.ScaryConnect(a, api.Open)
    27  }
    28  
    29  type checkConnectionCommand struct {
    30  	cmd.CommandBase
    31  	agentName string
    32  	config    AgentConf
    33  	connect   ConnectFunc
    34  }
    35  
    36  // NewCheckConnectionCommand returns a command that will test
    37  // connecting to the API with details from the agent's config.
    38  func NewCheckConnectionCommand(config AgentConf, connect ConnectFunc) cmd.Command {
    39  	return &checkConnectionCommand{
    40  		config:  config,
    41  		connect: connect,
    42  	}
    43  }
    44  
    45  // Info is part of cmd.Command.
    46  func (c *checkConnectionCommand) Info() *cmd.Info {
    47  	return jujucmd.Info(&cmd.Info{
    48  		Name:    "check-connection",
    49  		Args:    "<agent-name>",
    50  		Purpose: "check connection to the API server for the specified agent",
    51  	})
    52  }
    53  
    54  // Init is part of cmd.Command.
    55  func (c *checkConnectionCommand) Init(args []string) error {
    56  	if len(args) == 0 {
    57  		return &util.FatalError{"agent-name argument is required"}
    58  	}
    59  	agentName, args := args[0], args[1:]
    60  	if err := cmd.CheckEmpty(args); err != nil {
    61  		return err
    62  	}
    63  	tag, err := names.ParseTag(agentName)
    64  	if err != nil {
    65  		return errors.Annotatef(err, "agent-name")
    66  	}
    67  	if tag.Kind() != "machine" && tag.Kind() != "unit" {
    68  		return &util.FatalError{"agent-name must be a machine or unit tag"}
    69  	}
    70  	err = c.config.ReadConfig(agentName)
    71  	if err != nil {
    72  		return errors.Trace(err)
    73  	}
    74  	c.agentName = agentName
    75  	return nil
    76  }
    77  
    78  // Run is part of cmd.Command.
    79  func (c *checkConnectionCommand) Run(ctx *cmd.Context) error {
    80  	conn, err := c.connect(c.config)
    81  	if err != nil {
    82  		return errors.Annotatef(err, "checking connection for %s", c.agentName)
    83  	}
    84  	err = conn.Close()
    85  	if err != nil {
    86  		return errors.Annotatef(err, "closing connection for %s", c.agentName)
    87  	}
    88  	return nil
    89  }