Skip to content

Instantly share code, notes, and snippets.

@dch
Created October 28, 2025 10:40
Show Gist options
  • Select an option

  • Save dch/88faadfbc706b547a92257f4d17e6aa4 to your computer and use it in GitHub Desktop.

Select an option

Save dch/88faadfbc706b547a92257f4d17e6aa4 to your computer and use it in GitHub Desktop.
mix tasks to init couchdb and rabbitmq
defmodule Mix.Tasks.Koans.Init.Couch do
use Mix.Task
import ICouch
alias ICouch.Document, as: Doc
@shortdoc "Initialise CouchDB for koan-ci"
def run(_) do
# at this point in time, no apps are started and we do not have
# access to parameters stashed in ./config/*.ex
Application.ensure_all_started(:icouch)
couch = server_connection("http://admin:[email protected]:5984/")
# add the workflow db
_ = assert_db!(couch, "workflow")
# add the koans db
db = assert_db!(couch, "koans")
doc =
Doc.new("demo")
|> Doc.put("hmac_secret", "123456789abcdef0123456789abcdef0")
# dont care if the database/document already exists
_ = save_doc(db, doc)
doc =
Doc.new("skunkwerks")
|> Doc.put("hmac_secret", "e08949d75d7e289f210badf852d8309e")
# dont care if the database/document already exists
_ = save_doc(db, doc)
# add the koans user
db = assert_db!(couch, "_users")
doc =
Doc.new("org.couchdb.user:koans")
|> Doc.put("name", "koans")
|> Doc.put("password", "tiger")
|> Doc.put("roles", [])
|> Doc.put("type", "user")
# dont care if the database/document already exists
_ = save_doc(db, doc)
end
end
defmodule Mix.Tasks.Koans.Init.Rabbit do
use Mix.Task
use AMQP
@shortdoc "Initialise RabbitMQ broker"
@queue "koans.workflow"
@routing_key "koans.workflow"
@exchange "koans.topic"
def run(_) do
# at this point in time, no apps are started and we do not have
# access to parameters stashed in ./config/*.ex
Application.ensure_all_started(:amqp)
amqp = "amqp://guest:[email protected]:5672/%2f"
{:ok, conn} = AMQP.Connection.open(amqp)
{:ok, chan} = AMQP.Channel.open(conn)
{:ok, _} = AMQP.Queue.declare(chan, @queue, durable: true)
:ok = AMQP.Exchange.topic(chan, @exchange, durable: true)
:ok = AMQP.Queue.bind(chan, @queue, @exchange, routing_key: @routing_key)
Application.get_all_env(:mu) |> IO.inspect()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment