Skip to content

Commit

Permalink
Support kubernetes (actions#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalind1 authored Oct 18, 2021
1 parent 4487e89 commit 06c0a6b
Show file tree
Hide file tree
Showing 13 changed files with 706 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ _downloads

# Source
scripts
src
src/Runner.*
docs
24 changes: 15 additions & 9 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ RUN wget -O /tmp/IntelSHA2RootChain-Base64.zip --no-check-certificate "https://c
&& rm /tmp/IntelSHA2RootChain-Base64.zip \
&& update-ca-certificates --fresh

RUN wget -O /usr/local/bin/kubectl "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
&& chmod +x /usr/local/bin/kubectl

# arch command on OS X reports "i386" for Intel CPUs regardless of bitness
RUN export ARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) \
&& if [ "$ARCH" = "arm64" ]; then export ARCH=aarch64 ; fi \
Expand All @@ -71,30 +74,33 @@ RUN set -vx; \
&& usermod -aG docker runner \
&& echo "%sudo ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers

ENV RUNNER_TOOL_CACHE=/opt/hostedtoolcache
RUN mkdir /opt/hostedtoolcache \
&& chgrp docker /opt/hostedtoolcache \
&& chmod g+rwx /opt/hostedtoolcache

ENV RUNNER_ASSETS_DIR=/runnertmp
ENV HOME=/home/runner

RUN export ARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) \
&& if [ "$ARCH" = "amd64" ] || [ "$ARCH" = "x86_64" ] || [ "$ARCH" = "i386" ]; then export ARCH=x64 ; fi \
&& mkdir -p "$RUNNER_ASSETS_DIR/packages"

ADD _package/ $RUNNER_ASSETS_DIR/packages/
ADD src/Misc/layoutbin/installdependencies.sh /runnertmp/installdependencies.sh
RUN /runnertmp/installdependencies.sh \
&& apt-get install -y libyaml-dev \
&& rm -rf /var/lib/apt/lists/* \
&& /runnertmp/installdependencies.sh

ADD _package/ $RUNNER_ASSETS_DIR/packages/
RUN export ARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) \
&& if [ "$ARCH" = "amd64" ] || [ "$ARCH" = "x86_64" ] || [ "$ARCH" = "i386" ]; then export ARCH=x64 ; fi \
&& cd "$RUNNER_ASSETS_DIR" \
&& tar xzf ./packages/actions-runner-linux-${ARCH}-${RUNNER_VERSION}.tar.gz \
&& rm ./packages/actions-runner-linux-${ARCH}-${RUNNER_VERSION}.tar.gz \
&& rm -rf packages \
&& ./bin/installdependencies.sh \
&& mv ./externals ./externalstmp \
&& apt-get install -y libyaml-dev \
&& rm -rf /var/lib/apt/lists/*

ENV RUNNER_TOOL_CACHE=/opt/hostedtoolcache
RUN mkdir /opt/hostedtoolcache \
&& chgrp docker /opt/hostedtoolcache \
&& chmod g+rwx /opt/hostedtoolcache
&& chown runner:docker -R $RUNNER_ASSETS_DIR

COPY docker/entrypoint.sh docker/configure.sh /
RUN chmod 755 /entrypoint.sh
Expand Down
10 changes: 6 additions & 4 deletions docker/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ configure(){

retries_left=10
while [[ ${retries_left} -gt 0 ]]; do
log "Configuring the runner."
log "Configuring the 1Source GitHub Runner"
log "# Name: ${RUNNER_NAME}"
log "# Group: ${RUNNER_GROUPS}"
log "# Labels: ${RUNNER_LABELS}"
./config.sh --unattended --replace \
--name "${RUNNER_NAME}" \
--url "${GITHUB_URL}${ATTACH}" \
Expand Down Expand Up @@ -80,7 +83,6 @@ configure(){
RUNNER_HOME=${RUNNER_HOME:-/runner}

if [ -z "${GITHUB_URL}" ]; then
log "Working with public GitHub"
GITHUB_URL="https://github.com/"
else
length=${#GITHUB_URL}
Expand Down Expand Up @@ -124,9 +126,9 @@ if [ ! -d "${RUNNER_HOME}" ]; then
fi

config_args=()
if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" == "true" -a "${RUNNER_EPHEMERAL}" != "false" ]; then
if [ "${RUNNER_EPHEMERAL:-}" == "true" ]; then
config_args+=(--ephemeral)
echo "Passing --ephemeral to config.sh to enable the ephemeral runner."
echo "Using runner as ephemeral."
fi

cd ${RUNNER_HOME}
Expand Down
11 changes: 6 additions & 5 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ error(){
printf "${LIGHTRED}${@}${RESET}\n" 1>&2
}

log "Configuring workspace..."

if [ ! -z "${STARTUP_DELAY_IN_SECONDS}" ]; then
log "Delaying startup by ${STARTUP_DELAY_IN_SECONDS} seconds"
sleep ${STARTUP_DELAY_IN_SECONDS}
fi


# if this is not a testing environment
if [ -z "${UNITTEST:-}" ]; then
sudo chown -R runner:docker ${RUNNER_HOME}
Expand All @@ -41,7 +44,9 @@ if [ -n "${RUNNER_REGISTRATION_ONLY}" ]; then
fi

if [ -z "${UNITTEST:-}" ]; then
mkdir ./externals
if [ ! -d "${RUNNER_HOME}/externals" ]; then
mkdir ./externals
fi
# Hack due to the DinD volumes
mv ./externalstmp/* ./externals/

Expand All @@ -53,10 +58,6 @@ if [ -z "${UNITTEST:-}" ]; then
fi

args=()
if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" != "true" -a "${RUNNER_EPHEMERAL}" != "false" ]; then
args+=(--once)
echo "Passing --once to runsvc.sh to enable the legacy ephemeral runner."
fi

unset RUNNER_NAME RUNNER_REPO RUNNER_TOKEN
exec ./bin/runsvc.sh "${args[@]}"
8 changes: 8 additions & 0 deletions src/Runner.Common/HostContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ public async Task Delay(TimeSpan delay, CancellationToken cancellationToken)
await Task.Delay(delay, cancellationToken);
}

/// <summary>
/// Register a default dynamic service.
/// </summary>
public void RegisterService(Type type, Type target)
{
_serviceTypes.TryAdd(type, target);
}

/// <summary>
/// Creates a new instance of T.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Runner.Worker/ActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ public sealed class ActionManager : RunnerService, IActionManager
// We are running at the start of a job
if (rootStepId == default(Guid))
{
IOUtil.DeleteDirectory(HostContext.GetDirectory(WellKnownDirectory.Actions), executionContext.CancellationToken);
try
{
IOUtil.DeleteDirectory(HostContext.GetDirectory(WellKnownDirectory.Actions), executionContext.CancellationToken);
}
catch
{
executionContext.Debug($"Unable to delete {HostContext.GetDirectory(WellKnownDirectory.Actions)}.");
}
}
// We are running mid job due to a local composite action
else
Expand Down
27 changes: 27 additions & 0 deletions src/Runner.Worker/Container/DockerCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace GitHub.Runner.Worker.Container
[ServiceLocator(Default = typeof(DockerCommandManager))]
public interface IDockerCommandManager : IRunnerService
{
string Type { get; }
string DockerPath { get; }
string DockerInstanceLabel { get; }
Task<DockerVersion> DockerVersion(IExecutionContext context);
Expand All @@ -25,6 +26,9 @@ public interface IDockerCommandManager : IRunnerService
Task<int> DockerStart(IExecutionContext context, string containerId);
Task<int> DockerLogs(IExecutionContext context, string containerId);
Task<List<string>> DockerPS(IExecutionContext context, string options);
Task<List<string>> DockerListByLabel(IExecutionContext context);
Task<List<string>> DockerListByContainerId(IExecutionContext context, string containerId, string status = null);
Task<List<string>> DockerGetEnv(IExecutionContext context, string dockerObject);
Task<int> DockerRemove(IExecutionContext context, string containerId);
Task<int> DockerNetworkCreate(IExecutionContext context, string network);
Task<int> DockerNetworkRemove(IExecutionContext context, string network);
Expand All @@ -42,9 +46,12 @@ public class DockerCommandManager : RunnerService, IDockerCommandManager

public string DockerInstanceLabel { get; private set; }

public string Type { get; private set; }

public override void Initialize(IHostContext hostContext)
{
base.Initialize(hostContext);
Type = "docker";
DockerPath = WhichUtil.Which("docker", true, Trace);
DockerInstanceLabel = IOUtil.GetSha256Hash(hostContext.GetDirectory(WellKnownDirectory.Root)).Substring(0, 6);
}
Expand Down Expand Up @@ -273,6 +280,26 @@ public async Task<int> DockerLogs(IExecutionContext context, string containerId)
return await ExecuteDockerCommandAsync(context, "logs", $"--details {containerId}", context.CancellationToken);
}

public async Task<List<string>> DockerListByLabel(IExecutionContext context)
{
return await DockerPS(context, $"--all --quiet --no-trunc --filter \"label={DockerInstanceLabel}\"");
}

public async Task<List<string>> DockerGetEnv(IExecutionContext context, string containerID)
{
var options = "--format \"{{range .Config.Env}}{{println .}}{{end}}\"";
return await ExecuteDockerCommandAsync(context, "inspect", $"{options} {containerID}");
}

public async Task<List<string>> DockerListByContainerId(IExecutionContext context, string containerId, string status = null)
{
string filter = "";
if (status != "") {
filter = $" --filter status={status.ToLower()}";
}
return await DockerPS(context, $"--all --filter id={containerId}${filter} --no-trunc --format \"{{{{.ID}}}} {{{{.Status}}}}\"");
}

public async Task<List<string>> DockerPS(IExecutionContext context, string options)
{
return await ExecuteDockerCommandAsync(context, "ps", options);
Expand Down
Loading

0 comments on commit 06c0a6b

Please # to comment.