Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Oct 16, 2014
1 parent 3a07e82 commit ca9acb5
Show file tree
Hide file tree
Showing 36 changed files with 708 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
*.bundle
*.so
*.o
*.a
mkmf.log
.idea
gemfiles/*.gemfile.lock
5 changes: 5 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--color
--tty
--format progress
--order random
--backtrace
7 changes: 7 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
appraise 'rails3.1' do
gem 'rails', '~> 3.1.0'
end

appraise 'rails3.2' do
gem 'rails', '~> 3.2.0'
end
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

85 changes: 85 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
FROM ubuntu:trusty
MAINTAINER Merkushin Michail <[email protected]>

ENV DEBIAN_FRONTEND noninteractive

# ensure UTF-8
RUN locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

# YEKT locale
RUN mv /etc/localtime /etc/localtime-old
RUN ln -sf /usr/share/zoneinfo/Asia/Yekaterinburg /etc/localtime

# add apt repositories
RUN echo "deb http://archive.ubuntu.com/ubuntu/ trusty main universe" > /etc/apt/sources.list
RUN echo "deb http://archive.ubuntu.com/ubuntu/ trusty-updates main universe" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y -q upgrade

# create init script
RUN touch /root/init
RUN echo "#!/bin/sh" >> /root/init
RUN echo "set -x" >> /root/init
RUN chmod +x /root/init

# gem env
ENV GEM_HOME /usr/local/gems
ENV GEM_PATH /usr/local/gems
ENV PATH $GEM_PATH/bin:$PATH

# install curl and git
RUN apt-get install -y -q curl git vim

# install time command
RUN apt-get -y -q install time

# add GitHub to known hosts
RUN ssh-keyscan -H github.com >> /etc/ssh/ssh_known_hosts

# install ruby
ENV RVM_PATH /usr/local/rvm
ENV RVM ${RVM_PATH}/bin/rvm
ENV RVM_RUBY ruby-1.9.3-p545
RUN curl -L https://get.rvm.io | bash -s stable
RUN $RVM install $RVM_RUBY --patch railsexpress
ENV PATH ${RVM_PATH}/rubies/${RVM_RUBY}/bin:$PATH

# project deps
RUN apt-get install -y -q libxslt-dev libxml2-dev
RUN apt-get install -y -q libcurl3-dev
RUN apt-get install -y -q libpq-dev

# dnsmasq
RUN apt-get install -y -q dnsmasq-base dnsutils
RUN mkdir /etc/dnsmasq.d
RUN echo "address=/dev/127.0.0.1" >> /etc/dnsmasq.d/0hosts
RUN echo '$(which dnsmasq) -C /etc/dnsmasq.conf' >> /root/init
# dnsmasq configuration
RUN echo 'user=root' >> /etc/dnsmasq.conf
RUN echo 'listen-address=127.0.0.1' >> /etc/dnsmasq.conf
RUN echo 'resolv-file=/etc/resolv.dnsmasq.conf' >> /etc/dnsmasq.conf
RUN echo 'conf-dir=/etc/dnsmasq.d' >> /etc/dnsmasq.conf
# google dns
RUN echo 'nameserver 8.8.8.8' >> /etc/resolv.dnsmasq.conf
RUN echo 'nameserver 8.8.4.4' >> /etc/resolv.dnsmasq.conf

# postgres
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" > /etc/apt/sources.list.d/pgdg.list
RUN apt-get update
RUN apt-get -y -q install postgresql-9.3 postgresql-client-9.3 postgresql-plperl-9.3
RUN pg_dropcluster --stop 9.3 main
RUN pg_createcluster --locale $LC_ALL --start 9.3 main
RUN echo "sudo -u postgres /etc/init.d/postgresql start" >> /root/init
RUN echo "host all all 0.0.0.0/0 trust" > /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "host all all ::1/128 trust" > /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "local all all trust" >> /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
RUN echo "ssl=false" >> /etc/postgresql/9.3/main/postgresql.conf

# project db
RUN /etc/init.d/postgresql start &&\
sudo -u postgres psql -c "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
sudo -u postgres createdb -O docker docker
11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'http://apress:[email protected]'
source 'https://rubygems.org'

gem 'aws-s3', '~> 0.6.2', github: 'JeremyGeros/aws-s3'

group :development, :test do
gem 'combustion', github: 'abak-press/combustion'
end

# Specify your gem's dependencies in apress-amazon_assets.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2014 bibendi

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.
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
RAILS_ENV = test
BUNDLE = RAILS_ENV=${RAILS_ENV} bundle
BUNDLE_OPTIONS = -j 2
RSPEC = rspec
APPRAISAL = appraisal

all: test

test: config/database bundler/install appraisal/install
${BUNDLE} exec ${APPRAISAL} ${RSPEC} spec 2>&1

config/database:
touch spec/internal/config/database.yml
echo 'test:' >> spec/internal/config/database.yml
echo ' adapter: postgresql' >> spec/internal/config/database.yml
echo ' database: docker' >> spec/internal/config/database.yml
echo ' username: docker' >> spec/internal/config/database.yml
echo ' host: localhost' >> spec/internal/config/database.yml
echo ' min_messages: warning' >> spec/internal/config/database.yml

bundler/install:
if ! gem list bundler -i > /dev/null; then \
gem install bundler; \
fi
${BUNDLE} install ${BUNDLE_OPTIONS}

appraisal/install:
${BUNDLE} exec ${APPRAISAL} install
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Apress::AmazonAssets

[![Dolly](http://dolly.railsc.ru/badges/abak-press/apress-amazon_assets/master)](http://dolly.railsc.ru/projects/30/builds/latest/?ref=master)
[![Code Climate](https://codeclimate.com/repos/543e65e6e30ba041d401e98d/badges/fd82abeaab68a66be573/gpa.svg)](https://codeclimate.com/repos/543e65e6e30ba041d401e98d/feed)

Гем позволяет хранить файлы на Amazon S3.

Содержит в себе две модели: `Apress::AmazonAssets::PrivateAsset` и `Apress::AmazonAssets::PublicAsset`.

Отличие между ними в том, что в публичной файлы можно просматривать по прямой ссылке, а в другой только секретной.

## Installation

Add this line to your application's Gemfile:

gem 'apress-amazon_assets'

And then execute:

$ bundle

Or install it yourself as:

$ gem install apress-amazon_assets

## Usage

В проекте необходимо создать конфиг config/amazon_s3.yml

```yml
production:
bucket: amazon_assets
access_key_id: AKIAJR44XLNRQVCSDJTQ
secret_access_key: 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
```
Модель, хранящая в себе приложенные файлы app/models/name.rb
```ruby
class Name < ActiveRecord::Base
has_many :attachments, :class_name => "Apress::AmazonAssets::PrivateAsset", :as => :attachable, :dependent => :destroy, :limit => 3
end
```

Вначале файлы загружаются локально на сервер и потом через resque загружаются на амазон. Очередь resque называется `upload`.

В проектном crontab необходимо вызывать автоочистку через rake задачу

```
rake amazon:private_assets:autoclean
rake amazon:public_assets:autoclean
```

## Gem Releasing

1. должен быть настроен git remote upstream и должны быть права на push
1. git checkout master
2. git pull upstream master
3. правим версию гема в файле VERSION в корне гема. (читаем правила версионирования http://semver.org/)
4. bundle exec rake release

## Contributing

1. Fork it ( https://github.com/[my-github-username]/apress-amazon_assets/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

require 'apress/gems/rake_tasks'
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
26 changes: 26 additions & 0 deletions app/jobs/amazon_s3_upload_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
require 'resque-integration'

class AmazonS3UploadJob
include Resque::Integration

queue :upload
unique { |id, class_name| [id, class_name] }

# Запуск загрузки на амазон
#
# id - Integer
# class_name - String
#
# Returns nothing
def self.execute(id, class_name)
asset = class_name.constantize.find(id)
file = asset.local.to_file(:original)
asset.remote = file
asset.save!
ensure
if defined?(file) && file && file.respond_to?(:close)
file.close
end
end
end
Loading

0 comments on commit ca9acb5

Please sign in to comment.