#!/usr/bin/ruby # frozen_string_literal: true require "date" require "dhall" require "em-hiredis" require "pg/em/connection_pool" require "em-http" require "em_promise" require "json" require "optparse" require "ruby-bandwidth-iris" require "securerandom" require "sentry-ruby" require "time" @verbosity = 0 @real_data = true @dry_run = false @sources = [] @filters = {} OptionParser.new do |opts| opts.banner = "Usage: porting [-vn] " \ "[-c CUSTOMER_ID] " \ "-s DHALL_CONFIG" opts.on( "-v", "--verbose", "Print to terminal, run twice to not even send to customer" ) do @verbosity += 1 end opts.on( "-n", "--dry-run", "Figure out what state they're in, but don't take action" ) do @dry_run = true end opts.on("-h", "--help", "Print this help") do puts opts exit end opts.on( "-sSOURCE", "--source=SOURCE", "Source of ports (required, repeatable)" ) do |source| @sources.append source end opts.on( "-cCUSTOMER_ID", "--customer-id=CUSTOMER_ID", "Filter by customer ID (db source only)" ) do |customer_id| @filters[:customer_id] = customer_id end end.parse! @sources = ["db", "bandwidth"] if @sources.empty? SCHEMA = "{ bandwidth : { account: Text, username: Text, password: Text }, xmpp: { jid: Text, password: Text }, notification: { endpoint: Text, source_number: Text }, pubsub: { server: Text, node: Text }, testing_tel: Text, admin_server: Text }" raise "Need a Dhall config" unless ARGV[0] CONFIG = Dhall::Coder .new(safe: Dhall::Coder::JSON_LIKE + [Symbol, Proc]) .load("#{ARGV.first} : #{SCHEMA}", transform_keys: :to_sym) require_relative "../lib/blather_notify" require_relative "../lib/expiring_lock" require_relative "../lib/form_to_h" require_relative "../lib/porting_step_repo" require_relative "../lib/port_repo" require_relative "../lib/postgres" Faraday.default_adapter = :em_synchrony BandwidthIris::Client.global_options = { account_id: CONFIG[:bandwidth][:account], username: CONFIG[:bandwidth][:username], password: CONFIG[:bandwidth][:password] } @output = case @verbosity when 0 FullAutomatic.new( BlatherNotify::PubSub::Address.new(**CONFIG[:pubsub]), CONFIG[:notification][:endpoint], CONFIG[:notification][:source_number] ) when 1 ObservedAuto.new( CONFIG[:notification][:endpoint], CONFIG[:notification][:source_number] ) else FullManual.new end PORT_SOURCES = @sources.map(&:downcase).map { |source| case source when "bandwidth" PortRepo::Bandwidth.new(@output, dry_run: @dry_run) when "db" PortRepo::Db.new(@output, dry_run: @dry_run, filters: @filters) when "fake" PortRepo::Fake.new(@output, dry_run: @dry_run) else puts <<~ERR Invalid port source (-s / --source): #{source}. Valid options: fake, db, endstream ERR # EINVAL exit! 22 end } EM.run do REDIS = EM::Hiredis.connect DB = Postgres.connect(dbname: "jmp") BlatherNotify.start( CONFIG[:xmpp][:jid], CONFIG[:xmpp][:password] ).then { EMPromise.all(PORT_SOURCES.map(&:process)) }.catch { |e| @output.error("ROOT", :catch, e) }.then { BlatherNotify.shutdown } end