Forge Home

goscript

Template and generate go binaries

6,623 downloads

6,584 latest version

3.4 quality score

We run a couple of automated
scans to help you access a
module's quality. Each module is
given a score based on how well
the author has formatted their
code and documentation and
modules are also checked for
malware using VirusTotal.

Please note, the information below
is for guidance only and neither of
these methods should be considered
an endorsement by Puppet.

Support the Puppet Community by contributing to this module

You are welcome to contribute to this module by suggesting new features, currency updates, or fixes. Every contribution is valuable to help ensure that the module remains compatible with the latest Puppet versions and continues to meet community needs. Complete the following steps:

  1. Review the module’s contribution guidelines and any licenses. Ensure that your planned contribution aligns with the author’s standards and any legal requirements.
  2. Fork the repository on GitHub, make changes on a branch of your fork, and submit a pull request. The pull request must clearly document your proposed change.

For questions about updating the module, contact the module’s author.

Version information

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

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'ntnn-goscript', '0.1.1-rc1'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add ntnn-goscript
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install ntnn-goscript --version 0.1.1-rc1

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

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.