goscript

Template and generate go binaries

ntnn

ntnn

5,607 downloads

5,588 latest version

3.4 quality score

Version information

  • 0.1.1-rc1 (latest)
  • 0.1.0 (deleted)
released Nov 6th 2016

Start using this module

Documentation

ntnn/goscript — version 0.1.1-rc1 Nov 6th 2016

goscript

Table of Contents

  1. Description
    1. Testability
  2. Setup
    1. Requirements
  3. Usage
  4. Requirements

Description

Goscript takes care of compiling your go scripts on target nodes.

Puppets templating and using various interpreted languages is all fine, although the more powerful interpreted languages tend to be slow and shell-, awk- and sed-scripts are less testable.

Templating golang via puppet has several advantages:

  1. speed of execution at the one-time cost of compilation
  2. type safety
  3. testability (see below)

Testability

Using puppet epp render and golangs built-in test suite you can write makefiles which let you do ad-hoc tests of your golang scripts.

GO ?= go
PUPPET ?= puppet
EPPS ?= $(wildcard *.go.epp)
GOFILES ?= $(patsubst %.go.epp, %.go, $(EPPS))
TESTS ?= $(patsubst %.go.epp, %_test.go, $(EPPS))

default: $(GOFILES) $(TESTS)

%_test.go: %.go
        $(GO) test $@ $^

%.go: %.go.epp
        $(PUPPET) epp render $^ > $@

This makefile will render the epp's appropriately and run their respective _test files against them.

This also allows for more complicated setups with testing different configurations for different nodes.

For working examples see the examples/testability folder.

Setup

Requirements

  • Puppet Version >= 4
  • puppetlabs/stdlib >= 1.0.0
  • (currently) golang installed on nodes this module will be applied to

Usage

Write a golang template:

package main

import "fmt"

var list = []string{
    <% $example.each |$ex| { -%>
    "<%= $ex %>",
    <% } -%>
}

func main() {
    for x := range list {
        fmt.Println(list[x])
    }
}

And use the goscript resource to generate the binary:

goscript { '/bin/hello-world':
    content => epp(
        "${module_name}/hello-world.go.epp",
        {
            example => [ 'hello', 'world' ],
        }
    )
}

This results in the goscript module managing the file resource for /bin/hello-world.go and the exec resource which calls go build to compile the managed file. The exec resource is subscribed to the file resource, which will cause puppet to retrigger the compilation upon changes.

Reference

Types

Goscript::Path

Type; String or Array of Strings

Goscript::Mode

Type: String matching ^[0-7]{4}$

Defined Resources

goscript

Creates a file resource and attempts to compile the resulting file.

file (namevar)

Target filepath, defaults to $name

content/source

Mutually exclusive, same as with the File resource

owner (optional)

Owner of file and binary, defaults to root

group (optional)

Group of file and binary, defaults to root

mode (optional)

Mode of file, defaults to 0644.

binmode (optional)

Mode of binary, defaults to 0755

goscript::build

Builds go file at given path.

file (namevar)

Binary path, defaults to $name

owner (optional)

Owner of binary, defaults to root

group (optional)

Group of binary, defaults to root

binmode (optional)

Mode of binary, defaults to 0755

path (optional)

PATH for exec resource, String or Array of String. Defaults to /usr/local/bin:/usr/bin:/bin.

goscript::run

Executes given go file with go run.

file (namevar)

Binary path, defaults to $name

owner (optional)

Owner of binary, defaults to root

group (optional)

Group of binary, defaults to root

binmode (optional)

Mode of binary, defaults to 0755

path (optional)

PATH for exec resource, String or Array of String. Defaults to /usr/local/bin:/usr/bin:/bin.