Skip to content

Commit

Permalink
Merge pull request #3 from erdnaxeli/36.1
Browse files Browse the repository at this point in the history
New command "logs"
  • Loading branch information
erdnaxeli authored Feb 23, 2021
2 parents 4ce4062 + 5a56b00 commit 543d494
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ run:
crystal run src/main.cr

static:
docker run --rm -it -v ${PWD}:/workspace -w /workspace crystallang/crystal:0.35.1-alpine crystal build --static --release src/main.cr
docker run --rm -it -v ${PWD}:/workspace -w /workspace crystallang/crystal:0.36.1-alpine crystal build --static --release src/main.cr

test:
crystal spec --error-trace
Expand Down
2 changes: 1 addition & 1 deletion shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ shards:

caridina:
git: https://github.com/erdnaxeli/caridina.git
version: 0.7.0
version: 0.10.2

parameters:
git: https://github.com/erdnaxeli/crystal-parameters.git
Expand Down
8 changes: 4 additions & 4 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
name: docker-companion
version: 0.1.0
version: 0.4.0

authors:
- your-name-here <your-email-here>
- Alexandre Morignot <[email protected]>

targets:
docker-companion:
main: src/main.cr

crystal: 0.35.0
crystal: 0.36.1

dependencies:
caridina:
github: erdnaxeli/caridina
version: ~>0.7.0
version: ~>0.10.2
parameters:
github: erdnaxeli/crystal-parameters
version: ~>1.0.4
Expand Down
20 changes: 19 additions & 1 deletion src/bot.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ class Companion::Bot

@conn.send_message(room_id, msg)
end
parser.on("logs", "get logs") do
parser.banner = "logs PROJECT SERVICE"
parser.unknown_args do |args|
if args.size == 2
project = args.shift
name = args.shift
begin
logs = @manager.get_logs(project, name)
rescue ex
@conn.send_message(room_id, ex.to_s)
else
@conn.send_message(room_id, logs, "<pre><code>#{logs}</code></pre>")
end
else
@conn.send_message(room_id, parser.to_s)
end
end
end
parser.on("networks", "list networks") do
msg = String.build do |str|
str << "* " << @manager.networks.map { |n| %(#{n.name} #{n.id}) }.join("\n* ")
Expand Down Expand Up @@ -143,7 +161,7 @@ class Companion::Bot
end

private def follow_invites(event)
event = event.as(Caridina::Events::StrippedState)
event = event.as(Caridina::Events::StrippedMember)
room_id = event.room_id.not_nil!

if content = event.content.as?(Caridina::Events::Member::Content)
Expand Down
17 changes: 0 additions & 17 deletions src/core_ext/http/client.cr

This file was deleted.

6 changes: 3 additions & 3 deletions src/docker-companion.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require "time"
require "yaml"

module Companion
VERSION = "0.1.0"
VERSION = "0.4.0"

Log = ::Log.for(self)

Expand Down Expand Up @@ -57,7 +57,7 @@ module Companion
config = read_config

Log.info { "Connecting to Matrix" }
conn = Caridina::ConnectionImpl.new(
conn = Caridina::Connection.new(
config.matrix.homeserver,
config.matrix.access_token,
)
Expand Down Expand Up @@ -127,7 +127,7 @@ module Companion
# As we don't know (yet) if the currently running containers are correctly
# configured, we just shut them down and create new ones.
#
# TODO: do that in a better way
# TODO: do that in a better way
manager.down(name)
manager.up(name)
end
Expand Down
28 changes: 18 additions & 10 deletions src/docker/client/client.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "json"
require "socket"

require "./container"
require "./exceptions"
require "./image"
require "./network"
require "../../macro"
require "../../core_ext/http/client"

module Companion::Docker::Client
struct CreateContainerResponse
Expand Down Expand Up @@ -33,6 +33,9 @@ module Companion::Docker::Client
# Returns nil if the container is not found.
abstract def get_container_id(name : String) : String?

# Get a container's logs
abstract def get_logs(id : String, stdout : Bool, stderr : Bool, lines : Int32) : String

# Get images.
abstract def images : Array(Image)

Expand All @@ -55,13 +58,13 @@ class Companion::Docker::Client::Local
end

def initialize(@config = Config.new)
@client = HTTP::Client.unix(@config.socket)
@client = HTTP::Client.new(UNIXSocket.new(@config.socket))
end

# Creates a new container and return its id.
def create_container(options : CreateContainerOptions, name : String? = nil) : CreateContainerResponse
route = name ? "/containers/create?name=#{name}" : "/containers/create"
raw_response = @client.post(route, headers: HTTP::Headers{"Content-Type" => "application/json"}, body: options.to_json)
raw_response = @client.post("http://localhost#{route}", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: options.to_json)
response = CreateContainerResponse.from_json(raw_response.body)

if !raw_response.success?
Expand All @@ -77,14 +80,14 @@ class Companion::Docker::Client::Local
end

def create_network(options : CreateNetworkOptions) : CreateNetworkResponse
raw_response = @client.post("/networks/create", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: options.to_json)
raw_response = @client.post("http://localhost/networks/create", headers: HTTP::Headers{"Content-Type" => "application/json"}, body: options.to_json)
CreateNetworkResponse.from_json(raw_response.body)
end

# Create an image and returns its id
def create_image(from_image : String, tag : String)
params = HTTP::Params.encode({fromImage: from_image, tag: tag})
@client.post("/images/create?#{params}") do |response|
@client.post("http://localhost/images/create?#{params}") do |response|
response.body_io.each_line do |line|
yield CreateImageResponse.from_json(line)
end
Expand All @@ -94,7 +97,7 @@ class Companion::Docker::Client::Local
# Connect a container to a network
def connect_network(options : ConnectNetworkOptions) : Nil
response = @client.post(
"/networks/#{options.endpoint_config.network_id}/connect",
"http://localhost/networks/#{options.endpoint_config.network_id}/connect",
headers: HTTP::Headers{"Content-Type" => "application/json"},
body: options.to_json
)
Expand Down Expand Up @@ -124,13 +127,18 @@ class Companion::Docker::Client::Local
end
end

def get_logs(id : String, stdout : Bool, stderr : Bool, lines : Int32) : String
response = @client.get("http://localhost/containers/#{id}/logs?stdout=#{stdout}&stderr={stderr}&tail=#{lines}")
response.body.lines.map { |l| String.new(l.to_slice[8...]) }.join("\n")
end

def images : Array(Image)
response = @client.get("/images/json")
response = @client.get("http://localhost/images/json")
Array(Image).from_json(response.body)
end

def networks : Array(Network)
response = @client.get("/networks")
response = @client.get("http://localhost/networks")
Array(Network).from_json(response.body)
end

Expand All @@ -143,11 +151,11 @@ class Companion::Docker::Client::Local
#
# If the container is running, it will be killed.
def remove_container(id : String) : Nil
@client.delete("/containers/#{id}?force=true")
@client.delete("http://localhost/containers/#{id}?force=true")
end

# Starts a container.
def start_container(id : String) : Nil
@client.post("/containers/#{id}/start")
@client.post("http://localhost/containers/#{id}/start")
end
end
16 changes: 16 additions & 0 deletions src/manager.cr
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ class Companion::Manager
@projects.each_key
end

# Returns a project container's logs
def get_logs(project_name : String, name : String) : String
get_project(project_name).each_service do |service|
if service.name == name
container_name = get_container_name(project_name, service)
if id = @docker.get_container_id(container_name)
return @docker.get_logs(id, stdout: true, stderr: true, lines: 15)
else
raise "The service #{name} is not created yet"
end
end
end

raise "Unknown service #{name}"
end

# Creates and starts containers for the project *name*.
def up(name : String)
create(name)
Expand Down

0 comments on commit 543d494

Please sign in to comment.