Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit dedc4d6c959f1b14b1e14cc3258391242e873d3c Dominique Rose-Rosette committed Sep 5, 2012
Showing with 308 additions and 0 deletions.
  1. +18 −0 .gitignore
  2. +4 −0 Gemfile
  3. +22 −0 LICENSE
  4. +40 −0 README.md
  5. +2 −0 Rakefile
  6. +6 −0 bin/desi
  7. +20 −0 desi.gemspec
  8. +5 −0 lib/desi.rb
  9. +29 −0 lib/desi/downloader.rb
  10. +41 −0 lib/desi/http_client.rb
  11. +27 −0 lib/desi/local_install.rb
  12. +37 −0 lib/desi/registry.rb
  13. +54 −0 lib/desi/runner.rb
  14. +3 −0 lib/desi/version.rb
@@ -0,0 +1,18 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
+.rvmrc
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in desi.gemspec
+gemspec
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Dominique Rose-Rosette
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
+# Desi
+
+Desi (Developper ElasticSearch Installer) is very simple tool to quickly set up
+an [Elastic Search](http://www.elasticsearch.org/) local install for
+development purposes. It will download and install ElasticSearch (the latest
+version by default) and let you start/stop/restart it.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'desi'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install desi
+
+## Usage
+
+ $ desi list # List locally installed ElasticSearch versions
+ $ desi list_all # List all available Elastic Search versions
+ $ desi install # Install latest stable version
+ $ desi install-latest # Install latest version
+ $ desi install --version VERSION # Install a specific version
+ $ desi upgrade # Upgrade to latest ElasticSearch version
+ $ desi start # Start or restart Elastic Search
+ $ desi stop # Stop Elastic Search
+ $ desi switch VERSION # Switch currently active ES version to VERSION
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+# vim: se ft=ruby :
+
+require 'desi/runner'
+
+Desi::Runner.start
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/desi/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.name = "desi"
+ gem.authors = ["Dominique Rose-Rosette"]
+ gem.email = ["drose-rosette@af83.com"]
+ gem.description = %q{TODO: Write a gem description}
+ gem.summary = %q{TODO: Write a gem summary}
+ gem.homepage = ""
+ gem.version = Desi::VERSION
+
+ gem.add_dependency "boson"
+ gem.add_dependency "httpclient"
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+end
@@ -0,0 +1,5 @@
+require "desi/version"
+
+module Desi
+ # Your code goes here...
+end
@@ -0,0 +1,29 @@
+# encoding: utf-8
+
+require "pathname"
+require "desi/local_install"
+require "desi/http_client"
+require "uri"
+
+module Desi
+ class Downloader
+
+ def initialize(destination_dir = nil, host = 'http://cloud.github.com/')
+ @destination_dir = Pathname(destination_dir || Desi::LocalInstall.new)
+ @host = URI(host)
+ @client = Desi::HttpClient.new(@host)
+ end
+
+ def download!(version, opts = {})
+ path = "/downloads/elasticsearch/elasticsearch/#{version.name}"
+ destination_name = @destination_dir.join File.basename(version.name)
+
+ raise "ERROR: File #{destination_name} already present!" if destination_name.exist?
+
+ puts "* fetching release #{version} from #{@host + path}"
+
+ File.open(destination_name, 'w') {|f| f << @client.get(path).body }
+ end
+
+ end
+end
@@ -0,0 +1,41 @@
+# encoding: utf-8
+
+require "net/https"
+require "uri"
+require "json"
+
+module Desi
+
+ class HttpClient
+ def initialize(uri)
+ @uri = URI(uri)
+
+ case @uri.scheme
+ when 'https'
+ @http = ::Net::HTTP.new(@uri.host, 443)
+ @http.use_ssl = true
+ @http.verify_mode = ::OpenSSL::SSL::VERIFY_PEER
+ when 'http'
+ @http = ::Net::HTTP.new(@uri.host, 80)
+ else
+ raise ArgumentError, "Won't process scheme #{@uri.scheme}"
+ end
+ end
+
+ def get(uri, limit = 5)
+ raise "Too many HTTP redirects!" if limit <= 0
+
+ response = @http.request(Net::HTTP::Get.new(uri))
+
+ case response
+ when Net::HTTPSuccess
+ response
+ when Net::HTTPRedirection
+ fetch(response['location'], limit - 1)
+ else
+ raise response.error!
+ end
+ end
+ end
+
+end
@@ -0,0 +1,27 @@
+# encoding: utf-8
+
+module Desi
+ class LocalInstall
+ DEFAULT_DIR = '~/elasticsearch'
+
+ def initialize(workdir = DEFAULT_DIR)
+ @workdir = Pathname(File.expand_path(workdir))
+ end
+
+ def exists?
+ @workdir.exist?
+ end
+
+ def create!
+ FileUtils.mkdir_p @workdir
+ end
+
+ def versions
+ Dir[@workdir.join('*')].select {|subdir| File.directory?(subdir) && File.basename(subdir) =~ /^elasticsearch\-\d+\.\d+\.\d+/ }
+ end
+
+ def to_path
+ @workdir.to_s
+ end
+ end
+end
@@ -0,0 +1,37 @@
+# encoding: utf-8
+
+require "desi/http_client"
+require "httpclient"
+
+module Desi
+ class Registry
+
+ class Version < Struct.new(:name, :description, :release_date, :download_url)
+ def to_s
+ self.name
+ end
+ end
+
+ def initialize
+ @client = Desi::HttpClient.new('https://api.github.com/')
+ end
+
+ def releases
+ @releases ||= fetch_releases.
+ select {|v| v['content_type'] == 'application/gzip' }.
+ sort {|a,b| b["name"] <=> a['name'] }.
+ map {|v| Version.new(v['name'], v['description'], v['created_at'], v['html_url']) }
+ end
+
+ def latest_release
+ releases.first
+ end
+
+ private
+
+ def fetch_releases
+ JSON.parse @client.get('/repos/elasticsearch/elasticsearch/downloads').body
+ end
+
+ end
+end
@@ -0,0 +1,54 @@
+# encoding: utf-8
+
+require 'boson/runner'
+require "pp"
+
+module Desi
+ class Runner < Boson::Runner
+
+ desc "List locally installed Elastic Search versions"
+ def list
+ require 'desi/local_install'
+ puts "Local ES installs:"
+ Desi::LocalInstall.new.versions.each do |v|
+ puts "* #{v}"
+ end
+ end
+
+ desc "List all available ElasticSearch versions"
+ def list_all
+ require 'desi/registry'
+ Desi::Registry.new.releases.each do |v|
+ puts " * #{v.name} -- #{v.description} (#{v.release_date})"
+ end
+ end
+
+ # option :version, :type => :string #, desc: "Version (latest stable by default)"
+ desc "Install ES (to latest stable version by default)"
+ def install
+ puts " * fetching latest version"
+ require 'desi/registry'
+ require 'desi/downloader'
+ release = Desi::Registry.new.latest_release
+ Desi::Downloader.new.download!(release)
+ end
+
+ desc "Upgrade to latest ElasticSearch version"
+ def upgrade
+ end
+
+ desc "Start or restart Elastic Search"
+ def start
+ end
+
+ desc "Stop Elastic Search"
+ def stop
+ end
+
+ desc "Switch currently active ES version to VERSION"
+ option :version, type: :string
+ def switch
+ end
+
+ end
+end
@@ -0,0 +1,3 @@
+module Desi
+ VERSION = "0.0.1"
+end

0 comments on commit dedc4d6

Please sign in to comment.