Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) adds anka build provider #148

Merged
merged 14 commits into from
Aug 9, 2022
18 changes: 18 additions & 0 deletions command/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ type (
UserDataPath string `json:"user_data_Path,omitempty" yaml:"user_data_Path,omitempty"`
}

// AnkaBuild specifies the configuration for an Anka instance.
AnkaBuild struct {
Account struct {
Username string `json:"username,omitempty" yaml:"username"`
Password string `json:"password,omitempty" yaml:"password"`
}
VMID string `json:"vm_id,omitempty" yaml:"vm_id"`
RootDirectory string `json:"root_directory,omitempty" yaml:"root_directory"`
UserData string `json:"user_data,omitempty" yaml:"user_data"`
UserDataPath string `json:"user_data_Path,omitempty" yaml:"user_data_Path,omitempty"`
RegistryURL string `json:"registry_url,omitempty" yaml:"registry_url"`
NodeID string `json:"node_id,omitempty" yaml:"node_id"`
Tag string `json:"tag,omitempty" yaml:"tag"`
AuthToken string `json:"auth_token,omitempty" yaml:"auth_token"`
}

// Azure specifies the configuration for an Azure instance.
Azure struct {
Account AzureAccount `json:"account,omitempty"`
Expand Down Expand Up @@ -375,6 +391,8 @@ func (s *Instance) UnmarshalJSON(data []byte) error {
s.Spec = new(Amazon)
case string(types.Anka):
s.Spec = new(Anka)
case string(types.AnkaBuild):
s.Spec = new(AnkaBuild)
case string(types.Azure):
s.Spec = new(Azure)
case string(types.Google), "gcp":
Expand Down
2 changes: 1 addition & 1 deletion command/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *daemonCommand) run(*kingpin.ParseContext) error {
Repopulate: true,
}

engInstance, engineErr := engine.New(opts, poolManager, env.Runner.Name, env.Settings.LiteEnginePath)
engInstance, engineErr := engine.New(opts, poolManager, &env)
if engineErr != nil {
logrus.WithError(engineErr).
Fatalln("daemon: cannot load the engine")
Expand Down
6 changes: 1 addition & 5 deletions command/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func (c *execCommand) run(*kingpin.ParseContext) error { //nolint:gocyclo // its
if err != nil {
return err
}

envs := environ.Combine(
c.Environ,
environ.System(c.System),
Expand Down Expand Up @@ -243,10 +242,7 @@ func (c *execCommand) run(*kingpin.ParseContext) error { //nolint:gocyclo // its
logrus.StandardLogger(),
),
)

engineInstance, err := engine.New(engine.Opts{
Repopulate: false,
}, poolManager, runnerName, c.LiteEngineURL)
engineInstance, err := engine.New(engine.Opts{Repopulate: false}, poolManager, &envConfig)
if err != nil {
return err
}
Expand Down
193 changes: 193 additions & 0 deletions command/harness/scripts/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package scripts

const Clone = `
#!/bin/sh
# force the home directory path.

if [ "$HOME" != "/home/drone" ]; then
if [ -d "/home/drone" ]; then
echo "[DEBUG] setting default home directory"
export HOME=/home/drone
fi
fi

# if the netrc enviornment variables exist, write
# the netrc file.

if [[ ! -z "${DRONE_NETRC_MACHINE}" ]]; then
cat <<EOF > ${HOME}/.netrc
machine ${DRONE_NETRC_MACHINE}
login ${DRONE_NETRC_USERNAME}
password ${DRONE_NETRC_PASSWORD}
EOF
fi

# if the ssh_key environment variable exists, write
# the ssh key and add the netrc machine to the
# known hosts file.

if [[ ! -z "${DRONE_SSH_KEY}" ]]; then
mkdir ${HOME}/.ssh
echo "$DRONE_SSH_KEY" > ${HOME}/.ssh/id_rsa
chmod 600 ${HOME}/.ssh/id_rsa

touch ${HOME}/.ssh/known_hosts
chmod 600 ${HOME}/.ssh/known_hosts

SSH_KEYSCAN_FLAGS=""
if [[ ! -z "${DRONE_NETRC_PORT}" ]]; then
SSH_KEYSCAN_FLAGS="-p ${DRONE_NETRC_PORT}"
fi
ssh-keyscan -H ${SSH_KEYSCAN_FLAGS} ${DRONE_NETRC_MACHINE} > /etc/ssh/ssh_known_hosts 2> /dev/null

export GIT_SSH_COMMAND="ssh -i ${HOME}/.ssh/id_rsa ${SSH_KEYSCAN_FLAGS} -F /dev/null"
fi

# AWS codecommit support using AWS access key & secret key
# Refer: https://docs.aws.amazon.com/codecommit/latest/userguide/setting-up-https-unixes.html

if [[ ! -z "$DRONE_AWS_ACCESS_KEY" ]]; then
aws configure set aws_access_key_id $DRONE_AWS_ACCESS_KEY
aws configure set aws_secret_access_key $DRONE_AWS_SECRET_KEY
aws configure set default.region $DRONE_AWS_REGION

git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
fi

# configure git global behavior and parameters via the
# following environment variables:


if [[ -z "${DRONE_COMMIT_AUTHOR_NAME}" ]]; then
export DRONE_COMMIT_AUTHOR_NAME=drone
fi

if [[ -z "${DRONE_COMMIT_AUTHOR_EMAIL}" ]]; then
export DRONE_COMMIT_AUTHOR_EMAIL=drone@localhost
fi

export GIT_AUTHOR_NAME=${DRONE_COMMIT_AUTHOR_NAME}
export GIT_AUTHOR_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL}
export GIT_COMMITTER_NAME=${DRONE_COMMIT_AUTHOR_NAME}
export GIT_COMMITTER_EMAIL=${DRONE_COMMIT_AUTHOR_EMAIL}

# invoke the sub-script based on the drone event type.
# TODO we should ultimately look at the ref, since
# we need something compatible with deployment events.

CLONE_TYPE=$DRONE_BUILD_EVENT
case $DRONE_COMMIT_REF in
refs/tags/* ) CLONE_TYPE=tag ;;
refs/pull/* ) CLONE_TYPE=pull_request ;;
refs/pull-request/* ) CLONE_TYPE=pull_request ;;
refs/merge-requests/* ) CLONE_TYPE=pull_request ;;
esac

case $CLONE_TYPE in
pull_request)
FLAGS=""
if [[ ! -z "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi

if [ ! -d .git ]; then
set -x
git init
git remote add origin ${DRONE_REMOTE_URL}
set +x
fi

# If PR clone strategy is cloning only the source branch
if [ "$PLUGIN_PR_CLONE_STRATEGY" == "SourceBranch" ]; then
set -e
set -x

git fetch ${FLAGS} origin ${DRONE_COMMIT_REF}:
git checkout ${DRONE_COMMIT_SHA} -b ${DRONE_SOURCE_BRANCH}
exit 0
fi

# PR clone strategy is merge commit

targetRef=${DRONE_COMMIT_BRANCH}
if [[ ! -z "${DRONE_COMMIT_BEFORE}" ]]; then
targetRef="${DRONE_COMMIT_BEFORE} -b ${DRONE_COMMIT_BRANCH}"
fi


set -e
set -x

git fetch ${FLAGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}:
git checkout ${targetRef}

git fetch origin ${DRONE_COMMIT_REF}:
git merge ${DRONE_COMMIT_SHA}
;;
tag)
FLAGS=""
if [[ ! -z "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi

if [ ! -d .git ]; then
set -x
git init
git remote add origin ${DRONE_REMOTE_URL}
set +x
fi

set -e
set -x

git fetch ${FLAGS} origin +refs/tags/${DRONE_TAG}:
git checkout -qf FETCH_HEAD
;;
*)
FLAGS=""
if [[ ! -z "${PLUGIN_DEPTH}" ]]; then
FLAGS="--depth=${PLUGIN_DEPTH}"
fi

if [ ! -d .git ]; then
set -x
git init
git remote add origin ${DRONE_REMOTE_URL}
set +x
fi

# the branch may be empty for certain event types,
# such as github deployment events. If the branch
# is empty we checkout the sha directly. Note that
# we intentially omit depth flags to avoid failed
# clones due to lack of history.
if [[ -z "${DRONE_COMMIT_BRANCH}" ]]; then
set -e
set -x
git fetch origin
git checkout -qf ${DRONE_COMMIT_SHA}
exit 0
fi

# the commit sha may be empty for builds that are
# manually triggered in Harness CI Enterprise. If
# the commit is empty we clone the branch.
if [[ -z "${DRONE_COMMIT_SHA}" ]]; then
set -e
set -x
git fetch ${FLAGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}:
git checkout -b ${DRONE_COMMIT_BRANCH} origin/${DRONE_COMMIT_BRANCH}
exit 0
fi

set -e
set -x

git fetch ${FLAGS} origin +refs/heads/${DRONE_COMMIT_BRANCH}:
git checkout ${DRONE_COMMIT_SHA} -b ${DRONE_COMMIT_BRANCH}
;;
esac


`
10 changes: 9 additions & 1 deletion command/harness/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"time"

"github.com/drone-runners/drone-runner-aws/internal/oshelp"

"github.com/drone-runners/drone-runner-aws/command/config"
"github.com/drone-runners/drone-runner-aws/engine/resource"
"github.com/drone-runners/drone-runner-aws/internal/drivers"
Expand Down Expand Up @@ -134,7 +136,7 @@ func HandleSetup(ctx context.Context, r *SetupVMRequest, s store.StageOwnerStore
return nil, fmt.Errorf("failed to tag: %w", err)
}

client, err := lehelper.GetClient(instance, env.Runner.Name)
client, err := lehelper.GetClient(instance, env.Runner.Name, instance.Port)
if err != nil {
go cleanUpFn()
return nil, fmt.Errorf("failed to create LE client: %w", err)
Expand All @@ -149,6 +151,12 @@ func HandleSetup(ctx context.Context, r *SetupVMRequest, s store.StageOwnerStore

logr.Traceln("retry health check complete")

// Currently m1 architecture does not enable nested virtualisation, so we disable docker.
if instance.Platform.OS == oshelp.OSMac {
b := false
r.SetupRequest.MountDockerSocket = &b
}

setupResponse, err := client.Setup(ctx, &r.SetupRequest)
if err != nil {
go cleanUpFn()
Expand Down
47 changes: 46 additions & 1 deletion command/harness/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import (
"fmt"
"time"

"github.com/drone-runners/drone-runner-aws/command/harness/scripts"

"github.com/drone-runners/drone-runner-aws/command/config"
"github.com/drone-runners/drone-runner-aws/engine/resource"
"github.com/drone-runners/drone-runner-aws/internal/drivers"
"github.com/drone-runners/drone-runner-aws/internal/lehelper"
"github.com/drone-runners/drone-runner-aws/internal/oshelp"
errors "github.com/drone-runners/drone-runner-aws/internal/types"
"github.com/harness/lite-engine/api"
lespec "github.com/harness/lite-engine/engine/spec"

"github.com/dchest/uniuri"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -65,13 +70,40 @@ func HandleStep(ctx context.Context, r *ExecuteVMRequest, env *config.EnvConfig,

logr = logr.WithField("ip", inst.Address)

client, err := lehelper.GetClient(inst, env.Runner.Name)
client, err := lehelper.GetClient(inst, env.Runner.Name, inst.Port)
if err != nil {
return nil, fmt.Errorf("failed to create client: %w", err)
}

logr.Traceln("running StartStep")

// Currently the OSX m1 architecture does not enable nested virtualization, so we disable docker.
if inst.Platform.OS == oshelp.OSMac {
b := false
r.StartStepRequest.MountDockerSocket = &b
if r.StartStepRequest.Image == "harness/drone-git:1.2.0" {
eoinmcafee00 marked this conversation as resolved.
Show resolved Hide resolved
r.StartStepRequest.Image = ""
r.Volumes = nil
pipelinePlatform, _ := poolManager.Inspect(inst.Pool)

cloneScript := scripts.Clone
clonePath := fmt.Sprintf("%s/clone.sh", r.StartStepRequest.WorkingDir)

entrypoint := getEntrypoint(pipelinePlatform.OS)
command := []string{clonePath}
r.StartStepRequest.ID = random()
r.StartStepRequest.Name = "clone"
r.StartStepRequest.Run.Entrypoint = entrypoint
r.StartStepRequest.Run.Command = command
r.StartStepRequest.Files = []*lespec.File{
{
Path: clonePath,
Mode: 0700,
Data: cloneScript,
},
}
}
}
startStepResponse, err := client.StartStep(ctx, &r.StartStepRequest)
if err != nil {
return nil, fmt.Errorf("failed to call LE.StartStep: %w", err)
Expand All @@ -88,3 +120,16 @@ func HandleStep(ctx context.Context, r *ExecuteVMRequest, env *config.EnvConfig,

return pollResponse, nil
}

func getEntrypoint(pipelineOS string) []string {
if pipelineOS == oshelp.OSWindows {
return []string{"powershell"}
}

return []string{"sh", "-c"}
}

// random generator function
var random = func() string {
return "drone-" + uniuri.NewLen(20) //nolint:gomnd
}
2 changes: 1 addition & 1 deletion command/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (c *setupCommand) run(*kingpin.ParseContext) error {
Fatalln("setup: unable to start instance")
}
// create an LE client so we can test the instance
leClient, leErr := lehelper.GetClient(instance, runnerName)
leClient, leErr := lehelper.GetClient(instance, runnerName, instance.Port)
eoinmcafee00 marked this conversation as resolved.
Show resolved Hide resolved
if leErr != nil {
cleanErr := poolManager.Destroy(ctx, testPoolName, instance.ID)
consoleLogs, consoleErr := poolManager.InstanceLogs(ctx, testPoolName, instance.ID)
Expand Down
Loading