Class: PonyHost

Inherits:
Object
  • Object
show all
Defined in:
lib/ponyhost.rb

Constant Summary collapse

S3_CREDENTIAL_FILES =
["~/.ponyhost.yml"]
"https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&action=access-key"
DEFAULT_DOMAIN =
"ponyho.st"
VERSION =
"0.3.4"

Class Method Summary collapse

Class Method Details

.create(bucketname) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ponyhost.rb', line 42

def create(bucketname)
  bucket = AWS::S3::Bucket.create(bucketname, :access => :public_read)  

  body = '<?xml version="1.0" encoding="UTF-8"?>
  <WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <IndexDocument><Suffix>index.html</Suffix></IndexDocument>
    <ErrorDocument><Key>404.html</Key></ErrorDocument>
  </WebsiteConfiguration>'

  res = AWS::S3::Base.request(:put, "/#{bucketname}?website", {}, body)

  puts "Site created: http://#{bucketname}"
  puts "Push your files with: ponyhost push #{bucketname}"
end

.destroy(bucketname) ⇒ Object



77
78
79
80
# File 'lib/ponyhost.rb', line 77

def destroy(bucketname)
  AWS::S3::Bucket.delete(bucketname, :force => true)
  puts "'#{bucketname} is destroyed"
end

.flat_list_directory(dir, path = "") ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ponyhost.rb', line 95

def flat_list_directory(dir, path = "")
  list = []
  dir.each do |entry|
    unless [".", "..", ".git"].include?(entry)
      full_entry_path = path == "" ? entry : [path, entry].join("/")
      if File.directory?(full_entry_path)
        list += flat_list_directory(Dir.new(full_entry_path), full_entry_path)
      else
        list << full_entry_path
      end
    end
  end
  list
end

.listObject



73
74
75
# File 'lib/ponyhost.rb', line 73

def list
  AWS::S3::Service.buckets.map(&:name)
end

.md5sum(file_name) ⇒ Object



91
92
93
# File 'lib/ponyhost.rb', line 91

def md5sum(file_name)
  Digest::MD5.hexdigest(File.read(file_name))
end

.normalize_bucketname(bucketname) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/ponyhost.rb', line 110

def normalize_bucketname(bucketname)
  if bucketname.include?(".")
    bucketname
  else
    "#{bucketname}.#{DEFAULT_DOMAIN}"
  end
end

.obtain_credentialsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ponyhost.rb', line 23

def obtain_credentials
  credential_file = File.expand_path(S3_CREDENTIAL_FILES.first)
  if File.exists?(credential_file)
    return YAML.load_file(credential_file)
  else
    puts "AWS Credentials file '#{credential_file}' is missing."
    puts "Please insert your Amazon AWS S3 credentials. You can look them up on http://j.mp/aws-keys"
    puts "In case you don't have signed up for S3 yet you can do that on http://j.mp/s3-signup"

    credentials = {}
    print "Your AWS Access Key ID: "
    credentials[:access_key_id] = STDIN.gets.chop
    print "Your AWS Access Key Secret: "
    credentials[:access_key_secret] = STDIN.gets.chop
    File.open(credential_file, "w") {|file| file.puts(credentials.to_yaml) }
    return credentials
  end
end

.push(bucketname, directory = ".") ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ponyhost.rb', line 57

def push(bucketname, directory = ".")
  bucket = AWS::S3::Bucket.find(bucketname)
  file_names = flat_list_directory(Dir.new(directory))

  file_names.each do |file_name|
    local_md5 = md5sum(file_name).chomp    
    remote_md5 = bucket[file_name] && bucket[file_name].about["etag"].gsub('"', '').chomp
    if local_md5.to_s == remote_md5.to_s
      puts "Skipping \t#{file_name}"
    else
      puts "Pushing \t#{file_name}"
      AWS::S3::S3Object.store(file_name, open(file_name), bucketname,  :access => :public_read)
    end
  end  
end

.server(port = 9090) ⇒ Object



87
88
89
# File 'lib/ponyhost.rb', line 87

def server(port=9090)      
  s = WEBrick::HTTPServer.new(:Port => port,  :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start
end

.show(bucketname) ⇒ Object



82
83
84
85
# File 'lib/ponyhost.rb', line 82

def show(bucketname)
  res = AWS::S3::Base.request(:get, "/#{bucketname}?website") rescue res = $!
  puts res  
end