Skip to content

Commit

Permalink
Merge pull request #17 from aiyengar2/update_wins_version
Browse files Browse the repository at this point in the history
Update Go version, remove vendor, and misc. upgrades
  • Loading branch information
aiyengar2 authored Feb 24, 2021
2 parents 7c2d552 + 5471227 commit 2cb8b6f
Show file tree
Hide file tree
Showing 1,221 changed files with 1,198 additions and 598,125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# IDE
.idea/
.vscode/

# CMake
cmake-build-debug/
Expand Down
46 changes: 9 additions & 37 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM golang:1.12-windowsservercore
FROM golang:1.15-windowsservercore
SHELL ["powershell", "-NoLogo", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ARG DAPPER_HOST_ARCH
ENV HOST_ARCH=${DAPPER_HOST_ARCH} ARCH=${DAPPER_HOST_ARCH}

# install docker
RUN pushd c:\; \
$URL = 'https://github.com/StefanScherer/docker-cli-builder/releases/download/18.09.6/docker.exe'; \
$URL = 'https://github.com/StefanScherer/docker-cli-builder/releases/download/19.03.14/docker.exe'; \
\
Write-Host ('Downloading docker from {0} ...' -f $URL); \
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
Expand All @@ -17,7 +17,7 @@ RUN pushd c:\; \

# upgrade git
RUN pushd c:\; \
$URL = 'https://github.com/git-for-windows/git/releases/download/v2.21.0.windows.1/MinGit-2.21.0-64-bit.zip'; \
$URL = 'https://github.com/git-for-windows/git/releases/download/v2.30.1.windows.1/MinGit-2.30.1-64-bit.zip'; \
\
Write-Host ('Downloading git from {0} ...' -f $URL); \
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
Expand All @@ -34,7 +34,7 @@ RUN pushd c:\; \

# install golangci-lint
RUN pushd c:\; \
$URL = 'https://github.com/golangci/golangci-lint/releases/download/v1.17.1/golangci-lint-1.17.1-windows-amd64.zip'; \
$URL = 'https://github.com/golangci/golangci-lint/releases/download/v1.36.0/golangci-lint-1.36.0-windows-amd64.zip'; \
\
Write-Host ('Downloading golangci from {0} ...' -f $URL); \
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
Expand All @@ -47,7 +47,7 @@ RUN pushd c:\; \
Remove-Item -Force -Recurse -Path c:\golangci-lint.zip; \
\
Write-Host 'Updating PATH ...'; \
[Environment]::SetEnvironmentVariable('PATH', ('c:\golangci-lint-1.17.1-windows-amd64\;{0}' -f $env:PATH), [EnvironmentVariableTarget]::Machine); \
[Environment]::SetEnvironmentVariable('PATH', ('c:\golangci-lint-1.36.0-windows-amd64\;{0}' -f $env:PATH), [EnvironmentVariableTarget]::Machine); \
\
Write-Host 'Complete.'; \
popd;
Expand All @@ -63,6 +63,8 @@ RUN pushd c:\; \
popd;

# install gcc via cygwin: https://cygwin.com/faq/faq.html#faq.setup.cli
# step after cleanup is workaround for docker found in https://github.com/moby/moby/issues/41058#issuecomment-721156197
# If https://mirror.clarkson.edu/cygwin/ is broken, you can replace it with any other mirror from https://cygwin.com/mirrors.html
RUN pushd c:\; \
$URL = 'https://cygwin.com/setup-x86_64.exe'; \
\
Expand All @@ -73,7 +75,7 @@ RUN pushd c:\; \
\
Write-Host 'Installing gcc ...'; \
$PACKAGES = 'mingw64-x86_64-gcc-core,mingw64-x86_64-gcc-g++'; \
Start-Process -NoNewWindow -Wait -FilePath 'c:\cygwin64\setup.exe' -ArgumentList ('-q -d -X -s {0} -D -L -R {1} -l {2} -P {3}' -f 'http://cygwin.mirrors.pair.com/', 'C:/cygwin64', $env:TEMP, $PACKAGES); \
Start-Process -NoNewWindow -Wait -FilePath 'c:\cygwin64\setup.exe' -ArgumentList ('-q -d -X -s {0} -D -L -R {1} -l {2} -P {3}' -f 'https://mirror.clarkson.edu/cygwin/', 'C:/cygwin64', $env:TEMP, $PACKAGES); \
\
Write-Host 'Cleaning ...'; \
Remove-Item -Force -Recurse -Path $env:TEMP/*; \
Expand All @@ -82,39 +84,9 @@ RUN pushd c:\; \
[Environment]::SetEnvironmentVariable('PATH', ('c:\cygwin64\bin\;c:\cygwin64\sbin\;{0}' -f $env:PATH), [EnvironmentVariableTarget]::Machine); \
\
Write-Host 'Complete.'; \
popd;

# patch windows signal support: https://github.com/golang/go/pull/33311
RUN pushd c:\; \
$URL = 'https://nchc.dl.sourceforge.net/project/gnuwin32/patch/2.5.9-7/patch-2.5.9-7-bin.zip'; \
\
Write-Host ('Downloading patch from {0} ...' -f $URL); \
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
Invoke-WebRequest -UseBasicParsing -OutFile c:\patch.zip -Uri $URL; \
\
Write-Host 'Expanding ...'; \
Expand-Archive -Path c:\patch.zip -DestinationPath c:\patch; \
\
Write-Host 'Cleaning ...'; \
Remove-Item -Force -Recurse -Path c:\patch.zip; \
\
Write-Host 'Updating PATH ...'; \
[Environment]::SetEnvironmentVariable('PATH', ('c:\patch\bin\;{0}' -f $env:PATH), [EnvironmentVariableTarget]::Machine); \
Get-ChildItem -Path C:\cygwin64 -Recurse -Attributes ReparsePoint | Remove-Item; \
\
Write-Host 'Complete.'; \
popd;
RUN pushd c:\go; \
$URL = 'https://github.com/tianon/go/commit/9e05d631b542393f5ebb0eb3747157c8bd0de635.diff'; \
\
Write-Host ('Patching https://github.com/golang/go/pull/33311 ...'); \
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
Invoke-WebRequest -UseBasicParsing -OutFile c:\go\windows-signal.diff -Uri $URL; \
patch.exe --verbose -p1 -i windows-signal.diff; \
\
Write-Host 'Cleaning ...'; \
Remove-Item -Force -Path c:\go\windows-signal.diff; \
\
Write-Host 'Complete.'; \
popd;

ENV DAPPER_ENV REPO TAG DRONE_TAG
Expand Down
16 changes: 13 additions & 3 deletions cmd/client/process/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,23 @@ func _runRequestParser(cliCtx *cli.Context) (err error) {
}
var exposes []*types.ProcessExpose
if exposesList := flags.GetListValue(cliCtx, "exposes"); !exposesList.IsEmpty() {
exposes, err = parseExposes(exposesList.Get())
exposesListValue, err := exposesList.Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --exposes")
}
exposes, err = parseExposes(exposesListValue)
if err != nil {
return errors.Wrapf(err, "failed to parse --exposes %s", exposes)
}
}
args := flags.GetListValue(cliCtx, "args").Get()
envs := flags.GetListValue(cliCtx, "envs").Get()
args, err := flags.GetListValue(cliCtx, "args").Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --args")
}
envs, err := flags.GetListValue(cliCtx, "envs").Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --envs")
}
dir := cliCtx.String("dir")
if dir == "" {
dir = filepath.Dir(path)
Expand Down
6 changes: 5 additions & 1 deletion cmd/client/route/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ func _addRequestParser(cliCtx *cli.Context) error {
}

// parse
var err error
_addRequest = &types.RouteAddRequest{}
_addRequest.Addresses = addressList.Get()
_addRequest.Addresses, err = addressList.Get()
if err != nil {
return errors.Wrapf(err, "failed to parse --addresses")
}
for idx, address := range _addRequest.Addresses {
if !strings.Contains(address, "/") {
_addRequest.Addresses[idx] = fmt.Sprintf("%s/32", address)
Expand Down
40 changes: 33 additions & 7 deletions cmd/cmds/flags/list_value.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package flags

import (
"sort"
"strings"
"fmt"

"github.com/urfave/cli"
)
Expand All @@ -23,13 +22,40 @@ func (f *listValue) String() string {
return string(*f)
}

func (f *listValue) Get() []string {
func (f *listValue) Get() ([]string, error) {
if f == nil || f.IsEmpty() {
return nil
return nil, nil
}
ret := strings.Split(f.String(), valueSeparator)
sort.Strings(ret)
return ret

// Split the string by spaces, but respect escaped quotes
var ret []string
inEscapedDoubleQuotes := false
inEscapedSingleQuotes := false
currVal := ""
for _, c := range f.String() {
if string(c) == "\"" && !inEscapedSingleQuotes {
// toggle if not in single quotes
inEscapedDoubleQuotes = !inEscapedDoubleQuotes
}
if string(c) == "'" && !inEscapedDoubleQuotes {
// toggle if not in single quotes
inEscapedSingleQuotes = !inEscapedSingleQuotes
}
if string(c) == " " && !inEscapedDoubleQuotes && !inEscapedSingleQuotes {
// found an item to add the the list of args
ret = append(ret, currVal)
currVal = ""
continue // do not add the space to the entry
}
currVal += string(c)
}
// Check if input is malformed for the last entry
if inEscapedDoubleQuotes || inEscapedSingleQuotes {
return nil, fmt.Errorf("malformed listValue contains an unpaired escaped quote")
}
// Add the final field
ret = append(ret, currVal)
return ret, nil
}

func (f *listValue) IsEmpty() bool {
Expand Down
86 changes: 86 additions & 0 deletions cmd/cmds/flags/list_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package flags

import (
"fmt"
"testing"
)

func TestNormal(t *testing.T) {
val := listValue("RANCHER=hello WINS=world")
valList, err := val.Get()
if err != nil {
t.Fatal(err)
}
// Check outputted values
expectedList := []string{"RANCHER=hello", "WINS=world"}
err = fmt.Errorf("Failed to parse list value: expected %v, got %v", expectedList, valList)
for i, expected := range expectedList {
if valList[i] != expected {
t.Fatal(err)
}
}
}

func TestEscapedDoubleQuotes(t *testing.T) {
val := listValue("NICK=bye LUTHER=\"hello\" RANCHER=\"hello world\" WINS=world")
valList, err := val.Get()
if err != nil {
t.Fatal(err)
}
// Check outputted values
expectedList := []string{"NICK=bye", "LUTHER=\"hello\"", "RANCHER=\"hello world\"", "WINS=world"}
err = fmt.Errorf("Failed to parse list value: expected %v, got %v", expectedList, valList)
for i, expected := range expectedList {
if valList[i] != expected {
t.Fatal(err)
}
}
}

func TestEscapedSingleQuotes(t *testing.T) {
val := listValue("NICK=bye LUTHER='hello' RANCHER='hello world' WINS=world")
valList, err := val.Get()
if err != nil {
t.Fatal(err)
}
// Check outputted values
expectedList := []string{"NICK=bye", "LUTHER='hello'", "RANCHER='hello world'", "WINS=world"}
err = fmt.Errorf("Failed to parse list value: expected %v, got %v", expectedList, valList)
for i, expected := range expectedList {
if valList[i] != expected {
t.Fatal(err)
}
}
}

func TestEscapedSingleAndDoubleQuotes(t *testing.T) {
val := listValue("NICK=bye LUTHER=\"hello\" RANCHER='hello world' WINS=world")
valList, err := val.Get()
if err != nil {
t.Fatal(err)
}
// Check outputted values
expectedList := []string{"NICK=bye", "LUTHER=\"hello\"", "RANCHER='hello world'", "WINS=world"}
err = fmt.Errorf("Failed to parse list value: expected %v, got %v", expectedList, valList)
for i, expected := range expectedList {
if valList[i] != expected {
t.Fatal(err)
}
}
}

func TestEscapedQuotesInQuotes(t *testing.T) {
val := listValue("NICK=bye LUTHER=\"'hello'\" RANCHER='\"hello world\"' WINS=world")
valList, err := val.Get()
if err != nil {
t.Fatal(err)
}
// Check outputted values
expectedList := []string{"NICK=bye", "LUTHER=\"'hello'\"", "RANCHER='\"hello world\"'", "WINS=world"}
err = fmt.Errorf("Failed to parse list value: expected %v, got %v", expectedList, valList)
for i, expected := range expectedList {
if valList[i] != expected {
t.Fatal(err)
}
}
}
29 changes: 23 additions & 6 deletions cmd/server/app/run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,28 @@ func registerService() error {

serviceConfigFailureActions = 2
)
// serviceFailureActions correspond to https://docs.microsoft.com/en-us/windows/win32/api/winsvc/ns-winsvc-service_failure_actionsa
type serviceFailureActions struct {
ResetPeriod uint32
RebootMsg *uint16
Command *uint16
ActionsCount uint32
Actions uintptr
}
// scAction corresponds to https://docs.microsoft.com/en-us/windows/win32/api/winsvc/ns-winsvc-sc_action
type scAction struct {
Type uint32
Delay uint32
}
// Defines that wins should try to restart the service after 5s, 10s, and 15s. If it still fails, wins does not try to restart the service anymore
t := []scAction{
{Type: scActionRestart, Delay: uint32(5 * time.Second / time.Millisecond)},
{Type: scActionRestart, Delay: uint32(10 * time.Second / time.Millisecond)},
{Type: scActionRestart, Delay: uint32(15 * time.Second / time.Millisecond)},
{Type: scActionNone,},
{Type: scActionNone},
}
lpInfo := serviceFailureActions{ResetPeriod: uint32(5 * time.Minute / time.Second), ActionsCount: uint32(len(t)), Actions: uintptr(unsafe.Pointer(&t[0]))}
// Arguments provided adhere to https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-changeserviceconfig2w
err = windows.ChangeServiceConfig2(w.Handle, serviceConfigFailureActions, (*byte)(unsafe.Pointer(&lpInfo)))
if err != nil {
return errors.Wrap(err, "could not add failure action")
Expand Down Expand Up @@ -162,13 +166,18 @@ func unregisterService() error {
}

func runService(ctx context.Context, server *apis.Server) error {
// process windows service
// If the process is not currently executing as a Windows service, assume that this is an interactive session.
// debug.Run runs the binary that the service points to directly on the user's console and reacts to user actions
// e.g. clicking on Ctrl-C
run := debug.Run
isInteractive, err := svc.IsAnInteractiveSession()
isWindowsService, err := svc.IsWindowsService()
if err != nil {
return err
}
if !isInteractive {
if isWindowsService {
// If we can detect that this is a Windows service that is already running, execute it as a Service instead
// after configuring logrus to print logs to Event Tracing for Windows (ETW) and the service's Event Log

run = svc.Run

logrus.SetOutput(ioutil.Discard)
Expand All @@ -185,7 +194,10 @@ func runService(ctx context.Context, server *apis.Server) error {
logrus.AddHook(hook)
}

// Stack dump
// Creates a Win32 event defined on a Global scope at stackdump-{pid} that can be signaled by
// built-in adminstrators of the Windows machine or by the local system.
// If this Win32 event (Global//stackdump-{pid}) is signaled, a goroutine launched by this call
// will dump the current stack trace into {windowsTemporaryDirectory}/{default.WindowsServiceName}.{pid}.stack.logs
profilings.SetupDumpStacks(defaults.WindowsServiceName, os.Getpid())
}

Expand Down Expand Up @@ -228,14 +240,19 @@ func (h *serviceHandler) Execute(_ []string, r <-chan svc.ChangeRequest, s chan<
h.errC <- h.srv.Serve(ctx)
}()

s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.Accepted(windows.SERVICE_ACCEPT_PARAMCHANGE)}
s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.AcceptParamChange}

// The following loop configures the Windows service to respond to ChangeRequests from the Windows Service Manager
// It uses Go Labels to break out of the switch statement and the loop on recieving a Stop ChangeRequest
// See https://medium.com/golangspec/labels-in-go-4ffd81932339
Loop:
for c := range r {
switch c.Cmd {
case svc.Interrogate:
// Update the service's current status to the one provided by the ChangeRequest
s <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
// Cleanup before finishing execution
s <- svc.Status{State: svc.StopPending, Accepts: 0}
// stop wins server
h.srv.Close()
Expand Down
Loading

0 comments on commit 2cb8b6f

Please sign in to comment.