Sha256: a1177dbea6e7d4c4418fed671ea94d4d2654f3837a9649593692b16e18dac542

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module Vagrant
  module Notify
    class Server
      HTTP_RESPONSE = "Hi! You just reached the vagrant notification server"

      def self.run(env, port)
        uuid = env[:vm].uuid
        fork do
          $0 = "vagrant-notify-server (#{port})"
          tcp_server = TCPServer.open(port)
          server = self.new(uuid)
          loop {
            Thread.start(tcp_server.accept) do |client|
              server.receive_data(client)
            end
          }
        end
      end

      def initialize(uuid, env = Vagrant::Environment.new)
        @uuid = uuid
        @env  = env
      end

      def receive_data(client)
        args = read_args(client)
        if http_request?(args)
          client.puts HTTP_RESPONSE
        else
          download_icon!(args)
          system("notify-send #{args}")
        end
        client.close
      end

      private

      def read_args(client)
        ''.tap do |args|
          while tmp = client.gets and tmp !~ /^\s*$/
            args << tmp
          end
        end
      end

      def http_request?(args)
        args =~ /^GET/
      end

      def download_icon!(args)
        return unless args =~ /-i '([^']+)'/
        icon = $1
        # TODO: Handle system icons
        host_file = "/tmp/vagrant-notify-#{@uuid}-#{icon.gsub('/', '-')}"
        download(icon, host_file) unless File.exists?(host_file)
        args.gsub!(icon, host_file)
      end

      def download(icon, host_file)
        @env.vms[:default].channel.download(icon, host_file)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-notify-0.1.0 lib/vagrant-notify/server.rb