Module: CPEE::ModelManagement

Defined in:
lib/cpee-model-management/implementation.rb

Defined Under Namespace

Classes: AbandonUrl, Create, CreateDir, DeleteDir, DeleteItem, GetItem, GetList, GetListFull, GetStages, InstanceGet, InstancesGet, ManagementSend, MoveItem, OpenItem, PutItem, RenameDir, RenameItem, ShiftItem, ShowUrl, StatGet, StatReceive, StatSend

Constant Summary collapse

SERVER =
File.expand_path(File.join(__dir__,'moma.xml'))

Class Method Summary collapse

Class Method Details

.fs_cp(models, old, new) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/cpee-model-management/implementation.rb', line 84

def self::fs_cp(models,old,new)
  fname = File.join(models,old)
  fnname = File.join(models,new)
  FileUtils.cp(fname,fnname)
  File.delete(fname + '.active',fnname + '.active') rescue nil
  File.delete(fname + '.active-uuid',fnname + '.active-uuid') rescue nil
  FileUtils.cp(fname + '.attrs',fnname + '.attrs') rescue nil
end

.fs_mv(models, old, new) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/cpee-model-management/implementation.rb', line 76

def self::fs_mv(models,old,new)
  fname = File.join(models,old)
  fnname = File.join(models,new)
  FileUtils.mv(fname,fnname) rescue nil
  File.delete(fname + '.active',fnname + '.active') rescue nil
  File.delete(fname + '.active-uuid',fnname + '.active-uuid') rescue nil
  FileUtils.mv(fname + '.attrs',fnname + '.attrs') rescue nil
end

.fs_rm(models, new) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/cpee-model-management/implementation.rb', line 92

def self::fs_rm(models,new)
  fname = File.join(models,new)
  FileUtils.rm_rf(fname)
  File.delete(fname + '.active') rescue nil
  File.delete(fname + '.active-uuid') rescue nil
  File.delete(fname + '.attrs') rescue nil
end

.fs_shift(models, new) ⇒ Object



99
100
101
102
103
# File 'lib/cpee-model-management/implementation.rb', line 99

def self::fs_shift(models,new)
  fname = File.join(models,new)
  File.delete(fname + '.active') rescue nil
  File.delete(fname + '.active-uuid') rescue nil
end

.get_dn(dn) ⇒ Object

}}}



152
153
154
155
156
157
158
# File 'lib/cpee-model-management/implementation.rb', line 152

def self::get_dn(dn) #{{{
  if dn
    dn.split(',').map{ |e| e.split('=',2) }.to_h
  else
    { 'GN' => 'Christine', 'SN' => 'Ashcreek' }
  end
end

.git_commit(models, new, author) ⇒ Object



71
72
73
74
75
# File 'lib/cpee-model-management/implementation.rb', line 71

def self::git_commit(models,new,author)
  rp = File.realpath(models)
  crb = File.join(__dir__,'commit.rb')
  system 'ruby ' + crb + ' ' + [rp, new, author].shelljoin + ' &'
end

.git_dir(models, file) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/cpee-model-management/implementation.rb', line 105

def self::git_dir(models,file)
  return nil if file.nil?
  cdir = Dir.pwd
  tdir = File.dirname(file)
  Dir.chdir(File.join(models,tdir))
  res = `git rev-parse --absolute-git-dir 2>/dev/null`
  Dir.chdir(cdir)
  res == '' ? nil : res
end

.git_mv(models, old, new) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cpee-model-management/implementation.rb', line 36

def self::git_mv(models,old,new)
  cdir = Dir.pwd
  Dir.chdir(File.join(models,File.dirname(old)))
  p1 = Pathname.new(File.dirname(old))
  p2 = Pathname.new(File.dirname(new))
  told = File.basename(old)
  tnew = File.join(p1.relative_path_from(p1).to_s,File.basename(new))
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple mv     "#{told}"                      "#{tnew}"            2>/dev/null`
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{told + '.active'}"                               2>/dev/null`
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{told + '.active-uuid'}"                          2>/dev/null`
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple mv     "#{told + '.attrs'}"          "#{tnew + '.attrs'}"  2>/dev/null`
  Dir.chdir(cdir)
  CPEE::ModelManagement::fs_mv(models,old,new) # fallback
end

.git_rm(models, new) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cpee-model-management/implementation.rb', line 50

def self::git_rm(models,new)
  cdir = Dir.pwd
  Dir.chdir(File.join(models,File.dirname(new)))
  tnew = File.basename(new)
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{tnew}" 2>/dev/null`
   FileUtils.rm_rf(tnew)
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{tnew}.active"      2>/dev/null`
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{tnew}.active-uuid" 2>/dev/null`
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{tnew}.attrs"      2>/dev/null`
  Dir.chdir(cdir)
  CPEE::ModelManagement::fs_rm(models,new) # fallback
end

.git_shift(models, new) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/cpee-model-management/implementation.rb', line 62

def self::git_shift(models,new)
  cdir = Dir.pwd
  Dir.chdir(File.join(models,File.dirname(new)))
  nnew = File.basename(new)
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{nnew}.active"      2>/dev/null`
  `git -c user.name='Christine Ashcreek' -c [email protected] -c push.default=simple rm -rf "#{nnew}.active-uuid" 2>/dev/null`
  Dir.chdir(cdir)
  CPEE::ModelManagement::fs_shift(models,new) # fallback
end

.implementation(opts) ⇒ Object



860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File 'lib/cpee-model-management/implementation.rb', line 860

def self::implementation(opts)
  opts[:management_receivers] = []
  opts[:stat_receivers] = []

  ### set redis_cmd to nil if you want to do global
  ### at least redis_path or redis_url and redis_db have to be set if you do global
  opts[:redis_path]                 ||= 'redis.sock' # use e.g. /tmp/redis.sock for global stuff. Look it up in your redis config
  opts[:redis_db]                   ||= 0
  ### optional redis stuff
  opts[:redis_url]                  ||= nil
  opts[:redis_cmd]                  ||= 'redis-server --port 0 --unixsocket #redis_path# --unixsocketperm 600 --pidfile #redis_pid# --dir #redis_db_dir# --dbfilename #redis_db_name# --databases 1 --save 900 1 --save 300 10 --save 60 10000 --rdbcompression yes --daemonize yes'
  opts[:redis_pid]                  ||= 'redis.pid' # use e.g. /var/run/redis.pid if you do global. Look it up in your redis config
  opts[:redis_db_name]              ||= 'redis.rdb' # use e.g. /var/lib/redis.rdb for global stuff. Look it up in your redis config

  CPEE::redis_connect opts, 'Server Main'

  opts[:sse_keepalive_frequency]    ||= 10

  Proc.new do

    parallel do
      EM.add_periodic_timer(opts[:sse_keepalive_frequency]) do
        opts[:management_receivers].each do |sse|
          sse.send_with_id('heartbeat', '42') unless sse&.closed?
        end
        opts[:stat_receivers].each do |sse|
          sse.send_with_id('heartbeat', '42') unless sse&.closed?
        end
      end
    end

    interface 'events' do
      run StatReceive, opts[:redis], opts[:stat_receivers] if post 'event'
    end

    interface 'implementation' do
      run GetList, :main, opts[:views], opts[:models] if get 'stage'
      run GetListFull, opts[:views], opts[:models] if get 'full'
      run GetStages, opts[:themes] if get 'stages'
      run Create, :main, opts[:views], opts[:management_receivers], opts[:templates], opts[:models] if post 'item'
      run CreateDir, :main, opts[:management_receivers], opts[:models] if post 'dir'
      on resource 'management' do
        run ManagementSend, opts[:management_receivers] if sse
      end
      on resource '[a-zA-Z0-9öäüÖÄÜ _-]+\.dir' do
        run GetList, :sub, opts[:views], opts[:models] if get 'stage'
        run Create, :sub, opts[:views], opts[:management_receivers], opts[:templates], opts[:models] if post 'item'
        run DeleteDir, opts[:management_receivers], opts[:models] if delete
        run RenameDir, opts[:management_receivers], opts[:models] if put 'name'
        run CreateDir, :sub, opts[:management_receivers], opts[:models] if post 'dir'
        on resource '[a-zA-Z0-9öäüÖÄÜ _-]+\.xml' do
          run DeleteItem, :sub, opts[:management_receivers], opts[:models] if delete
          run GetItem, :sub, opts[:models] if get
          run PutItem, :sub, opts[:management_receivers], opts[:models] if put 'content'
          run RenameItem, :sub, opts[:management_receivers], opts[:models] if put 'name'
          run MoveItem, :sub, opts[:management_receivers], opts[:models] if put 'movedir'
          run Create, :sub, opts[:views], opts[:management_receivers], opts[:templates], opts[:models] if put 'dupdir'
          run ShiftItem, :sub, opts[:management_receivers], opts[:themes], opts[:models] if put 'newstage'
          on resource 'open' do
            run OpenItem, :sub, opts[:instantiate], opts[:cockpit], opts[:views], false, opts[:models] if get 'stage'
          end
          on resource 'open-new' do
            run OpenItem, :sub, opts[:instantiate], opts[:cockpit], opts[:views], true, opts[:models] if get 'stage'
          end
        end
      end
      on resource '[a-zA-Z0-9öäüÖÄÜ _-]+\.xml' do
        run DeleteItem, :main, opts[:management_receivers], opts[:models] if delete
        run GetItem, :main, opts[:models] if get
        run PutItem, :main, opts[:management_receivers], opts[:models] if put 'content'
        run RenameItem, :main, opts[:management_receivers], opts[:models] if put 'name'
        run MoveItem, :main, opts[:management_receivers], opts[:models] if put 'movedir'
        run Create, :main, opts[:views], opts[:management_receivers], opts[:templates], opts[:models] if put 'dupdir'
        run ShiftItem, :main, opts[:management_receivers], opts[:themes], opts[:models] if put 'newstage'
        on resource 'open' do
          run OpenItem, :main, opts[:instantiate], opts[:cockpit], opts[:views], false, opts[:models] if get 'stage'
        end
        on resource 'open-new' do
          run OpenItem, :main, opts[:instantiate], opts[:cockpit], opts[:views], true, opts[:models] if get 'stage'
        end
      end
      on resource 'dash' do
        on resource 'show' do
          run ShowUrl, opts[:show] if get 'url'
        end
        on resource 'abandon' do
          run AbandonUrl if put 'url'
        end
        on resource 'events' do
          run StatSend, opts[:stat_receivers] if sse
        end
        on resource 'instances' do
          run InstancesGet, opts[:redis] if get 'engine'
          on resource do
            run InstanceGet, opts[:redis] if get 'engine'
          end
        end
        on resource 'stats' do
          run StatGet, opts[:redis] if get 'engine'
        end
      end
    end
  end
end

.notify(conns, op, models, f, s = nil) ⇒ Object

}}}



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cpee-model-management/implementation.rb', line 159

def self::notify(conns,op,models,f,s=nil) #{{{
  what = if f =~ /\.dir$/
    if  op == 'delete'
      { :op => op, :type => :dir, :name => File.basename(f) }
    else
      attrs = JSON::load File.open(f + '.attrs')
      { :op => op, :type => :dir, :name => File.basename(f), :creator => attrs['creator'], :date => File.mtime(f).xmlschema }
    end
  else
    if  op == 'delete'
      { :op => op, :type => :file, :name => f.sub(Regexp.compile(File.join(models,'/')),'') }
    else
      attrs = JSON::load File.open(f + '.attrs')
      fstage = attrs['design_stage'] rescue 'draft'
      { :op => op, :type => :file, :name => f.sub(Regexp.compile(File.join(models,'/')),''), :creator => attrs['creator'], :author => attrs['author'], :stage => fstage, :date => File.mtime(f).xmlschema }
    end
  end
  what[:source] = s.sub(/models\//,'') unless s.nil?
  conns.each do |e|
    e.send what
  end
end

.op(author, op, models, new, old = nil) ⇒ Object

{{{



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cpee-model-management/implementation.rb', line 115

def self::op(author,op,models,new,old=nil) #{{{
  git_ndir = CPEE::ModelManagement::git_dir(models,new)
  git_odir = CPEE::ModelManagement::git_dir(models,old)

  if op == 'rm' && !git_ndir.nil?
    git_rm models, new
    git_commit models, new, author
  elsif op == 'shift' && !git_ndir.nil?
    git_shift models, new
    git_commit models, new, author
  elsif op == 'mv' && (!git_ndir.nil? || !git_odir.nil?)
    if git_ndir == git_odir
      git_mv models, old, new
      git_commit models, new, author
    elsif git_ndir != git_odir && !git_ndir.nil? && !git_odir.nil?
      fs_cp models, old, new
      git_rm models, old
      git_commit models, old, author
      git_commit models, new, author
    elsif git_ndir != git_odir && git_ndir.nil?
      fs_cp models, old, new
      git_rm models, old
      git_commit models, old, author
    elsif git_ndir != git_odir && git_odir.nil?
      fs_mv models, old, new
      git_commit models, new, author
    end
  elsif !git_ndir.nil?
    git_commit models, new, author
  else
    case op
      when 'mv'; fs_mv(models,old,new)
      when 'rm'; fs_rm(models,new)
      when 'shift'; fs_shift(models,new)
    end
  end
end