<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"

	"github.com/gorilla/mux"
)

const (
	PEBBLE_BOOT_URL string = "https://boot.getpebble.com/api/config/"
	STORE_URI       string = "https://santoku.adamfourney.com"
)

// BootJSON is just a Go container object for the JSON response.
type BootJSON struct {
	Config BootConfig `json:"config"`
}

// BootConfig contains the webviews from the JSON file.
type BootConfig struct {
	Algolia        json.RawMessage   `json:"algolia"`
	AppMeta        json.RawMessage   `json:"app_meta"`
	Authentication json.RawMessage   `json:"authentication"`
	Cohorts        json.RawMessage   `json:"cohorts"`
	Developer      json.RawMessage   `json:"developer"`
	Health         json.RawMessage   `json:"health"`
	Href           json.RawMessage   `json:"href"`
	Id             json.RawMessage   `json:"id"`
	KeenIo         json.RawMessage   `json:"keen_io"`
	LinkedServices json.RawMessage   `json:"linked_services"`
	Links          json.RawMessage   `json:"links"`
	Locker         json.RawMessage   `json:"locker"`
	Notifications  json.RawMessage   `json:"notifications"`
	SupportRequest json.RawMessage   `json:"support_request"`
	Timeline       json.RawMessage   `json:"timeline"`
	TreasureData   json.RawMessage   `json:"treasure_data"`
	Voice          json.RawMessage   `json:"voice"`
	Webviews       map[string]string `json:"webviews"`
}

// WebviewConfig contains the webviews in-which we would like to override.
type WebviewsConfig struct {
	FAQ                  string `json:"support/faq"`
	Application          string `json:"appstore/application"`
	ApplicationChangelog string `json:"appstore/application_changelog"`
	DeveloperApps        string `json:"appstore/developer_apps"`
	Watchfaces           string `json:"appstore/watchfaces"`
	Watchapps            string `json:"appstore/watchapps"`
}

// BootHandler is based off of [@afourney|https://github.com/afourney]'s
// development bootstrap override.
func BootHandler(w http.ResponseWriter, r *http.Request) {
	// Get a store uri from the request and determine if it matches a valid URI
	store_uri := r.URL.Query().Get("store_uri")
	if _, err := url.Parse(store_uri); err != nil {
		w.WriteHeader(400)
		w.Write([]byte("Invalid store_uri parameter"))
		return
	}

	// Copying the URL Query to modify it later
	urlquery := r.URL.Query()

	// If the user didn't specify a store_uri, use the pebble server
	if store_uri == "" {
		store_uri = STORE_URI
	} else {
		urlquery.Del("store_uri")
	}

	// Build up the request URL
	request_url := fmt.Sprintf("%s%s?%s", PEBBLE_BOOT_URL, mux.Vars(r)["path"], urlquery.Encode())

	// Make a request to an external server then parse the request
	req, err := http.Get(request_url)
	if err != nil {
		log.Fatal(err)
	}
	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Fatal(err)
	}

	// Decode the JSON data
	response := &BootJSON{}
	err = json.Unmarshal(data, response)
	if err != nil {
		log.Println(err)
		w.Write(data)
		return
	}

	// Replace items in the JSON object, then prepare to output it
	response.Config.Webviews["support/faq"] = fmt.Sprintf("%s/faq", store_uri)
	response.Config.Webviews["appstore/application"] = fmt.Sprintf("%s/application/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/application_changelog"] = fmt.Sprintf("%s/changelog/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/developer_apps"] = fmt.Sprintf("%s/developer/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/watchfaces"] = fmt.Sprintf("%s/watchfaces?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/watchapps"] = fmt.Sprintf("%s/watchapps?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	data, err = json.MarshalIndent(response, "", "\t")
	if err != nil {
		log.Fatal(err)
	}

	// Send the JSON object back to the user
	w.Header().Add("content-type", "application/json")
	w.Write(data)
}
<commit_msg>Ran gofmt on code<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"

	"github.com/gorilla/mux"
)

const (
	PEBBLE_BOOT_URL string = "https://boot.getpebble.com/api/config/"
	STORE_URI       string = "https://santoku.adamfourney.com"
)

// BootJSON is just a Go container object for the JSON response.
type BootJSON struct {
	Config BootConfig `json:"config"`
}

// BootConfig contains the webviews from the JSON file.
type BootConfig struct {
	Algolia        json.RawMessage   `json:"algolia"`
	AppMeta        json.RawMessage   `json:"app_meta"`
	Authentication json.RawMessage   `json:"authentication"`
	Cohorts        json.RawMessage   `json:"cohorts"`
	Developer      json.RawMessage   `json:"developer"`
	Health         json.RawMessage   `json:"health"`
	Href           json.RawMessage   `json:"href"`
	Id             json.RawMessage   `json:"id"`
	KeenIo         json.RawMessage   `json:"keen_io"`
	LinkedServices json.RawMessage   `json:"linked_services"`
	Links          json.RawMessage   `json:"links"`
	Locker         json.RawMessage   `json:"locker"`
	Notifications  json.RawMessage   `json:"notifications"`
	SupportRequest json.RawMessage   `json:"support_request"`
	Timeline       json.RawMessage   `json:"timeline"`
	TreasureData   json.RawMessage   `json:"treasure_data"`
	Voice          json.RawMessage   `json:"voice"`
	Webviews       map[string]string `json:"webviews"`
}

// WebviewConfig contains the webviews in-which we would like to override.
type WebviewsConfig struct {
	FAQ                  string `json:"support/faq"`
	Application          string `json:"appstore/application"`
	ApplicationChangelog string `json:"appstore/application_changelog"`
	DeveloperApps        string `json:"appstore/developer_apps"`
	Watchfaces           string `json:"appstore/watchfaces"`
	Watchapps            string `json:"appstore/watchapps"`
}

// BootHandler is based off of [@afourney|https://github.com/afourney]'s
// development bootstrap override.
func BootHandler(w http.ResponseWriter, r *http.Request) {
	// Get a store uri from the request and determine if it matches a valid URI
	store_uri := r.URL.Query().Get("store_uri")
	if _, err := url.Parse(store_uri); err != nil {
		w.WriteHeader(400)
		w.Write([]byte("Invalid store_uri parameter"))
		return
	}

	// Copying the URL Query to modify it later
	urlquery := r.URL.Query()

	// If the user didn't specify a store_uri, use the pebble server
	if store_uri == "" {
		store_uri = STORE_URI
	} else {
		urlquery.Del("store_uri")
	}

	// Build up the request URL
	request_url := fmt.Sprintf("%s%s?%s", PEBBLE_BOOT_URL, mux.Vars(r)["path"], urlquery.Encode())

	// Make a request to an external server then parse the request
	req, err := http.Get(request_url)
	if err != nil {
		log.Fatal(err)
	}
	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Fatal(err)
	}

	// Decode the JSON data
	response := &BootJSON{}
	err = json.Unmarshal(data, response)
	if err != nil {
		log.Println(err)
		w.Write(data)
		return
	}

	// Replace items in the JSON object, then prepare to output it
	response.Config.Webviews["support/faq"] = fmt.Sprintf("%s/faq", store_uri)
	response.Config.Webviews["appstore/application"] = fmt.Sprintf("%s/application/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/application_changelog"] = fmt.Sprintf("%s/changelog/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/developer_apps"] = fmt.Sprintf("%s/developer/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/watchfaces"] = fmt.Sprintf("%s/watchfaces?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	response.Config.Webviews["appstore/watchapps"] = fmt.Sprintf("%s/watchapps?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
	data, err = json.MarshalIndent(response, "", "\t")
	if err != nil {
		log.Fatal(err)
	}

	// Send the JSON object back to the user
	w.Header().Add("content-type", "application/json")
	w.Write(data)
}
<|endoftext|>


<commit_before>package block

import (
	"fmt"
	"html"

	"github.com/SoCloz/goprismic/fragment/link"
	"github.com/SoCloz/goprismic/fragment/span"
)

// Common block properties
type BaseBlock struct {
	Type  string               `json:"type"`
	Text  string               `json:"text"`
	Spans []span.SpanInterface `json:"spans"`
}

func (b *BaseBlock) AsText() string {
	return b.Text
}

// Formats the block content as html, without enclosing tags
func (b *BaseBlock) FormatHtmlText() string {
	t := html.EscapeString(b.Text)
	// store one more to be able to compute offsets[len(text)]
	offsets := make([]int, len(b.Text)+1)
	// compute byte offsets for utf8 string
	off := 0
	index := 0
	for k, r := range b.Text {
		offsets[index] = k+off
		off += len([]rune(html.EscapeString(string([]rune{r}))))-1
		index++
	}
	offsets[index] = len(b.Text)+off
	for _, s := range b.Spans {
		begin := s.HtmlBeginTag()
		end := s.HtmlEndTag()
		t = t[:offsets[s.GetStart()]] + begin + t[offsets[s.GetStart()]:offsets[s.GetEnd()]] + end + t[offsets[s.GetEnd()]:]
		for i := s.GetStart(); i < s.GetEnd(); i++ {
			offsets[i] += len(begin)
		}
		for i := s.GetEnd(); i < len(offsets); i++ {
			offsets[i] += len(begin) + len(end)
		}
	}
	return t
}

func (b *BaseBlock) decodeBlock(enc interface{}) error {
	dec, ok := enc.(map[string]interface{})
	if !ok {
		return fmt.Errorf("%+v is not a map", enc)
	}
	if v, found := dec["type"]; found {
		b.Type = v.(string)
	}
	if v, found := dec["text"]; found {
		b.Text = v.(string)
	}
	if v, found := dec["spans"]; found {
		dec2, ok := v.([]interface{})
		if !ok {
			return fmt.Errorf("%+v is not a slice", dec2)
		}
		b.Spans = make([]span.SpanInterface, 0, len(dec2))
		for _, v := range dec2 {
			dec3, ok := v.(map[string]interface{})
			if ok {
				var s span.SpanInterface
				switch dec3["type"] {
				case "strong":
					s = new(span.Strong)
				case "em":
					s = new(span.Em)
				case "hyperlink":
					s = new(span.Hyperlink)
				default:
					panic(fmt.Sprintf("Unknown span type %s", dec3["type"]))
				}
				err := s.Decode(v)
				if err != nil {
					return err
				}
				b.Spans = append(b.Spans, s)
			}
		}
	}
	return nil
}

// Resolves links
func (b *BaseBlock) ResolveLinks(r link.Resolver) {
	for _, v := range b.Spans {
		v.ResolveLinks(r)
	}
}
<commit_msg>go fmt<commit_after>package block

import (
	"fmt"
	"html"

	"github.com/SoCloz/goprismic/fragment/link"
	"github.com/SoCloz/goprismic/fragment/span"
)

// Common block properties
type BaseBlock struct {
	Type  string               `json:"type"`
	Text  string               `json:"text"`
	Spans []span.SpanInterface `json:"spans"`
}

func (b *BaseBlock) AsText() string {
	return b.Text
}

// Formats the block content as html, without enclosing tags
func (b *BaseBlock) FormatHtmlText() string {
	t := html.EscapeString(b.Text)
	// store one more to be able to compute offsets[len(text)]
	offsets := make([]int, len(b.Text)+1)
	// compute byte offsets for utf8 string
	off := 0
	index := 0
	for k, r := range b.Text {
		offsets[index] = k + off
		off += len([]rune(html.EscapeString(string([]rune{r})))) - 1
		index++
	}
	offsets[index] = len(b.Text) + off
	for _, s := range b.Spans {
		begin := s.HtmlBeginTag()
		end := s.HtmlEndTag()
		t = t[:offsets[s.GetStart()]] + begin + t[offsets[s.GetStart()]:offsets[s.GetEnd()]] + end + t[offsets[s.GetEnd()]:]
		for i := s.GetStart(); i < s.GetEnd(); i++ {
			offsets[i] += len(begin)
		}
		for i := s.GetEnd(); i < len(offsets); i++ {
			offsets[i] += len(begin) + len(end)
		}
	}
	return t
}

func (b *BaseBlock) decodeBlock(enc interface{}) error {
	dec, ok := enc.(map[string]interface{})
	if !ok {
		return fmt.Errorf("%+v is not a map", enc)
	}
	if v, found := dec["type"]; found {
		b.Type = v.(string)
	}
	if v, found := dec["text"]; found {
		b.Text = v.(string)
	}
	if v, found := dec["spans"]; found {
		dec2, ok := v.([]interface{})
		if !ok {
			return fmt.Errorf("%+v is not a slice", dec2)
		}
		b.Spans = make([]span.SpanInterface, 0, len(dec2))
		for _, v := range dec2 {
			dec3, ok := v.(map[string]interface{})
			if ok {
				var s span.SpanInterface
				switch dec3["type"] {
				case "strong":
					s = new(span.Strong)
				case "em":
					s = new(span.Em)
				case "hyperlink":
					s = new(span.Hyperlink)
				default:
					panic(fmt.Sprintf("Unknown span type %s", dec3["type"]))
				}
				err := s.Decode(v)
				if err != nil {
					return err
				}
				b.Spans = append(b.Spans, s)
			}
		}
	}
	return nil
}

// Resolves links
func (b *BaseBlock) ResolveLinks(r link.Resolver) {
	for _, v := range b.Spans {
		v.ResolveLinks(r)
	}
}
<|endoftext|>


<commit_before>package empiretest

import (
	"io"
	"net/http/httptest"
	"os"
	"testing"
	"text/template"

	"golang.org/x/net/context"

	"github.com/ejholmes/flock"
	"github.com/remind101/empire"
	"github.com/remind101/empire/pkg/dockerutil"
	"github.com/remind101/empire/pkg/image"
	"github.com/remind101/empire/procfile"
	"github.com/remind101/empire/scheduler"
	"github.com/remind101/empire/server"
	"github.com/remind101/empire/server/auth"
	"github.com/remind101/empire/server/github"
)

var (
	// DatabaseURL is a connection string for the postgres database to use
	// during integration tests.
	DatabaseURL = "postgres://localhost/empire?sslmode=disable"
)

// NewEmpire returns a new Empire instance suitable for testing. It ensures that
// the database is clean before returning.
func NewEmpire(t testing.TB) *empire.Empire {
	db, err := empire.OpenDB(DatabaseURL)
	if err != nil {
		t.Fatal(err)
	}

	// TODO: Run db.MigrateUp once migrations are in the binary.

	// Log queries if verbose mode is set.
	if testing.Verbose() {
		db.Debug()
	}

	e := empire.New(db, empire.DefaultOptions)
	e.Scheduler = scheduler.NewFakeScheduler()
	e.ExtractProcfile = ExtractProcfile

	if err := e.Reset(); err != nil {
		t.Fatal(err)
	}

	return e
}

// NewServer builds a new empire.Empire instance and returns an httptest.Server
// running the empire API.
func NewServer(t testing.TB, e *empire.Empire) *httptest.Server {
	if e == nil {
		e = NewEmpire(t)
	}

	server.DefaultOptions.GitHub.Webhooks.Secret = "abcd"
	server.DefaultOptions.Authenticator = auth.Anyone(&empire.User{Name: "fake"})
	server.DefaultOptions.GitHub.Deployments.Environments = []string{"test"}
	server.DefaultOptions.GitHub.Deployments.ImageBuilder = github.ImageFromTemplate(template.Must(template.New("image").Parse(github.DefaultTemplate)))
	return httptest.NewServer(server.New(e, server.DefaultOptions))
}

var dblock = "/tmp/empire.lock"

// Run runs testing.M after aquiring a lock against the database.
func Run(m *testing.M) {
	l, err := flock.NewPath(dblock)
	if err != nil {
		panic(err)
	}

	l.Lock()
	defer l.Unlock()

	os.Exit(m.Run())
}

// ExtractProcfile extracts a fake procfile.
func ExtractProcfile(ctx context.Context, img image.Image, w io.Writer) (procfile.Procfile, error) {
	return procfile.Procfile{
		"web": "./bin/web",
	}, dockerutil.FakePull(img, w)
}
<commit_msg>Set a RunRecorder in tests so it's exercised.<commit_after>package empiretest

import (
	"io"
	"io/ioutil"
	"net/http/httptest"
	"os"
	"testing"
	"text/template"

	"golang.org/x/net/context"

	"github.com/ejholmes/flock"
	"github.com/remind101/empire"
	"github.com/remind101/empire/pkg/dockerutil"
	"github.com/remind101/empire/pkg/image"
	"github.com/remind101/empire/procfile"
	"github.com/remind101/empire/scheduler"
	"github.com/remind101/empire/server"
	"github.com/remind101/empire/server/auth"
	"github.com/remind101/empire/server/github"
)

var (
	// DatabaseURL is a connection string for the postgres database to use
	// during integration tests.
	DatabaseURL = "postgres://localhost/empire?sslmode=disable"
)

// NewEmpire returns a new Empire instance suitable for testing. It ensures that
// the database is clean before returning.
func NewEmpire(t testing.TB) *empire.Empire {
	db, err := empire.OpenDB(DatabaseURL)
	if err != nil {
		t.Fatal(err)
	}

	// TODO: Run db.MigrateUp once migrations are in the binary.

	// Log queries if verbose mode is set.
	if testing.Verbose() {
		db.Debug()
	}

	e := empire.New(db, empire.DefaultOptions)
	e.Scheduler = scheduler.NewFakeScheduler()
	e.ExtractProcfile = ExtractProcfile
	e.RunRecorder = empire.RecordTo(ioutil.Discard)

	if err := e.Reset(); err != nil {
		t.Fatal(err)
	}

	return e
}

// NewServer builds a new empire.Empire instance and returns an httptest.Server
// running the empire API.
func NewServer(t testing.TB, e *empire.Empire) *httptest.Server {
	if e == nil {
		e = NewEmpire(t)
	}

	server.DefaultOptions.GitHub.Webhooks.Secret = "abcd"
	server.DefaultOptions.Authenticator = auth.Anyone(&empire.User{Name: "fake"})
	server.DefaultOptions.GitHub.Deployments.Environments = []string{"test"}
	server.DefaultOptions.GitHub.Deployments.ImageBuilder = github.ImageFromTemplate(template.Must(template.New("image").Parse(github.DefaultTemplate)))
	return httptest.NewServer(server.New(e, server.DefaultOptions))
}

var dblock = "/tmp/empire.lock"

// Run runs testing.M after aquiring a lock against the database.
func Run(m *testing.M) {
	l, err := flock.NewPath(dblock)
	if err != nil {
		panic(err)
	}

	l.Lock()
	defer l.Unlock()

	os.Exit(m.Run())
}

// ExtractProcfile extracts a fake procfile.
func ExtractProcfile(ctx context.Context, img image.Image, w io.Writer) (procfile.Procfile, error) {
	return procfile.Procfile{
		"web": "./bin/web",
	}, dockerutil.FakePull(img, w)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
)

// Convenient function to exec a command.
func cmd(name string, args ...string) error {
	cmd := exec.Command(name, args...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

// Convenient function to launch VBoxManage.
func vbm(args ...string) error {
	return cmd(B2D.VBM, args...)
}

// TODO: delete the hostonlyif and dhcpserver when we delete the vm! (need to
// reference count to make sure there are no other vms relying on them)

// Get or create the hostonly network interface
func getHostOnlyNetworkInterface() (string, error) {
	// Check if the interface exists.
	out, err := exec.Command(B2D.VBM, "list", "hostonlyifs").Output()
	if err != nil {
		return "", err
	}
	lists := regexp.MustCompile(`(?m)^(Name|IPAddress|VBoxNetworkName):\s+(.+?)\r?$`).FindAllSubmatch(out, -1)
	var ifname string
	index := 0

	for ifname == "" && len(lists) > index {
		if string(lists[index+1][2]) == B2D.HostIP {
			//test to see that the dhcpserver is the same too
			out, err := exec.Command(B2D.VBM, "list", "dhcpservers").Output()
			if err != nil {
				return "", err
			}
			//WARNING: this relies on the order the virtualbox gives not changing
			dhcp := regexp.MustCompile(`(?m)^(NetworkName|IP|NetworkMask|lowerIPAddress|upperIPAddress|Enabled):\s+(.+?)\r?$`).FindAllSubmatch(out, -1)
			i := 0

			for ifname == "" && len(dhcp) > i {
				if string(dhcp[i][2]) == string(lists[index+2][2]) &&
					string(dhcp[i+1][2]) == B2D.DHCPIP &&
					string(dhcp[i+2][2]) == B2D.NetworkMask &&
					string(dhcp[i+3][2]) == B2D.LowerIPAddress &&
					string(dhcp[i+4][2]) == B2D.UpperIPAddress &&
					string(dhcp[i+5][2]) == B2D.DHCPEnabled {
					ifname = string(lists[index][2])
					fmt.Printf("Reusing hostonly network interface %s\n", ifname)
				}

				i = i + 5
			}

		}
		index = index + 3
	}

	if ifname == "" {
		//create it all fresh
		fmt.Printf("Creating a new hostonly network interface\n")
		out, err = exec.Command(B2D.VBM, "hostonlyif", "create").Output()
		if err != nil {
			return "", err
		}
		groups := regexp.MustCompile(`(?m)^Interface '(\w+)' was successfully created`).FindSubmatch(out)
		if len(groups) < 2 {
			return "", err
		}
		ifname = string(groups[1])
		out, err = exec.Command(B2D.VBM, "dhcpserver", "add",
			"--ifname", ifname,
			"--ip", B2D.DHCPIP,
			"--netmask", B2D.NetworkMask,
			"--lowerip", B2D.LowerIPAddress,
			"--upperip", B2D.UpperIPAddress,
			"--enable",
		).Output()
		if err != nil {
			return "", err
		}
		out, err = exec.Command(B2D.VBM, "hostonlyif", "ipconfig", ifname,
			"--ip", B2D.HostIP,
			"--netmask", B2D.NetworkMask,
		).Output()
		if err != nil {
			return "", err
		}
	}
	return ifname, nil
}

// Get the state of a VM.
func status(vm string) vmState {
	// Check if the VM exists.
	out, err := exec.Command(B2D.VBM, "list", "vms").Output()
	if err != nil {
		if err.(*exec.Error).Err == exec.ErrNotFound {
			return vmVBMNotFound
		}
		return vmUnknown
	}
	found, err := regexp.Match(fmt.Sprintf(`(?m)^"%s"`, regexp.QuoteMeta(vm)), out)
	if err != nil {
		return vmUnknown
	}
	if !found {
		return vmUnregistered
	}

	if out, err = exec.Command(B2D.VBM, "showvminfo", vm, "--machinereadable").Output(); err != nil {
		if err.(*exec.Error).Err == exec.ErrNotFound {
			return vmVBMNotFound
		}
		return vmUnknown
	}
	groups := regexp.MustCompile(`(?m)^VMState="(\w+)"\r?$`).FindSubmatch(out)
	if len(groups) < 2 {
		return vmUnknown
	}
	switch state := vmState(groups[1]); state {
	case vmRunning, vmPaused, vmSaved, vmPoweroff, vmAborted:
		return state
	default:
		return vmUnknown
	}
}

// Get the VirtualBox base folder of the VM.
func basefolder(vm string) string {
	out, err := exec.Command(B2D.VBM, "showvminfo", vm, "--machinereadable").Output()
	if err != nil {
		return ""
	}
	groups := regexp.MustCompile(`(?m)^CfgFile="(.+)"\r?$`).FindSubmatch(out)
	if len(groups) < 2 {
		return ""
	}
	return filepath.Dir(string(groups[1]))
}

// Make a boot2docker VM disk image with the given size (in MB).
func makeDiskImage(dest string, size uint) error {
	// Create the dest dir.
	if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
		return err
	}

	// Convert a raw image from stdin to the dest VMDK image.
	sizeBytes := int64(size) * 1024 * 1024 // usually won't fit in 32-bit int
	cmd := exec.Command(B2D.VBM, "convertfromraw", "stdin", dest,
		fmt.Sprintf("%d", sizeBytes), "--format", "VMDK")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	stdin, err := cmd.StdinPipe()
	if err != nil {
		return err
	}
	if err := cmd.Start(); err != nil {
		return err
	}

	// Fill in the magic string so boot2docker VM will detect this and format
	// the disk upon first boot.
	magic := []byte("boot2docker, please format-me")
	if _, err := stdin.Write(magic); err != nil {
		return err
	}
	// The total number of bytes written to stdin must match sizeBytes, or
	// VBoxManage.exe on Windows will fail.
	if err := zeroFill(stdin, sizeBytes-int64(len(magic))); err != nil {
		return err
	}
	// cmd won't exit until the stdin is closed.
	if err := stdin.Close(); err != nil {
		return err
	}

	return cmd.Wait()
}

// Write n zero bytes into w.
func zeroFill(w io.Writer, n int64) (err error) {
	const blocksize = 32 * 1024
	zeros := make([]byte, blocksize)
	var k int
	for n > 0 {
		if n > blocksize {
			k, err = w.Write(zeros)
		} else {
			k, err = w.Write(zeros[:n])
		}
		if err != nil {
			return
		}
		n -= int64(k)
	}
	return
}
<commit_msg>No more naked returns per Go style guide<commit_after>package main

import (
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
)

// Convenient function to exec a command.
func cmd(name string, args ...string) error {
	cmd := exec.Command(name, args...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

// Convenient function to launch VBoxManage.
func vbm(args ...string) error {
	return cmd(B2D.VBM, args...)
}

// TODO: delete the hostonlyif and dhcpserver when we delete the vm! (need to
// reference count to make sure there are no other vms relying on them)

// Get or create the hostonly network interface
func getHostOnlyNetworkInterface() (string, error) {
	// Check if the interface exists.
	out, err := exec.Command(B2D.VBM, "list", "hostonlyifs").Output()
	if err != nil {
		return "", err
	}
	lists := regexp.MustCompile(`(?m)^(Name|IPAddress|VBoxNetworkName):\s+(.+?)\r?$`).FindAllSubmatch(out, -1)
	var ifname string
	index := 0

	for ifname == "" && len(lists) > index {
		if string(lists[index+1][2]) == B2D.HostIP {
			//test to see that the dhcpserver is the same too
			out, err := exec.Command(B2D.VBM, "list", "dhcpservers").Output()
			if err != nil {
				return "", err
			}
			//WARNING: this relies on the order the virtualbox gives not changing
			dhcp := regexp.MustCompile(`(?m)^(NetworkName|IP|NetworkMask|lowerIPAddress|upperIPAddress|Enabled):\s+(.+?)\r?$`).FindAllSubmatch(out, -1)
			i := 0

			for ifname == "" && len(dhcp) > i {
				if string(dhcp[i][2]) == string(lists[index+2][2]) &&
					string(dhcp[i+1][2]) == B2D.DHCPIP &&
					string(dhcp[i+2][2]) == B2D.NetworkMask &&
					string(dhcp[i+3][2]) == B2D.LowerIPAddress &&
					string(dhcp[i+4][2]) == B2D.UpperIPAddress &&
					string(dhcp[i+5][2]) == B2D.DHCPEnabled {
					ifname = string(lists[index][2])
					fmt.Printf("Reusing hostonly network interface %s\n", ifname)
				}

				i = i + 5
			}

		}
		index = index + 3
	}

	if ifname == "" {
		//create it all fresh
		fmt.Printf("Creating a new hostonly network interface\n")
		out, err = exec.Command(B2D.VBM, "hostonlyif", "create").Output()
		if err != nil {
			return "", err
		}
		groups := regexp.MustCompile(`(?m)^Interface '(\w+)' was successfully created`).FindSubmatch(out)
		if len(groups) < 2 {
			return "", err
		}
		ifname = string(groups[1])
		out, err = exec.Command(B2D.VBM, "dhcpserver", "add",
			"--ifname", ifname,
			"--ip", B2D.DHCPIP,
			"--netmask", B2D.NetworkMask,
			"--lowerip", B2D.LowerIPAddress,
			"--upperip", B2D.UpperIPAddress,
			"--enable",
		).Output()
		if err != nil {
			return "", err
		}
		out, err = exec.Command(B2D.VBM, "hostonlyif", "ipconfig", ifname,
			"--ip", B2D.HostIP,
			"--netmask", B2D.NetworkMask,
		).Output()
		if err != nil {
			return "", err
		}
	}
	return ifname, nil
}

// Get the state of a VM.
func status(vm string) vmState {
	// Check if the VM exists.
	out, err := exec.Command(B2D.VBM, "list", "vms").Output()
	if err != nil {
		if err.(*exec.Error).Err == exec.ErrNotFound {
			return vmVBMNotFound
		}
		return vmUnknown
	}
	found, err := regexp.Match(fmt.Sprintf(`(?m)^"%s"`, regexp.QuoteMeta(vm)), out)
	if err != nil {
		return vmUnknown
	}
	if !found {
		return vmUnregistered
	}

	if out, err = exec.Command(B2D.VBM, "showvminfo", vm, "--machinereadable").Output(); err != nil {
		if err.(*exec.Error).Err == exec.ErrNotFound {
			return vmVBMNotFound
		}
		return vmUnknown
	}
	groups := regexp.MustCompile(`(?m)^VMState="(\w+)"\r?$`).FindSubmatch(out)
	if len(groups) < 2 {
		return vmUnknown
	}
	switch state := vmState(groups[1]); state {
	case vmRunning, vmPaused, vmSaved, vmPoweroff, vmAborted:
		return state
	default:
		return vmUnknown
	}
}

// Get the VirtualBox base folder of the VM.
func basefolder(vm string) string {
	out, err := exec.Command(B2D.VBM, "showvminfo", vm, "--machinereadable").Output()
	if err != nil {
		return ""
	}
	groups := regexp.MustCompile(`(?m)^CfgFile="(.+)"\r?$`).FindSubmatch(out)
	if len(groups) < 2 {
		return ""
	}
	return filepath.Dir(string(groups[1]))
}

// Make a boot2docker VM disk image with the given size (in MB).
func makeDiskImage(dest string, size uint) error {
	// Create the dest dir.
	if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
		return err
	}

	// Convert a raw image from stdin to the dest VMDK image.
	sizeBytes := int64(size) * 1024 * 1024 // usually won't fit in 32-bit int
	cmd := exec.Command(B2D.VBM, "convertfromraw", "stdin", dest,
		fmt.Sprintf("%d", sizeBytes), "--format", "VMDK")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	stdin, err := cmd.StdinPipe()
	if err != nil {
		return err
	}
	if err := cmd.Start(); err != nil {
		return err
	}

	// Fill in the magic string so boot2docker VM will detect this and format
	// the disk upon first boot.
	magic := []byte("boot2docker, please format-me")
	if _, err := stdin.Write(magic); err != nil {
		return err
	}
	// The total number of bytes written to stdin must match sizeBytes, or
	// VBoxManage.exe on Windows will fail.
	if err := zeroFill(stdin, sizeBytes-int64(len(magic))); err != nil {
		return err
	}
	// cmd won't exit until the stdin is closed.
	if err := stdin.Close(); err != nil {
		return err
	}

	return cmd.Wait()
}

// Write n zero bytes into w.
func zeroFill(w io.Writer, n int64) error {
	const blocksize = 32 * 1024
	zeros := make([]byte, blocksize)
	var k int
	var err error
	for n > 0 {
		if n > blocksize {
			k, err = w.Write(zeros)
		} else {
			k, err = w.Write(zeros[:n])
		}
		if err != nil {
			return err
		}
		n -= int64(k)
	}
	return nil
}
<|endoftext|>


<commit_before>// (C) Copyright 2021 Hewlett Packard Enterprise Development LP
//
// 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 oneview

import (
	"testing"

	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccSSHAccess_2(t *testing.T) {

	resource.Test(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				Config: testAccSSHAccessData,
				Check: resource.ComposeTestCheckFunc(
					resource.TestCheckResourceAttr("oneview_appliance_ssh_access.test"),
				),
			},
		},
	})
}

var testAccSSHAccessData = `
  data "oneview_appliance_ssh_access" "test" {
  }`
<commit_msg>removed data source UT<commit_after><|endoftext|>


<commit_before>package main

import (
	"flag"
	"github.com/Shopify/sarama"
	"log"
	"os"
	"strings"
	"time"
)

var (
	brokerList = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The comma separated list of brokers in the Kafka cluster")
	topic      = flag.String("topic", "", "REQUIRED: the topic to consume")
)

func main() {
	flag.Parse()

	if *brokerList == "" {
		log.Fatal("You have to provide -brokers as a comma-separated list, or set the KAFKA_PEERS environment variable.")
	}
	if *topic == "" {
		log.Fatal("-topic is required")
	}

	config := sarama.NewConfig()
	config.Version = sarama.V1_1_1_0
	config.Consumer.IsolationLevel = sarama.ReadCommitted
	config.Consumer.MaxProcessingTime = 20 * 365 * 24 * time.Hour

	c, err := sarama.NewConsumer(strings.Split(*brokerList, ","), config)
	if err != nil {
		log.Fatalf("Failed to start consumer: %s", err)
	}

	partitionList, err := c.Partitions(*topic)
	if err != nil {
		log.Fatalf("Failed to get the list of partitions: %s", err)
	}

	for _, partition := range partitionList {
		pc, err := c.ConsumePartition(*topic, partition, sarama.OffsetOldest)
		if err != nil {
			log.Fatalf("Failed to start consumer for partition %d: %s", partition, err)
		}

		go func() {
			for err := range pc.Errors() {
				log.Fatal(err)
			}
		}()

		msgChannel := pc.Messages()
	read1Partition:
		for {
			//timeout := time.NewTimer(1 * time.Second)
			select {
			case msg, open := <-msgChannel:
				if !open {
					log.Println("channel message is closed")
					break read1Partition
				}
				log.Println(string(msg.Value))
				//case <-timeout.C:
				//	break read1Partition
			}
		}
	}

	log.Println("Done consuming topic", *topic)

	if err := c.Close(); err != nil {
		log.Println("Failed to close consumer: ", err)
	}
}
<commit_msg>comment import fast test<commit_after>package main

import (
	"flag"
	"log"
	"os"
	"strings"
	"time"

	// when testing locally, it is way faster to edit this to reference your local repo if different
	// if you want to do it properly but slowly you may use some dependency manager magic
	"github.com/Shopify/sarama"
)

var (
	brokerList = flag.String("brokers", os.Getenv("KAFKA_PEERS"), "The comma separated list of brokers in the Kafka cluster")
	topic      = flag.String("topic", "", "REQUIRED: the topic to consume")
)

func main() {
	flag.Parse()

	if *brokerList == "" {
		log.Fatal("You have to provide -brokers as a comma-separated list, or set the KAFKA_PEERS environment variable.")
	}
	if *topic == "" {
		log.Fatal("-topic is required")
	}

	config := sarama.NewConfig()
	config.Version = sarama.V1_1_1_0
	config.Consumer.IsolationLevel = sarama.ReadCommitted
	config.Consumer.MaxProcessingTime = 20 * 365 * 24 * time.Hour

	c, err := sarama.NewConsumer(strings.Split(*brokerList, ","), config)
	if err != nil {
		log.Fatalf("Failed to start consumer: %s", err)
	}

	partitionList, err := c.Partitions(*topic)
	if err != nil {
		log.Fatalf("Failed to get the list of partitions: %s", err)
	}

	for _, partition := range partitionList {
		pc, err := c.ConsumePartition(*topic, partition, sarama.OffsetOldest)
		if err != nil {
			log.Fatalf("Failed to start consumer for partition %d: %s", partition, err)
		}

		go func() {
			for err := range pc.Errors() {
				log.Fatal(err)
			}
		}()

		msgChannel := pc.Messages()
	read1Partition:
		for {
			//timeout := time.NewTimer(1 * time.Second)
			select {
			case msg, open := <-msgChannel:
				if !open {
					log.Println("channel message is closed")
					break read1Partition
				}
				log.Println(string(msg.Value))
				//case <-timeout.C:
				//	break read1Partition
			}
		}
	}

	log.Println("Done consuming topic", *topic)

	if err := c.Close(); err != nil {
		log.Println("Failed to close consumer: ", err)
	}
}
<|endoftext|>


<commit_before>package entities

import (
    "fmt"
    "encoding/json"
    "log"
)

type Player struct {
    username string
    Hash string
    HomePlanet string
    ScreenSize []int
    ScreenPosition []int

}

func (self Player) String() string {
    return self.username
}

func (self Player) GetKey() string {
    return fmt.Sprintf("player.%s", self.username)
}

func (self *Player) StartMission() error {
    // new_mission := CreateMission()
    //db.Do("RPUSH new_mission)
    return nil
}

func (self Player) Serialize() (string, []byte) {
    result, err := json.Marshal(self)
    if err != nil {
        log.Fatal(err)
    }
    return self.GetKey(), result
}

func CreatePlayer(username, Hash string, HomePlanet *Planet) Player {
    player := Player{username, Hash, HomePlanet.GetKey(), []int{0, 0}, []int{0, 0}}
    HomePlanet.Owner = username
    return player
}

<commit_msg>Set the public API for Player.StartMission<commit_after>package entities

import (
    "fmt"
    "encoding/json"
    "log"
)

type Player struct {
    username string
    Hash string
    HomePlanet string
    ScreenSize []int
    ScreenPosition []int

}

func (self Player) String() string {
    return self.username
}

func (self Player) GetKey() string {
    return fmt.Sprintf("player.%s", self.username)
}

func (self *Player) StartMission(start_planet, end_planet Planet, count int) error {
    // new_mission := CreateMission()
    //db.Do("RPUSH new_mission)
    return nil
}

func (self Player) Serialize() (string, []byte) {
    result, err := json.Marshal(self)
    if err != nil {
        log.Fatal(err)
    }
    return self.GetKey(), result
}

func CreatePlayer(username, Hash string, HomePlanet *Planet) Player {
    player := Player{username, Hash, HomePlanet.GetKey(), []int{0, 0}, []int{0, 0}}
    HomePlanet.Owner = username
    return player
}

<|endoftext|>


<commit_before><commit_msg>Use copy instead of for loop to copy cell data into bitmap.<commit_after><|endoftext|>


<commit_before><commit_msg>use freeString helper<commit_after><|endoftext|>


<commit_before><commit_msg>Display special chars in the graph output<commit_after><|endoftext|>


<commit_before>// +build ccm

package gocql

import (
	"log"
	"testing"
	"time"

	"github.com/gocql/gocql/internal/ccm"
)

func TestEventDiscovery(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	session := createSession(t)
	defer session.Close()

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("status=%+v\n", status)

	session.pool.mu.RLock()
	poolHosts := session.pool.hostConnPools // TODO: replace with session.ring
	t.Logf("poolhosts=%+v\n", poolHosts)
	// check we discovered all the nodes in the ring
	for _, host := range status {
		if _, ok := poolHosts[host.Addr]; !ok {
			t.Errorf("did not discover %q", host.Addr)
		}
	}
	session.pool.mu.RUnlock()
	if t.Failed() {
		t.FailNow()
	}
}

func TestEventNodeDownControl(t *testing.T) {
	t.Skip("FLAKE skipping")
	const targetNode = "node1"
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}

	cluster := createCluster()
	cluster.Hosts = []string{status[targetNode].Addr}
	session := createSessionFromCluster(cluster, t)
	defer session.Close()

	t.Log("marking " + targetNode + " as down")
	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	t.Logf("status=%+v\n", status)
	t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])

	time.Sleep(5 * time.Second)

	session.pool.mu.RLock()

	poolHosts := session.pool.hostConnPools
	node := status[targetNode]
	t.Logf("poolhosts=%+v\n", poolHosts)

	if _, ok := poolHosts[node.Addr]; ok {
		session.pool.mu.RUnlock()
		t.Fatal("node not removed after remove event")
	}
	session.pool.mu.RUnlock()

	host := session.ring.getHost(node.Addr)
	if host == nil {
		t.Fatal("node not in metadata ring")
	} else if host.IsUp() {
		t.Fatalf("not not marked as down after event in metadata: %v", host)
	}
}

func TestEventNodeDown(t *testing.T) {
	t.Skip("FLAKE skipping")
	const targetNode = "node3"
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	session := createSession(t)
	defer session.Close()

	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("status=%+v\n", status)
	t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])

	time.Sleep(5 * time.Second)

	session.pool.mu.RLock()
	defer session.pool.mu.RUnlock()

	poolHosts := session.pool.hostConnPools
	node := status[targetNode]
	t.Logf("poolhosts=%+v\n", poolHosts)

	if _, ok := poolHosts[node.Addr]; ok {
		t.Fatal("node not removed after remove event")
	}

	host := session.ring.getHost(node.Addr)
	if host == nil {
		t.Fatal("node not in metadata ring")
	} else if host.IsUp() {
		t.Fatalf("not not marked as down after event in metadata: %v", host)
	}
}

func TestEventNodeUp(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	log.Printf("status=%+v\n", status)

	session := createSession(t)
	defer session.Close()

	const targetNode = "node2"
	node := status[targetNode]

	_, ok := session.pool.getPool(node.Addr)
	if !ok {
		session.pool.mu.RLock()
		t.Errorf("target pool not in connection pool: addr=%q pools=%v", status[targetNode].Addr, session.pool.hostConnPools)
		session.pool.mu.RUnlock()
		t.FailNow()
	}

	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)

	_, ok = session.pool.getPool(node.Addr)
	if ok {
		t.Fatal("node not removed after remove event")
	}

	if err := ccm.NodeUp(targetNode); err != nil {
		t.Fatal(err)
	}

	// cassandra < 2.2 needs 10 seconds to start up the binary service
	time.Sleep(15 * time.Second)

	_, ok = session.pool.getPool(node.Addr)
	if !ok {
		t.Fatal("node not added after node added event")
	}

	host := session.ring.getHost(node.Addr)
	if host == nil {
		t.Fatal("node not in metadata ring")
	} else if !host.IsUp() {
		t.Fatalf("not not marked as UP after event in metadata: addr=%q host=%p: %v", node.Addr, host, host)
	}
}

func TestEventFilter(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	log.Printf("status=%+v\n", status)

	cluster := createCluster()
	cluster.HostFilter = WhiteListHostFilter(status["node1"].Addr)
	session := createSessionFromCluster(cluster, t)
	defer session.Close()

	if _, ok := session.pool.getPool(status["node1"].Addr); !ok {
		t.Errorf("should have %v in pool but dont", "node1")
	}

	for _, host := range [...]string{"node2", "node3"} {
		_, ok := session.pool.getPool(status[host].Addr)
		if ok {
			t.Errorf("should not have %v in pool", host)
		}
	}

	if t.Failed() {
		t.FailNow()
	}

	if err := ccm.NodeDown("node2"); err != nil {
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)

	if err := ccm.NodeUp("node2"); err != nil {
		t.Fatal(err)
	}

	time.Sleep(15 * time.Second)
	for _, host := range [...]string{"node2", "node3"} {
		_, ok := session.pool.getPool(status[host].Addr)
		if ok {
			t.Errorf("should not have %v in pool", host)
		}
	}

	if t.Failed() {
		t.FailNow()
	}

}

func TestEventDownQueryable(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	log.Printf("status=%+v\n", status)

	const targetNode = "node1"

	addr := status[targetNode].Addr

	cluster := createCluster()
	cluster.Hosts = []string{addr}
	cluster.HostFilter = WhiteListHostFilter(addr)
	session := createSessionFromCluster(cluster, t)
	defer session.Close()

	if pool, ok := session.pool.getPool(addr); !ok {
		t.Fatalf("should have %v in pool but dont", addr)
	} else if !pool.host.IsUp() {
		t.Fatalf("host is not up %v", pool.host)
	}

	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)

	if err := ccm.NodeUp(targetNode); err != nil {
		t.Fatal(err)
	}

	time.Sleep(15 * time.Second)

	if pool, ok := session.pool.getPool(addr); !ok {
		t.Fatalf("should have %v in pool but dont", addr)
	} else if !pool.host.IsUp() {
		t.Fatalf("host is not up %v", pool.host)
	}

	var rows int
	if err := session.Query("SELECT COUNT(*) FROM system.local").Scan(&rows); err != nil {
		t.Fatal(err)
	} else if rows != 1 {
		t.Fatalf("expected to get 1 row got %d", rows)
	}
}
<commit_msg>dont build ccm tests as they will be rewritten<commit_after>// +build ccm, ignore

package gocql

import (
	"log"
	"testing"
	"time"

	"github.com/gocql/gocql/internal/ccm"
)

func TestEventDiscovery(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	session := createSession(t)
	defer session.Close()

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("status=%+v\n", status)

	session.pool.mu.RLock()
	poolHosts := session.pool.hostConnPools // TODO: replace with session.ring
	t.Logf("poolhosts=%+v\n", poolHosts)
	// check we discovered all the nodes in the ring
	for _, host := range status {
		if _, ok := poolHosts[host.Addr]; !ok {
			t.Errorf("did not discover %q", host.Addr)
		}
	}
	session.pool.mu.RUnlock()
	if t.Failed() {
		t.FailNow()
	}
}

func TestEventNodeDownControl(t *testing.T) {
	t.Skip("FLAKE skipping")
	const targetNode = "node1"
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}

	cluster := createCluster()
	cluster.Hosts = []string{status[targetNode].Addr}
	session := createSessionFromCluster(cluster, t)
	defer session.Close()

	t.Log("marking " + targetNode + " as down")
	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	t.Logf("status=%+v\n", status)
	t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])

	time.Sleep(5 * time.Second)

	session.pool.mu.RLock()

	poolHosts := session.pool.hostConnPools
	node := status[targetNode]
	t.Logf("poolhosts=%+v\n", poolHosts)

	if _, ok := poolHosts[node.Addr]; ok {
		session.pool.mu.RUnlock()
		t.Fatal("node not removed after remove event")
	}
	session.pool.mu.RUnlock()

	host := session.ring.getHost(node.Addr)
	if host == nil {
		t.Fatal("node not in metadata ring")
	} else if host.IsUp() {
		t.Fatalf("not not marked as down after event in metadata: %v", host)
	}
}

func TestEventNodeDown(t *testing.T) {
	t.Skip("FLAKE skipping")
	const targetNode = "node3"
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	session := createSession(t)
	defer session.Close()

	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("status=%+v\n", status)
	t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])

	time.Sleep(5 * time.Second)

	session.pool.mu.RLock()
	defer session.pool.mu.RUnlock()

	poolHosts := session.pool.hostConnPools
	node := status[targetNode]
	t.Logf("poolhosts=%+v\n", poolHosts)

	if _, ok := poolHosts[node.Addr]; ok {
		t.Fatal("node not removed after remove event")
	}

	host := session.ring.getHost(node.Addr)
	if host == nil {
		t.Fatal("node not in metadata ring")
	} else if host.IsUp() {
		t.Fatalf("not not marked as down after event in metadata: %v", host)
	}
}

func TestEventNodeUp(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	log.Printf("status=%+v\n", status)

	session := createSession(t)
	defer session.Close()

	const targetNode = "node2"
	node := status[targetNode]

	_, ok := session.pool.getPool(node.Addr)
	if !ok {
		session.pool.mu.RLock()
		t.Errorf("target pool not in connection pool: addr=%q pools=%v", status[targetNode].Addr, session.pool.hostConnPools)
		session.pool.mu.RUnlock()
		t.FailNow()
	}

	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)

	_, ok = session.pool.getPool(node.Addr)
	if ok {
		t.Fatal("node not removed after remove event")
	}

	if err := ccm.NodeUp(targetNode); err != nil {
		t.Fatal(err)
	}

	// cassandra < 2.2 needs 10 seconds to start up the binary service
	time.Sleep(15 * time.Second)

	_, ok = session.pool.getPool(node.Addr)
	if !ok {
		t.Fatal("node not added after node added event")
	}

	host := session.ring.getHost(node.Addr)
	if host == nil {
		t.Fatal("node not in metadata ring")
	} else if !host.IsUp() {
		t.Fatalf("not not marked as UP after event in metadata: addr=%q host=%p: %v", node.Addr, host, host)
	}
}

func TestEventFilter(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	log.Printf("status=%+v\n", status)

	cluster := createCluster()
	cluster.HostFilter = WhiteListHostFilter(status["node1"].Addr)
	session := createSessionFromCluster(cluster, t)
	defer session.Close()

	if _, ok := session.pool.getPool(status["node1"].Addr); !ok {
		t.Errorf("should have %v in pool but dont", "node1")
	}

	for _, host := range [...]string{"node2", "node3"} {
		_, ok := session.pool.getPool(status[host].Addr)
		if ok {
			t.Errorf("should not have %v in pool", host)
		}
	}

	if t.Failed() {
		t.FailNow()
	}

	if err := ccm.NodeDown("node2"); err != nil {
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)

	if err := ccm.NodeUp("node2"); err != nil {
		t.Fatal(err)
	}

	time.Sleep(15 * time.Second)
	for _, host := range [...]string{"node2", "node3"} {
		_, ok := session.pool.getPool(status[host].Addr)
		if ok {
			t.Errorf("should not have %v in pool", host)
		}
	}

	if t.Failed() {
		t.FailNow()
	}

}

func TestEventDownQueryable(t *testing.T) {
	t.Skip("FLAKE skipping")
	if err := ccm.AllUp(); err != nil {
		t.Fatal(err)
	}

	status, err := ccm.Status()
	if err != nil {
		t.Fatal(err)
	}
	log.Printf("status=%+v\n", status)

	const targetNode = "node1"

	addr := status[targetNode].Addr

	cluster := createCluster()
	cluster.Hosts = []string{addr}
	cluster.HostFilter = WhiteListHostFilter(addr)
	session := createSessionFromCluster(cluster, t)
	defer session.Close()

	if pool, ok := session.pool.getPool(addr); !ok {
		t.Fatalf("should have %v in pool but dont", addr)
	} else if !pool.host.IsUp() {
		t.Fatalf("host is not up %v", pool.host)
	}

	if err := ccm.NodeDown(targetNode); err != nil {
		t.Fatal(err)
	}

	time.Sleep(5 * time.Second)

	if err := ccm.NodeUp(targetNode); err != nil {
		t.Fatal(err)
	}

	time.Sleep(15 * time.Second)

	if pool, ok := session.pool.getPool(addr); !ok {
		t.Fatalf("should have %v in pool but dont", addr)
	} else if !pool.host.IsUp() {
		t.Fatalf("host is not up %v", pool.host)
	}

	var rows int
	if err := session.Query("SELECT COUNT(*) FROM system.local").Scan(&rows); err != nil {
		t.Fatal(err)
	} else if rows != 1 {
		t.Fatalf("expected to get 1 row got %d", rows)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/go-martini/martini"
	"github.com/timothyye/vim-tips-web/routers"
	"net/http"
)

var (
	m = martini.Classic()
)

func main() {
	routers.Initialize(m)

	http.HandleFunc("/ws", routers.WSHandler)
	http.Handle("/", m)

	fmt.Println("Server started...")

	err := http.ListenAndServe("127.0.0.1:3001", nil)
	if err != nil {
		fmt.Println(err)
	}
}

func ShowMessage(mess string) {
	fmt.Println(mess)
}
<commit_msg>udpate server port<commit_after>package main

import (
	"fmt"
	"github.com/go-martini/martini"
	"github.com/timothyye/vim-tips-web/routers"
	"net/http"
)

var (
	m = martini.Classic()
)

func main() {
	routers.Initialize(m)

	http.HandleFunc("/ws", routers.WSHandler)
	http.Handle("/", m)

	fmt.Println("Server started...")

	err := http.ListenAndServe("0.0.0.0:3001", nil)
	if err != nil {
		fmt.Println(err)
	}
}

func ShowMessage(mess string) {
	fmt.Println(mess)
}
<|endoftext|>


<commit_before>package main

import (
    "github.com/go-martini/martini"
    "net/http"
    "encoding/json"
    "os/exec"
    "fmt"
    "strings"
    "github.com/garyburd/redigo/redis"
)

// Expire caches in an hour
const CACHE_TTL = 60 * 60

type ScoreResponse struct {
    Score string `json:"score"`
    URL string `json:"url"`
}

type ErrorResponse struct {
    Error string `json:"error"`
}

func MarshalToJsonBytes(res interface{}) []byte {
    resAsJson, _ := json.Marshal(res)
    return ([]byte(resAsJson))
}

func GetScoreResponseAsJson(score string, url_or_slug string) []byte {
    res := &ScoreResponse{
        Score:   score,
        URL: url_or_slug}
    return MarshalToJsonBytes(res)
}

func GetScoreErrorAsJson(url_or_slug string) []byte {
    res := &ErrorResponse{
        Error: "Could not determine score for " + url_or_slug}
    return MarshalToJsonBytes(res)
}

func GetScore(res http.ResponseWriter, req *http.Request, params martini.Params) {
    res.Header().Set("Content-Type", "application/json")

    query_params := req.URL.Query()

    var url_or_slug string
    var param_matches []string
    var ok bool
    if param_matches,ok = query_params["url"]; !ok {
        param_matches = query_params["github"]
    }
    url_or_slug = param_matches[0]

    score, score_error := GetScoreForUrlOrSlug(url_or_slug)

    if score_error != nil {
        res.Write(GetScoreErrorAsJson(url_or_slug))
    } else {
        res.Write(GetScoreResponseAsJson(score, url_or_slug))
    }

}

var redisConnection redis.Conn

func CacheKeyForUrlOrSlug(url_or_slug string) string{
    return "url_or_slug:" + url_or_slug
}

func GetCachedScoreForUrlOrSlug(url_or_slug string) (string, error) {
    score, err := redis.String(redisConnection.Do("GET", CacheKeyForUrlOrSlug(url_or_slug)))
    return score, err
}

func CacheScoreForUrlOrSlug(score string, url_or_slug string) {
    redisConnection.Do("SET", CacheKeyForUrlOrSlug(url_or_slug), score)
    redisConnection.Do("EXPIRE", CacheKeyForUrlOrSlug(url_or_slug), CACHE_TTL)
}

func GetScoreForUrlOrSlug(url_or_slug string) (string, error) {
    var score string
    var err error
    score, err = GetCachedScoreForUrlOrSlug(url_or_slug)
    if err != nil {
        rubyCmd := exec.Command("./get_score.rb", url_or_slug)
        var scoreOut []byte
        scoreOut, err = rubyCmd.Output()
        if err != nil {
            fmt.Println(err)
            return "", err
        }
        lines := strings.Split(string(scoreOut), "\n")
        score = lines[len(lines) - 2]
        CacheScoreForUrlOrSlug(score, url_or_slug)
    }

    return score, err
}

func main() {
    var redisError error
    redisConnection, redisError = redis.Dial("tcp", ":6379")
    if redisError != nil {
        panic(redisError)
    }
    defer redisConnection.Close()
    m := martini.Classic()
    m.Get("/score(\\.(?P<format>json|html))?", GetScore)
    m.Run()
}<commit_msg>remove global variables<commit_after>package main

import (
    "github.com/go-martini/martini"
    "net/http"
    "encoding/json"
    "os/exec"
    "fmt"
    "strings"
    "github.com/garyburd/redigo/redis"
    "time"
)

// Expire caches in an hour
const CACHE_TTL = 60 * 60

type Server struct {
    Redis *redis.Conn
    Martini *martini.ClassicMartini
}

type ScoreResponse struct {
    Score string `json:"score"`
    URL string `json:"url"`
}

type ErrorResponse struct {
    Error string `json:"error"`
}

func MarshalToJsonBytes(res interface{}) []byte {
    resAsJson, _ := json.Marshal(res)
    return ([]byte(resAsJson))
}

func GetScoreResponseAsJson(score string, url_or_slug string) []byte {
    res := &ScoreResponse{
        Score:   score,
        URL: url_or_slug}
    return MarshalToJsonBytes(res)
}

func GetScoreErrorAsJson(url_or_slug string) []byte {
    res := &ErrorResponse{
        Error: "Could not determine score for " + url_or_slug}
    return MarshalToJsonBytes(res)
}

func CacheKeyForUrlOrSlug(url_or_slug string) string{
    return "url_or_slug:" + url_or_slug
}

func (server *Server) GetScore(res http.ResponseWriter, req *http.Request, params martini.Params) {
    res.Header().Set("Content-Type", "application/json")

    query_params := req.URL.Query()

    var url_or_slug string
    var param_matches []string
    var ok bool
    if param_matches,ok = query_params["url"]; !ok {
        param_matches = query_params["github"]
    }
    url_or_slug = param_matches[0]

    score, score_error := server.GetScoreForUrlOrSlug(url_or_slug)

    if score_error != nil {
        res.Write(GetScoreErrorAsJson(url_or_slug))
    } else {
        res.Write(GetScoreResponseAsJson(score, url_or_slug))
    }

}

func (server *Server) GetCachedScoreForUrlOrSlug(url_or_slug string) (string, error) {
    score, err := redis.String((*server.Redis).Do("GET", CacheKeyForUrlOrSlug(url_or_slug)))
    return score, err
}

func (server *Server)  CacheScoreForUrlOrSlug(score string, url_or_slug string) {
    (*server.Redis).Do("SET", CacheKeyForUrlOrSlug(url_or_slug), score)
    (*server.Redis).Do("EXPIRE", CacheKeyForUrlOrSlug(url_or_slug), CACHE_TTL)
}

func (server *Server) GetScoreForUrlOrSlug(url_or_slug string) (string, error) {
    var score string
    var err error
    score, err = server.GetCachedScoreForUrlOrSlug(url_or_slug)
    if err != nil {
        rubyCmd := exec.Command("./get_score.rb", url_or_slug)
        var scoreOut []byte
        scoreOut, err = rubyCmd.Output()
        if err != nil {
            fmt.Println(err)
            return "", err
        }
        lines := strings.Split(string(scoreOut), "\n")
        score = lines[len(lines) - 2]
        server.CacheScoreForUrlOrSlug(score, url_or_slug)
    }

    return score, err
}

func ConnectRedis(redisChannel chan redis.Conn) {
    connection, redisError := redis.Dial("tcp", ":6379")
    if connection != nil {
        redisChannel <- connection
    } else if redisError != nil {
        fmt.Println(redisError)
    } else {
        fmt.Println("Everyting was nil?")
    }
}

func CreateServer(server *Server) {
    m := martini.Classic()
    m.Get("/score(\\.(?P<format>json|html))?", server.GetScore)
    server.Martini = m
    m.Run()
}

func Start(redisChannel chan redis.Conn, retryCount int, server *Server) {
    go ConnectRedis(redisChannel)
    select {
    case redisConnection := <- redisChannel:
        server.Redis = &redisConnection
        fmt.Println("Redis connected")
        defer redisConnection.Close()
        CreateServer(server)
    case <-time.After(time.Second * 1):
        retryCount += 1
        if retryCount < 5 {
            fmt.Println("Retrying Redis connection")
            Start(redisChannel, retryCount, server)
        } else {
            panic("Something is going wrong")
        }
    }

}

func main() {
    redisChannel := make(chan redis.Conn, 1)
    retryCount := 0
    server := Server{}
    Start(redisChannel, retryCount, &server)
}<|endoftext|>


<commit_before>package datad

import (
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"reflect"
	"strconv"
	"strings"
	"testing"
)

type datum struct{ value, version string }

// InMemoryProvider is maps keys to their version.
type InMemoryData map[string]datum

func NewInMemoryData(data map[string]datum) InMemoryData {
	if data == nil {
		data = make(map[string]datum)
	} else {
		// Ensure all paths begin with '/'.
		for k, d := range data {
			if !strings.HasPrefix(k, "/") {
				delete(data, k)
				data["/"+k] = d
			}
		}
	}
	return InMemoryData(data)
}

func (m InMemoryData) KeyVersion(key string) (string, error) {
	if !strings.HasPrefix(key, "/") {
		key = "/" + key
	}
	d, present := m[key]
	if !present {
		return "", ErrKeyNotExist
	}
	return d.version, nil
}

func (m InMemoryData) KeyVersions(keyPrefix string) (map[string]string, error) {
	if !strings.HasPrefix(keyPrefix, "/") {
		keyPrefix = "/" + keyPrefix
	}
	if !strings.HasSuffix(keyPrefix, "/") {
		keyPrefix += "/"
	}
	subkvs := make(map[string]string)
	for k, d := range m {
		if strings.HasPrefix(k, keyPrefix) {
			subkvs[strings.TrimPrefix(k, keyPrefix)] = d.version
		}
	}
	return subkvs, nil
}

type FakeServer struct {
	InMemoryData
}

func NewFakeServer(data map[string]datum) FakeServer {
	return FakeServer{NewInMemoryData(data)}
}

func (s FakeServer) Update(key, version string) error {
	// simulate some computation or remote data fetch
	ver, _ := strconv.Atoi(version)

	if !strings.HasPrefix(key, "/") {
		key = "/" + key
	}
	s.InMemoryData[key] = datum{"val" + string('A'+ver), version}

	if *debug {
		log.Printf("data[%q] = %+v", key, s.InMemoryData[key])
	}

	return nil
}

func (s FakeServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	d, present := s.InMemoryData[r.URL.Path]
	if !present {
		http.Error(w, ErrKeyNotExist.Error(), http.StatusNotFound)
	}
	w.Write([]byte(d.value))
}

func TestIntegration(t *testing.T) {
	data := map[string]datum{
		"/alice": {"valA", "0"},
		"/bob":   {"valB", "1"},
	}
	fakeServer := NewFakeServer(data)

	dataServer := httptest.NewServer(fakeServer)
	defer dataServer.Close()

	providerServer := httptest.NewServer(NewProviderHandler(fakeServer))
	defer providerServer.Close()

	c := NewClient(NewInMemoryBackend(nil))

	// Add the server.
	err := c.AddProvider(providerServer.URL, dataServer.URL)
	if err != nil {
		t.Fatal(err)
	}

	// Check that it was added.
	providers, err := c.ListProviders()
	if err != nil {
		t.Fatal(err)
	}
	if want := []string{providerServer.URL}; !reflect.DeepEqual(providers, want) {
		t.Errorf("got providers == %v, want %v", providers, want)
	}

	// Check that the key is unroutable because although it exists on the
	// provider, the provider has not yet synced to the registry (we call
	// RegisterKeysOnProvider below).
	_, err = c.DataURL("/alice")
	if err != ErrNoProviderForKey {
		t.Error(err)
	}

	// Register the server's existing data.
	err = c.RegisterKeysOnProvider(providerServer.URL)
	if err != nil {
		t.Fatal(err)
	}

	// After calling RegisterKeysOnProvider, the key should be routable.
	dataURL, err := c.DataURL("/alice")
	if err != nil {
		t.Fatal(err)
	}
	if want := dataServer.URL; dataURL.String() != want {
		t.Errorf("got DataURL == %q, want %q", dataURL, want)
	}

	dataTransport, err := c.DataTransport("/alice", nil)
	if err != nil {
		t.Fatal(err)
	}
	val := httpGet("DataTransport", t, dataTransport, "/alice")
	if want := "valA"; val != want {
		t.Errorf("got /alice == %q, want %q", val, want)
	}
}

func httpGet(label string, t *testing.T, transport http.RoundTripper, url string) string {
	c := &http.Client{Transport: transport}
	resp, err := c.Get(url)
	if err != nil {
		t.Fatalf("%s (%s): %s", label, url, err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("%s (%s): %s", label, url, err)
	}
	return string(body)
}
<commit_msg>more integration tests<commit_after>package datad

import (
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"reflect"
	"sort"
	"strconv"
	"strings"
	"testing"
)

type datum struct{ value, version string }

// InMemoryProvider is maps keys to their version.
type InMemoryData map[string]datum

func NewInMemoryData(data map[string]datum) InMemoryData {
	if data == nil {
		data = make(map[string]datum)
	} else {
		// Ensure all paths begin with '/'.
		for k, d := range data {
			if !strings.HasPrefix(k, "/") {
				delete(data, k)
				data["/"+k] = d
			}
		}
	}
	return InMemoryData(data)
}

func (m InMemoryData) KeyVersion(key string) (string, error) {
	if !strings.HasPrefix(key, "/") {
		key = "/" + key
	}
	d, present := m[key]
	if !present {
		return "", ErrKeyNotExist
	}
	return d.version, nil
}

func (m InMemoryData) KeyVersions(keyPrefix string) (map[string]string, error) {
	if !strings.HasPrefix(keyPrefix, "/") {
		keyPrefix = "/" + keyPrefix
	}
	if !strings.HasSuffix(keyPrefix, "/") {
		keyPrefix += "/"
	}
	subkvs := make(map[string]string)
	for k, d := range m {
		if strings.HasPrefix(k, keyPrefix) {
			subkvs[strings.TrimPrefix(k, keyPrefix)] = d.version
		}
	}
	return subkvs, nil
}

type FakeServer struct {
	InMemoryData
}

func NewFakeServer(data map[string]datum) FakeServer {
	return FakeServer{NewInMemoryData(data)}
}

func (s FakeServer) Update(key, version string) error {
	// simulate some computation or remote data fetch
	ver, _ := strconv.Atoi(version)

	if !strings.HasPrefix(key, "/") {
		key = "/" + key
	}
	s.InMemoryData[key] = datum{"val" + string('A'+ver), version}

	if *debug {
		log.Printf("data[%q] = %+v", key, s.InMemoryData[key])
	}

	return nil
}

func (s FakeServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	d, present := s.InMemoryData[r.URL.Path]
	if !present {
		http.Error(w, ErrKeyNotExist.Error(), http.StatusNotFound)
	}
	w.Write([]byte(d.value))
}

func TestIntegration_Simple(t *testing.T) {
	data := map[string]datum{
		"/alice": {"valA", "0"},
		"/bob":   {"valB", "1"},
	}
	fakeServer := NewFakeServer(data)

	dataServer := httptest.NewServer(fakeServer)
	defer dataServer.Close()

	providerServer := httptest.NewServer(NewProviderHandler(fakeServer))
	defer providerServer.Close()

	c := NewClient(NewInMemoryBackend(nil))

	// Add the server.
	err := c.AddProvider(providerServer.URL, dataServer.URL)
	if err != nil {
		t.Fatal(err)
	}

	// Check that it was added.
	providers, err := c.ListProviders()
	if err != nil {
		t.Fatal(err)
	}
	if want := []string{providerServer.URL}; !reflect.DeepEqual(providers, want) {
		t.Errorf("got providers == %v, want %v", providers, want)
	}

	// Check that the key is unroutable because although it exists on the
	// provider, the provider has not yet synced to the registry (we call
	// RegisterKeysOnProvider below).
	_, err = c.DataURL("/alice")
	if err != ErrNoProviderForKey {
		t.Error(err)
	}

	// Register the server's existing data.
	err = c.RegisterKeysOnProvider(providerServer.URL)
	if err != nil {
		t.Fatal(err)
	}

	// After calling RegisterKeysOnProvider, the key should be routable.
	dataURL, err := c.DataURL("/alice")
	if err != nil {
		t.Fatal(err)
	}
	if want := dataServer.URL; dataURL.String() != want {
		t.Errorf("got DataURL == %q, want %q", dataURL, want)
	}

	dataTransport, err := c.DataTransport("/alice", nil)
	if err != nil {
		t.Fatal(err)
	}
	val := httpGet("DataTransport", t, dataTransport, "/alice")
	if want := "valA"; val != want {
		t.Errorf("got /alice == %q, want %q", val, want)
	}
}

func TestIntegration_TwoProviders(t *testing.T) {
	data1 := map[string]datum{"/alice": {"valA", "0"}}
	fakeServer1 := NewFakeServer(data1)
	dataServer1 := httptest.NewServer(fakeServer1)
	defer dataServer1.Close()
	providerServer1 := httptest.NewServer(NewProviderHandler(fakeServer1))
	defer providerServer1.Close()

	data2 := map[string]datum{"/bob": {"valB", "1"}}
	fakeServer2 := NewFakeServer(data2)
	dataServer2 := httptest.NewServer(fakeServer2)
	defer dataServer2.Close()
	providerServer2 := httptest.NewServer(NewProviderHandler(fakeServer2))
	defer providerServer2.Close()

	c := NewClient(NewInMemoryBackend(nil))

	// Add the servers.
	err := c.AddProvider(providerServer1.URL, dataServer1.URL)
	if err != nil {
		t.Fatal(err)
	}
	err = c.AddProvider(providerServer2.URL, dataServer2.URL)
	if err != nil {
		t.Fatal(err)
	}

	// Check that they were added.
	providers, err := c.ListProviders()
	if err != nil {
		t.Fatal(err)
	}
	sort.Strings(providers)
	wantProviders := []string{providerServer1.URL, providerServer2.URL}
	sort.Strings(wantProviders)
	if !reflect.DeepEqual(providers, wantProviders) {
		t.Errorf("got providers == %v, want %v", providers, wantProviders)
	}

	// Register the servers' existing data.
	err = c.RegisterKeysOnProvider(providerServer1.URL)
	if err != nil {
		t.Fatal(err)
	}
	err = c.RegisterKeysOnProvider(providerServer2.URL)
	if err != nil {
		t.Fatal(err)
	}

	// After calling RegisterKeysOnProvider, the keys should be routable.

	// "/alice" is on server 1.
	dataURL, err := c.DataURL("/alice")
	if err != nil {
		t.Fatal(err)
	}
	if want := dataServer1.URL; dataURL.String() != want {
		t.Errorf("got DataURL == %q, want %q", dataURL, want)
	}

	// "/bob" is on server 2.
	dataURL, err = c.DataURL("/bob")
	if err != nil {
		t.Fatal(err)
	}
	if want := dataServer2.URL; dataURL.String() != want {
		t.Errorf("got DataURL == %q, want %q", dataURL, want)
	}
}

func TestIntegration_TwoProviders_DifferentVersions(t *testing.T) {
	t.Skip("not yet implemented")

	data1 := map[string]datum{"/alice": {"valA", "0"}}
	fakeServer1 := NewFakeServer(data1)
	dataServer1 := httptest.NewServer(fakeServer1)
	defer dataServer1.Close()
	providerServer1 := httptest.NewServer(NewProviderHandler(fakeServer1))
	defer providerServer1.Close()

	data2 := map[string]datum{"/alice": {"valB", "1"}}
	fakeServer2 := NewFakeServer(data2)
	dataServer2 := httptest.NewServer(fakeServer2)
	defer dataServer2.Close()
	providerServer2 := httptest.NewServer(NewProviderHandler(fakeServer2))
	defer providerServer2.Close()

	c := NewClient(NewInMemoryBackend(nil))

	// Add the servers.
	err := c.AddProvider(providerServer1.URL, dataServer1.URL)
	if err != nil {
		t.Fatal(err)
	}
	err = c.AddProvider(providerServer2.URL, dataServer2.URL)
	if err != nil {
		t.Fatal(err)
	}

	// Register the servers' existing data.
	err = c.RegisterKeysOnProvider(providerServer1.URL)
	if err != nil {
		t.Fatal(err)
	}
	err = c.RegisterKeysOnProvider(providerServer2.URL)
	if err != nil {
		t.Fatal(err)
	}

	// Server 1 has version 0 of "/alice" and server 2 has version 1 of
	// "/alice". TODO(sqs): The second call to RegisterKeysOnProvider recognize
	// this and trigger an update on server.

	// After the updates, they should both be at version 1 (TODO(sqs): or maybe they both
	// update from the source, since it's hard to know which is the newer one).
	dvs, err := c.DataURLVersions("/alice")
	if err != nil {
		t.Fatal(err)
	}
	for dataURL, ver := range dvs {
		if want := "1"; ver != want {
			t.Errorf("got dataURL %q version == %q, want %q", dataURL, ver, want)
		}
	}
}

func httpGet(label string, t *testing.T, transport http.RoundTripper, url string) string {
	c := &http.Client{Transport: transport}
	resp, err := c.Get(url)
	if err != nil {
		t.Fatalf("%s (%s): %s", label, url, err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("%s (%s): %s", label, url, err)
	}
	return string(body)
}
<|endoftext|>


<commit_before>// Copyright 2015 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build docker_integration

package docker

import (
	"bytes"
	"os"
	"testing"
)

var dockerEndpoint string

func init() {
	dockerEndpoint = os.Getenv("DOCKER_HOST")
	if dockerEndpoint == "" {
		dockerEndpoint = "unix:///var/run/docker.sock"
	}
}

func TestIntegrationPullCreateStartLogs(t *testing.T) {
	imageName := pullImage(t)
	client := getClient()
	hostConfig := HostConfig{PublishAllPorts: true}
	createOpts := CreateContainerOptions{
		Config: &Config{
			Image: imageName,
			Cmd:   []string{"cat", "/home/gopher/file.txt"},
			User:  "gopher",
		},
		HostConfig: &hostConfig,
	}
	container, err := client.CreateContainer(createOpts)
	if err != nil {
		t.Fatal(err)
	}
	err = client.StartContainer(container.ID, &hostConfig)
	if err != nil {
		t.Fatal(err)
	}
	status, err := client.WaitContainer(container.ID)
	if err != nil {
		t.Error(err)
	}
	if status != 0 {
		t.Error("WaitContainer(%q): wrong status. Want 0. Got %d", container.ID, status)
	}
	var stdout, stderr bytes.Buffer
	logsOpts := LogsOptions{
		Container:    container.ID,
		OutputStream: &stdout,
		ErrorStream:  &stderr,
		Stdout:       true,
		Stderr:       true,
	}
	err = client.Logs(logsOpts)
	if err != nil {
		t.Error(err)
	}
	if stderr.String() != "" {
		t.Errorf("Got unexpected stderr from logs: %q", stderr.String())
	}
	expected := `Welcome to reality, wake up and rejoice
Welcome to reality, you've made the right choice
Welcome to reality, and let them hear your voice, shout it out!
`
	if stdout.String() != expected {
		t.Errorf("Got wrong stdout from logs.\nWant:\n%#v.\n\nGot:\n%#v.", expected, stdout.String())
	}
}

func pullImage(t *testing.T) string {
	imageName := "fsouza/go-dockerclient-integration"
	var buf bytes.Buffer
	pullOpts := PullImageOptions{
		Repository:   imageName,
		OutputStream: &buf,
	}
	client := getClient()
	err := client.PullImage(pullOpts, AuthConfiguration{})
	if err != nil {
		t.Logf("Pull output: %s", buf.String())
		t.Fatal(err)
	}
	return imageName
}

func getClient() *Client {
	client, _ := NewClient(dockerEndpoint)
	return client
}
<commit_msg>integration_test: fix function call to use formatted output<commit_after>// Copyright 2015 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build docker_integration

package docker

import (
	"bytes"
	"os"
	"testing"
)

var dockerEndpoint string

func init() {
	dockerEndpoint = os.Getenv("DOCKER_HOST")
	if dockerEndpoint == "" {
		dockerEndpoint = "unix:///var/run/docker.sock"
	}
}

func TestIntegrationPullCreateStartLogs(t *testing.T) {
	imageName := pullImage(t)
	client := getClient()
	hostConfig := HostConfig{PublishAllPorts: true}
	createOpts := CreateContainerOptions{
		Config: &Config{
			Image: imageName,
			Cmd:   []string{"cat", "/home/gopher/file.txt"},
			User:  "gopher",
		},
		HostConfig: &hostConfig,
	}
	container, err := client.CreateContainer(createOpts)
	if err != nil {
		t.Fatal(err)
	}
	err = client.StartContainer(container.ID, &hostConfig)
	if err != nil {
		t.Fatal(err)
	}
	status, err := client.WaitContainer(container.ID)
	if err != nil {
		t.Error(err)
	}
	if status != 0 {
		t.Errorf("WaitContainer(%q): wrong status. Want 0. Got %d", container.ID, status)
	}
	var stdout, stderr bytes.Buffer
	logsOpts := LogsOptions{
		Container:    container.ID,
		OutputStream: &stdout,
		ErrorStream:  &stderr,
		Stdout:       true,
		Stderr:       true,
	}
	err = client.Logs(logsOpts)
	if err != nil {
		t.Error(err)
	}
	if stderr.String() != "" {
		t.Errorf("Got unexpected stderr from logs: %q", stderr.String())
	}
	expected := `Welcome to reality, wake up and rejoice
Welcome to reality, you've made the right choice
Welcome to reality, and let them hear your voice, shout it out!
`
	if stdout.String() != expected {
		t.Errorf("Got wrong stdout from logs.\nWant:\n%#v.\n\nGot:\n%#v.", expected, stdout.String())
	}
}

func pullImage(t *testing.T) string {
	imageName := "fsouza/go-dockerclient-integration"
	var buf bytes.Buffer
	pullOpts := PullImageOptions{
		Repository:   imageName,
		OutputStream: &buf,
	}
	client := getClient()
	err := client.PullImage(pullOpts, AuthConfiguration{})
	if err != nil {
		t.Logf("Pull output: %s", buf.String())
		t.Fatal(err)
	}
	return imageName
}

func getClient() *Client {
	client, _ := NewClient(dockerEndpoint)
	return client
}
<|endoftext|>


<commit_before>package main

import (
	"log"
	"net/http"

	"github.com/squiidz/fur"
	"github.com/squiidz/fur/middle"
	"github.com/squiidz/fur/context"
)

func main() {
	// Create a NewServer
	s := fur.NewServer("localhost", ":8080", false)

	// Set some default middleware for all Route
	// Use Mutate() to transform a handler into a Middleware
	s.Stack(MiddleLog, middle.Mutate(NonValid))

	// Add a new routes and add some middleware for this one only
	// You can force a HTTP method (.Get(), .Post(), .Put(), .Delete())
	s.AddRoute("/nuts", DefaultHandler).Get()
	s.AddRoute("/", DefaultHandler, MiddleRedirect)
	s.AddStatic("/public/", "my/assets/folder/")

	// Start the server
	s.Start()
}

// Application Handler
func DefaultHandler(rw http.ResponseWriter, req *http.Request) {
	// Short Retrive Context way
	value := context.FindContext(req).Get("MyKey")

	rw.Write([]byte(value.(string))
}

// Middleware Logger
func MiddleLog(next http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		// Short Context way
		context.NewContext(req).Set("MyKey", "MyValue")

		log.Printf("[%s] %s %s", req.Method, req.RequestURI, req.RemoteAddr)
		next.ServeHTTP(rw, req)
	})
}

// Useless Middleware, just for example
func MiddleRedirect(next http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		if req.RequestURI != "/nuts" {
			http.Redirect(rw, req, "/nuts", http.StatusFound)
		} else {
			next.ServeHTTP(rw, req)
		}
	})
}

// A middleware without the valid signature
func NonValid(rw http.ResponseWriter, req *http.Request) {
	log.Printf("%s", req.RequestURI)
}<commit_msg>Update Example<commit_after>package main

import (
	"log"
	"net/http"

	"github.com/squiidz/fur"
	"github.com/squiidz/fur/middle"
	"github.com/squiidz/fur/context"
)

func main() {
	// Create a NewServer
	s := fur.NewServer("localhost", ":8080", false)

	// Set some default middleware for all Route
	// Use Mutate() to transform a handler into a Middleware
	s.Stack(MiddleLog, middle.Mutate(NonValid))

	// Add a new routes and add some middleware for this one only
	// You can force a HTTP method (.Get(), .Post(), .Put(), .Delete())
	s.AddRoute("/nuts", DefaultHandler).Get()
	s.AddRoute("/", DefaultHandler, MiddleRedirect)
	s.AddStatic("/public/", "my/assets/folder/")

	// Start the server
	s.Start()
}

// Application Handler
func DefaultHandler(rw http.ResponseWriter, req *http.Request) {
	// Short Retrive Context way
	value := context.Find(req).Get("MyKey")

	rw.Write([]byte(value.(string))
}

// Middleware Logger
func MiddleLog(next http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		// Short Context way
		context.NewContext(req).Set("MyKey", "MyValue")

		log.Printf("[%s] %s %s", req.Method, req.RequestURI, req.RemoteAddr)
		next.ServeHTTP(rw, req)
	})
}

// Useless Middleware, just for example
func MiddleRedirect(next http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		if req.RequestURI != "/nuts" {
			http.Redirect(rw, req, "/nuts", http.StatusFound)
		} else {
			next.ServeHTTP(rw, req)
		}
	})
}

// A middleware without the valid signature
func NonValid(rw http.ResponseWriter, req *http.Request) {
	log.Printf("%s", req.RequestURI)
}<|endoftext|>


<commit_before>package main

import (
	"fmt"

	"github.com/transitorykris/go-keywords"
)

func main() {
	// Create our new keywords
	kw := keywords.New()

	// Add some users
	kw.Add("hello", 1)
	kw.Add("Hello", 1)
	kw.Add("hello", 2)
	kw.Add("world", 2)
	kw.Add("WORLD", 3)

	// See who matches this
	fmt.Println(kw.MatchedUsers("Hello, world!"))            // [1 2 3]
	fmt.Println(kw.MatchedUsers("This is an example World")) // [3]

	kw.Remove("world", 3)
	fmt.Println(kw.MatchedUsers("This is an example World")) // []
}
<commit_msg>correct the example output<commit_after>package main

import (
	"fmt"

	"github.com/transitorykris/go-keywords"
)

func main() {
	// Create our new keywords
	kw := keywords.New()

	// Add some users
	kw.Add("hello", 1)
	kw.Add("Hello", 1)
	kw.Add("hello", 2)
	kw.Add("world", 2)
	kw.Add("WORLD", 3)

	// See who matches this
	fmt.Println(kw.MatchedUsers("Hello, world!"))            // [1 2 3]
	fmt.Println(kw.MatchedUsers("This is an example World")) // [2 3]

	kw.Remove("world", 3)
	fmt.Println(kw.MatchedUsers("This is an example World")) // [2]
}
<|endoftext|>


<commit_before>// Copyright 2015 CoreOS, Inc.
//
// 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 oem

import (
	"fmt"
	"net/url"

	"github.com/coreos/ignition/config/types"
	"github.com/coreos/ignition/internal/providers"
	"github.com/coreos/ignition/internal/providers/azure"
	"github.com/coreos/ignition/internal/providers/digitalocean"
	"github.com/coreos/ignition/internal/providers/ec2"
	"github.com/coreos/ignition/internal/providers/file"
	"github.com/coreos/ignition/internal/providers/gce"
	"github.com/coreos/ignition/internal/providers/noop"
	"github.com/coreos/ignition/internal/providers/openstack"
	"github.com/coreos/ignition/internal/providers/packet"
	"github.com/coreos/ignition/internal/providers/qemu"
	"github.com/coreos/ignition/internal/providers/vmware"
	"github.com/coreos/ignition/internal/registry"

	"github.com/vincent-petithory/dataurl"
)

// Config represents a set of options that map to a particular OEM.
type Config struct {
	name              string
	fetch             providers.FuncFetchConfig
	baseConfig        types.Config
	defaultUserConfig types.Config
}

func (c Config) Name() string {
	return c.name
}

func (c Config) FetchFunc() providers.FuncFetchConfig {
	return c.fetch
}

func (c Config) BaseConfig() types.Config {
	return c.baseConfig
}

func (c Config) DefaultUserConfig() types.Config {
	return c.defaultUserConfig
}

var configs = registry.Create("oem configs")

func init() {
	configs.Register(Config{
		name:  "azure",
		fetch: azure.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "waagent.service"},
					{Name: "etcd.service", Dropins: []types.Dropin{
						{Name: "10-oem.conf", Contents: "[Service]\nEnvironment=ETCD_PEER_ELECTION_TIMEOUT=1200\n"},
					}},
					{Name: "etcd2.service", Dropins: []types.Dropin{
						{Name: "10-oem.conf", Contents: "[Service]\nEnvironment=ETCD_ELECTION_TIMEOUT=1200\n"},
					}},
				},
			},
			Storage: types.Storage{Files: []types.File{serviceFromOem("waagent.service")}},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("Azure", "azure")}}},
	})
	configs.Register(Config{
		name:  "cloudsigma",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "cloudstack",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "digitalocean",
		fetch: digitalocean.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{{Enable: true, Name: "coreos-metadata-sshkeys@.service"}},
			},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("DigitalOcean", "digitalocean")}}},
	})
	configs.Register(Config{
		name:              "brightbox",
		fetch:             openstack.FetchConfig,
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("BrightBox", "ec2-compat")}}},
	})
	configs.Register(Config{
		name:              "openstack",
		fetch:             openstack.FetchConfig,
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("OpenStack", "ec2-compat")}}},
	})
	configs.Register(Config{
		name:  "ec2",
		fetch: ec2.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "coreos-metadata-sshkeys@.service"},
					{Name: "etcd.service", Dropins: []types.Dropin{
						{Name: "10-oem.conf", Contents: "[Service]\nEnvironment=ETCD_PEER_ELECTION_TIMEOUT=1200\n"},
					}},
					{Name: "etcd2.service", Dropins: []types.Dropin{
						{Name: "10-oem.conf", Contents: "[Service]\nEnvironment=ETCD_ELECTION_TIMEOUT=1200\n"},
					}},
				},
			},
		},
		defaultUserConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Mask: true, Name: "user-configdrive.service"},
					{Mask: true, Name: "user-configvirtfs.service"},
					userCloudInit("EC2", "ec2-compat"),
				},
			},
		},
	})
	configs.Register(Config{
		name:  "exoscale",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "gce",
		fetch: gce.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "coreos-metadata-sshkeys@.service"},
					{Enable: true, Name: "oem-gce.service"},
				},
			},
			Storage: types.Storage{
				Files: []types.File{
					serviceFromOem("oem-gce.service"),
					{
						Node: types.Node{
							Filesystem: "root",
							Path:       "/etc/hosts",
						},
						FileEmbedded1: types.FileEmbedded1{
							Mode:     0444,
							Contents: contentsFromString("169.254.169.254 metadata\n127.0.0.1 localhost\n"),
						},
					},
					{
						Node: types.Node{
							Filesystem: "root",
							Path:       "/etc/profile.d/google-cloud-sdk.sh",
						},
						FileEmbedded1: types.FileEmbedded1{
							Mode: 0444,
							Contents: contentsFromString(`#!/bin/sh
alias gcloud="(docker images google/cloud-sdk || docker pull google/cloud-sdk) > /dev/null;docker run -t -i --net="host" -v $HOME/.config:/.config -v /var/run/docker.sock:/var/run/doker.sock -v /usr/bin/docker:/usr/bin/docker google/cloud-sdk gcloud"
alias gcutil="(docker images google/cloud-sdk || docker pull google/cloud-sdk) > /dev/null;docker run -t -i --net="host" -v $HOME/.config:/.config google/cloud-sdk gcutil"
alias gsutil="(docker images google/cloud-sdk || docker pull google/cloud-sdk) > /dev/null;docker run -t -i --net="host" -v $HOME/.config:/.config google/cloud-sdk gsutil"
`),
						},
					},
				},
			},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("GCE", "gce")}}},
	})
	configs.Register(Config{
		name:  "hyperv",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "niftycloud",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "packet",
		fetch: packet.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "coreos-metadata-sshkeys@.service"},
					{Enable: true, Name: "packet-phone-home.service"},
				},
			},
			Storage: types.Storage{Files: []types.File{serviceFromOem("packet-phone-home.service")}},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("Packet", "packet")}}},
	})
	configs.Register(Config{
		name:  "pxe",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "rackspace",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "rackspace-onmetal",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "vagrant",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "vmware",
		fetch: vmware.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{Units: []types.Unit{{Enable: true, Name: "vmtoolsd.service"}}},
			Storage: types.Storage{Files: []types.File{serviceFromOem("vmtoolsd.service")}},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("VMware", "vmware")}}},
	})
	configs.Register(Config{
		name:  "xendom0",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "interoute",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "qemu",
		fetch: qemu.FetchConfig,
	})
	configs.Register(Config{
		name:  "file",
		fetch: file.FetchConfig,
	})
}

func Get(name string) (config Config, ok bool) {
	config, ok = configs.Get(name).(Config)
	return
}

func MustGet(name string) Config {
	if config, ok := Get(name); ok {
		return config
	} else {
		panic(fmt.Sprintf("invalid OEM name %q provided", name))
	}
}

func Names() (names []string) {
	return configs.Names()
}

func contentsFromString(data string) types.FileContents {
	return types.FileContents{
		Source: (&url.URL{
			Scheme: "data",
			Opaque: "," + dataurl.EscapeString(data),
		}).String(),
	}
}

func contentsFromOem(path string) types.FileContents {
	return types.FileContents{
		Source: (&url.URL{
			Scheme: "oem",
			Path:   path,
		}).String(),
	}
}

func userCloudInit(name string, oem string) types.Unit {
	contents := `[Unit]
Description=Cloudinit from %s metadata

[Service]
Type=oneshot
ExecStart=/usr/bin/coreos-cloudinit --oem=%s

[Install]
WantedBy=multi-user.target
`

	return types.Unit{
		Name:     "oem-cloudinit.service",
		Enable:   true,
		Contents: fmt.Sprintf(contents, name, oem),
	}
}

func serviceFromOem(unit string) types.File {
	return types.File{
		Node: types.Node{
			Filesystem: "root",
			Path:       "/etc/systemd/system/" + unit,
		},
		FileEmbedded1: types.FileEmbedded1{
			Mode:     0444,
			Contents: contentsFromOem("/units/" + unit),
		},
	}
}
<commit_msg>oem: drop etcd.service drop-in<commit_after>// Copyright 2015 CoreOS, Inc.
//
// 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 oem

import (
	"fmt"
	"net/url"

	"github.com/coreos/ignition/config/types"
	"github.com/coreos/ignition/internal/providers"
	"github.com/coreos/ignition/internal/providers/azure"
	"github.com/coreos/ignition/internal/providers/digitalocean"
	"github.com/coreos/ignition/internal/providers/ec2"
	"github.com/coreos/ignition/internal/providers/file"
	"github.com/coreos/ignition/internal/providers/gce"
	"github.com/coreos/ignition/internal/providers/noop"
	"github.com/coreos/ignition/internal/providers/openstack"
	"github.com/coreos/ignition/internal/providers/packet"
	"github.com/coreos/ignition/internal/providers/qemu"
	"github.com/coreos/ignition/internal/providers/vmware"
	"github.com/coreos/ignition/internal/registry"

	"github.com/vincent-petithory/dataurl"
)

// Config represents a set of options that map to a particular OEM.
type Config struct {
	name              string
	fetch             providers.FuncFetchConfig
	baseConfig        types.Config
	defaultUserConfig types.Config
}

func (c Config) Name() string {
	return c.name
}

func (c Config) FetchFunc() providers.FuncFetchConfig {
	return c.fetch
}

func (c Config) BaseConfig() types.Config {
	return c.baseConfig
}

func (c Config) DefaultUserConfig() types.Config {
	return c.defaultUserConfig
}

var configs = registry.Create("oem configs")

func init() {
	configs.Register(Config{
		name:  "azure",
		fetch: azure.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "waagent.service"},
					{Name: "etcd2.service", Dropins: []types.Dropin{
						{Name: "10-oem.conf", Contents: "[Service]\nEnvironment=ETCD_ELECTION_TIMEOUT=1200\n"},
					}},
				},
			},
			Storage: types.Storage{Files: []types.File{serviceFromOem("waagent.service")}},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("Azure", "azure")}}},
	})
	configs.Register(Config{
		name:  "cloudsigma",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "cloudstack",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "digitalocean",
		fetch: digitalocean.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{{Enable: true, Name: "coreos-metadata-sshkeys@.service"}},
			},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("DigitalOcean", "digitalocean")}}},
	})
	configs.Register(Config{
		name:              "brightbox",
		fetch:             openstack.FetchConfig,
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("BrightBox", "ec2-compat")}}},
	})
	configs.Register(Config{
		name:              "openstack",
		fetch:             openstack.FetchConfig,
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("OpenStack", "ec2-compat")}}},
	})
	configs.Register(Config{
		name:  "ec2",
		fetch: ec2.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "coreos-metadata-sshkeys@.service"},
					{Name: "etcd2.service", Dropins: []types.Dropin{
						{Name: "10-oem.conf", Contents: "[Service]\nEnvironment=ETCD_ELECTION_TIMEOUT=1200\n"},
					}},
				},
			},
		},
		defaultUserConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Mask: true, Name: "user-configdrive.service"},
					{Mask: true, Name: "user-configvirtfs.service"},
					userCloudInit("EC2", "ec2-compat"),
				},
			},
		},
	})
	configs.Register(Config{
		name:  "exoscale",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "gce",
		fetch: gce.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "coreos-metadata-sshkeys@.service"},
					{Enable: true, Name: "oem-gce.service"},
				},
			},
			Storage: types.Storage{
				Files: []types.File{
					serviceFromOem("oem-gce.service"),
					{
						Node: types.Node{
							Filesystem: "root",
							Path:       "/etc/hosts",
						},
						FileEmbedded1: types.FileEmbedded1{
							Mode:     0444,
							Contents: contentsFromString("169.254.169.254 metadata\n127.0.0.1 localhost\n"),
						},
					},
					{
						Node: types.Node{
							Filesystem: "root",
							Path:       "/etc/profile.d/google-cloud-sdk.sh",
						},
						FileEmbedded1: types.FileEmbedded1{
							Mode: 0444,
							Contents: contentsFromString(`#!/bin/sh
alias gcloud="(docker images google/cloud-sdk || docker pull google/cloud-sdk) > /dev/null;docker run -t -i --net="host" -v $HOME/.config:/.config -v /var/run/docker.sock:/var/run/doker.sock -v /usr/bin/docker:/usr/bin/docker google/cloud-sdk gcloud"
alias gcutil="(docker images google/cloud-sdk || docker pull google/cloud-sdk) > /dev/null;docker run -t -i --net="host" -v $HOME/.config:/.config google/cloud-sdk gcutil"
alias gsutil="(docker images google/cloud-sdk || docker pull google/cloud-sdk) > /dev/null;docker run -t -i --net="host" -v $HOME/.config:/.config google/cloud-sdk gsutil"
`),
						},
					},
				},
			},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("GCE", "gce")}}},
	})
	configs.Register(Config{
		name:  "hyperv",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "niftycloud",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "packet",
		fetch: packet.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{
				Units: []types.Unit{
					{Enable: true, Name: "coreos-metadata-sshkeys@.service"},
					{Enable: true, Name: "packet-phone-home.service"},
				},
			},
			Storage: types.Storage{Files: []types.File{serviceFromOem("packet-phone-home.service")}},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("Packet", "packet")}}},
	})
	configs.Register(Config{
		name:  "pxe",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "rackspace",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "rackspace-onmetal",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "vagrant",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "vmware",
		fetch: vmware.FetchConfig,
		baseConfig: types.Config{
			Systemd: types.Systemd{Units: []types.Unit{{Enable: true, Name: "vmtoolsd.service"}}},
			Storage: types.Storage{Files: []types.File{serviceFromOem("vmtoolsd.service")}},
		},
		defaultUserConfig: types.Config{Systemd: types.Systemd{Units: []types.Unit{userCloudInit("VMware", "vmware")}}},
	})
	configs.Register(Config{
		name:  "xendom0",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "interoute",
		fetch: noop.FetchConfig,
	})
	configs.Register(Config{
		name:  "qemu",
		fetch: qemu.FetchConfig,
	})
	configs.Register(Config{
		name:  "file",
		fetch: file.FetchConfig,
	})
}

func Get(name string) (config Config, ok bool) {
	config, ok = configs.Get(name).(Config)
	return
}

func MustGet(name string) Config {
	if config, ok := Get(name); ok {
		return config
	} else {
		panic(fmt.Sprintf("invalid OEM name %q provided", name))
	}
}

func Names() (names []string) {
	return configs.Names()
}

func contentsFromString(data string) types.FileContents {
	return types.FileContents{
		Source: (&url.URL{
			Scheme: "data",
			Opaque: "," + dataurl.EscapeString(data),
		}).String(),
	}
}

func contentsFromOem(path string) types.FileContents {
	return types.FileContents{
		Source: (&url.URL{
			Scheme: "oem",
			Path:   path,
		}).String(),
	}
}

func userCloudInit(name string, oem string) types.Unit {
	contents := `[Unit]
Description=Cloudinit from %s metadata

[Service]
Type=oneshot
ExecStart=/usr/bin/coreos-cloudinit --oem=%s

[Install]
WantedBy=multi-user.target
`

	return types.Unit{
		Name:     "oem-cloudinit.service",
		Enable:   true,
		Contents: fmt.Sprintf(contents, name, oem),
	}
}

func serviceFromOem(unit string) types.File {
	return types.File{
		Node: types.Node{
			Filesystem: "root",
			Path:       "/etc/systemd/system/" + unit,
		},
		FileEmbedded1: types.FileEmbedded1{
			Mode:     0444,
			Contents: contentsFromOem("/units/" + unit),
		},
	}
}
<|endoftext|>