on_container 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: e6c6b69a780d2c6b4f874f5e5ba73c5cd466cefaa12417387e17c56450feba6d
4
- data.tar.gz: 3c0ad63961d07fdc00f3903346680270b51f26c5d30c3f2cd4fb53e63a065190
2
+ SHA1:
3
+ metadata.gz: 20a0354095f53ceb65e945d63fb79c4daaac2c20
4
+ data.tar.gz: bcf5aa4c31eeeb52a7ac97ecbfa8f9fa4ee7ca4c
5
5
  SHA512:
6
- metadata.gz: f250e92ac740599c98efbce04aefd437fae5a29d59bf3e0ff3334e7672391088c72ff4f7119878d7eb36dbe55cefaa594286095731c09247f97107cc4c04d572
7
- data.tar.gz: 7816fdf82eef22a8fd907bf89dbfaf3e1866f68b131529638e94f8bd661a0d51401ac36095cfaeb124cde2fe90109d30b96bd67202fa117513f810b18f6aa00b
6
+ metadata.gz: ce35b61eb314e07871f4e2004dedaff36c3c64d3abcd3e303b43d33c1a079ab115ea9ae8921cf07601c7275a678783d6fb6c28649b985937abe621703ccde6bf
7
+ data.tar.gz: 51f7dee953ae4692733673f72cd0c87ed08ec32b341f82e3baa767ae5cdc2c4aab1791a3239b829af842d7438d277a34ab36994dcb9a36aac0af9e1feeb78f32
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ module OnContainer
5
+ module Dev
6
+ module ActiveRecordOps
7
+ def app_setup_wait
8
+ ENV.fetch('APP_SETUP_WAIT', '5').to_i
9
+ end
10
+
11
+ def parse_activerecord_config_file
12
+ require 'erb'
13
+ require 'yaml'
14
+
15
+ database_yaml = Pathname.new File.expand_path('config/database.yml')
16
+ loaded_yaml = YAML.load(ERB.new(database_yaml.read).result) || {}
17
+ shared = loaded_yaml.delete('shared')
18
+
19
+ loaded_yaml.each { |_k, values| values.reverse_merge!(shared) } if shared
20
+ Hash.new(shared).merge(loaded_yaml)
21
+ end
22
+
23
+ def activerecord_config
24
+ @activerecord_config ||= parse_activerecord_config_file
25
+ .fetch ENV.fetch('RAILS_ENV', 'development')
26
+ end
27
+
28
+ def establish_activerecord_database_connection
29
+ unless defined?(ActiveRecord)
30
+ require 'rubygems'
31
+ require 'bundler'
32
+
33
+ Bundler.setup(:default)
34
+
35
+ require 'active_record'
36
+ end
37
+
38
+ ActiveRecord::Base.establish_connection activerecord_config
39
+ ActiveRecord::Base.connection_pool.with_connection { |connection| }
40
+ end
41
+
42
+ def activerecord_database_initialized?
43
+ ActiveRecord::Base.connection_pool.with_connection do |connection|
44
+ connection.data_source_exists? :schema_migrations
45
+ end
46
+ end
47
+
48
+ def activerecord_database_ready?
49
+ connection_tries ||= 3
50
+
51
+ establish_activerecord_database_connection
52
+ activerecord_database_initialized?
53
+
54
+ rescue PG::ConnectionBad
55
+ unless (connection_tries -= 1).zero?
56
+ puts "Retrying DB connection #{connection_tries} more times..."
57
+ sleep app_setup_wait
58
+ retry
59
+ end
60
+ false
61
+
62
+ rescue ActiveRecord::NoDatabaseError
63
+ false
64
+ end
65
+
66
+ def setup_activerecord_database
67
+ system 'rails db:setup'
68
+ end
69
+ end
70
+ end
71
+ end
72
+
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnContainer
4
+ module Dev
5
+ module BundlerOps
6
+ def bundle_path
7
+ '/usr/local/bundle'
8
+ end
9
+
10
+ def bundle_owner_id
11
+ File.stat(bundle_path).uid
12
+ end
13
+
14
+ def current_user_id
15
+ Etc.getpwuid.uid
16
+ end
17
+
18
+ def bundle_belongs_to_current_user?
19
+ bundle_owner_id == current_user_id
20
+ end
21
+
22
+ def make_bundle_belong_to_current_user
23
+ target_ownership = "#{current_user_id}:#{current_user_id}"
24
+ system "sudo chown -R #{target_ownership} #{bundle_path}"
25
+ end
26
+
27
+ def ensure_bundle_belongs_to_current_user
28
+ return if bundle_belongs_to_current_user?
29
+
30
+ make_bundle_belong_to_current_user
31
+ end
32
+
33
+ def ensure_project_gems_are_installed
34
+ ensure_bundle_belongs_to_current_user
35
+
36
+ system 'bundle check || bundle install'
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnContainer
4
+ module Dev
5
+ module ContainerCommandOps
6
+ def set_given_or_default_command
7
+ ARGV.concat %w[rails server -p 3000 -b 0.0.0.0] if ARGV.empty?
8
+ end
9
+
10
+ def execute_given_or_default_command
11
+ exec(*ARGV)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'etc'
4
+
5
+ module OnContainer
6
+ module Dev
7
+ module NodeModulesOps
8
+ APP_PATH = File.expand_path '.'
9
+
10
+ def node_modules_path
11
+ "#{APP_PATH}/node_modules"
12
+ end
13
+
14
+ def node_modules_owner_id
15
+ File.stat(node_modules_path).uid
16
+ end
17
+
18
+ def current_user_id
19
+ Etc.getpwuid.uid
20
+ end
21
+
22
+ def node_modules_belong_to_current_user?
23
+ node_modules_owner_id == current_user_id
24
+ end
25
+
26
+ def make_node_modules_belong_to_current_user
27
+ target_ownership = "#{current_user_id}:#{current_user_id}"
28
+ system "sudo chown -R #{target_ownership} #{node_modules_path}"
29
+ end
30
+
31
+ def ensure_node_modules_belong_to_current_user
32
+ return if node_modules_belong_to_current_user?
33
+
34
+ make_node_modules_belong_to_current_user
35
+ end
36
+
37
+ def ensure_project_node_packages_are_installed
38
+ ensure_node_modules_belong_to_current_user
39
+
40
+ system 'yarn check --integrity || yarn install'
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'on_container/dev/rails_ops'
4
+ require 'on_container/dev/setup_ops'
5
+ require 'on_container/dev/bundler_ops'
6
+ require 'on_container/dev/node_modules_ops'
7
+ require 'on_container/dev/active_record_ops'
8
+ require 'on_container/dev/container_command_ops'
9
+
10
+ include OnContainer::Dev::RailsOps
11
+ include OnContainer::Dev::SetupOps
12
+ include OnContainer::Dev::BundlerOps
13
+ include OnContainer::Dev::NodeModulesOps
14
+ include OnContainer::Dev::ActiveRecordOps
15
+ include OnContainer::Dev::ContainerCommandOps
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnContainer
4
+ module Dev
5
+ module RailsOps
6
+ def remove_rails_pidfile
7
+ system "rm -rf #{File.expand_path('tmp/pids/server.pid')}"
8
+ end
9
+
10
+ def rails_server?
11
+ ARGV[0] == 'rails' && %w[server s].include?(ARGV[1])
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnContainer
4
+ module Dev
5
+ module SetupOps
6
+ APP_PATH = File.expand_path '.'
7
+
8
+ def app_temp_path; "#{APP_PATH}/tmp"; end
9
+ def app_setup_wait; ENV.fetch('APP_SETUP_WAIT', '5').to_i; end
10
+ def app_setup_lock_path; "#{app_temp_path}/setup.lock"; end
11
+
12
+ def lock_setup
13
+ system "mkdir -p #{app_temp_path} && touch #{app_setup_lock_path};"
14
+ end
15
+
16
+ def unlock_setup
17
+ system "rm -rf #{app_setup_lock_path}"
18
+ end
19
+
20
+ def wait_setup
21
+ puts 'Waiting for app setup to finish...'
22
+ sleep app_setup_wait
23
+ end
24
+
25
+ def on_setup_lock_acquired
26
+ wait_setup while File.exist?(app_setup_lock_path)
27
+
28
+ lock_setup
29
+ yield
30
+ unlock_setup
31
+ end
32
+
33
+ def command_requires_setup?
34
+ %w[
35
+ rails rspec sidekiq hutch puma rake webpack webpack-dev-server
36
+ ].include?(ARGV[0])
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OnContainer
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -0,0 +1,5 @@
1
+ RSpec.describe OnContainer do
2
+ it "has a version number" do
3
+ expect(OnContainer::VERSION).not_to be nil
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require "bundler/setup"
2
+ require "on_container"
3
+
4
+ RSpec.configure do |config|
5
+ # Enable flags like --only-failures and --next-failure
6
+ config.example_status_persistence_file_path = ".rspec_status"
7
+
8
+ # Disable RSpec exposing methods globally on `Module` and `main`
9
+ config.disable_monkey_patching!
10
+
11
+ config.expect_with :rspec do |c|
12
+ c.syntax = :expect
13
+ end
14
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'on_container/step_down_from_root'
4
+
5
+ RSpec.describe OnContainer::StepDownFromRoot do
6
+ let(:root_user) do
7
+ instance_double "struct Etc::Passwd",
8
+ name: 'root',
9
+ passwd: 'x',
10
+ uid: 0,
11
+ gid: 0,
12
+ gecos: 'root',
13
+ dir: '/root',
14
+ shell: '/bin/bash'
15
+ end
16
+
17
+ let(:developer_user) do
18
+ instance_double "struct Etc::Passwd",
19
+ name: 'developer',
20
+ passwd: 'x',
21
+ uid: 1000,
22
+ gid: 1000,
23
+ gecos: 'Developer User,,,',
24
+ dir: '/usr/src',
25
+ shell: '/bin/bash'
26
+ end
27
+
28
+ let(:example_current_user) { root_user }
29
+ let(:example_target_user) { developer_user }
30
+ let(:example_developer_uid) { '1000' }
31
+
32
+ before do
33
+ allow(ENV).to receive(:fetch).with('DEVELOPER_UID', '') { example_developer_uid }
34
+ allow(Etc).to receive(:getpwuid) { example_current_user }
35
+ allow(Etc).to receive(:getpwuid).with(example_target_user.uid) { example_target_user }
36
+ allow(Kernel).to receive(:exec).with('su-exec', example_target_user.name, any_args)
37
+ end
38
+
39
+ describe '#perform' do
40
+ it 'changes to the target user' do
41
+ subject.perform
42
+ expect(Kernel).to have_received(:exec).with 'su-exec', example_target_user.name, any_args
43
+ end
44
+
45
+ context 'without a developer uid' do
46
+ let(:example_developer_uid) { '' }
47
+
48
+ example 'does not change the current user' do
49
+ subject.perform
50
+ expect(Kernel).not_to have_received(:exec)
51
+ end
52
+ end
53
+
54
+ context 'when not as root' do
55
+ let(:example_current_user) { developer_user }
56
+
57
+ example 'does not change the current user' do
58
+ subject.perform
59
+ expect(Kernel).not_to have_received(:exec)
60
+ end
61
+ end
62
+ end
63
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: on_container
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Quintanilla
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-17 00:00:00.000000000 Z
11
+ date: 2020-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,9 +38,19 @@ files:
38
38
  - README.md
39
39
  - Rakefile
40
40
  - lib/on_container.rb
41
+ - lib/on_container/dev/active_record_ops.rb
42
+ - lib/on_container/dev/bundler_ops.rb
43
+ - lib/on_container/dev/container_command_ops.rb
44
+ - lib/on_container/dev/node_modules_ops.rb
45
+ - lib/on_container/dev/rails.rb
46
+ - lib/on_container/dev/rails_ops.rb
47
+ - lib/on_container/dev/setup_ops.rb
41
48
  - lib/on_container/step_down_from_root.rb
42
49
  - lib/on_container/version.rb
43
50
  - on_container.gemspec
51
+ - spec/on_container_spec.rb
52
+ - spec/spec_helper.rb
53
+ - spec/step_down_from_root_spec.rb
44
54
  homepage: https://github.com/IcaliaLabs/on-container-for-ruby
45
55
  licenses:
46
56
  - MIT
@@ -64,9 +74,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
74
  - !ruby/object:Gem::Version
65
75
  version: '0'
66
76
  requirements: []
67
- rubygems_version: 3.0.3
77
+ rubyforge_project:
78
+ rubygems_version: 2.5.2.3
68
79
  signing_key:
69
80
  specification_version: 4
70
81
  summary: A small collection of scripts and routines to help ruby development within
71
82
  containers
72
- test_files: []
83
+ test_files:
84
+ - spec/on_container_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/step_down_from_root_spec.rb