Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gitlab ci 1 #4

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
image: ruby:2.4.2

stages:
- build
- test
- review
- stage
- production

variables:
DATABASE_URL: 'mongodb://mongo/user_posts'

before_script:
- cd reddit
- bundle install

build_job:
stage: build
script:
- echo 'Building'

test_unit_job:
stage: test
services:
- mongo:latest
script:
- ruby simpletest.rb
- echo 'Testing 1'

test_integration_job:
stage: test
script:
- echo 'Testing 2'

deploy_dev_job:
stage: review
script:
- echo 'Deploy'
environment:
name: dev
url: http://dev.example.com

branch review:
stage: review
script: echo "Deploy to $CI_ENVIRONMENT_SLUG"
environment:
name: branch/$CI_COMMIT_REF_NAME
url: http://$CI_ENVIRONMENT_SLUG.example.com
only:
- branches
except:
- master

staging:
stage: stage
when: manual
only:
- /^\d+\.\d+\.\d+/
script:
- echo 'Deploy'
environment:
name: beta
url: http://beta.example.com

production:
stage: production
when: manual
script:
- echo 'Deploy'
environment:
name: production
url: http://example.com
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ docker-4
2. Вынесены настройки в .env
Настройка префикса сервисов при сборке docker-compose зависит от папки, в которой расположен docker-compose.yml.
Префикс можно задать исопльзуя опцию -p, --project-name NAME

gitlab-ci-1
Выполнено:
1. На VM произведена инсталляция Gitlab CI
2. Подготовлен репозиторий с кодом приложения
3. Описаны этапы пайплайна
4. Определены окружения
15 changes: 15 additions & 0 deletions gitlab-ci/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
web:
image: 'gitlab/gitlab-ce:latest'
restart: always
hostname: 'gitlab.example.com'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://51.250.81.47'
ports:
- '80:80'
- '443:443'
- '2222:22'
volumes:
- '/srv/gitlab/config:/etc/gitlab'
- '/srv/gitlab/logs:/var/log/gitlab'
- '/srv/gitlab/data:/var/opt/gitlab'
1 change: 1 addition & 0 deletions reddit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*log
15 changes: 15 additions & 0 deletions reddit/Capfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Load DSL and set up stages
require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
install_plugin Capistrano::Puma

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
25 changes: 25 additions & 0 deletions reddit/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
source 'https://rubygems.org'

gem 'sinatra', '~> 2.0.1'
gem 'haml'
gem 'bson_ext'
gem 'bcrypt'
gem 'puma'
gem 'mongo'
gem 'json'
gem 'sinatra', '~> 2.0.1'
gem 'haml'
gem 'bson_ext'
gem 'bcrypt'
gem 'puma'
gem 'mongo'
gem 'json'
gem 'rack-test'


group :development do
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end
68 changes: 68 additions & 0 deletions reddit/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
GEM
remote: https://rubygems.org/
specs:
airbrussh (1.3.0)
sshkit (>= 1.6.1, != 1.7.0)
bcrypt (3.1.11)
bson (4.2.2)
bson_ext (1.5.1)
capistrano (3.9.0)
airbrussh (>= 1.0.0)
i18n
rake (>= 10.0.0)
sshkit (>= 1.9.0)
capistrano-bundler (1.2.0)
capistrano (~> 3.1)
sshkit (~> 1.2)
capistrano-rvm (0.1.2)
capistrano (~> 3.0)
sshkit (~> 1.2)
capistrano3-puma (3.1.1)
capistrano (~> 3.7)
capistrano-bundler
puma (~> 3.4)
haml (5.0.2)
temple (>= 0.8.0)
tilt
i18n (0.8.6)
json (2.1.0)
mongo (2.4.3)
bson (>= 4.2.1, < 5.0.0)
mustermann (1.0.2)
net-scp (1.2.1)
net-ssh (>= 2.6.5)
net-ssh (4.1.0)
puma (3.10.0)
rack (2.0.5)
rack-protection (2.0.2)
rack
rake (12.0.0)
sinatra (2.0.2)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.2)
tilt (~> 2.0)
sshkit (1.14.0)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
temple (0.8.0)
tilt (2.0.8)

PLATFORMS
ruby

DEPENDENCIES
bcrypt
bson_ext
capistrano
capistrano-bundler
capistrano-rvm
capistrano3-puma
haml
json
mongo
puma
sinatra (~> 2.0.1)

BUNDLED WITH
1.16.1
22 changes: 22 additions & 0 deletions reddit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Monolith application

## Deploy using Capistrano

#### Requirements for the target host:
* Ruby (>2.2.0) installed via `rvm`
* MongDB
* ports `22` and `9292` should be reachable by you

#### Steps:
1. Install required gems:
`bundle install`
2. Set env vars:
```bash
export SERVER_IP=<ip_address> # public IP address of the target host
export REPO_NAME=<account/name> # repo name to fetch the code from, e.g. Artemmkin/reddit
export DEPLOY_USER=deploy # username used to connect via SSH
```
3. Deploy using capistrano:
```bash
bundle exec cap production deploy:initial
```
160 changes: 160 additions & 0 deletions reddit/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
require 'sinatra'
require 'json/ext' # for .to_json
require 'haml'
require 'uri'
require 'mongo'
require 'bcrypt'
require './helpers'


configure do
db = Mongo::Client.new([ ENV['DATABASE_URL'] || '127.0.0.1:27017' ], database: 'user_posts', heartbeat_frequency: 2)
set :mongo_db, db[:posts]
set :comments_db, db[:comments]
set :users_db, db[:users]
set :bind, '0.0.0.0'
enable :sessions
end

before do
session[:flashes] = [] if session[:flashes].class != Array
end


get '/' do
@title = 'All posts'
begin
@posts = JSON.parse(settings.mongo_db.find.sort(timestamp: -1).to_a.to_json)
rescue
session[:flashes] << { type: 'alert-danger', message: 'Can\'t show blog posts, some problems with database. <a href="." class="alert-link">Refresh?</a>' }
end
@flashes = session[:flashes]
session[:flashes] = nil
haml :index
end


get '/new' do
@title = 'New post'
@flashes = session[:flashes]
session[:flashes] = nil
haml :create
end

post '/new' do
db = settings.mongo_db
if params['link'] =~ URI::regexp
begin
result = db.insert_one title: params['title'], created_at: Time.now.to_i, link: params['link'], votes: 0
db.find(_id: result.inserted_id).to_a.first.to_json
rescue
session[:flashes] << { type: 'alert-danger', message: 'Can\'t save your post, some problems with the post service' }
else
session[:flashes] << { type: 'alert-success', message: 'Post successuly published' }
end
redirect '/'
else
session[:flashes] << { type: 'alert-danger', message: 'Invalid URL' }
redirect back
end
end


get '/signup' do
@title = 'Signup'
@flashes = session[:flashes]
session[:flashes] = nil
haml :signup
end


get '/login' do
@title = 'Login'
@flashes = session[:flashes]
session[:flashes] = nil
haml :login
end


post '/signup' do
db = settings.users_db
password_salt = BCrypt::Engine.generate_salt
password_hash = BCrypt::Engine.hash_secret(params[:password], password_salt)
u = db.find(_id: params[:username]).to_a.first.to_json
if u == "null"
result = db.insert_one _id: params[:username], salt: password_salt, passwordhash: password_hash
session[:username] = params[:username]
session[:flashes] << { type: 'alert-success', message: 'User created' }
redirect '/'
else
session[:flashes] << { type: 'alert-danger', message: 'User already exists' }
redirect back
end
end


post '/login' do
db = settings.users_db
u = db.find(_id: params[:username]).to_a.first.to_json
if u != "null"
user = JSON.parse(u)
if user["passwordhash"] == BCrypt::Engine.hash_secret(params[:password], user["salt"])
session[:username] = params[:username]
redirect '/'
else
session[:flashes] << { type: 'alert-danger', message: 'Wrong username or password' }
redirect back
end
else
session[:flashes] << { type: 'alert-danger', message: 'Wrong username or password' }
redirect back
end
end


get '/logout' do
session[:username] = nil
redirect back
end


put '/post/:id/vote/:type' do
if logged_in?
id = object_id(params[:id])
post = JSON.parse(document_by_id(params[:id]))
post['votes'] += params[:type].to_i

settings.mongo_db.find(:_id => id).
find_one_and_update('$set' => {:votes => post['votes']})
document_by_id(id)
else
session[:flashes] << { type: 'alert-danger', message: 'You need to log in before you can vote' }
end
redirect back
end


get '/post/:id' do
@title = 'Post'
@post = JSON.parse(document_by_id(params[:id]))
id = object_id(params[:id])
@comments = JSON.parse(settings.comments_db.find(post_id: "#{id}").to_a.to_json)
@flashes = session[:flashes]
session[:flashes] = nil
haml :show
end


post '/post/:id/comment' do
content_type :json
db = settings.comments_db
begin
result = db.insert_one post_id: params[:id], name: session[:username], body: params['body'], created_at: Time.now.to_i
db.find(_id: result.inserted_id).to_a.first.to_json
rescue
session[:flashes] << { type: 'alert-danger', message: 'Can\'t save your comment, some problems with the comment service' }
else
session[:flashes] << { type: 'alert-success', message: 'Comment successuly published' }
end
redirect back
end
2 changes: 2 additions & 0 deletions reddit/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './app'
run Sinatra::Application
Loading