Skip to content

Commit

Permalink
Project structure (#22)
Browse files Browse the repository at this point in the history
* Changed directory structure to Crystal standard format

* Changed Dockerfile to Alpine 3.15

* Updated CI/CD to use Crystal 1.3.2
  • Loading branch information
marghidanu authored Apr 1, 2022
1 parent 0dc5fe7 commit 48b28c3
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 61 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install Crystal
uses: oprypin/install-crystal@v1
with:
crystal: 1.2.2
crystal: 1.3.2

- name: Additional Linux dependencies
if: contains(matrix.os, 'ubuntu')
Expand All @@ -39,7 +39,7 @@ jobs:
- name: Update version
run: |
yq eval -i ".version = \"${{ github.event.release.tag_name }}\"" shard.yml
perl -pi -e 's/VERSION = ".*"/VERSION = "${{ github.event.release.tag_name }}"/g' src/version.cr
perl -pi -e 's/VERSION = ".*"/VERSION = "${{ github.event.release.tag_name }}"/g' src/werk.cr
- name: Install dependencies
run: shards install --production --ignore-crystal-version
Expand Down Expand Up @@ -84,7 +84,7 @@ jobs:
- name: Update version
run: |
yq eval -i ".version = \"${{ github.event.release.tag_name }}\"" shard.yml
perl -pi -e 's/VERSION = ".*"/VERSION = "${{ github.event.release.tag_name }}"/g' src/version.cr
perl -pi -e 's/VERSION = ".*"/VERSION = "${{ github.event.release.tag_name }}"/g' src/werk.cr
- name: Setup QEMU
uses: docker/setup-qemu-action@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Install Crystal
uses: oprypin/install-crystal@v1
with:
crystal: 1.2.2
crystal: 1.3.2

- name: Additional Linux dependencies
if: contains(matrix.os, 'ubuntu')
Expand Down
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
FROM crystallang/crystal:1.2.2-alpine AS build
FROM alpine:3.15 AS build

# hadolint ignore=DL3018
# RUN sed -i -e 's/v[[:digit:]]\..*\//edge\//g' /etc/apk/repositories \
# && apk add --no-cache build-base crystal shards libressl-dev yaml-static zlib-static
RUN apk add --no-cache build-base crystal shards libressl-dev yaml-static zlib-static

WORKDIR /opt/app
COPY . .

RUN shards install --production --ignore-crystal-version \
&& shards build --release --no-debug --static

FROM alpine:3.13
FROM alpine:3.15

COPY --from=build /opt/app/bin/werk /usr/local/bin/

CMD [ "werk" ]
ENTRYPOINT [ "werk" ]
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ dependencies:
github: marghidanu/docr
dotenv:
github: gdotdesign/cr-dotenv
crystal: 1.2.2
crystal: 1.3.2
license: MIT
4 changes: 2 additions & 2 deletions spec/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe "Config" do
config.version.should eq "1.0"
config.jobs.keys.should eq ["shell", "container"]

config.jobs["shell"].should be_a Werk::Job::Local
config.jobs["container"].should be_a Werk::Job::Docker
config.jobs["shell"].should be_a Werk::Jobs::Local
config.jobs["container"].should be_a Werk::Jobs::Docker
end

it "should load the config file" do
Expand Down
5 changes: 1 addition & 4 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
require "spec"

require "../src/config"
require "../src/report"
require "../src/jobs/*"
require "../src/utils/*"
require "../src/werk/*"
3 changes: 0 additions & 3 deletions src/version.cr

This file was deleted.

28 changes: 16 additions & 12 deletions src/werk.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
require "log"

require "./application"

begin
Log.setup_from_env(
default_sources: "werk.*",
log_level_env: "WERK_LOG_LEVEL",
)

Werk::Application.run
rescue ex : Exception
puts "Error: #{ex.message}"
exit(1)
require "./werk/application"

module Werk
VERSION = "0.0.0"

begin
Log.setup_from_env(
default_sources: "werk.*",
log_level_env: "WERK_LOG_LEVEL",
)

Werk::Application.run
rescue ex : Exception
puts "Error: #{ex.message}"
exit(1)
end
end
5 changes: 2 additions & 3 deletions src/application.cr → src/werk/application.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
require "admiral"

require "./version"
require "./commands/*"

module Werk
class Application < Admiral::Command
define_version Werk::VERSION
define_help description: "Werk"

register_sub_command plan : Werk::Command::Plan,
register_sub_command plan : Werk::Commands::Plan,
description: "Display execution plan"

register_sub_command run : Werk::Command::Run,
register_sub_command run : Werk::Commands::Run,
description: "Run a job by name"

def run
Expand Down
4 changes: 2 additions & 2 deletions src/commands/plan.cr → src/werk/commands/plan.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ require "colorize"
require "../config"
require "../scheduler"

module Werk
class Command::Plan < Admiral::Command
module Werk::Commands
class Plan < Admiral::Command
define_help description: "List jobs information"

define_argument target : String,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/run.cr → src/werk/commands/run.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require "docr"
require "../config"
require "../scheduler"

module Werk
class Command::Run < Admiral::Command
module Werk::Commands
class Run < Admiral::Command
Log = ::Log.for(self)

define_help description: "Run target"
Expand Down
4 changes: 2 additions & 2 deletions src/config.cr → src/werk/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ module Werk
getter interpreter = "/bin/sh"

use_yaml_discriminator "executor", {
local: Werk::Job::Local,
docker: Werk::Job::Docker,
local: Werk::Jobs::Local,
docker: Werk::Jobs::Docker,
}

abstract def run(session_id : UUID, name : String, context : String) : {Int32, String}
Expand Down
4 changes: 2 additions & 2 deletions src/jobs/docker.cr → src/werk/jobs/docker.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ require "digest/md5"
require "docr"
require "log"

module Werk
class Job::Docker < Config::Job
module Werk::Jobs
class Docker < Werk::Config::Job
@[YAML::Field(key: "image")]
getter image = "alpine:latest"

Expand Down
4 changes: 2 additions & 2 deletions src/jobs/local.cr → src/werk/jobs/local.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "log"

module Werk
class Job::Local < Config::Job
module Werk::Jobs
class Local < Werk::Config::Job
Log = ::Log.for(self)

def run(session_id : UUID, name : String, context : String) : {Int32, String}
Expand Down
File renamed without changes.
15 changes: 4 additions & 11 deletions src/scheduler.cr → src/werk/scheduler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@ module Werk
Log.debug { "Retrieve execution plan for '#{target}'" }
plan = self.get_plan(target)

if @config.max_jobs < 1
raise "Max parallel jobs must be greater than 0!"
end
Log.debug { "Running scheduler with max_jobs set to #{@config.max_jobs}" }
raise "Max parallel jobs must be greater than 0!" if @config.max_jobs < 1

@config.dotenv.each do |env_file|
@config.variables.merge!(Dotenv.load(env_file))
end
Log.debug { "Running scheduler with max_jobs set to #{@config.max_jobs}" }
@config.dotenv.each { |f| @config.variables.merge!(Dotenv.load(f)) }

report = Werk::Report.new(target: target, plan: plan)
plan.each_with_index do |stage, stage_id|
Expand All @@ -41,10 +37,7 @@ module Werk
batch.each_with_index do |name, job_id|
job = @config.jobs[name]

job.dotenv.each do |env_file|
job.variables.merge!(Dotenv.load(env_file))
end

job.dotenv.each { |f| job.variables.merge!(Dotenv.load(f)) }
vars = Hash(String, String).new
vars.merge!(@config.variables)
vars.merge!(job.variables)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/colors.cr → src/werk/utils/colors.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "colorize"

module Werk
class Utils::Colors
module Werk::Utils
class Colors
getter colors = [
Colorize::ColorANSI::LightRed,
Colorize::ColorANSI::LightGreen,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/graph.cr → src/werk/utils/graph.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Werk
module Werk::Utils
# Minimal implementation for a Directed Acyclic Graph.
class Utils::Graph
class Graph
# Creates a new empty graph. The implementation uses an adjacency list representation for the graph.
def initialize
@adjacency_list = Hash(String, Set(String)).new
Expand Down
4 changes: 2 additions & 2 deletions src/utils/prefix.cr → src/werk/utils/prefix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ require "colorize"

require "./colors"

module Werk
class Utils::PrefixIO < IO
module Werk::Utils
class PrefixIO < IO
@color : Colorize::Color

# Creates a new instance that will use output to write to
Expand Down

0 comments on commit 48b28c3

Please sign in to comment.