-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from splaplapla/app-rb-for-pbm-cloud
app.rb.erbを評価してapp.rbを生成する
- Loading branch information
Showing
18 changed files
with
514 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pbmenv | ||
class CreateVersionService | ||
class AlreadyCreatedError < StandardError; end | ||
class NotSupportVersionError < StandardError; end | ||
|
||
attr_accessor :version, :use_option, :enable_pbm_cloud | ||
|
||
def initialize(version: , use_option: , enable_pbm_cloud: ) | ||
self.version = version | ||
self.use_option = use_option | ||
self.enable_pbm_cloud = enable_pbm_cloud | ||
end | ||
|
||
def execute! | ||
if File.exists?("/usr/share/pbm/v#{version}") | ||
raise AlreadyCreatedError | ||
end | ||
|
||
begin | ||
source_path = download_src(version) | ||
build_app_file(source_path: source_path) | ||
create_if_miss_shared_dir | ||
create_if_miss_device_id_file | ||
link_device_id_file(version: version) | ||
create_if_miss_current_dir(version: version) | ||
rescue DownloadSrcService::DownloadError | ||
puts "Download failed. Check the version name." | ||
raise NotSupportVersionError | ||
rescue => e | ||
Helper.system_and_puts "rm -rf #{VersionPathname.new(version).version_path}" | ||
raise | ||
ensure | ||
if source_path && Dir.exists?(source_path) | ||
Helper.system_and_puts "rm -rf #{source_path}" | ||
end | ||
end | ||
|
||
return true | ||
end | ||
|
||
private | ||
|
||
# @return [String] | ||
def download_src(version) | ||
Pbmenv::DownloadSrcService.new(version).execute! | ||
return "procon_bypass_man-#{version}" | ||
end | ||
|
||
def build_app_file(source_path: ) | ||
pathname = VersionPathname.new(version) | ||
|
||
if File.exists?(File.join(source_path, "project_template/app.rb.erb")) | ||
Helper.system_and_puts <<~SHELL | ||
mkdir -p #{pathname.version_path} && | ||
cp procon_bypass_man-#{version}/project_template/app.rb.erb #{pathname.version_path}/ | ||
cp procon_bypass_man-#{version}/project_template/README.md #{pathname.version_path}/ | ||
cp procon_bypass_man-#{version}/project_template/setting.yml #{pathname.version_path}/ | ||
cp -r procon_bypass_man-#{version}/project_template/systemd_units #{pathname.version_path}/ | ||
SHELL | ||
require "./procon_bypass_man-#{version}/project_template/lib/app_generator" | ||
AppGenerator.new( | ||
prefix_path: pathname.version_path, | ||
enable_integration_with_pbm_cloud: enable_pbm_cloud, | ||
).generate | ||
Helper.system_and_puts "rm #{pathname.app_rb_erb_path}" | ||
else | ||
Helper.system_and_puts <<~SHELL | ||
mkdir -p #{pathname.version_path} && | ||
cp procon_bypass_man-#{version}/project_template/app.rb #{pathname.version_path}/ | ||
cp procon_bypass_man-#{version}/project_template/README.md #{pathname.version_path}/ | ||
cp procon_bypass_man-#{version}/project_template/setting.yml #{pathname.version_path}/ | ||
cp -r procon_bypass_man-#{version}/project_template/systemd_units #{pathname.version_path}/ | ||
SHELL | ||
end | ||
|
||
# 旧実装バージョン | ||
if enable_pbm_cloud | ||
text = File.read(pathname.app_rb_path) | ||
if text =~ /config\.api_servers\s+=\s+\['(https:\/\/.+)'\]/ && (url = $1) | ||
text.gsub!(/#\s+config\.api_servers\s+=\s+.+$/, "config.api_servers = '#{url}'") | ||
end | ||
File.write(pathname.app_rb_path, text) | ||
end | ||
end | ||
|
||
def create_if_miss_shared_dir | ||
unless File.exists?(VersionPathname.shared) | ||
Helper.system_and_puts <<~SHELL | ||
mkdir -p #{VersionPathname.shared} | ||
SHELL | ||
end | ||
end | ||
|
||
def create_if_miss_device_id_file | ||
device_id_path_in_shared = VersionPathname.device_id_path_in_shared | ||
unless File.exists?(device_id_path_in_shared) | ||
File.write(device_id_path_in_shared, "d_#{SecureRandom.uuid}") | ||
end | ||
end | ||
|
||
def link_device_id_file(version: ) | ||
pathname = VersionPathname.new(version) | ||
Helper.system_and_puts <<~SHELL | ||
ln -s #{pathname.device_id_path_in_shared} #{pathname.device_id_path_in_version} | ||
SHELL | ||
end | ||
|
||
def create_if_miss_current_dir(version: ) | ||
# 初回だけinstall時にcurrentを作成する | ||
if !File.exists?(VersionPathname.current) || use_option | ||
UseVersionService.new(version: version).execute! | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pbmenv | ||
class DestroyVersionService | ||
class VersionNotFoundError < StandardError; end | ||
|
||
attr_accessor :version | ||
|
||
def initialize(version: ) | ||
@version = version | ||
end | ||
|
||
def execute! | ||
version_pathname = VersionPathname.new(version) | ||
|
||
unless File.exists?(version_pathname.version_path) | ||
raise VersionNotFoundError | ||
end | ||
Helper.system_and_puts "rm -rf #{version_pathname.version_path}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Pbmenv | ||
class DownloadSrcService | ||
class DownloadError < StandardError; end | ||
|
||
attr_accessor :version | ||
|
||
def initialize(version) | ||
self.version = version | ||
end | ||
|
||
def execute! | ||
if ENV["DEBUG_INSTALL"] | ||
shell = <<~SHELL | ||
git clone https://github.com/splaplapla/procon_bypass_man.git procon_bypass_man-#{version} | ||
SHELL | ||
else | ||
# TODO cache for testing | ||
shell = <<~SHELL | ||
curl -L https://github.com/splaplapla/procon_bypass_man/archive/refs/tags/v#{version}.tar.gz | tar xvz > /dev/null | ||
SHELL | ||
end | ||
|
||
if Helper.system_and_puts(shell) | ||
unless File.exists?("procon_bypass_man-#{version}/project_template") | ||
raise NotSupportVersionError, "This version is not support by pbmenv" | ||
end | ||
else | ||
raise DownloadError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Pbmenv | ||
class Helper | ||
def self.system_and_puts(shell) | ||
to_stdout "[SHELL] #{shell}" | ||
system(shell) | ||
end | ||
|
||
def self.to_stdout(text) | ||
puts text | ||
end | ||
|
||
def self.normalize_version(version) | ||
/\Av?([\w.]*)\z/ =~ version && $1 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pbmenv | ||
class UseVersionService | ||
class VersionNotFoundError < StandardError; end | ||
|
||
attr_accessor :version | ||
|
||
def initialize(version: ) | ||
self.version = version | ||
end | ||
|
||
def execute! | ||
throw_error_if_has_not_version | ||
relink_current_path | ||
end | ||
|
||
private | ||
|
||
def throw_error_if_has_not_version | ||
version_pathname = VersionPathname.new(version) | ||
|
||
if !File.exists?(version_pathname.version_path_without_v) && !File.exists?(version_pathname.version_path) | ||
raise UseVersionService::VersionNotFoundError | ||
end | ||
end | ||
|
||
def relink_current_path | ||
version_pathname = VersionPathname.new(version) | ||
|
||
if File.symlink?(VersionPathname.current) | ||
Helper.system_and_puts "unlink #{VersionPathname.current}" | ||
end | ||
|
||
Helper.system_and_puts "ln -s #{version_pathname.version_path} #{VersionPathname.current}" | ||
end | ||
end | ||
end |
Oops, something went wrong.