Skip to content

Commit b4529d1

Browse files
committed
WIP
1 parent f0b0fd1 commit b4529d1

File tree

9 files changed

+197
-1
lines changed

9 files changed

+197
-1
lines changed

api/Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ gem 'rails', '~> 7.1.3'
66

77
gem 'aws-sdk-s3'
88
gem 'base62-rb'
9+
gem 'bcrypt_pbkdf'
910
gem 'bootsnap', require: false
11+
gem 'ed25519'
1012
gem 'faraday'
1113
gem 'faraday-multipart'
1214
gem 'jb'
1315
gem 'metabobank_tools', github: 'ddbj/metabobank_tools'
16+
gem 'net-ssh'
1417
gem 'noodles_gff', path: '../noodles_gff-rb'
1518
gem 'openid_connect'
1619
gem 'pagy'

api/Gemfile.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ GEM
123123
aws-eventstream (~> 1, >= 1.0.2)
124124
base62-rb (0.3.1)
125125
base64 (0.2.0)
126+
bcrypt_pbkdf (1.1.1)
127+
bcrypt_pbkdf (1.1.1-arm64-darwin)
126128
bigdecimal (3.1.8)
127129
bindata (2.5.0)
128130
bootsnap (1.18.3)
@@ -142,6 +144,7 @@ GEM
142144
reline (>= 0.3.8)
143145
diff-lcs (1.5.1)
144146
drb (2.2.1)
147+
ed25519 (1.3.0)
145148
email_validator (2.2.4)
146149
activemodel
147150
erubi (1.13.0)
@@ -213,6 +216,7 @@ GEM
213216
timeout
214217
net-smtp (0.5.0)
215218
net-protocol
219+
net-ssh (7.2.3)
216220
nio4r (2.7.3)
217221
nokogiri (1.16.6-aarch64-linux)
218222
racc (~> 1.4)
@@ -398,14 +402,17 @@ PLATFORMS
398402
DEPENDENCIES
399403
aws-sdk-s3
400404
base62-rb
405+
bcrypt_pbkdf
401406
bootsnap
402407
climate_control
403408
debug
409+
ed25519
404410
factory_bot_rails
405411
faraday
406412
faraday-multipart
407413
jb
408414
metabobank_tools!
415+
net-ssh
409416
noodles_gff!
410417
openid_connect
411418
pagy

api/app/models/database/dra.rb

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,54 @@ def validate(validation)
5050

5151
class Submitter
5252
def submit(submission)
53-
# do nothing
53+
submitter_db = Dway.submitter_db
54+
drmdb = Dway.drmdb
55+
56+
submitter_id = submission.validation.user.uid
57+
user_id = submitter_db[:login].where(submitter_id:).get(:usr_id)
58+
59+
drmdb.transaction isolation: :serializable do
60+
serial = (drmdb[:submission].where(submitter_id:).max(:serial) || 0) + 1
61+
62+
sub_id = drmdb[:submission].insert(
63+
usr_id: user_id,
64+
submitter_id: ,
65+
serial: ,
66+
create_date: Date.current
67+
)
68+
69+
submission_id = "#{submitter_id}-#{serial.to_s.rjust(4, '0')}"
70+
71+
drmdb[:status_history].insert(
72+
sub_id: ,
73+
status: 100 # SubmissionStatus.NEW
74+
)
75+
76+
drmdb[:operation_history].insert(
77+
type: 3, # LogLevel.INFO
78+
summary: 'Status update to new',
79+
usr_id: user_id,
80+
serial: ,
81+
submitter_id:
82+
)
83+
84+
ext_id = drmdb[:ext_entity].insert(
85+
acc_type: 'DRA',
86+
ref_name: submission_id,
87+
status: 0 # ExtStatus.INPUTTING
88+
)
89+
90+
drmdb[:ext_permit].insert(
91+
ext_id: ,
92+
submitter_id:
93+
)
94+
95+
host, user, key_data = ENV.values_at('DRA_SSH_HOST', 'DRA_SSH_USER', 'DRA_SSH_KEY_DATA')
96+
97+
Net::SSH.start host, user, key_data: [key_data] do |ssh|
98+
ssh.exec! "sudo /usr/local/sbin/chroot-createdir.sh #{submitter_id} #{submission_id}"
99+
end
100+
end
54101
end
55102
end
56103
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Database::DRA::Submitter, type: :model do
4+
example do
5+
submission = create(:submission, {
6+
validation: build(:validation, :valid, {
7+
user: build(:user, uid: 'alice')
8+
})
9+
})
10+
11+
user_id = Dway.submitter_db[:login].insert(
12+
submitter_id: 'alice',
13+
password: 'password',
14+
)
15+
16+
expect(Net::SSH).to receive(:start)
17+
18+
Database::DRA::Submitter.new.submit submission
19+
20+
expect(Dway.drmdb[:submission].count).to eq(1)
21+
22+
dway_submission = Dway.drmdb[:submission].first
23+
expect(dway_submission).to include(usr_id: user_id, submitter_id: 'alice', serial: 1)
24+
25+
expect(Dway.drmdb[:status_history].count).to eq(1)
26+
27+
status_history = Dway.drmdb[:status_history].first
28+
expect(status_history).to include(sub_id: dway_submission[:sub_id], status: 100)
29+
30+
expect(Dway.drmdb[:operation_history].count).to eq(1)
31+
32+
operation_history = Dway.drmdb[:operation_history].first
33+
expect(operation_history).to include(type: 3, summary: 'Status update to new', usr_id: user_id, serial: 1, submitter_id: 'alice')
34+
35+
expect(Dway.drmdb[:ext_entity].count).to eq(1)
36+
37+
ext_entity = Dway.drmdb[:ext_entity].first
38+
expect(ext_entity).to include(acc_type: 'DRA', ref_name: 'alice-0001', status: 0)
39+
40+
expect(Dway.drmdb[:ext_permit].count).to eq(1)
41+
42+
ext_permit = Dway.drmdb[:ext_permit].first
43+
expect(ext_permit).to include(ext_id: ext_entity[:ext_id], submitter_id: 'alice')
44+
end
45+
end

dra_validator/Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'bcrypt_pbkdf'
4+
gem 'ed25519'
5+
gem 'net-sftp', require: 'net/sftp'
6+
gem 'pg'
7+
gem 'sequel'

dra_validator/Gemfile.lock

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
bcrypt_pbkdf (1.1.1)
5+
bigdecimal (3.1.8)
6+
ed25519 (1.3.0)
7+
net-sftp (4.0.0)
8+
net-ssh (>= 5.0.0, < 8.0.0)
9+
net-ssh (7.2.3)
10+
pg (1.5.6)
11+
sequel (5.81.0)
12+
bigdecimal
13+
14+
PLATFORMS
15+
ruby
16+
x86_64-linux
17+
18+
DEPENDENCIES
19+
bcrypt_pbkdf
20+
ed25519
21+
net-sftp
22+
pg
23+
sequel
24+
25+
BUNDLED WITH
26+
2.5.13

dra_validator/Rakefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace :db do
2+
desc 'Run migrations'
3+
task :migrate, [:version] do |t, args|
4+
require 'sequel/core'
5+
6+
Sequel.extension :migration
7+
8+
Sequel.connect ENV.fetch('DATABASE_URL') do |db|
9+
Sequel::Migrator.run db, 'db/migrations', target: args[:version]&.to_i
10+
end
11+
end
12+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Sequel.migration do
2+
change do
3+
create_table :submission do
4+
primary_key :sub_id, type: :bigint
5+
6+
bigint :usr_id, null: false
7+
text :submitter_id, null: false
8+
integer :serial, null: false
9+
integer :charge
10+
date :create_date
11+
date :submit_date
12+
date :hold_date
13+
date :dist_date
14+
date :finish_date
15+
text :note
16+
end
17+
end
18+
end

dra_validator/main.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'bundler/setup'
2+
3+
Bundler.require
4+
5+
# DB = Sequel.connect(ENV.fetch('DATABASE_URL'))
6+
7+
# submissions = DB[:submission]
8+
9+
# submissions.insert usr_id: 42, submitter_id: '42', serial: 42
10+
11+
# ディレクトリ名を引数で受け取れるようにする
12+
# 受け取った引数を元に再帰的にディレクトリを作成するメソッドを一つ用意する
13+
# そのメソッドの中で例外処理をする
14+
15+
def mkdir_p!(sftp, dir)
16+
dir_names = dir.split('/')
17+
18+
dir_names.size.times.map {|i|
19+
dir_names[0..i].join('/')
20+
}.each do |dir_name|
21+
begin
22+
sftp.mkdir! dir_name
23+
rescue Net::SFTP::StatusException => e
24+
raise unless e.code == 4
25+
end
26+
end
27+
end
28+
29+
Net::SFTP.start 'localhost', 'maimu', key_data: [File.read('/Users/maimu/.ssh/id_ed25519_no_passphrase')] do |sftp|
30+
mkdir_p!(sftp, 'Documents/upload/foo/bar')
31+
end

0 commit comments

Comments
 (0)