Skip to content

Commit

Permalink
Merge pull request #60 from sanger/creating_printers_in_cupsd
Browse files Browse the repository at this point in the history
Adding printer to cups
  • Loading branch information
emrojo authored Oct 14, 2020
2 parents d500f24 + a60178b commit 143b365
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ENV BUNDLER_VERSION=2.1.4

RUN apt-get update -qq && apt-get install -y
RUN apt-get -y install cups-client cups-bsd
RUN apt-get -y install expect

WORKDIR /code

Expand All @@ -14,6 +15,9 @@ ADD . /code/
RUN gem install bundler
RUN bundle install

COPY create-cups-printer.sh /
RUN ["chmod", "+x", "/create-cups-printer.sh"]

COPY docker-entrypoint.sh /
RUN ["chmod", "+x", "/docker-entrypoint.sh"]
ENTRYPOINT ["/docker-entrypoint.sh"]
Expand Down
21 changes: 21 additions & 0 deletions app/models/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,25 @@ class Printer < ApplicationRecord
filters
end
end

after_save :update_printer_in_cups

def update_printer_in_cups
return unless Rails.configuration.auto_create_printer_in_cupsd

existing_printers = Printer.existing_cups_printers
check_if_printer_exists(existing_printers)
end

def check_if_printer_exists(existing_printers)
return if existing_printers.include? name

`/create-cups-printer.sh #{name}`
end

def self.existing_cups_printers
existing_printers = `lpstat -a`.split("\n")
existing_printers.map! { |printer| printer.split(' ').first }
existing_printers
end
end
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
# Suppress logger output for asset requests.
config.assets.quiet = true

# Default setting for adding printers to cupsd server on printer create
config.auto_create_printer_in_cupsd = false

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

Expand Down
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new

# Default setting for adding printers to cupsd server on printer create
config.auto_create_printer_in_cupsd = true

# Use a different logger for distributed setups.
# require 'syslog/logger'
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
Expand Down
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test

# Default setting for adding printers to cupsd server on printer create
config.auto_create_printer_in_cupsd = false

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr

Expand Down
6 changes: 6 additions & 0 deletions create-cups-printer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/expect
set printername [lindex $argv 0];
spawn lpadmin -U "$env(CUPSD_USERNAME)" -p $printername -v socket://$printername.internal.sanger.ac.uk -E
expect "Password for $env(CUPSD_USERNAME) on cupsd?"
send -- "$env(CUPSD_PASSWORD)\n"
expect eof
6 changes: 6 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/bin/bash
mkdir /etc/cups/
echo "ServerName $CUPSD_SERVER_NAME" > /etc/cups/client.conf
if [ $CUPSD_UPDATE == "true" ]
then
echo "-> Running printers rake task"
bundle exec rails printers:create
echo "-> Printers rake task complete"
fi
exec "$@"
12 changes: 12 additions & 0 deletions lib/tasks/create_printers.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# Can only run in production, unless config for auto_create_printer_in_cupsd is changed
namespace :printers do
desc 'generate list of printers'
task create: :environment do |_t|
existing_printers = Printer.existing_cups_printers
Printer.all.each do |printer|
printer.check_if_printer_exists(existing_printers)
end
end
end

0 comments on commit 143b365

Please sign in to comment.