Skip to content

Commit

Permalink
Merge pull request #290 from splaplapla/support-bundle-exec
Browse files Browse the repository at this point in the history
pbmenvからインストールしたときにrootを使わずに起動できるようにする
  • Loading branch information
jiikko authored May 31, 2024
2 parents b37a789 + a98f22b commit d77f9c2
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
tag: 3.0.1
steps:
- checkout
- run: ruby bin/generate_default_app && git diff --exit-code project_template/app.rb
- run: ruby bin/build_project_template && git diff --exit-code project_template/app.rb project_template/Gemfile

bundle_install:
parameters:
Expand Down
20 changes: 20 additions & 0 deletions bin/build_project_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env ruby

# build app.rb
require "./project_template/lib/app_generator"
AppGenerator.new(
prefix_path: "./project_template",
enable_integration_with_pbm_cloud: false,
).generate

puts "Created project_template/app.rb"


# build Gemfile
require "erb"
match = (/=\s+?"([\d\.]*)"/).match(File.read('./lib/procon_bypass_man/version.rb'))
body_erb = File.read('project_template/Gemfile.erb')
pbm_version = pbm_version = match[1]
body = ERB.new(body_erb, trim_mode: '-').result(binding)
File.write('project_template/Gemfile', body)
puts "Created project_template/Gemfile"
10 changes: 0 additions & 10 deletions bin/generate_default_app

This file was deleted.

1 change: 1 addition & 0 deletions project_template/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0.1
7 changes: 7 additions & 0 deletions project_template/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

# if you want to use the latest version, please use the following line.
# gem 'procon_bypass_man', github: 'splaplapla/procon_bypass_man', branch: "master"
gem 'procon_bypass_man', '0.3.12'
7 changes: 7 additions & 0 deletions project_template/Gemfile.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

# if you want to use the latest version, please use the following line.
# gem 'procon_bypass_man', github: 'splaplapla/procon_bypass_man', branch: "master"
gem 'procon_bypass_man', '<%= pbm_version %>'
59 changes: 32 additions & 27 deletions project_template/app.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
#!/usr/bin/env ruby

require 'bundler/inline'
if !ENV['RUN_WITH_BUNDLE_EXEC']
require 'bundler/inline'

retry_count_on_git_command_error = 0
begin
if retry_count_on_git_command_error > 10
STDOUT.puts "Stopped the procon_bypass_man program because could not download any source codes."
exit 1
end
retry_count_on_git_command_error = 0
begin
if retry_count_on_git_command_error > 10
STDOUT.puts "Stopped the procon_bypass_man program because could not download any source codes."
exit 1
end

gemfile do
source 'https://rubygems.org'
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'procon_bypass_man', '0.3.12'
# uncomment if you want to use master branch
# gem 'procon_bypass_man', github: 'splaplapla/procon_bypass_man', branch: 'master'
# uncomment if you want to use serial communication feature
# gem "serialport"
end
rescue Bundler::Source::Git::GitCommandError => e
retry_count_on_git_command_error = retry_count_on_git_command_error + 1
sleep(5) # サービスの起動順によっては、まだoffline状態なので待機する
gemfile do
source 'https://rubygems.org'
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'procon_bypass_man', '0.3.12'
# uncomment if you want to use master branch
# gem 'procon_bypass_man', github: 'splaplapla/procon_bypass_man', branch: 'master'
# uncomment if you want to use serial communication feature
# gem "serialport"
end
rescue Bundler::Source::Git::GitCommandError => e
retry_count_on_git_command_error = retry_count_on_git_command_error + 1
sleep(5) # サービスの起動順によっては、まだoffline状態なので待機する

# install中に強制終了するとgitの管理ファイルが不正状態になり、次のエラーが起きるので発生したらcache directoryを削除する
#"Git error: command `git fetch --force --quiet --tags https://github.com/splaplapla/procon_bypass_man refs/heads/\\*:refs/heads/\\*` in directory /home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af has failed.\n\nIf this error persists you could try removing the cache directory '/home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af'"
if /try removing the cache directory '([^']+)'/ =~ e.message && $1&.start_with?('/home/pi/.rbenv')
require 'fileutils'
FileUtils.rm_rf($1)
STDOUT.puts "Deleted #{$1}"
end
# install中に強制終了するとgitの管理ファイルが不正状態になり、次のエラーが起きるので発生したらcache directoryを削除する
#"Git error: command `git fetch --force --quiet --tags https://github.com/splaplapla/procon_bypass_man refs/heads/\\*:refs/heads/\\*` in directory /home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af has failed.\n\nIf this error persists you could try removing the cache directory '/home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af'"
if /try removing the cache directory '([^']+)'/ =~ e.message && $1&.start_with?('/home/pi/.rbenv')
require 'fileutils'
FileUtils.rm_rf($1)
STDOUT.puts "Deleted #{$1}"
end

retry
retry
end
else
require 'bundler'
Bundler.require(:default)
end

ProconBypassMan.configure do |config|
Expand Down
59 changes: 32 additions & 27 deletions project_template/app.rb.erb
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
#!/usr/bin/env ruby

require 'bundler/inline'
if !ENV['RUN_WITH_BUNDLE_EXEC']
require 'bundler/inline'

retry_count_on_git_command_error = 0
begin
if retry_count_on_git_command_error > 10
STDOUT.puts "Stopped the procon_bypass_man program because could not download any source codes."
exit 1
end
retry_count_on_git_command_error = 0
begin
if retry_count_on_git_command_error > 10
STDOUT.puts "Stopped the procon_bypass_man program because could not download any source codes."
exit 1
end

gemfile do
source 'https://rubygems.org'
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'procon_bypass_man', '0.3.12'
# uncomment if you want to use master branch
# gem 'procon_bypass_man', github: 'splaplapla/procon_bypass_man', branch: 'master'
# uncomment if you want to use serial communication feature
# gem "serialport"
end
rescue Bundler::Source::Git::GitCommandError => e
retry_count_on_git_command_error = retry_count_on_git_command_error + 1
sleep(5) # サービスの起動順によっては、まだoffline状態なので待機する
gemfile do
source 'https://rubygems.org'
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem 'procon_bypass_man', '0.3.12'
# uncomment if you want to use master branch
# gem 'procon_bypass_man', github: 'splaplapla/procon_bypass_man', branch: 'master'
# uncomment if you want to use serial communication feature
# gem "serialport"
end
rescue Bundler::Source::Git::GitCommandError => e
retry_count_on_git_command_error = retry_count_on_git_command_error + 1
sleep(5) # サービスの起動順によっては、まだoffline状態なので待機する

# install中に強制終了するとgitの管理ファイルが不正状態になり、次のエラーが起きるので発生したらcache directoryを削除する
#"Git error: command `git fetch --force --quiet --tags https://github.com/splaplapla/procon_bypass_man refs/heads/\\*:refs/heads/\\*` in directory /home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af has failed.\n\nIf this error persists you could try removing the cache directory '/home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af'"
if /try removing the cache directory '([^']+)'/ =~ e.message && $1&.start_with?('/home/pi/.rbenv')
require 'fileutils'
FileUtils.rm_rf($1)
STDOUT.puts "Deleted #{$1}"
end
# install中に強制終了するとgitの管理ファイルが不正状態になり、次のエラーが起きるので発生したらcache directoryを削除する
#"Git error: command `git fetch --force --quiet --tags https://github.com/splaplapla/procon_bypass_man refs/heads/\\*:refs/heads/\\*` in directory /home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af has failed.\n\nIf this error persists you could try removing the cache directory '/home/pi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/cache/bundler/git/procon_bypass_man-ae4c9016d76b667658c8ba66f3bbd2eebf2656af'"
if /try removing the cache directory '([^']+)'/ =~ e.message && $1&.start_with?('/home/pi/.rbenv')
require 'fileutils'
FileUtils.rm_rf($1)
STDOUT.puts "Deleted #{$1}"
end

retry
retry
end
else
require 'bundler'
Bundler.require(:default)
end

ProconBypassMan.configure do |config|
Expand Down
25 changes: 25 additions & 0 deletions project_template/exe/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

# rootで起動していた影響で一般ユーザで起動するには権限を修正しないといけない
EXECUINTG_USER="$(whoami)"
if find /usr/share/pbm/ -user root | read; then
sudo chown "$EXECUINTG_USER:$EXECUINTG_USER" -R /usr/share/pbm
echo "We have recovered /usr/share/pbm/* permissions"
fi

if [ -e /tmp/pbm_blue_green_process_pids ]; then
if find /tmp/pbm_blue_green_process_pids -user root | read; then
sudo chown "$EXECUINTG_USER:$EXECUINTG_USER" /tmp/pbm_blue_green_process_pids
echo "We have recovered /tmp/pbm_blue_green_process_pids permissions"
fi
fi

if find "/home/$EXECUINTG_USER/.rbenv/" -user root | read; then
sudo chown "$EXECUINTG_USER:$EXECUINTG_USER" "/home/$EXECUINTG_USER/.rbenv/"
echo "We have recovered /home/$EXECUINTG_USER/.rbenv/"
fi

bundle check > /dev/null || bundle install
RUN_WITH_BUNDLE_EXEC=1 bundle exec ruby app.rb

0 comments on commit d77f9c2

Please sign in to comment.