panamax: simplify, refactor, add test

This commit is contained in:
Jaka Hudoklin 2015-01-07 02:40:55 +01:00 committed by Matej Cotman
parent 8c79a2df63
commit ae34f923f7
6 changed files with 197 additions and 223 deletions

View File

@ -5,8 +5,8 @@ with lib;
let
cfg = config.services.panamax;
panamax_api = pkgs.panamax_api.override { dataDir = cfg.dataDir+"/api"; };
panamax_ui = pkgs.panamax_ui.override { dataDir = cfg.dataDir+"/ui"; };
panamax_api = pkgs.panamax_api.override { dataDir = cfg.dataDir + "/api"; };
panamax_ui = pkgs.panamax_ui.override { dataDir = cfg.dataDir + "/ui"; };
in {
@ -48,7 +48,7 @@ in {
type = types.str;
default = "http://127.0.0.1:4001";
description = ''
Fleetctl endpoint.
Panamax fleetctl endpoint.
'';
};
@ -56,7 +56,7 @@ in {
type = types.str;
default = "http://127.0.0.1:19531";
description = ''
Journal endpoint.
Panamax journal endpoint.
'';
};
@ -64,7 +64,7 @@ in {
type = types.str;
default = "SomethingVeryLong.";
description = ''
Secret key (do change this).
Panamax secret key (do change this).
'';
};
@ -72,33 +72,64 @@ in {
##### Implementation
config = mkIf cfg.enable {
systemd.services.panamax_api = {
systemd.services.panamax-api = {
description = "Panamax API";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "fleet.service" "etcd.service" "docker.service" ];
path = [ panamax_api ];
environment = {
RAILS_ENV = "production";
JOURNAL_ENDPOINT = cfg.journalEndpoint;
FLEETCTL_ENDPOINT = cfg.fleetctlEndpoint;
PANAMAX_DATABASE_PATH = "${cfg.dataDir}/api/db/mnt/db.sqlite3";
};
preStart = "${panamax_api}/bin/panamax-api-init";
preStart = ''
rm -rf ${cfg.dataDir}/state/tmp
mkdir -p ${cfg.dataDir}/api/{db/mnt,state/log,state/tmp}
ln -sf ${panamax_api}/share/panamax-api/_db/{schema.rb,seeds.rb,migrate} ${cfg.dataDir}/api/db/
if [ ! -f ${cfg.dataDir}/.created ]; then
bundle exec rake db:setup
bundle exec rake db:seed
bundle exec rake panamax:templates:load || true
touch ${cfg.dataDir}/.created
else
bundle exec rake db:migrate
fi
'';
serviceConfig = {
ExecStart = "${panamax_api}/bin/panamax-api-run --port ${toString cfg.APIPort}";
ExecStart = "${panamax_api}/bin/bundle exec rails server --binding 127.0.0.1 --port ${toString cfg.APIPort}";
User = "panamax";
Group = "panamax";
};
};
systemd.services.panamax_ui = {
systemd.services.panamax-ui = {
description = "Panamax UI";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "panamax_api.service" ];
path = [ panamax_ui ];
environment = {
RAILS_ENV = "production";
JOURNAL_ENDPOINT = cfg.journalEndpoint;
PMX_API_PORT_3000_TCP_ADDR = "localhost";
PMX_API_PORT_3000_TCP_PORT = toString cfg.APIPort;
SECRET_KEY_BASE = cfg.secretKey;
};
preStart = ''
rm -rf ${cfg.dataDir}/state/tmp
mkdir -p ${cfg.dataDir}/ui/state/{log,tmp}
'';
serviceConfig = {
ExecStart = "${panamax_ui}/bin/panamax-ui-run --port ${toString cfg.UIPort}";
ExecStart = "${panamax_ui}/bin/bundle exec rails server --binding 127.0.0.1 --port ${toString cfg.UIPort}";
User = "panamax";
Group = "panamax";
};

View File

@ -300,6 +300,7 @@ in rec {
tests.nfs3 = callTest tests/nfs.nix { version = 3; };
tests.nsd = callTest tests/nsd.nix {};
tests.openssh = callTest tests/openssh.nix {};
tests.panamax = scrubDrv (import tests/panamax.nix { system = "x86_64-linux"; });
tests.peerflix = callTest tests/peerflix.nix {};
tests.printing = callTest tests/printing.nix {};
tests.proxy = callTest tests/proxy.nix {};

18
nixos/tests/panamax.nix Normal file
View File

@ -0,0 +1,18 @@
import ./make-test.nix {
name = "panamax";
machine = { config, pkgs, ... }: {
services.panamax.enable = true;
};
testScript =
''
startAll;
$machine->waitForUnit("panamax-api.service");
$machine->waitForUnit("panamax-ui.service");
$machine->waitForOpenPort(3000);
$machine->waitForOpenPort(8888);
$machine->succeed("curl --fail http://localhost:8888/ > /dev/null");
$machine->shutdown;
'';
}

View File

@ -1,139 +1,86 @@
{ stdenv, buildEnv, fetchgit, fetchurl, ruby_2_1, rubygemsFun, libxslt, libxml2
, sqlite, openssl, cacert, writeScriptBin, docker
{ stdenv, buildEnv, fetchgit, fetchurl, makeWrapper
, ruby, rubygemsFun, libxslt, libxml2, sqlite, openssl, cacert, docker
, dataDir ? "/var/lib/panamax-api" }:
with stdenv.lib;
let
ruby = ruby_2_1;
rubygems = rubygemsFun ruby_2_1;
database_yml = builtins.toFile "database.yml" ''
production:
adapter: sqlite3
database: <%= ENV["PANAMAX_DATABASE_PATH"] || "${dataDir}/db/mnt/db.sqlite3" %>
timeout: 5000
'';
in stdenv.mkDerivation rec {
name = "panamax-api-${version}";
version = "0.2.11";
bundler = fetchurl {
url = "http://rubygems.org/downloads/bundler-1.7.9.gem";
sha256 = "1gd201rh17xykab9pbqp0dkxfm7b9jri02llyvmrc0c5bz2vhycm";
};
src = fetchgit {
rev = "refs/tags/v${version}";
url = "git://github.com/CenturyLinkLabs/panamax-api";
sha256 = "01sz7jibn1rqfga85pr4p8wk6jfldzfaxj1726vs6znmcwhfkcgj";
};
gemspec = map (gem: fetchurl { url=gem.url; sha256=gem.hash; }) (import ./Gemfile-api.nix);
srcs = {
bundler = fetchurl {
url = "http://rubygems.org/downloads/bundler-1.7.9.gem";
sha256 = "1gd201rh17xykab9pbqp0dkxfm7b9jri02llyvmrc0c5bz2vhycm";
};
};
buildInputs = [ makeWrapper sqlite openssl ruby (rubygemsFun ruby) ];
panamax_api = stdenv.mkDerivation rec {
name = "panamax-api-${version}";
version = "0.2.11";
setSourceRoot = ''
mkdir -p $out/share
cp -R git-export $out/share/panamax-api
export sourceRoot="$out/share/panamax-api"
'';
src = fetchgit {
rev = "refs/tags/v${version}";
url = "git://github.com/CenturyLinkLabs/panamax-api";
sha256 = "01sz7jibn1rqfga85pr4p8wk6jfldzfaxj1726vs6znmcwhfkcgj";
};
buildInputs = [ sqlite openssl ruby ];
installPhase = ''
mkdir -p $out/share/panamax-api
cp -R . $out/share/panamax-api
cd $out/share/panamax-api
export HOME=$PWD
export GEM_HOME=$PWD
export PATH="${rubygems}/bin:$PATH"
export RAILS_ENV=production
find . -type f -exec sed -e 's|/usr/bin/docker|${docker}/bin/docker|g' -i "{}" \;
mkdir -p vendor/cache
${stdenv.lib.concatStrings (map (gem: "ln -s ${gem} vendor/cache/${gem.name};") gemspec)}
${"ln -s ${srcs.bundler} vendor/cache/${srcs.bundler.name};"}
gem install --local vendor/cache/${srcs.bundler.name}
ln -sf ${database_yml} config/database.yml
bin/bundle install -j4 --verbose --local --deployment
rm -f ./bin/*
./gems/bundler-*/bin/bundle exec rake rails:update:bin
rm -rf log
ln -sf ${dataDir}/state/log .
ln -sf ${dataDir}/state/tmp .
mv ./db ./_db
ln -sf ${dataDir}/db .
'';
};
panamax_api_init = writeScriptBin "panamax-api-init" ''
#!${stdenv.shell}
test -d ${dataDir}/db && exit 0
cd ${panamax_api}/share/panamax-api
postPatch = ''
find . -type f -exec sed -e 's|/usr/bin/docker|${docker}/bin/docker|g' -i "{}" \;
'';
configurePhase = ''
export HOME=$PWD
export GEM_HOME=$PWD
export PATH="${panamax_api}/share/panamax-api/bin:${ruby}/bin:$PATH"
export OPENSSL_X509_CERT_FILE="${cacert}/etc/ca-bundle.crt"
export RAILS_ENV=production
mkdir -p ${dataDir}/state/log
mkdir -p ${dataDir}/db/mnt
ln -sf ${panamax_api}/share/panamax-api/_db/schema.rb ${dataDir}/db/
ln -sf ${panamax_api}/share/panamax-api/_db/seeds.rb ${dataDir}/db/
ln -sf ${panamax_api}/share/panamax-api/_db/migrate ${dataDir}/db/
bundle exec rake db:setup
bundle exec rake db:seed
bundle exec rake panamax:templates:load
mkdir -p vendor/cache
${concatStrings (map (gem: "ln -s ${gem} vendor/cache/${gem.name};") gemspec)}
ln -s ${bundler} vendor/cache/${bundler.name}
ln -sf ${database_yml} config/database.yml
'';
panamax_api_run = writeScriptBin "panamax-api-run" ''
#!${stdenv.shell}
cd ${panamax_api}/share/panamax-api
mkdir -p ${dataDir}/state/tmp
export HOME=$PWD
export GEM_HOME=$PWD
export PATH="${panamax_api}/share/panamax-api/bin:${ruby}/bin:${docker}/bin:$PATH"
export RAILS_ENV=production
export SSL_CERT_FILE="${cacert}/etc/ca-bundle.crt"
bin/bundle exec rails s $@
buildPhase = ''
gem install --local vendor/cache/${bundler.name}
bin/bundle install -j4 --verbose --local --deployment --without development test
'';
database_yml = builtins.toFile "database.yml" ''
development:
adapter: sqlite3
database: ${dataDir}/db/mnt/development.sqlite3
pool: 5
timeout: 5000
installPhase = ''
rm -rf log tmp
mv ./db ./_db
ln -sf ${dataDir}/{db,state/log,state/tmp} .
# See corresponding schema load in spec/support/in_memory_database
test:
adapter: sqlite3
database: ":memory:"
verbosity: quiet
production:
adapter: sqlite3
database: ${dataDir}/db/mnt/production.sqlite3
pool: 5
timeout: 5000
mkdir -p $out/bin
makeWrapper bin/bundle "$out/bin/bundle" \
--run "cd $out/share/panamax-api" \
--prefix "PATH" : "$out/share/panamax-api/bin:${ruby}/bin:$PATH" \
--prefix "HOME" : "$out/share/panamax-api" \
--prefix "GEM_HOME" : "$out/share/panamax-api" \
--prefix "OPENSSL_X509_CERT_FILE" : "${cacert}/etc/ca-bundle.crt" \
--prefix "SSL_CERT_FILE" : "${cacert}/etc/ca-bundle.crt"
'';
in
stdenv.mkDerivation rec {
name = panamax_api.name;
postFixup = ''
rm -r vendor/cache/*
'';
unpackPhase = "true";
installPhase = ''
mkdir -p $out/bin
ln -s ${panamax_api_init}/bin/* $out/bin
ln -s ${panamax_api_run}/bin/* $out/bin
'';
meta = with stdenv.lib; {
homepage = https://github.com/CenturyLinkLabs/panamax-api;
description = "The API behind The Panamax UI";
license = licenses.asl20;
maintainers = with maintainers; [ matejc ];
platforms = platforms.linux;
};
}
meta = with stdenv.lib; {
homepage = https://github.com/CenturyLinkLabs/panamax-api;
description = "The API behind The Panamax UI";
license = licenses.asl20;
maintainers = with maintainers; [ matejc offline ];
platforms = platforms.linux;
};
}

View File

@ -1,101 +1,74 @@
{ stdenv, buildEnv, fetchgit, fetchurl, ruby_2_1, rubygemsFun
, libxml2, libxslt, openssl, writeScriptBin, sqlite
, dataDir ? "/var/lib/panamax-ui" }:
let
ruby = ruby_2_1;
rubygems = rubygemsFun ruby_2_1;
{ stdenv, fetchgit, fetchurl, makeWrapper
, ruby, rubygemsFun, openssl, sqlite, dataDir ? "/var/lib/panamax-ui"}:
with stdenv.lib;
stdenv.mkDerivation rec {
name = "panamax-ui-${version}";
version = "0.2.11";
bundler = fetchurl {
url = "http://rubygems.org/downloads/bundler-1.7.9.gem";
sha256 = "1gd201rh17xykab9pbqp0dkxfm7b9jri02llyvmrc0c5bz2vhycm";
};
src = fetchgit {
rev = "refs/tags/v${version}";
url = "git://github.com/CenturyLinkLabs/panamax-ui";
sha256 = "17j5ac8fzp377bzg7f239jdcc9j0c63bkx0ill5nl10i3h05z7jh";
};
gemspec = map (gem: fetchurl { url=gem.url; sha256=gem.hash; }) (import ./Gemfile-ui.nix);
srcs = {
bundler = fetchurl {
url = "http://rubygems.org/downloads/bundler-1.7.9.gem";
sha256 = "1gd201rh17xykab9pbqp0dkxfm7b9jri02llyvmrc0c5bz2vhycm";
};
};
buildInputs = [ makeWrapper ruby openssl sqlite (rubygemsFun ruby) ];
panamax_ui = stdenv.mkDerivation rec {
name = "panamax-ui-${version}";
version = "0.2.11";
src = fetchgit {
rev = "refs/tags/v${version}";
url = "git://github.com/CenturyLinkLabs/panamax-ui";
sha256 = "17j5ac8fzp377bzg7f239jdcc9j0c63bkx0ill5nl10i3h05z7jh";
};
buildInputs = [ ruby openssl sqlite ];
installPhase = ''
mkdir -p $out/share/panamax-ui
cp -R . $out/share/panamax-ui
cd $out/share/panamax-ui
find . -type f -iname "*.haml" -exec sed -e 's|CoreOS Journal|NixOS Journal|g' -i "{}" \;
find . -type f -iname "*.haml" -exec sed -e 's|CoreOS Local|NixOS Local|g' -i "{}" \;
find . -type f -iname "*.haml" -exec sed -e 's|CoreOS Host|NixOS Host|g' -i "{}" \;
sed -e 's|CoreOS Local|NixOS Local|g' -i "spec/features/manage_application_spec.rb"
export HOME=$PWD
export GEM_HOME=$PWD
export PATH="${rubygems}/bin:$PATH"
mkdir -p vendor/cache
${stdenv.lib.concatStrings (map (gem: "ln -s ${gem} vendor/cache/${gem.name};") gemspec)}
${"ln -s ${srcs.bundler} vendor/cache/${srcs.bundler.name};"}
gem install --local vendor/cache/${srcs.bundler.name}
bin/bundle install --verbose --local --without development test
rm -f ./bin/*
./gems/bundler-*/bin/bundle exec rake rails:update:bin
rm -rf log
ln -sf ${dataDir}/state/log .
rm -rf tmp
ln -sf ${dataDir}/state/tmp .
rm -rf db
ln -sf ${dataDir}/db .
'';
};
panamax_ui_run = writeScriptBin "panamax-ui-run" ''
#!${stdenv.shell}
cd ${panamax_ui}/share/panamax-ui
export PATH="${panamax_ui}/share/panamax-ui/bin:${ruby}/bin:$PATH"
export RAILS_ENV="production"
export HOME="${panamax_ui}/share/panamax-ui"
export GEM_HOME="${panamax_ui}/share/panamax-ui"
export GEM_PATH="${panamax_ui}/share/panamax-ui"
echo ${panamax_ui}/share/panamax-ui
mkdir -p ${dataDir}/state/log
mkdir -p ${dataDir}/state/tmp
mkdir -p ${dataDir}/db
export PMX_API_PORT_3000_TCP_ADDR=localhost
bin/rails server $@
setSourceRoot = ''
mkdir -p $out/share
cp -R git-export $out/share/panamax-ui
export sourceRoot="$out/share/panamax-ui"
'';
in
stdenv.mkDerivation rec {
name = panamax_ui.name;
postPatch = ''
find . -type f -iname "*.haml" -exec sed -e 's|CoreOS Journal|NixOS Journal|g' -i "{}" \;
find . -type f -iname "*.haml" -exec sed -e 's|CoreOS Local|NixOS Local|g' -i "{}" \;
find . -type f -iname "*.haml" -exec sed -e 's|CoreOS Host|NixOS Host|g' -i "{}" \;
sed -e 's|CoreOS Local|NixOS Local|g' -i "spec/features/manage_application_spec.rb"
'';
unpackPhase = "true";
configurePhase = ''
export HOME=$PWD
export GEM_HOME=$PWD
installPhase = ''
mkdir -p $out/bin
ln -s ${panamax_ui_run}/bin/* $out/bin
'';
mkdir -p vendor/cache
${concatStrings (map (gem: "ln -s ${gem} vendor/cache/${gem.name};") gemspec)}
ln -s ${bundler} vendor/cache/${bundler.name}
'';
meta = with stdenv.lib; {
homepage = https://github.com/CenturyLinkLabs/panamax-ui;
description = "The Web GUI for Panamax";
license = licenses.asl20;
maintainers = with maintainers; [ matejc ];
platforms = platforms.linux;
};
}
buildPhase = ''
gem install --local vendor/cache/${bundler.name}
bin/bundle install --verbose --local --without development test
rm -f ./bin/*
ruby ./gems/bundler-*/bin/bundle exec rake rails:update:bin
'';
installPhase = ''
rm -rf log tmp db
ln -sf ${dataDir}/{db,state/log,state/tmp} .
mkdir -p $out/bin
makeWrapper bin/bundle "$out/bin/bundle" \
--run "cd $out/share/panamax-ui" \
--prefix "PATH" : "$out/share/panamax-ui/bin:${ruby}/bin:$PATH" \
--prefix "HOME" : "$out/share/panamax-ui" \
--prefix "GEM_HOME" : "$out/share/panamax-ui" \
--prefix "GEM_PATH" : "$out/share/panamax-ui"
'';
meta = with stdenv.lib; {
homepage = https://github.com/CenturyLinkLabs/panamax-ui;
description = "The Web GUI for Panamax";
license = licenses.asl20;
maintainers = with maintainers; [ matejc offline ];
platforms = platforms.linux;
};
}

View File

@ -10789,8 +10789,12 @@ let
namecoin = callPackage ../applications/misc/namecoin { };
namecoinqt = callPackage ../applications/misc/namecoin/qt.nix { };
panamax_api = callPackage ../applications/networking/cluster/panamax/api.nix {};
panamax_ui = callPackage ../applications/networking/cluster/panamax/ui.nix {};
panamax_api = callPackage ../applications/networking/cluster/panamax/api.nix {
ruby = ruby_2_1;
};
panamax_ui = callPackage ../applications/networking/cluster/panamax/ui.nix {
ruby = ruby_2_1;
};
pcmanfm = callPackage ../applications/misc/pcmanfm { };