github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/service/expose.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package service
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  
    10  	"github.com/juju/juju/api/service"
    11  	"github.com/juju/juju/cmd/juju/block"
    12  	"github.com/juju/juju/cmd/modelcmd"
    13  )
    14  
    15  var usageExposeSummary = `
    16  Makes a service publicly available over the network.`[1:]
    17  
    18  var usageExposeDetails = `
    19  Adjusts the firewall rules and any relevant security mechanisms of the
    20  cloud to allow public access to the service.
    21  
    22  Examples:
    23      juju expose wordpress
    24  
    25  See also: 
    26      unexpose`[1:]
    27  
    28  // NewExposeCommand returns a command to expose services.
    29  func NewExposeCommand() cmd.Command {
    30  	return modelcmd.Wrap(&exposeCommand{})
    31  }
    32  
    33  // exposeCommand is responsible exposing services.
    34  type exposeCommand struct {
    35  	modelcmd.ModelCommandBase
    36  	ServiceName string
    37  }
    38  
    39  func (c *exposeCommand) Info() *cmd.Info {
    40  	return &cmd.Info{
    41  		Name:    "expose",
    42  		Args:    "<service name>",
    43  		Purpose: usageExposeSummary,
    44  		Doc:     usageExposeDetails,
    45  	}
    46  }
    47  
    48  func (c *exposeCommand) Init(args []string) error {
    49  	if len(args) == 0 {
    50  		return errors.New("no service name specified")
    51  	}
    52  	c.ServiceName = args[0]
    53  	return cmd.CheckEmpty(args[1:])
    54  }
    55  
    56  type serviceExposeAPI interface {
    57  	Close() error
    58  	Expose(serviceName string) error
    59  	Unexpose(serviceName string) error
    60  }
    61  
    62  func (c *exposeCommand) getAPI() (serviceExposeAPI, error) {
    63  	root, err := c.NewAPIRoot()
    64  	if err != nil {
    65  		return nil, errors.Trace(err)
    66  	}
    67  	return service.NewClient(root), nil
    68  }
    69  
    70  // Run changes the juju-managed firewall to expose any
    71  // ports that were also explicitly marked by units as open.
    72  func (c *exposeCommand) Run(_ *cmd.Context) error {
    73  	client, err := c.getAPI()
    74  	if err != nil {
    75  		return err
    76  	}
    77  	defer client.Close()
    78  	return block.ProcessBlockedError(client.Expose(c.ServiceName), block.BlockChange)
    79  }