Skip to content

Commit

Permalink
Host system commands (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderK5 committed Jun 24, 2020
1 parent 003e8e1 commit 6e6e30b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
1 change: 1 addition & 0 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ blocks:
- unknown_command
- broken_unicode
- check_dev_kvm
- host_setup_commands

promotions:
- name: Release
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM ubuntu
FROM python:3

RUN apt-get update && \
apt-get install -y python python-dev python-distribute python-pip && \
apt-get install curl -y && \
curl -sSL https://get.docker.com/ | sh && \
apt-get install -y ssh && \
Expand Down
1 change: 1 addition & 0 deletions pkg/api/job_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ImagePullCredentials struct {
type Compose struct {
ImagePullCredentials []ImagePullCredentials `json:"image_pull_credentials" yaml:"image_pull_credentials"`
Containers []Container `json:"containers" yaml:"containers"`
HostSetupCommands []Command `json:"host_setup_commands" yaml:"host_setup_commands"`
}

type Command struct {
Expand Down
21 changes: 21 additions & 0 deletions pkg/executors/docker_compose_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func NewDockerComposeExecutor(request *api.JobRequest, logger *eventlogger.Logge

func (e *DockerComposeExecutor) Prepare() int {
err := os.MkdirAll(e.tmpDirectory, os.ModePerm)
if err != nil {
return 1
}

err = e.executeHostCommands()
if err != nil {
return 1
}
Expand All @@ -57,6 +61,23 @@ func (e *DockerComposeExecutor) Prepare() int {
return e.setUpSSHJumpPoint()
}

func (e *DockerComposeExecutor) executeHostCommands() error {
hostCommands := e.jobRequest.Compose.HostSetupCommands

for _, c := range hostCommands {
log.Println("Executing Host Command:", c.Directive)
cmd := exec.Command("bash", "-c", c.Directive)

out, err := cmd.CombinedOutput()
if err != nil {
log.Println("Error:", err)
return err
}
log.Println(out)
}
return nil
}

func (e *DockerComposeExecutor) setUpSSHJumpPoint() int {
err := InjectEntriesToAuthorizedKeys(e.jobRequest.SSHPublicKeys)

Expand Down
63 changes: 63 additions & 0 deletions test/e2e/docker/host_setup_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/ruby
# rubocop:disable all

require_relative '../../e2e'

start_job <<-JSON
{
"id": "#{$JOB_ID}",
"executor": "dockercompose",
"compose": {
"containers": [
{
"name": "main",
"image": "ruby:2.6"
}
],
"host_setup_commands": [
{ "directive": "echo HostSetupCommands" }
]
},
"env_vars": [],
"files": [],
"commands": [
{ "directive": "echo Hello World" }
],
"epilogue_always_commands": [],
"callbacks": {
"finished": "https://httpbin.org/status/200",
"teardown_finished": "https://httpbin.org/status/200"
}
}
JSON

wait_for_job_to_finish

assert_job_log <<-LOG
{"event":"job_started", "timestamp":"*"}
{"event":"cmd_started", "timestamp":"*", "directive":"Pulling docker images..."}
*** LONG_OUTPUT ***
{"event":"cmd_finished", "timestamp":"*", "directive":"Pulling docker images...","event":"cmd_finished","exit_code":0,"finished_at":"*","started_at":"*","timestamp":"*"}
{"event":"cmd_started", "timestamp":"*", "directive":"Starting the docker image..."}
{"event":"cmd_output", "timestamp":"*", "output":"Starting a new bash session.\\n"}
{"event":"cmd_finished", "timestamp":"*", "directive":"Starting the docker image...","event":"cmd_finished","exit_code":0,"finished_at":"*","started_at":"*","timestamp":"*"}
{"event":"cmd_started", "timestamp":"*", "directive":"Exporting environment variables"}
{"event":"cmd_finished", "timestamp":"*", "directive":"Exporting environment variables","exit_code":0,"finished_at":"*","started_at":"*"}
{"event":"cmd_started", "timestamp":"*", "directive":"Injecting Files"}
{"event":"cmd_finished", "timestamp":"*", "directive":"Injecting Files","exit_code":0,"finished_at":"*","started_at":"*"}
{"event":"cmd_started", "timestamp":"*", "directive":"echo Hello World"}
{"event":"cmd_output", "timestamp":"*", "output":"Hello World\\n"}
{"event":"cmd_finished", "timestamp":"*", "directive":"echo Hello World","exit_code":0,"finished_at":"*","started_at":"*"}
{"event":"cmd_started", "timestamp":"*", "directive":"export SEMAPHORE_JOB_RESULT=passed"}
{"event":"cmd_finished", "timestamp":"*", "directive":"export SEMAPHORE_JOB_RESULT=passed","exit_code":0,"finished_at":"*","started_at":"*"}
{"event":"job_finished", "timestamp":"*", "result":"passed"}
LOG

0 comments on commit 6e6e30b

Please sign in to comment.