github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/builtin/app/docker-external/app.go (about)

     1  package dockerext
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/otto/app"
    10  	"github.com/hashicorp/otto/appfile"
    11  	"github.com/hashicorp/otto/foundation"
    12  	"github.com/hashicorp/otto/helper/bindata"
    13  	"github.com/hashicorp/otto/helper/compile"
    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=dockerext -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  	fragmentPath := filepath.Join(ctx.Dir, "dev-dep", "Vagrantfile.fragment")
    34  
    35  	var opts compile.AppOptions
    36  	custom := &customizations{Opts: &opts}
    37  	opts = compile.AppOptions{
    38  		Ctx: ctx,
    39  		Result: &app.CompileResult{
    40  			Version: 1,
    41  		},
    42  		FoundationConfig: foundation.Config{
    43  			ServiceName: ctx.Application.Name,
    44  		},
    45  		Bindata: &bindata.Data{
    46  			Asset:    Asset,
    47  			AssetDir: AssetDir,
    48  			Context: map[string]interface{}{
    49  				"fragment_path": fragmentPath,
    50  				"path": map[string]string{
    51  					"guest_working": fmt.Sprintf(
    52  						"/otto-deps/%s-%s",
    53  						ctx.Application.Name,
    54  						ctx.Appfile.ID),
    55  				},
    56  			},
    57  		},
    58  		Customization: (&compile.Customization{
    59  			Callback: custom.process,
    60  			Schema: map[string]*schema.FieldSchema{
    61  				"image": &schema.FieldSchema{
    62  					Type:        schema.TypeString,
    63  					Default:     "",
    64  					Description: "Image name to run",
    65  				},
    66  
    67  				"run_args": &schema.FieldSchema{
    68  					Type:        schema.TypeString,
    69  					Default:     "",
    70  					Description: "Args to pass to `docker run`",
    71  				},
    72  			},
    73  		}).Merge(compile.VagrantCustomizations(&opts)),
    74  	}
    75  
    76  	return compile.App(&opts)
    77  }
    78  
    79  func (a *App) Build(ctx *app.Context) error {
    80  	return nil
    81  }
    82  
    83  func (a *App) Deploy(ctx *app.Context) error {
    84  	// Check if we have a deployment script
    85  	path := filepath.Join(ctx.Dir, "deploy", "main.tf")
    86  	if _, err := os.Stat(path); err != nil {
    87  		return fmt.Errorf(deployError)
    88  	}
    89  
    90  	// We do! Run it
    91  	return terraform.Deploy(&terraform.DeployOptions{
    92  		DisableBuild: true,
    93  		InfraOutputMap: map[string]string{
    94  			"region":         "aws_region",
    95  			"subnet-private": "private_subnet_id",
    96  			"subnet-public":  "public_subnet_id",
    97  		},
    98  	}).Route(ctx)
    99  }
   100  
   101  func (a *App) Dev(ctx *app.Context) error {
   102  	var layered *vagrant.Layered
   103  
   104  	// We only setup a layered environment if we've recompiled since
   105  	// version 0. If we're still at version 0 then we have to use the
   106  	// non-layered dev environment.
   107  	if ctx.CompileResult.Version > 0 {
   108  		// Setup layers
   109  		var err error
   110  		layered, err = vagrant.DevLayered(ctx, []*vagrant.Layer{})
   111  		if err != nil {
   112  			return err
   113  		}
   114  	}
   115  
   116  	// Build the actual development environment
   117  	return vagrant.Dev(&vagrant.DevOptions{
   118  		Instructions: strings.TrimSpace(devInstructions),
   119  		Layer:        layered,
   120  	}).Route(ctx)
   121  }
   122  
   123  func (a *App) DevDep(dst, src *app.Context) (*app.DevDep, error) {
   124  	// Nothing needs to be done for this
   125  	return nil, nil
   126  }
   127  
   128  const devInstructions = `
   129  A development environment has been created.
   130  
   131  Note that this development environment is just an example of what a
   132  consumer of this application might see as a development dependency.
   133  "docker-external" application types represent Docker images that are
   134  developed and built external to Otto. A future version of Otto will include
   135  a native "docker" type for a Docker-based development workflow. For
   136  the "docker-external" type, the specified docker image is started.
   137  `
   138  
   139  const deployError = `
   140  Deployment isn't supported for "docker-external" yet.
   141  
   142  This will be supported very soon. Otto plans to integrate with
   143  Nomad (nomadproject.io) and once we do that, Otto will schedule the
   144  container to run. Until then, deployment isn't supported. Sorry!
   145  `