<commit_before>package client

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"strings"
	"text/template"

	flag "github.com/docker/docker/pkg/mflag"
	"github.com/docker/docker/utils"
)

// CmdInspect displays low-level information on one or more containers or images.
//
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
func (cli *DockerCli) CmdInspect(args ...string) error {
	cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true)
	tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
	cmd.Require(flag.Min, 1)

	cmd.ParseFlags(args, true)

	var tmpl *template.Template
	if *tmplStr != "" {
		var err error
		if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
			fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
			return &utils.StatusError{StatusCode: 64,
				Status: "Template parsing error: " + err.Error()}
		}
	}

	indented := new(bytes.Buffer)
	indented.WriteByte('[')
	status := 0

	for _, name := range cmd.Args() {
		obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil))
		if err != nil {
			if strings.Contains(err.Error(), "Too many") {
				fmt.Fprintf(cli.err, "Error: %v", err)
				status = 1
				continue
			}

			obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
			if err != nil {
				if strings.Contains(err.Error(), "No such") {
					fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
				} else {
					fmt.Fprintf(cli.err, "%s", err)
				}
				status = 1
				continue
			}
		}

		if tmpl == nil {
			if err = json.Indent(indented, obj, "", "    "); err != nil {
				fmt.Fprintf(cli.err, "%s\n", err)
				status = 1
				continue
			}
		} else {
			// Has template, will render
			var value interface{}
			if err := json.Unmarshal(obj, &value); err != nil {
				fmt.Fprintf(cli.err, "%s\n", err)
				status = 1
				continue
			}
			if err := tmpl.Execute(cli.out, value); err != nil {
				return err
			}
			cli.out.Write([]byte{'\n'})
		}
		indented.WriteString(",")
	}

	if indented.Len() > 1 {
		// Remove trailing ','
		indented.Truncate(indented.Len() - 1)
	}
	indented.WriteString("]\n")

	if tmpl == nil {
		if _, err := io.Copy(cli.out, indented); err != nil {
			return err
		}
	}

	if status != 0 {
		return &utils.StatusError{StatusCode: status}
	}
	return nil
}
<commit_msg>Remove dead code looking for non-existent err msg<commit_after>package client

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"strings"
	"text/template"

	flag "github.com/docker/docker/pkg/mflag"
	"github.com/docker/docker/utils"
)

// CmdInspect displays low-level information on one or more containers or images.
//
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
func (cli *DockerCli) CmdInspect(args ...string) error {
	cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true)
	tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
	cmd.Require(flag.Min, 1)

	cmd.ParseFlags(args, true)

	var tmpl *template.Template
	if *tmplStr != "" {
		var err error
		if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
			fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
			return &utils.StatusError{StatusCode: 64,
				Status: "Template parsing error: " + err.Error()}
		}
	}

	indented := new(bytes.Buffer)
	indented.WriteByte('[')
	status := 0

	for _, name := range cmd.Args() {
		obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil))
		if err != nil {
			obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
			if err != nil {
				if strings.Contains(err.Error(), "No such") {
					fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
				} else {
					fmt.Fprintf(cli.err, "%s", err)
				}
				status = 1
				continue
			}
		}

		if tmpl == nil {
			if err = json.Indent(indented, obj, "", "    "); err != nil {
				fmt.Fprintf(cli.err, "%s\n", err)
				status = 1
				continue
			}
		} else {
			// Has template, will render
			var value interface{}
			if err := json.Unmarshal(obj, &value); err != nil {
				fmt.Fprintf(cli.err, "%s\n", err)
				status = 1
				continue
			}
			if err := tmpl.Execute(cli.out, value); err != nil {
				return err
			}
			cli.out.Write([]byte{'\n'})
		}
		indented.WriteString(",")
	}

	if indented.Len() > 1 {
		// Remove trailing ','
		indented.Truncate(indented.Len() - 1)
	}
	indented.WriteString("]\n")

	if tmpl == nil {
		if _, err := io.Copy(cli.out, indented); err != nil {
			return err
		}
	}

	if status != 0 {
		return &utils.StatusError{StatusCode: status}
	}
	return nil
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/terraform/helper/hashcode"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsVpnGatewayAttachment() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsVpnGatewayAttachmentCreate,
		Read:   resourceAwsVpnGatewayAttachmentRead,
		Delete: resourceAwsVpnGatewayAttachmentDelete,

		Schema: map[string]*schema.Schema{
			"vpc_id": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"vpn_gateway_id": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
		},
	}
}

func resourceAwsVpnGatewayAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).ec2conn

	vpcId := d.Get("vpc_id").(string)
	vgwId := d.Get("vpn_gateway_id").(string)

	createOpts := &ec2.AttachVpnGatewayInput{
		VpcId:        aws.String(vpcId),
		VpnGatewayId: aws.String(vgwId),
	}
	log.Printf("[DEBUG] VPN Gateway attachment options: %#v", *createOpts)

	_, err := conn.AttachVpnGateway(createOpts)
	if err != nil {
		return fmt.Errorf("Error attaching VPN Gateway %q to VPC %q: %s",
			vgwId, vpcId, err)
	}

	d.SetId(vpnGatewayAttachmentId(vpcId, vgwId))
	log.Printf("[INFO] VPN Gateway %q attachment ID: %s", vgwId, d.Id())

	stateConf := &resource.StateChangeConf{
		Pending:    []string{"detached", "attaching"},
		Target:     []string{"attached"},
		Refresh:    vpnGatewayAttachmentStateRefresh(conn, vpcId, vgwId),
		Timeout:    15 * time.Minute,
		Delay:      10 * time.Second,
		MinTimeout: 5 * time.Second,
	}

	_, err = stateConf.WaitForState()
	if err != nil {
		return fmt.Errorf("Error waiting for VPN Gateway %q to attach to VPC %q: %s",
			vgwId, vpcId, err)
	}
	log.Printf("[DEBUG] VPN Gateway %q attached to VPC %q.", vgwId, vpcId)

	return resourceAwsVpnGatewayAttachmentRead(d, meta)
}

func resourceAwsVpnGatewayAttachmentRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).ec2conn

	vgwId := d.Get("vpn_gateway_id").(string)

	resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
		VpnGatewayIds: []*string{aws.String(vgwId)},
	})

	if err != nil {
		awsErr, ok := err.(awserr.Error)
		if ok && awsErr.Code() == "InvalidVPNGatewayID.NotFound" {
			log.Printf("[WARN] VPN Gateway %q not found.", vgwId)
			d.SetId("")
			return nil
		}
		return err
	}

	vgw := resp.VpnGateways[0]
	if *vgw.State == "deleted" {
		log.Printf("[INFO] VPN Gateway %q appears to have been deleted.", vgwId)
		d.SetId("")
		return nil
	}

	vga := vpnGatewayGetAttachment(vgw)
	if len(vgw.VpcAttachments) == 0 || *vga.State == "detached" {
		d.Set("vpc_id", "")
		return nil
	}

	d.Set("vpc_id", *vga.VpcId)
	return nil
}

func resourceAwsVpnGatewayAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).ec2conn

	vpcId := d.Get("vpc_id").(string)
	vgwId := d.Get("vpn_gateway_id").(string)

	if vpcId == "" {
		log.Printf("[DEBUG] Not detaching VPN Gateway %q as no VPC ID is set.", vgwId)
		return nil
	}

	_, err := conn.DetachVpnGateway(&ec2.DetachVpnGatewayInput{
		VpcId:        aws.String(vpcId),
		VpnGatewayId: aws.String(vgwId),
	})

	if err != nil {
		awsErr, ok := err.(awserr.Error)
		if ok {
			switch awsErr.Code() {
			case "InvalidVPNGatewayID.NotFound":
				return nil
			case "InvalidVpnGatewayAttachment.NotFound":
				return nil
			}
		}

		return fmt.Errorf("Error detaching VPN Gateway %q from VPC %q: %s",
			vgwId, vpcId, err)
	}

	stateConf := &resource.StateChangeConf{
		Pending:    []string{"attached", "detaching"},
		Target:     []string{"detached"},
		Refresh:    vpnGatewayAttachmentStateRefresh(conn, vpcId, vgwId),
		Timeout:    15 * time.Minute,
		Delay:      10 * time.Second,
		MinTimeout: 5 * time.Second,
	}

	_, err = stateConf.WaitForState()
	if err != nil {
		return fmt.Errorf("Error waiting for VPN Gateway %q to detach from VPC %q: %s",
			vgwId, vpcId, err)
	}
	log.Printf("[DEBUG] VPN Gateway %q detached from VPC %q.", vgwId, vpcId)

	return nil
}

func vpnGatewayAttachmentStateRefresh(conn *ec2.EC2, vpcId, vgwId string) resource.StateRefreshFunc {
	return func() (interface{}, string, error) {
		resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
			Filters: []*ec2.Filter{
				&ec2.Filter{
					Name:   aws.String("attachment.vpc-id"),
					Values: []*string{aws.String(vpcId)},
				},
			},
			VpnGatewayIds: []*string{aws.String(vgwId)},
		})

		if err != nil {
			awsErr, ok := err.(awserr.Error)
			if ok {
				switch awsErr.Code() {
				case "InvalidVPNGatewayID.NotFound":
					fallthrough
				case "InvalidVpnGatewayAttachment.NotFound":
					return nil, "", nil
				}
			}

			return nil, "", err
		}

		vgw := resp.VpnGateways[0]
		if len(vgw.VpcAttachments) == 0 {
			return vgw, "detached", nil
		}

		vga := vpnGatewayGetAttachment(vgw)

		log.Printf("[DEBUG] VPN Gateway %q attachment status: %s", vgwId, *vga.State)
		return vgw, *vga.State, nil
	}
}

func vpnGatewayAttachmentId(vpcId, vgwId string) string {
	return fmt.Sprintf("vpn-attachment-%x", hashcode.String(fmt.Sprintf("%s-%s", vpcId, vgwId)))
}
<commit_msg>Correct error code for missing VPN gateway.<commit_after>package aws

import (
	"fmt"
	"log"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/terraform/helper/hashcode"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsVpnGatewayAttachment() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsVpnGatewayAttachmentCreate,
		Read:   resourceAwsVpnGatewayAttachmentRead,
		Delete: resourceAwsVpnGatewayAttachmentDelete,

		Schema: map[string]*schema.Schema{
			"vpc_id": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
			"vpn_gateway_id": &schema.Schema{
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},
		},
	}
}

func resourceAwsVpnGatewayAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).ec2conn

	vpcId := d.Get("vpc_id").(string)
	vgwId := d.Get("vpn_gateway_id").(string)

	createOpts := &ec2.AttachVpnGatewayInput{
		VpcId:        aws.String(vpcId),
		VpnGatewayId: aws.String(vgwId),
	}
	log.Printf("[DEBUG] VPN Gateway attachment options: %#v", *createOpts)

	_, err := conn.AttachVpnGateway(createOpts)
	if err != nil {
		return fmt.Errorf("Error attaching VPN Gateway %q to VPC %q: %s",
			vgwId, vpcId, err)
	}

	d.SetId(vpnGatewayAttachmentId(vpcId, vgwId))
	log.Printf("[INFO] VPN Gateway %q attachment ID: %s", vgwId, d.Id())

	stateConf := &resource.StateChangeConf{
		Pending:    []string{"detached", "attaching"},
		Target:     []string{"attached"},
		Refresh:    vpnGatewayAttachmentStateRefresh(conn, vpcId, vgwId),
		Timeout:    15 * time.Minute,
		Delay:      10 * time.Second,
		MinTimeout: 5 * time.Second,
	}

	_, err = stateConf.WaitForState()
	if err != nil {
		return fmt.Errorf("Error waiting for VPN Gateway %q to attach to VPC %q: %s",
			vgwId, vpcId, err)
	}
	log.Printf("[DEBUG] VPN Gateway %q attached to VPC %q.", vgwId, vpcId)

	return resourceAwsVpnGatewayAttachmentRead(d, meta)
}

func resourceAwsVpnGatewayAttachmentRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).ec2conn

	vgwId := d.Get("vpn_gateway_id").(string)

	resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
		VpnGatewayIds: []*string{aws.String(vgwId)},
	})

	if err != nil {
		awsErr, ok := err.(awserr.Error)
		if ok && awsErr.Code() == "InvalidVpnGatewayID.NotFound" {
			log.Printf("[WARN] VPN Gateway %q not found.", vgwId)
			d.SetId("")
			return nil
		}
		return err
	}

	vgw := resp.VpnGateways[0]
	if *vgw.State == "deleted" {
		log.Printf("[INFO] VPN Gateway %q appears to have been deleted.", vgwId)
		d.SetId("")
		return nil
	}

	vga := vpnGatewayGetAttachment(vgw)
	if len(vgw.VpcAttachments) == 0 || *vga.State == "detached" {
		d.Set("vpc_id", "")
		return nil
	}

	d.Set("vpc_id", *vga.VpcId)
	return nil
}

func resourceAwsVpnGatewayAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).ec2conn

	vpcId := d.Get("vpc_id").(string)
	vgwId := d.Get("vpn_gateway_id").(string)

	if vpcId == "" {
		log.Printf("[DEBUG] Not detaching VPN Gateway %q as no VPC ID is set.", vgwId)
		return nil
	}

	_, err := conn.DetachVpnGateway(&ec2.DetachVpnGatewayInput{
		VpcId:        aws.String(vpcId),
		VpnGatewayId: aws.String(vgwId),
	})

	if err != nil {
		awsErr, ok := err.(awserr.Error)
		if ok {
			switch awsErr.Code() {
			case "InvalidVpnGatewayID.NotFound":
				return nil
			case "InvalidVpnGatewayAttachment.NotFound":
				return nil
			}
		}

		return fmt.Errorf("Error detaching VPN Gateway %q from VPC %q: %s",
			vgwId, vpcId, err)
	}

	stateConf := &resource.StateChangeConf{
		Pending:    []string{"attached", "detaching"},
		Target:     []string{"detached"},
		Refresh:    vpnGatewayAttachmentStateRefresh(conn, vpcId, vgwId),
		Timeout:    15 * time.Minute,
		Delay:      10 * time.Second,
		MinTimeout: 5 * time.Second,
	}

	_, err = stateConf.WaitForState()
	if err != nil {
		return fmt.Errorf("Error waiting for VPN Gateway %q to detach from VPC %q: %s",
			vgwId, vpcId, err)
	}
	log.Printf("[DEBUG] VPN Gateway %q detached from VPC %q.", vgwId, vpcId)

	return nil
}

func vpnGatewayAttachmentStateRefresh(conn *ec2.EC2, vpcId, vgwId string) resource.StateRefreshFunc {
	return func() (interface{}, string, error) {
		resp, err := conn.DescribeVpnGateways(&ec2.DescribeVpnGatewaysInput{
			Filters: []*ec2.Filter{
				&ec2.Filter{
					Name:   aws.String("attachment.vpc-id"),
					Values: []*string{aws.String(vpcId)},
				},
			},
			VpnGatewayIds: []*string{aws.String(vgwId)},
		})

		if err != nil {
			awsErr, ok := err.(awserr.Error)
			if ok {
				switch awsErr.Code() {
				case "InvalidVpnGatewayID.NotFound":
					fallthrough
				case "InvalidVpnGatewayAttachment.NotFound":
					return nil, "", nil
				}
			}

			return nil, "", err
		}

		vgw := resp.VpnGateways[0]
		if len(vgw.VpcAttachments) == 0 {
			return vgw, "detached", nil
		}

		vga := vpnGatewayGetAttachment(vgw)

		log.Printf("[DEBUG] VPN Gateway %q attachment status: %s", vgwId, *vga.State)
		return vgw, *vga.State, nil
	}
}

func vpnGatewayAttachmentId(vpcId, vgwId string) string {
	return fmt.Sprintf("vpn-attachment-%x", hashcode.String(fmt.Sprintf("%s-%s", vpcId, vgwId)))
}
<|endoftext|>


<commit_before>package server

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// Version of IronFunctions
var Version = "0.1.40"

func handleVersion(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{"version": Version})
}
<commit_msg>functions: 0.1.41 release [skip ci]<commit_after>package server

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// Version of IronFunctions
var Version = "0.1.41"

func handleVersion(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{"version": Version})
}
<|endoftext|>


<commit_before>package config

var(
    CFG_NAME="my blog"
    CFG_DSN="root:123@tcp(localhost:3306)/blog?charset=utf8"
    CFG_TPL_DIR="/root/workspaces/go/go-blog/blog/views/"
    CFG_STATIC_DIR="/root/workspaces/go/go-blog/staticDir/"
)
<commit_msg>update config<commit_after>package config

var(
    CFG_NAME="my blog"
    CFG_DSN="root:123@tcp(localhost:3306)/blog?charset=utf8"
    CFG_TPL_DIR="/root/workspaces/go/go-curd/blog/views/"
    CFG_STATIC_DIR="/root/workspaces/go/go-curd/staticDir/"
)
<|endoftext|>


<commit_before>// Copyright 2017 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//     http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package nfserver provides netflow collection services via UDP and passes flows into annotator layer
package nfserver

import (
	"fmt"
	"net"
	"strconv"
	"strings"
	"sync/atomic"

	"github.com/golang/glog"
	"github.com/google/tflow2/convert"
	"github.com/google/tflow2/netflow"
	"github.com/google/tflow2/nf9"
	"github.com/google/tflow2/stats"
)

// fieldMap describes what information is at what index in the slice
// that we get from decoding a netflow packet
type fieldMap struct {
	srcAddr  int
	dstAddr  int
	protocol int
	packets  int
	size     int
	intIn    int
	intOut   int
	nextHop  int
	family   int
	vlan     int
	ts       int
	srcAsn   int
	dstAsn   int
	srcPort  int
	dstPort  int
}

// NetflowServer represents a Netflow Collector instance
type NetflowServer struct {
	// tmplCache is used to save received flow templates
	// for later lookup in order to decode netflow packets
	tmplCache *templateCache

	// receiver is the channel used to receive flows from the annotator layer
	Output chan *netflow.Flow

	// debug defines the debug level
	debug int

	// bgpAugment is used to decide if ASN information from netflow packets should be used
	bgpAugment bool
}

// New creates and starts a new `NetflowServer` instance
func New(listenAddr string, numReaders int, bgpAugment bool, debug int) *NetflowServer {
	nfs := &NetflowServer{
		debug:      debug,
		tmplCache:  newTemplateCache(),
		Output:     make(chan *netflow.Flow),
		bgpAugment: bgpAugment,
	}

	addr, err := net.ResolveUDPAddr("udp", listenAddr)
	if err != nil {
		panic(fmt.Sprintf("ResolveUDPAddr: %v", err))
	}

	con, err := net.ListenUDP("udp", addr)
	if err != nil {
		panic(fmt.Sprintf("Listen: %v", err))
	}

	// Create goroutines that read netflow packet and process it
	for i := 0; i < numReaders; i++ {
		go func(num int) {
			nfs.packetWorker(num, con)
		}(i)
	}

	return nfs
}

// packetWorker reads netflow packet from socket and handsoff processing to processFlowSets()
func (nfs *NetflowServer) packetWorker(identity int, conn *net.UDPConn) {
	buffer := make([]byte, 8960)
	for {
		length, remote, err := conn.ReadFromUDP(buffer)
		if err != nil {
			glog.Errorf("Error reading from socket: %v", err)
			continue
		}
		atomic.AddUint64(&stats.GlobalStats.Packets, 1)

		remote.IP = remote.IP.To4()
		if remote.IP == nil {
			glog.Errorf("Received IPv6 packet. Dropped.")
			continue
		}

		nfs.processPacket(remote.IP, buffer[:length])
	}
}

// processPacket takes a raw netflow packet, send it to the decoder, updates template cache
// (if there are templates in the packet) and passes the decoded packet over to processFlowSets()
func (nfs *NetflowServer) processPacket(remote net.IP, buffer []byte) {
	length := len(buffer)
	packet, err := nf9.Decode(buffer[:length], remote)
	if err != nil {
		glog.Errorf("nf9packet.Decode: %v", err)
		return
	}

	nfs.updateTemplateCache(remote, packet)
	nfs.processFlowSets(remote, packet.Header.SourceID, packet.DataFlowSets(), int64(packet.Header.UnixSecs), packet)
}

// processFlowSets iterates over flowSets and calls processFlowSet() for each flow set
func (nfs *NetflowServer) processFlowSets(remote net.IP, sourceID uint32, flowSets []*nf9.FlowSet, ts int64, packet *nf9.Packet) {
	addr := remote.String()
	keyParts := make([]string, 3, 3)
	for _, set := range flowSets {
		template := nfs.tmplCache.get(convert.Uint32(remote), sourceID, set.Header.FlowSetID)

		if template == nil {
			templateKey := makeTemplateKey(addr, sourceID, set.Header.FlowSetID, keyParts)
			if nfs.debug > 0 {
				glog.Warningf("Template for given FlowSet not found: %s", templateKey)
			}
			continue
		}

		records := template.DecodeFlowSet(*set)
		if records == nil {
			glog.Warning("Error decoding FlowSet")
			continue
		}
		nfs.processFlowSet(template, records, remote, ts, packet)
	}
}

// process generates Flow elements from records and pushes them into the `receiver` channel
func (nfs *NetflowServer) processFlowSet(template *nf9.TemplateRecords, records []nf9.FlowDataRecord, agent net.IP, ts int64, packet *nf9.Packet) {
	fm := generateFieldMap(template)

	for _, r := range records {
		if fm.family == 4 {
			atomic.AddUint64(&stats.GlobalStats.Flows4, 1)
		} else if fm.family == 6 {
			atomic.AddUint64(&stats.GlobalStats.Flows6, 1)
		} else {
			glog.Warning("Unknown address family")
			continue
		}

		var fl netflow.Flow
		fl.Router = agent
		fl.Timestamp = ts
		fl.Family = uint32(fm.family)
		fl.Packets = convert.Uint32(r.Values[fm.packets])
		fl.Size = uint64(convert.Uint32(r.Values[fm.size]))
		fl.Protocol = convert.Uint32(r.Values[fm.protocol])
		fl.IntIn = convert.Uint32(r.Values[fm.intIn])
		fl.IntOut = convert.Uint32(r.Values[fm.intOut])
		fl.SrcPort = convert.Uint32(r.Values[fm.srcPort])
		fl.DstPort = convert.Uint32(r.Values[fm.dstPort])
		fl.SrcAddr = convert.Reverse(r.Values[fm.srcAddr])
		fl.DstAddr = convert.Reverse(r.Values[fm.dstAddr])
		fl.NextHop = convert.Reverse(r.Values[fm.nextHop])

		if !nfs.bgpAugment {
			fl.SrcAs = convert.Uint32(r.Values[fm.srcAsn])
			fl.DstAs = convert.Uint32(r.Values[fm.dstAsn])
		}

		/*if debug > 2 {
			fl.Packet = packet
			fl.Template = template.Header.TemplateID
			Dump(&fl)
		}*/

		nfs.Output <- &fl
	}
}

// Dump dumps a flow on the screen
func Dump(fl *netflow.Flow) {
	fmt.Printf("--------------------------------\n")
	fmt.Printf("Flow dump:\n")
	fmt.Printf("Router: %d\n", fl.Router)
	fmt.Printf("Family: %d\n", fl.Family)
	fmt.Printf("SrcAddr: %s\n", net.IP(fl.SrcAddr).String())
	fmt.Printf("DstAddr: %s\n", net.IP(fl.DstAddr).String())
	fmt.Printf("Protocol: %d\n", fl.Protocol)
	fmt.Printf("NextHop: %s\n", net.IP(fl.NextHop).String())
	fmt.Printf("IntIn: %d\n", fl.IntIn)
	fmt.Printf("IntOut: %d\n", fl.IntOut)
	fmt.Printf("Packets: %d\n", fl.Packets)
	fmt.Printf("Bytes: %d\n", fl.Size)
	fmt.Printf("--------------------------------\n")
}

// DumpTemplate dumps a template on the screen
func DumpTemplate(tmpl *nf9.TemplateRecords) {
	fmt.Printf("Template %d\n", tmpl.Header.TemplateID)
	for rec, i := range tmpl.Records {
		fmt.Printf("%d: %v\n", i, rec)
	}
}

// generateFieldMap processes a TemplateRecord and populates a fieldMap accordingly
// the FieldMap can then be used to read fields from a flow
func generateFieldMap(template *nf9.TemplateRecords) *fieldMap {
	var fm fieldMap
	i := -1
	for _, f := range template.Records {
		i++

		switch f.Type {
		case nf9.IPv4SrcAddr:
			fm.srcAddr = i
			fm.family = 4
		case nf9.IPv6SrcAddr:
			fm.srcAddr = i
			fm.family = 6
		case nf9.IPv4DstAddr:
			fm.dstAddr = i
		case nf9.IPv6DstAddr:
			fm.dstAddr = i
		case nf9.InBytes:
			fm.size = i
		case nf9.Protocol:
			fm.protocol = i
		case nf9.InPkts:
			fm.packets = i
		case nf9.InputSnmp:
			fm.intIn = i
		case nf9.OutputSnmp:
			fm.intOut = i
		case nf9.IPv4NextHop:
			fm.nextHop = i
		case nf9.IPv6NextHop:
			fm.nextHop = i
		case nf9.L4SrcPort:
			fm.srcPort = i
		case nf9.L4DstPort:
			fm.dstPort = i
		case nf9.SrcAs:
			fm.srcAsn = i
		case nf9.DstAs:
			fm.dstAsn = i
		}
	}
	return &fm
}

// updateTemplateCache updates the template cache
func (nfs *NetflowServer) updateTemplateCache(remote net.IP, p *nf9.Packet) {
	templRecs := p.GetTemplateRecords()
	for _, tr := range templRecs {
		nfs.tmplCache.set(convert.Uint32(remote), tr.Packet.Header.SourceID, tr.Header.TemplateID, *tr)
	}
}

// makeTemplateKey creates a string of the 3 tuple router address, source id and template id
func makeTemplateKey(addr string, sourceID uint32, templateID uint16, keyParts []string) string {
	keyParts[0] = addr
	keyParts[1] = strconv.Itoa(int(sourceID))
	keyParts[2] = strconv.Itoa(int(templateID))
	return strings.Join(keyParts, "|")
}
<commit_msg>Re-enabling packet dumps at debug level > 2<commit_after>// Copyright 2017 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//     http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package nfserver provides netflow collection services via UDP and passes flows into annotator layer
package nfserver

import (
	"fmt"
	"net"
	"strconv"
	"strings"
	"sync/atomic"

	"github.com/golang/glog"
	"github.com/google/tflow2/convert"
	"github.com/google/tflow2/netflow"
	"github.com/google/tflow2/nf9"
	"github.com/google/tflow2/stats"
)

// fieldMap describes what information is at what index in the slice
// that we get from decoding a netflow packet
type fieldMap struct {
	srcAddr  int
	dstAddr  int
	protocol int
	packets  int
	size     int
	intIn    int
	intOut   int
	nextHop  int
	family   int
	vlan     int
	ts       int
	srcAsn   int
	dstAsn   int
	srcPort  int
	dstPort  int
}

// NetflowServer represents a Netflow Collector instance
type NetflowServer struct {
	// tmplCache is used to save received flow templates
	// for later lookup in order to decode netflow packets
	tmplCache *templateCache

	// receiver is the channel used to receive flows from the annotator layer
	Output chan *netflow.Flow

	// debug defines the debug level
	debug int

	// bgpAugment is used to decide if ASN information from netflow packets should be used
	bgpAugment bool
}

// New creates and starts a new `NetflowServer` instance
func New(listenAddr string, numReaders int, bgpAugment bool, debug int) *NetflowServer {
	nfs := &NetflowServer{
		debug:      debug,
		tmplCache:  newTemplateCache(),
		Output:     make(chan *netflow.Flow),
		bgpAugment: bgpAugment,
	}

	addr, err := net.ResolveUDPAddr("udp", listenAddr)
	if err != nil {
		panic(fmt.Sprintf("ResolveUDPAddr: %v", err))
	}

	con, err := net.ListenUDP("udp", addr)
	if err != nil {
		panic(fmt.Sprintf("Listen: %v", err))
	}

	// Create goroutines that read netflow packet and process it
	for i := 0; i < numReaders; i++ {
		go func(num int) {
			nfs.packetWorker(num, con)
		}(i)
	}

	return nfs
}

// packetWorker reads netflow packet from socket and handsoff processing to processFlowSets()
func (nfs *NetflowServer) packetWorker(identity int, conn *net.UDPConn) {
	buffer := make([]byte, 8960)
	for {
		length, remote, err := conn.ReadFromUDP(buffer)
		if err != nil {
			glog.Errorf("Error reading from socket: %v", err)
			continue
		}
		atomic.AddUint64(&stats.GlobalStats.Packets, 1)

		remote.IP = remote.IP.To4()
		if remote.IP == nil {
			glog.Errorf("Received IPv6 packet. Dropped.")
			continue
		}

		nfs.processPacket(remote.IP, buffer[:length])
	}
}

// processPacket takes a raw netflow packet, send it to the decoder, updates template cache
// (if there are templates in the packet) and passes the decoded packet over to processFlowSets()
func (nfs *NetflowServer) processPacket(remote net.IP, buffer []byte) {
	length := len(buffer)
	packet, err := nf9.Decode(buffer[:length], remote)
	if err != nil {
		glog.Errorf("nf9packet.Decode: %v", err)
		return
	}

	nfs.updateTemplateCache(remote, packet)
	nfs.processFlowSets(remote, packet.Header.SourceID, packet.DataFlowSets(), int64(packet.Header.UnixSecs), packet)
}

// processFlowSets iterates over flowSets and calls processFlowSet() for each flow set
func (nfs *NetflowServer) processFlowSets(remote net.IP, sourceID uint32, flowSets []*nf9.FlowSet, ts int64, packet *nf9.Packet) {
	addr := remote.String()
	keyParts := make([]string, 3, 3)
	for _, set := range flowSets {
		template := nfs.tmplCache.get(convert.Uint32(remote), sourceID, set.Header.FlowSetID)

		if template == nil {
			templateKey := makeTemplateKey(addr, sourceID, set.Header.FlowSetID, keyParts)
			if nfs.debug > 0 {
				glog.Warningf("Template for given FlowSet not found: %s", templateKey)
			}
			continue
		}

		records := template.DecodeFlowSet(*set)
		if records == nil {
			glog.Warning("Error decoding FlowSet")
			continue
		}
		nfs.processFlowSet(template, records, remote, ts, packet)
	}
}

// process generates Flow elements from records and pushes them into the `receiver` channel
func (nfs *NetflowServer) processFlowSet(template *nf9.TemplateRecords, records []nf9.FlowDataRecord, agent net.IP, ts int64, packet *nf9.Packet) {
	fm := generateFieldMap(template)

	for _, r := range records {
		if fm.family == 4 {
			atomic.AddUint64(&stats.GlobalStats.Flows4, 1)
		} else if fm.family == 6 {
			atomic.AddUint64(&stats.GlobalStats.Flows6, 1)
		} else {
			glog.Warning("Unknown address family")
			continue
		}

		var fl netflow.Flow
		fl.Router = agent
		fl.Timestamp = ts
		fl.Family = uint32(fm.family)
		fl.Packets = convert.Uint32(r.Values[fm.packets])
		fl.Size = uint64(convert.Uint32(r.Values[fm.size]))
		fl.Protocol = convert.Uint32(r.Values[fm.protocol])
		fl.IntIn = convert.Uint32(r.Values[fm.intIn])
		fl.IntOut = convert.Uint32(r.Values[fm.intOut])
		fl.SrcPort = convert.Uint32(r.Values[fm.srcPort])
		fl.DstPort = convert.Uint32(r.Values[fm.dstPort])
		fl.SrcAddr = convert.Reverse(r.Values[fm.srcAddr])
		fl.DstAddr = convert.Reverse(r.Values[fm.dstAddr])
		fl.NextHop = convert.Reverse(r.Values[fm.nextHop])

		if !nfs.bgpAugment {
			fl.SrcAs = convert.Uint32(r.Values[fm.srcAsn])
			fl.DstAs = convert.Uint32(r.Values[fm.dstAsn])
		}

		if nfs.debug > 2 {
			Dump(&fl)
		}

		nfs.Output <- &fl
	}
}

// Dump dumps a flow on the screen
func Dump(fl *netflow.Flow) {
	fmt.Printf("--------------------------------\n")
	fmt.Printf("Flow dump:\n")
	fmt.Printf("Router: %d\n", fl.Router)
	fmt.Printf("Family: %d\n", fl.Family)
	fmt.Printf("SrcAddr: %s\n", net.IP(fl.SrcAddr).String())
	fmt.Printf("DstAddr: %s\n", net.IP(fl.DstAddr).String())
	fmt.Printf("Protocol: %d\n", fl.Protocol)
	fmt.Printf("NextHop: %s\n", net.IP(fl.NextHop).String())
	fmt.Printf("IntIn: %d\n", fl.IntIn)
	fmt.Printf("IntOut: %d\n", fl.IntOut)
	fmt.Printf("Packets: %d\n", fl.Packets)
	fmt.Printf("Bytes: %d\n", fl.Size)
	fmt.Printf("--------------------------------\n")
}

// DumpTemplate dumps a template on the screen
func DumpTemplate(tmpl *nf9.TemplateRecords) {
	fmt.Printf("Template %d\n", tmpl.Header.TemplateID)
	for rec, i := range tmpl.Records {
		fmt.Printf("%d: %v\n", i, rec)
	}
}

// generateFieldMap processes a TemplateRecord and populates a fieldMap accordingly
// the FieldMap can then be used to read fields from a flow
func generateFieldMap(template *nf9.TemplateRecords) *fieldMap {
	var fm fieldMap
	i := -1
	for _, f := range template.Records {
		i++

		switch f.Type {
		case nf9.IPv4SrcAddr:
			fm.srcAddr = i
			fm.family = 4
		case nf9.IPv6SrcAddr:
			fm.srcAddr = i
			fm.family = 6
		case nf9.IPv4DstAddr:
			fm.dstAddr = i
		case nf9.IPv6DstAddr:
			fm.dstAddr = i
		case nf9.InBytes:
			fm.size = i
		case nf9.Protocol:
			fm.protocol = i
		case nf9.InPkts:
			fm.packets = i
		case nf9.InputSnmp:
			fm.intIn = i
		case nf9.OutputSnmp:
			fm.intOut = i
		case nf9.IPv4NextHop:
			fm.nextHop = i
		case nf9.IPv6NextHop:
			fm.nextHop = i
		case nf9.L4SrcPort:
			fm.srcPort = i
		case nf9.L4DstPort:
			fm.dstPort = i
		case nf9.SrcAs:
			fm.srcAsn = i
		case nf9.DstAs:
			fm.dstAsn = i
		}
	}
	return &fm
}

// updateTemplateCache updates the template cache
func (nfs *NetflowServer) updateTemplateCache(remote net.IP, p *nf9.Packet) {
	templRecs := p.GetTemplateRecords()
	for _, tr := range templRecs {
		nfs.tmplCache.set(convert.Uint32(remote), tr.Packet.Header.SourceID, tr.Header.TemplateID, *tr)
	}
}

// makeTemplateKey creates a string of the 3 tuple router address, source id and template id
func makeTemplateKey(addr string, sourceID uint32, templateID uint16, keyParts []string) string {
	keyParts[0] = addr
	keyParts[1] = strconv.Itoa(int(sourceID))
	keyParts[2] = strconv.Itoa(int(templateID))
	return strings.Join(keyParts, "|")
}
<|endoftext|>


<commit_before>package booklitcmd

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/vito/booklit"
	"github.com/vito/booklit/baselit"
	"github.com/vito/booklit/load"
	"github.com/vito/booklit/render"
)

type Command struct {
	In  string `long:"in"  short:"i" required:"true" description:"Input .lit file."`
	Out string `long:"out" short:"o" required:"true" description:"Output directory in which to render."`

	Plugins []string `long:"plugin" short:"p" description:"Package to import, providing a plugin."`

	HTMLEngine struct {
		Templates string `long:"templates" description:"Directory containing .tmpl files to load."`
	} `group:"HTML Rendering Engine" namespace:"html"`
}

func (cmd *Command) Execute(args []string) error {
	if len(cmd.Plugins) > 0 && os.Getenv("BOOKLIT_REEXEC") == "" {
		return cmd.reexec()
	}

	processor := &load.Processor{
		PluginFactories: []booklit.PluginFactory{
			booklit.PluginFactoryFunc(baselit.NewPlugin),
		},
	}

	section, err := processor.LoadFile(cmd.In)
	if err != nil {
		return err
	}

	err = os.MkdirAll(cmd.Out, 0755)
	if err != nil {
		return err
	}

	engine := render.NewHTMLRenderingEngine()
	err = engine.LoadTemplates(cmd.HTMLEngine.Templates)
	if err != nil {
		return err
	}

	writer := render.Writer{
		Engine:      engine,
		Destination: cmd.Out,
	}

	return writer.WriteSection(section)
}

func (cmd *Command) reexec() error {
	tmpdir, err := ioutil.TempDir("", "booklit-reexec")
	if err != nil {
		return err
	}

	defer os.RemoveAll(tmpdir)

	src := filepath.Join(tmpdir, "main.go")
	bin := filepath.Join(tmpdir, "booklit")

	goSrc := "package main\n"
	goSrc += "import \"github.com/vito/booklit/booklitcmd\"\n"
	for _, p := range cmd.Plugins {
		goSrc += "import _ \"" + p + "\"\n"
	}
	goSrc += "func main() {\n"
	goSrc += "	booklitcmd.Main()\n"
	goSrc += "}\n"

	err = ioutil.WriteFile(src, []byte(goSrc), 0644)
	if err != nil {
		return err
	}

	build := exec.Command("go", "build", "-o", bin, src)
	build.Stdout = os.Stdout
	build.Stderr = os.Stderr
	err = build.Run()
	if err != nil {
		return err
	}

	run := exec.Command(bin, os.Args[1:]...)
	run.Env = append(os.Environ(), "BOOKLIT_REEXEC=1")
	run.Stdout = os.Stdout
	run.Stderr = os.Stderr
	return run.Run()
}
<commit_msg>only load templates if flag is given<commit_after>package booklitcmd

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/vito/booklit"
	"github.com/vito/booklit/baselit"
	"github.com/vito/booklit/load"
	"github.com/vito/booklit/render"
)

type Command struct {
	In  string `long:"in"  short:"i" required:"true" description:"Input .lit file."`
	Out string `long:"out" short:"o" required:"true" description:"Output directory in which to render."`

	Plugins []string `long:"plugin" short:"p" description:"Package to import, providing a plugin."`

	HTMLEngine struct {
		Templates string `long:"templates" description:"Directory containing .tmpl files to load."`
	} `group:"HTML Rendering Engine" namespace:"html"`
}

func (cmd *Command) Execute(args []string) error {
	if len(cmd.Plugins) > 0 && os.Getenv("BOOKLIT_REEXEC") == "" {
		return cmd.reexec()
	}

	processor := &load.Processor{
		PluginFactories: []booklit.PluginFactory{
			booklit.PluginFactoryFunc(baselit.NewPlugin),
		},
	}

	section, err := processor.LoadFile(cmd.In)
	if err != nil {
		return err
	}

	err = os.MkdirAll(cmd.Out, 0755)
	if err != nil {
		return err
	}

	engine := render.NewHTMLRenderingEngine()

	if cmd.HTMLEngine.Templates != "" {
		err := engine.LoadTemplates(cmd.HTMLEngine.Templates)
		if err != nil {
			return err
		}
	}

	writer := render.Writer{
		Engine:      engine,
		Destination: cmd.Out,
	}

	return writer.WriteSection(section)
}

func (cmd *Command) reexec() error {
	tmpdir, err := ioutil.TempDir("", "booklit-reexec")
	if err != nil {
		return err
	}

	defer os.RemoveAll(tmpdir)

	src := filepath.Join(tmpdir, "main.go")
	bin := filepath.Join(tmpdir, "booklit")

	goSrc := "package main\n"
	goSrc += "import \"github.com/vito/booklit/booklitcmd\"\n"
	for _, p := range cmd.Plugins {
		goSrc += "import _ \"" + p + "\"\n"
	}
	goSrc += "func main() {\n"
	goSrc += "	booklitcmd.Main()\n"
	goSrc += "}\n"

	err = ioutil.WriteFile(src, []byte(goSrc), 0644)
	if err != nil {
		return err
	}

	build := exec.Command("go", "build", "-o", bin, src)
	build.Stdout = os.Stdout
	build.Stderr = os.Stderr
	err = build.Run()
	if err != nil {
		return err
	}

	run := exec.Command(bin, os.Args[1:]...)
	run.Env = append(os.Environ(), "BOOKLIT_REEXEC=1")
	run.Stdout = os.Stdout
	run.Stderr = os.Stderr
	return run.Run()
}
<|endoftext|>


<commit_before>package helpers

import (
	"math/rand"
	"strings"
	"sync"
	"time"
)

const (
	letterBytes   = "abcdefghijklmnopqrstuvwxyz"
	randSuffixLen = 8
	sep = "-"
)

var (
	r        *rand.Rand
	rndMutex *sync.Mutex
	once     sync.Once
)

func initSeed() {
	seed := time.Now().UTC().UnixNano()
	r = rand.New(rand.NewSource(seed))
	rndMutex = &sync.Mutex{}
}

func AppendRandomString(prefix string) string {
	once.Do(initSeed)
	suffix := make([]byte, randSuffixLen)

	rndMutex.Lock()
	defer rndMutex.Unlock()

	for i := range suffix {
		suffix[i] = letterBytes[r.Intn(len(letterBytes))]
	}

	return strings.Join([]string{prefix, string(suffix)}, sep)
}<commit_msg>golang format tools (#276)<commit_after>package helpers

import (
	"math/rand"
	"strings"
	"sync"
	"time"
)

const (
	letterBytes   = "abcdefghijklmnopqrstuvwxyz"
	randSuffixLen = 8
	sep           = "-"
)

var (
	r        *rand.Rand
	rndMutex *sync.Mutex
	once     sync.Once
)

func initSeed() {
	seed := time.Now().UTC().UnixNano()
	r = rand.New(rand.NewSource(seed))
	rndMutex = &sync.Mutex{}
}

func AppendRandomString(prefix string) string {
	once.Do(initSeed)
	suffix := make([]byte, randSuffixLen)

	rndMutex.Lock()
	defer rndMutex.Unlock()

	for i := range suffix {
		suffix[i] = letterBytes[r.Intn(len(letterBytes))]
	}

	return strings.Join([]string{prefix, string(suffix)}, sep)
}
<|endoftext|>


<commit_before>// Copyright 2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package helpers

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"os"

	log "github.com/sirupsen/logrus"
)

// SSHMeta contains metadata to SSH into a remote location to run tests
type SSHMeta struct {
	sshClient *SSHClient
	env       []string
}

// CreateSSHMeta returns an SSHMeta with the specified host, port, and user, as
// well as an according SSHClient.
func CreateSSHMeta(host string, port int, user string) *SSHMeta {
	return &SSHMeta{
		sshClient: GetSSHClient(host, port, user),
	}
}

func (s *SSHMeta) String() string {
	return fmt.Sprintf("environment: %s, SSHClient: %s", s.env, s.sshClient.String())

}

// GetVagrantSSHMetadata returns a SSHMeta initialized based on the provided
// SSH-config target.
func GetVagrantSSHMetadata(vmName string) *SSHMeta {
	var vagrant Vagrant
	config, err := vagrant.GetVagrantSSHMetadata(vmName)
	if err != nil {
		return nil
	}

	log.Debugf("generated SSHConfig for node %s", vmName)
	nodes, err := ImportSSHconfig(config)
	if err != nil {
		return nil
	}
	log.Debugf("done importing ssh config")
	node := nodes[vmName]
	if node == nil {
		return nil
	}

	return &SSHMeta{
		sshClient: node.GetSSHClient(),
	}
}

// Execute executes cmd on the provided node and stores the stdout / stderr of
// the command in the provided buffers. Returns false if the command failed
// during its execution.
func (s *SSHMeta) Execute(cmd string, stdout io.Writer, stderr io.Writer) bool {
	if stdout == nil {
		stdout = os.Stdout
	}

	if stderr == nil {
		stderr = os.Stderr
	}

	command := &SSHCommand{
		Path:   cmd,
		Stdin:  os.Stdin,
		Stdout: stdout,
		Stderr: stderr,
	}
	err := s.sshClient.RunCommand(command)
	if err != nil {
		return false
	}
	return true
}

//ExecWithSudo executes the provided command using sudo privileges. The stdout
//and stderr of the command are written to the specified stdout / stderr
//buffers accordingly. Returns false if execution of cmd failed.
func (s *SSHMeta) ExecWithSudo(cmd string, stdout io.Writer, stderr io.Writer) bool {
	command := fmt.Sprintf("sudo %s", cmd)
	return s.Execute(command, stdout, stderr)
}

//Exec executes the provided cmd and returns metadata about its result in CmdRes
func (s *SSHMeta) Exec(cmd string) *CmdRes {
	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)

	exit := s.Execute(cmd, stdout, stderr)

	return &CmdRes{
		cmd:    cmd,
		stdout: stdout,
		stderr: stderr,
		exit:   exit,
	}
}

//ExecContext run a command in background and stop when cancel the context
func (s *SSHMeta) ExecContext(ctx context.Context, cmd string) *CmdRes {
	if ctx == nil {
		panic("no context provided")
	}

	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)

	command := &SSHCommand{
		Path:   cmd,
		Stdin:  os.Stdin,
		Stdout: stdout,
		Stderr: stderr,
	}

	go func() {
		s.sshClient.RunCommandContext(ctx, command)
	}()

	return &CmdRes{
		cmd:    cmd,
		stdout: stdout,
		stderr: stderr,
		exit:   false,
	}
}
<commit_msg>test/helpers: cleanup comments in node.go<commit_after>// Copyright 2017 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package helpers

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"os"

	log "github.com/sirupsen/logrus"
)

// SSHMeta contains metadata to SSH into a remote location to run tests
type SSHMeta struct {
	sshClient *SSHClient
	env       []string
}

// CreateSSHMeta returns an SSHMeta with the specified host, port, and user, as
// well as an according SSHClient.
func CreateSSHMeta(host string, port int, user string) *SSHMeta {
	return &SSHMeta{
		sshClient: GetSSHClient(host, port, user),
	}
}

func (s *SSHMeta) String() string {
	return fmt.Sprintf("environment: %s, SSHClient: %s", s.env, s.sshClient.String())

}

// GetVagrantSSHMetadata returns a SSHMeta initialized based on the provided
// SSH-config target.
func GetVagrantSSHMetadata(vmName string) *SSHMeta {
	var vagrant Vagrant
	config, err := vagrant.GetVagrantSSHMetadata(vmName)
	if err != nil {
		return nil
	}

	log.Debugf("generated SSHConfig for node %s", vmName)
	nodes, err := ImportSSHconfig(config)
	if err != nil {
		return nil
	}
	log.Debugf("done importing ssh config")
	node := nodes[vmName]
	if node == nil {
		return nil
	}

	return &SSHMeta{
		sshClient: node.GetSSHClient(),
	}
}

// Execute executes cmd on the provided node and stores the stdout / stderr of
// the command in the provided buffers. Returns false if the command failed
// during its execution.
func (s *SSHMeta) Execute(cmd string, stdout io.Writer, stderr io.Writer) bool {
	if stdout == nil {
		stdout = os.Stdout
	}

	if stderr == nil {
		stderr = os.Stderr
	}

	command := &SSHCommand{
		Path:   cmd,
		Stdin:  os.Stdin,
		Stdout: stdout,
		Stderr: stderr,
	}
	err := s.sshClient.RunCommand(command)
	if err != nil {
		return false
	}
	return true
}

// ExecWithSudo executes the provided command using sudo privileges via SSH.
// The stdout  and stderr of the command are written to the specified stdout and
// stderr buffers accordingly. Returns false if execution of cmd fails.
func (s *SSHMeta) ExecWithSudo(cmd string, stdout io.Writer, stderr io.Writer) bool {
	command := fmt.Sprintf("sudo %s", cmd)
	return s.Execute(command, stdout, stderr)
}

// Exec returns the results of executing the provided cmd via SSH.
func (s *SSHMeta) Exec(cmd string) *CmdRes {
	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)

	exit := s.Execute(cmd, stdout, stderr)

	return &CmdRes{
		cmd:    cmd,
		stdout: stdout,
		stderr: stderr,
		exit:   exit,
	}
}

// ExecContext returns the results of running cmd via SSH in the specified
// context.
func (s *SSHMeta) ExecContext(ctx context.Context, cmd string) *CmdRes {
	if ctx == nil {
		panic("no context provided")
	}

	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)

	command := &SSHCommand{
		Path:   cmd,
		Stdin:  os.Stdin,
		Stdout: stdout,
		Stderr: stderr,
	}

	go func() {
		s.sshClient.RunCommandContext(ctx, command)
	}()

	return &CmdRes{
		cmd:    cmd,
		stdout: stdout,
		stderr: stderr,
		exit:   false,
	}
}
<|endoftext|>


<commit_before>package gamepaicoresdk // import "gamecenter.mobi/paicode/client/gamepaicoresdk"

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gocraft/web"
	"github.com/hyperledger/fabric/peerex"

	clicore "gamecenter.mobi/paicode/client"
	gamepaicorecommon "gamecenter.mobi/paicode/client/gamepaicorecommon"
)

const defPaicodeName string = "gamepaicore_v01"
const defRegion string = "gamepai01"

var restLogger = peerex.InitLogger("gamepaiREST")

//var debugmode bool = false
var offlinemode bool = false

//var logtostd bool = false
var listenaddr string = ""
var router *web.Router

type GamepaiCoreConfig struct {
	FileSystemPath string
	CrtFileName    string
	YamlFileName   string
	Address        string
	Port           int
}

func StartCoreDaemon(config string) string {
	log.Println("config: ", config)

	var coreConfig GamepaiCoreConfig
	err := json.Unmarshal([]byte(config), &coreConfig)
	if err != nil {
		log.Println("Parse config error: ", err)
		return fmt.Sprintf("failed.")
	}

	log.Println("FileSystemPath: ", coreConfig.FileSystemPath)
	log.Println("CrtFileName: ", coreConfig.CrtFileName)
	log.Println("YamlFileName: ", coreConfig.YamlFileName)
	log.Println("Address: ", coreConfig.Address)
	log.Println("Port: ", coreConfig.Port)
	crtFile := filepath.Join(coreConfig.FileSystemPath, coreConfig.CrtFileName+".crt")
	yamlFile := filepath.Join(coreConfig.FileSystemPath, coreConfig.YamlFileName+".yaml")
	log.Println("CrtFile: ", crtFile)
	log.Println("YamlFile: ", yamlFile)

	globalConfig := &peerex.GlobalConfig{}
	globalConfig.ConfigPath = make([]string, 1, 10)
	globalConfig.ConfigPath[0] = coreConfig.FileSystemPath // Path to look for the config file in
	globalConfig.ConfigFileName = coreConfig.YamlFileName

	defaultViperSetting := make(map[string]string)
	defaultViperSetting["peer.fileSystemPath"] = coreConfig.FileSystemPath
	defaultViperSetting["peer.tls.rootcert.file"] = crtFile

	err = globalConfig.InitGlobalWrapper(true, defaultViperSetting)
	if err != nil {
		log.Println("Init global config error: ", err)
		return fmt.Sprintf("failed.")
	}

	err = os.MkdirAll(globalConfig.GetPeerFS(), 0777)
	if err != nil {
		restLogger.Error("Mkdir error: ", err)
		return fmt.Sprintf("failed.")
	}

	gamepaicorecommon.DefClient = clicore.NewClientCore(globalConfig)

	if !offlinemode {

		conn := peerex.ClientConn{}

		err := conn.Dialdefault()
		if err != nil {
			restLogger.Error("Dial default error: ", err)
			return fmt.Sprintf("failed.")
		}

		gamepaicorecommon.DefClient.PrepareRpc(conn)
		gamepaicorecommon.DefClient.SetRpcRegion(defRegion)
		gamepaicorecommon.DefClient.Rpc.Rpcbuilder.ChaincodeName = defPaicodeName
		restLogger.Infof("Start rpc, chaincode is %s", gamepaicorecommon.DefClient.Rpc.Rpcbuilder.ChaincodeName)
	} else {
		restLogger.Info("Run under off-line mode")
	}

	if listenaddr == "" {
		listenaddr = fmt.Sprintf("%s:%d", coreConfig.Address, coreConfig.Port)
		//listenaddr = "0.0.0.0:7280"
	}

	gamepaicorecommon.DefClient.Accounts.KeyMgr.Load()
	//defer defClient.Accounts.KeyMgr.Persist()

	// Initialize the REST service object
	restLogger.Infof("Initializing the REST service on %s", listenaddr)
	router = gamepaicorecommon.BuildRouter()
	go startHttpServer()

	return "success"
}

func startHttpServer() {
	restLogger.Info("Starting HTTP Server ...")
	err := http.ListenAndServe(listenaddr, router)
	restLogger.Info("HTTP server is stopped.")
	if err != nil {
		restLogger.Error("Listen and Serve error: ", err)
	}

	if gamepaicorecommon.DefClient.IsRpcReady() {
		gamepaicorecommon.DefClient.ReleaseRpc()
	}
}
<commit_msg>add SDKVersion<commit_after>package gamepaicoresdk // import "gamecenter.mobi/paicode/client/gamepaicoresdk"

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gocraft/web"
	"github.com/hyperledger/fabric/peerex"

	clicore "gamecenter.mobi/paicode/client"
	gamepaicorecommon "gamecenter.mobi/paicode/client/gamepaicorecommon"
)

const defPaicodeName string = "gamepaicore_v01"
const defRegion string = "gamepai01"
const defSDKVersion string = "1.0.0"

var restLogger = peerex.InitLogger("gamepaiREST")

//var debugmode bool = false
var offlinemode bool = false

//var logtostd bool = false
var listenaddr string = ""
var router *web.Router

type GamepaiCoreConfig struct {
	FileSystemPath string
	CrtFileName    string
	YamlFileName   string
	Address        string
	Port           int
}

func StartCoreDaemon(config string) string {
	log.Println("config: ", config)

	var coreConfig GamepaiCoreConfig
	err := json.Unmarshal([]byte(config), &coreConfig)
	if err != nil {
		log.Println("Parse config error: ", err)
		return fmt.Sprintf("failed.")
	}

	log.Println("FileSystemPath: ", coreConfig.FileSystemPath)
	log.Println("CrtFileName: ", coreConfig.CrtFileName)
	log.Println("YamlFileName: ", coreConfig.YamlFileName)
	log.Println("Address: ", coreConfig.Address)
	log.Println("Port: ", coreConfig.Port)
	crtFile := filepath.Join(coreConfig.FileSystemPath, coreConfig.CrtFileName+".crt")
	yamlFile := filepath.Join(coreConfig.FileSystemPath, coreConfig.YamlFileName+".yaml")
	log.Println("CrtFile: ", crtFile)
	log.Println("YamlFile: ", yamlFile)

	globalConfig := &peerex.GlobalConfig{}
	globalConfig.ConfigPath = make([]string, 1, 10)
	globalConfig.ConfigPath[0] = coreConfig.FileSystemPath // Path to look for the config file in
	globalConfig.ConfigFileName = coreConfig.YamlFileName

	defaultViperSetting := make(map[string]string)
	defaultViperSetting["peer.fileSystemPath"] = coreConfig.FileSystemPath
	defaultViperSetting["peer.tls.rootcert.file"] = crtFile

	err = globalConfig.InitGlobalWrapper(true, defaultViperSetting)
	if err != nil {
		log.Println("Init global config error: ", err)
		return fmt.Sprintf("failed.")
	}

	err = os.MkdirAll(globalConfig.GetPeerFS(), 0777)
	if err != nil {
		restLogger.Error("Mkdir error: ", err)
		return fmt.Sprintf("failed.")
	}

	gamepaicorecommon.DefClient = clicore.NewClientCore(globalConfig)

	if !offlinemode {

		conn := peerex.ClientConn{}

		err := conn.Dialdefault()
		if err != nil {
			restLogger.Error("Dial default error: ", err)
			return fmt.Sprintf("failed.")
		}

		gamepaicorecommon.DefClient.PrepareRpc(conn)
		gamepaicorecommon.DefClient.SetRpcRegion(defRegion)
		gamepaicorecommon.DefClient.Rpc.Rpcbuilder.ChaincodeName = defPaicodeName
		restLogger.Infof("Start rpc, chaincode is %s", gamepaicorecommon.DefClient.Rpc.Rpcbuilder.ChaincodeName)
	} else {
		restLogger.Info("Run under off-line mode")
	}

	if listenaddr == "" {
		listenaddr = fmt.Sprintf("%s:%d", coreConfig.Address, coreConfig.Port)
		//listenaddr = "0.0.0.0:7280"
	}

	gamepaicorecommon.DefClient.Accounts.KeyMgr.Load()
	//defer defClient.Accounts.KeyMgr.Persist()

	// Initialize the REST service object
	restLogger.Infof("Initializing the REST service on %s", listenaddr)
	router = gamepaicorecommon.BuildRouter()
	go startHttpServer()

	return "success"
}

func GetSDKVersion() string {
	return defSDKVersion
}

func startHttpServer() {
	restLogger.Info("Starting HTTP Server ...")
	err := http.ListenAndServe(listenaddr, router)
	restLogger.Info("HTTP server is stopped.")
	if err != nil {
		restLogger.Error("Listen and Serve error: ", err)
	}

	if gamepaicorecommon.DefClient.IsRpcReady() {
		gamepaicorecommon.DefClient.ReleaseRpc()
	}
}
<|endoftext|>


<commit_before>package broker_test

import (
	"testing"
	"github.com/buptmiao/msgo/broker"
	"github.com/buptmiao/msgo/client"
	"github.com/buptmiao/msgo/msg"
	"fmt"
	"sync"
)

func TestSubscribeAndPublish(t *testing.T) {
	loadConfig()
	//broker.EnableDebug()
	b := broker.GetInstance()
	go b.Start()
	addr := fmt.Sprintf("127.0.0.1:%d", broker.Config.MsgPort)
	consumer := client.NewConsumer(addr)
	wg := sync.WaitGroup{}
	wg.Add(4)
	err := consumer.Subscribe("msgo", "msgo", func(m ...*msg.Message) error {
		for _, v := range m {
			fmt.Println(string(v.GetBody()))
		}
		wg.Done()
		return nil
	})
	if err != nil {
		panic(err)
	}

	producer := client.NewProducer(addr)
	producer.PublishDirect("msgo", "msgo", []byte("hello world1"))
	producer.PublishDirectPersist("msgo", "msgo", []byte("hello world2"))
	producer.PublishFanout("msgo", "msgo", []byte("hello world3"))
	producer.PublishFanoutPersist("msgo", "msgo", []byte("hello world4"))
	producer.PublishDirect("msgo", "msgo2", []byte("hello world5"))
	wg.Wait()

	err = consumer.UnSubscribe("msgo")
	if err != nil {
		panic(err)
	}
	b.Stop()
	b.Storage().Truncate()
}
<commit_msg>Fix test cases<commit_after>package broker_test

import (
	"testing"
	"github.com/buptmiao/msgo/broker"
	"github.com/buptmiao/msgo/client"
	"github.com/buptmiao/msgo/msg"
	"fmt"
	"sync"
)

func TestSubscribeAndPublish(t *testing.T) {
	loadConfig()
	broker.EnableDebug()
	b := broker.GetInstance()
	go b.Start()
	addr := fmt.Sprintf("127.0.0.1:%d", broker.Config.MsgPort)
	consumer := client.NewConsumer(addr)
	wg := sync.WaitGroup{}
	wg.Add(4)
	err := consumer.Subscribe("msgo", "msgo", func(m ...*msg.Message) error {
		for _, v := range m {
			fmt.Println(string(v.GetBody()))
			wg.Done()
		}
		return nil
	})
	if err != nil {
		panic(err)
	}

	producer := client.NewProducer(addr)
	producer.PublishDirect("msgo", "msgo", []byte("hello world1"))
	producer.PublishDirectPersist("msgo", "msgo", []byte("hello world2"))
	producer.PublishFanout("msgo", "msgo", []byte("hello world3"))
	producer.PublishFanoutPersist("msgo", "msgo", []byte("hello world4"))
	producer.PublishDirect("msgo", "msgo2", []byte("hello world5"))
	wg.Wait()

	err = consumer.UnSubscribe("msgo")
	if err != nil {
		panic(err)
	}
	b.Stop()
	b.Storage().Truncate()
}
<|endoftext|>


<commit_before>package examples

import (
	"fmt"
	simulationclient "github.com/3dsim/simulation-goclient/client"
	"github.com/3dsim/simulation-goclient/client/operations"
	"github.com/3dsim/simulation-goclient/models"
	openapiclient "github.com/go-openapi/runtime/client"
	"github.com/go-openapi/strfmt"
	"github.com/stretchr/testify/assert"
	"testing"
)

func _TestExampleUseOfAPIWithAuthentication(t *testing.T) {
	token := "token received from auth0 (without Bearer in front)"
	bearerTokenAuth := openapiclient.BearerToken(token)

	client := simulationclient.New(openapiclient.New("3dsim-qa.cloud.tyk.io", "simulation-api", []string{"https"}), strfmt.Default)

	simulation := models.Simulation{
		OrganizationID: int32ToPointer(1),
		Title:          stringToPointer("Title"),
	}
	assumedStrainParams := models.AssumedStrainSimulationParameters{
		AnisotropicStrainCoefficientsZ:             float64ToPointer(1.0),
		AnisotropicStrainCoefficientsParallel:      float64ToPointer(1.5),
		AnisotropicStrainCoefficientsPerpendicular: float64ToPointer(0.5),
		AssumedStrain:                              float64ToPointer(1.0),
		ElasticModulus:                             float64ToPointer(208e9),
		HatchSpacing:                               float64ToPointer(100e-6),
		LaserWattage:                               float64ToPointer(195),
		LayerThickness:                             float64ToPointer(50e-6),
		LayerRotationAngle:                         float64ToPointer(67),
		MaximumWallDistance:                        float64ToPointer(3e-3),
		MaximumWallThickness:                       float64ToPointer(1e-3),
		MinimumSupportHeight:                       float64ToPointer(5e-3),
		MinimumWallDistance:                        float64ToPointer(0),
		MinimumWallThickness:                       float64ToPointer(5e-5),
		OutputShrinkage:                            boolToPointer(true),
		PoissonRatio:                               float64ToPointer(0.33),
		StrainScalingFactor:                        float64ToPointer(1.0),
		ScanSpeed:                                  float64ToPointer(1.0),
		SlicingStripeWidth:                         float64ToPointer(2e-3),
		StartingLayerAngle:                         float64ToPointer(57),
		StressMode:                                 stringToPointer("LinearElastic"),
		SupportAngle:                               float64ToPointer(45),
		SupportFactorOfSafety:                      float64ToPointer(1.0),
		SupportOptimization:                        boolToPointer(true),
		SupportYieldStrength:                       float64ToPointer(480e6),
		SupportYieldStrengthRatio:                  float64ToPointer(0.4375),
		UsePeriodicAnalysis:                        boolToPointer(false),
		VoxelSize:                                  float64ToPointer(5e-4),
	}
	simulationToCreate := &models.AssumedStrainSimulation{
		Simulation:                        simulation,
		AssumedStrainSimulationParameters: assumedStrainParams,
	}
	createdSimulation, err := client.Operations.PostAssumedStrainSimulation(operations.NewPostAssumedStrainSimulationParams().WithAssumedStrainSimulation(simulationToCreate), bearerTokenAuth)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("Result: %v\n", createdSimulation)
}

func _TestExampleGetSimulations(t *testing.T) {
	token := "token received from auth0 (without Bearer in front)"
	bearerTokenAuth := openapiclient.BearerToken(token)

	client := simulationclient.New(openapiclient.New("3dsim-qa.cloud.tyk.io", "simulation-api", []string{"https"}), strfmt.Default)

	simulations, err := client.Operations.GetSimulations(operations.NewGetSimulationsParams().WithStatus([]string{"InProgress", "Error", "Success"}).WithOrganizationID(1), bearerTokenAuth)
	if err != nil {
		t.Fatal(err)
	}

	assert.NotEmpty(t, simulations.Payload, "Expected some simulations returned")
	fmt.Printf("Result: %v\n", simulations.Payload)
}

func _TestPatch(t *testing.T) {
	token := "sample token"
	bearerTokenAuth := openapiclient.BearerToken(token)

	client := simulationclient.New(openapiclient.New("localhost:5000", "", []string{"http"}), strfmt.Default)

	patch := &models.PatchDocument{Op: stringToPointer(models.PatchDocumentOpReplace), Path: stringToPointer("/title"), Value: "Test Patch"}
	patch2 := &models.PatchDocument{Op: stringToPointer(models.PatchDocumentOpReplace), Path: stringToPointer("/status"), Value: models.SimulationStatusInProgress}
	patchList := []*models.PatchDocument{patch, patch2}
	patchedSimulation, err := client.Operations.PatchSimulation(operations.NewPatchSimulationParams().WithSimulationPatch(patchList).WithID(65), bearerTokenAuth)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("Result: %v\n", patchedSimulation)
}

func stringToPointer(text string) *string {
	return &text
}

func boolToPointer(b bool) *bool {
	return &b
}

func int64ToPointer(i int64) *int64 {
	return &i
}

func int32ToPointer(i int32) *int32 {
	return &i
}

func float64ToPointer(f float64) *float64 {
	return &f
}
<commit_msg>Remove unused field<commit_after>package examples

import (
	"fmt"
	simulationclient "github.com/3dsim/simulation-goclient/client"
	"github.com/3dsim/simulation-goclient/client/operations"
	"github.com/3dsim/simulation-goclient/models"
	openapiclient "github.com/go-openapi/runtime/client"
	"github.com/go-openapi/strfmt"
	"github.com/stretchr/testify/assert"
	"testing"
)

func _TestExampleUseOfAPIWithAuthentication(t *testing.T) {
	token := "token received from auth0 (without Bearer in front)"
	bearerTokenAuth := openapiclient.BearerToken(token)

	client := simulationclient.New(openapiclient.New("3dsim-qa.cloud.tyk.io", "simulation-api", []string{"https"}), strfmt.Default)

	simulation := models.Simulation{
		OrganizationID: int32ToPointer(1),
		Title:          stringToPointer("Title"),
	}
	assumedStrainParams := models.AssumedStrainSimulationParameters{
		AnisotropicStrainCoefficientsZ:             float64ToPointer(1.0),
		AnisotropicStrainCoefficientsParallel:      float64ToPointer(1.5),
		AnisotropicStrainCoefficientsPerpendicular: float64ToPointer(0.5),
		AssumedStrain:                              float64ToPointer(1.0),
		ElasticModulus:                             float64ToPointer(208e9),
		HatchSpacing:                               float64ToPointer(100e-6),
		LaserWattage:                               float64ToPointer(195),
		LayerThickness:                             float64ToPointer(50e-6),
		LayerRotationAngle:                         float64ToPointer(67),
		MaximumWallDistance:                        float64ToPointer(3e-3),
		MaximumWallThickness:                       float64ToPointer(1e-3),
		MinimumSupportHeight:                       float64ToPointer(5e-3),
		MinimumWallDistance:                        float64ToPointer(0),
		MinimumWallThickness:                       float64ToPointer(5e-5),
		OutputShrinkage:                            boolToPointer(true),
		PoissonRatio:                               float64ToPointer(0.33),
		StrainScalingFactor:                        float64ToPointer(1.0),
		ScanSpeed:                                  float64ToPointer(1.0),
		SlicingStripeWidth:                         float64ToPointer(2e-3),
		StartingLayerAngle:                         float64ToPointer(57),
		StressMode:                                 stringToPointer("LinearElastic"),
		SupportAngle:                               float64ToPointer(45),
		SupportFactorOfSafety:                      float64ToPointer(1.0),
		SupportOptimization:                        boolToPointer(true),
		SupportYieldStrength:                       float64ToPointer(480e6),
		SupportYieldStrengthRatio:                  float64ToPointer(0.4375),
		VoxelSize:                                  float64ToPointer(5e-4),
	}
	simulationToCreate := &models.AssumedStrainSimulation{
		Simulation:                        simulation,
		AssumedStrainSimulationParameters: assumedStrainParams,
	}
	createdSimulation, err := client.Operations.PostAssumedStrainSimulation(operations.NewPostAssumedStrainSimulationParams().WithAssumedStrainSimulation(simulationToCreate), bearerTokenAuth)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("Result: %v\n", createdSimulation)
}

func _TestExampleGetSimulations(t *testing.T) {
	token := "token received from auth0 (without Bearer in front)"
	bearerTokenAuth := openapiclient.BearerToken(token)

	client := simulationclient.New(openapiclient.New("3dsim-qa.cloud.tyk.io", "simulation-api", []string{"https"}), strfmt.Default)

	simulations, err := client.Operations.GetSimulations(operations.NewGetSimulationsParams().WithStatus([]string{"InProgress", "Error", "Success"}).WithOrganizationID(1), bearerTokenAuth)
	if err != nil {
		t.Fatal(err)
	}

	assert.NotEmpty(t, simulations.Payload, "Expected some simulations returned")
	fmt.Printf("Result: %v\n", simulations.Payload)
}

func _TestPatch(t *testing.T) {
	token := "sample token"
	bearerTokenAuth := openapiclient.BearerToken(token)

	client := simulationclient.New(openapiclient.New("localhost:5000", "", []string{"http"}), strfmt.Default)

	patch := &models.PatchDocument{Op: stringToPointer(models.PatchDocumentOpReplace), Path: stringToPointer("/title"), Value: "Test Patch"}
	patch2 := &models.PatchDocument{Op: stringToPointer(models.PatchDocumentOpReplace), Path: stringToPointer("/status"), Value: models.SimulationStatusInProgress}
	patchList := []*models.PatchDocument{patch, patch2}
	patchedSimulation, err := client.Operations.PatchSimulation(operations.NewPatchSimulationParams().WithSimulationPatch(patchList).WithID(65), bearerTokenAuth)
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("Result: %v\n", patchedSimulation)
}

func stringToPointer(text string) *string {
	return &text
}

func boolToPointer(b bool) *bool {
	return &b
}

func int64ToPointer(i int64) *int64 {
	return &i
}

func int32ToPointer(i int32) *int32 {
	return &i
}

func float64ToPointer(f float64) *float64 {
	return &f
}
<|endoftext|>


<commit_before>package ant

import "testing"

func TestSlack(t *testing.T) {
	s := &Slack{
		URL:     "https://hooks.slack.com/services/xx",
		IconURL: "https://txthinking-file.storage.googleapis.com/a1d234ca87944bf7b4f1e8c8d06e4474/NoName.jpeg",
	}
	//if err := s.Send("hello bot"); err != nil {
	//t.Fatal(err)
	//}
	//if err := s.Send([]string{"hello bot", "hello jerry"}); err != nil {
	//t.Fatal(err)
	//}
	//if err := s.Send(map[string]string{"hello": "1", "world": "2"}); err != nil {
	//t.Fatal(err)
	//}
	//if err := s.Send(&http.Client{}); err != nil {
	//t.Fatal(err)
	//}
}
<commit_msg>fixed bug<commit_after>package ant

import "testing"

func TestSlack(t *testing.T) {
	//s := &Slack{
	//URL:     "https://hooks.slack.com/services/xx",
	//IconURL: "https://txthinking-file.storage.googleapis.com/a1d234ca87944bf7b4f1e8c8d06e4474/NoName.jpeg",
	//}
	//if err := s.Send("hello bot"); err != nil {
	//t.Fatal(err)
	//}
	//if err := s.Send([]string{"hello bot", "hello jerry"}); err != nil {
	//t.Fatal(err)
	//}
	//if err := s.Send(map[string]string{"hello": "1", "world": "2"}); err != nil {
	//t.Fatal(err)
	//}
	//if err := s.Send(&http.Client{}); err != nil {
	//t.Fatal(err)
	//}
}
<|endoftext|>


<commit_before>//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type IAPConfig

package googlecompute

import (
	"bufio"
	"bytes"
	"context"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"runtime"
	"strconv"
	"strings"
	"text/template"
	"time"

	"github.com/hashicorp/packer/common/net"
	"github.com/hashicorp/packer/common/retry"
	"github.com/hashicorp/packer/helper/communicator"
	"github.com/hashicorp/packer/helper/multistep"
	"github.com/hashicorp/packer/packer"
	"github.com/hashicorp/packer/packer/tmp"
)

// StepStartTunnel represents a Packer build step that launches an IAP tunnel
type IAPConfig struct {
	// Whether to use an IAP proxy.
	// Prerequisites and limitations for using IAP:
	// - You must manually enable the IAP API in the Google Cloud console.
	// - You must have the gcloud sdk installed on the computer running Packer.
	// - You must be using a Service Account with a credentials file (using the
	//	 account_file option in the Packer template)
	// - This is currently only implemented for the SSH communicator, not the
	//   WinRM Communicator.
	// - You must add the given service account to project level IAP permissions
	//   in https://console.cloud.google.com/security/iap. To do so, click
	//   "project" > "SSH and TCP resoures" > "All Tunnel Resources" >
	//   "Add Member". Then add your service account and choose the role
	//   "IAP-secured Tunnel User" and add any conditions you may care about.
	IAP bool `mapstructure:"use_iap" required:"false"`
	// Which port to connect the local end of the IAM localhost proxy to. If
	// left blank, Packer will choose a port for you from available ports.
	IAPLocalhostPort int `mapstructure:"iap_localhost_port"`
	// What "hashbang" to use to invoke script that sets up gcloud.
	// Default: "/bin/sh"
	IAPHashBang string `mapstructure:"iap_hashbang" required:"false"`
	// What file extension to use for script that sets up gcloud.
	// Default: ".sh"
	IAPExt string `mapstructure:"iap_ext" required:"false"`
}

type TunnelDriver interface {
	StartTunnel(context.Context, string) error
	StopTunnel()
}

func RunTunnelCommand(cmd *exec.Cmd) error {
	// set stdout and stderr so we can read what's going on.
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		err := fmt.Errorf("Error calling gcloud sdk to launch IAP tunnel: %s",
			err)
		return err
	}

	// Give tunnel 30 seconds to either launch, or return an error.
	// Unfortunately, the SDK doesn't provide any official acknowledgment that
	// the tunnel is launched when it's not being run through a TTY so we
	// are just trusting here that 30s is enough to know whether the tunnel
	// launch was going to fail. Yep, feels icky to me too. But I spent an
	// afternoon trying to figure out how to get the SDK to actually send
	// the "Listening on port [n]" line I see when I run it manually, and I
	// can't justify spending more time than that on aesthetics.
	for i := 0; i < 30; i++ {
		time.Sleep(1 * time.Second)

		lineStderr, err := stderr.ReadString('\n')
		if err != nil && err != io.EOF {
			log.Printf("Err from scanning stderr is %s", err)
			return fmt.Errorf("Error reading stderr from tunnel launch: %s", err)
		}
		if lineStderr != "" {
			log.Printf("stderr: %s", lineStderr)
		}

		lineStdout, err := stdout.ReadString('\n')
		if err != nil && err != io.EOF {
			log.Printf("Err from scanning stdout is %s", err)
			return fmt.Errorf("Error reading stdout from tunnel launch: %s", err)
		}
		if lineStdout != "" {
			log.Printf("stdout: %s", lineStdout)
		}

		if strings.Contains(lineStderr, "ERROR") {
			// 4033: Either you don't have permission to access the instance,
			// the instance doesn't exist, or the instance is stopped.
			// The two sub-errors we may see while the permissions settle are
			// "not authorized" and "failed to connect to backend," but after
			// about a minute of retries this goes away and we're able to
			// connect.
			// 4003: "failed to connect to backend". Network blip.
			if strings.Contains(lineStderr, "4033") || strings.Contains(lineStderr, "4003") {
				return RetryableTunnelError{lineStderr}
			} else {
				log.Printf("NOT RETRYABLE: %s", lineStderr)
				return fmt.Errorf("Non-retryable tunnel error: %s", lineStderr)
			}
		}
	}

	log.Printf("No error detected after tunnel launch; continuing...")
	return nil
}

type RetryableTunnelError struct {
	s string
}

func (e RetryableTunnelError) Error() string {
	return "Tunnel start: " + e.s
}

type StepStartTunnel struct {
	IAPConf     *IAPConfig
	CommConf    *communicator.Config
	AccountFile string
	ProjectId   string

	tunnelDriver TunnelDriver
}

func (s *StepStartTunnel) ConfigureLocalHostPort(ctx context.Context) error {
	if s.IAPConf.IAPLocalhostPort == 0 {
		log.Printf("Finding an available TCP port for IAP proxy")
		l, err := net.ListenRangeConfig{
			Min:     8000,
			Max:     9000,
			Addr:    "0.0.0.0",
			Network: "tcp",
		}.Listen(ctx)

		if err != nil {
			err := fmt.Errorf("error finding an available port to initiate a session tunnel: %s", err)
			return err
		}

		s.IAPConf.IAPLocalhostPort = l.Port
		l.Close()
		log.Printf("Setting up proxy to listen on localhost at %d",
			s.IAPConf.IAPLocalhostPort)
	}
	return nil
}

func (s *StepStartTunnel) createTempGcloudScript(args []string) (string, error) {
	// Generate temp script that contains both gcloud auth and gcloud compute
	// iap launch call.

	// Create temp file.
	tf, err := tmp.File("gcloud-setup")
	if err != nil {
		return "", fmt.Errorf("Error preparing gcloud setup script: %s", err)
	}
	defer tf.Close()
	// Write our contents to it
	writer := bufio.NewWriter(tf)

	if s.IAPConf.IAPHashBang != "" {
		s.IAPConf.IAPHashBang = fmt.Sprintf("#!%s\n", s.IAPConf.IAPHashBang)
		log.Printf("[INFO] (google): Prepending inline gcloud setup script with %s",
			s.IAPConf.IAPHashBang)
		_, err = writer.WriteString(s.IAPConf.IAPHashBang)
		if err != nil {
			return "", fmt.Errorf("Error preparing inline hashbang: %s", err)
		}

	}

	launchTemplate := `
gcloud auth activate-service-account --key-file='{{.AccountFile}}'
gcloud config set project {{.ProjectID}}
{{.Args}}
`
	if runtime.GOOS == "windows" {
		launchTemplate = `
call gcloud auth activate-service-account --key-file "{{.AccountFile}}"
call gcloud config set project {{.ProjectID}}
call {{.Args}}
`
	}
	// call command
	args = append([]string{"gcloud"}, args...)
	argString := strings.Join(args, " ")

	var tpl = template.Must(template.New("createTunnel").Parse(launchTemplate))
	var b bytes.Buffer

	opts := map[string]string{
		"AccountFile": s.AccountFile,
		"ProjectID":   s.ProjectId,
		"Args":        argString,
	}

	err = tpl.Execute(&b, opts)
	if err != nil {
		fmt.Println(err)
	}

	if _, err := writer.WriteString(b.String()); err != nil {
		return "", fmt.Errorf("Error preparing gcloud shell script: %s", err)
	}

	if err := writer.Flush(); err != nil {
		return "", fmt.Errorf("Error preparing shell script: %s", err)
	}
	// Have to close temp file before renaming it or Windows will complain.
	tf.Close()
	err = os.Chmod(tf.Name(), 0700)
	if err != nil {
		log.Printf("[ERROR] (google): error modifying permissions of temp script file: %s", err.Error())
	}

	// figure out what extension the file should have, and rename it.
	tempScriptFileName := tf.Name()
	if s.IAPConf.IAPExt != "" {
		err := os.Rename(tempScriptFileName, fmt.Sprintf("%s%s", tempScriptFileName, s.IAPConf.IAPExt))
		if err != nil {
			return "", fmt.Errorf("Error setting the correct temp file extension: %s", err)
		}
		tempScriptFileName = fmt.Sprintf("%s%s", tempScriptFileName, s.IAPConf.IAPExt)
	}

	return tempScriptFileName, nil
}

// Run executes the Packer build step that creates an IAP tunnel.
func (s *StepStartTunnel) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	if !s.IAPConf.IAP {
		log.Printf("Skipping step launch IAP tunnel; \"iap\" is false.")
		return multistep.ActionContinue
	}

	// shell out to create the tunnel.
	ui := state.Get("ui").(packer.Ui)
	instanceName := state.Get("instance_name").(string)
	c := state.Get("config").(*Config)

	ui.Say("Step Launch IAP Tunnel...")

	err := s.ConfigureLocalHostPort(ctx)
	if err != nil {
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	// Generate list of args to use to call gcloud cli.
	args := []string{"compute", "start-iap-tunnel", instanceName,
		strconv.Itoa(s.CommConf.Port()),
		fmt.Sprintf("--local-host-port=localhost:%d", s.IAPConf.IAPLocalhostPort),
		"--zone", c.Zone,
	}

	// This is the port the IAP tunnel listens on, on localhost.
	// TODO make setting LocalHostPort optional
	s.CommConf.SSHPort = s.IAPConf.IAPLocalhostPort

	log.Printf("Creating tunnel launch script with args %#v", args)
	// Create temp file that contains both gcloud authentication, and gcloud
	// proxy setup call.
	tempScriptFileName, err := s.createTempGcloudScript(args)
	if err != nil {
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer os.Remove(tempScriptFileName)

	s.tunnelDriver = NewTunnelDriver()

	err = retry.Config{
		Tries: 11,
		ShouldRetry: func(err error) bool {
			switch err.(type) {
			case RetryableTunnelError:
				return true
			default:
				return false
			}
		},
		RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
	}.Run(ctx, func(ctx context.Context) error {
		// tunnel launcher/destroyer has to be different on windows vs. unix.
		err := s.tunnelDriver.StartTunnel(ctx, tempScriptFileName)
		return err
	})
	if err != nil {
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	return multistep.ActionContinue
}

// Cleanup stops the IAP tunnel and cleans up processes.
func (s *StepStartTunnel) Cleanup(state multistep.StateBag) {
	if !s.IAPConf.IAP {
		log.Printf("Skipping cleanup of IAP tunnel; \"iap\" is false.")
		return
	}
	if s.tunnelDriver != nil {
		s.tunnelDriver.StopTunnel()
	}
}
<commit_msg>use local port in listener config, when set<commit_after>//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type IAPConfig

package googlecompute

import (
	"bufio"
	"bytes"
	"context"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"runtime"
	"strconv"
	"strings"
	"text/template"
	"time"

	"github.com/hashicorp/packer/common/net"
	"github.com/hashicorp/packer/common/retry"
	"github.com/hashicorp/packer/helper/communicator"
	"github.com/hashicorp/packer/helper/multistep"
	"github.com/hashicorp/packer/packer"
	"github.com/hashicorp/packer/packer/tmp"
)

// StepStartTunnel represents a Packer build step that launches an IAP tunnel
type IAPConfig struct {
	// Whether to use an IAP proxy.
	// Prerequisites and limitations for using IAP:
	// - You must manually enable the IAP API in the Google Cloud console.
	// - You must have the gcloud sdk installed on the computer running Packer.
	// - You must be using a Service Account with a credentials file (using the
	//	 account_file option in the Packer template)
	// - This is currently only implemented for the SSH communicator, not the
	//   WinRM Communicator.
	// - You must add the given service account to project level IAP permissions
	//   in https://console.cloud.google.com/security/iap. To do so, click
	//   "project" > "SSH and TCP resoures" > "All Tunnel Resources" >
	//   "Add Member". Then add your service account and choose the role
	//   "IAP-secured Tunnel User" and add any conditions you may care about.
	IAP bool `mapstructure:"use_iap" required:"false"`
	// Which port to connect the local end of the IAM localhost proxy to. If
	// left blank, Packer will choose a port for you from available ports.
	IAPLocalhostPort int `mapstructure:"iap_localhost_port"`
	// What "hashbang" to use to invoke script that sets up gcloud.
	// Default: "/bin/sh"
	IAPHashBang string `mapstructure:"iap_hashbang" required:"false"`
	// What file extension to use for script that sets up gcloud.
	// Default: ".sh"
	IAPExt string `mapstructure:"iap_ext" required:"false"`
}

type TunnelDriver interface {
	StartTunnel(context.Context, string) error
	StopTunnel()
}

func RunTunnelCommand(cmd *exec.Cmd) error {
	// set stdout and stderr so we can read what's going on.
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		err := fmt.Errorf("Error calling gcloud sdk to launch IAP tunnel: %s",
			err)
		return err
	}

	// Give tunnel 30 seconds to either launch, or return an error.
	// Unfortunately, the SDK doesn't provide any official acknowledgment that
	// the tunnel is launched when it's not being run through a TTY so we
	// are just trusting here that 30s is enough to know whether the tunnel
	// launch was going to fail. Yep, feels icky to me too. But I spent an
	// afternoon trying to figure out how to get the SDK to actually send
	// the "Listening on port [n]" line I see when I run it manually, and I
	// can't justify spending more time than that on aesthetics.
	for i := 0; i < 30; i++ {
		time.Sleep(1 * time.Second)

		lineStderr, err := stderr.ReadString('\n')
		if err != nil && err != io.EOF {
			log.Printf("Err from scanning stderr is %s", err)
			return fmt.Errorf("Error reading stderr from tunnel launch: %s", err)
		}
		if lineStderr != "" {
			log.Printf("stderr: %s", lineStderr)
		}

		lineStdout, err := stdout.ReadString('\n')
		if err != nil && err != io.EOF {
			log.Printf("Err from scanning stdout is %s", err)
			return fmt.Errorf("Error reading stdout from tunnel launch: %s", err)
		}
		if lineStdout != "" {
			log.Printf("stdout: %s", lineStdout)
		}

		if strings.Contains(lineStderr, "ERROR") {
			// 4033: Either you don't have permission to access the instance,
			// the instance doesn't exist, or the instance is stopped.
			// The two sub-errors we may see while the permissions settle are
			// "not authorized" and "failed to connect to backend," but after
			// about a minute of retries this goes away and we're able to
			// connect.
			// 4003: "failed to connect to backend". Network blip.
			if strings.Contains(lineStderr, "4033") || strings.Contains(lineStderr, "4003") {
				return RetryableTunnelError{lineStderr}
			} else {
				log.Printf("NOT RETRYABLE: %s", lineStderr)
				return fmt.Errorf("Non-retryable tunnel error: %s", lineStderr)
			}
		}
	}

	log.Printf("No error detected after tunnel launch; continuing...")
	return nil
}

type RetryableTunnelError struct {
	s string
}

func (e RetryableTunnelError) Error() string {
	return "Tunnel start: " + e.s
}

type StepStartTunnel struct {
	IAPConf     *IAPConfig
	CommConf    *communicator.Config
	AccountFile string
	ProjectId   string

	tunnelDriver TunnelDriver
}

func (s *StepStartTunnel) ConfigureLocalHostPort(ctx context.Context) error {
	minPortNumber, maxPortNumber := 8000, 9000

	if s.IAPConf.IAPLocalhostPort == 0 {
		minPortNumber = s.IAPConf.IAPLocalhostPort
		maxPortNumber = minPortNumber
		log.Printf("Using TCP port for %d IAP proxy", s.IAPConf.IAPLocalhostPort)
	} else {
		log.Printf("Finding an available TCP port for IAP proxy")
	}

	l, err := net.ListenRangeConfig{
		Min:     minPortNumber,
		Max:     maxPortNumber,
		Addr:    "0.0.0.0",
		Network: "tcp",
	}.Listen(ctx)

	if err != nil {
		err := fmt.Errorf("error finding an available port to initiate a session tunnel: %s", err)
		return err
	}

	s.IAPConf.IAPLocalhostPort = l.Port
	l.Close()
	log.Printf("Setting up proxy to listen on localhost at %d",
		s.IAPConf.IAPLocalhostPort)

	return nil
}

func (s *StepStartTunnel) createTempGcloudScript(args []string) (string, error) {
	// Generate temp script that contains both gcloud auth and gcloud compute
	// iap launch call.

	// Create temp file.
	tf, err := tmp.File("gcloud-setup")
	if err != nil {
		return "", fmt.Errorf("Error preparing gcloud setup script: %s", err)
	}
	defer tf.Close()
	// Write our contents to it
	writer := bufio.NewWriter(tf)

	if s.IAPConf.IAPHashBang != "" {
		s.IAPConf.IAPHashBang = fmt.Sprintf("#!%s\n", s.IAPConf.IAPHashBang)
		log.Printf("[INFO] (google): Prepending inline gcloud setup script with %s",
			s.IAPConf.IAPHashBang)
		_, err = writer.WriteString(s.IAPConf.IAPHashBang)
		if err != nil {
			return "", fmt.Errorf("Error preparing inline hashbang: %s", err)
		}

	}

	launchTemplate := `
gcloud auth activate-service-account --key-file='{{.AccountFile}}'
gcloud config set project {{.ProjectID}}
{{.Args}}
`
	if runtime.GOOS == "windows" {
		launchTemplate = `
call gcloud auth activate-service-account --key-file "{{.AccountFile}}"
call gcloud config set project {{.ProjectID}}
call {{.Args}}
`
	}
	// call command
	args = append([]string{"gcloud"}, args...)
	argString := strings.Join(args, " ")

	var tpl = template.Must(template.New("createTunnel").Parse(launchTemplate))
	var b bytes.Buffer

	opts := map[string]string{
		"AccountFile": s.AccountFile,
		"ProjectID":   s.ProjectId,
		"Args":        argString,
	}

	err = tpl.Execute(&b, opts)
	if err != nil {
		fmt.Println(err)
	}

	if _, err := writer.WriteString(b.String()); err != nil {
		return "", fmt.Errorf("Error preparing gcloud shell script: %s", err)
	}

	if err := writer.Flush(); err != nil {
		return "", fmt.Errorf("Error preparing shell script: %s", err)
	}
	// Have to close temp file before renaming it or Windows will complain.
	tf.Close()
	err = os.Chmod(tf.Name(), 0700)
	if err != nil {
		log.Printf("[ERROR] (google): error modifying permissions of temp script file: %s", err.Error())
	}

	// figure out what extension the file should have, and rename it.
	tempScriptFileName := tf.Name()
	if s.IAPConf.IAPExt != "" {
		err := os.Rename(tempScriptFileName, fmt.Sprintf("%s%s", tempScriptFileName, s.IAPConf.IAPExt))
		if err != nil {
			return "", fmt.Errorf("Error setting the correct temp file extension: %s", err)
		}
		tempScriptFileName = fmt.Sprintf("%s%s", tempScriptFileName, s.IAPConf.IAPExt)
	}

	return tempScriptFileName, nil
}

// Run executes the Packer build step that creates an IAP tunnel.
func (s *StepStartTunnel) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
	if !s.IAPConf.IAP {
		log.Printf("Skipping step launch IAP tunnel; \"iap\" is false.")
		return multistep.ActionContinue
	}

	// shell out to create the tunnel.
	ui := state.Get("ui").(packer.Ui)
	instanceName := state.Get("instance_name").(string)
	c := state.Get("config").(*Config)

	ui.Say("Step Launch IAP Tunnel...")

	err := s.ConfigureLocalHostPort(ctx)
	if err != nil {
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	// Generate list of args to use to call gcloud cli.
	args := []string{"compute", "start-iap-tunnel", instanceName,
		strconv.Itoa(s.CommConf.Port()),
		fmt.Sprintf("--local-host-port=localhost:%d", s.IAPConf.IAPLocalhostPort),
		"--zone", c.Zone,
	}

	// This is the port the IAP tunnel listens on, on localhost.
	// TODO make setting LocalHostPort optional
	s.CommConf.SSHPort = s.IAPConf.IAPLocalhostPort

	log.Printf("Creating tunnel launch script with args %#v", args)
	// Create temp file that contains both gcloud authentication, and gcloud
	// proxy setup call.
	tempScriptFileName, err := s.createTempGcloudScript(args)
	if err != nil {
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer os.Remove(tempScriptFileName)

	s.tunnelDriver = NewTunnelDriver()

	err = retry.Config{
		Tries: 11,
		ShouldRetry: func(err error) bool {
			switch err.(type) {
			case RetryableTunnelError:
				return true
			default:
				return false
			}
		},
		RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
	}.Run(ctx, func(ctx context.Context) error {
		// tunnel launcher/destroyer has to be different on windows vs. unix.
		err := s.tunnelDriver.StartTunnel(ctx, tempScriptFileName)
		return err
	})
	if err != nil {
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}

	return multistep.ActionContinue
}

// Cleanup stops the IAP tunnel and cleans up processes.
func (s *StepStartTunnel) Cleanup(state multistep.StateBag) {
	if !s.IAPConf.IAP {
		log.Printf("Skipping cleanup of IAP tunnel; \"iap\" is false.")
		return
	}
	if s.tunnelDriver != nil {
		s.tunnelDriver.StopTunnel()
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"text/template"
	"time"
)

// This file contains functions and types for rendering the blog.

// baseDot is the base for all "dot" structures used as the environment of the
// HTML template.
type baseDot struct {
	BlogTitle     string
	Author        string
	RootURL       string
	HomepageTitle string
	Categories    []categoryMeta

	CategoryMap map[string]string
	BaseCSS     string
}

func newBaseDot(bc *blogConf, css string) *baseDot {
	b := &baseDot{bc.Title, bc.Author, bc.RootURL,
		bc.Index.Title, bc.Categories, make(map[string]string), css}
	for _, m := range bc.Categories {
		b.CategoryMap[m.Name] = m.Title
	}
	return b
}

type articleDot struct {
	*baseDot
	article
}

type categoryDot struct {
	*baseDot
	Category string
	Prelude  string
	Articles []articleMeta
	ExtraCSS string
	ExtraJS  string
}

type feedDot struct {
	*baseDot
	Articles     []article
	LastModified rfc3339Time
}

// rfc3339Time wraps time.Time to provide a RFC3339 String() method.
type rfc3339Time time.Time

func (t rfc3339Time) String() string {
	return time.Time(t).Format(time.RFC3339)
}

// contentIs generates a code snippet to fix the free reference "content" in
// the HTML template.
func contentIs(what string) string {
	return fmt.Sprintf(
		`{{ define "content" }} {{ template "%s-content" . }} {{ end }}`,
		what)
}

const fontFaceTemplate = `@font-face { font-family: %v; font-weight: %v; font-style: %v; font-stretch: normal; font-display: swap; src: url("%v/fonts/%v.woff2") format("woff");}`

func newTemplate(name, root string, sources ...string) *template.Template {
	t := template.New(name).Funcs(template.FuncMap(map[string]interface{}{
		"is":      func(s string) bool { return s == name },
		"rootURL": func() string { return root },
		"getEnv":  os.Getenv,
		"fontFace": func(family string, weight int, style string, fname string) string {
			return fmt.Sprintf(fontFaceTemplate, family, weight, style, root, fname)
		},
	}))
	for _, source := range sources {
		template.Must(t.Parse(source))
	}
	return t
}

func openForWrite(fname string) *os.File {
	file, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Fatal(err)
	}
	return file
}

func executeToFile(t *template.Template, data interface{}, fname string) {
	file := openForWrite(fname)
	defer file.Close()
	err := t.Execute(file, data)
	if err != nil {
		log.Fatalf("rendering %q: %s", fname, err)
	}
}
<commit_msg>website: Avoid reflow with "font-display: block".<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"text/template"
	"time"
)

// This file contains functions and types for rendering the blog.

// baseDot is the base for all "dot" structures used as the environment of the
// HTML template.
type baseDot struct {
	BlogTitle     string
	Author        string
	RootURL       string
	HomepageTitle string
	Categories    []categoryMeta

	CategoryMap map[string]string
	BaseCSS     string
}

func newBaseDot(bc *blogConf, css string) *baseDot {
	b := &baseDot{bc.Title, bc.Author, bc.RootURL,
		bc.Index.Title, bc.Categories, make(map[string]string), css}
	for _, m := range bc.Categories {
		b.CategoryMap[m.Name] = m.Title
	}
	return b
}

type articleDot struct {
	*baseDot
	article
}

type categoryDot struct {
	*baseDot
	Category string
	Prelude  string
	Articles []articleMeta
	ExtraCSS string
	ExtraJS  string
}

type feedDot struct {
	*baseDot
	Articles     []article
	LastModified rfc3339Time
}

// rfc3339Time wraps time.Time to provide a RFC3339 String() method.
type rfc3339Time time.Time

func (t rfc3339Time) String() string {
	return time.Time(t).Format(time.RFC3339)
}

// contentIs generates a code snippet to fix the free reference "content" in
// the HTML template.
func contentIs(what string) string {
	return fmt.Sprintf(
		`{{ define "content" }} {{ template "%s-content" . }} {{ end }}`,
		what)
}

const fontFaceTemplate = `@font-face { font-family: %v; font-weight: %v; font-style: %v; font-stretch: normal; font-display: block; src: url("%v/fonts/%v.woff2") format("woff");}`

func newTemplate(name, root string, sources ...string) *template.Template {
	t := template.New(name).Funcs(template.FuncMap(map[string]interface{}{
		"is":      func(s string) bool { return s == name },
		"rootURL": func() string { return root },
		"getEnv":  os.Getenv,
		"fontFace": func(family string, weight int, style string, fname string) string {
			return fmt.Sprintf(fontFaceTemplate, family, weight, style, root, fname)
		},
	}))
	for _, source := range sources {
		template.Must(t.Parse(source))
	}
	return t
}

func openForWrite(fname string) *os.File {
	file, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Fatal(err)
	}
	return file
}

func executeToFile(t *template.Template, data interface{}, fname string) {
	file := openForWrite(fname)
	defer file.Close()
	err := t.Execute(file, data)
	if err != nil {
		log.Fatalf("rendering %q: %s", fname, err)
	}
}
<|endoftext|>