Skip to content

Commit

Permalink
Merge pull request #126 from whywaita/feat/support-not-linux
Browse files Browse the repository at this point in the history
Support not linux platform
  • Loading branch information
whywaita authored Mar 15, 2022
2 parents 9c56ef8 + bc36cda commit e864147
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
26 changes: 19 additions & 7 deletions pkg/starter/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func getPatchedFiles() (string, error) {
return runnerService, nil
}

func (s *Starter) getSetupScript(ctx context.Context, target datastore.Target) (string, error) {
rawScript, err := s.getSetupRawScript(ctx, target)
func (s *Starter) getSetupScript(ctx context.Context, target datastore.Target, runnerName string) (string, error) {
rawScript, err := s.getSetupRawScript(ctx, target, runnerName)
if err != nil {
return "", fmt.Errorf("failed to get raw setup scripts: %w", err)
}
Expand All @@ -42,7 +42,7 @@ func (s *Starter) getSetupScript(ctx context.Context, target datastore.Target) (
return fmt.Sprintf(templateCompressedScript, encoded), nil
}

func (s *Starter) getSetupRawScript(ctx context.Context, target datastore.Target) (string, error) {
func (s *Starter) getSetupRawScript(ctx context.Context, target datastore.Target, runnerName string) (string, error) {
runnerUser := ""
if target.RunnerUser.Valid {
runnerUser = target.RunnerUser.String
Expand Down Expand Up @@ -70,6 +70,7 @@ func (s *Starter) getSetupRawScript(ctx context.Context, target datastore.Target
Scope: target.Scope,
GHEDomain: target.GHEDomain.String,
RunnerRegistrationToken: token,
RunnerName: runnerName,
RunnerUser: runnerUser,
RunnerVersion: runnerVersion,
RunnerServiceJS: runnerServiceJs,
Expand Down Expand Up @@ -104,6 +105,7 @@ type templateCreateLatestRunnerOnceValue struct {
Scope string
GHEDomain string
RunnerRegistrationToken string
RunnerName string
RunnerUser string
RunnerVersion string
RunnerServiceJS string
Expand All @@ -119,7 +121,7 @@ set -e
runner_scope={{.Scope}}
ghe_hostname={{.GHEDomain}}
runner_name=${3:-$(hostname)}
runner_name={{.RunnerName}}
RUNNER_TOKEN={{.RunnerRegistrationToken}}
RUNNER_USER={{.RunnerUser}}
RUNNER_VERSION={{.RunnerVersion}}
Expand All @@ -130,9 +132,6 @@ if [ $(id -u) -eq 0 ]; then # if root
sudo_prefix="sudo -E -u ${RUNNER_USER} "
fi
export HOME="/home/${RUNNER_USER}"
export AGENT_TOOLSDIRECTORY="/opt/hostedtoolcache"
echo "Configuring runner @ ${runner_scope}"
#---------------------------------------
Expand All @@ -147,6 +146,14 @@ function fatal()
exit 1
}
function configure_environment()
{
export HOME="/home/${RUNNER_USER}"
if [ "${runner_plat}" = "osx" ]; then
export HOME="/Users/${RUNNER_USER}"
fi
}
function install_jq()
{
echo "jq is not installed, will be install jq."
Expand All @@ -165,6 +172,10 @@ function install_docker()
sudo apt-get update -y -qq
sudo apt-get install -y docker.io
fi
if [ "${runner_plat}" = "osx"]; then
brew install docker
fi
}
function download_runner()
Expand Down Expand Up @@ -205,6 +216,7 @@ which jq || fatal "jq required. Please install in PATH with apt-get, brew, etc"
which docker || install_docker
which docker || fatal "docker required. Please install in PATH with apt-get, brew, etc"
configure_environment
cd ${RUNNER_BASE_DIRECTORY}
${sudo_prefix}mkdir -p runner
Expand Down
14 changes: 8 additions & 6 deletions pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ func (s *Starter) processJob(ctx context.Context, job datastore.Job) error {
return fmt.Errorf("failed to bung (target ID: %s, job ID: %s): %w", job.TargetID, job.UUID, err)
}

runnerName := runner.ToName(job.UUID.String())
if config.Config.Strict {
if err := s.checkRegisteredRunner(ctx, cloudID, *target); err != nil {
if err := s.checkRegisteredRunner(ctx, runnerName, *target); err != nil {
logger.Logf(false, "failed to check to register runner (target ID: %s, job ID: %s): %+v\n", job.TargetID, job.UUID, err)

if err := deleteInstance(ctx, cloudID); err != nil {
Expand Down Expand Up @@ -225,7 +226,9 @@ func (s *Starter) processJob(ctx context.Context, job datastore.Job) error {
// bung is start runner, like a pistol! :)
func (s *Starter) bung(ctx context.Context, jobUUID uuid.UUID, target datastore.Target) (string, string, string, error) {
logger.Logf(false, "start create instance (job: %s)", jobUUID)
script, err := s.getSetupScript(ctx, target)
runnerName := runner.ToName(jobUUID.String())

script, err := s.getSetupScript(ctx, target, runnerName)
if err != nil {
return "", "", "", fmt.Errorf("failed to get setup scripts: %w", err)
}
Expand All @@ -236,7 +239,6 @@ func (s *Starter) bung(ctx context.Context, jobUUID uuid.UUID, target datastore.
}
defer teardown()

runnerName := runner.ToName(jobUUID.String())
cloudID, ipAddress, shoesType, err := client.AddInstance(ctx, runnerName, script, target.ResourceType)
if err != nil {
return "", "", "", fmt.Errorf("failed to add instance: %w", err)
Expand All @@ -263,7 +265,7 @@ func deleteInstance(ctx context.Context, cloudID string) error {
}

// checkRegisteredRunner check to register runner to GitHub
func (s *Starter) checkRegisteredRunner(ctx context.Context, cloudID string, target datastore.Target) error {
func (s *Starter) checkRegisteredRunner(ctx context.Context, runnerName string, target datastore.Target) error {
client, err := gh.NewClient(target.GitHubToken, target.GHEDomain.String)
if err != nil {
return fmt.Errorf("failed to create github client: %w", err)
Expand All @@ -283,15 +285,15 @@ func (s *Starter) checkRegisteredRunner(ctx context.Context, cloudID string, tar
// timeout
return fmt.Errorf("faied to to check existing runner in GitHub: timeout in %s", runner.MustRunningTime)
case <-ticker.C:
if _, err := gh.ExistGitHubRunner(cctx, client, owner, repo, cloudID); err == nil {
if _, err := gh.ExistGitHubRunner(cctx, client, owner, repo, runnerName); err == nil {
// success to register runner to GitHub
return nil
} else if !errors.Is(err, gh.ErrNotFound) {
// not retryable error
return fmt.Errorf("failed to check existing runner in GitHub: %w", err)
}
count++
logger.Logf(true, "%s is not found in GitHub, will retry... (second: %ds)", cloudID, count)
logger.Logf(true, "%s is not found in GitHub, will retry... (second: %ds)", runnerName, count)
}
}
}

0 comments on commit e864147

Please sign in to comment.