github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/builtin/app/node/app.go (about)

     1  package nodeapp
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/otto/app"
     9  	"github.com/hashicorp/otto/appfile"
    10  	"github.com/hashicorp/otto/helper/bindata"
    11  	"github.com/hashicorp/otto/helper/compile"
    12  	"github.com/hashicorp/otto/helper/oneline"
    13  	"github.com/hashicorp/otto/helper/packer"
    14  	"github.com/hashicorp/otto/helper/schema"
    15  	"github.com/hashicorp/otto/helper/terraform"
    16  	"github.com/hashicorp/otto/helper/vagrant"
    17  )
    18  
    19  //go:generate go-bindata -pkg=nodeapp -nomemcopy -nometadata ./data/...
    20  
    21  // App is an implementation of app.App
    22  type App struct{}
    23  
    24  func (a *App) Meta() (*app.Meta, error) {
    25  	return Meta, nil
    26  }
    27  
    28  func (a *App) Implicit(ctx *app.Context) (*appfile.File, error) {
    29  	return nil, nil
    30  }
    31  
    32  func (a *App) Compile(ctx *app.Context) (*app.CompileResult, error) {
    33  	var opts compile.AppOptions
    34  	custom := &customizations{Opts: &opts}
    35  	opts = compile.AppOptions{
    36  		Ctx: ctx,
    37  		Result: &app.CompileResult{
    38  			Version: 1,
    39  		},
    40  		Bindata: &bindata.Data{
    41  			Asset:    Asset,
    42  			AssetDir: AssetDir,
    43  			Context:  map[string]interface{}{},
    44  		},
    45  		Customization: (&compile.Customization{
    46  			Callback: custom.process,
    47  			Schema: map[string]*schema.FieldSchema{
    48  				"node_version": &schema.FieldSchema{
    49  					Type:        schema.TypeString,
    50  					Default:     "4.1.0",
    51  					Description: "Node version to install",
    52  				},
    53  			},
    54  		}).Merge(compile.VagrantCustomizations(&opts)),
    55  	}
    56  
    57  	return compile.App(&opts)
    58  }
    59  
    60  func (a *App) Build(ctx *app.Context) error {
    61  	return packer.Build(ctx, &packer.BuildOptions{
    62  		InfraOutputMap: map[string]string{
    63  			"region": "aws_region",
    64  			"vpc_id": "aws_vpc_id",
    65  			"subnet_public":  "aws_subnet_id",
    66  		},
    67  	})
    68  }
    69  
    70  func (a *App) Deploy(ctx *app.Context) error {
    71  	return terraform.Deploy(&terraform.DeployOptions{
    72  		InfraOutputMap: map[string]string{
    73  			"region":         "aws_region",
    74  			"subnet-private": "private_subnet_id",
    75  			"subnet-public":  "public_subnet_id",
    76  		},
    77  	}).Route(ctx)
    78  }
    79  
    80  func (a *App) Dev(ctx *app.Context) error {
    81  	var layered *vagrant.Layered
    82  
    83  	// We only setup a layered environment if we've recompiled since
    84  	// version 0. If we're still at version 0 then we have to use the
    85  	// non-layered dev environment.
    86  	if ctx.CompileResult.Version > 0 {
    87  		// Read the go version, since we use that for our layer
    88  		version, err := oneline.Read(filepath.Join(ctx.Dir, "dev", "node_version"))
    89  		if err != nil {
    90  			return err
    91  		}
    92  
    93  		// Setup layers
    94  		layered, err = vagrant.DevLayered(ctx, []*vagrant.Layer{
    95  			&vagrant.Layer{
    96  				ID:          fmt.Sprintf("node%s", version),
    97  				Vagrantfile: filepath.Join(ctx.Dir, "dev", "layer-base", "Vagrantfile"),
    98  			},
    99  		})
   100  		if err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	// Build the actual development environment
   106  	return vagrant.Dev(&vagrant.DevOptions{
   107  		Instructions: strings.TrimSpace(devInstructions),
   108  		Layer:        layered,
   109  	}).Route(ctx)
   110  }
   111  
   112  func (a *App) DevDep(dst, src *app.Context) (*app.DevDep, error) {
   113  	return vagrant.DevDep(dst, src, &vagrant.DevDepOptions{})
   114  }
   115  
   116  const devInstructions = `
   117  A development environment has been created for writing a generic Node-based
   118  application. For this development environment, Node is pre-installed. To
   119  work on your project, edit files locally on your own machine. The file changes
   120  will be synced to the development environment.
   121  
   122  When you're ready to build or test your project, run 'otto dev ssh'
   123  to enter the development environment. You'll be placed directly into the
   124  working directory where you can run "npm install", "npm run", etc.
   125  
   126  You can access the environment from this machine using the IP address above.
   127  For example, if your app is running on port 5000, then access it using the
   128  IP above plus that port.
   129  `