From 946297388fc1aabb9d69dd6144d8f0414cd6a5e5 Mon Sep 17 00:00:00 2001 From: Jeremiah Wuenschel Date: Fri, 22 Jul 2016 13:31:38 -0700 Subject: [PATCH] Creating initial worker build --- .gitignore | 3 +++ CONTRIBUTING.md | 32 ++++++++++++++++++++++++++++++++ LICENSE | 24 ++++++++++++++++++++++++ README.md | 17 +++++++++++++++-- launch.go | 41 +++++++++++++++++++++++++++++++++++++++++ launch_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ wercker.yml | 11 +++++++++++ 7 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 launch.go create mode 100644 launch_test.go create mode 100644 wercker.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a101a01b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +artifacts/ +.DS_STORE +.*.swp diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..b2266342 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,32 @@ +# Contributing + +Thank you for considering contributing! There are many ways you can help. + +## Issues + +File an issue if you think you've found a bug. Be sure to describe + +1. How can it be reproduced? +2. What did you expect? +3. What actually occurred? +4. Version, platform, etc. if possibly relevant. + +## Docs + +Documentation, READMEs, and examples are extremely important. Please help improve them and if you find a typo or notice a problem, please send a fix or say something. + +## Submitting Patches + +Patches for fixes, features, and improvements are accepted through pull requests. + +* Write good commit messages, in the present tense! (Add X, not Added X). Short title, blank line, bullet points if needed. Capitalize the first letter of the title or bullet item. No punctuation in the title. +* Code must pass lint and style checks. +* All external methods must be documented. +* Include tests to improve coverage and prevent regressions. +* Squash changes into a single commit per feature/fix. Ask if you're unsure how to discretize your work. + +Please ask before embarking on a large improvement so you're not disappointed if it does not align with the goals of the project or owner(s). + +## Feature Requests + +Make the case for a feature via an issue with a good title. The feature should be discussed and given a target inclusion milestone or closed. diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..627108f7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +Copyright 2016 Yahoo Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the Yahoo! Inc. nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL YAHOO! INC. BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index 147e4022..3cb7dd61 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ -# launcher -The entrypoint for job launching in Screwdriver. +# Screwdriver Job Launcher +[![Build Status][wercker-image]][wercker-url] [![Open Issues][issues-image]][issues-url] + +> The entrypoint for job launching in Screwdriver. + +## Usage + +## License + +Code licensed under the BSD 3-Clause license. See LICENSE file for terms. + +[issues-image]: https://img.shields.io/github/issues/screwdriver-cd/launcher.svg +[issues-url]: https://github.com/screwdriver-cd/launcher/issues +[wercker-image]: https://app.wercker.com/status/822503b7af879d54018006aeafb317ae +[wercker-url]: https://app.wercker.com/project/bykey/822503b7af879d54018006aeafb317ae diff --git a/launch.go b/launch.go new file mode 100644 index 00000000..759e47e5 --- /dev/null +++ b/launch.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + "os/exec" +) + +type shellOutInterface interface { + run(string, ...string) ([]byte, error) +} + +var shellOut shellOutInterface + +type shellOutImpl struct{} + +func init() { + shellOut = shellOutImpl{} +} + +func (s shellOutImpl) run(cmd string, args ...string) (out []byte, err error) { + return exec.Command(cmd, args...).CombinedOutput() +} + +func runLauncher(args ...string) (out []byte, err error) { + cmdName := "/opt/screwdriver/launch.sh" + return shellOut.run(cmdName, args...) +} + +func main() { + var out []byte + var err error + + fmt.Println("Launching the launcher...") + if out, err = runLauncher(os.Args[1:]...); err != nil { + fmt.Fprintln(os.Stderr, "There was an error shelling out to the launcher: ", err) + os.Exit(1) + } + fmt.Println(string(out)) + fmt.Println("Launched successfully.") +} diff --git a/launch_test.go b/launch_test.go new file mode 100644 index 00000000..20e5a541 --- /dev/null +++ b/launch_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "strings" + "testing" +) + +var shellOutSetup map[string]string + +type shellOutMock struct{} + +func (s shellOutMock) run(cmd string, args ...string) (out []byte, err error) { + cmdslice := []string{cmd} + cmdslice = append(cmdslice, args...) + + cmd = strings.Join(cmdslice, " ") + output := shellOutSetup[cmd] + if output == "" { + return nil, fmt.Errorf("Error: %s not mocked", cmd) + } + + return []byte(output), nil +} + +func Test(t *testing.T) { + shellOut = shellOutMock{} + + shellOutSetup = map[string]string{ + "/opt/screwdriver/launch.sh abc": "abc", + } + + output, err := runLauncher("abc") + if string(output) != shellOutSetup["/opt/screwdriver/launch.sh abc"] { + errmsg := fmt.Sprintf("Expected %s, got %s", shellOutSetup["echo"], output) + t.Error(errmsg) + } + if err != nil { + errmsg := fmt.Sprintf("Error running command: %s", err) + t.Error(errmsg) + } +} diff --git a/wercker.yml b/wercker.yml new file mode 100644 index 00000000..7ed9c717 --- /dev/null +++ b/wercker.yml @@ -0,0 +1,11 @@ +box: google/golang + +build: + steps: + - script: + name: go test + code: go test ./... + + - script: + name: go build + code: go build launch.go